← Back to all chapters

Chapter 16Down the Rabbit Hole: How Mixture-of-Experts Models Keep Their Experts Busy

8 min read
rabbit-holellmmixture-of-expertstraining

DeepSeek-V3 has 671B parameters, but each token touches about 37B. Every MoE layer has one shared expert and 256 routed experts, and a small router picks 8 of the 256 per token. You get the knowledge of a huge network at the compute cost of a much smaller one.

What pulled me in was one line in the V3 report about how they keep those experts evenly used. Everyone before added an extra loss term. DeepSeek mostly didn't. They kept a little number on each expert, nudged up or down after every batch, that tilts the router's choices. No gradient.

A hospital with 256 doctors#

A triage nurse at the door sends each patient (token) to the 8 most relevant of 256 specialists. Only those 8 work.

Technically the router is a linear map from the token's hidden state to one logit per expert. Take the top-k, run those k expert FFNs, mix their outputs with gate weights. Compute scales with k; memory scales with all N experts.

Experts are sharded across GPUs (expert parallelism) and tokens ship to them by all-to-all, a synchronous barrier: the layer finishes when the busiest GPU does.

One token through an MoE layer. Click a box.

Imbalance is not cosmetic. In a toy run I'll come back to, mean expert load was 500 tokens and the hottest expert got about 1,750, so that layer takes roughly 3.5x as long as a balanced one. With a hard capacity cap (Switch Transformer used capacity factors of 1.0 to 2.0), overflow tokens aren't processed by that expert at all.

Why the router collapses on its own#

A learned router doesn't stay balanced, and the cause is a feedback loop, not bad initialization. More tokens means more gradient, which makes the expert better, which raises its router scores, which sends it more tokens. The rest sit idle and untrained: memory paid for nothing.

And since top-k isn't differentiable, an expert that never wins never learns it could have been useful.

The classic fix: fine the nurse#

Switch Transformer (and GShard) add an auxiliary loss:

L_aux = alpha * N * sum_i ( f_i * P_i )
 
f_i = fraction of tokens whose top choice is expert i   (hard count)
P_i = mean router probability assigned to expert i      (soft)

alpha = 1e-2 in Switch. With uniform routing every f_i and P_i is 1/N and the loss bottoms out at exactly alpha. f_i is an argmax count with no gradient; P_i has one. The product pushes overloaded experts down in proportion to their load: the derivative with respect to a token's probability for expert i is alpha * N * f_i / T.

But it's a second objective landing on the same router weights as the real one. Too small an alpha and the router collapses anyway. Too large and logits get dragged toward uniform regardless of the token, erasing the specialization that made MoE worth it.

The new fix: sticky notes on the doors#

Wang et al. (2024) proposed what DeepSeek-V3 then used at scale: leave the nurse alone, put a sticky note on each door. Each expert gets a bias b_i, added to the score only to choose the top-k. The gate weight that multiplies the output comes from the raw, unbiased score.

tokens -> router -> top-k -> experts -> task loss
            ^                              |
            +--- gradient: task + L_aux ---+

two objectives pull on the same router weights
Where the balancing signal enters

One token, 4 experts, k = 2, with V3's sigmoid affinity:

logits                    = [ 1.2,   0.4,  -0.3,   0.9 ]
affinity s = sigmoid      = [0.769, 0.599, 0.426, 0.711]
bias b (running state)    = [-0.20, +0.05, +0.12,  0.00]   # expert 0 has been hot
selection score s + b     = [0.569, 0.649, 0.546, 0.711]
top-2 by selection score  -> experts {3, 1}   (unbiased top-2 would be {0, 3})
gates from RAW s          = {e1: 0.599, e3: 0.711}
normalised                = {e1: 0.457, e3: 0.543}

Expert 0 had the top raw score but lost its slot because it's been overloaded. Expert 1 won a close call, yet is weighted by its own unbiased affinity, so the bias never leaks into what the layer computes.

raw affinity s (sets the gate weight)s + b (used only to choose the top 2)0.770.57expert 0b = -0.200.600.65expert 1b = +0.050.430.55expert 2b = +0.120.710.71expert 3b = 0.00
The sticky note in action, using the post’s one-token example. Expert 0 has the top raw score but loses its slot to the bias; red bars are the two picked.

After each batch the update is almost embarrassingly simple:

# loads: tokens routed to each expert this batch
mean = loads.mean()
b += u * np.sign(mean - loads)   # overloaded -> bias down, idle -> bias up

No autograd, no loss term. The bias is controller state, not a parameter. Wang et al. used u = 0.001; DeepSeek-V3 used 0.001 for the first 14.3T tokens, then 0 for the last 500B, freezing the biases.

Two choices do the heavy lifting:

  • Selection and weighting use different scores. Gating with s + b would make the bias part of the function the model must learn around.
  • The update uses sign, not imbalance size. One wildly hot expert can't make its bias overshoot. The cost: the bias never sits still, oscillating by roughly u.

Watching the controller in a toy#

The dive's numpy experiment: 16 experts, top-2, 4,096 tokens per step, 400 steps, a frozen router with built-in popularity skew. MaxVio = (max load - mean load) / mean load; 0 is perfect balance.

uMaxVio step 0step 100mean of last 100
0.02.5232.5802.568
0.0012.5231.2170.084
0.012.5230.0980.158
0.052.5230.5120.492
  • u = 0 (no bias)2.568
  • u = 0.0010.084
  • u = 0.010.158
  • u = 0.050.492
MaxVio averaged over the last 100 steps of the toy run, by bias update rate u. Lower is more balanced.

No bias never balances. u = 0.001 converges slowly but settles tight. 0.01 gets there fast, then jitters. 0.05 limit-cycles and never settles. The usual speed-versus-oscillation knob; Wang et al. report the same shape (0.001 best, 0.01 fluctuating). Caveat: frozen router, no language-modelling loss, so this shows dynamics, not model quality.

For quality, use the paper's numbers:

SizePerplexity, aux loss to loss-freeMaxVio
1B9.56 to 9.500.72 to 0.04
3B7.97 to 7.920.52 to 0.04
  • 1B, aux loss0.72
  • 1B, loss-free0.04
  • 3B, aux loss0.52
  • 3B, loss-free0.04
MaxVio, aux loss versus loss-free balancing, from the paper. Lower is more balanced.

Better balance and slightly better perplexity, the opposite of the usual trade. DeepSeek-V3 still keeps a sequence-wise balance loss with alpha = 0.0001, "just to avoid extreme imbalance within any single sequence". A tiny safety net, not the mechanism.

Balance over what, exactly?#

"Balanced" needs a scope: per sequence, micro-batch, or global batch. Qiu et al. (2025) found common frameworks compute the loss per micro-batch, often 1 to 4 sequences, forcing each tiny group to use every expert about equally. A micro-batch of all code gets punished for using code-heavy experts, which is the specialization you wanted. A global-batch loss gave better perplexity and more specialization, in runs up to 42.8B parameters and 400B tokens.

Where it breaks: your batch-mates#

At inference there's no controller, and routing depends on whoever shares your batch. That makes load balancing a security problem. Hayes, Shumailov and Yona showed that with capacity limits, an attacker whose queries share your batch can fill an expert's queue so your tokens overflow onto worse experts.

Hayes et al. Figure 1: adversarial inputs in a shared batch fill expert token queues, forcing the user's tokens onto non-ideal experts.

Figure 1 from Hayes, Shumailov and Yona, "Buffer Overflow in Mixture of Experts", arXiv:2402.05526, licensed CC BY 4.0. Cropped and recompressed.

A follow-up by Yona, Shumailov, Hayes and Carlini went further. With Expert-Choice routing (each expert picks its top tokens across the batch, perfect balance) plus the way CUDA's topk breaks ties, they recovered a victim's prompt with about 100 queries per token, shown on a two-layer Mixtral. The property that gives perfect balance, coupling every token to every other, is what leaks.

A misconception too: "experts specialise in topics". Mixtral's own analysis found mostly syntactic, token-level specialization, and a 2026 paper argues routing mostly reflects the geometry of the hidden space.

The open question#

Can you balance experts without giving up specialization? They conflict whenever the data is skewed, and there's no agreed Pareto frontier. A global-batch scope helped, and loss-free balancing matched or beat the aux loss. But expert parallelism still wants strict balance at serve time, exactly when the training-time controller isn't running.

Making routing independent of batch composition at inference would fix the straggler problem and the security problem together. As far as I can tell, nobody knows how without giving up something else.

Sources#

© 2026 Bharat Kaurav. Built with Next.js & Tailwind.

>_ bk@netherwatcheresc to close

Welcome, reader. Type help to see what this shell can do.