Skip to content

Commit c54408e

Browse files
authored
Merge pull request #13 from BinaryOracle/master
Master
2 parents 29a1431 + cc90770 commit c54408e

File tree

2 files changed

+25
-0
lines changed

2 files changed

+25
-0
lines changed

src/MMLLM/庖丁解牛BLIP2.md

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -582,6 +582,12 @@ class BertEncoder(nn.Module):
582582
# Step 5: 提取语言模型损失
583583
loss_lm = lm_output.loss # 使用交叉熵损失衡量生成与真实之间的差异
584584
```
585+
**文本生成阶段:**
586+
587+
将缓存的 past_key_values 作为文本解码器的初始状态。
588+
589+
文本 token 在自回归生成时,通过 self-attention 复用缓存的视觉信息。
590+
585591
5. BertLMHeadModel: 自回归语言建模任务(如文本生成)
586592

587593
```python
@@ -607,6 +613,7 @@ class BertLMHeadModel(BertPreTrainedModel):
607613
reduction="mean",
608614
):
609615
...
616+
# 调用 BertModel 进行文本编码 (结合缓存的attention key&value)
610617
outputs = self.bert(
611618
input_ids,
612619
attention_mask=attention_mask,
@@ -652,3 +659,21 @@ class BertLMHeadModel(BertPreTrainedModel):
652659
cross_attentions=outputs.cross_attentions,
653660
)
654661
```
662+
> BertModel 的 forward 方法中,当is_decoder=True时,会在get_extended_attention_mask方法中,构建一个下三角矩阵作为因果掩码矩阵。
663+
664+
### Stage 2: Generative Learning(生成学习)
665+
666+
Stage 2 是为了把 Q-Former 和冻结参数的 LLM 连接起来,以利用 LLM 的文本生成能力。
667+
668+
支持两种LLM(decoder only、encoder-decoder based):
669+
670+
![Generative Learning](庖丁解牛BLIP2/11.png)
671+
672+
1. 首先输入图片,直接输入冻结参数的 Image Encoder,得到图像的表征。
673+
674+
2. 然后图像的表征和 Queries 一起送入 Q-Former,得到 Queries 的输出 $Z$ ,使用全连接 (FC) 层将 $Z$ 线性投影到与 LLM 的text embedding相同维度。
675+
676+
3. 后将投影后的 $Z$ 添加到 input text embeddings前面,Queries 的输出蕴含了视觉信息,送入LLM时,充当了soft visual prompts 。
677+
678+
4. 由于 Q-Former 已经过预训练以提取语言信息视觉表示,因此它有效地充当信息瓶颈,将最有用的信息提供给 LLM,同时删除不相关的视觉信息。这减少了LLM学习视觉语言对齐的负担,从而缓解了灾难性的遗忘问题。
679+

src/MMLLM/庖丁解牛BLIP2/11.png

185 KB
Loading

0 commit comments

Comments
 (0)