TL;DR
A mixture of experts layer routes each token to a few experts, and the router is trained by gradient descent on the task loss alone. Left alone, the router tends to pick the same few experts again and again. Those experts improve, attract still more tokens, and the rest receive almost nothing. The load-balancing loss is an extra term added to the training objective. It is small when tokens are spread evenly across experts and grows when a few experts take most of the traffic, so the router learns to share the work.
Where you'll see it: the training recipe of sparse transformers such as Switch Transformer and Mixtral-style models, where it is added to the language modelling loss for every batch.
Intuition
A call centre with favourite agents
Imagine a call centre with eight agents and a dispatcher who sends each call to the agent who seems best suited. At first the dispatcher's guesses are noisy. By chance, agent 3 handles a few more calls early on and gets good at them. The dispatcher notices, sends agent 3 more calls, and agent 3 gets better still. Meanwhile agents 5 to 8 sit idle, never get practice, and never look good enough to be chosen. The centre has eight agents of capacity, but it behaves like one.
A mixture of experts has the same failure. The router's scores are learned, so the easiest path for the optimiser is to strengthen whatever expert already works. The fix is a rule that says "do not let one agent take everything". The load-balancing loss is that rule written as a number that the optimiser can reduce.
The plot below shows the idea in the simplest case, two experts. Let be the share of tokens that go to expert 1, so expert 2 gets . The penalty is lowest at an even split and rises toward the extremes.
- penalty / α, two experts
The math
Consider a layer with experts and a batch of tokens. The router computes, for each token , a probability vector over the experts with a softmax, and sends the token to its top-scoring expert.
For expert , the dispatch fraction is
It is the share of the batch that actually lands on expert . is 1 when the condition holds and 0 otherwise.
For expert , the mean gate probability is
where is the router's softmax probability for expert on token . It is the average confidence the router places on expert .
The two quantities are both fractions that sum to 1 over the experts: and . The first says where tokens actually went, and the second says where the router wanted them to go.
The auxiliary loss
Fedus and colleagues defined the load-balancing loss as
where is a small coefficient. The total training loss is the task loss plus this term:
Why the product, and why the factor
The dispatch fraction comes from a hard argmax, so it has no gradient. The gate probability is a softmax output, so it does. The product lets the gradient flow through , while supplies the information about which experts are actually loaded. Differentiating with held fixed gives
An expert that is already overloaded has a large , so the loss pushes its gate probabilities down. The router becomes less eager to send tokens to it.
The factor keeps the scale independent of the number of experts. If the routing is perfectly uniform, then for every , so
So a balanced router pays exactly , whatever is. The Switch Transformer paper uses , which keeps the term small next to the task loss.
A caveat. The sum is not minimised at uniform routing in every mathematical sense. If expert 1 gets all the tokens while the router puts all its probability on expert 2, the sum is zero. The loss is designed so that the dangerous case, where the same experts both receive the traffic and carry the probability, is penalised. That case gives the largest values. The uniform point is the intended operating point, and it is where the loss equals .
Shazeer's version
The 2017 sparsely gated layer used a different auxiliary term. It defined an importance for each expert as the sum of its gate values over the batch, and penalised the squared coefficient of variation of those importances, so that a batch with uneven importances costs more. A second term of the same form, on a smooth estimate of how many tokens each expert receives, balanced the load itself, and each term had its own hand-tuned weight. The fraction-times-probability form above folds the two ideas into one term with a single coefficient.
Worked examples
Take experts, tokens, and . Suppose each expert receives exactly two tokens and the router's mean gate probabilities are also uniform.
- Dispatch fractions: for each .
- Mean gate probabilities: for each .
- Sum: .
- Loss: .
Interpretation: the balanced state costs exactly . The gradient on each is , the same for every expert, so the penalty pushes no expert up or down relative to the others.
Use the same , and . Now expert 1 receives six of the eight tokens, experts 2 and 3 receive one each, and expert 4 receives none. The router's mean gate probabilities follow the same pattern, with .
- Dispatch fractions: .
- Products : , , , .
- Sum: .
- Loss: .
Interpretation: the collapsed batch costs , which is times the balanced cost. The gradient on expert 1 is , while expert 4 gets . The optimiser is told to move probability away from expert 1 and gives expert 4 no push at all. Expert 4 can still gain traffic from the task loss, but the balancing term does not help it.
Many systems also cap how many tokens each expert accepts per batch. With a capacity factor of , the cap is tokens per expert.
- In the balanced batch, every expert gets two tokens, under the cap of three. Nothing is dropped.
- In the collapsed batch, expert 1 receives six tokens, accepts three, and drops three. Those three tokens skip the expert layer and pass through the residual connection unchanged.
Interpretation: imbalance does more than waste capacity. It also changes the output for the tokens that are dropped, so the loss is protecting quality as well as speed.
Tradeoffs and failure modes
The coefficient is a trade-off. Too small, and the router collapses anyway. Too large, and the balancing term dominates the task loss, so tokens are spread evenly but each expert is assigned without regard to what it is good at. Specialisation is the reason to have experts, so a balance term that is too strong defeats the purpose.
| Situation | Problem | Better choice |
|---|---|---|
| Router collapses within the first few thousand steps | A few experts get almost all tokens, the rest starve | Add the auxiliary loss, or raise within the usual range |
| Balance looks good but loss is flat | Experts may be interchangeable, so there is little specialisation | Lower , or check whether the task needs specialisation at all |
| Many tokens dropped under a capacity cap | Overflow tokens skip the layer and lose quality | Raise the capacity factor, or accept the drops and measure the effect |
| Top-1 routing with a hard cap | Gradients through the chosen gate only | Use the softmax probability to scale the output, as Switch does |
| Batches are small or vary a lot in composition | is noisy from batch to batch | Compute the balance over larger batches, or across devices |
The loss balances tokens per batch, not per sequence or per domain. A model can be balanced on average and still send all code tokens to one expert and all prose tokens to another. That may be fine, or it may be the specialisation you wanted. The loss cannot tell the difference, so check routing statistics on real data.
Finally, the auxiliary loss is a training device. At inference time the router still sends each token to its top experts. Balance during training is what keeps the experts trained, and it does not guarantee balance on a particular inference batch.
History
Mixtures of local experts date to the early 1990s. Jacobs, Jordan, Nowlan and Hinton proposed a gating network that blends several expert networks in 1991, with a training objective that encouraged the experts to specialise. The idea was limited by the compute of its day.
Shazeer and colleagues brought sparse routing back in 2017 with the sparsely gated layer. Their paper showed that a model with far more parameters could be trained at a similar compute cost, and that balancing the load had to be addressed directly. Their importance term was an early and influential balance loss for this kind of layer.
Fedus, Zoph and Shazeer simplified the design in 2021 with the Switch Transformer. They routed each token to a single expert, and used the fraction-times-probability loss with . The simpler loss and top-1 routing made large sparse models easier to train, and variants of the same loss appear in later sparse models. The reason it caught on is that it adds a single term, works without changing the architecture, and removes the most common failure of the router.
Check yourself
QuestionIn one sentence, why does a learned router tend to collapse onto a few experts without a balancing term?Show answer
The experts that receive more tokens get more training and improve, which makes the router send them even more tokens. Experts that receive few tokens do not get trained, so they never look good enough to be chosen. The router's own learning dynamics reinforce the imbalance, and nothing in the task loss pushes against it.
QuestionA layer has experts, a batch of tokens, and . Each expert receives exactly four tokens, and the mean gate probabilities are uniform. What is ?Show answer
Dispatch fractions are and gate probabilities are . The sum . The loss is . This is the balanced value , as expected.
QuestionWhy is the dispatch fraction not differentiated, while the gate probability is?Show answer
The dispatch fraction is computed from a hard top-k choice, which is a step function of the scores and has zero gradient almost everywhere. Its role is to report which experts are loaded. The gate probability is a smooth softmax output, so the gradient can reach the router through it. Using as a constant and differentiating through gives a useful training signal.
QuestionA team sees that one expert handles 70 percent of tokens in a model with eight experts. Roughly what does the balance loss push the router to do, and why might that still hurt quality?Show answer
The loss pushes the gate probabilities of the overloaded expert down, which makes the router send fewer tokens to it. That moves traffic toward the other experts, which get trained and become useful. It may hurt quality if the overloaded expert was genuinely the best choice for many tokens, because forcing traffic away from it can reduce accuracy. The team should check whether the imbalance reflects real specialisation or a training artefact before tuning .
QuestionA deployment caps each expert at a fixed number of tokens per batch. Why does this matter for a model trained with a balance loss?Show answer
Even with a balance loss, batches vary, so some experts will exceed the cap on some batches. Overflow tokens are dropped and skip the expert layer. A balance loss reduces how often this happens, and a capacity factor sets how much slack is allowed. Together they control the trade-off between wasted compute and dropped tokens.
Sources
- Fedus, Zoph and Shazeer, Switch Transformers: Scaling to Trillion Parameter Models with Simple and Efficient Sparsity, 2021. arxiv.org/abs/2101.03961
- Shazeer et al., Outrageously Large Neural Networks: The Sparsely-Gated Mixture-of-Experts Layer, 2017. arxiv.org/abs/1701.06538
- Mixture of experts, Wikipedia. en.wikipedia.org/wiki/Mixture_of_experts