Sparse experts. An MoE layer replaces the MLP with E expert MLPs and a router, a linear layer giving each token one score per expert. Each token goes to its top k experts and their outputs are mixed with the softmax weights (Shazeer et al. 2017). Parameters grow with E while compute per token stays that of k experts. Mixtral 8x7B has 46.7 B parameters but uses 12.9 B per token (Jiang et al. 2024); you will count both.
Balance. Hardware processes each expert’s tokens as a fixed-size batch, so every expert has a capacity: capacity factor × T k / E slots. Tokens beyond it are dropped and pass through on the residual stream. Left alone, a router sends most tokens to the few experts that got ahead early. Switch Transformer adds E Σ fi Pi to the loss, with fi the fraction of tokens routed to expert i and Pi its mean router probability (Fedus et al. 2021). A z-loss on the router logits keeps them small and stable (Zoph et al. 2022).
Long context. Attention costs O(T²), and the KV cache grows with T (5.1). Sliding-window attention lets each token see only the last w tokens; stacked layers still pass information further (Beltagy et al. 2020; Mistral 7B, Jiang et al. 2023). RoPE models fail beyond their training length because they meet rotation angles they never saw. Position interpolation divides positions by the extension factor, a little fine-tuning recovers quality (Chen et al. 2023), and NTK-aware scaling instead raises the RoPE base, which keeps the high frequencies that resolve neighbouring tokens (Peng et al. 2023, YaRN).
Static view on small screens. Open this page on a laptop to interact.
experts E:top-k:
0 steps applied
balancing loss E·Σ fiPi = 1.151 (about 1 when even, 4 at total collapse)dropped choices: 4 of 40; capacity 13 slots per expertrouter z-loss: 7.95; routing fractions f: 0.42, 0.23, 0.15, 0.20
Each dot is a token; the router is a linear map from the embedding to one score per expert, so the experts split the plane into wedges. The tokens come from four topics of unequal size, so equal wedges already overload some experts, and pushing the bias toward expert 0 makes it worse: tokens beyond an expert's capacity are dropped (crossed) and skip the layer. The balancing-loss button follows its gradient, which lowers the scores of experts that get more than their share and raises the rest, until the wedges carve the tokens up evenly. A higher capacity factor drops fewer tokens but wastes compute on padding.
Left: 40 tokens in a two-dimensional embedding space, from four topics of unequal size, coloured by the expert the router picks first, over a background showing which expert wins in each region; tokens dropped for lack of capacity are crossed. Right: a bar per expert with the number of routing choices it received against a capacity line, with overflow dashed. Buttons set the number of experts and k, and apply 20 gradient steps of the balancing loss to the router or reset it; sliders set the capacity factor and a bias toward expert 0. The readout gives the balancing loss, dropped choices, the z-loss and the routing fractions. Keyboard: Tab to the buttons and press Enter; Tab to the sliders and use the arrow keys.
Top-k routing, load balancing, capacity, and RoPE context extensionstubs/m5_6_moe.py
Loading starter file…
Prefer your own editor? Edit exercises/stubs/m5_6_moe.py and run mlc check 5.6. The tests are identical; import the results on the progress page.
Hints · dispatch0/3 revealed
Hints · moe_forward0/3 revealed
Hints · moe_param_count0/3 revealed
Then train one. Open notebooks/5.6-moe-gpt.ipynb on Kaggle with a GPU and paste in your 4.6 GPT and your 5.6 file. It checks a PyTorch MoE layer against your moe_forward, then trains three models on TinyStories for the same steps: your dense GPT; an MoE with 8 half-width experts and top-2 routing, so the same compute per token; and that MoE without the balancing loss. Compare validation loss, parameter counts and how evenly each layer uses its experts. The gain from experts is modest at this size and grows with scale.