Repository navigation
[moe][perf] Skip the per-expert token count in backends that don't read it (1/3) - #5516
Conversation
ff6017e to
ea6129d
Compare
ea6129d to
9266852
Compare
| def count_tokens(self, routing_map_TE: torch.Tensor) -> torch.Tensor | None: | ||
| """Skip the count: ``forward`` builds its dispatch from the expert ids.""" | ||
| if spmd.is_type_checking(): | ||
| # The typechecker rejects ``None`` where a tensor input is declared. |
There was a problem hiding this comment.
I feel introducing this function is not ideal and it a little bit confusing.
what about passing routing map into routed experts? and dist moe just delete it?
There was a problem hiding this comment.
reply from claude:
Agreed, and done one level deeper than you suggested, so DeepEP and HybridEP skip the sum too. Only the Local / AllToAll / TorchAO dispatchers read the per-expert count; the router's stats (tokens_per_expert_E, aux loss) sum their own padding-masked map.
Changes:
MoE,KimiLatentMoEandMuseSparkMoEpassrouting_map_TEto the routed experts and stop summing.RoutedExperts.forwardpasses it totoken_dispatcher.dispatch: Local / AllToAll / TorchAO sum it, DeepEP / HybridEPdelit.DistMoeRoutedExperts.forwarddels it.count_tokensand its typechecker branch are gone: the map is a real tensor at thelocal_mapboundary.- The count is now summed inside the local region, so the two
mutate_type(P -> V)calls go away.
Same kernels: the stock path sums inside dispatch (bitwise vs main, TP 2 x EP 2 on 2 CPU ranks, output and all grads), and Dist-MoE still launches no sum, so the body's numbers hold.
c28ea9a to
062b072
Compare
|
@claude review |
|
Claude finished @tianyu-l's task in 2m 2s —— View job Review of #5516: Skip the per-expert token count in backends that don't read it
Verdict: LGTM. I found no correctness problems. The change is a clean way to address @acisseJZhong's comment, and it goes one level further than asked, so DeepEP and HybridEP skip the sum as well. What I checkedCallsite audit (CLAUDE.md principle 6)
SPMD types (
Numerics and kernels
Remat pins
GraphTrainer
Nits (optional)
Not done
|
MoE.forward summed the (T, E) routing map into per-expert token counts for every routed-experts backend. Dist-MoE builds its dispatch from the expert ids and deleted the counts unread. Routed experts now expose count_tokens(): the stock RoutedExperts returns the sum; Dist-MoE returns None (the sum only while the SPMD typechecker runs, which rejects None for a declared tensor input), so MoE, MuseSparkMoE and KimiLatentMoE skip the sum. Qwen3.5-35B-A3B MoE block (router + Dist-MoE routed experts + inline shared experts), EP 4 on 4 GB300, 8 layers, CUDA graphs, decode, min of 2 processes, us per layer: T per GPU 32 64 256 before 153.2 158.2 177.4 after 146.4 150.0 165.4 Outputs bitwise equal (12/12 saved per-rank outputs).
Only the Local, AllToAll and TorchAO token dispatchers read the per-expert
token count. The MoE now passes the router's (T, E) routing map to the routed
experts, and RoutedExperts passes it to token_dispatcher.dispatch:
- Local, AllToAll and TorchAO dispatch sum it; DeepEP and HybridEP delete it.
- DistMoeRoutedExperts.forward deletes it (its MXFP8 and LoRA variants inherit).
- MoE, KimiLatentMoE and MuseSparkMoE no longer sum; count_tokens and its
typechecker branch are gone.
- The count is now summed inside the routed experts' local SPMD region, so it
is already local: the two mutate_type(P -> V) calls are gone. The sharding
config declares the map with the layout of topk_expert_ids_TK.
What moved: the sum runs at the top of dispatch (before AllToAll's dispatch
remat region), so the stock path launches the same kernels. In Kimi and Muse
the sum now runs a few ops later.
Under GraphTrainer its node is now labeled {"EP": "dispatch"} instead of
MoE.forward's {"EP": "compute"}. No pass reads that label: the EP overlap pass
keys on EP_token_exchange and EP_token_count_sync.
CPU, 2 ranks on gloo, TP 2 x EP 2: MoE forward + backward with SP off and on is
bitwise equal to the previous commit (32/32 tensors). The parallelized routed
experts pass the SPMD typechecker with EP 2 and EP 1.
062b072 to
d76a48d
Compare
TLDR: Only the token dispatchers that read the per-expert count now sum the routing map, so Dist-MoE, DeepEP and HybridEP skip one reduction per MoE layer (Qwen3.5-35B-A3B, Dist-MoE decode: -4.4 to -6.8% per layer, bitwise identical).
Stack (3 PRs, merge bottom-up; this PR is in bold)
Problem
MoE.forwardsums the(T, E)routing map into per-expert counts (routing_map_TE.sum(dim=0)) and passes them to the routed experts.DistMoeRoutedExperts.forwardand the DeepEP and HybridEP dispatchers work from the expert ids and delete the counts unread.Solution
MoE,KimiLatentMoEandMuseSparkMoEpass the router'srouting_map_TEto the routed experts instead of summing it.RoutedExperts.forwardpasses it on totoken_dispatcher.dispatch: the Local, AllToAll and TorchAO dispatchers sum it; DeepEP and HybridEP delete it.DistMoeRoutedExperts.forwarddeletes it (its MXFP8 and LoRA variants inherit that).mutate_type(P -> V)calls in the Local and AllToAll dispatchers go away.dispatch) and the router's load-balance statistics (tokens_per_expert_E, aux loss).Results
NOTE:
{"EP": "dispatch"}instead ofMoE.forward's{"EP": "compute"}. No pass reads that label; the EP overlap pass keys on the token-exchange annotations.You may ask:
RoutedExperts.forward? DeepEP and HybridEP would still sum and throw it away.topk_expert_ids_TK? That adds a new kernel (ascatter_add) to the stock training path; summing the map keeps the kernel it runs today.Test plan
pytest tests/unit_tests/cpu/models/common/test_moe.py -k routing_map:MoE,KimiLatentMoEandMuseSparkMoEhand the routed experts the(T, E)bool map; fails if a caller sums again.test_moe.py,test_dist_moe.py,test_aux_loss.py,recipes/test_inference_moe.py,models/kimi_k3,models/muse_spark. The 2 MXFP8 recipe tests need torchao.dispatchwith a count now pass a map, and pass on 2 GB300:gpu/models/common/test_dist_moe.py(stock vs Dist-MoE parity),test_quantile_balance.py,gpu/distributed/test_remat.py,test_remat_all_to_all.py(TorchAO case not run: no torchao),test_deepep_activation_checkpoint.py.pytest torchtitan/experiments/graph_trainer/tests/test_passes.py -k "ep_overlap or moe_ep": the EP annotation and overlap-pass tests pass.