research27 min studyLesson 573 in Engineering

Load-balancing losses: write the denominator before tuning the coefficient

Derive a stated Switch-style auxiliary objective and distinguish hard counts, soft probabilities, top-k normalization, and local versus global reduction scope.

On this page

What you’ll understand

  • Compute a top-one auxiliary loss from hard and soft routing statistics.
  • Identify which terms carry gradients and which are treated as hard observations.
  • Compare top-k and distributed reduction conventions without changing loss scale accidentally.

A load-balancing coefficient has no portable meaning without the exact loss it multiplies. Hard assignment counts, average soft probabilities, selected weights, and post-drop utilization are different statistics. Their normalizations can differ by expert count, top-k, layer count, or batch reduction. Write the complete expression and its population before comparing coefficients, gradient magnitudes, or the apparent balance of two training runs.

A Switch-style loss couples hard demand with soft mass

For a nonempty top-one routing batch, define f_e as the fraction of tokens whose requested winning expert is e, before capacity dropping. Define P_e as the mean full-softmax probability for that expert over all eligible tokens. Both vectors sum to one under these assumptions, but they need not be equal. The stated auxiliary loss multiplies their dot product by expert count and a coefficient. A balanced reference point with both vectors uniform gives coefficient alpha, independent of expert count.

During ordinary backpropagation through this hard-routing construction, counts are observations rather than a smooth argmax derivative. Gradients flow through the soft probabilities. A busy expert's larger observed fraction increases the pressure attached to its probability mass, but this is a surrogate optimization signal rather than a proof of equal future workloads. Do not claim every possible pair of nonuniform hard and soft statistics has a loss above the uniform reference or that minimizing one minibatch guarantees an even next batch.

fe=1T∑t=1T1{at=e},Pe=1T∑t=1Tpt,e,Lbal=αE∑e=1EfePef_e=\frac1T\sum_{t=1}^{T}\mathbf1\{a_t=e\},\qquad P_e=\frac1T\sum_{t=1}^{T}p_{t,e},\qquad L_{\rm bal}=\alpha E\sum_{e=1}^{E}f_eP_e
This is the stated top-one Switch-style convention: T>0 eligible tokens, E routed experts, requested winner a_t, full-softmax p, pre-drop f treated as nondifferentiable, and dimensionless α≥0. It is not a universal formula for every sparse router or every reported auxiliary loss.↔ Scroll wide equations horizontally.

Top-k and distributed scope change the statistic

With k requested experts per token, raw counts divided by T sum to k. Dividing by Tk instead makes assignment fractions sum to one. Either can appear in an implementation if the remaining scale compensates, so record the factor explicitly. DeepSpeed 0.19.7's general top-k path includes a division by k in its count/probability auxiliary expression, while its top-two path uses first-choice counts in the corresponding loss. A label such as top-two balancing therefore does not uniquely identify which assignments drive its gradient.

Averaging losses computed separately on routing groups is generally different from multiplying globally averaged f and P. The loss contains a product, and correlations between group demand and soft mass do not disappear by renaming the reduction global. Sequence-wise balancing can intentionally pressure every sequence rather than only an aggregate batch. Excluding padding tokens, counting only accepted assignments, or averaging across experts instead of summing also changes scale or meaning. Inspect masks and reduction groups alongside the equation, especially when data or expert parallelism changes.

fe(k)=neTk,∑efe(k)=1,1G∑g=1Gfg,ePg,e≠(1G∑gfg,e)(1G∑gPg,e)f_e^{(k)}=\frac{n_e}{Tk},\qquad \sum_e f_e^{(k)}=1,\qquad \frac1G\sum_{g=1}^{G}f_{g,e}P_{g,e}\ne\left(\frac1G\sum_g f_{g,e}\right)\left(\frac1G\sum_g P_{g,e}\right)
The first identity assumes exactly k distinct requested assignments per eligible token before dropping. The inequality denotes that the two expressions are not equal in general for equally weighted groups; particular data can make them equal. Unequal group sizes require explicit token weighting.↔ Scroll wide equations horizontally.

Loss values need operational counters and ablations

A low auxiliary value alone does not establish useful specialization, accepted workload balance, or good model quality. Track hard requested counts, soft mass, accepted counts, overflow, and per-rank work with clearly named denominators. Sweep the coefficient while keeping main-loss scaling and token budget fixed. Compare quality and resource use, because a router can improve count symmetry while harming the task or moving work onto slower communication paths. Report whether the logged value includes its coefficient and how layer losses are combined.

The function lab illustrates a concentration statistic, not this router loss. For a two-way fraction vector [(1+x)/2,(1−x)/2] with x between zero and one, squared concentration is (1+x²)/2. Reset, set a=1 and x=0, predict y=0 and concentration one half, then Play or Step. Separately Reset, set a=1 and x=1, predict y=1 and concentration one, then Play or Step. The fixed graph y=ax² has no hard assignments, P vector, autograd, or distributed groups. Its curve cannot substitute for measuring the specified f–P product.

Compute a coefficient-scaled top-one objective

Eight eligible tokens request four experts with counts [4,2,2,0]. Their mean full-softmax probabilities are [0.4,0.3,0.2,0.1]. Use the stated top-one loss with alpha 0.01.

  1. The hard fractions are [0.5,0.25,0.25,0], which sum to one. They are pre-drop fractions, independent of any accepted-capacity clipping.

  2. The dot product is 0.5×0.4+0.25×0.3+0.25×0.2=0.325. Multiply by E=4 to obtain 1.3 before the coefficient.

  3. Multiplying by 0.01 gives L_bal=0.013. For comparison, uniform hard and soft vectors under this same convention give 0.01, not zero.

The result

The logged value is interpretable only with its coefficient, expert-count factor, pre-drop population, and top-one convention.

Explore the model

Interactive lab

Interactive lab · function

A separate two-way concentration curve

Reset, set a=1 and x=0, predict y=0 and (1+y)/2=0.5, then Play or Step. Separately Reset, set a=1 and x=1, predict y=1 and concentration 1, then Play or Step. This fixed quadratic does not compute a router auxiliary loss, routing counts, or gradients.

A separate two-way concentration curveThis lab uses only y = ax². Playback traces the curve within the fixed integration interval from 0 to x; it does not estimate numerical error or change the chosen point. Current readings: y at chosen x: 0.608; Tangent slope · 2ax: 1.35; Signed area · ax³/3: 0.182. All parameters are adjustable with the labeled controls below.−2.4−1.201.22.4−9−4.504.59xy(x, ax²)
y = ax²TangentSigned integral
y at chosen x
0.608
Tangent slope · 2ax
1.35
Signed area · ax³/3
0.182
t = 0 s

This lab uses only y = ax². Playback traces the curve within the fixed integration interval from 0 to x; it does not estimate numerical error or change the chosen point. Parameter changes pause playback and recompute the model at the current elapsed time. Reset restores both parameters and returns time to zero. Playback starts paused. Adjust either parameter, then choose Play or Step.

Look forDifferent imbalance statistics should not share an undocumented normalization.

Check your understanding

Why can averaging local load-balancing losses differ from computing one loss using global hard and soft averages?

Try it yourself

Under the stated top-one convention with eight experts and alpha 0.02, both f and P are uniform. Find the loss. What changes if a top-two count vector divided by T is used without compensating for its sum of two?

Take a moment to make a prediction or work it out on paper. Open a hint when you need one.

Give me a hint

Sum eight products of 1/8; then consider the missing division by k.

Compare with the solution

The dot product is 8×(1/8)²=1/8, so the loss is 0.02×8×1/8=0.02. If otherwise comparable top-two counts are divided by T instead of 2T, their fractions double and the objective doubles unless another factor compensates. Coefficients cannot be compared while hiding that convention.

Take it with you

  • Hard counts and soft probability mass play different roles in the auxiliary gradient.
  • Top-k denominators and distributed reduction scope define loss scale.
  • Balance objectives require quality ablations and requested-versus-accepted counters.

Read further

Original sources and deeper explanations to follow at your own pace.

Optional progress, saved in this browser when available. Revisit any lesson at any time.

Lesson not marked complete.