AI / ML Theory
Chapter 8 · 28 min

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 ∼16d2\sim 16 d^2 per layer, the parameter access per token is ∼8d2\sim 8 d^2 — the whole FFN block is read from memory and applied densely.

Mixture of Experts (MoE) breaks this coupling. Replace the single FFN with EE parallel FFNs ("experts") plus a small router that, per token, selects the top-kk most relevant experts. Each token visits only kk of EE experts. Compute per token: ∼16d2⋅(k/E′)\sim 16 d^2 \cdot (k / E'), where E′E' is an effective expert count discussed below. Total parameter count: ∼16d2⋅E\sim 16 d^2 \cdot E (with a constant set by how wide each expert is). The decoupling: parameters scale with EE; compute scales with kk.

Frontier models in 2024–2026 routinely have E∈[8,256]E \in [8, 256] experts with k∈[1,8]k \in [1, 8] activated per token. Mixtral-8x7B uses E=8,k=2E = 8, k = 2 — 4747B total parameters, ∼13\sim 13B activated per token. DeepSeek-V3 uses E=256E = 256 fine-grained experts + 1 shared expert with k=8k = 8 activated — 671671B total, 3737B activated. The model has GPT-4-scale capacity at ∼1/20\sim 1/20 of GPT-4's inference cost per token.

This session derives the construction (router, top-kk, 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 EE identical-shape experts {fe}e=1E\{f_e\}_{e=1}^{E}, each being a standard SwiGLU FFN with its own parameters:

fe(x)=((xW1(e))⊙Swish(xW2(e)))W3(e)∈Rd.f_e(x) = \bigl( (x W_1^{(e)}) \odot \text{Swish}(x W_2^{(e)}) \bigr) W_3^{(e)} \in \mathbb{R}^{d}.

A small router matrix Wg∈Rd×EW_g \in \mathbb{R}^{d \times E} produces per-token gate logits:

router(x)=x Wg∈RE.\text{router}(x) = x\, W_g \in \mathbb{R}^{E}.

The router selects the top-kk experts per token by largest gate logit. Let Tk(x)⊂{1,…,E}\mathcal{T}_k(x) \subset \{1, \dots, E\} be the set of top-kk indices for token xx, and define normalised soft weights over those:

ge(x)={exp⁡(router(x)e)∑e′∈Tk(x)exp⁡(router(x)e′),e∈Tk(x),0,otherwise.g_e(x) = \begin{cases} \dfrac{\exp(\text{router}(x)_e)}{\sum_{e' \in \mathcal{T}_k(x)} \exp(\text{router}(x)_{e'})}, & e \in \mathcal{T}_k(x), \\ 0, & \text{otherwise}. \end{cases}

The MoE layer output is the convex combination of the kk activated experts' outputs:

 MoE(x)=∑e∈Tk(x)ge(x) fe(x). \boxed{\ \text{MoE}(x) = \sum_{e \in \mathcal{T}_k(x)} g_e(x)\, f_e(x). \ }

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: EE logits via xWgx W_g, then a top-kk selection (sorting cost O(Elog⁡k)O(E \log k), negligible). Compute: 2dE2 d E FLOPs.
  • kk expert evaluations: 16d216 d^2 per expert (using dff=(8/3)dd_\text{ff} = (8/3) d as in §8.4 §3), times kk activated experts. Compute: 16d2k16 d^2 k FLOPs.
  • Combine: kk scalar multiplications and additions on dd-vectors, O(kd)O(k d), negligible.

Total: ≈16d2k\approx 16 d^2 k FLOPs per token per layer, plus 2dE2 d E for the router. For E≫kE \gg k, the FFN compute per token is the same as a dense FFN of width dff=(8/3)kdd_\text{ff} = (8/3) k d. We say the model has kk active experts.

Parameter count for the MoE FFN sub-layer:

#θMoE-FFN=E⋅(3d⋅dff)+dE=E⋅8d2+dE,\#\theta_\text{MoE-FFN} = E \cdot (3 d \cdot d_\text{ff}) + d E = E \cdot 8 d^2 + d E,

where the first term is the experts and the second term is the router. For E=64E = 64, d=4096d = 4096: experts use E⋅8d2≈8.6⋅109E \cdot 8 d^2 \approx 8.6 \cdot 10^9 parameters per MoE layer, vs 8d2≈1.3⋅1088 d^2 \approx 1.3 \cdot 10^8 for a dense FFN. Total parameters scale linearly with EE.

So the decoupling: Ntotal∝EN_\text{total} \propto E, Nactive per token∝kN_\text{active per token} \propto k. At E=64,k=2E = 64, k = 2, the model has 32×32\times more parameters than a dense baseline but spends only 2×2\times 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:

  1. Capacity is wasted. The unused experts' parameters contribute nothing; effective parameter count drops from EE to the number of frequently-used experts.
  2. Expert-parallel hardware utilisation drops. Experts are typically sharded one-per-device (§6 below). If 80%80\% of tokens go to 20%20\% of experts, 80%80\% of the devices are idle for 80%80\% 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 fef_e be the fraction of tokens (within a training batch) that route to expert ee and let PeP_e be the router probability mass assigned to expert ee, averaged over the batch:

fe=1Tbatch∑i=1Tbatch1[e∈Tk(xi)],Pe=1Tbatch∑i=1Tbatchsoftmax(xiWg)e.f_e = \frac{1}{T_\text{batch}} \sum_{i=1}^{T_\text{batch}} \mathbb{1}[e \in \mathcal{T}_k(x_i)], \qquad P_e = \frac{1}{T_\text{batch}} \sum_{i=1}^{T_\text{batch}} \text{softmax}(x_i W_g)_e.

The auxiliary loss:

 Laux=α⋅E⋅∑e=1Efe⋅Pe. \boxed{\ \mathcal{L}_\text{aux} = \alpha \cdot E \cdot \sum_{e=1}^{E} f_e \cdot P_e. \ }

It is added to the LM cross-entropy with weight α∈[10−3,10−2]\alpha \in [10^{-3}, 10^{-2}]. The product fe⋅Pef_e \cdot P_e is the empirical "activity" of expert ee; the sum is minimised when both fef_e and PeP_e are uniform =1/E= 1/E, in which case ∑efePe=1/E\sum_e f_e P_e = 1/E and Laux=α\mathcal{L}_\text{aux} = \alpha. Any deviation from uniform routing makes Laux\mathcal{L}_\text{aux} larger.

The loss is differentiable in PeP_e (the router's softmax output) but not in fef_e (which is the hard top-kk assignment). The trick: Laux\mathcal{L}_\text{aux} drives the router's continuous probabilities PeP_e towards uniform, which empirically dragsthe hard top-kk assignments fef_e 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

capacity=Tbatch⋅kE⋅c,\text{capacity} = \frac{T_\text{batch} \cdot k}{E} \cdot c,

where c≥1c \geq 1 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 c=1.0c = 1.0 means strict balance; c=1.25c = 1.25–2.02.0 leaves slack. Larger cc 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 E=64E = 64, k=1k = 1 (a single expert per token), with each expert being a full-width SwiGLU FFN. Mixtral-8x7B used E=8E = 8, k=2k = 2, 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 dffd_\text{ff}) but have many more of them, with kk correspondingly larger. The activated parameter count is the same; what changes is the combinatorial capacity of the gating.

Concretely: a baseline of E=8,k=2E = 8, k = 2 has (82)=28\binom{8}{2} = 28 possible 2-expert combinations per token. Splitting each expert into 44 pieces gives E=32,k=8E = 32, k = 8 — same activated compute, but now (328)=10,518,300\binom{32}{8} = 10{,}518{,}300 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 E=160+2E = 160 + 2 shared experts with k=6+2k = 6 + 2; DeepSeek-V3 uses E=256+1E = 256 + 1 shared with k=8+1k = 8 + 1. 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-kk routed experts. The MoE output becomes

MoE(x)=fshared(x)+∑e∈Tk(x)ge(x) fe(x).\text{MoE}(x) = f_\text{shared}(x) + \sum_{e \in \mathcal{T}_k(x)} g_e(x)\, f_e(x).

The shared expert is the "common knowledge" path; the routed experts specialise in their respective niches. Empirically, having 11–22 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 θ\theta in the model, including all experts.
  • Activated parameters: the count of parameters touched per token per forward pass — including attention, normalisation, embedding, all of kk activated experts, and the router.

For Mixtral-8x7B: 4747B total, ∼13\sim 13B activated. For DeepSeek-V3: 671671B total, 3737B activated. For DBRX (Mosaic, 2024): 132132B total, 3636B 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:

  1. MoE shifts the parameter-to-compute ratio. A dense 1313B model and a MoE model with 1313B 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.

  2. MoE requires more memory. The full EE 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 E=256E = 256 experts, how do you train and serve the model? You cannot keep all 256256 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:

  1. Every device computes the router's top-kk for its local tokens.
  2. An all-to-all collective communication redistributes each token to the device(s) hosting its top-kk experts.
  3. Each device runs its experts on the tokens it received.
  4. A second all-to-all sends the expert outputs back to each token's origin device.
  5. Each device combines its tokens' expert outputs as ∑egefe\sum_e g_e f_e.

The two all-to-alls per MoE layer add communication cost — typically ∼10\sim 10–20%20\% 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 ×\times TP ×\times PP ×\times EP, with EP typically =E/(experts per device)= E / (\text{experts per device}).

[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-1010B total parameters, the FFN capacity gain is small relative to the routing overhead and load-balancing constraints.
  • Memory-constrained single-GPU inference. All EE 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

  1. MoE replaces the FFN with EE experts and a top-kk router. Activated parameters ∝k\propto k, total parameters ∝E\propto E — the two are decoupled.

  2. 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 Laux=αE∑efePe\mathcal{L}_\text{aux} = \alpha E \sum_e f_e P_e.

  3. Fine-grained experts (E=64E = 64–256256, k=6k = 6–88) outperform coarse experts (E=8,k=2E = 8, k = 2) at iso-activated-compute. Adding a small number of always-on shared experts further improves quality by carrying common knowledge.

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

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

Self-test

(a) Write the MoE forward equation MoE(x)=∑e∈Tkge(x)fe(x)\text{MoE}(x) = \sum_{e \in \mathcal{T}_k} g_e(x) f_e(x) in full. Identify the router, the top-kk selection, and the soft weights. Why are geg_e normalised over only Tk\mathcal{T}_k, not all EE?

(b) For Mixtral-8x7B (E=8,k=2E = 8, k = 2, dense expert with d=4096d = 4096, dff=(8/3)dd_\text{ff} = (8/3) d), compute the per-MoE-layer (i) total parameters in experts, (ii) activated parameters per token, (iii) router parameters. Confirm the reported 4747B / 1313B total / activated.

(c) State the load-balancing auxiliary loss of §3.1. Show that it is minimised when fe=Pe=1/Ef_e = P_e = 1/E for all ee, giving Laux=α\mathcal{L}_\text{aux} = \alpha.

(d) Fine-grained MoE: a baseline E=8,k=2E = 8, k = 2 has (82)=28\binom{8}{2} = 28 expert combinations. Compute the number of combinations for DeepSeek-V3 (E=256,k=8E = 256, k = 8). 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. [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. [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. [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-kk gating (8.6 §2).

    ↩
  4. [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. [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. [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. [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. [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).

    ↩