Unit 3 of 10 · about 5 hours · implement
Token packing, all-to-all dispatch and inverse combine
Dispatch is where routing meets the network. Packing decides the grouped matrix layout, the two all-to-alls move the rows, and the inverse permutation decides whether each output lands back on its token.
You'll be able to
- Pack routed rows by destination expert and rank with a stable permutation, and invert it exactly on combine.
- Exchange variable row counts with
all_to_all_singleusing explicit uneven splits, including empty experts. - Prove token identity through dispatch, expert compute and combine against a single-process oracle.
Covers
Read
In this order, only the parts listed
0 of 4 done
- Dispatch and combine as linear algebraGShard: Scaling Giant Models with Conditional Computation and Automatic Sharding
Section 3.1, Algorithm 2 (arXiv:2006.16668v1): dispatch einsum, grouped expert einsums and combine einsum; Section 3.3.1 for the AllToAll resharding primitive
- All-to-all dispatch and combine walkthroughTorchFeather - Expert Parallelism
19:01:56 (all-to-all dispatch and combine): counts all-to-all first, payload all-to-all second, then permute and unpermute
- Dispatch, permute and combine codeTorchFeather - Expert Parallelism
ExpertParallel._token_dispatch(countsall_to_all_single,input_splitsandoutput_splitsfrom row sums,all_to_all_single_autogradpayload,_permute) and_token_combine(_unpermute, then swapped splits);model/moe/utils.py_permuteand_unpermute all_to_all_singlesplit semanticsPyTorch Distributed - all-to-all collectiveinput_split_sizesandoutput_split_sizesfor dim 0 with the uneven-split example (zero entries also run: a 4-processgloocheck with an empty expert completes, though the docs example does not show one)
Focus on
- Two all-to-alls: an equal-split exchange of per-expert counts, then an uneven payload exchange with explicit splits. Skipping the counts exchange leaves receive sizes unknown.
- Splits count rows, not bytes. Dispatch sends with the receive counts first and the send counts second; combine swaps them.
_permuteconverts source-major rows to padded local-expert-major rows for grouped matrix multiply;_unpermuteinverts it before the combine all-to-all. Padding rows are zeros for compute only and never return to tokens.- GShard expresses the same path as einsums over dispatch and combine weights. The TorchFeather code is that math with ranks, splits and padding made explicit.
Common mistakes · 3
- Passing equal splits to the payload all-to-all when experts are imbalanced. If every rank uses the same wrong table, rows reach the wrong experts with no error (measured on 2
glooprocesses); if send and receive splits disagree,glooraises an enforce failure, and other backends need not check. Derive splits from exchanged counts every step. - Padded grouped-MM row counts from
_permuteare multiples of 8 and are not token counts. Using them for load fractions or capacity overstates load. - CPU
glootimings validate byte counts and identity, not GPU interconnect behavior. Do not quote them as NVLink or InfiniBand evidence.
Try it yourself
Closed-book, before checking anything
torchrun --nproc-per-node 4 on the gloo backend with dist.all_to_all_single and assert identical outputs and recovered order.Quiz yourself
Answer first, then mark it. Missed ones go to Review.
0 got · 0 missed
1.In the file worked example rank 0 sends . What do they sum to and why?Answer
32, which is : 8 local tokens after the TP split times top-.
2.Rank 0 receives 31 rows but sent 32. Is that a bug?Answer
No. Sends count rows by destination expert; receives count rows for owned experts from all sources. Imbalance makes them differ; the exchanged counts predict both.
3.Combine swaps the split arguments. Why?Answer
Dispatch moves rows from source layout to owner layout; combine moves results back. Each rank send sizes on dispatch are its receive sizes on combine.
4.An expert gets zero tokens on all ranks. What must still hold?Answer
Every rank passes matching zero splits for it, the payload all-to-all still runs, and the expert contributes nothing. Empty splits are legal, as the 4-process
gloocheck shows.
You're done when
Your oracle round-trips token identity through pack, uneven exchange and combine with an empty expert, and the gloo run matches it exactly.
Only when you can do this without notes.
Leads to
Notes
Resources · 3 for this unit, 9 more in the section
- TorchFeather - Expert ParallelismTorchFeather · required
Watch the video segments from 16:49:58 (MoE) through 18:36:24 (expert parallelism), 19:01:56 (all-to-all dispatch and combine) and 19:27:38 (expert tensor parallelism), then trace
model/moe/anddistributed/expert_parallel.py. Build a tiny router-dispatch-expert-combine oracle first, then map each group, placement andall_to_all_singlecall to it. - GShard: Scaling Giant Models with Conditional Computation and Automatic ShardingLepikhin et al. (ICLR 2021) · support
Read the mixture-of-experts layer: top-2 gating, expert capacity, the auxiliary loss and random routing. Compare its capacity rule with Switch.
- PyTorch Distributed - all-to-all collectivePyTorch · reference
Read
all_to_all_singleforinput_split_sizesandoutput_split_sizeson dim 0 and its uneven-split example. Use it for the dispatch and combine calls in the tiny oracle, and check zero splits yourself.
Also in this section
- Ultra-Scale Playbook - Expert ParallelismHugging Face · primary
Read Expert Parallelism and its interaction with DP in the 5D summary. Derive tokens, bytes and expected imbalance for one top-k routing example; use the linked MoE introduction only if the architecture is unfamiliar.
- How to Scale Your Model - Expert Parallelism on GPUsGoogle DeepMind · support
Read the expert-parallelism part of Rooflines for LLM Scaling on GPUs and work its quiz before the answer. Use it for the all-to-all cost half of your MoE accounting.
- Mixture of Experts ExplainedHugging Face · required
Study routing, capacity, token dropping, auxiliary loss and the expert-parallelism sections. Implement the small routing examples and verify conservation of token contributions through combine; group composition and FSDP interaction come from the papers and TorchTitan.
- ST-MoE: Designing Stable and Transferable Sparse Expert ModelsZoph et al. (2022) · support
Read the stability section that introduces the router z-loss and compare it with the auxiliary balancing loss.
- DeepSeek-V3 Technical Report - MoE SectionsDeepSeek-AI (2024) · support
Read the DeepSeekMoE architecture (shared and fine-grained experts, auxiliary-loss-free balancing, node-limited routing) and the all-to-all communication design. Its throughput figures are its own cluster's results.
- TensorTonic - Top-k MoE Router with Load StatisticsTensorTonic · support
Solve Top-k MoE Router with Load Statistics, including its lower-index tie rule, before building dispatch.
- Bumblebee - Router, Dispatcher and Expert PrimitivesBumblebee · depth
Only as a possible capstone target: read
primitive/modules/router.py,dispatcher.py,experts.pyandmoe.pyin a clean checkout. Its own todo lists no multi-GPU correctness test yet, so do not treat it as a reference implementation. - TorchTitan - MoE Model and ParallelismPyTorch · support
Read the MoE model files and their parallelization plan only after the tiny oracle passes, at a pinned commit. Record how a production PyTorch stack composes EP, expert FSDP and the other mesh dimensions.
- Switch TransformersGoogle Research · support
Read Section 2 for simplified routing, the capacity factor and the load-balancing loss, and Section 5 for combining data, model and expert parallelism. Reproduce its capacity arithmetic for one batch.