8.6 — Mixture of Experts (MoE)
Goal: Replace the dense FFN with a router-plus-experts sparse layer. Derive the top-k gate, the load-balancing auxiliary loss, and the activated-vs-total parameter distinction. Place Switch Transformer, GShard, and the fine-grained DeepSeek-MoE design within one framework, and connect to expert parallelism.
1. Why this session exists
The dense FFN of 6.8 and the SwiGLU FFN of §8.4 have one structural property in common: every token visits every FFN parameter. The compute per token is per layer, the parameter access per token is — the whole FFN block is read from memory and applied densely.
Mixture of Experts (MoE) breaks this coupling. Replace the single FFN with parallel FFNs ("experts") plus a small router that, per token, selects the top- most relevant experts. Each token visits only of experts. Compute per token: , where is an effective expert count discussed below. Total parameter count: (with a constant set by how wide each expert is). The decoupling: parameters scale with ; compute scales with .
Frontier models in 2024–2026 routinely have experts with activated per token. Mixtral-8x7B uses — B total parameters, B activated per token. DeepSeek-V3 uses fine-grained experts + 1 shared expert with activated — B total, B activated. The model has GPT-4-scale capacity at of GPT-4's inference cost per token.
This session derives the construction (router, top-, output mix), the load-balancing auxiliary loss that prevents expert collapse, and the operational tradeoffs (expert parallelism, capacity factor, fine-grained design). MoE is the largest architectural perturbation in Chapter 8.[1]
2. The basic MoE layer
Replace one FFN sub-layer with identical-shape experts , each being a standard SwiGLU FFN with its own parameters:
A small router matrix produces per-token gate logits:
The router selects the top- experts per token by largest gate logit. Let be the set of top- indices for token , and define normalised soft weights over those:
The MoE layer output is the convex combination of the activated experts' outputs:
This replaces the single FFN in a Transformer block. Everything outside the FFN — attention, residuals, normalisation — is unchanged.
2.1 Compute and parameter counts
Per token per layer:
- Router: logits via , then a top- selection (sorting cost , negligible). Compute: FLOPs.
- expert evaluations: per expert (using as in §8.4 §3), times activated experts. Compute: FLOPs.
- Combine: scalar multiplications and additions on -vectors, , negligible.
Total: FLOPs per token per layer, plus for the router. For , the FFN compute per token is the same as a dense FFN of width . We say the model has active experts.
Parameter count for the MoE FFN sub-layer:
where the first term is the experts and the second term is the router. For , : experts use parameters per MoE layer, vs for a dense FFN. Total parameters scale linearly with .
So the decoupling: , . At , the model has more parameters than a dense baseline but spends only the FFN compute per token. The arithmetic of "more parameters per token for less than proportional compute" is the entire economic argument for MoE.[3]
3. The load-balancing problem
The router learns to assign tokens to experts. There is no a priori constraint that says "every expert should be assigned roughly the same number of tokens." Without intervention, the router has a strong incentive to collapse: a few experts become "popular" and absorb most tokens, the rest become unused. This is bad for two reasons:
- Capacity is wasted. The unused experts' parameters contribute nothing; effective parameter count drops from to the number of frequently-used experts.
- Expert-parallel hardware utilisation drops. Experts are typically sharded one-per-device (§6 below). If of tokens go to of experts, of the devices are idle for of the time.
The fix, introduced in the GShard paper (Lepikhin et al., 2020, arXiv:2006.16668)[4] and refined in Switch Transformer (Fedus et al., 2021, arXiv:2101.03961)[5], is an auxiliary load-balancing loss added to the training objective.
3.1 The Switch Transformer auxiliary loss
For each MoE layer, let be the fraction of tokens (within a training batch) that route to expert and let be the router probability mass assigned to expert , averaged over the batch:
The auxiliary loss:
It is added to the LM cross-entropy with weight . The product is the empirical "activity" of expert ; the sum is minimised when both and are uniform , in which case and . Any deviation from uniform routing makes larger.
The loss is differentiable in (the router's softmax output) but not in (which is the hard top- assignment). The trick: drives the router's continuous probabilities towards uniform, which empirically dragsthe hard top- assignments in the same direction.
3.2 Capacity factor and dropping
A second balancing tool: cap the number of tokens any single expert can process per batch at
where is the capacity factor. Tokens beyond the cap are dropped (the residual stream still propagates them, but they skip the MoE layer entirely — equivalent to the expert output being zero). Capacity factor means strict balance; – leaves slack. Larger means fewer drops but more wasted compute on padding.
Capacity-factor dropping was central to early Switch Transformer training; modern MoE designs (DeepSeek-MoE, Mixtral) use auxiliary-loss-only or "auxiliary-loss-free" variants that adjust the router's logits to enforce balance without dropping.
4. Fine-grained experts: DeepSeek-MoE
Switch Transformer used , (a single expert per token), with each expert being a full-width SwiGLU FFN. Mixtral-8x7B used , , again with full-width experts.
DeepSeek-MoE (Dai et al., 2024, arXiv:2401.06066)[6] introduced the fine-grained expert design: make each expert smaller (lower ) but have many more of them, with correspondingly larger. The activated parameter count is the same; what changes is the combinatorial capacity of the gating.
Concretely: a baseline of has possible 2-expert combinations per token. Splitting each expert into pieces gives — same activated compute, but now possible 8-expert combinations. Each combination is a distinct "skill" the layer can specialise to.
DeepSeek-MoE's empirical finding: at iso-activated-parameters and iso-total-parameters, fine-grained MoE outperforms coarse MoE. DeepSeek-V2 uses shared experts with ; DeepSeek-V3 uses shared with . The "+ shared" experts are always activated for every token — they capture the cross-expert common knowledge that would otherwise have to be redundantly replicated.
4.1 Shared experts
A shared expert is an FFN that runs for every token, in addition to the top- routed experts. The MoE output becomes
The shared expert is the "common knowledge" path; the routed experts specialise in their respective niches. Empirically, having – shared experts prevents the routed experts from each having to relearn basic syntactic and lexical patterns, and improves quality at iso-parameters.
5. The activated-vs-total parameter convention
MoE models are always reported with both an "activated" and a "total" parameter count. The convention:
- Total parameters: the count you would get by summing all in the model, including all experts.
- Activated parameters: the count of parameters touched per token per forward pass — including attention, normalisation, embedding, all of activated experts, and the router.
For Mixtral-8x7B: B total, B activated. For DeepSeek-V3: B total, B activated. For DBRX (Mosaic, 2024): B total, B activated.
The activated count is the relevant number for compute (and therefore for both training and inference cost). The total count is the relevant number for capacity and (importantly) for memory — the full model still has to fit in GPU memory across the cluster, even if only a fraction is used per token.
Two consequences:
-
MoE shifts the parameter-to-compute ratio. A dense B model and a MoE model with B activated parameters spend the same compute per token, but the MoE model has substantially more capacity. At iso-compute, MoE typically outperforms dense on knowledge-heavy benchmarks (MMLU, GSM8K) and is roughly tied on reasoning (HumanEval, MATH) — the latter being the workloads where attention compute matters more than FFN capacity.
-
MoE requires more memory. The full experts must be resident across the inference cluster. This is the dominant reason MoE serving uses many GPUs even at modest batch sizes — the parameters are large.[7]
6. Expert parallelism
A practical question: at experts, how do you train and serve the model? You cannot keep all experts in one GPU's memory.
The standard solution is expert parallelism: shard the experts across devices, one (or a few) per device. Each GPU holds only its assigned experts' parameters. At each MoE layer:
- Every device computes the router's top- for its local tokens.
- An all-to-all collective communication redistributes each token to the device(s) hosting its top- experts.
- Each device runs its experts on the tokens it received.
- A second all-to-all sends the expert outputs back to each token's origin device.
- Each device combines its tokens' expert outputs as .
The two all-to-alls per MoE layer add communication cost — typically – of total step time at the cluster scales used for MoE training. The load-balancing loss is critical here: if tokens are unbalanced across experts, some devices receive far more tokens than others and become the bottleneck.
Expert parallelism is orthogonal to (and composes with) the data/tensor/pipeline parallelism axes of 7.9. A modern MoE training run uses DP TP PP EP, with EP typically .
[2, 8]7. When does MoE help, and when does it not?
MoE is not a free win — it is a specific tradeoff with operational costs (memory, communication) that pay off for some workloads.
MoE helps most:
- Knowledge-heavy tasks. FFN parameters store factual knowledge (6.8 §6 Aside on "FFN as key-value memory"). More total FFN parameters = more capacity to store facts = better closed-book QA, MMLU, etc.
- Multi-domain / multi-skill workloads. Different experts specialise to different domains (one for code, one for poetry, one for math, etc.); a dense model would have to compress all domains into the same FFN.
- Inference-cost-sensitive deployment at sufficient scale. If you have the GPU memory to host the full model, the per-token serving cost is dictated by activated parameters — MoE serves cheaper than a dense model of equivalent capability.
MoE does not help (or hurts):
- Pure reasoning / chain-of-thought tasks. Long-context reasoning is attention-compute-dominated, not FFN-capacity-dominated. MoE's wins are smaller here.
- Small models. At sub-B total parameters, the FFN capacity gain is small relative to the routing overhead and load-balancing constraints.
- Memory-constrained single-GPU inference. All experts must be resident; this rules out MoE for many edge / on-device deployments.
The frontier in 2026 is mixed: GPT-4, Claude-3+, Gemini-1.5+ are believed to be MoE; LLaMA-3 was dense; DeepSeek-V3 is aggressively MoE; Mistral-Large is dense; Mixtral is MoE. The choice depends on the deployment target and the organisation's tolerance for the operational complexity.
8. What we keep going forward
-
MoE replaces the FFN with experts and a top- router. Activated parameters , total parameters — the two are decoupled.
-
Load-balancing is required. Without an auxiliary loss (or an auxiliary-loss-free control loop), the router collapses to a few popular experts. The standard auxiliary loss is .
-
Fine-grained experts (–, –) outperform coarse experts () at iso-activated-compute. Adding a small number of always-on shared experts further improves quality by carrying common knowledge.
-
Expert parallelism shards experts across devices. Two all-to-all collectives per MoE layer; expensive but standard. MoE training composes with DP × TP × PP as a fourth parallelism axis.
-
MoE is a knowledge-capacity tool, not a compute-saver. It pays off most for knowledge-heavy tasks at sufficient scale, and pays cost in memory and communication.
The next two sessions (§8.7 and §8.8) leave the Transformer block entirely: state space models replace attention with a different sequence operator, and hybrid architectures interleave attention and SSM layers. Then we close the chapter with the encoder-decoder T5 appendix.
(a) Write the MoE forward equation in full. Identify the router, the top- selection, and the soft weights. Why are normalised over only , not all ?
(b) For Mixtral-8x7B (, dense expert with , ), compute the per-MoE-layer (i) total parameters in experts, (ii) activated parameters per token, (iii) router parameters. Confirm the reported B / B total / activated.
(c) State the load-balancing auxiliary loss of §3.1. Show that it is minimised when for all , giving .
(d) Fine-grained MoE: a baseline has expert combinations. Compute the number of combinations for DeepSeek-V3 (). Argue why this combinatorial capacity is the empirical reason fine-grained MoE outperforms coarse MoE.
(e) Trace the data flow through a single MoE layer under expert parallelism: the two all-to-alls, the per-device computation, the load-balance dependence. Where does throughput collapse if balance is poor?
(f) Argue why MoE helps knowledge-heavy benchmarks (MMLU) more than reasoning benchmarks (MATH). Cite the FFN-as-knowledge view of 6.8 §6.
References
- [1]
DeepSeek-AI (2024). "DeepSeek-V2: A strong, economical, and efficient mixture-of-experts language model". arXiv:2405.04434 — Multi-head Latent Attention (MLA) with decoupled RoPE (8.5); also primary source for fine-grained MoE (8.6).
↩ - [2]
DeepSeek-AI (2024). "DeepSeek-V3 technical report". arXiv:2412.19437 — V3 architecture: MLA + 256-expert fine-grained MoE + auxiliary-loss-free balancing (8.5, 8.6 §6).
↩ - [3]
Shazeer, N., Mirhoseini, A., Maziarz, K., et al. (2017). "Outrageously large neural networks: The sparsely-gated mixture-of-experts layer". arXiv:1701.06538 — Original deep MoE with top- gating (8.6 §2).
↩ - [4]
Lepikhin, D., Lee, H., Xu, Y., et al. (2020). "GShard: Scaling giant models with conditional computation and automatic sharding". arXiv:2006.16668 — Capacity factor, auxiliary load-balance loss, expert parallelism (8.6 §3, §6).
↩ - [5]
Fedus, W., Zoph, B., & Shazeer, N. (2021). "Switch Transformer: Scaling to trillion parameter models with simple and efficient sparsity". arXiv:2101.03961 — Top-1 routing; standard auxiliary loss formulation (8.6 §3.1).
↩ - [6]
Dai, D., Deng, C., Zhao, C., et al. (2024). "DeepSeek-MoE: Towards ultimate expert specialization in mixture-of-experts language models". arXiv:2401.06066 — Fine-grained experts + shared experts (8.6 §4).
↩ - [7]
Jiang, A. Q., Sablayrolles, A., Roux, A., et al. (2024). "Mixtral of experts". arXiv:2401.04088 — Mixtral-8x7B; production MoE LLM (8.6 §5).
↩ - [8]
Wang, L., Gao, H., Zhao, C., Sun, X., & Dai, D. (2024). "Auxiliary-loss-free load balancing strategy for mixture-of-experts" (DeepSeek-V3 routing). arXiv:2408.15664 — Control-loop-based balance without auxiliary loss (8.6 §6 Aside).
↩