Repository navigation
fix(megatron): cast get_cp_local_num_tokens to torch.int for Megatron int32 token accumulator - #290
Conversation
Both branches (cp_size == 1 and cp_size > 1) returned float32 (loss_mask.sum()), crashing every --calculate-per-token-loss training run at step 0: Megatron schedules.py accumulates `total_num_tokens += num_tokens` on an int tensor, so a float input raises "Result type Float can't be cast to the desired output type Int". Match the accumulator dtype, and compare the normalizer dtype-safely in the rloo dispatch test while pinning the int contract.
|
@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: Didn't find any major issues. Hooray! Reviewed commit: ℹ️ 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". |
# 🔩 Chore - Merge current main into the int32 token-count fix. - Retain the shared CP mask-sum helper, SFT mask alignment, and CP=1 clamping while casting the final count to Megatron's torch.int dtype. - Validate with all-file pre-commit hooks, four focused CPU tests including two/four-process Gloo reductions, and ten isolated CP mask-alignment tests. - Multi-node GPU/Megatron integration was not run because this environment has CPU-only PyTorch and no Megatron installation. Co-authored-by: Codex <[email protected]>
Nyanpasu 审查看板审查状态: ✅ 已通过 审查版本: 9fdd83e 已完成目标提交 9fdd83e 审查,未发现需要修改的问题。本轮新提交仅为合并 main:两个改动文件与上轮已批准的 b71db72 逐字节一致,且已核实 main 侧变更(CP stage 3、repetition 诊断、megatron patch 扩充)不触及本修复的调用链与 int32 累加器契约。CPU eager 模式 4 项 RLOO 回归测试通过,GitHub CI(含 H20 4-GPU 单测与 Qwen3-4B/VL GPU、NPU 异步集成)全部通过。
没有未解决的审查问题。 提交范围 · 接收 2 · 建议移出 0 · 待确认 0接收 2 个文件 · 建议移出 0 个文件 · 待确认 0 个文件。移出与待确认部分暂停深审,不代表审查通过。
精简审查与验证依据
生产代码的必要性与替代方案
测试的必要性与替代方案
Powered by Nyanpasu with glm-5.3[1m] xhigh, please check the suggestions carefully.
|
rai-studio-bot
left a comment
There was a problem hiding this comment.
已审查目标提交 b71db72,未发现需要修改的问题,同意合入。已核对 CP token 计数、损失归一化及标准/streaming 调度器的整数累加契约。
两组 RLOO 回归测试在 CPU eager 模式下共 4 项通过;因本地 Triton 不兼容,验证时关闭了 TorchDynamo。未执行 GPU/多节点集成测试,本地缺少相应运行环境;GitHub CI 仍在运行。
get_cp_local_num_tokens to torch.int for Megatron int32 token accumulator
rai-studio-bot
left a comment
There was a problem hiding this comment.
结论:同意合入
已审查目标提交 9fdd83e(本轮新提交仅为合并 main)。两个改动文件与上轮已批准的 b71db72 逐字节一致;已核实 main 侧新变更(CP stage 3、repetition 诊断、megatron patch 扩充)不触及本修复的调用链与 int32 累加器契约:get_cp_local_num_tokens 的全部消费方、relax/backends/megatron/loss.py 与 streaming_schedules.py(torch.int 累加器)自上轮以来未被 main 修改,pinned megatron patch 也未改动 schedules.py。
本地 CPU eager 模式下 4 项 RLOO 回归测试通过(TorchInductor 后端为本地环境不兼容,非本次改动问题);GitHub CI 在该提交全部通过(含 H20 4-GPU 单测与 Qwen3-4B/VL GPU、NPU 异步集成)。
审查详情与范围记录:#290 (comment)
# 🔩 Chore ## Resolve conflict with redai-studio#290 - Keep exact CP=1 token counting without the per-sample `clamp_min(., 1)` - Keep the `torch.int` cast of `get_cp_local_num_tokens` required by the Megatron int32 token accumulator
Why
Megatron pipeline schedules accumulate the per-token loss normalizer on an int tensor:
megatron/core/pipeline_parallel/schedules.py—total_num_tokens = torch.zeros([], dtype=torch.int)(lines 256/652/1144/2363)relax/backends/megatron/streaming_schedules.py:106,295uses the same int accumulatorget_cp_local_num_tokensreturned float32 (aggregatedloss_mask.sum()), so every training run with--calculate-per-token-losscrashes at step 0 on the firsttotal_num_tokens += num_tokens:17/30 example launchers enable
--calculate-per-token-loss(all six sdpo launchers, most OPD/agentic recipes, rloo, mini_swe, nemo_gym).How
Cast the three return paths of
get_cp_local_num_tokenstotorch.intso the value handed toforward_backward_*matches the accumulator dtype. Non-per-token-loss paths are unaffected (their normalizer istorch.tensor(1)), and mixing the int32 scalar intolog_valuesstill produces identical float32 metrics.Testing
pytest tests/backends/megatron/test_rloo_policy_loss_dispatch.py tests/backends/megatron/test_rloo_cp_reduction.pypasses on CPU.test_rloo_policy_loss_dispatchnow compares the normalizer dtype-safely and pinsnormalizer.dtype == torch.intas the regression contract (the dtype-stricttorch.allclosewould otherwise fail after this fix).Relates to #285 (touches the same function cp_size == 1 branch).