Repository navigation
[moe][perf] Turn on gather-sum dispatch and combine for DeepSeek-V3/V4, Kimi K3, GPT-OSS and Qwen3 MoE (4/4) - #5518
Conversation
2caaff2 to
3d93124
Compare
3d93124 to
2043f96
Compare
|
Reply from claude: @claude review |
|
Claude finished @felipemello1's task in 1m 32s —— View job Request changes -- the 13 golden loss files aren't regenerated yet, the old/new loss evidence is still a TODO, and the human-written summary is empty. See the review. |
There was a problem hiding this comment.
Verdict: Request changes -- the change is right and as small as it can be, but it deliberately changes numerics for 13 golden suites and ships none of the regenerated goldens or the old/new loss evidence, so it can't land as-is.
What this PR does. Adds "moe_dispatch_combine" to the default local_compile.regions of DeepSeek-V3, DeepSeek-V4, Kimi K3, GPT-OSS and Qwen3, so their AllToAll/local MoE dispatch and combine use the compiled gather-sum from stack PR 3 (an fp32 per-token weighted sum, gathers in backward) instead of the eager multiply + deterministic_scatter_add. DeepEP/HybridEP/Dist-MoE paths never reach the decorated functions, so they don't change. Dense Qwen3 has no MoE layer, so nothing changes there.
Example. One token with top_k=2, expert outputs r_a, r_b, scores s_a, s_b (bf16):
before (eager): out = bf16(bf16(r_a*s_a) + bf16(r_b*s_b)) # each product and partial sum rounded
after (compiled): out = bf16(fp32(r_a)*s_a + fp32(r_b)*s_b) # one rounding at the end
So any model whose recipe keeps the default region list gets slightly different loss/grad_norm. That is expected, and it is why the goldens below move.
Must fix
- Goldens not regenerated. The description lists 13 files under
tests/assets/losses/that change (DeepSeek-V3 x5, GPT-OSS x5, Qwen3 MoE x3). I checked them againsttests/integration_tests/models.py:694-811: those are the right files, and every one of those configs keeps the model's default region list. None are in this diff, though. The golden-numerics jobs on this PR will fail until they are added. Please regenerate all 13 from this PR's CI run (real-PG and fake-PG, a10g and mi350x) and commit them here. - Numerics evidence is a placeholder. The
<details>block still saysTODO before landing: per golden file, the old and the new loss lines. Please fill it in so a maintainer can see the change is a small, explained drift and not a regression. Per-file old vs new final loss and grad_norm is enough. - "Summary (human-written)" is empty. The author has to fill it in before landing.
Details
- Gate: the problem is real (these models fall back to the eager combine on the base branch). The fix is at the right layer: a model config default, with no change to shared code. It is not one-sided either, since the vLLM generator reads the same list, so trainer and generator numerics stay consistent. A config default like this is the expected way to turn the region on.
- Kimi K3:
kimi_k3_mmandkimi_k3_mm_muon(the only golden) go through_set_spmd_typechecking(typechecking=True), which setsregions = [](torchtitan_recipes/tests/__init__.py:27-30). So the description's claim that the Kimi K3 golden is unaffected holds. However,kimi_k3_fsdp2_tp2_ep2_pp2_vpp4(tests/integration_tests/b200.py:76) turns typechecking off, so it will run the new region under FSDP+TP+EP+interleaved PP with 4 stages per rank. That run is the only TP>1 + EP + PP check this region gets, and it has no golden. Please make sure that job passes on this PR before landing. The B200 run was still in progress when this review was written. - DeepSeek-V4:
deepseek_v4_fsdp+tp+eplikewise runs the region at TP2 x EP2 with no golden, which gives more coverage of the TP>1 case. - The two unit-test edits only update the expected region lists in existing assertions. No new tests are added.
Not verified: GPU numerics and perf numbers (taken from the description); whether the B200 Kimi K3 PP cell passes with the region on.
|
Reply from claude: the 13 goldens are in, taken from this PR's CI jobs: Fake PG (3), Real PG full suite on a10g (5), ROCm mi350x (5). The body has old vs new loss and grad_norm, first and last step, for each file.
|
|
Reply from claude: @claude review |
|
Claude finished @felipemello1's task in 1m 14s —— View job Approve -- the goldens and the numerics evidence are in; the only item left is the empty human-written summary. See the review. |
There was a problem hiding this comment.
Verdict: Approve -- please address the item below before merging. Two of the three earlier must-fix items are fixed. The one left, the human-written summary, is the author's to write.
What this PR does. Adds "moe_dispatch_combine" to the default local_compile.regions of DeepSeek-V3, DeepSeek-V4, Kimi K3, GPT-OSS and Qwen3. Their AllToAll/local MoE combine then runs the compiled gather-sum from stack position 3 instead of the eager multiply + scatter-add. It also commits the 13 regenerated goldens.
Example. One token, top_k=2, bf16:
before (eager): out = bf16(bf16(r_a*s_a) + bf16(r_b*s_b)) # rounds each product and partial sum
after (compiled): out = bf16(fp32(r_a)*s_a + fp32(r_b)*s_b) # rounds once
Earlier review, at HEAD
- Goldens not regenerated: fixed. All 13 files are in the diff, under fake_pg/a10g, real_pg/a10g and real_pg/mi350x. No other golden reads these models' default list:
rl_grpo_cuda.txtis dense Qwen3-0.6B batch-invariant, andsft.txtis llama3. - Numerics evidence: fixed. The
<details>table gives old vs new loss and grad_norm at the first and last step for each file. - Human-written summary: still open (see below).
- Kimi K3 FSDP+TP+EP+PP cell on B200 with the region on: the author reports it passed.
Must fix
- "Summary (human-written)" is still empty. The author has to fill it in before landing. Agents don't write it.
Details
- Golden timing: the Fake PG goldens (41f5fc8) were committed right after merging stack position 3's round-2 fixes (629f94e), so I checked whether that merge changes numerics. Its
token_dispatcher.pydiff only renamesexpert_sort_Ntoroute_indices_experts_sorted_N, moves the eager early-return in_sort_routesto the top, rewrites docstrings, and adds a TorchAO TODO. The sort order and the eager and compiled math are the same, so goldens taken before or after it should match. f70b6fa only touches a unit test. - The code change is still 5 one-line config additions plus the 2 matching expected-list updates in existing tests. Nothing new is needed.
Not verified: the golden and B200 jobs on this HEAD were still queued or running when this review was written. A maintainer still needs to review.
|
Claude finished @felipemello1's task in 58s —— View job Approve -- the new merge is a rename only, and the body's golden numbers match the files; the only item left is the empty human-written summary. See the review. |
There was a problem hiding this comment.
Verdict: Approve -- please address the item below before merging. The new merge (b21ca94) is a rename only and doesn't change numerics. The body edits match the golden files. The only item left is the empty human-written summary.
What changed since the last review.
- b21ca94 merges stack position 3's
_sort_and_gather->_reorder_by_expertrename intorchtitan/models/common/moe/token_dispatcher.py(+8/-6). It renames the method at both call sites and in two comments, and adds a comment saying why_sort_routeskeeps the inlineindex_putinverses. The math is the same, so the goldens committed before this merge are still valid. Nothing in the repo still refers to_sort_and_gather. - I checked the new numbers in the body against the golden files at HEAD:
- Step-1 relative moves run from 7.3e-7 (mi350x
deepseek_v3_cp_pp) to 9.1e-5 (fake_pgqwen3). - GPT-OSS real_pg/a10g step 5 moves by -0.155 (4.4837 -> 4.3285), where main's grad_norm goes 2.60 -> 6.54.
- GPT-OSS fake_pg/a10g step 7 moves by +0.150 (5.9384 -> 6.0884), where main's grad_norm goes 3.79 -> 40.87.
- GPT-OSS mi350x step 5 moves by -0.029. That fits "at most 0.03" for the other 11 files.
- Step-1 relative moves run from 7.3e-7 (mi350x
- The code change is still 5 one-line region-list additions plus the 2 matching updates to existing test assertions.
Example. One token, top_k=2, bf16:
before (eager): out = bf16(bf16(r_a*s_a) + bf16(r_b*s_b)) # rounds each product and partial sum
after (compiled): out = bf16(fp32(r_a)*s_a + fp32(r_b)*s_b) # rounds once
Must fix
- "Summary (human-written)" is still empty (only the template placeholder is there). The author has to write it before landing. Agents don't fill it in.
Not verified: whether the golden and B200 jobs pass on b21ca94; I relied on the rename being numerics-neutral. A maintainer still needs to review.
b21ca94 to
06b8637
Compare
… GPT-OSS and Qwen3 MoE Lists moe_dispatch_combine in DeepSeekV3Model.Config, DeepSeekV4Model.Config, KimiK3Model.Config, GptOssModel.Config and Qwen3Model.Config. Their compiled runs then use the gather-sum combine instead of a scatter-add, and in training a per-token sum instead of an accumulating index_put as the token gather's backward. Kimi K2.5 keeps its empty region list: its Config takes MultimodalModel.Config's default first. Muse Spark keeps its list: its recipes run Dist-MoE.
…gion From this PR's 1 GPU Integration (Fake PG) job: DeepSeek-V3, GPT-OSS and Qwen3 MoE now sum each token's routes in fp32 with one rounding.
…region From this PR's ROCm 8 GPU Integration (models) job: DeepSeek-V3 (2), GPT-OSS (2) and Qwen3 MoE. Llama 3 PP's mi350x golden fails on main too and is unchanged.
…gion From this PR's 8 GPU Integration (Real PG, full suite, models) job: DeepSeek-V3 (2), GPT-OSS (2) and Qwen3 MoE. The required-subset job gave the same PP values.
bbba078 to
d740884
Compare
Summary (human-written)
Agent details
TLDR: DeepSeek-V3, DeepSeek-V4, Kimi K3, GPT-OSS and Qwen3 MoE list the
moe_dispatch_combineregion. One DeepSeek-V3-671B MoE layer, fwd+bwd at the recipe's 64k tokens: 309.5 -> 221.7 ms (-28.3%), peak 79.1 -> 45.6 GiB. Measured on 1 GB300 at EP=1 with CUDA graphs, as a stand-in for one EP2 rank. The recipe runs EP2 + PP without graphs. GPT-OSS-20b fwd+bwd -26% (CUDA graphs), Qwen3-30B-A3B -30% (no graphs). Their RL recipes run without CUDA graphs, so today GPT-OSS mainly gets fewer kernel launches (600 -> 430 per 2-layer step).Stack (4 PRs, merge bottom-up; this PR is in bold)
Problem
moe_dispatch_combine. Today only Qwen3.5 (and Qwen3.6 / 3.8) lists it.index_putas the token gather's backward. The GPT-OSS and Qwen3-30B-A3B RL recipes run this all-to-all path at EP 2-4.torch.compiledoes them in 0.65 + 8.1 ms.Solution
"moe_dispatch_combine"toDeepSeekV3Model.Config,DeepSeekV4Model.Config,KimiK3Model.Config,GptOssModel.ConfigandQwen3Model.Config(dense Qwen3 has no MoE layer, so nothing changes there).deepseek_v3_671b,rl_grpo_gpt_oss_20b_varlen,rl_grpo_qwen3_30b_a3b_varlen(the vLLM generator reads the same list) and the DeepSeek-V4 test recipes. Stays off: recipes with their own list (the NVFP4 DeepSeek-V3 recipe, batch-invariant and SPMD-typechecked configs), Kimi K2.5/K2.7 (KimiK25Model.ConfigtakesMultimodalModel.Config's empty list first) and Muse Spark (every recipe runs Dist-MoE:torchtitan_recipes/rl/dapo_math.py:394-398,torchtitan_recipes/models/muse_spark.py:483,:829).Results
NOTE:
moe_dispatch_combine; on main aloneapply()raises "Unknown local_compile.regions entries".cuda_graphmode NONE (torchtitan_recipes/rl/alphabet_sort.py:399,:420,:623,:641).rl-b200(4 Kimi K3 bitwise-parity tests hit a vLLM KV-cache layout error), ROCmllama3_fsdp+tp+pp(step 6 mismatch, the same values on main), and RL Multi-GPU's Qwen3.5 bitwise-parity test (its recipe clears the region list).The 13 golden files: old vs new, first and last step
Qwen3 MoE's Fake PG grad_norm swings between ~2 and 1e13-1e17 on main too.
Test plan
pytest tests/unit_tests/cpu/models/kimi_k3/ tests/unit_tests/cpu/models/deepseek_v3/ tests/unit_tests/cpu/models/deepseek_v4/ tests/unit_tests/cpu/models/gpt_oss/ tests/unit_tests/cpu/models/test_build_model_config.py pytest tests/unit_tests/cpu/quantization/test_quantization.py -k qwen3_recipes_resolve # needs torchao; not run (no torchao here, and drafts skip CPU unit tests)