LLM Kitchen
Explanations

Part X ยท Other kinds of models

Ch 37. Many experts

Every weight, every token

GPT-2 small is dense: all 124 million of its weights work on every token, so a bigger dense model is slower for every token. A gets around this. It splits the MLP in each block into several smaller ones, the experts, and adds a small router that sends each token to only the best few.[1]

GPT-2 small is dense: every one of its 124 million weights takes part in predicting every token. Making a dense model bigger makes every token more expensive to process. A gets around this. Take the MLP inside each block (Ch 10) and split it into several smaller MLPs, the experts. In front of them goes a small router: for each token, it scores every expert and sends the token only to the best few.[1][1]

Here is the router at work on โ€œThe cat sat on theโ€, choosing 2 of 8 experts for each token. Pick a token, and change how many experts each token gets:

Try it

Send each token to a few experts

Computed in your browser
Experts per token
The router's score for each expert, for each token of โ€œThe cat sat on theโ€. Chosen experts are marked.
TokenExpert 1Expert 2Expert 3Expert 4Expert 5Expert 6Expert 7Expert 8
1.6%46%(chosen)1.8%10%0.2%6.3%33%(chosen)1.0%
1.9%4.7%8.0%18%(chosen)3.6%7.5%55%(chosen)1.2%
9.3%10%12%0.4%13%(chosen)0.6%54%(chosen)<0.1%
0.1%6.7%0.3%57%(chosen)3.8%0.9%32%(chosen)0.2%
0.9%35%(chosen)6.0%12%5.0%32%(chosen)5.8%3.4%

ยทcat goes to experts 7 and 4, and its output mixes theirs 75% / 25%. Each token runs through 2 of 8 experts, so it uses 25% of the experts' weights.

Simplified: this router's weights are random rather than trained, and it reads each token's embedding from Ch 5. In a real model, the router in every block is trained along with the rest and reads the token's vector in the residual stream.

Each tokenโ€™s result blends the outputs of its chosen experts. The other experts donโ€™t run for that token at all.

Each tokenโ€™s output is a blend of its chosen expertsโ€™ outputs, weighted by the routerโ€™s scores. The other experts donโ€™t run at all for that token, so their weights cost nothing. Different tokens go to different experts, so over a whole text every expert gets used.

The router, written down

For a tokenโ€™s vector x, the router multiplies by a weight matrix Wg to get one score per expert, keeps the top k, and turns those into weights with a softmax. The output is the weighted sum of the chosen expertsโ€™ outputs:

y=โˆ‘iโˆˆtopย kgi(x)Ei(x),g(x)=softmaxโก(topk(xWg))

Thatโ€™s Mixtralโ€™s layer, with k=2.[2] The widget takes a softmax over all eight scores and then rescales the top k to add up to 1, which gives the same weights.

Only the MLPs are split; attention and the embeddings are shared by every token. Thatโ€™s why Mixtral uses 13 billion weights per token, not a quarter of 47. If each expert has E weights and S are shared, then S+8E=47 billion and S+2E=13 billion, so each expert holds about 5.7 billion and about 1.7 billion are shared.

Training needs a push to spread the work. A router that favors a few experts trains them more, which makes it favor them even more, so Shazeer and his colleagues added a loss term that penalizes uneven use.[1] DeepSeek-V3 balances its experts without an extra loss term.[3]

Big, but cheap to run

Many large open models work this way. Mixtral 8x7B has 47 billion weights but uses only 13 billion for each token.[2] DeepSeek-V3 has 671 billion and uses 37 billion.[3]

This is how many large open models are built. Mixtral 8x7B has 8 experts in each block and sends each token to 2 of them: it has 47 billion weights in total, but each token uses only 13 billion, about a quarter.[2][2] DeepSeek-V3 goes further: 671 billion weights in total, 37 billion per token, about 6%.[3][3]

You might imagine one expert for grammar and one for cooking, but trained experts donโ€™t split the work that neatly. In Mixtral, they followed things like code syntax more than topics.[2]

It is tempting to imagine one expert for grammar, one for science, and one for cooking, but trained experts donโ€™t divide the work that neatly. When the Mixtral team checked which experts handled text on different topics, they found no obvious pattern. What showed up instead was syntax: the word self in Python code, or the spaces that indent a line, kept going to the same experts.[2][2]

Recipe card

Recap

  • A mixture-of-experts model splits each MLP into many experts, and a router sends each token to only a few of them.
  • The model can hold far more weights than it uses for any one token: Mixtral uses 13 of its 47 billion.
  • Experts arenโ€™t neat topic specialists; the router learns its own way of dividing the work.
  • The output is a weighted sum of the top k experts; only the MLPs are split, so a token uses more than k/n of the weights.

Next question: attention keeps every earlier token around, and that gets expensive for long texts. Could a model keep just a short summary instead?

Sources

  1. Noam Shazeer et al. (2017). Outrageously Large Neural Networks: The Sparsely-Gated Mixture-of-Experts Layer. ICLR 2017.
  2. Albert Q. Jiang et al. (2024). Mixtral of Experts. arXiv.
  3. DeepSeek-AI (2024). DeepSeek-V3 Technical Report. arXiv.

Sources

  1. Noam Shazeer et al. (2017). Outrageously Large Neural Networks: The Sparsely-Gated Mixture-of-Experts Layer. ICLR 2017.
  2. Albert Q. Jiang et al. (2024). Mixtral of Experts. arXiv.
  3. DeepSeek-AI (2024). DeepSeek-V3 Technical Report. arXiv.

Sources

  1. Noam Shazeer et al. (2017). Outrageously Large Neural Networks: The Sparsely-Gated Mixture-of-Experts Layer. ICLR 2017.
  2. Albert Q. Jiang et al. (2024). Mixtral of Experts. arXiv.
  3. DeepSeek-AI (2024). DeepSeek-V3 Technical Report. arXiv.