DL0109 Mixture-of-Experts MoE Routing

Explain mixture-of-experts (MoE) and compare Sparse Mixture-of-Experts (MoE) routing strategies, as used in models like Mixtral and DeepSeek-V3.

Answer

A sparse mixture-of-experts layer replaces one feed-forward block with N parallel feed-forward blocks (the experts) plus a tiny linear router, and sends each token to only k \ll N of them. This is conditional computation: parameter count grows with N while the FLOPs per token grow only with k, so Mixtral 8x7B holds 46.7B parameters but activates about 12.9B per token, and DeepSeek-V3 holds 671B while activating 37B. The interesting engineering is not the experts, which are ordinary MLPs, but the routing strategy, because a learned router is free to collapse onto a few favorite experts and then most of the capacity you paid for is never trained. The dominant strategy is token-choice top-k: every token picks its own k experts, each expert has a fixed buffer set by a capacity factor, and tokens that arrive at a full expert are dropped through the residual connection. The main alternatives invert or remove that choice: expert-choice routing lets each expert select its top tokens (perfect load balance, but some tokens get no expert), hash layers assign tokens deterministically with no learned router at all, and BASE layers solve a global linear assignment per batch. The trade-off axis is always the same, namely how much balance you buy and what you pay for it in dropped tokens, extra loss terms, batch dependence, and all-to-all communication.

(1) Parameters Decouple From FLOPs: with N=8, k=2 a token touches 25% of the expert weights, so quality scales with total parameters while latency scales with active parameters.
(2) The Router Is One Matrix: a single d \times N projection followed by top-k selection and a renormalized softmax over the selected logits, which is why gradients reach the router only through the chosen experts.
(3) Load Balancing Is The Central Problem: Switch-style training adds an auxiliary balance loss (typically \alpha = 0.01) and a router z-loss to keep logits small and the assignment spread out.
(4) Capacity Factor Controls Token Dropping: a buffer of c \cdot T \cdot k / N slots per expert means c near 1.0 is cheap but discards tokens whenever the router skews.
(5) Routing Families Differ In Who Chooses: token-choice, expert-choice, deterministic hashing, and global assignment sit on a spectrum from fully learned and imbalanced to fully balanced and inflexible.
(6) Serving Cost Is Memory, Not Compute: every expert must be resident in HBM even though each token uses k of them, and expert parallelism adds two all-to-all collectives per MoE layer.

Two-panel bipartite diagram: on the left, token-choice top-2 routing where four tokens each select two experts, expert one is oversubscribed and two edges are dropped at capacity; on the right, expert-choice routing where each expert selects its top two tokens, giving perfect expert load but leaving one token with no expert

Figure 1: The two families differ in the direction of selection. Token-choice top-k guarantees every token gets k experts but not that experts get equal load, so overflow is dropped at capacity; expert-choice guarantees equal expert load but not that every token is served.

Token-choice top-k stays the production default for autoregressive language models for one structural reason: it is a per-token function, so the routing decision for token t does not depend on any other token in the batch. Expert-choice and BASE layers both rank or match tokens against each other, which makes the forward pass batch-dependent, breaks the causal guarantee during teacher-forced training, and cannot be reproduced at decode time when the batch is a single token. Hash layers show how much of MoE’s benefit comes from capacity rather than from clever routing, since a fixed hash of the token ID is perfectly balanced by construction and still recovers a large part of the gain, but it can never learn semantic specialization. In practice the fix for token-choice imbalance is not to abandon it: DeepSeek-V3 keeps token-choice top-8 over 256 fine-grained experts, adds one always-on shared expert to absorb common knowledge, restricts each token to experts on at most 4 nodes to bound communication, and replaces the auxiliary loss with a per-expert bias that is nudged up or down to equalize load.

Mathematical Formulation:
h(x) = W_r x \in \mathbb{R}^{N}
\mathcal{T}(x) = \mathrm{TopK}(h(x), k)
g_i(x) = \frac{\exp(h_i)}{\sum_{j \in \mathcal{T}} \exp(h_j)}
y = x + \sum_{i \in \mathcal{T}} g_i(x)\, E_i(x)
\mathcal{L}_{\mathrm{bal}} = \alpha N \sum_{i=1}^{N} f_i P_i
C = \left\lceil \frac{c\, T\, k}{N} \right\rceil

Where:

  • x \in \mathbb{R}^{d} is the token hidden state and y the MoE layer output, written with the residual path that a dropped token falls back to.
  • W_r \in \mathbb{R}^{N \times d} is the router, h(x) its logits, and E_i the i-th expert MLP; N is the expert count and k the number kept.
  • \mathcal{T}(x) is the selected index set and g_i the gate weight, renormalized over the selected logits only so the weights sum to 1.
  • f_i is the fraction of tokens in the batch dispatched to expert i (piecewise constant, no gradient) and P_i the mean router probability for expert i (differentiable), so \mathcal{L}_{\mathrm{bal}} pushes probability mass away from overloaded experts.
  • \alpha is the balance-loss coefficient, commonly \alpha = 0.01; the product N \sum_i f_i P_i equals 1 under a perfectly uniform assignment and grows toward N under total collapse.
  • T is the tokens per device, c the capacity factor (usually 1.0 \leq c \leq 1.25 in training, larger at eval), and C the per-expert buffer; any token beyond C is dropped.
Line chart of the fraction of routed tokens dropped versus capacity factor from 1.0 to 2.0 for eight experts under uniform, mildly skewed, and heavily skewed router distributions; the uniform curve is flat at zero while the heavily skewed curve stays above twenty percent even at capacity factor two

Figure 2: Token dropping is a joint function of the capacity factor and the router’s skew. Under a balanced router, c = 1.0 drops nothing; under a collapsed router, even doubling capacity leaves a quarter of the assignments discarded, which is why the balance loss matters more than the buffer size.

Architecture diagram of a sparse mixture-of-experts layer showing the data flow from input token through a linear router and top-k softmax selection to two selected expert FFNs out of N, with the remaining N-k non-selected experts shown folded in a dashed box, then a weighted sum with gate weights and a residual connection producing the output

Figure 3: The full MoE layer in one picture. A single linear router produces logits over all N experts, the top-k selection picks only k of them, and the selected expert outputs are combined with renormalized gate weights and added back through the residual connection. Non-selected experts stay resident in memory but consume zero FLOPs for this token, which is why total parameters scale with N while active compute scales with k.

PropertyToken-choice top-kExpert-choiceHash / global assignment
Who selectsEach token picks its k highest-scoring expertsEach expert picks its C highest-scoring tokensA fixed hash of the token ID, or a linear-assignment solver over the batch
Load balanceNot guaranteed; needs an auxiliary loss or a bias correctionExact by construction, every expert receives C tokensExact by construction, with no learned router to collapse
Failure modeOverflow tokens are dropped to the residual and get no expert computeA token can be selected by zero experts, and tokens compete across the batchNo semantic specialization (hash) or expensive, batch-coupled solves (BASE)
Per-token independenceYes; identical decision at batch size 1 and at batch size 1MNo; ranking couples tokens, so causal decoding does not match trainingYes for hashing, no for assignment-based methods
Where it is usedGShard, Switch (k=1), Mixtral (k=2), DeepSeek-V3 (k=8), OLMoEEncoder-style and vision MoE, and research settings with full-sequence visibilityBaselines and ablations that isolate capacity from learned specialization

Login to view more content


Log in to track your progress

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *