Paper commentary
Switch Transformers: Scaling to Trillion Parameter Models with Simple and Efficient Sparsity
- Problem Dense Transformers gain quality mainly by growing every weight and every computation per example together. Earlier sparse Mixture-of-Experts models were hard to adopt because of routing complexity, communication cost and training instability.
- Method Routes each token to one expert with a top-1 gate, caps each expert's batch with a capacity factor, adds a load-balancing loss, and casts only the router to float32 so that bfloat16 training stays stable.
- Results A 64-expert Switch-Base matched T5-Base's negative log perplexity at step 60k only at step 450k, a 7.5x step-time speedup.
Why add parameters without adding compute
Growing a dense Transformer raises the parameter count and the computation per example together, so a larger model is also a more expensive one to train. The authors treat parameter count as a separate scaling axis: they keep the floating point operations per example fixed and add parameters through sparse activation. The scaling study they build on reports power-law gains with model size, data and compute, and it favours large models trained on comparatively little data.
Mixture-of-Experts layers already activate only part of the network for each input, but their adoption had been held back by complexity, communication costs and training instability. Sparse computation also fits poorly with libraries and accelerators that were built around dense matrix multiplication. The Switch Transformer targets these obstacles, and its Mixture-of-Experts baseline in the comparisons uses top-2 routing.
Routing each token to one expert
A Mixture-of-Experts layer takes a token representation x and sends it to the best top-k experts out of N experts. A router weight matrix \(W_r\) maps x to logits \(h(x) = W_r \cdot x\), and a softmax over the N experts turns the logits into gate values \(p_i(x) = e^{h(x)_i} / \sum_{j=1}^{N} e^{h(x)_j}\). The layer output is the gate-weighted sum of the selected experts' outputs, \(y = \sum_{i \in T} p_i(x) E_i(x)\), where T is the set of selected indices.
The Switch layer uses k = 1, so T holds one index and the output is the chosen expert's output scaled by its gate value, with the gate keeping the router differentiable. Figure 2 shows two tokens in an encoder block, each routed independently to one of four FFN experts, with the output of the selected expert multiplied by the router gate value.
The authors list three gains from routing to one expert: the router computes less, each expert's batch can be at least halved, and the routing implementation and its communication are simpler. They report that this simplification preserves model quality and performs better than the top-k routing it replaces.
Expert capacity and dropped tokens
TPU compilation needs statically declared tensor shapes, but routing decisions change at run time, so each expert is given a fixed capacity. Expert capacity is the tokens per batch divided by the number of experts, multiplied by a capacity factor: \[ \text{expert capacity} = \left( \frac{\text{tokens per batch}}{\text{number of experts}} \right) \times \text{capacity factor} \]
A capacity factor above 1.0 gives buffer for unevenly routed tokens, while a token that overflows its expert is dropped: its expert computation is skipped and the token passes to the next layer through the residual connection. Larger capacity lowers drops but wastes computation and memory, and Figure 3 shows the same routing at capacity factors 1.0 and 1.5 with overflowed and padded slots.
Across the reported experiments the dropped-token fraction was typically below 1%, with no dependence on the number of experts that the authors could detect, provided the load-balancing coefficient was high enough.
Keeping experts evenly loaded
Each Switch layer adds an auxiliary loss to the total training loss, a simplified form of the separate load-balancing and importance-weighting losses in the original Mixture-of-Experts design. For N experts and a batch B of T tokens, the auxiliary loss is \[ \text{loss} = \alpha \cdot N \cdot \sum_{i=1}^{N} f_i \cdot P_i \] Here \(f_i = \frac{1}{T} \sum_{x \in B} \mathbb{1}\{\arg\max p(x) = i\}\) is the fraction of tokens dispatched to expert i, and \(P_i = \frac{1}{T} \sum_{x \in B} p_i(x)\) is the average router probability given to expert i.
The P vector is differentiable while the f vector is not, because f comes from hard argmax assignments.
Switch versus MoE and T5 at matched compute
Models are pre-trained on C4 with masked language modeling, where 15% of tokens are dropped out and replaced by a single sentinel token. Quality is reported as negative log perplexity, where higher is better; Switch-Base is FLOP-matched to T5-Base, while MoE-Base passes each token through two expert FFNs and so uses more FLOPs.
Table 1 reports T5-Base at -1.731 quality after 100k steps and 1600 examples/sec, and T5-Base did not reach the -1.50 threshold within 100k steps. At capacity factor 1.0, Switch-Base reaches -1.561 quality at 1000 examples/sec and crosses the -1.50 threshold in 62.8 hours, while MoE-Base reaches -1.572 at 860 examples/sec and needs 80.1 hours.
All MoE and Switch models use 128 experts, placed at every other feed-forward layer, and were trained on 32 TPUv3 cores.
Switch-Base+ widens the hidden size from 768 to 896 and the heads from 14 to 16 until its speed matches MoE-Base, and it reaches -1.534 quality in 67.6 hours. Speed also depends on implementation: the MoE model at capacity factor 2.0 to 1.25 slowed from 840 to 790 examples/sec, which the authors call unexpected.
The authors draw three findings: Switch-Base beats both dense and MoE models on a speed-quality basis, it has a smaller computational footprint than MoE, and it performs better at capacity factors 1.0 and 1.25. In the authors' view, small expert capacities matter in large-model regimes where model memory is scarce.
Keeping bfloat16 training stable
Hard routing decisions can destabilize training, and low-precision formats such as bfloat16 make the problem worse, particularly in the softmax computation of the router. Lepikhin et al. (2020) avoided the problem by training their MoE Transformer in float32 throughout.
The authors cast only the router input to float32, inside the router function on each device, and recast the resulting dispatch and combine tensors to bfloat16 at the end of the function. Because the float32 values stay local to the device, the layer does not have to send float32 tensors through all-to-all communication while keeping the stability of float32.
Table 2 compares Switch-Base in float32, which reaches -1.718 at 1160 examples/sec, with bfloat16, which diverged at -3.780 with 1390 examples/sec, and with selective precision, which reaches -1.716 at 1390 examples/sec. These runs use a 32-expert model measured after a fixed early step count, so they compare learning dynamics early in training.
The authors recommend reducing the default Transformer scale s = 1.0 by a factor of 10, and they use this scheme for models from the 223M-parameter baseline up to over one trillion parameters.
Table 3 records a 32-expert model after 3.5k steps over 3 random seeds: the 0.1x initialization reaches an average of -2.72 with standard deviation 0.01, against -3.60 with standard deviation 0.68 for the 1.0x initialization.
Expert dropout during fine-tuning
Switch models have significantly more parameters than their FLOP-matched dense counterpart, and fine-tuning sets are often small, so overfitting becomes a larger risk. T5 applies dropout at each layer during fine-tuning to limit overfitting.
Expert dropout raises the dropout rate only in the interim feed-forward computation inside each expert, and keeps a standard rate at the non-expert layers.
Table 4 sweeps dropout for Switch-Base pre-trained on 34B tokens of C4, and compares it with T5-Base at dropout 0.1, which scores 82.9 on GLUE. Switch-Base at dropout 0.1, 0.2 and 0.3 scores 84.7, 84.4 and 83.9 on GLUE, and raising dropout across all layers led to worse performance in these runs. Setting dropout 0.1 at non-expert layers and 0.4 inside experts gives 85.2 on GLUE, 83.7 on SQuAD and 73.0 on SuperGLUE.
The authors report that this split improves performance on four smaller downstream tasks.
Scaling with the number of experts
The study uses the C4 corpus with over 180B target tokens and trains until returns diminish, so data is not the bottleneck. The number of experts is treated as the most efficient scaling dimension, because each token still runs through one expert whatever the number of experts. The router's extra cost is O(d_model × num experts), which is light next to the expert computation.
Figure 4 doubles the expert count from 2 up to 256 at fixed FLOPs per token, starting from the 223M-parameter T5-Base and ending at a 256-expert model of 14.7B parameters. On a step basis, the Switch-Base 64-expert model matched T5-Base at step 60k only at step 450k, which the authors count as a 7.5x speedup in step time.
The authors also report that larger models are more sample efficient, learning faster for a fixed number of observed tokens, in line with Kaplan et al. (2020). The gains on a step basis do not yet say whether wall-clock time improves in the same way.
Wall-clock speed and a larger dense baseline
Switch layers add communication across devices and the extra computation of routing, so step gains may not carry over to training time. The authors therefore measure quality against training time at a fixed compute budget.
All models in Figure 5 are trained on 32 TPUv3 cores with equal FLOPs per example. In this setting the 64-expert Switch-Base reaches the same quality in one-seventh of the time T5-Base needs, and it keeps improving afterwards.
Figure 6 compares Switch-Base with T5-Large, which applies 3.5x more FLOPs per token. Switch-Base is still more sample efficient per step, and on wall-clock time it yields a 2.5x speedup over T5-Large. The authors then build Switch-Large, a sparse model FLOP-matched to T5-Large, and examine its scaling and fine-tuning in the next section.
Distillation, multilingual pre-training and downstream gains
The authors distill a fine-tuned Switch-Base teacher into T5-Base using a mixture of hard and soft losses. The stated result is that the student preserves 30% of the quality gain of the sparse teacher.
The introduction claims model-size reductions of up to 99%, while the SuperGLUE distillation table describes a student compressed by 97%. The authors expect that significantly better and larger teachers would need larger students to reach such compression rates.
With 1M pre-training steps over all 101 languages considered, Switch increases the final negative log perplexity over the dense baseline on every one of them. Figure 7 shows these per-language improvements for Switch T5 Base relative to the dense baseline in multi-task training.
Measured as the step speedup needed to reach the same quality, Switch averages 5x over mT5-Base across the 101 languages. For 91% of languages the speedup is 4x or more, and the speedup is the ratio of baseline steps to the steps the model needs for the same quality.
Combining data, model and expert parallelism
Widening d_model or d_ff raises both parameters and computation per token, and the limit is the memory of each accelerator. Once a model exceeds that memory, single program multiple data model parallelism is needed.
In the FFN the intermediate is \(h = x W_{in}\) and the output is \(y = \text{ReLU}(h) W_{out}\), with \(W_{in}\) of size [d_model, d_ff] and \(W_{out}\) of size [d_ff, d_model]. The cores form a two-dimensional logical mesh with n data-parallel and m model-parallel ways, so N = n × m, and the E experts, each processing up to C tokens, form a further dimension.
Data parallelism sets n = N and m = 1, so no communication is needed until the gradients are aggregated after the full forward and backward pass. Model parallelism sets n = 1 and m = N, so each core holds a slice of the weights, and whenever a partitioned dimension must be summed an all-reduce is added to the forward and backward pass.
Switch layers add all-to-all communication: tokens routed locally are sent to the cores that hold their expert and are sent back to be combined with the router probabilities. The Mesh TensorFlow pseudocode builds the dispatch and combine tensors per core before this exchange.
Pre-training up to trillion-parameter models gives a 4x speedup over the T5-XXL model.
Limitations
The stability techniques that held for Switch-Base, Switch-Large and Switch-C were not sufficient for Switch-XXL, and the authors describe stability for the largest models as unsolved.
Better pre-training does not always carry over downstream: Switch-C with 1.6T parameters reaches 87.7 exact match on SQuAD, below the 89.6 of Switch-XXL, despite similar C4 perplexities. The authors note that Switch-XXL applies about 10x the FLOPS per token of Switch-C while having about 4x fewer unique parameters, and they call this dependence poorly understood.
The largest Switch models also do not always translate upstream perplexity into SuperGLUE fine-tuning results.
With float32, attention-based experts reach -1.524 after 100k steps against -1.548 for expert FFNs, and FFN plus attention experts reach -1.513 at 1240 examples/sec against 1480 for expert FFNs alone. Attention experts diverged in bfloat16, so the final model keeps expert layers only in the feed-forward part, and this variant is left for future work.
Speed numbers reflect both the algorithm and the implementation, so the efficiency gains depend on the hardware and software used. The authors state that they cannot fully preserve model quality under compression, and that distillation keeps about 30% of the quality gain of the expert model.
Open directions the authors list include heterogeneous experts, expert layers outside the FFN, and modalities beyond language.
Paper info
- Authors
- William Fedus, Barret Zoph, Noam Shazeer
- Venue
- arXiv · Preprint
- First published
- 2021-1-11
- Added
- 2026-10-20
- Updated
- 2022-6-16
- Version
- v3
- arXiv
- 2101.03961
- Category
- cs.LG · cs.AI
- Original paper terms
- arXiv non-exclusive 1.0
- What we show
- Paper info and commentary
AI commentary is a separate explanation from the original paper and may contain errors. The original paper and figures follow each source's terms of use and any rights noted separately.
Quote from the paper
William Fedus, Barret Zoph, Noam Shazeer (2021). Switch Transformers: Scaling to Trillion Parameter Models with Simple and Efficient Sparsity. arXiv:2101.03961v3. https://arxiv.org/abs/2101.03961v3
BibTeX
@misc{fedus2021switch,
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.03961v3}
}