6.1.5 · D2Scaling & Efficient Architectures

Visual walkthrough — Sparse routing and gating networks

2,998 words14 min readBack to topic

This page rebuilds the central result of Sparse routing and gating networks — the sparse Mixture-of-Experts forward pass — one picture at a time. We assume you have never seen a "gate", a "softmax", or an "expert". Every symbol is earned before it is used.


Step 1 — What is an "input" and what is an "expert"?

WHAT. An input is just a list of numbers. When a Transformer (see Transformer Architecture) reads the word "chlorine", it hands the next layer a vector — a fixed-length list of numbers. We call it .

Two more everyday quantities will show up later, so let us name them now:

An expert is a small neural network that eats one and returns a transformed vector of the same size. Think of it as one specialist doctor: give it a patient , it gives back an opinion .

Figure — Sparse routing and gating networks

In the picture, the orange dot on the left is . The violet boxes are the experts. Right now none are lit — we have not decided who to call. That decision is the whole game.


Step 2 — Scoring the experts (why a linear map?)

WHAT. Before we can pick experts, we need a number for each one saying "how relevant are you to this ?" We produce such numbers, called logits, with one matrix multiply.

WHY a linear map and not something fancier? We want the cheapest possible judge. A single learned matrix turns the -number input into scores in one shot, and it is differentiable so training can improve it. Anything heavier would eat the compute savings we are trying to win.

Figure — Sparse routing and gating networks

Look at the bars: expert 3 scored high (long magenta bar), expert 1 scored negative. These are not yet probabilities — they can be huge, tiny, or negative. We fix that next.


Step 3 — Turning scores into a probability with softmax (why softmax?)

WHAT. We convert the raw logits into numbers that are all positive and add up to — a probability distribution over experts. That conversion is the softmax.

WHY softmax and not "just divide by the sum"? Plain division breaks on negative scores (probabilities can't be negative). Softmax first sends every score through — the exponential — which is always positive and preserves order (bigger logit → bigger weight). It is also smooth, so gradients can flow back into . That is exactly the tool for "make positive, keep ranking, stay differentiable".

Figure — Sparse routing and gating networks

Same experts as Step 2, but now the bars are all positive, colored by height, and their total area is exactly one full block. Expert 3 grabbed most of the probability; the negative-logit experts got tiny slivers, not zero. That "not zero" is a problem for compute — fix in Step 4.


Step 4 — Sparsifying: keep only the Top- (why throw the rest away?)

WHAT. We keep only the largest gate values and set every other gate to . Here is a tiny fixed number like or ; can be or .

WHY? A doctor's opinion costs money to compute — running an expert is the expensive part. Softmax gave every expert a nonzero weight, so naively we'd run all . But most weights are near-zero noise. By hard-cutting to the top , we run only experts and skip of them entirely. This is the heart of Conditional Computation: spend compute only where it matters.

Figure — Sparse routing and gating networks

With , only the two tallest bars survive (bright), the rest are grayed to . The two survivors are re-scaled so their heights add back to a full block. Only these two experts will actually run.


Step 5 — Combining the chosen experts into the output

WHAT. Run each surviving expert on , weight its output by its (renormalized) gate value, and add them up. That sum is the layer's output .

WHY a weighted sum? The gate value already encodes "how much do I trust expert for this ". Multiplying each expert's answer by its trust and summing is the natural blend — a confident expert dominates, a marginal one contributes a whisper.

Figure — Sparse routing and gating networks

Two bright arrows (the two chosen experts) flow into the sum node; the gray experts send nothing. Their outputs, scaled by the little gate weights, merge into the single orange output .

The compute payoff, in one line. A token pays for experts, each an FFN of hidden width (Step 1) and hence cost :

  • — experts actually run (fixed, small).
  • — input width; — expert hidden width, the expensive dimension.
  • — the cost of the gate matrix multiply, tiny next to the expert FFNs.

The cost depends on (fixed), not on . So we can grow — and total parameters — almost for free. This is the sublinear scaling MoE is famous for.


Step 6 — Backprop through a hard choice (why Top- needs a trick)

WHAT. Top- makes a discrete decision — expert in or out. There is no smooth slope on "should this expert have been in the set?", so the ordinary chain rule cannot push a gradient through the selection itself.

WHY it matters. Consider the two kinds of experts after a forward pass:

  • Selected experts do get a gradient: their output is multiplied by the live, differentiable weight , so both the expert and that gate value are trained normally.
  • Non-selected experts get zero gradient. The gate learns nothing about whether skipping them was wise, because their contribution was hard-cut to before any loss was computed.

PICTURE + FIX. The standard workarounds:

  • Keep the soft gate weight on the backward path. Because the surviving weight is a real, differentiable number, the gate does receive a usable gradient for the experts it did pick — the selection is discrete but the magnitude is smooth. This alone gives the gate a learning signal.
  • Straight-through estimator. Treat the hard 0/1 mask as the identity during the backward pass: forward uses the discrete Top-, but backward pretends the mask was the smooth softmax, letting a little gradient reach even near-miss experts.
  • Load-balancing loss (below). An auxiliary term that adds gradient pressure spreading tokens across all experts, so no expert is starved of data long enough to fossilize.
Figure — Sparse routing and gating networks

The figure contrasts the solid gradient path through a selected expert (bright arrow back to the gate) with the dead, dashed path of a skipped expert — and shows the straight-through shortcut that revives a trickle of signal.


Step 7 — The degenerate case: collapse, and the balancing loss

WHAT. What if the gate learns "Expert 3 is best for everything"? Then Top- always picks expert 3, the other experts never run, never learn, and your -expert model behaves like a -expert model. This is collapse, and it must be handled or the whole idea fails.

WHY it happens. Early in training one expert is randomly slightly better; the gate prefers it; that expert gets all the data and improves more; the gate prefers it even harder. A runaway feedback loop — a Long-tail Distribution over experts where one expert eats everything.

THE FIX — a differentiable balancing loss. Define two per-expert quantities over a batch of tokens:

The Switch Transformer auxiliary loss adds:

  • — a hyperparameter (a knob you set, typically around ) that trades off balancing pressure against the main task loss: too small and experts collapse, too large and the model routes uniformly at random and stops specializing.
  • is treated as a detached constant — gradients flow only through the differentiable .
  • The factor keeps the loss for any expert count.
Figure — Sparse routing and gating networks

Left bars: collapsed load — one expert towers, the rest starve. Right bars: after repeated aux-loss nudges, load spreads toward the dashed uniform line .


The one-picture summary

Figure — Sparse routing and gating networks

The whole pipeline in one frame: linear gate softmax Top- cut (a discrete step needing a backprop trick) run only the chosen experts weighted sum , with the balance loss watching over expert usage.

Recall Feynman retelling — say it in plain words

An input arrives as a list of numbers. A cheap little judge (one matrix) scores every expert. We squash those scores through softmax so they become positive and sum to one — a vote share per expert. We keep only the two biggest votes and throw the rest away, because running an expert costs money and we only want the best two. We re-scale those two votes so they add to one, run just those two experts on the input, weight each answer by its vote, and add them up — that sum is the output. The keeping-the-top-two step is a hard yes/no with no slope, so to train the judge we lean on the smooth vote sizes it did assign (plus a straight-through shortcut) to carry gradient back. The only remaining danger is one expert hogging all the votes and the others starving; a balancing loss keeps nudging probability off overused experts each batch until the work settles into an even spread. Because we always run just two experts no matter how many exist, we can pile up thousands of experts almost for free.

Recall Quick self-test

What does softmax fix that plain division cannot? ::: Plain division breaks on negative logits; softmax runs every score through first, making them all positive while keeping their order, and stays differentiable. Why does active compute grow with and not ? ::: We only run the top- experts, so cost is where is the expert hidden width; the extra experts are never computed, only the tiny gate scales with . Why can't ordinary backprop pass through Top-, and what do we do? ::: Top- is a discrete select — no slope on membership. We rely on the smooth surviving gate weights for selected experts and use a straight-through estimator to leak a little gradient to near-miss ones. Is minimized only at uniform routing? ::: No — with fixed it is linear in . It supplies a per-step gradient pushing probability off overloaded experts; the balanced state (loss value ) is its stable fixed point, not a static minimizer. What is ? ::: A hyperparameter weighting the balance loss against the task loss — too small lets experts collapse, too large forces near-random routing.

See also: Model Parallelism (how the experts get spread across devices), Knowledge Distillation (compressing a huge MoE into a dense student), Neural Architecture Search, Attention Mechanisms, Batch Normalization.