FFN & MoE · January 2021
Switch Transformer
intermediate
routing
Strip the MoE routing decision down to its simplest possible form: route each token to a single expert. Demonstrate that this 'simplification' actually trains stably at trillion-parameter scale.
§ 1 · Premise
Why top-2 was thought necessary
By late 2020 the sparse-MoE state of the art was GShard (Lepikhin et al. 2020, arXiv 2006.16668): top-2 routing across hundreds of experts at 600B encoder-only parameters. The top-2 choice was inherited from Sparse-MoE 2017 (Shazeer et al., arXiv 1701.06538, §2.1) for what appeared to be a load-bearing reason. Consider a gating network that picks expert per token. The forward pass uses only the selected expert ; the backward pass only updates along the direction that produced ‘s score. The runner-up expert receives no gradient signal — so the gate cannot learn that it should have been chosen instead. The 2017 paper argued this was a stability hazard and made a recipe-level requirement.
Switch Transformer (Fedus et al. 2021, arXiv 2101.03961) makes the contrarian bet: trains fine if you (a) multiply the selected expert’s output by its gate value so the gate gets a multiplicative gradient even at the chosen expert, (b) introduce an expert capacity dispatch cap that absorbs imbalance via token dropping rather than via routing softness, and (c) use a calibrated auxiliary load-balancing loss that scales correctly with the expert count. The bet pays off: Switch-C at 1.6T parameters with 64B active per token matches dense T5-XXL quality on multilingual translation and C4 span corruption at ~7× lower per-token FLOPs (Fedus et al. 2021, Table 9).
For the foundational top-K-gating machinery and the noisy-top-K trick on which Switch builds, see Sparse MoE. This entry walks the three things Switch changes: the top-1 routing form with multiplicative gating, the capacity-factor dispatch contract, and the simplified single-term load-balance loss that every subsequent top-K MoE (Mixtral, ST-MoE, DeepSeekMoE V2) copies.
§ 2 · Derivation
Top-1 routing, capacity factor, simplified balance loss
Top-1 routing. Let be the gate logits and the gate distribution. The Switch layer routes to the single expert with the largest logit:
The crucial detail (Fedus et al. 2021, eq. 1) is the multiplicative gate value in front of the expert output. The argmax is non-differentiable, but the value is differentiable in , and the chain rule gives — a gradient signal proportional to the expert’s output. This is the equivalent of saying “if produced a useful output, increase its gate score; if it produced noise, decrease it.” The runner-up expert still gets no signal — but Switch’s experiments show this does not matter in practice once the gate is properly regularized.
Why the gate must enter multiplicatively. Without the prefactor — i.e., if the layer were simply — the only gradient path back to the gate is via , which is identically zero almost everywhere. Multiplicative gating turns the gate’s output magnitude into a tunable scalar that the loss can adjust, and this single scalar is what closes the gradient loop.
Expert capacity. Top-1 routing has a hardware problem that top-K with partially hides: a popular expert can be selected by many more tokens in a batch than it has allocated compute slots for. Under top-2, overflow tokens fall back to their second-choice expert with the appropriate gate-weight renormalization. Under top-1 there is no second choice. Switch formalizes the dispatch contract with an expert capacity parameter (Fedus et al. 2021, eq. 3):
Each expert handles at most tokens per batch step. If — i.e., expert is over-subscribed — the excess tokens are dropped: their output for that layer is the identity (the residual stream passes through unchanged). is the theoretical minimum (no slack); is GShard’s default (100% slack); is the Switch default, a 25% slack budget.
Why drop rather than soften? Dropping makes the dispatch step a fixed-size scatter-gather: a buffer that every expert sees, regardless of how spiky the routing was. The kernel becomes a single batched matmul instead of variable- size matmuls. The cost is that some tokens at some layers have a forward pass equivalent to , which is a quality regression — but only on the fraction of tokens that overflow, which the auxiliary loss is meant to keep small.
The auxiliary load-balance loss. Sparse-MoE 2017 (Shazeer et al., §4) used two coupled loss terms — an importance loss on the per-expert sum of gate weights, and a load loss on the per-expert dispatched-token count. Switch replaces these with a single product (Fedus et al. 2021, eq. 4):
where for a batch of tokens
- is the fraction of tokens routed to expert (a post-argmax count, so non-differentiable);
- is the mean softmax probability assigned to expert by the gate (differentiable in );
- is a scalar (default );
- the leading factor rescales so ‘s effect is approximately constant across .
The loss is minimized when both and are uniform at . At that minimum . At maximum imbalance (all tokens to one expert) the loss approaches . The factor- normalization keeps the loss range rather than , which would shrink to zero as grows.
Why the product form, not the sum of squares. A natural alternative is or — penalize squared deviations from uniform. Both fail. The form has no gradient on the gate weights since is non-differentiable. The form has a gradient but it does not depend on actual routing decisions — the gate can satisfy “uniform ” while still routing all tokens to one expert (by making the top-1 spike-out happen via rather than via softmax magnitudes). The product is differentiable in via the factor and measures actual dispatch via the factor, so its gradient correctly pulls the gate toward choices that distribute the empirical traffic uniformly.
Why must be small. The auxiliary loss’s gradient pulls away from the gate’s purely accuracy-driven choice. If is too large the gate is forced to near-uniform routing and the experts cannot specialize. Fedus et al. 2021 §3.2 sweep and find a wide plateau around where load balance stays acceptable and accuracy is not measurably hurt.
Parameter and FLOP accounting. A Switch layer stores expert FFNs plus the gate: parameters. Per token only expert fires, so per-token FFN FLOPs equal a single dense FFN’s. Switch-C uses at the trillion-parameter total (Fedus et al. 2021, Table 9), giving an active/total ratio of for the MoE blocks — the most aggressive sparsity ratio in the pre-DeepSeek MoE literature.
§ 3 · Reference implementation
Top-1 with capacity-bounded dispatch
def switch_layer(x, experts, gate, capacity_factor=1.25):
# x: [B, T, d_model] gate.weight: [E, d_model] experts: list of E FFNs
B, T, d = x.shape
E = len(experts)
logits = gate(x.view(B * T, d)) # [B*T, E]
probs = logits.softmax(-1) # [B*T, E]
top_p, top_idx = probs.max(-1) # both [B*T]
capacity = int((B * T / E) * capacity_factor) # tokens per expert per step
out = torch.zeros_like(x.view(B * T, d))
for i in range(E):
token_mask = (top_idx == i)
token_pos = token_mask.nonzero(as_tuple=True)[0][:capacity] # drop overflow
if token_pos.numel() == 0:
continue
# Multiplicative gating: expert output scaled by its gate probability
out[token_pos] = top_p[token_pos, None] * experts[i](x.view(B * T, d)[token_pos])
return out.view(B, T, d)
def switch_aux_loss(probs, top_idx, alpha=0.01):
# probs: [B*T, E] top_idx: [B*T]
E = probs.shape[-1]
P = probs.mean(0) # [E] mean gate softmax
f = torch.bincount(top_idx, minlength=E).float() / top_idx.numel() # [E] dispatch fraction
return alpha * E * (f * P).sum()
§ 4 · Empirical evidence
What Switch demonstrated and what followed
Quality vs FLOP-matched dense. Fedus et al. 2021 Table 5 reports Switch-Base (220M active / 7.4B total, ) matching T5-Base on the C4 span-corruption objective in ~6× fewer training steps (negative log perplexity, batched over multilingual data). Switch-Large (800M active / 26B total) similarly beats T5-Large. The headline result is Switch-C (1.6T total, 64B active, , distributed across 64 TPU pods) matching dense T5-XXL on the same objective at ~7× lower per-token FLOPs — the trillion-parameter sparse-MoE existence proof.
Capacity-factor sensitivity. Table 4 sweeps for Switch-Base at . drops ~16% of tokens and loses 0.3 perplexity points vs ; recovers the perplexity but doubles the dispatch buffer and only marginally improves over . The Switch default is the chosen plateau point.
Top-1 vs top-2 head-to-head. Fedus et al. 2021 §4.2 includes a controlled top-1 vs top-2 comparison at matched activated-parameter count. Top-1 is 1.6× faster per step but loses 0.5–1.2 perplexity points depending on . The team’s argument: at very large the quality gap closes, and the throughput advantage outweighs the residual quality cost. ST-MoE (Zoph et al. 2022, arXiv 2202.08906, Table 5) later revisits this ablation at production scale and finds top-2 retains a 0.3–0.6 perplexity advantage; the field largely returned to top-2 (Mixtral) or top-K with (DeepSeek V3) on these grounds.
Routing jitter — a Switch-specific stability trick. §2.2 introduces router z-loss-style multiplicative noise on the gate’s input embeddings during training. The noise is small () and serves the same purpose as Sparse-MoE 2017’s noisy-top-K: it randomizes a small fraction of routing decisions to keep all experts receiving some gradient signal. ST-MoE (Zoph et al. 2022, §3.4) refines this into the router z-loss , which penalizes large gate logits and stabilizes training in low-precision regimes — a fix for fp16 underflow in the gate softmax that Switch’s plain jitter does not address.
Auxiliary loss in subsequent literature. The single-term form is reused unchanged in GShard variants, ST-MoE, Mixtral, OLMoE, and DeepSeekMoE V2 (Dai et al. 2024 §3.2 cites Switch’s eq. 4 directly). DeepSeek V3 (Wang et al. 2024, arXiv 2408.15664) introduces the aux-loss-free bias variant that replaces this loss but acknowledges Switch’s formulation as the baseline it competes against — see aux-loss-free routing.
Direct adoption in modern decoders. None. Production decoder LLMs have moved to top-K with (Mixtral, OLMoE) or to fine-grained-plus-shared (DeepSeekMoE, Kimi K2). The top-1 form persists in encoder-only or vision MoE work (V-MoE, Riquelme et al. 2021, arXiv 2106.05974) where the quality cost is more tolerable, but the decoder-LLM lineage has moved past it. Switch’s lasting contributions are the load-balance loss formula, the capacity-factor framing of dispatch contracts, and the trillion-parameter existence proof — not the top-1 routing itself.
Reproductions at scale. Switch’s open-source code (Fedus et al.’s mesh-tensorflow implementation) has been independently re-run at smaller sizes by the OLMoE team (Muennighoff et al. 2024 §6.1), who confirm the , top-1 Switch baseline trains to within 0.1 perplexity points of the originally reported numbers. The trillion-parameter Switch-C result has not been independently reproduced — the compute requirements rule out academic verification.
Lineage
- Predecessors
- GShardGShard
- Successors
- Mixtral-Style Coarse MoEMixtral MoE
Cite
BibTeX entry for the original paper
@article{arxiv2101_03961,
title = {Switch Transformers: Scaling to Trillion Parameter Models with Simple and Efficient Sparsity},
author = {William Fedus, Barret Zoph, Noam Shazeer},
year = {2021},
eprint = {2101.03961},
archivePrefix = {arXiv},
url = {https://arxiv.org/abs/2101.03961}
} Or cite the paper directly: arXiv:2101.03961.
Export
BibTeX
@article{arxiv_2101_03961,
title = {Switch Transformers: Scaling to Trillion Parameter Models with Simple and Efficient Sparsity},
author = {William Fedus and Barret Zoph and Noam Shazeer},
year = {2021},
eprint = {2101.03961},
archivePrefix = {arXiv},
url = {https://arxiv.org/abs/2101.03961}
} CSL JSON
{
"id": "arxiv_2101_03961",
"type": "article-journal",
"title": "Switch Transformers: Scaling to Trillion Parameter Models with Simple and Efficient Sparsity",
"author": [
{
"literal": "William Fedus"
},
{
"literal": "Barret Zoph"
},
{
"literal": "Noam Shazeer"
}
],
"issued": {
"date-parts": [
[
2021
]
]
},
"URL": "https://arxiv.org/abs/2101.03961",
"number": "2101.03961",
"source": "arXiv"
} RIS
TY - JOUR
TI - Switch Transformers: Scaling to Trillion Parameter Models with Simple and Efficient Sparsity
AU - William Fedus
AU - Barret Zoph
AU - Noam Shazeer
PY - 2021
JO - arXiv
AN - arXiv:2101.03961
UR - https://arxiv.org/abs/2101.03961
ER -