Expert Parallelism and Mixture of Experts
MoE makes compute sparse but communication and load imbalance explicit. A correct route-dispatch-expert-combine path and capacity accounting are the foundation for any performance claim.
Section 7 of 10 · 10 units · about 43 hours
By the end you can Implement a tiny top-k router, all-to-all-equivalent dispatch and combine path with forward and gradient oracles, report capacity and imbalance, and explain one hotspot or empty-expert failure from traces.
Units
- Sparse MoE layers, router scores and shared and fine-grained experts5h
- Top-k selection with deterministic ties and output recombination4h
- Token packing, all-to-all dispatch and inverse combine5h
- Expert process groups, expert tensor parallelism and expert state ownership4h
- Capacity factor and dropless, dropped and padded routing4h
- Auxiliary load-balancing loss and router z-loss4h
- Tokens-per-expert histograms, hot experts and all-to-all skew4h
- Masked-token, router-gradient and empty-expert tests5h
- FSDP with expert-sharded parameters and EP, TP and DP groups4h
- MoE communication and memory accounting and topology-aware placement4h
Build it
Mini project · about 12 to 16 hours
Expert-parallel MoE with balanced routing and honest accounting
Build the tiny MoE dispatcher in parallel/ with packing, all-to-all exchange and combine, then balance, diagnose and cost it. Per-expert histograms, capacity math and placement records show where tokens move and which links pay.
- 1Implement the router and top- selection in
parallel/with deterministic ties, renormalized weights and shared-expert recombination, reporting token fractions and mean probabilities. Pass the Deep-ML sparse-expert and TensorTonic top- router cases, then keep the hand derivation as the first regression. - 2Pack rows by expert and rank with a permutation in
parallel/, exchange counts and payload over all-to-all with explicit splits, and invert the permutation on combine. Prove token identity and gradient flow againstmodel/oracle with uneven and empty splits, confirmed by a Gloo run. - 3Build the rank-coordinate mapper for expert and expert-tensor groups in
comm/, with round-trip and invalid-configuration tests. Tabulate one dense and sparse mesh composition with validation checks, keeping routed experts sparse and gate and shared state dense, plus one rejected configuration for each rule. - 4Derive per-expert capacity for one batch with drop and pad accounting, and show overflow behavior under skewed routing. Implement the auxiliary balancing loss and router z-loss with detached count fractions, passing the Deep-ML balancing-loss cases and contrasting bias routing without gradients.
- 5Instrument per-expert histograms with drop counts and per-rank timing and communication splits in
trace/, diagnosing one hotspot or empty-expert case. Harden the path with masked, repeated-token, router-gradient and empty-expert tests in the TorchTitan CPU style, using the Deep-ML routing cases as the recombination oracle. - 6Derive dispatch and combine bytes with memory accounting for one layer in
plan/, state a placement with its contended links, and keep one measured-versus-predicted residual. Label CPU timings as accounting checks only, with link claims reserved for measured hardware.
What you hand in
- The
parallel/dispatcher with packing, exchange, combine and routing oracles. - Group mapper, mesh tables, capacity math and imbalance evidence with one diagnosis.
plan/byte and memory accounting with placement, contended links and one residual.tests/andtrace/replaying identity, gradients and negative cases.
How to check it
- Self-grade with the rubric below
- Deep-ML 125 Sparse Mixture of Experts Layer
Pass both forward tests; add routing and dispatch checks around them.
- TensorTonic Top-k MoE Router with Load Statistics
Match order, dtype and device on all public cases exactly.
- Deep-ML 389 MoE Load Balancing Loss
Derive both expected values from the formula before running.
- Deep-ML 229 Sparse MoE Top-K Routing
Use as the recombination oracle beside masked and empty tests.
- TorchTitan CPU MoE unit tests
Copy the padding, counter and transition patterns, not the framework.
How it's graded
Dispatch identity
Pass: Uneven and empty splits round-trip token identity through a stable permutation, and gradients reach router and experts.
Great: A duplicated-token case and a swapped-split mutation each fail a named assertion.
Groups and meshes
Pass: Coordinates round-trip, invalid configurations are rejected, and every family sits on its mesh.
Great: Each composition rule has a rejected configuration naming the exact rule broken.
Balance and capacity
Pass: Capacity math predicts drops and pads, and balancing losses match expected values.
Great: Skewed routing shows where the buffer overflows, and bias routing is contrasted without gradients.
Cost honesty
Pass: Bytes count both exchanges, placement names contended links, and one residual is kept.
Great: CPU timings stay labeled as accounting checks, and the hotspot diagnosis names rank and expert.
Resources
- Ultra-Scale Playbook - Expert ParallelismHugging Face
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.
- TorchFeather - Expert ParallelismTorchFeather
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. - Mixture of Experts ExplainedHugging Face
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.
- How to Scale Your Model - Expert Parallelism on GPUsGoogle DeepMind
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.
- GShard: Scaling Giant Models with Conditional Computation and Automatic ShardingLepikhin et al. (ICLR 2021)
Read the mixture-of-experts layer: top-2 gating, expert capacity, the auxiliary loss and random routing. Compare its capacity rule with Switch.
- ST-MoE: Designing Stable and Transferable Sparse Expert ModelsZoph et al. (2022)
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)
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
Solve Top-k MoE Router with Load Statistics, including its lower-index tie rule, before building dispatch.
- TorchTitan - MoE Model and ParallelismPyTorch
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
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.
- Bumblebee - Router, Dispatcher and Expert PrimitivesBumblebee
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. - PyTorch Distributed - all-to-all collectivePyTorch
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.