skip to content

Search

← The Engine Room

Part III — The Transformer-Industrial Complex (2020–2025)

Mixture of Experts

Router softmax, top-k dispatch, load-balancing losses — paying for a fraction of the model.

2017–2025 14 min read 4 papers

It is January 2017. The Transformer does not exist yet — sequence models are still stacks of LSTM layers, the architecture Chapter 3 of the language-models book covers — and a team at Google has already run into the problem this chapter is about: making a dense neural network bigger multiplies the compute spent on every single token, whether or not that token needed the extra capacity. A paper published that month proposes a layer that is, in its own words, outrageously large in parameter count while staying nearly constant in per-token compute. It will take four more years to reach the Transformer, and eight more to reach a 671-billion-parameter model trained for $5.576 million.

Where We Were

A dense feed-forward or LSTM layer touches every one of its parameters for every input it processes. Doubling a dense layer’s width doubles both its parameter count and the multiply-accumulate operations needed to run it — capacity and compute move in lockstep, by construction. Chapters 8 and 9 cover how data, tensor, and pipeline parallelism let a team spread that compute across more accelerators, but none of those techniques break the lockstep: they let a bigger dense model fit and run faster across a cluster, without changing the fact that every added parameter costs every token something.

Conditional computation — the idea that different inputs might route through different parts of a network, so most of the network can stay idle for most inputs — had been proposed in various forms through the early-to-mid 2010s, but no version of it scaled past small experiments. The obstacle was not the idea but the engineering: choosing a subset of a network to activate is a discrete decision, discrete decisions do not have gradients, and any routing mechanism has to keep the units it activates busy with enough work to justify the communication cost of getting inputs to them across accelerators, or the exercise saves no wall-clock time even as it saves arithmetic.

Mixture of experts sits outside the mainstream for four years before it returns The routing idea predates the Transformer by five months, then waits for a reason to matter again. Grey monospace text names the paper or chapter behind each step. Sep 2014 seq2seq Encoder-decoder LSTMs language models 2015 Stacked LSTMs Language models keep deepening capacity pressure 2016 Multi-node clusters Dense training becomes routine Chapter 8 Jan 2017 Sparsely-gated MoE Capacity decoupled from per-token compute Shazeer et al. the architecture of the day capacity pressure the alternative fix the routing idea
The routing idea arrives five months before the Transformer, then sits outside the mainstream for four years before it matters again. PNG ↓

The Breakthrough

Shazeer et al. at Google proposed the Sparsely-Gated Mixture-of-Experts layer in January 2017 (Shazeer et al., 2017), inserting it between stacked LSTM layers rather than replacing a single feed-forward block. The layer holds up to thousands of identically-shaped feed-forward sub-networks — the experts — plus a small trainable router that looks at each token’s representation and picks a handful of experts to send it to, ignoring the rest entirely. Because the router’s choice is discrete, gradients cannot flow through it directly by the usual chain rule; the paper’s routing function adds tunable Gaussian noise to the router’s raw scores before selecting the top few, which both makes the discrete choice differentiable in expectation and, as a side effect, spreads tokens more evenly across experts than a noiseless top-k choice would. Trained as part of language models with up to 137 billion parameters in the MoE layer alone, and using up to 131,072 experts in the largest configuration, the resulting models reported a perplexity of 28.0 on a 1-billion-word language modeling benchmark against a prior state of the art of 34.7, and a BLEU score of 40.56 on WMT’14 English-to-French translation against a 39.22 baseline — the paper’s own framing was “greater than 1000x improvements in model capacity with only minor losses in computational efficiency.”

The idea then sat mostly outside the mainstream for four years. The field’s attention shifted to the Transformer, published five months later in June 2017 — language models, Chapter 5 covers it in full — and reproducing Shazeer et al.’s routing and load-balancing machinery inside a Transformer’s feed-forward blocks, at a scale that justified the added engineering, took until Fedus, Zoph, and Shazeer published the Switch Transformer in January 2021 (Fedus et al., 2021). Switch Transformers made one deliberate simplification: instead of routing each token to several experts, as the original layer and most follow-up work did, it routes each token to exactly one — switch routing. Sending a token to a single expert rather than several cuts the communication volume between accelerators roughly in proportion to how many experts each token would otherwise have visited, and the paper reports that this simplification does not cost quality: a 64-expert Switch-Base model reached a T5-Base model’s quality roughly 7.5 times faster in training steps at matched compute, and the largest configuration, Switch-C, scaled to 2,048 experts and roughly 1.571 trillion parameters — the first published trillion-parameter model — training in bfloat16 without the instability the field had come to expect from models at that scale.

Reproducing that recipe at the scale that mattered next required an openly available model, and Mistral AI supplied one with Mixtral 8x7B in January 2024 (Jiang et al., 2024), continuing the open-weights lineage covered in language models, Chapter 14. Mixtral widens the router back out to top-2 — each token visits two of eight experts per layer — landing between Shazeer et al.’s top-k and Switch’s top-1. The released model holds 47 billion total parameters but uses only 13 billion of them for any given token, and Mistral AI reported it matching or exceeding a dense 70-billion-parameter Llama 2 model and GPT-3.5 on most benchmarks, including an MMLU score of 70.6% against Llama 2 70B’s 69.9% and GPT-3.5’s reported 70.0%, while running inference at roughly the cost of a 13-billion-parameter dense model. It shipped under the Apache 2.0 license, making it the first widely-used sparse MoE model that any team could inspect, fine-tune, or self-host.

DeepSeek-AI closed out this lineage in December 2024 with DeepSeek-V3, a 671-billion-parameter model activating 37 billion parameters per token (DeepSeek-AI, 2024). Its MoE layers use 256 routed experts plus one always-active shared expert, with each token routed to the shared expert and the top 8 of the 256 routed experts — more granular than Mixtral’s top-2-of-8, and without Switch’s single-expert bottleneck. DeepSeek-V3’s specific contribution to routing is a fix for a longstanding side effect of the auxiliary load-balancing losses every prior model in this lineage relied on: forcing traffic to be even across experts, via an extra loss term added to the training objective, pulls the model’s parameters away from whatever direction the main training loss alone would have chosen, at every single step. DeepSeek-V3 replaces that loss term with a per-expert bias that only affects which experts get picked, not the training objective at all — detailed under The Mathematics below — reporting a full training run — pretraining plus context extension plus post-training — of 2.788 million H800 GPU-hours, a total training cost of $5.576 million, and no irrecoverable loss spikes across the entire run. Trained in FP8 mixed precision (covered in Chapter 7), the resulting model reached an MMLU score of 87.1 against LLaMA 3.1 405B’s 84.4, and an MMLU-Pro score of 64.4 against 52.8 — a dense 405-billion-parameter model beaten on both by a sparse model activating well under a tenth of its total parameters per token.

Only a handful of experts ever run for a given token A router picks a small subset from up to thousands of identically-shaped feed-forward networks, and combines only their outputs. Token input representation Router one score per expert noisy top-k, Shazeer 2017 Top-k selection 1, 2, or 8 of N kept Expert 1 Expert 2 Expert N Combine weighted sum Output
Only a handful of experts ever run for a given token, so total compute per token stays fixed no matter how many thousand experts the layer holds. PNG ↓
Two of eight, in the order they light up Mixtral scores all eight experts in the layer, keeps only the top two, and blends just their outputs. Grey monospace text is the parameter count active. Scroll slowly, this plays once. Token arrives hidden state x one layer, 8 experts Router scores 8 one score per expert Softmax(Top2(xWg)) E1 E2 E3 E4 E5 E6 E7 E8 Weighted sum only E3 and E7's output 13B of 47B active one of the two experts chosen this pass scored, then ignored entirely Every token in the layer repeats this, and the two chosen experts differ token to token.
Mixtral scores all eight experts and keeps only two; the other six were never idle, they were simply outvoted. (animates as you scroll to it) PNG ↓
The router's fan-out widens, narrows, then widens again Each generation picks a different point on the same top-k spectrum. Sparsely-gated MoE 2017, LSTM stacks, noisy top-k Shazeer et al. Switch Transformer 2021, top-1, trillion params Fedus et al. Mixtral 8x7B 2024, top-2, open weights Jiang et al. DeepSeek-V3 2024, top-8 of 256, aux-loss-free DeepSeek-AI
The router's fan-out widens, narrows to one, then widens again; each generation picks a different point on the same top-k spectrum. PNG ↓

The Mathematics

Shazeer et al.’s router scores every expert for a given input, adds tunable noise to break ties and spread load, and keeps only the top kk scores before turning them into weights:

G(x)=Softmax(KeepTopK(H(x),k)),H(x)i=(xWg)i+N(0,1)⋅Softplus((xWnoise)i)G(x) = \mathrm{Softmax}\big(\mathrm{KeepTopK}(H(x), k)\big), \qquad H(x)_i = (x W_g)_i + \mathcal{N}(0,1) \cdot \mathrm{Softplus}\big((x W_{\mathrm{noise}})_i\big)
SymbolMeaning
xxtoken’s input representation
WgW_g, WnoiseW_{\mathrm{noise}}learned weights producing preference scores and noise scale
H(x)iH(x)_iexpert ii‘s noisy routing score
kknumber of experts kept per token
KeepTopK\mathrm{KeepTopK}zeroes every score outside the top kk
G(x)G(x)final gate vector, nonzero only for chosen experts

Switch Transformers simplify kk to 1 and route with a plain softmax over a linear projection h(x)h(x), sending each token’s full weight to a single expert:

pi(x)=exp⁡(h(x)i)∑j=1Nexp⁡(h(x)j)y=pi∗(x) Ei∗(x),i∗=arg⁡max⁡i pi(x)\begin{aligned} p_i(x) &= \frac{\exp\big(h(x)_i\big)}{\sum_{j=1}^{N}\exp\big(h(x)_j\big)} \\ y &= p_{i^*}(x)\, E_{i^*}(x), \qquad i^* = \arg\max_i\, p_i(x) \end{aligned}
SymbolMeaning
NNtotal number of experts in the layer
h(x)h(x)linear projection of the input used to score experts
pi(x)p_i(x)router’s softmax probability for expert ii
i∗i^*index of the single highest-probability expert
yylayer output
Ei∗E_{i^*}the selected expert’s feed-forward block

Sending every token to whichever expert scores highest, with nothing else, tends to concentrate traffic on a few popular experts and starve the rest — the exact failure mode the training data punishes as wasted capacity, not the router. Switch Transformers, like Shazeer et al. before them, counteract this with an auxiliary loss added to the training objective:

Laux=α N∑i=1Nfi Pi,fi=1T∑x∈B1{i∗(x)=i},Pi=1T∑x∈Bpi(x)\mathcal{L}_{\mathrm{aux}} = \alpha\, N \sum_{i=1}^{N} f_i\, P_i, \qquad f_i = \frac{1}{T}\sum_{x \in \mathcal{B}} \mathbb{1}\{i^*(x) = i\}, \qquad P_i = \frac{1}{T}\sum_{x \in \mathcal{B}} p_i(x)
SymbolMeaning
B\mathcal{B}, TTtraining batch and its token count
fif_ifraction of tokens actually routed to expert ii
PiP_ifraction of router probability mass assigned to expert ii
α\alphafixed coefficient weighting the auxiliary loss
NNtotal number of experts

Mixtral widens the router to the top 2 of NN experts and combines their outputs directly, with no separate load-balancing term appearing in its published objective:

G(x)=Softmax(Top2(xWg)),y=∑i=1NG(x)i SwiGLUi(x)G(x) = \mathrm{Softmax}\big(\mathrm{Top2}(x W_g)\big), \qquad y = \sum_{i=1}^{N} G(x)_i \, \mathrm{SwiGLU}_i(x)
SymbolMeaning
NNnumber of experts per layer, 8 in Mixtral
WgW_grouter’s learned weight matrix
Top2\mathrm{Top2}keeps the two highest scores, discards the rest
SwiGLUi\mathrm{SwiGLU}_iexpert ii‘s feed-forward block
yylayer output, combining the two chosen experts

DeepSeek-V3 keeps a Switch-style top-kk selection but decides membership using a score the training loss never touches directly:

i∈TopkKr({ sj,t+bj  :  1≤j≤Nr })i \in \mathrm{Top}k_{K_r}\big(\{\, s_{j,t} + b_j \;:\; 1 \le j \le N_r \,\}\big)
SymbolMeaning
NrN_r, KrK_rnumber of routed experts and how many are selected per token
sj,ts_{j,t}expert jj‘s routing affinity score for token tt
bjb_jper-expert bias, adjusted after each batch, excluded from the gate weight

What It Enabled

Dense scaling (Chapters 8–9)Sparse MoE (this chapter)
Relationship between size and per-token computeproportional — doubling parameters roughly doubles FLOPs per tokendecoupled — total parameters can grow far faster than active parameters per token
Representative modelany model that fits the cluster, run in full for every tokenSwitch-C, 1.571T total parameters, 2,048 experts (2021)
Frontier exampledense 405B-parameter LLaMA 3.1DeepSeek-V3, 671B total / 37B active parameters (2024)
Reported result at frontierMMLU 84.4, MMLU-Pro 52.8 (LLaMA 3.1 405B)MMLU 87.1, MMLU-Pro 64.4 (DeepSeek-V3)
ModelYearRoutingTotal paramsActive params/tokenLoad balancing
Sparsely-Gated MoE2017noisy top-kkup to 137B in MoE layervaries with kkimportance + load losses
Switch Transformer (Switch-C)2021top-1~1.571Tfraction of one expert per tokenauxiliary loss, α=10−2\alpha=10^{-2}
Mixtral 8x7B2024top-2 of 847B13Bnone published
DeepSeek-V32024top-8 of 256 + 1 shared671B37Bauxiliary-loss-free bias

Decoupling capacity from per-token compute changed what “bigger model” meant for a training budget: total parameters stopped being a reliable predictor of either training or inference cost, and active parameters per token became the number that mapped to GPU-hours. That shift interacts directly with how a sparse model gets served rather than trained — a request that only touches 37 billion of DeepSeek-V3’s 671 billion parameters still needs every one of those 671 billion in accelerator memory somewhere in the cluster, since which experts get used shifts from token to token and batch to batch, a scheduling and memory problem Chapter 17 covers. It also raised the value of getting inter-chip communication right, since a router that spreads tokens across experts living on different accelerators needs an all-to-all exchange every layer, tying MoE more tightly to the interconnect story in Chapter 20. And a 671-billion-parameter model that costs $5.576 million to pretrain, rather than the tens of millions a dense model of comparable benchmark performance would have cost, changed the economics of what came after it: DeepSeek-V3 became the base model for DeepSeek-R1, a reasoning model trained with reinforcement learning and covered in language models, Chapter 20, and cheaper frontier-scale pretraining runs are a direct input to the test-time-compute economics covered in Chapter 19.

A sparse frontier model touches four later chapters at once DeepSeek-V3's routing choices ripple into reasoning training, serving, hardware, and the economics of thinking longer. Grey monospace text is the chapter that picks up the thread. DeepSeek-V3 671B total, 37B active Chapter 13 DeepSeek-R1 reasoning RL on top of V3 language models, Chapter 20 Serving sparse models active parameters, not total Chapter 17 Interconnect and coevolution export-controlled hardware Chapter 20 Test-time compute economics cheap-per-token serving Chapter 19
A sparse frontier model's routing choices ripple into reasoning training, serving, hardware, and the economics of thinking longer, all at once. PNG ↓

Key Papers

PaperAuthorsYearLinkWhy it matters
Outrageously Large Neural Networks: The Sparsely-Gated Mixture-of-Experts LayerShazeer et al.2017arXiv:1701.06538Introduced noisy top-k routing and load-balancing losses, decoupling model capacity from per-token compute for the first time at scale
Switch Transformers: Scaling to Trillion Parameter Models with Simple and Efficient SparsityFedus et al.2021arXiv:2101.03961Simplified routing to top-1, cutting communication cost and reaching the first published trillion-parameter model
Mixtral of ExpertsJiang et al.2024arXiv:2401.04088First widely-used open-weights sparse MoE, matching a 70B dense model’s benchmarks at 13B active parameters
DeepSeek-V3 Technical ReportDeepSeek-AI2024arXiv:2412.19437Replaced the auxiliary load-balancing loss with a bias-only mechanism, reaching 671B total parameters for $5.576M in training cost

PM Lens

Papers referenced