Pidoku

GShard (2020) and Switch Transformer (2021)

Module 30 Lesson 30.e 40 min Difficulty 3/5

Prerequisites 14.g, 14.e, 9.f


Before you start: words you need#

WordPlain meaning
Mixture of experts (MoE)A layer with many feed-forward networks (“experts”); each token is sent to only a few of them (14.g).
Router / gateA small learned layer that scores the experts for each token and picks the top-k.
Top-k routingSend each token to its k best-scoring experts (k = 2 in GShard, k = 1 in Switch).
Sparse activationOnly a fraction of the model’s parameters do work for any one token, so cost per token stays low while total parameters grow.
Expert capacityThe most tokens one expert accepts per batch. Extra tokens are dropped (skip the layer through the residual connection).
Capacity factorCapacity as a multiple of the even share: 1.0 means exactly tokens ÷ experts; 1.25 gives 25% slack.
Auxiliary (load-balancing) lossAn extra training penalty that pushes the router to spread tokens evenly over experts.
All-to-allA collective in which every device sends a different piece of data to every other device (9.f). How tokens reach experts on other chips.
Expert parallelismPutting different experts on different devices (14.e).

1. The paper#

  • GShard: Dmitry Lepikhin, HyoukJoong Lee, Yuanzhong Xu, Dehao Chen, Orhan Firat, Yanping Huang, Maxim Krikun, Noam Shazeer, Zhifeng Chen (Google), “GShard: Scaling Giant Models with Conditional Computation and Automatic Sharding”, ICLR 2021. arXiv:2006.16668
  • Switch Transformer: William Fedus, Barret Zoph, Noam Shazeer (Google), “Switch Transformers: Scaling to Trillion Parameter Models with Simple and Efficient Sparsity”, Journal of Machine Learning Research 23 (2022). arXiv:2101.03961

The MoE layer itself is older (Shazeer et al., 2017, for LSTMs). These two papers put it inside the Transformer and showed how to train it across thousands of chips.


2. The problem#

Bigger models were better, but a dense model’s cost per token grows with every parameter. Training a 2.3-billion-parameter dense translation model took GShard’s authors six weeks on 2048 TPU v3 cores. Going ten or a hundred times bigger that way was out of reach.

MoE promised a way out: many parameters, few used per token. But in 2020 it had three practical problems: it was complex to implement across devices, the router could collapse onto a few experts, and training was unstable, especially in low precision.


3. The idea#

GShard: replace every other feed-forward layer of a Transformer with an MoE layer, route each token to its top 2 experts, put one expert per device, and move tokens with all-to-all. Keep it balanced with an expert capacity, an auxiliary loss, and local groups (the batch is split into groups, each with its own share of every expert’s capacity). Write the model once with sharding annotations and let the XLA compiler partition it.

Switch: make it simpler. Route each token to one expert (top-1). That halves the expert compute, shrinks the router, and halves the communication, and the authors found quality held up. Add three training tricks: compute the router in float32 inside an otherwise bfloat16 model (“selective precision”), scale down the weight initialisation, and use more dropout inside experts when fine-tuning.

flowchart LR
  T["Tokens on device 0..N"] --> R["Router: score experts, pick top-k"]
  R --> A2A1["All-to-all: send tokens to expert devices"]
  A2A1 --> E["Experts: each device runs its own FFN on the tokens it got"]
  E --> A2A2["All-to-all: send results back"]
  A2A2 --> C["Combine: weighted sum + residual"]
  class T io
  class R,C neutral
  class A2A1,A2A2 queue
  class E compute

4. How it works#

Routing. The router is one linear layer plus softmax over experts. GShard takes the top 2 (the second only with probability proportional to its weight, “random routing”); Switch takes the top 1. The expert output is multiplied by its gate value, so the router gets a gradient.

Capacity. Each expert accepts at most (tokens ÷ experts) × capacity factor tokens per batch. Overflow tokens are dropped from that layer. Switch found top-1 routing worked best at low capacity factors (1.0–1.25), which saves memory and communication.

Balancing. The auxiliary loss multiplies, for each expert, the fraction of tokens it received by the average router probability it was given, and sums. It is smallest when both are uniform.

Sharding. GShard’s main systems contribution is the compiler side: annotate a few tensors with how to split them, and XLA’s SPMD partitioner produces one program that runs on all 2048 devices, with compile time that does not grow with the device count.

For inference, two consequences matter (taught in 14.h):

  • Each token’s compute is that of a small dense model, but all experts must sit in memory. MoE trades FLOPs for memory capacity and network traffic.
  • At small batch, each expert sees few tokens, so its GEMMs are skinny and memory-bound. MoE serving needs large batches spread over many devices (wide expert parallelism) to be efficient.

5. Results#

GShard (translation from 100 languages to English):

ModelTrainingCost (TPU v3 core-years)Average ΔBLEU over bilingual baselines
Dense Transformer, 2.3 B2048 cores, 6 weeks235.56.1
MoE, 37 B (128 experts, 36 layers)–about 68.2
MoE, 600 B (2048 experts, 36 layers)2048 cores, 4 days2213.5

Model size grew 16× (37.5 B → 600 B in the paper’s Figure 1) while training cost grew 3.6× (6 → 22 core-years). Training all 100 bilingual baselines separately would have cost 29 core-years.

Switch Transformer (C4 pretraining):

ComparisonResult
Switch-Base vs T5-Base, same FLOPs per tokenup to 7× faster to the same quality
Switch-Base (capacity 1.0) vs top-2 MoE-Base (capacity 1.0)62.8 h vs 80.1 h to quality threshold; 1000 vs 860 examples/s (Table 1)
Multilingual (101 languages) vs mT5-Basegains on all 101; 91% of languages at least 4× faster
Switch-C: 1,571 B parameters, 2048 experts4× faster than T5-XXL to a fixed perplexity
Distillation into a small dense modelup to 99% smaller while keeping about 30% of the quality gain
Selective precision (router in float32)stable like float32 training, speed of bfloat16 (1390 vs 1160 examples/s)

A finding the paper flags itself: Switch-C (1.6 T parameters) matched the smaller Switch-XXL (395 B) on pretraining perplexity but did worse on some fine-tuned tasks. Total parameters are not the whole story; FLOPs per token matter too.


6. What held up and what did not#

Held up

  • MoE Transformers won. Most frontier open models in 2026 are MoE: DeepSeek-V3 and successors (256 routed experts, top-8, plus a shared expert), Qwen3’s large models (128 experts, top-8), Kimi K2 (384 experts), gpt-oss, Llama 4 and others.
  • All-to-all for expert parallelism. Still how tokens reach experts (DeepEP, 25.i).
  • Float32 routers and careful initialisation remain standard practice.

Did not, or changed

  • Top-1 did not stick. Modern models use top-k with k = 4 to 8 over many small, fine-grained experts, plus one or more shared experts every token uses (DeepSeekMoE, 2024).
  • Token dropping is gone from inference. Engines run “dropless” MoE: every token reaches its experts, and kernels such as grouped GEMM (25.h) handle uneven loads. Dropping tokens changes answers depending on what else is in the batch, which is unacceptable when serving.
  • Auxiliary losses were softened. DeepSeek-V3 (30.v) balances with a per-expert bias instead, with only a tiny sequence-level auxiliary loss, because the auxiliary loss itself hurts quality.
  • Every other layer → almost every layer. Most 2026 MoE models put experts in nearly all layers.
  • Imbalance is now a serving problem. Hot experts at inference time are handled by replicating them (EPLB-style load balancers; 14.h), not by capacity limits.

7. Where it lives in 2026#

  • Taught in depth: MoE in 14.g, expert parallelism in 14.e, MoE serving in 14.h, MoE kernels in 25.h, the all-to-all in 9.f.
  • Engines: vLLM, SGLang and TensorRT-LLM all serve MoE with expert parallelism over NVLink domains such as NVL72 racks (7.d), with DeepEP-style all-to-all kernels.
  • The Switch paper’s GitHub pointers (t5x, mesh_tensorflow) are historical; GShard’s SPMD partitioner lives on inside XLA and JAX’s sharding system (jax.sharding, used on TPUs; 7.i).

Checked on 13 November 2026.


8. Questions#

  1. Why does an MoE model cost about the same FLOPs per token as a much smaller dense model?
  2. What is a capacity factor, and what happens to a token when its expert is full?
  3. Why did Switch use top-1 routing, and why did later models go back to top-k with k ≥ 2?
  4. Why is token dropping acceptable in training but not in serving?
  5. Which collective moves tokens to experts, and why is it hard on a slow network?
  6. An MoE model has 671 B parameters and activates 37 B per token. What does that mean for memory versus compute when serving it?
  7. What did DeepSeek-V3 change about load balancing, and why?

9. Further reading#

↑↓ navigate↵ openesc close

100% drag to pan · scroll or pinch to zoom · double-click to zoom in