Mixture of Experts (MoE)
Replace the dense feed-forward block with many expert FFNs and a router that sends each token to only the top-k, so total parameters (capacity) grow while compute per token stays at the active subset.
Analogy: A firm with 64 consultants and a dispatcher who assigns each task to two of them: the firm knows a lot, each task costs two consultants' time, but all 64 still need a desk.
How it works
- Size splits in two: total parameters you must store, and active parameters each token computes.
- Mixtral 8x7B holds ~47B, not 56B, because only the FFNs are replicated; ~13B are active.
- Memory follows the total: every expert stays loaded, so Mixtral needs ~94 GB in bf16.
- Load balancing needs an auxiliary loss or bias term, or the router collapses onto a few favourite experts.
- FLOP savings pay off at large, compute-bound batches; at low traffic you pay for idle memory.
Check yourself
Answer to mark it learned; your answers aren't scored.
An internal assistant serves ~20 users with light, bursty traffic. A teammate wants to self-host DeepSeek-V3 (671B total, 37B active) instead of a dense 70B, because "it computes only 37B per token, so it's cheaper." Your answer?
Go deeper
Full explanation
A Mixture of Experts model swaps the dense feed-forward (FFN) block in some or all Transformer architecture layers for many FFNs (experts) plus a router. For each token, at each MoE layer, the router scores the experts and sends the token to the top-k (often 1–8); their outputs are mixed using the router weights. Attention stays shared.
That splits model size into two numbers:
- Total parameters: what you must store. Capacity and knowledge.
- Active parameters: what each token computes. FLOPs per token.
Mixtral 8x7B has ~47B total but ~13B active (top-2 of 8; not 56B, because only the FFNs are replicated). DeepSeek-V3 has 671B total and 37B active (8 of 256 routed experts plus 1 shared). You get large-model quality at small-model compute per token.
The bill arrives elsewhere:
- Memory: every expert must be loaded; Mixtral needs ~94 GB in bf16 though it computes like a 13B.
- Load balancing: the router collapses onto a few favourite experts unless an auxiliary loss or bias term spreads tokens.
- Expert parallelism: experts live on different GPUs, so each MoE layer does an all-to-all token exchange.
- Serving: a batch's tokens together touch most experts, so decode still streams most weights from memory; the FLOP savings pay off at large, compute-bound batches.
Code A top-2 MoE layer in NumPy
import numpy as np
d_model, n_experts, top_k = 64, 8, 2
rng = np.random.default_rng(0)
W_router = rng.normal(size=(d_model, n_experts)) * 0.02
experts = [ # each expert is an ordinary FFN: d -> 4d -> d
(rng.normal(size=(d_model, 4 * d_model)) * 0.02,
rng.normal(size=(4 * d_model, d_model)) * 0.02)
for _ in range(n_experts)
]
def moe_layer(x): # x: (tokens, d_model)
logits = x @ W_router # (tokens, n_experts)
chosen = np.argsort(logits, axis=1)[:, -top_k:] # top-k experts per token
out = np.zeros_like(x)
for t, ids in enumerate(chosen):
w = np.exp(logits[t, ids] - logits[t, ids].max())
w /= w.sum() # softmax over the chosen k only
for weight, e in zip(w, ids):
W1, W2 = experts[e]
out[t] += weight * (np.maximum(x[t] @ W1, 0) @ W2) # only k FFNs run
return out, chosen
y, chosen = moe_layer(rng.normal(size=(16, d_model)))
print(chosen[:3]) # first 3 tokens, 2 of 8 experts each
print(np.bincount(chosen.ravel(), minlength=n_experts)) # load per expert
# Uneven load is the norm: training adds a balancing loss or bias to fix itReal example
Compare two self-hosted options. A dense Llama-3-70B: ~140 GB of bf16 weights and ~140 GFLOPs per token. DeepSeek-V3: 671B total parameters, ~671 GB even in its native FP8, so a full 8×H200 node, but only 37B active, ~74 GFLOPs per token. Per token the MoE does about half the math of the 70B while holding ~10× the parameters. At high traffic that saving becomes throughput; at low traffic you pay for a node of memory that sits mostly idle. Middle ground: gpt-oss-120b (~117B total, ~5.1B active) fits on one 80 GB GPU.
Trade-offs +3 · −3
+ You gain
- More capacity per FLOP: quality of a much larger dense model at the compute of a small one.
- Cheaper training to a given quality: Switch Transformer reported up to 7× faster pretraining at equal FLOPs per token.
- High throughput at large batch sizes; most frontier open models use it as of 2025 (DeepSeek-V3, Qwen3, Llama 4, gpt-oss).
− You pay
- Memory is sized by total parameters: you host 671B to compute 37B.
- Harder to train: load balancing, router instability, dropped tokens at expert capacity limits.
- Harder to serve: expert parallelism, all-to-all traffic, hot experts that create tail latency.
Use it when
High-throughput serving where you can fill large batches and afford the GPU memory: API providers, shared internal platforms. When comparing models, active params predict compute cost; total params predict memory.
Avoid it when
Memory-bound or edge deployments, low-traffic single-tenant services, or teams without multi-GPU serving experience: a dense model that fits on one GPU is simpler and cheaper to run.
Numbers to know 5
- Mixtral 8x7B: ~47B total, ~13B active (top-2 of 8 experts).
- DeepSeek-V3: 671B total, 37B active (8 of 256 routed experts + 1 shared).
- Qwen3-235B-A22B: 235B total, 22B active.
- gpt-oss-120b: ~117B total, ~5.1B active; fits one 80 GB GPU with MXFP4 weights.
- Compute per token ≈ 2 × active params; weight memory ≈ total params × bytes per param.
Common pitfalls 4
- Sizing GPUs by active parameters; memory is set by total parameters.
- Treating experts as topic specialists: routing is per token and per layer, and Mixtral's analysis found mostly syntactic, not domain, patterns.
- Benchmarking at batch 1 and extrapolating: MoE speed depends heavily on batch size and expert placement.
- Using total parameters in 6ND or FLOP-per-token estimates; use active parameters.
Interview questions 2
Answer out loud first, then reveal.
Why is Mixtral 8x7B ~47B parameters and not 56B, and what does it cost to serve?Show answer
Only the feed-forward blocks are replicated into 8 experts; attention, embeddings and norms are shared, so the total is ~47B. Each token goes to 2 experts, so it computes ~13B parameters' worth, roughly the FLOPs of a 13B dense model. But all 47B must sit in memory: ~94 GB in bf16, so two 80 GB GPUs or quantization. The trade-off I'd state: MoE buys quality per FLOP and pays in memory and serving complexity.
When would you choose an MoE over a dense model for self-hosting?Show answer
When traffic fills large batches and I have the GPU memory, because then fewer FLOPs per token turn into real throughput. For low-traffic or edge deployments I'd pick dense: memory is my binding constraint, and MoE makes me store every parameter while each token uses a fraction. I'd also check the serving stack, since vLLM and SGLang support expert parallelism but load imbalance across GPUs shows up as tail latency, and I'd benchmark at my real batch sizes, never at batch 1.