Skip to content

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.

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 kk 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.
SymbolMeaningType / shape
NNtokens (every leading axis flattened)int
EE, kkexperts, experts per tokenint
zn∈REz_n \in \mathbb{R}^Erouter logits of token nn: WgxnW_g x_nfloat32[E]
pn=softmax(zn)p_n = \mathrm{softmax}(z_n)router probabilitiesfloat32[E]
Tn\mathcal{T}_nthe kk experts chosen for token nnids
wn,ew_{n,e}the weight of expert ee for token nnfloat32
fef_eassignments to expert ee, divided by NNfloat
PeP_emean of pn,ep_{n,e} over tokensfloat
beb_ethe aux-free correction biasfloat32[E]

An MoE layer computes, for each token,

yn=∑e∈Tnwn,e Experte(xn)  +  Shared(xn).y_n = \sum_{e \in \mathcal{T}_n} w_{n,e}\, \mathrm{Expert}_e(x_n) \;+\; \mathrm{Shared}(x_n).

Parameters grow with EE, and compute grows with kk. Published models differ in how they choose Tn\mathcal{T}_n and how they compute the weights; the three routers the solution supports are:

routerchooses Tn\mathcal{T}_n byweights wn,ew_{n,e}used by
softmax_topktop kk of pnp_npn,ep_{n,e}, renormalized over Tn\mathcal{T}_n when norm_topkMixtral (norm), Qwen-MoE (configurable)
topk_softmaxtop kk of znz_nsoftmax over the chosen logitsSwitch-style
sigmoidtop kk of σ(zn)+b\sigma(z_n) + bσ(zn,e)\sigma(z_{n,e}), renormalized when norm_topkDeepSeek-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 SS shared experts of width ff are one gated MLP of width SfS f.

The obvious implementation loops over tokens and calls kk experts for each. That is correct but runs NkN k tiny matmuls. Sorted dispatch rearranges the work so each expert runs once.

Flatten the NkN k assignments as a=nk+ja = n k + j. 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 kk slots. This is the shape of every fast MoE kernel: one matrix product per expert instead of one per token.

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:

Laux=E∑e=1EfePe.L_{aux} = E \sum_{e=1}^{E} f_e P_e .

At perfect balance fe=k/Ef_e = k / E and Pe=1/EP_e = 1 / E, so Laux=kL_{aux} = k; concentrating raises it. ff comes from an argmax and has no gradient, so the router learns through PP: ∂Laux/∂pn,e=Efe/N\partial L_{aux} / \partial p_{n,e} = E f_e / N, 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 Tn\mathcal{T}_n is piecewise constant, so no gradient flows through it. The router still learns through the weights wn,ew_{n,e} that multiply the chosen experts’ outputs, and through PP in the loss. Away from ties, central differences agree with the analytic gradient.

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 γ\gamma:

be←be+γ sign(load‾−loade).b_e \leftarrow b_e + \gamma\, \mathrm{sign}\big(\overline{\mathrm{load}} - \mathrm{load}_e\big).

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.

Take four experts, top 2, and two tokens with logits z0=(ln⁡2,ln⁡4,0,0)z_0 = (\ln 2, \ln 4, 0, 0) and z1=(ln⁡3,0,ln⁡3,ln⁡2)z_1 = (\ln 3, 0, \ln 3, \ln 2).

  • The softmax gives p0=(2,4,1,1)/8=(1/4,1/2,1/8,1/8)p_0 = (2, 4, 1, 1)/8 = (1/4, 1/2, 1/8, 1/8) and p1=(3,1,3,2)/9p_1 = (3, 1, 3, 2)/9.
  • Token 0 takes experts 1 and 0 (largest first), with weights (1/2,1/4)(1/2, 1/4) renormalized to (2/3,1/3)(2/3, 1/3). Token 1 has a tie between experts 0 and 2 at 1/31/3; both are in the top 2, lowest id first, so it takes (0,2)(0, 2) with weights (1/2,1/2)(1/2, 1/2).
  • The assignments a=nk+ja = n k + j are (t0,e1),(t0,e0),(t1,e0),(t1,e2)(t_0, e_1), (t_0, e_0), (t_1, e_0), (t_1, e_2), with experts [1,0,0,2][1, 0, 0, 2]. The stable sort gives perm =(1,2,0,3)= (1, 2, 0, 3), tokens perm // 2 =(0,1,0,1)= (0, 1, 0, 1), counts (2,1,1,0)(2, 1, 1, 0), offsets =(0,2,3,4,4)= (0, 2, 3, 4, 4), and inv_perm =(2,0,1,3)= (2, 0, 1, 3).
  • If the experts output (10,20,30,40)(10, 20, 30, 40) in sorted order, inv_perm puts back (30,10,20,40)(30, 10, 20, 40) in assignment order. Token 0 gets 2330+1310=70/3\frac{2}{3} 30 + \frac{1}{3} 10 = 70/3 and token 1 gets 1220+1240=30\frac{1}{2} 20 + \frac{1}{2} 40 = 30.
  • For the loss, f=(2,1,1,0)/2f = (2, 1, 1, 0)/2 and P=(7/24,11/36,11/48,25/144)P = (7/24, 11/36, 11/48, 25/144), so Laux=4 (1⋅724+12⋅1136+12⋅1148)=161/72≈2.236L_{aux} = 4\,(1 \cdot \frac{7}{24} + \frac{1}{2} \cdot \frac{11}{36} + \frac{1}{2} \cdot \frac{11}{48}) = 161/72 \approx 2.236, above k=2k = 2.
def topk_ids(scores, k) -> NDArray # [N, k], ties to the lowest id
def load_balance_loss(router_probs: Tensor, topk_idx, n_experts) -> Tensor
def dispatch(x: Tensor, topk_idx, n_experts) -> tuple[Tensor, NDArray, NDArray] # x_sorted, offsets, inv_perm
def combine(y_sorted: Tensor, topk_w: Tensor, inv_perm) -> Tensor
class 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) -> None

The 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.

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 NkN k 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 ff (no gradient) and a mean probability PP (gradient). Computing ff with np.bincount and PP 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 SS of width ff are one GatedMLP of width SfS f, added after the combine.

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, p1=(3,1,3,2)/9p_1 = (3, 1, 3, 2)/9; sorting −p1-p_1 stably gives ids (0,2,3,1)(0, 2, 3, 1), and the first two are (0,2)(0, 2).

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)

ff is divided by NN, not NkN k, as Hugging Face does; that is why the balanced value is kk, not 1. The gradient with respect to pn,ep_{n,e} is Efe/NE f_e / N: F.mean contributes 1/N1/N and the product contributes fef_e.

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 slots

Run 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 (10,20,30,40)(10, 20, 30, 40) indexed by inv_perm give (30,10,20,40)(30, 10, 20, 40), reshaped to token rows (30,10)(30, 10) and (20,40)(20, 40), weighted to 70/370/3 and 3030.

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.

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, aux

Every 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.

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 (EE 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.

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.

Each token runs kk experts, so expert FLOPs are k/Ek/E of running all of them; routing adds an O(NdE)O(N d E) gate and an O(Nklog⁡(Nk))O(N k \log(N k)) sort.

Compare the alternatives. A per-token loop gives the same numbers but runs NkN k small matmuls in Python; sorting gives EE 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 E/kE/k 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.

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 kk at balance, larger when concentrated, with gradient Efe/NE f_e / N. 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 perm instead of inv_perm produces outputs of the right shape on the wrong tokens. perm[i] names the assignment in sorted slot ii; to read assignment aa‘s output you need its slot, inv_perm[a].

The full list:

PitfallSymptomHow the solution avoids it
renormalizing when the config says norm_topk_prob = falseQwen-MoE outputs scaled wrongrenormalizes only under if self.norm_topk
weighing by the biased scorethe bias leaks into the output; DeepSeek disagreesthe bias is added to s.data for topk_ids only; weights gather from s
scattering back with perm instead of its inverseoutputs land on the wrong tokenscombine indexes with inv_perm
reading tokens in token order, not grouped by experteach expert runs on another expert’s tokensdispatch returns x[perm // k], grouped by expert
counting ff from probabilities instead of assignmentsthe loss no longer measures the routingf = bincount(idx) / N
a bias rule with the sign flippedoverloaded experts get more tokenssign(load.mean() - load): above-mean load lowers the bias
ties to the highest idrouting differs between implementationsstable argsort of -scores
shared experts skippedDeepSeek outputs miss a termy + self.shared_experts(xf) when n_shared > 0
routed_scaling ignoredDeepSeek outputs 2.5 times too small in the routed partw * self.routed_scaling
router weights detachedthe router never learnsweights are F.gather/F.softmax of the gate tensor, not of .data
offsets that hold each expert’s endslices shifted by one expertoffsets starts with 0 and has E+1E + 1 entries
topk_softmax weighing by the full softmaxweights do not sum to 1softmax over F.gather(z, idx), the chosen logits only

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.

  • 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 DeepseekV3TopkRouter implements group-limited routing (top groups first with n_group and topk_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.
  • Mixture of experts (MoE): a layer of many expert MLPs of which a router picks kk 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 E∑efePeE \sum_e f_e P_e, equal to kk 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.
  • A router picks the top kk of EE experts per token and weighs their outputs; parameters grow with EE, compute with kk.
  • 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 E∑efePeE \sum_e f_e P_e is kk at perfect balance; ff is a count with no gradient, so the router learns through PP.
  • 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.
  1. Why does inv_perm, not perm, 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.
  2. Why does the router still learn when top-k has no gradient?
    AnswerThe 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$.
  3. Why is Laux=kL_{aux} = k at perfect balance, and why does ff carry no gradient?
    AnswerBalanced, $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.
  4. What goes wrong if the DeepSeek bias is added to the weights as well as to the choice?
    AnswerThe 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.
  5. Why are SS shared experts one GatedMLP of width SfS f?
    AnswerEvery 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.
  6. Why does the solution not impose an expert capacity, as Switch Transformer does?
    AnswerDropping 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.