当前位置:   article > 正文

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

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

多模态大模型学习笔记

吐槽

想要做一个以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, logger

class MQWenModel(QWenModel):
    def __init__(self, config, otherConfig):
        super().__init__(config)

        self.otherConfig = otherConfig

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8

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,
    ):
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17

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.device
        first_step = False
        if 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.device
        first_step = False
        if 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 text

            input_ids = torch.stack(new_input_ids, dim = 0).to(input_ids)
            first_step = True
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/小惠珠哦/article/detail/1010237
推荐阅读
相关标签
  

闽ICP备14008679号