Sparse Activation and Gated Routing in Mixture-of-Experts Architectures
Mixture-of-Experts (MoE) enables sparse activation: models can have massive total parameters while each token only activates a small subset. This article explains how MoE layers are structured, how Top-K routing works, why temperature does not change expert selection, how load balancing prevents routing collapse, and the engineering costs—memory, all-to-all communication, and load imbalance—that are often underestimated when deploying MoE models.
1. Background: The Compute Bottleneck of Dense Models
In a standard dense model, every input token passes through the full set of parameters. Doubling the parameter count roughly doubles the compute required for a single inference step. Model capability and inference cost therefore scale linearly, which limits how large dense models can be deployed.
Sparse activation offers an alternative path: the model’s total parameter count can be very large, but each token only invokes a small fraction of those parameters. Total model capacity is determined by all experts, while per-token compute is determined only by the activated ones. The Mixture of Experts (MoE) architecture is the most common way to implement sparse activation.
2. Basic Structure of an MoE Layer
A typical MoE layer contains two components:
- Expert networks (Experts): a set of feed-forward networks (FFNs) with identical architecture but independent parameters.
- Gating network (Router/Gate): a small linear layer that computes a score for each expert based on the input token’s representation.
The layer output is a weighted sum of the selected experts’ outputs:
y = Σ_{i∈TopK(x)} g_i(x) · E_i(x)
where E_i denotes the i-th expert and g_i(x) is the weight assigned to that expert by the gate.
3. How Top-K Routing Works
Routing typically proceeds in four steps:
- Compute gating logits for the input x: z = W_g · x.
- Select the indices of the K highest-scoring experts.
- Apply softmax to these K scores to obtain normalized weights.
- Send the token to the K experts and sum their outputs by weight.
Illustrative pseudocode:
function moe_forward(x):z = gate_linear(x) # one score per expertidx, val = topk(z, K) # pick the K highest-scoring expertsw = softmax(val) # normalize only among selected expertsout = 0for j in range(K):out += w[j] * experts[idx[j]](x)return out
With this design, the expert layer’s compute depends only on K. If there are N experts in total and K are activated each time, the expert layer’s compute is roughly K/N of an equivalently sized dense FFN (excluding routing overhead).
4. Temperature: A Common Misunderstanding
The temperature T appears in the tempered softmax:
p_i = exp(z_i / T) / Σ_j exp(z_j / T)
A larger T flattens the distribution; a smaller T sharpens it. This much is certain. However, one common misconception needs to be clarified: temperature does not change the set of experts selected by Top-K. For any T > 0, the ordering of z_i / T is identical to that of z_i, so Top-K selection depends only on the ranking of the raw logits. Temperature changes how weights are distributed among the selected experts—not which experts are selected.
The following illustrative data makes this concrete (logits are [2.0, 1.0, 0.5, -1.0], K = 2, and weights are renormalized among the selected experts):
| Temperature T | Weight of top expert | Weight of second expert | Selected experts |
|---|---|---|---|
| 2.0 | 0.622 | 0.378 | The two highest-scoring experts |
| 1.0 | 0.731 | 0.269 | The two highest-scoring experts |
| 0.5 | 0.881 | 0.119 | The two highest-scoring experts |
As shown, a lower temperature emphasizes the dominant expert’s weight, but it does not increase the number of activated experts. If you need to adjust the expert count dynamically, use threshold-based filtering or a dynamic-K strategy rather than relying on temperature.
5. Load Balancing: Preventing Routing Collapse
If the gate persistently favors a small number of experts, routing collapse occurs: popular experts are continuously overloaded while unpopular ones receive almost no training. Three common countermeasures are:
- Auxiliary loss: penalizes imbalance between expert usage frequency and gating probability. A common form is L_aux = α · N · Σ_i f_i · P_i, where f_i is the fraction of tokens assigned to expert i, P_i is the gate’s average probability for expert i, and α is the loss coefficient.
- Expert capacity limits (capacity factor): caps the number of tokens each expert can process in a batch; excess tokens overflow to a fallback path or are dropped.
- Routing noise: adds small random noise to the logits during training to encourage exploration in routing.
The auxiliary loss coefficient requires careful tuning: too large a value degrades model quality, while too small a value fails to prevent collapse.
6. Key Engineering Challenges in Deployment
The compute benefits of MoE are often emphasized, but several engineering costs are easily underestimated in practice.
- Memory footprint: even though only some experts are computed per token during inference, all expert parameters must remain resident in GPU memory or high-speed storage. Memory requirements are determined by the total parameter count, not the activated parameter count.
- Cross-device communication: experts are typically distributed across multiple accelerators. Tokens must be sent to their target experts via all-to-all communication, and results must be gathered back afterward. Communication latency can become the dominant bottleneck.
- Load imbalance across compute: different experts receive different numbers of tokens, leaving some devices idle while they wait.
- Kernel efficiency: the efficiency of operators used for sparse computation is a practical concern that affects whether theoretical compute savings translate into real speedups.
