问题:
- 模型会 attend 到所有 padding token
- Causal attention 会看到未来的 padding
- 导致模型学习错误的 attention 模式
Sample
Epoch1: "当然,我需要您提供帮助,我需要您提供帮助您提供帮助您提供。请提供您提供您您您您您您您您您的..."(崩溃式重复)
Epoch1: "好的,我需要您需要您需要您提供您提供您提供您您您您您您。"
初步分析,不确定是否正确
训练时:
[user_prompt, assistant_response, , , ]
↓ 没有 attention_mask
模型 attend 到所有 token(包括 padding)
↓
梯度被 padding 污染
↓
模型学会"忽略输入",输出高频模板
❌ 1. Attention Mask 缺失
原始代码(train_full_sft.py & train_pretrain.py):
res = model(X) # ⚠️ 只传了 input_ids!
❌ 2. 右侧 Padding + Decoder-Only
原始代码(lm_dataset.py):
input_ids += [pad_token_id] * (max_length - len(input_ids)) # 右padding
问题:
- Decoder-only 模型应该用左padding
- 右padding 会导致生成位置错位
- 训练和推理的 padding 策略不一致
❌ 3. Loss Mask ≠ Attention Mask
logits_loss = (loss * loss_mask).sum() / loss_mask.sum()
问题:
loss_mask 只影响 loss 计算
- 不影响 attention 机制
- 模型在 forward 时依然看到 padding
症状确认
✅ 不同输入 → 相似输出(4% 重复率)
✅ 模型倾向生成通用模板
- "当然,为了提供帮助,我需要您提供..."
- "好的,我为您生成一个..."
✅ 重复循环问题
- Token 生成陷入局部模式
- "我我我我..." / "您您您您..."
问题:
Sample
Epoch1: "当然,我需要您提供帮助,我需要您提供帮助您提供帮助您提供。请提供您提供您您您您您您您您您的..."(崩溃式重复)
Epoch1: "好的,我需要您需要您需要您提供您提供您提供您您您您您您。"
初步分析,不确定是否正确
训练时:
[user_prompt, assistant_response, , , ]
↓ 没有 attention_mask
模型 attend 到所有 token(包括 padding)
↓
梯度被 padding 污染
↓
模型学会"忽略输入",输出高频模板
❌ 1. Attention Mask 缺失
原始代码(
train_full_sft.py&train_pretrain.py):❌ 2. 右侧 Padding + Decoder-Only
原始代码(
lm_dataset.py):问题:
❌ 3. Loss Mask ≠ Attention Mask
问题:
loss_mask只影响 loss 计算症状确认
✅ 不同输入 → 相似输出(4% 重复率)
✅ 模型倾向生成通用模板
✅ 重复循环问题