Before you move a control in the demo, say what you expect to change.
Foundation reference
Sparse Mixture of Experts: Routing, Load Balancing & Expert Parallelism
A sparse mixture-of-experts layer routes each token to a few expert networks and combines their outputs. Mixtral 8x7B has 47B parameters but uses about 13B for each token, because every layer routes each token to 2 of its 8 experts (Jiang et al., 2024).
Study guide
Where to start
Why does an MoE layer save arithmetic but not memory, given that every expert's weights must stay available (or sharded across devices) although each token uses only k of them?
How to study this pagePredict · Check · Generalize · Continue
Compare what the demo shows with the key equation and with the papers cited below.
Identify the part of the idea that survives a change of notation or model, so you can recognize it in another paper or system.
The newer notebook covers the same concept and has an interactive demo.
Why It Matters for Modern Models
- A sparse mixture-of-experts layer routes each token to a few expert networks and combines their outputs. Mixtral 8x7B has 47B parameters but uses about 13B for each token, because every layer routes each token to 2 of its 8 experts (Jiang et al., 2024).
- DeepSeek-V2 has 236B parameters with 21B active per token and a 128K-token context, and pairs MoE with a compressed KV cache (multi-head latent attention) to keep serving costs down (DeepSeek-AI, 2024).
- MoE saves arithmetic but not memory: all experts must be stored, and routing tokens dynamically splits each batch into uneven groups per expert, which makes serving harder.
- Where LLM Serving changes how a fixed model is run, MoE changes the model itself so that each token costs less computation.
Questions to Think About
- Why does a router trained only by backpropagation tend to send most tokens to a few experts unless a balancing loss is added?
- At what granularity should load be balanced (per micro-batch, per global batch, per sequence), and how can balancing too finely work against expert specialization?
- How does MoE differ from an ensemble, given that each token passes through a different subnetwork?
- Why does distributed MoE need two all-to-all exchanges per layer, one to send tokens to their experts and one to bring the results back?
Interactive Demo
Core Math
If you prefer intuition first, try the demo above, then come back for the math.
A sparse mixture-of-experts (MoE) layer lets the total parameter count grow while the compute per token stays roughly fixed, because each token runs only of the experts (Shazeer et al., 2017).
Router probabilities (per token, per layer):
Given a token's hidden state , a linear router produces expert probabilities:
Top-k gating (sparse activation):
Let . Only those experts run, and their outputs are mixed:
Load-balancing loss (Switch Transformers form, Fedus et al., 2022):
With experts, let be the fraction of tokens in the batch sent to expert and the average router probability given to expert :
With uniform loads, for every expert, the loss equals 1 whatever the . Fedus et al. describe it as minimized under uniform routing. That holds when and coincide, but the hard counts need not match the average probabilities , and an uneven split can score lower. With two experts and four tokens whose router probabilities are three times and once, top-1 routing gives and , so
What the loss reliably supplies is a training signal, added with a small weight (0.01 in Fedus et al.). The counts are not differentiable; the gradient flows through , with . Measured in , that gradient is largest for the experts that received the most tokens, so a descent step shifts router probability, on balance, toward less-loaded experts: it lowers the load-weighted sum . The router reaches only through each token's softmax, so in a single step one busy expert's average probability can still rise.