Skip to content

[moe][perf] Turn on gather-sum dispatch and combine for DeepSeek-V3/V4, Kimi K3, GPT-OSS and Qwen3 MoE (4/4) - #5518

Draft
felipemello1 wants to merge 4 commits into
felipemello1/gen-perf/3-train-gather-combinefrom
felipemello1/moe/12-regions-dsv3-kimi
Draft

felipemello1 wants to merge 4 commits into
felipemello1/gen-perf/3-train-gather-combinefrom
felipemello1/moe/12-regions-dsv3-kimi

Conversation

@felipemello1

@felipemello1 felipemello1 commented Oct 19, 2026 •

Copy link
Copy Markdown
Contributor

Summary (human-written)

Agent details

TLDR: DeepSeek-V3, DeepSeek-V4, Kimi K3, GPT-OSS and Qwen3 MoE list the moe_dispatch_combine region. 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)

# PR Independent? Needs (stack positions)
1 AllToAll dispatcher: permute and unpermute with gathers yes —
2 Argsort expert ids on the narrowest integer type yes —
3 Gather-sum MoE dispatch and combine, training and inference, any EP no 2
4 Turn on gather-sum dispatch and combine for DeepSeek-V3/V4, Kimi K3, GPT-OSS and Qwen3 MoE no 3

Problem

  1. Opt-in. The gather-sum runs only for models whose region list has moe_dispatch_combine. Today only Qwen3.5 (and Qwen3.6 / 3.8) lists it.
  2. The others keep the eager combine (an fp32 multiply and a scatter-add) and an accumulating index_put as the token gather's backward. The GPT-OSS and Qwen3-30B-A3B RL recipes run this all-to-all path at EP 2-4.
  3. Cost. One DeepSeek-V3 MoE layer on main: the combine forward (with its residual add) and backward take 40.8 + 32.6 ms of a 316 ms fwd+bwd at 64k tokens; per-layer torch.compile does them in 0.65 + 8.1 ms.

Solution

  1. Add "moe_dispatch_combine" to DeepSeekV3Model.Config, DeepSeekV4Model.Config, KimiK3Model.Config, GptOssModel.Config and Qwen3Model.Config (dense Qwen3 has no MoE layer, so nothing changes there).
  2. Reaches 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.Config takes MultimodalModel.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

Numerics: not bitwise (fp32 sum, one rounding). 1 DeepSeek-V3 layer, fwd+bwd 16k, routing fixed: all 16 tensors
inside main's own spread for a 1-ulp input change (worst 0.40 of it). GPT-OSS: rel L2 2.1e-3 vs 2.6e-3 for main's
own regions vs main eager.

1 GB300, one run per row. before: DeepSeek-V3 = main; the others = this stack with the model's list unchanged
model (layers, local experts)       mode       T       before     after
DeepSeek-V3-671B (1, 128), AC       fwd+bwd    65536   309.5 ms   221.7 ms   -28.3%  (per-layer compile 234.4 ms, on another GB300 where main read 317.6)
GPT-OSS-20b (2, 16)                 decode     64        728 us     595 us   -18.2%
                                    prefill    2048     1920       1438      -25.1%
                                    fwd+bwd    4096    11084       8246      -25.6%
Qwen3-30B-A3B (1 MoE, 32)           decode     64        223        141      -36.4%
                                    prefill    2048      589        281      -52.3%
                                    fwd+bwd*   4096     3151       2195      -30.3%
DeepSeek-V4 flash (1 MoE, 256)      decode     64       2018       1935       -4.1%
                                    prefill    2048     3125       2716      -13.1%
                                    fwd+bwd*   16384   23926      17565      -26.6%
CUDA-graph replay, except *: capturing that MoE layer's fwd+bwd gave an empty graph (cause not investigated), so
both arms ran without graphs.
graphs: 1 per compiled function in the trainer (3 steps, new routing each), no recompiles; generator (GPT-OSS):
sort 4, combine 3 (limit 8)

NOTE:

  • Needs stack position 3, which registers moe_dispatch_combine; on main alone apply() raises "Unknown local_compile.regions entries".
  • The GPT-OSS and Qwen3-30B-A3B RL recipes run the trainer without CUDA graphs and vLLM with cuda_graph mode NONE (torchtitan_recipes/rl/alphabet_sort.py:399, :420, :623, :641).
  • Overlaps two open drafts: "[moe][perf] Compile the MoE router and its load-balance sums" (the same region lists and goldens) and "[gpt_oss][perf] Add w13's expert bias inside the SwiGLU region" (conflicts in all 5 GPT-OSS golden files). Whichever lands second regenerates them.
  • Goldens: 13 files change, taken from this PR's CI jobs (Fake PG, Real PG full suite, ROCm): DeepSeek-V3 (5), GPT-OSS (5), Qwen3 MoE (3). Step 1 moves by 7e-7 to 9e-5 relative. The two largest moves sit on steps where main's grad_norm spikes: GPT-OSS real_pg/a10g step 5, loss -0.155 (main's grad_norm 2.60 -> 6.54 there), and GPT-OSS fake_pg/a10g step 7, loss +0.150 (main's grad_norm 3.79 -> 40.87). The other 11 files move by at most 0.03 (GPT-OSS mi350x step 5). Kimi K3's golden test turns on the SPMD typechecker, which clears the list; DeepSeek-V4 has none.
  • Two mi350x "old" columns were already red on main: main's ROCm run fails DeepSeek-V3 cp_pp at step 5 and GPT-OSS pp at step 4. This PR's goldens turn both green.
  • Red on main too: rl-b200 (4 Kimi K3 bitwise-parity tests hit a vLLM KV-cache layout error), ROCm llama3_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
file                               step    loss old   loss new  grad_norm old grad_norm new
fake_pg/a10g/deepseek_v3.txt          1    8.011864   8.011676        2.32317       2.32326
                                     10    6.329323   6.329686        3.68021       3.67992
fake_pg/a10g/gpt_oss.txt              1    7.639480   7.639595        159.706       159.777
                                     10    5.025739   5.053090         4.9529       5.30002
fake_pg/a10g/qwen3.txt                1    3.514194   3.513875    7.49237e+13   7.38785e+13
                                     10    2.965225   2.959582    2.29567e+17   5.95549e+16
real_pg/a10g/deepseek_v3.txt          1    8.218992   8.219008        3.63827       3.63828
                                     10    3.810009   3.809943         1.7263       1.73033
real_pg/a10g/deepseek_v3_cp_pp.txt    1    8.237020   8.237032        3.85243       3.85243
                                     10    3.830781   3.831090        1.57421       1.57844
real_pg/a10g/gpt_oss.txt              1    7.955585   7.955798        46.2483       46.2437
                                     10    3.863626   3.838488        1.83918       1.63769
real_pg/a10g/gpt_oss_pp.txt           1    7.947326   7.947175        42.1434       42.1437
                                     10    3.908026   3.908218        1.42159       1.43128
real_pg/a10g/qwen3.txt                1    7.722271   7.722469        23.2174       23.2212
                                     10    5.349347   5.354222        3.48261        3.4847
real_pg/mi350x/deepseek_v3.txt        1    8.218930   8.219003        3.63831       3.63833
                                     10    3.810421   3.809863        1.73368       1.72839
real_pg/mi350x/deepseek_v3_cp_pp.txt    1    8.237011   8.237017        3.85243       3.85243
                                     10    3.831059   3.831013        1.58023       1.57717
real_pg/mi350x/gpt_oss.txt            1    7.955068   7.955287        46.2437       46.2491
                                     10    3.847173   3.842745        1.70434       1.66147
real_pg/mi350x/gpt_oss_pp.txt         1    7.947314   7.947339        42.1423       42.1419
                                     10    3.909404   3.908144        1.43496       1.43383
real_pg/mi350x/qwen3.txt              1    7.722477   7.722407        23.2192       23.2168
                                     10    5.349069   5.349163        3.48235       3.48261

Qwen3 MoE's Fake PG grad_norm swings between ~2 and 1e13-1e17 on main too.

  • Not run by hand: these models at EP > 1 (one EP rank's share at EP=1 instead), MXFP8 / NVFP4, H100, GPT-OSS 120b. CI ran the golden tests above (FSDP, TP, CP, PP and EP, on a10g and mi350x) and Kimi K3 with FSDP + TP + EP + PP on B200.

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)

@meta-cla meta-cla Bot added the CLA Signed This label is managed by the Meta Open Source bot. label Oct 19, 2026
@felipemello1
felipemello1 added this pull request to stack #5519 October 19, 2026 02:17
@felipemello1
felipemello1 removed this pull request from stack #5519 October 19, 2026 06:46
@felipemello1
felipemello1 changed the base branch from felipemello1/gen-perf/3-train-gather-combine to main October 19, 2026 06:47
@felipemello1
felipemello1 force-pushed the felipemello1/moe/12-regions-dsv3-kimi branch from 2caaff2 to 3d93124 Compare October 19, 2026 06:48
@felipemello1
felipemello1 changed the base branch from main to felipemello1/gen-perf/3-train-gather-combine October 19, 2026 06:48
@felipemello1
felipemello1 added this pull request to stack #5533 October 19, 2026 06:48
@felipemello1 felipemello1 changed the title [moe][perf] Enable the gather-sum dispatch and combine regions for DeepSeek-V3 and Kimi K3 [moe][perf] Turn on gather-sum dispatch and combine for DeepSeek-V3 and Kimi (6/6) Oct 19, 2026
@felipemello1
felipemello1 removed this pull request from stack #5533 November 7, 2026 06:00
@felipemello1
felipemello1 force-pushed the felipemello1/moe/12-regions-dsv3-kimi branch from 3d93124 to 2043f96 Compare November 7, 2026 06:00
@felipemello1
felipemello1 added this pull request to stack #5932 November 7, 2026 06:00
@felipemello1 felipemello1 changed the title [moe][perf] Turn on gather-sum dispatch and combine for DeepSeek-V3 and Kimi (6/6) [moe][perf] Turn on gather-sum dispatch and combine for DeepSeek-V3/V4, Kimi K3, GPT-OSS and Qwen3 MoE (4/4) Nov 7, 2026
@felipemello1 felipemello1 added ciflow/fake-pg Run 1-GPU Fake PG integration tests ciflow/real-pg Run the full 8-GPU Real PG integration suite ciflow/rocm ciflow/b200 Trigger B200 CI labels Nov 7, 2026
@felipemello1

Copy link
Copy Markdown
Contributor Author

Reply from claude: @claude review

@claude

claude Bot commented Nov 7, 2026 •

Copy link
Copy Markdown
Contributor

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.

@claude claude Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

  1. 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 against tests/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.
  2. Numerics evidence is a placeholder. The <details> block still says TODO 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.
  3. "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_mm and kimi_k3_mm_muon (the only golden) go through _set_spmd_typechecking(typechecking=True), which sets regions = [] (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+ep likewise 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.

@felipemello1

Copy link
Copy Markdown
Contributor Author

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.

  • Every golden test failed only at its expected flavor before this push, with step-1 mismatches of 1e-6 to 3e-5 relative.
  • Red on main too, with the same tests and values: rl-b200 (4 Kimi K3 bitwise-parity tests hit a vLLM KV-cache layout error; main's scheduled run fails the same 4) and ROCm llama3_fsdp+tp+pp (step 6, 4.709462 vs 4.709483, on 4 main commits).
  • B200 Integration passed, including kimi_k3_fsdp2_tp2_ep2_pp2_vpp4 with the region on (typechecking off).
  • The branch also merges stack position 3's latest head (review fixes, test fold).

@felipemello1

Copy link
Copy Markdown
Contributor Author

Reply from claude: @claude review

@claude

claude Bot commented Nov 7, 2026 •

Copy link
Copy Markdown
Contributor

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.

@claude claude Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.txt is dense Qwen3-0.6B batch-invariant, and sft.txt is 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

  1. "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.py diff only renames expert_sort_N to route_indices_experts_sorted_N, moves the eager early-return in _sort_routes to 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.

@felipemello1

Copy link
Copy Markdown
Contributor Author

Reply from claude: body fixes in (step-1 range 7e-7 to 9e-5, the two largest golden moves, the two mi350x goldens that were red on main, and the second overlapping draft). b21ca94 merges stack position 3's rename. @claude review

@claude

claude Bot commented Nov 7, 2026 •

Copy link
Copy Markdown
Contributor

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.

@claude claude Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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_expert rename in torchtitan/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_routes keeps the inline index_put inverses. 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_pg qwen3).
    • 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.
  • 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

  1. "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.

@felipemello1
felipemello1 force-pushed the felipemello1/moe/12-regions-dsv3-kimi branch from b21ca94 to 06b8637 Compare November 7, 2026 20:52
Felipe Mello added 4 commits November 7, 2026 20:42
… 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.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

ciflow/b200 Trigger B200 CI ciflow/fake-pg Run 1-GPU Fake PG integration tests ciflow/real-pg Run the full 8-GPU Real PG integration suite ciflow/rl ciflow/rocm CLA Signed This label is managed by the Meta Open Source bot.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant