Model accuracy alignment#1
Open
zhengshengning wants to merge 7 commits into
Open
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
问题1: sequentialMLP 权重代理
问题描述: SequentialMLP 缺少 weight{i} 接口,导致权重访问不兼容 GroupedMLP。
修复文件: Megatron-LM/megatron/core/transformer/moe/experts.py
问题2: 禁用 TE 配置 (ms-swift前置修改)
问题描述: 为了使拆分后的独立 LayerNorm 模块正常工作,需要禁用 Transformer Engine 融合算子。
修复文件: ms-swift-test/swift/megatron/model/register.py
问题3: 不添加任何后缀token (ms-swift前置修改)
修复文件: swift/template/templates/utils.py
问题4: 保留TopKRouter 偏差(expert_bias 被 Float16Module 截断成 bf16)
描述:MG 的 TopKRouter 把 expert_bias 注册成 fp32 buffer,但 Float16Module 在 module.bfloat16() 时把它一起 cast 成 bf16。routing 的 topk 是离散选择,对 bias 末位敏感,bf16 截断直接让 scores_for_choice → topk → probs/routing_map 全部分叉。
修改:改在 Megatron-LM/megatron/core/transformer/module.py 的 Float16Module.init:
问题5: Router TE GEMM 精度差异
问题描述: MoE Router 的 logits 计算在 MG 和 PF 之间存在差异,输入和权重完全一致但输出不同。
问题6: CrossEntropyLoss 精度差异
【修复的问题描述】:Megatron 默认走
tensor_parallel.vocab_parallel_cross_entropy,在 TP=1 场景下其内部累加顺序与 paddlenn.CrossEntropyLoss/F.cross_entropy不一致,导致两框架 LM loss 末位存在 diff。此处在 TP=1 且 logits 未做 vocab 切分时切换到torch.nn.functional.cross_entropy,与 Paddle 侧 CE 路径对齐。问题7: MoE unpermute 阶段 scatter_add_ 在 bf16 下存在随机性
【修复的问题描述】:MoE unpermute 阶段
scatter_add_在 bf16 下走 atomic 累加,多 expert 输出回写到同一 token 行时累加顺序不可复现,与 PaddleFleet 末位 diff。在 unpermute(无 probs、非 drop_and_pad)的标准路径上改走 fp32 累加。问题8: SwiGLU 数值路径不一致(bias_activation_fusion=false情况)
【修复的问题描述】:MoE expert 内 SwiGLU 与 router prob 相乘的计算精度对齐。GLU 已在 fp32 下计算,这里把 per_token_scale 也 cast 到 fp32 相乘,最后一次性 cast 回原始 bf16 dtype,对齐 PaddleFleet fused_swiglu_scale「fp32 激活 × fp32 prob → 单次 bf16 round」的数值路径。