JA EN
LearnPaper Deep-Dives
·★ MEMBER·PAPER·9 min read

Mixture of Experts (MoE) from Scratch — Routing and Load Balancing in the Switch Transformer

A ground-up explanation of Mixture of Experts, the sparse architecture behind today's largest LLMs, built strictly from the Switch Transformer paper: the router math, expert capacity, the load-balancing loss, and the three tricks that make sparse training stable.

ModalitytextTaskarchitecture

Switch Transformers: Scaling to Trillion Parameter Models with Simple and Efficient Sparsity

Primary source — what this article is built on

undefined2021-01-11undefined2026-08-125y 7mo later

Switch Transformers: Scaling to Trillion Parameter Models with Simple and Efficient SparsityWilliam Fedus, Barret Zoph, Noam Shazeer · 2021-01-11 · v3arXiv:2101.03961Paper page·PDF
undefined

In deep learning, models typically reuse the same parameters for all inputs. Mixture of Experts (MoE) defies this and instead selects different parameters for each incoming example. The result is a sparsely-activated model -- with outrageous numbers of parameters -- but a constant computational cost. However, despite several notable successes of MoE, widespread adoption has been hindered by complexity, communication costs and training instability -- we address these with the Switch Transformer. We simplify the MoE routing algorithm and design intuitive improved models with reduced communication and computational costs. Our proposed training techniques help wrangle the instabilities and we show large sparse models may be trained, for the first time, with lower precision (bfloat16) formats. We design models based off T5-Base and T5-Large to obtain up to 7x increases in pre-training speed with the same computational resources. These improvements extend into multilingual settings where we measure gains over the mT5-Base version across all 101 languages. Finally, we advance the current scale of language models by pre-training up to trillion parameter models on the "Colossal Clean Crawled Corpus" and achieve a 4x speedup over the T5-XXL model.


More Parameters, Same Compute

An ordinary neural network uses all of its parameters for every input. To make it smarter you add parameters, and every parameter you add makes each forward pass proportionally more expensive. That is the bargain dense models are stuck with.

Mixture of Experts (MoE) breaks the bargain: it selects different parameters for each incoming example. The result is a sparsely-activated model — an outrageous number of parameters, but a constant computational cost per token.

The 2021 paper Switch Transformer (Fedus, Zoph, Shazeer) simplified MoE to its bare minimum, trained a model with 1.6 trillion parameters stably, and reported up to 7x faster pre-training than T5 with the same computational resources (§Abstract). This article builds MoE from the ground up using that paper as the sole primary source.

The Metaphor: A Hospital Front Desk

A dense model is a hospital where one enormous generalist sees every patient — and the smarter you make the doctor, the slower every visit gets. In an MoE hospital, the front desk sends each patient to one specialist. You can grow the staff to 100 doctors while each patient still gets exactly one consultation. That is the whole trick.

The Intuition: Decouple Knowledge from Compute

The most parameter-heavy part of a Transformer block is the FFN (feed-forward network). The Switch Transformer replaces it with N copies — the experts — and passes each token through exactly one of them (Figure 2).

Scaling laws had studied three axes: model size, data, and compute. This paper proposes a fourth axis: increase the parameter count while keeping FLOPs per token constant (§2). The only added compute is the router itself, a lightweight O(dmodel×num experts)O(d_{model} \times \text{num experts}) operation (§3).

Mechanism, Part 1: The Router Picks Experts with a Softmax

Start with the standard MoE that preceded Switch (Shazeer et al. 2017, reviewed in §2.1). Given a token representation xx, the router computes logits h(x)=Wrxh(x) = W_r \cdot x with a weight matrix WrW_r, then turns them into probabilities:

pi(x)=eh(x)ijNeh(x)jp_i(x) = \frac{e^{h(x)_i}}{\sum_{j}^{N} e^{h(x)_j}}
(1)

Here pi(x)p_i(x) is "how much the router wants to send token xx to expert ii," and the N values sum to 1 — the same softmax you met in attention. Put in words: score every expert, make each score positive by exponentiating it, then give each expert its share of the total. Because the scores pass through an exponential first, an expert that scores only slightly higher ends up with a much larger share — the front desk forms strong preferences from weak evidence. Classic MoE keeps the top-kk experts and blends their outputs, weighted by these probabilities:

y=iTpi(x)Ei(x)y = \sum_{i \in \mathcal{T}} p_i(x) E_i(x)
(2)

T\mathcal{T} is the set of chosen experts and Ei(x)E_i(x) is what expert ii computes on the token. Read it as "a weighted average of the chosen specialists' opinions, weighted by the front desk's confidence." Multiplying by pi(x)p_i(x) is what lets gradients flow into the router so it can learn (§2.1). Now look at which experts appear in that sum: only the chosen ones. The other N−k never enter the equation at all — a detail which says, in a single line, why sparsity is free. An expert you never evaluate costs you nothing, exactly like a doctor who was never paged.

FIG 1The router's output is exactly this softmax distribution — each bar is the probability of dispatching to one expert. Too flat and no specialization emerges; too sharp and a few experts absorb all the load. That tug-of-war is the central design problem of MoE

Mechanism, Part 2: Switch's Answer — Route to Just One

The 2017 conventional wisdom held that the router could only learn by comparing at least two experts, so k>1k > 1 was considered necessary. The Switch paper contradicts this head-on and routes each token to a single expert, k=1k = 1 (§2.1). Three benefits: (1) less routing computation, (2) each expert's batch — its capacity, defined below — can be at least halved, and (3) the implementation gets simpler and communication costs drop. The gate value from Equation (2) survives, so differentiability is preserved.

In Table 1 the paper runs a head-to-head against top-2 MoE under identical conditions (128 experts, 32 TPUv3 cores): Switch is faster, and at small capacity factors it wins on quality too (§2.3). The intuition that "you can't learn without a comparison" simply did not survive the experiment.

Now the unglamorous part that makes or breaks real MoE systems. TPUs require statically-shaped tensors, so the number of tokens each expert processes — its expert capacity — must be fixed in advance (§2.2):

What's behind this

§

Members-only from here

371 walkthroughs, 26 textbook chapters, 48 student units and 6 close readings — all included for $4.99/mo, with three new explainers every day. Cancel any time; access runs to the end of the period.

Already a member? Sign in to keep reading

References

  1. William Fedus, Barret Zoph, Noam Shazeer. (2021-01-11) Switch Transformers: Scaling to Trillion Parameter Models with Simple and Efficient Sparsity. arXiv:2101.03961Paper page·PDF

This article is written from the source paper above. Where they differ, the original is authoritative.

Comments

Sign in to comment