您的位置:首页 > 财经 > 金融 > 多模态大模型学习笔记 - 1

多模态大模型学习笔记 - 1

2024/10/6 0:34:11 来源:https://blog.csdn.net/FlowerLoveJava/article/details/141288782  浏览:    关键词:多模态大模型学习笔记 - 1

多模态大模型学习笔记

  • 吐槽
  • 今日心得
    • MQwen.py

吐槽

想要做一个以Qwen-7B-Insturct为language decoder, 以CLIP-VIT-14为vision encoder的image captioning模型,找了很多文章和库的源码,但是无奈都不怎么看得懂,刚开始打算直接给language decoder加上cross attention层对接vison encoder的图片编码结果,无奈不会写TAT,看了Qwen的源码半天都没搞懂这么多类是干什么的,心累。今天机缘巧合在Github上刷到一个北邮学生手搓的多模态模型,改了改Qwen的forward方法和其他一些配置,看起来比文献和transformers里的源码简易一些,遂打算好好钻研一下。

今日心得

由于今天找到这个repo的时间段比较晚,所以也没有看太多源码,浅谈一下今天阅读到的源码。

MQwen.py

在这个py文件里重写了Qwen的forward方法,可以看到从当前文件前中导入了QWenLMHeadModel等QWen源码中的类,继承了QWenModel的成员变量和方法,并且重写了QWenModel,初始化传入两个参数,otherConfig应该是自己的参数。

from .modeling_qwen import QWenLMHeadModel, QWenModel, BaseModelOutputWithPast, loggerclass MQWenModel(QWenModel):def __init__(self, config, otherConfig):super().__init__(config)self.otherConfig = otherConfig

forward方法里传入的变量如下:
input_ids:输入序列的索引,将token映射为唯一的整数数字索引
images: 传递入的图像特征
past_key_values:用于存储过去计算得到的键值对,用来加速训练,减少重复计算
attention_mask:没什么好说的,注意力掩码,用来防止信息泄露,指定序列中参与注意力计算的部分
tojken_type_ids:指定不同类型的token
position_ids:老熟人,位置索引,提供token的位置信息
head_mask:和attention_mask相似,用于指定那些头的信息应该被忽略/关注
input_embeds:input_ids编码后的结果
use_cache:指定是否使用缓存的past_key_values加速训练
return_dict:指定返回值的形式是否为字典

def forward(self,input_ids: Optional[torch.LongTensor] = None,images: Optional[torch.Tensor] = None,past_key_values: Optional[Tuple[Tuple[torch.Tensor]]] = None,attention_mask: Optional[torch.FloatTensor] = None,token_type_ids: Optional[torch.LongTensor] = None,position_ids: Optional[torch.LongTensor] = None,head_mask: Optional[torch.FloatTensor] = None,inputs_embeds: Optional[torch.FloatTensor] = None,encoder_hidden_states: Optional[torch.Tensor] = None,encoder_attention_mask: Optional[torch.FloatTensor] = None,use_cache: Optional[bool] = None,output_attentions: Optional[bool] = None,output_hidden_states: Optional[bool] = None,return_dict: Optional[bool] = None,):

device指定我们使用cuda还是cpu, first_step变量判断是否是处理序列数据的第一步。如果提供了图像特征并且past_key_values为None,代表我们在处理一个新序列的开始。用torch.where函数判断输入的input_ids哪些地方应该被替换为图像信息,返回值代表了每个批次中,图像信息所在的列索引。根据列索引去除input_ids中每个批次的image_token。最后通过torch.stack方法重新构建一个去除了image_token的input_ids,至此第一步处理完成。

 device = input_ids.device if input_ids is not None else inputs_embeds.devicefirst_step = Falseif images is not None and past_key_values is None:image_index = torch.where(input_ids == self.otherConfig["replace_token_id"])[1]new_input_ids = []for b_idx, img_idx in enumerate(image_index):device = input_ids.device if input_ids is not None else inputs_embeds.devicefirst_step = Falseif images is not None and past_key_values is None:image_index = torch.where(input_ids == self.otherConfig["replace_token_id"])[1]new_input_ids = []for b_idx, img_idx in enumerate(image_index):new_input_ids.append(torch.cat([input_ids[b_idx][:img_idx], input_ids[b_idx][img_idx+1:]], dim = 0))   #############  concat image and textinput_ids = torch.stack(new_input_ids, dim = 0).to(input_ids)first_step = True

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com