Prhub

#3219 fix: align MTP loss masks with input tokens before context-parallel slicing

原始 PR 作者 Shi-Dong 合并时间 2026-09-15 04:20 文件变更 4 提交数 4 评论 3 代码增减 +265 / -37

执行摘要

修复 MTP 掩码错位,统一 input-aligned 契约

PR body 明确指出:Miles builds full_loss_masks in next-token/prediction alignment, but calls Megatron with labels=None。Megatron 的 MTP 路径从 input_ids 推导 labels 并“shifts the supplied mask along with them before applying the additional MTP-depth shift”,因此“Passing an already prediction-aligned mask ... advances the mask one token too far relative to the MTP targets”。例如目标监督区间 4–8 会实际选中 3–7,辅助损失作用于错误边界位置。

实现拆解

该变更按以下 4 步落地:

  1. 新增掩码构造函数:在 miles/backends/training_utils/data.py 中新增 _prepare_loss_masks(),把 response 掩码先做 prompt_length 的左补零(去掉了原先 prediction-aligned 的 prompt_length - 1 左补 + 1 右补),得到 mask[i] == 1 iff tokens[i] 是受监督 response token 的 input-aligned 掩码,随后用与 tokens 完全相同的 packing/CP 切片逻辑处理(slice_with_cp、F.pad、chunk(cp_size)),保证在 CP 分片后仍与 tokens 形状一致。

  2. 替换批处理键:get_batch() 中调用 _prepare_loss_masks() 产出 `batch[

文件 模块 状态 重要度
miles/backends/training_utils/data.py miles/backends modified 7.56
tests/fast/backends/megatron_utils/test_mtp_loss_mask_alignment.py mtp/loss/mask/alignment added 7.12
tests/fast/backends/training_utils/test_input_loss_masks.py input/loss/masks added 6.97
miles/backends/megatron_utils/model.py miles/backends modified 5.8

关键符号

_prepare_loss_masks test_actual_mtp_loss_selects_intended_targets output_layer _parallel_state test_mask_alignment_survives_packing_and_cp

分析完成后,这里会展示 LLM 生成的相对完整源码片段和详细注释。

评论区精华

没有提炼出高价值讨论线程

当前评论区没有形成足够清晰的争议点或结论,后续有更多讨论时会体现在这里。

风险与影响

关联 Issue

未识别关联 Issue

当前没有检测到明确关联的 Issue 链接,后续同步到相关引用后会出现在这里。

完整报告

参与讨论