From Routing to a Balanced MoE
Introduction
Project introduction
From Routing to a Balanced MoE
You have now written every piece of a mixture-of-experts layer. These are top-k routing, dispatch and combine, parameter counts, expert capacity, the balancing and z-losses, and the bias update.
In the notebook these pieces become one MoE layer with 8 experts inside a tiny character language model. It reads made-up text from four domains (arithmetic, English, DNA and code) and predicts the next character from the 8 before it. It is a small MLP, not a transformer. It has no attention, so the feed-forward layer that becomes the experts does most of the work. Mixtral makes the same swap inside every transformer block. A dense model with one expert-sized layer gets the same training recipe, so both do about the same work per character.
What you will find out
With nothing balancing it, the router sends about two thirds of the characters to one expert and leaves five almost unused. This happens in this top-1 setup, which was picked because it collapses reliably; small attention transformers we tried stayed balanced. The loss hardly notices, but once every expert gets a capacity of 1.25 times an even share, about half the characters are turned away and the MoE does far worse than the dense model.
The Switch balancing loss and the bias method both spread the load nearly evenly. The bias method drops no characters under the same capacity and keeps the MoE ahead of the dense model; the balancing loss still drops a few and ends about level with it or slightly behind. Both fixes use stronger settings than the papers because the runs are short, with a balancing weight of 0.05 instead of 0.01 and a bias rate of 0.01 instead of 0.001. At this tiny scale the MoE's gain over the dense model is small, 0.007 to 0.025 nats per character in 4 seeds, while the papers report much larger gains at scale. Finally you see which characters each expert learned to take.
Each character visits one expert
8 experts, top-1The router scores all 8 experts for "4" and sends it to expert 4 only.
expert 0
0
expert 1
0
expert 2
0
expert 3
0
expert 4
1
expert 5
0
expert 6
0
expert 7
0
How the notebook is organized
01
Build and race
Assemble the MoE layer and train it next to a dense model of equal compute.
02
Watch it collapse
Measure the load per expert and what a capacity limit does to a lopsided router.
03
Balance it
Train with the balancing loss, then with the bias update, and compare all three.
Train a mixture-of-experts language model
Run the whole investigation in the browser, about 45 minutes.