AI & machine learning / Mixture of experts

Mixture of experts

A layer made of many small networks and a router that wakes only a few of them for each token. Seven steps through the routing, the arithmetic, the saving and the training pitfalls.

Intermediate7 steps~7 min
Token — the word or word piece being processed
Router — scores the experts, picks the top few
Expert — a small network; blue when it runs
Idle expert — skipped for this token, costs nothing
// THE SHORT VERSION

In 30 seconds

A mixture-of-experts layer replaces one big block of a neural network with many smaller blocks, the experts, plus a small router.

For every piece of input the router picks just a few experts (two in this lesson), and only those do any work. Their outputs are blended into one result.

The model can therefore hold far more weights than it uses at any moment: more capacity for about the same computing cost. The price is harder training, since the router must learn to spread the work, and all the experts still have to fit in memory.

Mixture of experts, step by step

STEPS01 / 07

Expert outputs are really long vectors, shown here as single numbers. The routing table and load percentages are made-up illustrations of typical behaviour. Shares 0.73 and 0.27 are rounded.

STEP 01 / 07 One big layer, always on
THE MATH
output = layer(token) weights used: all of them bigger layer = slower token
  1. Step 1: One big layer, always on. A language model is built from layers, and in an ordinary (dense) layer every weight takes part in processing every token (a word or word piece). A bigger layer makes the model more capable, but the cost of each token grows in step. Mixture of experts is a way to add weights without paying for all of them every time.
  2. Step 2: Split it into experts. Replace the one big block with several smaller ones, the experts, each a small network with its own weights. In front of them sits a router (also called the gate): a much smaller network whose only job is to look at the incoming token and decide which experts should handle it. This layer has 8 experts.
  3. Step 3: The router scores them. For each token the router produces one score per expert. Only the highest few are kept, here the top 2: expert 2 with 2.0 and expert 6 with 1.0. The kept scores are turned into shares that add up to 1 (a softmax), giving 0.73 and 0.27. The other six experts are dropped for this token.
  4. Step 4: Only the chosen two run. Only the two chosen experts compute anything. Their outputs are blended using the router’s shares: 0.73 × 4.0 + 0.27 × 2.0 = 3.46. The other six experts are skipped entirely, which is where the saving comes from. Real outputs are long lists of numbers rather than single ones, blended in the same way.
  5. Step 5: Each token, its own experts. The choice is made afresh for every token, so different tokens use different experts, and similar tokens tend to share them. Nobody assigns the specialities: they emerge in training. In published analyses they often follow patterns like word type or punctuation rather than neat subjects such as biology.
  6. Step 6: More capacity, same compute. Count the weights. With 8 experts of 1 million weights each, the layer stores 8 million, but each token only passes through 2 million. A dense layer costing the same per token could hold just 2 million. So the model gets four times the capacity for about the same compute. It still needs memory for all 8 million.
  7. Step 7: Why training is hard. The router and the experts learn together, and that is delicate. Experts that happen to be picked early improve, so they are picked more, while the rest receive few tokens and learn little. An unchosen expert gets no feedback at all from a token. Common fixes add noise to the router’s scores or a penalty when the load is uneven.
VALUES SO FAR
denseall weights runbaseline
experts····layer
top 2····router
shares····router
output····result
idle····experts
stored····capacity
used····compute
risk····training

Spotted a mistake on this page?