Skip to content
mentoor

Topic 4 of 6 · 6 min read

Routing and load balancing in MoE

Goal: Compute total and active parameters from a mixture-of-experts config and explain what happens when routing collapses.

A mixture-of-experts (MoE) model replaces each feed-forward network with many smaller expert networks and a router that sends every token to only its top-k experts, so total parameters are large while the work per token stays small. Mixtral, DeepSeek and Qwen's MoE models all use this idea.

Check yourself · 1 of 3

An MoE model has 8 experts per layer and sends each token to 2. What decides the GPU memory needed to load it?

Choose one answer

Why it matters

Two numbers describe an MoE model and they answer different questions. Total parameters decide memory: every expert must be loaded. Active parameters decide compute per token. A model that is 47B total and 13B active needs the memory of a 47B model and runs at roughly the per-token cost of a 13B one.

How routing works

  1. A small linear layer (the router) scores each expert for the token.
  2. The top-k scores pick the experts; their scores are normalised into weights (softmax).
  3. The token goes through those k experts only, and the outputs are summed with the weights.
logits = router(x)                         # (tokens, n_experts)
w, idx = torch.topk(logits.softmax(-1), k=2)   # top-2
w = w / w.sum(-1, keepdim=True)
y = sum(w[:, j, None] * experts[idx[:, j]](x) for j in range(2))

Parameter arithmetic

Mixtral-style: 32 layers, hidden 4096, feed-forward 14336, 8 experts, top-2. One SwiGLU expert has 3 * 4096 * 14336 = 176.2M parameters. Experts total 32 * 8 * 176.2M = 45.1B; attention adds 1.34B and embeddings about 0.26B, giving 46.7B. Active uses 2 experts: 32 * 2 * 176.2M = 11.3B plus the shared 1.6B is 12.9B. These match the Mixtral paper's 46.7B and 12.9B.

Load balancing and collapse

Left alone, the router favours a few experts: they get more training, score better, and get picked more. The rest stay weak. This is routing collapse. Training adds an auxiliary balancing loss that penalises uneven expert use (some recent models use bias adjustments instead). At serving time, uneven routing also hurts: one expert's GPU becomes the bottleneck.

Before and after

Weak: "47B total, so it needs the compute of a 47B model." No; compute follows active parameters. Better: "It needs memory for 47B and compute of about 13B per token. Check whether the memory fits first."

What goes wrong

  • Sizing GPUs from active parameters; the idle experts still occupy memory.
  • Ignoring that batch routing is uneven, so latency varies by prompt.
  • Fine-tuning without a balancing term, which can collapse routing.

Examples

Total versus active

Eight experts at 176.2M each across 32 layers is 45.1B in experts. With top-2, only 11.3B of that runs per token. A 4-bit copy of the whole model still needs about 23 GB for weights alone, even though compute looks like a 13B model.

What collapse looks like

A training log shows expert 3 receiving 71 % of tokens in layer 12 while four experts receive under 2 % each. The model behaves like a much smaller dense model and wastes the memory of the unused experts. The fix is a stronger balancing loss or bias adjustment, not more training steps.

Key points

  • Total parameters set memory because every expert must be loaded; active parameters set compute per token.
  • A router scores experts and sends each token to its top-k, mixing their outputs by normalised weights.
  • Active parameters equal shared parameters plus k experts per layer, not all experts.
  • Without a balancing mechanism the router favours a few experts, which is routing collapse.
  • Uneven routing also creates serving bottlenecks when experts live on different GPUs.

Common mistakes

  • Sizing GPU memory from active parameters.
  • Treating all experts as equally trained without checking token counts per expert.
  • Counting only one feed-forward per layer when computing total parameters.
  • Dropping the balancing loss during fine-tuning.

Check yourself · 2 of 3

One expert has 100M parameters, there are 16 experts per layer, 20 layers, top-2 routing, and 500M shared parameters. How many active parameters per token?

Choose one answer

Check yourself · 3 of 3

Training logs show one expert receiving most tokens in a layer. What is this called, and what is the usual remedy?

Choose one answer

Practice items, not counted towards your mastery.

Start here

More for this topic