Topic 58 of 62 · Wider ML toolkit
MoE auxiliary losses: balance and router z-loss
HardWhy can a sparse Transformer need losses beyond token cross-entropy?
1 · Start with the intuition
A router can send nearly every token to the same expert even while the language-model loss improves. That wastes capacity and overloads communication. A separate issue is the absolute size of router logits, which softmax probabilities alone do not reveal. Balancing and z-loss address these different problems.
2 · How the mechanism works
Mechanism step 1 of 3 · no question to answer
Measure expert traffic
For top-1 routing, compute f_i: the fraction of valid tokens dispatched to expert i; P_i: the mean soft routing probability for i.
Next, we will look at: Add a balancing term. Move on when this operation makes sense.
Visualizing the behavior
Toy dispatch fractions: useful capacity versus collapse
DerivedBalanced dispatch
Collapsed dispatch
3 · The governing equation
L_balance = E Σ_i f_i P_i (top-1 Switch-style) L_z = (1/N) Σ_t [log Σ_i exp(r_ti)]² L_total = L_tokens + α L_balance + β L_z
Notation: E is expert count; N is valid routed-token count; r_ti is a router logit. α and β are tunable coefficients. This is a specific top-1 formulation; top-k implementations and loss-free balancing schemes differ.
A larger balancing coefficient can improve utilization but compete with specialization and language modeling. A router can also add a constant to every logit without changing softmax; z-loss notices that shift because logsumexp changes. Use stable logsumexp, exclude padding, define whether statistics are local or global, and monitor dropped tokens, utilization and task loss separately. These are MoE-specific auxiliaries, not required losses for every dense Transformer.
4 · Put concrete numbers on it
Worked numbers (from stated assumptions)5 · Common misconception to avoid
Router z-loss is not a load-balancing loss, and load balancing is not a guarantee that every expert learns a useful specialization. Do not report the total auxiliary-augmented objective as token NLL or perplexity.
Scenario practice · Choose the loss for the scenario
10 scenarios, ordered Easy → Medium → Hard. Choose the best option, then rate your confidence. A first miss gives a nudge and one retry; a second miss or reveal gives an explanation and zero credit.
Session-only practice. Changing level, leaving, or reloading resets this set.
6 · Self-check & interview synthesis
Explain it in your own words first
Before revealing the reference answer, try answering the opening question aloud in four beats: what it is → how the math works → what trade-off it makes → a concrete number or failure mode.
Follow-up question interviewers often ask next
MediumWhy can z-loss change when routing probabilities do not?
Show follow-up reasoning
Softmax is invariant to a common logit shift, but logsumexp(r+c)=c+logsumexp(r). Its square therefore detects shifts invisible to the probability distribution.