Search

Search papers and lessons

Switch Routing

Level 4 · 11 min read

Switch routing sends each token to exactly one expert, chosen by a softmax router, so the compute per token stays fixed as the number of experts grows.

On this page

  • Colour = topic mix
  • Linear algebra
  • Neural network basics
  • Architectures
  • Discrete math
  • Efficiency
  • Probability
  • Sign in to see your progress

TL;DR

Switch routing is a way of running a mixture of experts layer where every token goes to exactly one expert. A small router network scores the experts with a softmax, the token is sent to the highest-scoring one, and that expert's output is multiplied by the router's probability for it. Because each token runs through one expert only, the compute per token does not grow when the number of experts grows.

The price is that the router can send too many tokens to one expert. Switch routing handles this with a capacity limit per expert and a loss term that pushes the load to spread out.

Where you'll see it: in the feedforward blocks of the Switch Transformer family and in the sparse layers of later large models that chose top-1 routing for simplicity.

Intuition

One token, one specialist

The Mixture of Experts lesson describes a hospital where each patient sees two specialists and the answers are blended. Switch routing is the simpler version of that hospital. Each patient sees exactly one specialist, the one the front desk judges most suitable. There is no blending of several opinions, so the desk's job is only to pick.

Why would anyone accept less? Because it is cheap and easy to build. With one expert per token, the work per token is one expert call, no matter how many experts there are. The routing decision is a single arg-max, and the dispatch is a simple gather. Engineers can add experts until memory runs out and the time per token barely moves.

The difficulty is that the front desk now has a hard decision. If most patients are sent to the same specialist, that specialist is overloaded and the others sit idle. The rest of this lesson is about making that single choice both trainable and balanced.

flowchart LR
  T[Token x] --> R[Router: softmax over E scores]
  R -->|argmax expert i| I[Expert i]
  I --> M[Multiply by router probability p_i]
  M --> Y[Output y]
  R -.->|other experts| Z[Not run for this token]

Why the gate is not renormalised

Top-k routing divides the kept gates by their sum, so the weights add to one. With one expert kept, that division gives exactly 1, and the router receives no gradient through the output. Switch routing skips the renormalisation. The output is the chosen expert's output scaled by its raw probability, which is less than 1 in general. That scale is what carries the training signal back into the router, so the router learns which expert to prefer.

−3−2−1012300.20.40.60.81router score for chosen expert, others at 0value
  • router probability of chosen expert
  • size of its gradient
The gate rises with its score, while the gradient that trains the router is largest when the router is undecided (score near 0) and shrinks once it is confident.

The math

The router

Let there be EE experts, each a feedforward block fi(x)f_i(\mathbf{x}). For a token representation x∈Rd\mathbf{x} \in \mathbb{R}^d, the router is a learned matrix Wr∈RE×dW_r \in \mathbb{R}^{E \times d}. It produces a probability for each expert:

p(x)=softmax(Wrx),pi(x)=exp⁡(zi)∑j=1Eexp⁡(zj),z=Wrx.\mathbf{p}(\mathbf{x}) = \mathrm{softmax}(W_r \mathbf{x}), \qquad p_i(\mathbf{x}) = \frac{\exp\big(z_i\big)}{\sum_{j=1}^{E} \exp\big(z_j\big)}, \qquad \mathbf{z} = W_r \mathbf{x}.

Here ziz_i is the raw score for expert ii and pip_i is the probability after the softmax. The probabilities are positive and sum to one. The router picks one expert:

i∗(x)=arg⁡max⁡i pi(x).i^{\ast}(\mathbf{x}) = \arg\max_{i} \, p_i(\mathbf{x}).

The layer output

The layer output for a token is

y(x)=pi∗(x) fi∗(x).y(\mathbf{x}) = p_{i^{\ast}}(\mathbf{x}) \, f_{i^{\ast}}(\mathbf{x}).

Only one expert is evaluated. The derivative of yy with respect to the router's probability for the chosen expert is fi∗(x)f_{i^{\ast}}(\mathbf{x}), which is generally nonzero. So the loss gradient reaches the router through pi∗p_{i^{\ast}} and the router can learn. The choice of i∗i^{\ast} itself is discrete and receives no gradient, which is the usual trade-off for hard routing.

Expert capacity

Batches are split across devices, and each expert has a fixed buffer size. For a batch of TT tokens and EE experts, each expert can take at most

C=TE⋅cC = \frac{T}{E} \cdot c

tokens, where cc is the capacity factor, typically a little above 1 (the paper uses values such as 1.0 and 1.25). Tokens that arrive at an expert after its buffer is full are dropped from the expert computation. They pass through the layer unchanged via the residual connection, so the token is not lost, but it gets no feedforward update in that layer.

The load-balancing loss

Let TT be the number of tokens in a batch. Define two quantities for each expert ii. The fraction of tokens sent to it is

fi=1T∑t=1T1[i∗(xt)=i],f_i = \frac{1}{T} \sum_{t=1}^{T} \mathbb{1}\big[i^{\ast}(\mathbf{x}_t) = i\big],

and the average router probability it received is

Pi=1T∑t=1Tpi(xt).P_i = \frac{1}{T} \sum_{t=1}^{T} p_i(\mathbf{x}_t).

The auxiliary loss, added to the main training loss, is

Laux=α E∑i=1Efi Pi.\mathcal{L}_{\text{aux}} = \alpha \, E \sum_{i=1}^{E} f_i \, P_i.

Here α\alpha is a small coefficient, 0.010.01 in the Switch Transformer paper. The fif_i term is not differentiable, since it counts hard choices, but the PiP_i term is. Minimising the loss pushes probability mass away from over-used experts. When the load is perfectly uniform, fi=Pi=1/Ef_i = P_i = 1/E for every ii, and the loss equals α\alpha. Any skew makes it larger.

Worked examples

Example 1: routing one token

A layer has four experts. For one token, the router gives the scores z=(1.0, 2.0, 0.5, −1.0)\mathbf{z} = (1.0,\ 2.0,\ 0.5,\ -1.0). Expert 2 has output f2(x)=4f_2(\mathbf{x}) = 4 for this token.

Example
One token through a switch layer
  1. Exponentiate the scores: e1.0≈2.718e^{1.0} \approx 2.718, e2.0≈7.389e^{2.0} \approx 7.389, e0.5≈1.649e^{0.5} \approx 1.649, e−1.0≈0.368e^{-1.0} \approx 0.368.
  2. Their sum is 12.12412.124. Divide each by it to get the probabilities p≈(0.2242, 0.6095, 0.1360, 0.0303)\mathbf{p} \approx (0.2242,\ 0.6095,\ 0.1360,\ 0.0303).
  3. The arg-max is expert 2, with p2≈0.6095p_2 \approx 0.6095. Only expert 2 runs.
  4. The output is y=0.6095×4=2.438y = 0.6095 \times 4 = 2.438.
  5. The router's gradient through this token is ∂y/∂p2=f2=4\partial y / \partial p_2 = f_2 = 4. Because the gate was not renormalised, this is the full value of the expert output, not zero.

Interpretation: one expert was evaluated out of four. The output is about 2.44, a little under the expert's own answer of 4, because the router is only 61 percent sure of its choice. As training goes on and the router grows more confident, p2p_2 approaches 1 and the output approaches the expert's answer.

Example 2: the load-balancing loss

Take E=4E = 4 experts and a batch of T=8T = 8 tokens. Suppose the arg-max choices are: expert 1 for five tokens, expert 2 for two, expert 3 for one, expert 4 for none.

Example
Computing the auxiliary loss
  1. Dispatch fractions: f=(5/8, 2/8, 1/8, 0)=(0.625, 0.25, 0.125, 0)f = (5/8,\ 2/8,\ 1/8,\ 0) = (0.625,\ 0.25,\ 0.125,\ 0).
  2. Suppose the batch-average router probabilities are P=(0.5, 0.2, 0.2, 0.1)P = (0.5,\ 0.2,\ 0.2,\ 0.1). They sum to 1.
  3. Compute the sum: 0.625×0.5+0.25×0.2+0.125×0.2+0×0.1=0.3125+0.05+0.025=0.38750.625 \times 0.5 + 0.25 \times 0.2 + 0.125 \times 0.2 + 0 \times 0.1 = 0.3125 + 0.05 + 0.025 = 0.3875.
  4. Multiply by αE=0.01×4=0.04\alpha E = 0.01 \times 4 = 0.04. The loss is 0.04×0.3875=0.01550.04 \times 0.3875 = 0.0155.
  5. For comparison, a perfectly balanced batch has f=P=(0.25,0.25,0.25,0.25)f = P = (0.25, 0.25, 0.25, 0.25). The sum is 4×0.0625=0.254 \times 0.0625 = 0.25, and the loss is 0.04×0.25=0.010.04 \times 0.25 = 0.01.

Interpretation: the skewed batch costs 0.0155 against a floor of 0.01 for a balanced one. The extra 0.0055 is the gradient signal that tells the router to send less to expert 1 and more to experts 3 and 4. Note that the loss is at its minimum when both ff and PP are uniform, not just when ff is.

Example 3: capacity and dropped tokens

A batch has T=1024T = 1024 tokens, E=8E = 8 experts, and capacity factor c=1.25c = 1.25. Each expert's buffer holds

C=10248×1.25=160C = \frac{1024}{8} \times 1.25 = 160

tokens. Suppose expert 3 is chosen by 210 tokens.

Example
Overflow at one expert
  1. Expert 3 can process 160 tokens. The other 50 are over capacity.
  2. Those 50 tokens skip the feedforward update in this layer and pass through the residual connection.
  3. The fraction of expert 3's requests that are dropped is 50/210≈0.23850 / 210 \approx 0.238, about 24 percent.
  4. Across the whole batch the drop rate is 50/1024≈0.04950 / 1024 \approx 0.049, about 5 percent.

Interpretation: a modest overload causes a small drop rate for the batch, but it can hurt a single expert badly. This is why the load-balancing loss is needed, and why a capacity factor above 1 is used: it leaves slack for the router to be imperfect.

Tradeoffs and failure modes

Switch routing is the cheapest sparse layer to run, and it fails in predictable ways.

SituationWhat goes wrongBetter choice
The router collapses onto a few expertsThose experts are overloaded, tokens are dropped, and the rest of the parameters are wastedAdd the load-balancing loss, and tune its coefficient α\alpha
Capacity factor set to exactly 1Any imbalance drops tokensUse a capacity factor of about 1.25 or more, or accept the drop rate
Tokens need several expert opinionsOne expert cannot cover the mix of skills a token needsTop-2 routing with renormalised gates, as in Mixture of Experts
Router runs in low precisionSoftmax and exponentials can overflow or lose accuracy, and training becomes unstableCompute the router in float32 even when the rest of the model uses bfloat16
Very small batchesThe batch fractions fif_i and PiP_i are noisy, so the balance signal is unreliableUse larger batches or accumulate statistics over more tokens

The main alternative to switch routing is top-2 routing, which blends two experts and is more robust to a bad single choice, at twice the expert compute. Dense layers avoid routing altogether and are simpler to serve, but they cost compute in proportion to parameters.

History

Mixtures of local experts were proposed in the early 1990s by Jacobs, Jordan, Nowlan and Hinton, with a gating network that weighted all expert outputs. Hierarchical versions followed. The sparse, large-scale idea became practical in 2017, when Shazeer and colleagues placed a sparsely gated mixture of experts between LSTM layers and kept only the top few experts per input. That work introduced the noisy top-k gate and the balancing ideas that later systems refined.

In 2021, Fedus, Zoph and Shazeer published the Switch Transformer. Their argument was that top-1 routing is simpler than top-2, halves the expert compute per token, and is good enough if the balancing loss and capacity limit are set well. The paper trained models with up to about 1.6 trillion parameters and reported pretraining speedups of up to 7x over T5 baselines at the same FLOPs per token. They also noted that Shazeer and colleagues had conjectured that routing to more than one expert was needed for the router to get useful gradients, and showed that top-1 works. The paper reports that dropped tokens were typically under 1 percent in their experiments. The simplicity of one expert per token is why the design caught on in later sparse language models.

Check yourself

QuestionWhat does the router output for a token, and how many experts run for that token?Show answer

The router computes a softmax over the EE expert scores, giving positive probabilities that sum to one. The token is sent to the expert with the largest probability, so exactly one expert runs, however large EE is.

QuestionA router gives probabilities (0.5, 0.3, 0.2)(0.5,\ 0.3,\ 0.2) over three experts and the chosen expert's output is f=2f = 2. What is the layer output for this token?Show answer

The arg-max is the first expert, with probability 0.50.5. The output is y=0.5×2=1.0y = 0.5 \times 2 = 1.0. The gate is not renormalised, so the output is the probability times the expert output, not just the expert output.

QuestionWhy does switch routing keep the raw probability as the gate instead of renormalising it to 1?Show answer

If the single kept gate is renormalised, it always equals 1, so the output no longer depends on the router's probability and the router receives no gradient through the output. Keeping the raw probability makes the output depend on the router, so the router can learn which expert to prefer.

QuestionA batch has T=512T = 512 tokens and E=4E = 4 experts, and the load is perfectly balanced. With α=0.01\alpha = 0.01, what is the value of the auxiliary loss?Show answer

Balanced means fi=Pi=1/4f_i = P_i = 1/4 for each expert. The sum is 4×(1/4)2=0.254 \times (1/4)^2 = 0.25. The loss is αE×0.25=0.01×4×0.25=0.01\alpha E \times 0.25 = 0.01 \times 4 \times 0.25 = 0.01. This is the minimum value, and it equals α\alpha.

QuestionA deployment wants to serve a switch layer with a capacity factor of 1.0 on a skewed workload. What should the team expect, and what would you change?Show answer

Any expert that receives more than its share of tokens will overflow its buffer, and the overflow tokens skip the feedforward update in that layer. The team should expect a noticeable drop rate and quality loss on the overloaded experts. A reasonable change is to raise the capacity factor to around 1.25, strengthen the load-balancing loss, or move to top-2 routing if the drop rate remains high.

Sources