Skip to content
mlmentorship

Mixture of Experts (MoE)

Replace one large feed-forward block with N smaller experts and a router that activates only k of them per token. Trades parameter count for compute.

Published · 5 min read ·Core ·Intermediate

Visual quick review

Visual first · depth when needed

Trace one token through top-2 routing and distinguish the two experts that perform compute from all four expert parameter sets that remain resident in memory.

Preparing the visual…

Summary

A Mixture-of-Experts layer replaces a single dense feed-forward network with parallel “expert” FFNs and a router that sends each token to the top- experts (typically or ). Total parameters scale with ; per-token compute scales with .

The defining tradeoff: a -of- MoE has roughly the same per-token FLOPs as a dense model with of the parameters, but the capacity of all experts. Mixtral 8×7B (Jiang et al., 2023) has 47B total parameters and uses ~13B per token. Quality close to a 70B dense model at ~5× lower inference compute.

MoE is the dominant strategy for scaling parameter count beyond what dense training and inference can afford. GPT-4, Mixtral, DeepSeek-V3, Grok 1, and many other 2024-2026 frontier models are MoE.

The mechanism

For each transformer block, replace the single FFN with:

  1. Router: a small linear layer . For each token, compute logits , take top- experts, and softmax-normalize over those .
  2. Experts: independent FFN blocks , each the same shape as the dense FFN it replaces.
  3. Combine: output is where is the router weight.

Attention layers are typically not MoE (shared across all tokens).

Learning objective

How can one token use only two experts while the model stores all four?

One token routed to experts one and three Token x enters a router that scores four experts. After top-2 selection and softmax, solid paths send x to expert one with weight 0.65 and expert three with weight 0.35. Dashed paths to experts two and four are marked skipped with gate weight zero. 1 · ROUTE THIS TOKEN x token router score → top 2 softmax selected E1 E2 E3 E4 0.65 0 · skip 0.35 0 · skip SOLID = EXECUTE · DASHED = BYPASS
Two active experts but four resident parameter sets All four expert parameter blocks remain resident in memory. Expert one and expert three are labeled active and feed a weighted sum. Expert two and expert four are labeled resident but skipped. The accounting states that compute uses two of four experts while memory stores four of four. 2 · ACCOUNT FOR COMPUTE AND MEMORY E1 active E2 skipped E3 active E4 skipped weighted sum 0.65 E1(x) + 0.35 E3(x) compute 2 of 4 execute memory 4 of 4 resident SPARSE FLOPs · DENSE PARAMETER STORAGE
Read it this way: top-2 routing makes only E1 and E3 run for this token, and their normalized weights combine the two outputs. E2 and E4 do no work for this token, but their parameters still occupy memory because another token may select them.

Load balancing

The router will collapse to a few favorite experts unless penalized. Standard fix: an auxiliary load-balancing loss that penalizes uneven expert usage within a batch. Alternatives include expert-choice routing (each expert picks its top tokens, Zhou et al., 2022) and noise injection.

If experts go unused for many steps their parameters drift; a few production systems “reset” dead experts.

Capacity and expert parallelism

With experts, an intuitive serving setup is expert parallelism: each GPU holds one expert. Tokens are routed via all-to-all communication. This works but introduces:

  • Communication overhead: all-to-all is bandwidth-bound and stalls when the routing is imbalanced.
  • Capacity factor: each expert has a max number of tokens it can process per batch; overflow tokens are dropped (or sent to a fallback path). Capacity factor of 1.25 is common.

Tradeoffs vs. dense

  • Memory: MoE needs FFN parameters in HBM even though only are used per token. Inference VRAM is dominated by all experts being loaded, not just active ones.
  • Throughput: MoE wins per-FLOP. Throughput per VRAM-byte is worse than dense.
  • Quality at fixed FLOPs: MoE generally beats dense at matched per-token FLOPs.
  • Fine-tuning: MoE is harder to fine-tune cleanly; routing can drift, and small datasets exacerbate load imbalance.

Common pitfalls

  • Quoting “total parameters” as if they were active. A 47B MoE with 13B active is a 13B-FLOPs model with 47B-VRAM cost.
  • Ignoring the routing loss. Without it, training collapses to using a few experts.
  • Assuming MoE always wins. At small scale or with limited compute for routing experimentation, dense is simpler and competitive.