13.8 Mixture of Experts
The gated MLP of Gated MLPs: SwiGLU and GeGLU holds two thirds of a Llama block’s parameters, and every token pays for all of them. Mixtral, Qwen-MoE, and DeepSeek-V3 replace it with many smaller MLPs and a router: each token uses two of them (or eight of 256), so a model can hold far more knowledge at the same cost per token.
Concepts from other sections (12)
This section uses these ideas. Each has a short definition and a link to the section that explains it in full. Tap a dotted term in the text to preview it.
- Active parameters: parameters participating in the computation for a particular token. Read more: 20.9 Architecture choices and performance limits
- op library (
F): the module of differentiable functions, each a NumPy forward plus one closed-form VJP joined byfrom_op. Read more: 6.3 The op library and gradcheck_all - logits: unnormalized log-scores that softmax turns into probabilities; here the table stores . Read more: 6.1 Byte bigram: counts, logits, safetensors v0
- tie rule: the fixed backtrace preference (diagonal, deletion, insertion) that makes the S, D, I split deterministic. Read more: 15.5 Multimodal eval metrics: WER, CER, VQA, captions, zero-shot
- Stable sort: a sort that keeps equal elements in their input order; heapsort is not one. Read more: 18.4 Binary heap in Programming and Algorithms
- Prefix sum: the running total of an array up to each position, so any subarray sum is the difference of two prefix sums; with a hash map of the prefix sums seen so far, it counts the subarrays that sum to in . Read more: Chapter 3: Arrays and hashing in Programming and Algorithms
- central differences: the numerical derivative , used as an oracle for analytic gradients. Read more: 6.3 The op library and gradcheck_all
- sigmoid: , mapping the real line into , with . Read more: 7.3 Activation Functions and Their Derivatives in Mathematics
- Sign rule: flipping signs so that , which makes the factorization unique. Read more: 9.3 Orthogonality, Householder QR, and orthogonal initialization in Mathematics
- Gradcheck: a comparison of a hand-written gradient against central differences of . Read more: 3.3 Oracles, golden and differential tests, gradcheck as a test in Software Engineering
- central difference: , a second-order approximation of . Read more: 7.1 Derivatives and Finite Differences in Mathematics
- tensor / pipeline / expert parallelism: splitting a model across GPUs by weight matrix, by layer, or by MoE expert. Read more: Chapter 20: Inference Engines and GPU Deployment
The TinyStories capstone asks a concrete question with this layer: at equal active parameters, does a mixture of experts (MoE) beat the dense MLP on TinyStories? Answering it needs three things, and this section builds each: a router that matches the published ones, a dispatch that does not loop over tokens in Python, and a way to stop every token from crowding onto the same expert, either with Switch Transformer’s auxiliary loss or with DeepSeek-V3’s loss-free bias.
Every expert is a GatedMLP. You will also use Linear, ModuleList, and Module from Module system and basic layers, the op library from The op library and gradcheck_all, Tensor indexing from Tensor, broadcasting backward, and no_grad, and PCG32 from Modular arithmetic, hashing, and PCG32 in Python in Mathematics. The reference solution is moe.py, with MoE (route, forward, update_bias), topk_ids, dispatch, combine, and load_balance_loss, against the contract course/contracts/py/tinyllm/modern/moe.pyi.
Learning objectives
- Route tokens with each of the three router types and explain how the choice and the weights are computed separately.
- Implement sorted dispatch and its inverse permutation, and trace them by hand.
- Compute the Switch load-balancing loss, explain why it equals at balance, and explain where its gradient comes from.
- Implement DeepSeek-V3’s aux-free bias rule and explain why the bias chooses experts but never weighs them.
Notation
Section titled “Notation”| Symbol | Meaning | Type / shape |
|---|---|---|
| tokens (every leading axis flattened) | int | |
| , | experts, experts per token | int |
| router logits of token : | float32[E] | |
| router probabilities | float32[E] | |
| the experts chosen for token | ids | |
| the weight of expert for token | float32 | |
| assignments to expert , divided by | float | |
| mean of over tokens | float | |
| the aux-free correction bias | float32[E] |
Routers
Section titled “Routers”An MoE layer computes, for each token,
Parameters grow with , and compute grows with . Published models differ in how they choose and how they compute the weights; the three routers the solution supports are:
router | chooses by | weights | used by |
|---|---|---|---|
softmax_topk | top of | , renormalized over when norm_topk | Mixtral (norm), Qwen-MoE (configurable) |
topk_softmax | top of | softmax over the chosen logits | Switch-style |
sigmoid | top of | , renormalized when norm_topk | DeepSeek-V3 |
Every weight is then multiplied by routed_scaling (DeepSeek-V3 uses 2.5). Ties go to the lowest id, the one tie rule used throughout the book. Shared experts (DeepSeek) see every token, and shared experts of width are one gated MLP of width .
Sorted dispatch
Section titled “Sorted dispatch”The obvious implementation loops over tokens and calls experts for each. That is correct but runs tiny matmuls. Sorted dispatch rearranges the work so each expert runs once.
Flatten the assignments as . A stable sort by expert id gives perm; x_sorted = x[perm // k] puts each expert’s tokens in one slice, and offsets (a prefix sum of the counts) bounds the slices. Each expert runs once on its slice, and inv_perm (the inverse permutation, inv_perm[perm[i]] = i) puts each output back at its assignment before the weighted sum over the slots. This is the shape of every fast MoE kernel: one matrix product per expert instead of one per token.
The load-balancing loss
Section titled “The load-balancing loss”A router left alone learns to favor a few experts: they get more gradient, improve, and attract more tokens. Switch Transformer’s loss, as Hugging Face computes it, pushes back:
At perfect balance and , so ; concentrating raises it. comes from an argmax and has no gradient, so the router learns through : , which means the experts that got the most tokens get their probabilities pushed down hardest. MoE.route returns aux_loss_coef * L_aux.
That raises a broader question: how does the router learn at all, if the choice of experts is discrete? The choice of is piecewise constant, so no gradient flows through it. The router still learns through the weights that multiply the chosen experts’ outputs, and through in the loss. Away from ties, central differences agree with the analytic gradient.
Balancing without a loss
Section titled “Balancing without a loss”An auxiliary loss also pulls on the main objective. DeepSeek-V3 avoids that by adding a bias only to the scores used for choosing, and after each step moving it by a fixed rate :
An overloaded expert’s bias falls until it is chosen less. The bias never enters the weights, so the output is still a mixture of the experts’ own scores. It is plain state, not a trained parameter: no optimizer touches it and it is not in state_dict.
Working it by hand
Section titled “Working it by hand”Take four experts, top 2, and two tokens with logits and .
- The softmax gives and .
- Token 0 takes experts 1 and 0 (largest first), with weights renormalized to . Token 1 has a tie between experts 0 and 2 at ; both are in the top 2, lowest id first, so it takes with weights .
- The assignments are , with experts . The stable sort gives
perm, tokensperm // 2, counts ,offsets, andinv_perm. - If the experts output in sorted order,
inv_permputs back in assignment order. Token 0 gets and token 1 gets . - For the loss, and , so , above .
The interface
Section titled “The interface”def topk_ids(scores, k) -> NDArray # [N, k], ties to the lowest iddef load_balance_loss(router_probs: Tensor, topk_idx, n_experts) -> Tensordef dispatch(x: Tensor, topk_idx, n_experts) -> tuple[Tensor, NDArray, NDArray] # x_sorted, offsets, inv_permdef combine(y_sorted: Tensor, topk_w: Tensor, inv_perm) -> Tensorclass MoE(Module): def __init__(self, d, d_ff_expert, n_experts, top_k, n_shared=0, router="softmax_topk", norm_topk=True, aux_loss_coef=0.01, bias_update_rate=0.0, routed_scaling=1.0, act="silu", rng=None) def route(self, x) -> tuple[NDArray, Tensor, Tensor] # idx, weights, aux def forward(self, x) -> Tensor; aux_loss, expert_load (properties of the last forward) def update_bias(self, expert_load) -> NoneThe free functions are the reusable pieces: tie-safe top-k, the loss, and the two halves of dispatch. MoE composes them; after each forward it exposes aux_loss and expert_load for the training loop, which passes the load to update_bias when aux-free balancing is on.
Building it step by step
Section titled “Building it step by step”Routing is a discrete choice followed by continuous weights, so split it that way. Choose ids from plain numpy scores (no gradient can flow through a choice anyway), then gather the weights from the autograd tensor, so the router learns through them. Ties must break the same way in every implementation; a stable sort of -scores keeps equal scores in id order, giving “lowest id wins” with no extra code.
Each expert should run once on a contiguous batch. Flatten the assignments, stable-sort them by expert id (perm), and gather the token rows perm // k. A prefix sum of per-expert counts gives slice boundaries, and the inverse permutation puts outputs back. Gathering rows by index is differentiable, so gradients flow back to x through dispatch and combine without any custom backward.
The Switch loss needs a count (no gradient) and a mean probability (gradient). Computing with np.bincount and with F.mean encodes exactly that split. The DeepSeek bias must change the choice and nothing else, so it is added only to the numpy scores fed to topk_ids, and the weights are gathered from the unbiased sigmoid. It is updated by a rule, so it is a numpy array, not a Tensor parameter. Finally, shared experts see every token, so of width are one GatedMLP of width , added after the combine.
Top-k with the tie rule
Section titled “Top-k with the tie rule”def topk_ids(scores, k): s = np.asarray(scores) if not 1 <= k <= s.shape[-1]: raise ValueError(...) # A stable sort of -s keeps equal scores in id order: ties go to the lowest id. return np.argsort(-s, axis=-1, kind="stable")[..., :k].astype(np.int64)On token 1 of the hand example, ; sorting stably gives ids , and the first two are .
The loss
Section titled “The loss”def load_balance_loss(router_probs, topk_idx, n_experts): idx = _ids(topk_idx, n_experts) # integer [N, k], ids in range # ... router_probs must be [N, E] N = idx.shape[0] # f_e counts assignments: a constant for autograd. P_e carries the gradient. f = np.bincount(idx.ravel(), minlength=n_experts).astype(router_probs.dtype) / N P = F.mean(router_probs, axis=0) return F.sum(P * f) * float(n_experts) is divided by , not , as Hugging Face does; that is why the balanced value is , not 1. The gradient with respect to is : F.mean contributes and the product contributes .
Dispatch and combine
Section titled “Dispatch and combine”def dispatch(x, topk_idx, n_experts): idx = _ids(topk_idx, n_experts) # ... x must be [N, d] k = idx.shape[1] perm = np.argsort(idx.ravel(), kind="stable") # assignment n*k + j, grouped by expert counts = np.bincount(idx.ravel(), minlength=n_experts) # offsets[e] .. offsets[e+1] is expert e's slice; E + 1 entries, starting at 0 offsets = np.concatenate([[0], np.cumsum(counts)]).astype(np.int64) inv_perm = np.empty_like(perm) inv_perm[perm] = np.arange(perm.size) # sorted slot of each assignment # Assignment a belongs to token a // k. return x[perm // k], offsets, inv_perm.astype(np.int64)
def combine(y_sorted, topk_w, inv_perm): N, k = topk_w.shape y = y_sorted[np.asarray(inv_perm)] # back to assignment order n*k + j y = F.reshape(y, (N, k, y_sorted.shape[-1])) return F.sum(y * F.reshape(topk_w, (N, k, 1)), axis=1) # weighted sum over the k slotsRun the hand example through the code. The experts per assignment are [1, 0, 0, 2]; stable argsort gives perm = (1, 2, 0, 3), perm // 2 = (0, 1, 0, 1), counts (2, 1, 1, 0), and offsets = (0, 2, 3, 4, 4). inv_perm[perm] = arange writes inv_perm = (2, 0, 1, 3). Sorted outputs indexed by inv_perm give , reshaped to token rows and , weighted to and .
Stability matters twice: within an expert’s slice tokens stay in order, so a kernel reading the slice sees a predictable layout, and the result is deterministic.
Routing
Section titled “Routing” def route(self, x): # ... x must be [N, d] z = self.gate(x) # router logits, a Tensor k = self.top_k if self.router == "sigmoid": s = F.sigmoid(z) idx = topk_ids(s.data + self.e_score_correction_bias, k) # the bias chooses ... w = F.gather(s, idx, axis=-1) # ... the unbiased score weighs if self.norm_topk: w = w / (F.sum(w, axis=-1, keepdims=True) + 1e-20) # HF's epsilon probs = s / F.sum(s, axis=-1, keepdims=True) # for the loss only elif self.router == "softmax_topk": probs = F.softmax(z, axis=-1) idx = topk_ids(probs.data, k) w = F.gather(probs, idx, axis=-1) if self.norm_topk: w = w / F.sum(w, axis=-1, keepdims=True) else: # topk_softmax probs = F.softmax(z, axis=-1) idx = topk_ids(z.data, k) w = F.softmax(F.gather(z, idx, axis=-1), axis=-1) # softmax over the chosen k only if self.routed_scaling != 1.0: w = w * self.routed_scaling aux = load_balance_loss(probs, idx, self.n_experts) * self.aux_loss_coef return idx, w, auxEvery choice reads .data (numpy) and every weight is built from the tensor z, which is the split between choice and weight in code. For the sigmoid router, the loss needs probabilities that sum to 1, so it normalizes the sigmoid scores; this never affects the output weights.
The forward
Section titled “The forward” def forward(self, x): lead, d = x.shape[:-1], x.shape[-1] xf = F.reshape(x, (-1, d)) # every leading axis becomes N idx, w, aux = self.route(xf) xs, offsets, inv_perm = dispatch(xf, idx, self.n_experts) parts = [] for e in range(self.n_experts): a, b = int(offsets[e]), int(offsets[e + 1]) if b > a: # an expert with no token does no work parts.append(self.experts[e](xs[a:b])) y = combine(F.concat(parts, axis=0), w, inv_perm) # parts concatenated are in sorted order if self.shared_experts is not None: y = y + self.shared_experts(xf) # shared experts see every token self._last = {"aux": aux, "load": np.bincount(idx.ravel(), minlength=self.n_experts).astype(np.int64)} return F.reshape(y, lead + (d,))The Python loop is over experts ( iterations), never over tokens. The per-step statistics are kept in a plain dict because, as the constructor’s comment notes, a Tensor that requires grad assigned as an attribute would register as a parameter.
The aux-free bias rule
Section titled “The aux-free bias rule” def update_bias(self, expert_load): load = np.asarray(expert_load, dtype=np.float64) # ... must be [E] # Overloaded experts (load above the mean) get a lower bias, so the # router picks them less next step; underloaded ones a higher bias. step = self.bias_update_rate * np.sign(load.mean() - load) self.e_score_correction_bias = (self.e_score_correction_bias + step).astype(np.float32)The name e_score_correction_bias and the module names gate, experts, and shared_experts follow the Hub checkpoints, so the Llama-family loader reads them by name. The full solution is in moe.py.
Why this design
Section titled “Why this design”Each token runs experts, so expert FLOPs are of running all of them; routing adds an gate and an sort.
Compare the alternatives. A per-token loop gives the same numbers but runs small matmuls in Python; sorting gives batched matmuls, the shape that MegaBlocks and vLLM’s fused_moe turn into grouped GEMMs. A dense mask, running every expert on every token and masking, is simple but costs times the FLOPs, which removes the point of an MoE.
The solution also has no capacity limit. Switch Transformer drops tokens beyond an expert’s capacity to keep shapes static; the solution keeps every token (slices can be any length, even zero), which matches the Hugging Face reference outputs exactly, whereas dropping would make the output depend on batch composition. And keeping e_score_correction_bias out of the parameters ensures no optimizer or weight decay touches it, so the sign rule is the only thing that moves it.
What can go wrong
Section titled “What can go wrong”The hand example (routing with a tie, the dispatch arrays, combine, and the loss) must come out exactly. Hugging Face goldens cover Mixtral, Qwen3-MoE without renormalizing, DeepSeek-V3 with bias, shared experts, and routed scaling, so MoE checkpoints and configs load correctly. Sorted dispatch must equal the dense per-token loop for all three routers, which proves the speed path is the correct path, and dispatch must be a stable grouping: contiguous slices, stable order, an exact inverse, and gradients back to x. Ties must go to the lowest id so routing reproduces across Python and C. The loss must be at balance, larger when concentrated, with gradient . A float64 central-difference gradcheck reaches x and the router, since the capstone trains the MoE. The bias rule is checked by hand, including that the bias chooses but never weighs, and 60 updates must spread a skewed router’s 64 tokens. Hub key names must match, an idle expert must work, and the statistics must not be parameters. Bad top_k, routers, rates, and expert ids must raise.
test_moe.py uses the per-token loop in numpy float64 as its oracle, with each router’s weights computed from the logits by the router table above (sorting with an explicit (score, id) key for the tie rule), run for every router. It adds a hand-sized dispatch with known offsets and roundtrip, the tie rule, the loss and its gradient on a two-token case, a central difference for the router weight, the bias rule, and the validation errors. The per-token loop is the definition of an MoE; sorted dispatch is an optimisation, so the loop is exactly the right oracle for it.
Warning: Scattering outputs back with
perminstead ofinv_permproduces outputs of the right shape on the wrong tokens.perm[i]names the assignment in sorted slot ; to read assignment ‘s output you need its slot,inv_perm[a].
The full list:
| Pitfall | Symptom | How the solution avoids it |
|---|---|---|
renormalizing when the config says norm_topk_prob = false | Qwen-MoE outputs scaled wrong | renormalizes only under if self.norm_topk |
| weighing by the biased score | the bias leaks into the output; DeepSeek disagrees | the bias is added to s.data for topk_ids only; weights gather from s |
scattering back with perm instead of its inverse | outputs land on the wrong tokens | combine indexes with inv_perm |
| reading tokens in token order, not grouped by expert | each expert runs on another expert’s tokens | dispatch returns x[perm // k], grouped by expert |
| counting from probabilities instead of assignments | the loss no longer measures the routing | f = bincount(idx) / N |
| a bias rule with the sign flipped | overloaded experts get more tokens | sign(load.mean() - load): above-mean load lowers the bias |
| ties to the highest id | routing differs between implementations | stable argsort of -scores |
| shared experts skipped | DeepSeek outputs miss a term | y + self.shared_experts(xf) when n_shared > 0 |
routed_scaling ignored | DeepSeek outputs 2.5 times too small in the routed part | w * self.routed_scaling |
| router weights detached | the router never learns | weights are F.gather/F.softmax of the gate tensor, not of .data |
| offsets that hold each expert’s end | slices shifted by one expert | offsets starts with 0 and has entries |
topk_softmax weighing by the full softmax | weights do not sum to 1 | softmax over F.gather(z, idx), the chosen logits only |
Where this leads
Section titled “Where this leads”The Llama-family model builds MoE layers from tl_num_experts, tl_top_k_experts, and first_k_dense_replace, so the decoder loads and matches Hugging Face MoE checkpoints. The TinyStories capstone compares MoE against dense at equal active parameters, using the active count from Counting: parameters, FLOPs, KV bytes, and memory plans.
Further reading
Section titled “Further reading”- Shazeer et al., “Outrageously Large Neural Networks” (2017).
- Fedus et al., “Switch Transformers” (2021), sections 2.1 and 2.2; section 2.2 covers the capacity factor that drops tokens over an expert’s capacity.
- Wang et al., “Auxiliary-Loss-Free Load Balancing” (2024).
- DeepSeek-V3 report, section 2.1.2; Hugging Face
DeepseekV3TopkRouterimplements group-limited routing (top groups first withn_groupandtopk_group, then experts inside them). - MegaBlocks and vLLM
fused_moe(vllm/model_executor/layers/fused_moe/): grouped GEMMs over sorted tokens, no padding to a capacity. - DeepSeek DeepEP: expert parallelism, with experts on different GPUs and tokens moved by all-to-all.
Key terms
Section titled “Key terms”- Mixture of experts (MoE): a layer of many expert MLPs of which a router picks per token.
- Router: the linear gate and selection rule that chooses experts and computes their weights.
- Sorted dispatch: grouping assignments by expert with a stable sort so each expert runs once on a contiguous slice.
- Inverse permutation (
inv_perm): the map from each assignment to its sorted slot, used to put outputs back. - Load-balancing loss: Switch Transformer’s , equal to at perfect balance.
- Aux-free bias: DeepSeek-V3’s per-expert correction added to choice scores only, moved by a sign rule.
- Shared expert: an expert that sees every token with weight 1.
Summary
Section titled “Summary”- A router picks the top of experts per token and weighs their outputs; parameters grow with , compute with .
- Sorted dispatch gives each expert one contiguous slice of tokens, puts results back with the inverse permutation, and equals the per-token loop exactly.
- The Switch loss is at perfect balance; is a count with no gradient, so the router learns through .
- DeepSeek-V3’s bias chooses experts but never weighs them, and a sign rule balances the load with no loss term.
- The choice uses numpy scores and the weights are gathered from tensors, so the router learns despite the discrete choice.
Review questions
Section titled “Review questions”- Why does
inv_perm, notperm, put the outputs back?Answer
`perm[i]` says which assignment sits in sorted slot $i$. To read assignment $a$'s output we need its sorted slot, which is `inv_perm[a]`. Indexing with `perm` applies the permutation a second time and lands outputs on the wrong tokens. - Why does the router still learn when top-k has no gradient?
Answer
The weights $w_{n,e}$ are gathered from the router's softmax or sigmoid tensor and multiply the chosen experts' outputs, so the task loss reaches the gate through them. The aux loss adds gradient through $P$. - Why is at perfect balance, and why does carry no gradient?
Answer
Balanced, $f_e = k/E$ (counts over $N$ with $k$ per token) and $P_e = 1/E$, so $E \sum_e (k/E)(1/E) = k$. $f$ is a count of argmax choices, a step function of the logits, so its derivative is zero almost everywhere; the solution makes it an explicit constant. - What goes wrong if the DeepSeek bias is added to the weights as well as to the choice?
Answer
The output becomes a mixture weighted by the bias, which is not a trained quantity and drifts every step. DeepSeek's checkpoints and Hugging Face's outputs use the unbiased sigmoid scores as weights. - Why are shared experts one
GatedMLPof width ?Answer
Every shared expert sees every token with weight 1, so their outputs add. The sum of $S$ gated MLPs is a gated MLP whose hidden layer is the concatenation of theirs, width $S f$; that is also how the checkpoints store them. - Why does the solution not impose an expert capacity, as Switch Transformer does?
Answer
Dropping tokens over capacity makes a token's output depend on what else is in the batch and breaks exact agreement with the Hugging Face references. Sorted dispatch handles slices of any length, including zero, so no token needs to be dropped.