Topic 16 of 62 · Inside the Transformer block
Mixture of Experts
MediumHow does a sparse MoE layer increase model capacity without using every parameter on every token?
1 · Start with the intuition
Suppose we want more parameters, but cannot afford to run every token through a much larger feed-forward network. Could we keep several networks and choose only a few for each token? That is the sparse MoE idea. A learned router chooses k experts from a bank of E, and we combine their outputs. We have increased the parameters available to the model without activating all of them for every token.
2 · How the mechanism works
Mechanism step 1 of 3 · no question to answer
Score & select top-k
The router projects the token’s hidden state to E logits and picks the k highest-scoring experts.
Next, we will look at: Dispatch to active experts. Move on when this operation makes sense.
Visualizing the behavior
One token’s routing decision (8 experts, top-2)
IllustrativeRouter softmax
Top-2 weights renormalized (0.32 / 0.53, 0.21 / 0.53)
Stored vs active parameters (toy layer from the example)
Derived- Shared parameters
- Expert parameters
3 · The governing equation
S(x) = TopK(router(x), k)
y = Σ_{e ∈ S(x)} pₑ(x) · Eₑ(x)
Total parameters ≈ P_shared + E × P_expert
Active parameters / token ≈ P_shared + k × P_expertNotation: E is total experts per layer, k is active experts per token, Eₑ(x) is expert e’s FFN output, and pₑ(x) is the normalized routing weight.
Follow two tokens rather than one. The first might choose experts 1 and 3; the second might choose 2 and 7. Only two experts run for each token, but four different experts are needed across those tokens. This explains why active parameter count is not a memory budget: a resident serving setup must keep the whole expert bank available. If those experts live on different GPUs, the tokens also have to travel to them and their results return. And if many tokens choose the same expert, that device gets more work. Memory, communication, and load balance follow directly from the routing decision.
4 · Put concrete numbers on it
Worked numbers (from stated assumptions)5 · Common misconception to avoid
Thinking “top-2 out of 8 means the GPU only needs memory for 2 experts” or “an 8×7B MoE has 56B parameters and costs as little memory to serve as a 14B model.” Attention is shared (so total parameters are less than 8 × 7B), and all 8 experts must stay in HBM to serve a batch.
In practice · implementation & engineering rule of thumb
Always log per-expert token counts and max/mean expert load ratio. If one expert receives 5× the average token share, that GPU becomes the straggler for the entire all-to-all step.
# Top-k routing with load-balance monitoring: logits = router(x) # [T, num_experts] probs = logits.softmax(dim=-1) weights, top_idx = torch.topk(probs, k=2, dim=-1) weights = weights / weights.sum(dim=-1, keepdim=True) # Diagnostic: fraction of tokens routed to each expert load_frac = torch.bincount(top_idx.reshape(-1), minlength=E).float() / (top_idx.numel())
Go one level deeper
Follow-ups that often come after a correct first answer.
- Load-balancing loss
- Switch Transformer adds α·N·Σᵢ fᵢ·Pᵢ, where fᵢ is the fraction of tokens sent to expert i and Pᵢ is the mean router probability for it. It is minimized when routing is uniform, which discourages the router from collapsing onto a few experts. The paper used α = 0.01.
- Capacity factor and dropped tokens
- Expert capacity = (tokens per batch / number of experts) × capacity factor. Tokens beyond an expert’s capacity are dropped (they skip the layer through the residual) or rerouted, depending on the implementation. A higher factor drops fewer tokens but wastes compute on padding.
6 · Self-check & interview synthesis
Explain it in your own words first
Before revealing the reference answer, try answering the opening question aloud in four beats: what it is → how the math works → what trade-off it makes → a concrete number or failure mode.
Follow-up question interviewers often ask next
MediumWhy can an MoE layer have lower throughput than expected even when active FLOPs per token are small?
Show follow-up reasoning
Because each expert only receives a fraction of the batch (~k/E of the tokens on average, and less for under-routed experts), making the per-expert matrix multiplications smaller and more memory-bandwidth-bound. Add router overhead, load skew across experts, and all-to-all network latency across GPUs, and wall-clock time is governed by systems efficiency rather than active FLOPs alone.