Mixture of Experts for LLMs
Mixture of Experts (MoE) is a conditional computation architecture in which a learned routing function selects a sparse subset of specialized sub-networks ("experts") to process each input token, enabling model capacity to scale super-linearly with parameter count while keeping per-token FLOPs approximately constant. It matters because it is the dominant technique by which frontier LLMs (GPT-4, Gemini, Mixtral, DeepSeek-V2) achieve trillion-parameter scale at tractable training and inference cost. The counterintuitive fact: despite each expert being trained on only a fraction of tokens, MoE models consistently outperform dense models of equal compute budget — more parameters with fewer FLOPs per token is strictly better than the naive tradeoff suggests.
The fundamental insight is that not every part of a network needs to activate for every input. In a dense transformer, all parameters process every token — this couples model capacity tightly to compute. MoE breaks this coupling: by routing each token to only of experts, capacity grows as while per-token compute grows as of the total — making the two quantities independently controllable. The "aha moment" is recognizing that language is heterogeneous — mathematical tokens, code, French prose, and logical connectives do not benefit from the same learned features — so routing different token types to different specialists is not just a computational trick but a semantically meaningful decomposition.
The most pervasive error is assuming that MoE's efficiency advantage is simply "we use fewer parameters per forward pass, so it's faster." This conflates parameter count with memory bandwidth, communication volume, and load balance, all of which are the actual bottlenecks in practice. The correct mental model: in distributed training, each expert lives on a different device, so routing induces all-to-all communication whose cost scales with the number of tokens dispatched across devices — not with expert parameter count. A router that sends all tokens to two popular experts (a degenerate but low-loss solution) collapses the MoE to a dense model plus communication overhead, strictly worse than a baseline. The auxiliary load-balancing loss exists precisely to fight this collapse, and ignoring it or under-weighting it is the single most common failure mode when implementing MoE from scratch.
How It Works
Routing and Sparse Dispatch
Consider a transformer with layers. At each MoE layer, the standard FFN sublayer is replaced by expert networks , each with its own parameters . A lightweight router — typically a linear projection followed by softmax — assigns per-token routing weights. Let be the hidden state of an input token. The router computes logits:
Explanation: The router projects the token representation into a logit vector of dimension equal to the number of experts, producing an unnormalized score for each expert. The top- routing function selects the experts with highest logit values and softmax-normalizes only over those scores:
Explanation: Only the selected experts receive nonzero gating weights; all others are masked to before the softmax, making the computation exactly sparse. The MoE sublayer output is then the weighted sum of selected expert outputs:
Explanation: Each selected expert processes the full token representation independently, and their outputs are blended by the normalized routing weights . Here denotes the index set of the top- experts for token , is the -th component of the gating vector (zero for non-selected experts), and is typically a two-layer FFN with SwiGLU or ReLU activation.
Load Balancing
Without regularization, the router collapses: a few experts accumulate most tokens while the rest receive near-zero gradient signal — the expert collapse or rich-get-richer phenomenon. The standard remedy (Fedus et al., 2022; Shazeer et al., 2017) is an auxiliary load-balancing loss added to the primary language modeling objective. Define as the fraction of tokens routed to expert in a batch of tokens, and as the mean routing probability assigned to expert :
Explanation: This loss is minimized when all experts receive equal token fractions () and equal mean probability (), penalizing any concentration of routing mass on a subset of experts. The coefficient (typically to ) controls the tradeoff between routing quality and balance. Note that is non-differentiable (it depends on the discrete top- selection), so the gradient flows only through — a deliberate trick that makes this a bilinear surrogate loss.
Expert Capacity and Token Dropping
In hardware-efficient implementations, each expert is given a capacity buffer where is the capacity factor. Tokens routed to an expert beyond its capacity are dropped (replaced by zeros or their residual stream value). This introduces a controlled approximation that prevents stragglers in parallel dispatch:
Explanation: With capacity factor , up to total expert-token slots exist; setting wastes 25% of capacity but ensures fewer than 25% of tokens are ever dropped under balanced routing.
Shazeer was a longtime Google Brain researcher who co-authored the original transformer paper before departing to co-found Character.AI. His 2017 paper "Outrageously Large Neural Networks: The Sparsely-Gated Mixture-of-Experts Layer" introduced the top- noisy gating mechanism and demonstrated for the first time that sparse MoE could be integrated into sequence-to-sequence models at scale, achieving state-of-the-art on machine translation with 137 billion parameters. Remarkably, his 2017 contribution was largely ignored for four years before the field rediscovered it via the Switch Transformer in 2021.
Fedus is a research scientist at Google Brain whose 2021 Switch Transformer paper demonstrated that routing to a single expert () is not only sufficient but superior to in many regimes, dramatically simplifying the load-balancing problem. The Switch Transformer was the first publicly described trillion-parameter model and showed 7× speedup over T5-XXL at equal compute, catalyzing the modern era of sparse LLM research. Fedus has noted in interviews that the initial reviewer consensus was skeptical that top-1 routing could be stable — a misconception his paper definitively refuted.
Intuition
Imagine a large hospital where every patient — whether presenting with a broken bone, a psychiatric episode, a cardiac event, or a skin rash — is seen by the exact same generalist physician every single time. That's a dense transformer: one FFN, all tokens. Now imagine the hospital is reorganized into specialized departments: orthopedics, cardiology, psychiatry, dermatology. A triage nurse (the router) looks at each patient and sends them to the appropriate specialist — only one or two departments per visit, not all sixteen. The specialists become extremely good at their narrow domain because they see concentrated, relevant training cases. You can add new departments without making any individual department busier — the hospital's capacity scales without scaling the cost per patient. The MoE router is that triage nurse: it makes a fast, low-cost binary decision that unlocks massively parallel specialization. The pathology of expert collapse is equivalent to the triage nurse always routing everyone to the same two departments because they happen to be slightly more popular in the training distribution — a bureaucratic habit that destroys the entire point of specialization.