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.
- router probability of chosen expert
- size of its gradient
The math
The router
Let there be experts, each a feedforward block . For a token representation , the router is a learned matrix . It produces a probability for each expert:
Here is the raw score for expert and is the probability after the softmax. The probabilities are positive and sum to one. The router picks one expert:
The layer output
The layer output for a token is
Only one expert is evaluated. The derivative of with respect to the router's probability for the chosen expert is , which is generally nonzero. So the loss gradient reaches the router through and the router can learn. The choice of 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 tokens and experts, each expert can take at most
tokens, where 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 be the number of tokens in a batch. Define two quantities for each expert . The fraction of tokens sent to it is
and the average router probability it received is
The auxiliary loss, added to the main training loss, is
Here is a small coefficient, in the Switch Transformer paper. The term is not differentiable, since it counts hard choices, but the term is. Minimising the loss pushes probability mass away from over-used experts. When the load is perfectly uniform, for every , and the loss equals . 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 . Expert 2 has output for this token.
- Exponentiate the scores: , , , .
- Their sum is . Divide each by it to get the probabilities .
- The arg-max is expert 2, with . Only expert 2 runs.
- The output is .
- The router's gradient through this token is . 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, approaches 1 and the output approaches the expert's answer.
Example 2: the load-balancing loss
Take experts and a batch of tokens. Suppose the arg-max choices are: expert 1 for five tokens, expert 2 for two, expert 3 for one, expert 4 for none.
- Dispatch fractions: .
- Suppose the batch-average router probabilities are . They sum to 1.
- Compute the sum: .
- Multiply by . The loss is .
- For comparison, a perfectly balanced batch has . The sum is , and the loss is .
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 and are uniform, not just when is.
Example 3: capacity and dropped tokens
A batch has tokens, experts, and capacity factor . Each expert's buffer holds
tokens. Suppose expert 3 is chosen by 210 tokens.
- Expert 3 can process 160 tokens. The other 50 are over capacity.
- Those 50 tokens skip the feedforward update in this layer and pass through the residual connection.
- The fraction of expert 3's requests that are dropped is , about 24 percent.
- Across the whole batch the drop rate is , 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.
| Situation | What goes wrong | Better choice |
|---|---|---|
| The router collapses onto a few experts | Those experts are overloaded, tokens are dropped, and the rest of the parameters are wasted | Add the load-balancing loss, and tune its coefficient |
| Capacity factor set to exactly 1 | Any imbalance drops tokens | Use a capacity factor of about 1.25 or more, or accept the drop rate |
| Tokens need several expert opinions | One expert cannot cover the mix of skills a token needs | Top-2 routing with renormalised gates, as in Mixture of Experts |
| Router runs in low precision | Softmax and exponentials can overflow or lose accuracy, and training becomes unstable | Compute the router in float32 even when the rest of the model uses bfloat16 |
| Very small batches | The batch fractions and are noisy, so the balance signal is unreliable | Use 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 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 is.
QuestionA router gives probabilities over three experts and the chosen expert's output is . What is the layer output for this token?Show answer
The arg-max is the first expert, with probability . The output is . 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 tokens and experts, and the load is perfectly balanced. With , what is the value of the auxiliary loss?Show answer
Balanced means for each expert. The sum is . The loss is . This is the minimum value, and it equals .
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.