SysDesignAtlas

Mixture of Experts

Decoupling parameter count from compute per token — how sparse routing works, why it complicates serving far more than dense models, and what expert parallelism costs.

CoreHard10 min readaimoesparsityrouting
Confidence

#The constraint MoE breaks

In an ordinary — dense — transformer, every one of the model's parameters is used for every token. So how much a model can know (its parameter count) and what each token costs to compute are locked together: ten times the parameters means ten times the work per token.

Mixture of Experts breaks that link. Replace each feed-forward block with many parallel copies of it — the experts — and add a small router that picks two of them for each token. The model can then hold enormous knowledge while any individual token only pays for the fraction it actually used.

   DENSE FFN                        MoE FFN

   x ──► [ one FFN, 4d wide ] ──►   x ──► [router] ──┬─► [expert 1]
                                                     ├─► [expert 2]  ← chosen
         every token uses                            ├─► [expert 3]
         every parameter                             ├─► ...
                                                     └─► [expert 64] ← chosen
                                                          ↓
                                          weighted sum of the 2 chosen

   64 experts, top-2 routing:
     parameters:  64× a dense FFN
     FLOPs/token:  2× a dense FFN
     → 32× more capacity for 2× the compute

That's the trade in one line: MoE converts a compute problem into a memory problem. For training, where you're compute-bound, this is an enormous win. For inference, where you're already memory-bound, it is much more ambiguous — and it's why MoE serving infrastructure looks so different from dense serving.

#Routing

A small learned router — usually one linear layer — scores each expert for each token and picks the top k.

   For each token's hidden state x:

     scores = softmax(x · W_router)      → one score per expert
     pick top-k (typically k = 1 or 2)
     output = Σ  score_i × expert_i(x)   over the chosen experts
              ─────────
              the router's scores scale each output, so
              training can learn to route better

   Routing is per TOKEN, per LAYER.
   A sequence of 1,000 tokens through 60 MoE layers makes
   60,000 independent routing decisions.

That last point is what makes MoE serving hard: routing is data-dependent and changes every token, so you cannot precompute which experts a request will need.

#Load balancing, the central training problem

Left alone, routing collapses. A slightly better expert gets picked more often, so it receives more training updates, gets better still, and gets picked even more. It ends up receiving nearly all traffic, while the others are rarely chosen, barely trained, and effectively wasted.

   WITHOUT balancing                 WITH an auxiliary loss

   expert 1  ████████████████ 71%    expert 1  ███ 13%
   expert 2  ███ 14%                 expert 2  ███ 12%
   expert 3  █ 6%                    expert 3  ███ 13%
   expert 4  █ 4%                    expert 4  ███ 12%
   ...       (dead)                  ...       (all ~12.5%)

   → you paid for 8 experts and trained 1.5

The standard fix: alongside the goal of predicting well, give training a second goal of spreading tokens evenly, and penalise imbalance. That extra objective is an auxiliary loss — auxiliary because it isn't what you actually want from the model, just a corrective pressure applied during training. It works, and it trades a little model quality for balance — a genuine cost, since you're optimising for something other than prediction.

DeepSeek-V3 (2024) showed a way around that cost: auxiliary-loss-free balancing. Each expert gets a bias added to its routing score, used only to choose experts, not to weight their outputs. After each training step the bias is nudged up for under-used experts and down for over-used ones. Balance comes from that feedback loop rather than from a second objective pulling against prediction quality.

Capacity factor is the other mechanism: cap how many tokens any single expert may accept per batch. Tokens beyond the cap are dropped — they skip the FFN entirely and pass through on the residual connection.

#Serving MoE: three genuine problems

#1. Memory is the whole game

All experts must be resident. A 400B-parameter MoE at FP8 needs 400 GB — five 80 GB GPUs minimum, before KV cache. Meanwhile a dense model with the same active parameter count needs 50 GB.

You are paying for eight times the memory to get the quality of a much larger model at the compute cost of a small one. Whether that's a good deal depends entirely on whether your GPUs are memory-limited or compute-limited — and in inference they're almost always memory-limited.

#2. Expert parallelism and all-to-all

Experts get distributed across GPUs. Each token must then travel to whichever GPU holds its chosen expert, and the results must come back.

   EXPERT PARALLELISM, 8 GPUs, 64 experts (8 each)

     each GPU embeds its own tokens
       ↓
     ALL-TO-ALL:  every GPU sends each token to the GPU
                  holding its routed expert
       ↓
     each GPU runs its experts on whatever it received
       ↓
     ALL-TO-ALL:  send results back to the originating GPU
       ↓
     continue to the next layer

   TWO all-to-all collectives PER MoE LAYER.
   60 MoE layers → 120 all-to-all operations per forward pass.

A collective is a communication step that every GPU in the group takes part in at once. All-to-all is the most demanding collective there is — every GPU sends something to every other GPU. Inside one server (a node), GPUs are joined by NVLink, which is fast enough that this is tolerable. Across servers the traffic goes over InfiniBand, a network several times slower, and the all-to-all becomes the dominant cost. This is why MoE serving strongly prefers to keep expert parallelism within a node, and why interconnect bandwidth matters even more for MoE than for dense models. See interconnect and collectives.

#3. Load imbalance at inference

Balancing losses shape the average over training. A single inference batch can still be badly skewed — if all requests are code, the code-ish experts get hammered while others idle.

   Batch of 64 tokens, 8 GPUs holding 8 experts each:

     GPU 0:  31 tokens routed here   ← everyone waits for this
     GPU 1:   3 tokens
     GPU 2:   1 token
     ...
     GPU 7:   0 tokens

   The step takes as long as GPU 0. The other seven are idle.
   Effective utilisation: ~25%.

Mitigations: larger batches (averaging smooths skew), replicating hot experts across GPUs, or dynamically rebalancing expert placement based on observed traffic. None of them fully solves it, and MoE serving efficiency is genuinely harder to achieve than dense.

#Design variations

ChoiceOptionsEffect
Experts per layer8 to 256+More = more capacity, worse balance, more all-to-all
Top-k1 or 2, occasionally morek=1 halves compute and communication; k=2 gives better quality and gradient flow
Shared expertOne expert every token always usesCaptures common patterns; lets routed experts specialise
Fine-grained expertsMany small experts rather than few largeMore routing combinations, better specialisation
MoE layer frequencyEvery layer, or every otherAlternating reduces communication substantially at modest quality cost
Routing granularityPer token, or per sequencePer-sequence routing is far cheaper to serve and less expressive

Recent frontier designs (DeepSeek-V3, Mixtral, and others) converged on many fine-grained experts plus a shared expert, which balances better and specialises more than a few large experts.

#Exercise

#Check yourself

What does mixture of experts decouple?Show answer
Capacity from compute per token: many expert FFNs with a router choosing top-k per token — e.g. 64 experts, top-2 gives 32× the capacity for 2× the compute.
What is MoE's central trade for serving?Show answer
It converts a compute problem into a memory problem: all experts must stay resident, because routing is decided per token, per layer, and can't be predicted.
Why does MoE routing collapse without help?Show answer
A slightly better expert gets picked more, trains more and wins nearly all traffic. An auxiliary balancing loss (or DeepSeek-V3's bias-based loss-free balancing) spreads tokens evenly.
What is token dropping, and why is it a correctness issue at inference?Show answer
A capacity factor caps tokens per expert; overflow tokens skip the FFN. Batch composition then changes outputs, so inference uses large capacity or drop-free routing — check this if temperature-0 outputs vary.
What does expert parallelism cost?Show answer
Two all-to-all collectives per MoE layer (send tokens to experts, return results) — 120 per pass for 60 layers — so keep it inside a node on NVLink.
Why does inference load imbalance hurt MoE?Show answer
One batch can route most tokens to one GPU's experts; the step waits for it while others idle. Mitigate with bigger batches, replicated hot experts and dynamic placement.
What design did recent frontier MoEs converge on?Show answer
Many fine-grained experts plus a shared expert every token uses — better balance and specialisation than a few large experts.
When is MoE the wrong choice?Show answer
Batch-1, memory-limited, single-GPU or latency-critical serving — you pay for all parameters and all-to-all latency. It wins at high batch on multi-GPU nodes with fast interconnect.

#What to carry forward

  • MoE decouples capacity from compute per token — many parameters, few active.
  • It converts a compute problem into a memory problem: all experts must be resident.
  • Routing is per token, per layer, and data-dependent — nothing can be precomputed.
  • Load balancing needs an auxiliary loss; capacity factors cause token dropping, which is a correctness issue at inference.
  • Two all-to-all collectives per MoE layer make interconnect bandwidth decisive.
  • MoE wins at high batch, multi-GPU, fast interconnect. At batch 1 on limited memory, a dense model of the same footprint is better.
00:00
/ 10m