6.1.6 · D1Scaling & Efficient Architectures

Foundations — Load balancing in MoE

1,925 words9 min readBack to topic

Before you can read a single load-balancing formula, you need to be fluent in the pieces those formulas are built from. This page introduces each symbol and idea from absolute zero, in an order where every new thing only uses things already defined. Nothing here assumes you have seen Mixture of Experts (MoE) Architecture before — we build it.


0. The scene: what an MoE layer physically is

Look at the figure. A single input arrives on the left. Instead of one big network processing it, we have stations. A little box at the bottom — the router — decides which stations the input actually visits. Only the chosen stations do work; the rest stay idle for this input.

Why bother? Because you get the knowledge of a huge model (all experts' parameters) while only paying the compute of the few you actually use. That trade is the whole reason MoE exists — see Mixture of Experts (MoE) Architecture.


1. The token — the thing being routed

The picture of a vector: an arrow / a column of numbers, e.g. . Each number is one feature the earlier layers extracted.

Why we need it: the router looks at and must decide "which experts suit this token?" So is the question; the routing decision is the answer.


2. Counting symbols: , , , ,

These are plain counters. Meet them before any formula uses them.

The picture: a grid, rows = experts , columns = tokens . A cell is lit if token went to expert . This grid is the routing decision, and everything about balancing is about the shape of the lit cells — see Top-K Routing in MoE.


3. Softmax — turning scores into a choice

The router first produces a raw score for each expert. Scores can be any numbers (negative, huge, tiny). We need to turn them into something interpretable: probabilities that are all positive and add up to . The tool that does exactly this is softmax.

Why this tool and not, say, "just divide each score by the total"? Two reasons, both visible in the figure:

  1. Raw scores can be negative; plain division would give negative "probabilities," which are meaningless. The (the exponential — a function that turns any real number into a positive one) fixes that: .
  2. The denominator (the sum over all experts) forces the outputs to add to exactly . That is what "" means later — it is not magic, it is baked into softmax.

4. Gating scores — the router's opinion

Break the pieces:

  • = the router's weight matrix. Multiplying turns the token into raw scores (one per expert). This is the only thing the router learns.
  • Wrapping softmax around it makes those scores into probabilities: , all positive, summing to .

Picture: for one token, a bar chart of bars whose heights add to . A tall bar = "the router really wants this expert."

Why we need : it is soft (a smooth number), so gradients can flow through it during training. Hold onto that word "soft" — it is the whole reason exists later.


5. Top-K and the indicator — the hard choice

The router has soft opinions , but a token only visits experts. So we keep the biggest and throw the rest away. That is Top-K routing.

So reads: " if token actually went to expert , else ." That is exactly a lit cell in the grid from Section 2.

The tilde just means "the cleaned-up version." The output blends the chosen experts: where is expert 's output for the token.


6. Two ways to measure "how busy is expert ": and

Here is the payoff of the soft/hard distinction. We describe an expert's load two ways.

Look at the two bar charts. (left, teal) counts actual visits — jagged, because it comes from hard switches. (right, orange) is the router's average intention — smooth, because it comes from softmax. They usually look similar, but they are not the same object, and the load-balancing loss deliberately multiplies one by the other. That is the entire trick of the Switch Transformer auxiliary loss you meet in D2.


7. Two useful sum facts (memorise the pictures)

Picture for : each token is a coin worth "visits"; across the batch of tokens the visits get spread over experts, and when you total the fractions they must come back to . Nothing is created or lost. These two facts are the constraints that make the load-balancing loss behave the way it does.


8. Capacity — the physical shelf size

Picture: a shelf holding at most that many tokens. If more tokens are routed to expert than the shelf holds, the overflow is dropped (skips the expert, passes through unchanged). That overflow idea belongs to Expert Capacity and Token Dropping. It is the hard safety net that complements the soft auxiliary loss.


Prerequisite map

token vector x

router weights Wg

counters N B K

softmax

gate g_i soft probability

Top-K pick hard switch

indicator 1 of chosen

mean prob P_i differentiable

dispatch fraction f_i hard count

Load balancing loss

capacity C shelf size

Load balancing in MoE

Every arrow means "you must understand the source before the target makes sense." The two paths into the loss — the soft path and the hard path — are exactly the two quantities the Switch Transformer and GShard losses combine.


Equipment checklist

Cover the right side; answer, then reveal.

What is an expert in an MoE layer?
A small ordinary neural network; the layer holds of them side by side.
What does the bold stand for?
One token, represented as a vector (a list of numbers).
What do , , and count?
experts, tokens in a batch, experts each token may visit.
What does softmax guarantee about its outputs?
They are all positive and sum to exactly .
Why does softmax use the exponential ?
To turn any real score (even negative) into a positive number before normalizing.
What is ?
The router's soft probability for expert , from softmax of .
What does the indicator output?
if the condition is true, if false.
Which is differentiable, or , and why?
— it averages the smooth gate ; is a hard count with no slope.
State the two conservation laws.
and .
What is the fair-share target for and for ?
and .
What does the capacity factor control?
The size of each expert's token buffer, ; overflow tokens are dropped.