Mixture-of-Experts (MoE)
One-line summary. A Mixture-of-Experts layer replaces one big feed-forward network with many smaller ones ("experts") plus a tiny router that, for each token, picks just a handful to run. You get a model with a huge total parameter count but a small active compute cost per token — capacity without the proportional FLOPs.
What it is (intuition first)
Imagine a hospital. A dense neural network is one doctor who has personally memorised all of medicine and sees every patient. It works, but that single brain is the bottleneck: to make it smarter you have to make that one doctor bigger, and every patient pays the full cost of all that knowledge.
A Mixture-of-Experts hospital instead has a triage nurse (the router or gating network) at the door and a roster of specialist doctors (the experts). When a patient (a token) arrives, the nurse glances at them and sends them to the 1–2 most relevant specialists, not all of them. The hospital as a whole "knows" an enormous amount — the sum of every specialist — but each patient only consumes the time of the two doctors they actually see.
That is the whole trick, and it has two consequences that drive everything else:
- Total knowledge (parameters) and per-patient cost (FLOPs) are decoupled. You can add more specialists to grow the hospital's expertise without making any single visit slower. This is conditional computation / sparse activation.
- The nurse's judgment is now critical and learned. If the nurse sends everyone to the cardiologist, the other specialists never learn and the cardiologist is overwhelmed. So a big chunk of MoE engineering is about making the router both smart (route to the right expert) and fair (spread the work) — this is load balancing.
Concretely in a transformer LLM: an MoE layer is a drop-in replacement for the standard feed-forward (FFN/MLP) sub-block. You keep attention exactly as it is (see [[attention-and-transformers]]), but swap the single FFN for N parallel FFN experts + a router. Each token, at each MoE layer, independently picks its top-k experts. Mixtral 8×7B uses N=8, k=2; DeepSeek-V3 uses 256 routed experts + 1 shared, k=8.
Crucial disambiguation. MoE is not the same as [[mixture-of-agents]]. MoE is a layer inside one model that routes tokens to sub-networks during a single forward pass — it's a low-level architecture choice. Mixture-of-Agents is a system-level pattern where several complete, separate LLMs answer a prompt and their full responses are aggregated (see also [[fusion-and-llm-councils]] and [[model-routing]]). MoE routes tokens to weights; MoA routes prompts to whole models. Different abstraction levels, easily confused because both have a "router" and "experts."
Why it matters
The dominant lever in modern LLMs is scaling (see [[scaling-laws]]): more parameters + more data ≈ lower loss. But for a dense model, doubling parameters roughly doubles the FLOPs of every forward and backward pass — training and inference cost scale with capacity. That gets economically brutal fast.
MoE breaks that coupling. The original sparsely-gated MoE paper (Shazeer et al., 2017) demonstrated >1000× increases in model capacity with only minor losses in computational efficiency, scaling an LSTM language/translation model to ~137B parameters Shazeer et al. 2017, [arXiv:1701.06538]. Switch Transformers (2021) carried this into transformers and reported up to 7× pre-training speed-ups at the same compute and the first trillion-parameter language models Fedus et al. 2021, [arXiv:2101.03961].
The headline numbers that make MoE matter today:
- Mixtral 8×7B: 47B total parameters, but only 13B active per token. It matches or beats Llama-2 70B and GPT-3.5 across most benchmarks while using ~5× fewer active params than Llama-2 70B at inference Jiang et al. 2024, [arXiv:2401.04088].
- DeepSeekMoE 16B reaches Llama-2-7B-level performance with ~40% of the computation Dai et al. 2024, [arXiv:2401.06066].
- DeepSeek-V3: 671B total parameters with only 37B activated per token — a frontier-class model trained remarkably cheaply precisely because the active compute is small DeepSeek-AI 2024, [arXiv:2412.19437].
So MoE is the mainstream answer to "how do I keep scaling parameters without scaling my training/serving FLOPs linearly." Most frontier open models in 2024–2025 (Mixtral, DeepSeek-V2/V3, Qwen-MoE, and others) are sparse MoEs.
How it works (the real mechanics)
The MoE layer
Replace the dense FFN with N expert FFNs {FFN_1, ..., FFN_N} and a gating network G. For an input token representation x, the layer output is a gated sum over experts:
y = Σ_{i=1..N} G(x)_i · FFN_i(x)The magic is that G(x) is sparse — almost all of its entries are exactly 0. If G(x)_i = 0, you never compute FFN_i(x) at all. Only the experts with nonzero gate values run. That is where the FLOPs savings come from.
The router / gating network
The simplest gate is a linear layer followed by softmax (the "softmax gate"):
G_σ(x) = Softmax(x · W_g)where W_g is a learned [d_model × N] matrix — tiny compared to the experts. But a plain softmax gives a dense distribution (every expert nonzero). To get sparsity you keep only the top-k entries and zero the rest.
Noisy Top-k gating (Shazeer et al. 2017) is the classic recipe, in three steps:
- Add learned noise (helps load balancing + exploration): ``
H(x)_i = (x · W_g)_i + StandardNormal() · Softplus((x · W_noise)_i)`` - Keep only the top-k logits, set the rest to −∞ (so softmax sends them to 0): ``
KeepTopK(v, k)_i = v_i if v_i in the top-k of v = −∞ otherwise`` - Softmax over the survivors: ``
G(x) = Softmax(KeepTopK(H(x), k))``
With k typically 1 or 2, each token runs only 1–2 of N experts. The router weights W_g (and W_noise) are trained jointly with everything else by ordinary backprop — gradients flow through the selected experts and through the gate values that weighted them. (Note the non-differentiable top-k selection is a known subtlety — gradients reach the chosen experts and their weights, not the unchosen ones.)
Top-k: how many experts per token?
- Top-1 (Switch Transformer). Fedus et al. argued you don't actually need ≥2 experts for the router to learn — routing to the single best expert works, halves routing/communication overhead, and simplifies the system. This made trillion-param training tractable [arXiv:2101.03961].
- Top-2 (GShard, Mixtral). Two experts give the gate a useful comparison signal and a bit more expressive capacity per token. Mixtral selects 2 of 8 per token per layer; "the selected experts can be different at each timestep," so over a sequence a token's path through the model is highly dynamic [arXiv:2401.04088].
- Top-k high (DeepSeek). With many fine-grained experts you activate more of them (e.g. 6–8) but each is smaller, so active FLOPs stay controlled (see fine-grained segmentation below).
Expert capacity and token dropping
Real systems run experts in parallel across devices, which needs fixed-size buffers. So each expert gets a hard capacity:
expert_capacity = (tokens_per_batch / num_experts) · capacity_factorIf more tokens route to an expert than its capacity, the overflow tokens are dropped — they skip the MoE computation and pass through via the residual connection unchanged. A capacity_factor of 1.0 means "exactly even share"; Switch Transformer found 1.0–1.25 works well [arXiv:2101.03961]. Higher factors waste compute/memory on padding; lower factors drop more tokens. Token dropping is a systems compromise, not a desired behavior — and it's a strong reason load balancing matters.
Load balancing: the central problem
Routers have a nasty failure mode: rich-get-richer collapse. If a few experts get slightly more traffic early, they train faster, become more attractive, attract even more tokens, and the rest of the experts atrophy. You end up paying for N experts but effectively using 2. To prevent this, MoE adds an auxiliary load-balancing loss to the main training loss.
The standard form (GShard / Switch / DeepSeekMoE) multiplies, per expert i, the fraction of tokens routed to it (f_i) by the average router probability mass it received (P_i), summed over experts and scaled by a small coefficient α:
L_aux = α · N · Σ_{i=1..N} f_i · P_iwith, over a batch of T tokens:
f_i = (fraction of tokens whose top-k selection includes expert i)
P_i = (1/T) · Σ_t s_{i,t} # mean softmax/router prob for expert iIntuition: f_i is the hard count (discrete, non-differentiable), P_i is the soft probability (differentiable — this is what gradients actually push on). The product f_i · P_i is minimized when load is uniform (f_i ≈ P_i ≈ 1/N), so minimizing this term nudges the router toward an even split without dictating which token goes where. α is kept small (e.g. ~0.01) — too large and it overrides the real task loss and hurts quality.
Router z-loss (ST-MoE) is a second, complementary auxiliary loss that penalizes large router logits (log Σ exp(logits)²), which stabilizes training by preventing exp() overflow/roundoff in the gate [HF MoE blog].
DeepSeekMoE: fine-grained experts + shared experts
DeepSeekMoE (2024) refined the recipe with two ideas that the later DeepSeek-V2/V3 frontier models adopted Dai et al. 2024, [arXiv:2401.06066].
Baseline top-K MoE (their Eq. 3–5), with residual:
h_t = u_t + Σ_{i=1..N} g_{i,t} · FFN_i(u_t)
g_{i,t} = s_{i,t} if s_{i,t} ∈ TopK({s_{j,t}}, K)
= 0 otherwise
s_{i,t} = Softmax_i( u_t^T · e_i ) # e_i = learned centroid for expert i(1) Fine-grained expert segmentation. Split each expert into m smaller ones (shrink the hidden dim by 1/m) so you have mN experts and activate mK of them. Active params/FLOPs are unchanged, but the number of ways to combine experts explodes combinatorially — far more specialized routing patterns become expressible. (Choosing 2 of 8 is 28 combinations; choosing, say, 8 of 64 is billions.) Eq. 6–8:
h_t = u_t + Σ_{i=1..mN} g_{i,t} · FFN_i(u_t) # TopK over mN, picking mK(2) Shared expert isolation. Reserve K_s experts that are always on for every token, to absorb common knowledge (grammar, general patterns) so the routed experts don't each have to redundantly relearn it. Only the remaining mN − K_s experts are routed (top mK − K_s). Eq. 9–11:
h_t = u_t
+ Σ_{i=1..K_s} FFN_i(u_t) # shared, always active
+ Σ_{i=K_s+1..mN} g_{i,t} · FFN_i(u_t) # routedDeepSeekMoE also adds a device-level balance loss on top of the expert-level one, because at scale experts are sharded across machines and you care about balancing device load (communication), not just per-expert counts. With experts grouped into D device-buckets E_1..E_D:
L_DevBal = α_2 · Σ_{i=1..D} f_i' · P_i'
where f_i' = mean over experts in E_i of f_j, P_i' = sum over experts in E_i of P_jResult: DeepSeekMoE 2B ≈ GShard 2.9B with 1.5× fewer expert params; 16B ≈ Llama-2-7B at ~40% compute [arXiv:2401.06066].
DeepSeek-V3: auxiliary-loss-free balancing
The aux loss has a cost: it's a regularizer fighting the task loss, so balancing and quality trade off. DeepSeek-V3 introduces an auxiliary-loss-free strategy DeepSeek-AI 2024, [arXiv:2412.19437]:
- The gate uses a sigmoid affinity
s_i = sigmoid(h^T e_i)(then normalized among the selected), not softmax over all. - Each expert gets a learnable bias `b_i` that is added only to the top-k *selection* score (
s_i + b_idecides who gets picked) but is excluded from the gating weight that actually weights the output. - During training, the system monitors each expert's load and nudges `b_i` up/down by a step `γ`: overloaded → decrease its bias (picked less), underloaded → increase it. No gradient, no extra loss term competing with the task — just a control loop on the selection threshold.
This balances load without the quality penalty of a large aux loss. DeepSeek-V3 ships 256 routed + 1 shared expert, top-8 routing, 671B total / 37B active, plus Multi-head Latent Attention (MLA) for KV-cache compression and a multi-token-prediction objective. (See [[deepseek-architecture]] for the full picture — MLA, MTP, FP8 training; this article focuses on the MoE half.)
Key ideas & tradeoffs
| Lever | What it buys | What it costs |
|---|---|---|
| More experts (`N`) | More total capacity / knowledge | More VRAM (all experts must be resident), harder balancing, more comms |
| Higher top-`k` | More expressive per-token mixing, better quality | More active FLOPs, more routing/comms overhead |
| Fine-grained experts (small + many) | Combinatorial routing flexibility, specialization | More routing decisions, scheduling complexity |
| Shared experts (always-on) | Less redundant relearning of common knowledge | A floor of always-paid compute |
| Higher capacity factor | Fewer dropped tokens | Wasted compute/memory on padding |
| Strong aux-loss `α` | Even expert utilization | Drags on task quality (→ V3's loss-free fix) |
Mental model of the core tension: MoE buys capacity with memory, not compute. You trade VRAM/parameter-storage and systems complexity (routing, all-to-all communication, balancing) for cheap active FLOPs. Whether that's a win depends entirely on whether you're FLOP-bound (MoE helps a lot) or memory-bound (MoE may not — see caveats).
Honest caveats & open questions
- Memory is the brutal catch. Even though only
kexperts run per token, all experts must be loaded in VRAM, because across a batch/sequence every expert gets used. Mixtral 8×7B activates 13B params but you must hold the full 47B in memory [HF MoE blog]. So MoE saves compute, not memory — it's great for throughput-bound serving with enough RAM, less so on memory-constrained single GPUs. Quantization ([[quantization]]) and offloading partly mitigate this. - Training instability. Routers are finicky: collapse, oscillation, and exp()-overflow are real. Noisy gating, aux losses, router z-loss, and (newer) loss-free bias control are all patches on a fundamentally non-differentiable, discrete-decision problem. Top-k selection isn't differentiable; we train around it.
- Fine-tuning & overfitting. "Sparse models are more prone to overfitting" and need more regularization; historically MoEs fine-tuned worse than dense models, though instruction-tuning was later shown to benefit MoEs more than dense models [HF MoE blog]. Still an active area.
- Reasoning vs. knowledge. Empirically MoEs tend to shine on knowledge-heavy tasks and lag dense models on some reasoning tasks at matched active-params [HF MoE blog]. The intuition (specialists store more facts; reasoning wants depth/shared computation) is plausible but not fully settled.
- What do experts actually specialize in? Less than the "hospital specialist" intuition suggests. Studies often find routing correlates with surface features (token IDs, syntax) more than clean semantic domains. "Expert" is an aspirational name, not a guarantee of interpretable specialization.
- Token dropping silently degrades inputs. Under imbalance, dropped tokens skip the FFN entirely — a quiet quality leak that's easy to miss.
- Systems complexity is first-class. All-to-all communication, expert-parallel sharding, capacity buffers, and balancing turn MoE into as much a distributed-systems problem as an ML one. The reported speed-ups assume you've solved that plumbing.
How it connects to OpenAlice
Be precise here — don't overclaim a tie that isn't there.
- Default provider is an MoE model. Alice's default LLM stack runs on
gpt-5.x/ Codex-family providers, and the open frontier she's benchmarked against (DeepSeek-V3) is a sparse MoE. Understanding active vs. total params directly explains why DeepSeek-class models are cheap to serve relative to their size — relevant whenever the lab reasons about provider cost/latency tradeoffs ([[model-routing]]). - MoE ≠ Alice's routing. Alice's own multi-model orchestration (council/fusion patterns, the cost-ladder research rig, dispatching Opus/Sonnet/Codex sub-agents) is Mixture-of-Agents / model-routing at the *system* level, not MoE at the weight level. The conceptual cousins are [[mixture-of-agents]], [[fusion-and-llm-councils]], and [[model-routing]] — keep the distinction crisp in any internal doc: MoE picks neurons, Alice's orchestrator picks whole models.
- The balancing intuition transfers. The load-balancing problem (don't let one expert/model hog all the traffic and starve the rest) is structurally the same challenge Alice's orchestrator faces when distributing sub-agent work across providers under a shared quota — the math differs, the failure mode (rich-get-richer collapse) rhymes.
- No claim that OpenAlice trains its own MoE. As of this writing there's no honest tie to in-house MoE training in the codebase; the relevance is (1) understanding the models we consume and (2) the conceptual mirror to agent-level routing.
See also
- [[deepseek-architecture]] — the full DeepSeek-V2/V3 stack (MLA, MTP, FP8) that wraps this MoE; the natural next read.
- [[mixture-of-agents]] — the system-level cousin people confuse with MoE; routes whole models, not tokens.
- [[fusion-and-llm-councils]] · [[model-routing]] — aggregating / selecting across complete LLMs.
- [[scaling-laws]] — why decoupling params from FLOPs is such a big deal.
- [[attention-and-transformers]] — the block MoE leaves untouched while it replaces the FFN.
- [[quantization]] · [[flash-attention]] — systems-side levers that make giant (often MoE) models servable.
- [[llm-from-scratch]] · [[microgpt-build-an-llm-from-scratch]] — build the dense FFN that MoE generalizes.
Sources (fetched & verified 2026-06-16)
- Shazeer, Mirhoseini, Maziarz, Davis, Le, Hinton, Dean (2017). Outrageously Large Neural Networks: The Sparsely-Gated Mixture-of-Experts Layer. arXiv:1701.06538
- Fedus, Zoph, Shazeer (2021). Switch Transformers: Scaling to Trillion Parameter Models with Simple and Efficient Sparsity. arXiv:2101.03961
- Jiang et al. (2024). Mixtral of Experts. arXiv:2401.04088
- Dai et al. (2024). DeepSeekMoE: Towards Ultimate Expert Specialization in Mixture-of-Experts Language Models. arXiv:2401.06066
- DeepSeek-AI (2024). DeepSeek-V3 Technical Report. arXiv:2412.19437
- Hugging Face (2023). Mixture of Experts Explained. huggingface.co/blog/moe