Search

Search papers and lessons

Load-Balancing Loss

Level 4 · 11 min read

An auxiliary loss that penalises a mixture of experts router when a few experts take most of the tokens, so that every expert gets trained.

On this page

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

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 xx be the share of tokens that go to expert 1, so expert 2 gets 1−x1 - x. The penalty is lowest at an even split and rises toward the extremes.

00.20.40.60.8111.52share of tokens sent to expert 1penalty / α
  • penalty / α, two experts
The penalty equals α at an even split and 2α when one expert takes everything. This assumes the router's probabilities match the routed fractions, a simplification the next section removes.

The math

Consider a layer with EE experts and a batch of TT tokens. The router computes, for each token tt, a probability vector over the experts with a softmax, and sends the token to its top-scoring expert.

Definition: Dispatch fraction

For expert ii, the dispatch fraction is

ϕi=1T∑t=1T1[token t is sent to expert i].\phi_i = \frac{1}{T}\sum_{t=1}^{T} \mathbf{1}[\text{token } t \text{ is sent to expert } i].

It is the share of the batch that actually lands on expert ii. 1[⋅]\mathbf{1}[\cdot] is 1 when the condition holds and 0 otherwise.

Definition: Mean gate probability

For expert ii, the mean gate probability is

Pi=1T∑t=1Tpi(t),P_i = \frac{1}{T}\sum_{t=1}^{T} p_i(t),

where pi(t)p_i(t) is the router's softmax probability for expert ii on token tt. It is the average confidence the router places on expert ii.

The two quantities are both fractions that sum to 1 over the experts: ∑iϕi=1\sum_i \phi_i = 1 and ∑iPi=1\sum_i P_i = 1. 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

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

where α\alpha is a small coefficient. The total training loss is the task loss plus this term:

L=Ltask+Laux.\mathcal{L} = \mathcal{L}_{\text{task}} + \mathcal{L}_{\text{aux}}.

Why the product, and why the factor EE

The dispatch fraction ϕi\phi_i comes from a hard argmax, so it has no gradient. The gate probability PiP_i is a softmax output, so it does. The product lets the gradient flow through PiP_i, while ϕi\phi_i supplies the information about which experts are actually loaded. Differentiating with ϕi\phi_i held fixed gives

∂Laux∂Pi=α E ϕi.\frac{\partial \mathcal{L}_{\text{aux}}}{\partial P_i} = \alpha\, E\, \phi_i .

An expert that is already overloaded has a large ϕi\phi_i, so the loss pushes its gate probabilities down. The router becomes less eager to send tokens to it.

The factor EE keeps the scale independent of the number of experts. If the routing is perfectly uniform, then ϕi=Pi=1/E\phi_i = P_i = 1/E for every ii, so

∑i=1EϕiPi=E⋅1E2=1E,Laux=αE⋅1E=α.\sum_{i=1}^{E} \phi_i P_i = E \cdot \frac{1}{E^2} = \frac{1}{E}, \qquad \mathcal{L}_{\text{aux}} = \alpha E \cdot \frac{1}{E} = \alpha .

So a balanced router pays exactly α\alpha, whatever EE is. The Switch Transformer paper uses α=0.01\alpha = 0.01, which keeps the term small next to the task loss.

A caveat. The sum ∑iϕiPi\sum_i \phi_i P_i 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 α\alpha.

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

Example
A balanced batch

Take E=4E = 4 experts, T=8T = 8 tokens, and α=0.01\alpha = 0.01. Suppose each expert receives exactly two tokens and the router's mean gate probabilities are also uniform.

  1. Dispatch fractions: ϕi=2/8=0.25\phi_i = 2/8 = 0.25 for each ii.
  2. Mean gate probabilities: Pi=0.25P_i = 0.25 for each ii.
  3. Sum: ∑iϕiPi=4×(0.25×0.25)=0.25\sum_i \phi_i P_i = 4 \times (0.25 \times 0.25) = 0.25.
  4. Loss: Laux=0.01×4×0.25=0.01\mathcal{L}_{\text{aux}} = 0.01 \times 4 \times 0.25 = 0.01.

Interpretation: the balanced state costs exactly α=0.01\alpha = 0.01. The gradient on each PiP_i is αEϕi=0.01×4×0.25=0.01\alpha E \phi_i = 0.01 \times 4 \times 0.25 = 0.01, the same for every expert, so the penalty pushes no expert up or down relative to the others.

Example
A collapsed batch

Use the same EE, TT and α\alpha. 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 P=(0.7, 0.1, 0.1, 0.1)P = (0.7,\ 0.1,\ 0.1,\ 0.1).

  1. Dispatch fractions: ϕ=(6/8, 1/8, 1/8, 0)=(0.75, 0.125, 0.125, 0)\phi = (6/8,\ 1/8,\ 1/8,\ 0) = (0.75,\ 0.125,\ 0.125,\ 0).
  2. Products ϕiPi\phi_i P_i: 0.75×0.7=0.5250.75 \times 0.7 = 0.525, 0.125×0.1=0.01250.125 \times 0.1 = 0.0125, 0.125×0.1=0.01250.125 \times 0.1 = 0.0125, 0×0.1=00 \times 0.1 = 0.
  3. Sum: 0.525+0.0125+0.0125+0=0.550.525 + 0.0125 + 0.0125 + 0 = 0.55.
  4. Loss: Laux=0.01×4×0.55=0.022\mathcal{L}_{\text{aux}} = 0.01 \times 4 \times 0.55 = 0.022.

Interpretation: the collapsed batch costs 0.0220.022, which is 2.22.2 times the balanced cost. The gradient on expert 1 is αEϕ1=0.01×4×0.75=0.03\alpha E \phi_1 = 0.01 \times 4 \times 0.75 = 0.03, while expert 4 gets 00. 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.

Example
What the balance costs in dropped tokens

Many systems also cap how many tokens each expert accepts per batch. With a capacity factor of 1.251.25, the cap is ⌈1.25×T/E⌉=⌈1.25×8/4⌉=⌈2.5⌉=3\lceil 1.25 \times T / E \rceil = \lceil 1.25 \times 8 / 4 \rceil = \lceil 2.5 \rceil = 3 tokens per expert.

  1. In the balanced batch, every expert gets two tokens, under the cap of three. Nothing is dropped.
  2. 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 α\alpha 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.

SituationProblemBetter choice
Router collapses within the first few thousand stepsA few experts get almost all tokens, the rest starveAdd the auxiliary loss, or raise α\alpha within the usual range
Balance looks good but loss is flatExperts may be interchangeable, so there is little specialisationLower α\alpha, or check whether the task needs specialisation at all
Many tokens dropped under a capacity capOverflow tokens skip the layer and lose qualityRaise the capacity factor, or accept the drops and measure the effect
Top-1 routing with a hard capGradients through the chosen gate onlyUse the softmax probability to scale the output, as Switch does
Batches are small or vary a lot in compositionϕi\phi_i is noisy from batch to batchCompute 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 α=0.01\alpha = 0.01. 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 E=4E = 4 experts, a batch of T=16T = 16 tokens, and α=0.01\alpha = 0.01. Each expert receives exactly four tokens, and the mean gate probabilities are uniform. What is Laux\mathcal{L}_{\text{aux}}?Show answer

Dispatch fractions are ϕi=4/16=0.25\phi_i = 4/16 = 0.25 and gate probabilities are Pi=0.25P_i = 0.25. The sum ∑iϕiPi=4×0.0625=0.25\sum_i \phi_i P_i = 4 \times 0.0625 = 0.25. The loss is 0.01×4×0.25=0.010.01 \times 4 \times 0.25 = 0.01. This is the balanced value α\alpha, as expected.

QuestionWhy is the dispatch fraction ϕi\phi_i not differentiated, while the gate probability PiP_i 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 ϕi\phi_i as a constant and differentiating through PiP_i 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 α\alpha.

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