Repository navigation
fix(mtp): 修复 CP 下 MTP 有效 token 归一化 - #488
Conversation
|
Thanks for contributing to Relax, @Yangruipis! 感谢你为 Relax 做出贡献! 📚 Contribution guide / 贡献指南Describe the problem, your changes, and how you validated them. Keep each PR focused and run 请说明问题、改动和验证方式,保持 PR 聚焦,并在提交前运行 🛠️ CI commands / CI 指令
Put one command on the first line of a new PR comment. Rerun/cancel require PR authorship or repository write access. 在新 PR 评论的首行写一条指令。PR 作者或有仓库写权限的贡献者可以重跑、取消 CI。 🔎 Merge requirements / 合入条件GitHub:⏳ 合入条件未满足
|
Nyanpasu 审查看板审查状态: ✅ 已通过 审查版本: 334db7f 目标分支: main 已完成 334db7f 的跟进审查,未发现需要修改的问题。新基线未改变 MTP 的 mask 坐标、CP 分组、空 rank 图连接或最终 token 归一化;前次 cp2 测试修复和已有回归证据保持适用。
没有未解决的审查问题。 提交范围 · 接收 7 · 建议移出 0 · 待确认 0接收 7 个文件 · 建议移出 0 个文件 · 待确认 0 个文件。移出与待确认部分暂停深审,不代表审查通过。
Powered by Nyanpasu with gpt-6.1-sol xhigh, please check the suggestions carefully.
|
- Sum valid MTP token counts across CP before normalizing native and chunked auxiliary losses. - Preserve main-update-token normalization, static chunked dispatch and the current tracker reporting path. - Correct CP loss reporting and apply the companion MCore patch in both CUDA image builds. - Reproduce the unpatched CP2 gradient mismatch with CPU/Gloo. - Pass 31 targeted regression tests, including unequal masks, empty ranks, all-zero masks and teacher masks. - Verify the native patch against the pinned MCore patch sequence with zero fuzz. - Pass all-files pre-commit. GPU and end-to-end training were not run.
79ebc34 to
1a87434
Compare
|
@codex review following the repository AGENTS.md and skills/code-review/SKILL.md |
Codex Review SummaryThis comment shows the latest Codex review activity on this pull request.
ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings. |
|
Codex Review: Something went wrong. Try again later by commenting “@codex review”. ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
If Codex has suggestions, it will comment; otherwise it will react with 👍. Codex can also answer questions or update the PR. Try commenting "@codex address that feedback". |
Keep the native normalization helper active while simulating peer counts for the fake CP group. Assert both MTP depths reduce on that group, and skip the CP case on older MCore builds without the normalization patch.
Keep both MTP CP normalization and DP-reshardable checkpoint patches in the CUDA 13 image, including main branch checksum checks and cleanup. Validation: full pre-commit checks pass; replayed both affected Megatron files from the pinned revision with zero fuzz and verified optimizer hashes.
## What The ROCm image (gfx950/gfx942), the en/zh ROCm guide plus the docker/README.md section, and the cuda_ipc fix for a ROCm regression on main since #495. ## How - Image: how it matches Dockerfile.cu13 (SGLang patch, Rust tree core, router, Megatron patches including #488's), plus the prebuilt wheels, the requirements handling and the runtime environment variables. - SGLang patch: the one remaining fix, linked to sgl-project/sglang#44266, with a note that it can be dropped once Relax upgrades past that. - Transport fix: how the cuda_ipc default is skipped on ROCm. ## Testing - End-to-end colocated GRPO on 8× MI350X (`gfx950` image): | Task | Rollouts | Result | `mismatch_kl` | | --- | --- | --- | --- | | Qwen3-4B DAPO-Math (`run-qwen3-4B-8xgpu.sh`) | 3 | Succeeded; weight update 3.9 s, step ≈ 159 s | 6.2e-4 – 1.0e-3 | | Qwen3-VL-4B Open-R1 (`run-qwen3-vl-4B-8xgpu.sh`) | 3 | Succeeded; reward 0.36 → 0.52, weight update 4.3 s | 6.7e-4 – 7.2e-4 | | Qwen3-Omni-30B-A3B AVQA (16-GPU recipe on one node, EP 4) | 450 | Held-out AVQA accuracy 0.750 → 0.884; reward 0.81 → 0.95 (first/last 50 rollouts) | 3.0e-3 – 6.2e-3 | - [x] `pre-commit run --all-files` passes - [ ] Tests pass (`pytest tests/`) - [x] New tests added (if applicable) - [x] Documentation updated (if applicable) ## Type of Change - [ ] Bug fix (non-breaking change that fixes an issue) - [x] New feature (non-breaking change that adds functionality) - [ ] Breaking change (fix or feature that would cause existing functionality to change) - [x] Documentation update - [ ] Refactoring (no functional changes) - [ ] Performance improvement - [ ] CI/CD or build changes ## Screenshots / Logs <!-- If applicable, add screenshots or log output to help explain the changes. --> --------- Co-authored-by: Nyakku Shigure <[email protected]> Co-authored-by: Codex <[email protected]>
What
修复 Context Parallel(CP)下 MTP 辅助 loss 的有效 token 归一化,使 native 和 sequence-chunked 两条路径使用同一套 CP 组计数,避免各 rank 的有效监督数量不一致时,loss 和梯度随 CP 切分方式发生偏移。
同时调整 chunked 路径的 loss 日志口径,并在 CUDA 12、CUDA 13 两个镜像构建入口安装配套 MCore 补丁。
Why
CP 会将序列分到多个 rank,但各 rank 真正参与 MTP 监督的 token 数并不一定相同:
原先使用本 rank 的有效 token 数作为分母,再经过 CP 梯度平均,相当于对“每个 rank 的均值”取平均,没有按有效 token 数正确加权。
例如,两张卡分别有 6 个、2 个有效 token,局部平均 loss 分别为 10、2:
该问题也会影响 MTP 辅助梯度;当某个 rank 完全没有有效 token 时,简单 clamp 本地分母只能避免除零,不能恢复正确的全局权重。
How
1. 在 CP 组内统一计数
新增 MCore 配套补丁
docker/patch/megatron/mtp-cp-normalization.patch,提供共享计数函数:cp_group执行 all-reduce;本地有效 token 数为零的 rank 同样参加 collective。计数只在 CP 组内汇总,不将不同 DP 副本混成同一个 microbatch 的分母。
2. 按既有训练模式修正缩放
令
C为 CP 大小,N_d为当前预测深度的全 CP 有效 token 数,N_0为移动前的全 CP 有效 token 数,α = mtp_loss_scaling_factor / mtp_num_layers。每个 rank 注入的辅助 loss 为:α × C × local_loss / max(N_d, 1)α × local_loss × N_0 / max(N_d, 1)main_update_tokensα × local_loss修复保留现有的 microbatch/update 归一化目标,不额外改变跨 microbatch 的训练加权方式。全局有效 token 数为零时,仅 clamp 分母,辅助 loss 和梯度仍为零。
3. 对齐 native、chunked 与日志路径
Testing
CUDA_VISIBLE_DEVICES="" OMP_NUM_THREADS=1 python -m pytest \ tests/backends/megatron/test_mtp_cp_normalization.py \ tests/backends/megatron/test_chunked_mtp_loss.py \ tests/backends/megatron/test_mtp_tracker_reporting.py -q新增双进程测试使用真实的 loss、cross-entropy、MTP 辅助梯度注入和 tracker;CPU roll adapter 交换序列分片并处理已知 packed 边界。它将 CP2 的参数梯度、hidden-state 梯度和日志 loss,与未切分参考逐项比较,覆盖:
main_update_tokens模式。还验证了固定 MCore 源码与现有补丁序列上,新补丁可用
patch --batch --forward --fuzz=0应用,静态 chunked dispatch 保留,结果可通过 Python 编译。本次未运行 GPU、多节点或端到端训练,也未完整重建镜像;验证范围为 CPU/Gloo 数值回归和原生补丁接入检查。
pre-commit run --all-filespassespytest tests/)Type of Change
Screenshots / Logs
修复前的 CP2 数值回归失败,修复后通过;零有效 token 场景保持有限值及零辅助梯度,原有 CP1 等价性和 tracker 日志回归通过。