Mixture of Experts Architecture: A Key Path to Efficient LLM Inference
This article explains how Mixture of Experts (MoE) separates total parameters from activated parameters to improve LLM inference efficiency. It covers expert subnetworks, gating networks, sparse top-K activation, output merging, clarifies the effect of gating temperature, provides a code example, and discusses engineering trade-offs and practical selection guidance.
1. Background: The Tension Between Parameter Scale and Per-Computation Cost
The capabilities of large models generally improve as parameter scale grows, but inference cost rises as well. When a dense model processes each token, nearly all parameters participate in computation. If the parameter count doubles, the amount of computation for a single forward pass is also close to doubling, and memory usage and latency increase accordingly. For services deployed at scale, hardware investment and electricity costs continue to climb.
How to reduce the number of parameters actually used in each computation without significantly sacrificing capability has been one of the core questions in large-model architecture design in recent years. The Mixture of Experts (MoE) architecture is a widely adopted approach in the industry. It separates the “total parameter count” from the “activated parameter count”: the total model capacity can be large, but each input invokes only a small subset of it.
2. Basic Components of the MoE Architecture
2.1 Expert Subnetworks
Feed-forward layers in the model are replaced with several parallel expert subnetworks. The experts usually have the same structure but independent parameters. Their specialization is not manually assigned; it emerges naturally during training.
2.2 Gating Network
The gating network, or router, is a small linear layer. It receives the hidden representation of the current token and outputs a score for each expert. The scores are normalized with softmax to represent the relative weight of each expert.
2.3 Sparse Activation
The system selects only the K experts with the highest scores to participate in computation, while the remaining experts do not perform a forward pass. Suppose a configuration contains 64 experts and selects 6 each time (hypothetical numbers for illustration only); then a single token triggers only about one-tenth of the expert parameters. The total model capacity remains large, but the computation is close to that of a much smaller dense model.
2.4 Output Merging
The outputs of the selected experts are weighted by the gating weights and summed to produce the output of that layer. The weights are usually renormalized within the selected experts so that they sum to 1.
3. Gating Temperature: Clarifying a Common Misconception
A common claim in the community is that increasing the softmax temperature activates more experts, while lowering the temperature keeps only the top-K experts. This understanding needs correction.
The softmax temperature T acts on the gating scores in the form softmax(z / T). When T is large, the probabilities of the experts tend toward uniform; when T is small, the distribution becomes sharper. This part is correct. However, in top-K routing, which K experts are selected depends only on the ordering of the scores z. When T is greater than 0, the ordering of z / T remains the same as that of z, so regardless of the temperature value, the selected expert set is exactly the same. Temperature changes the weight allocation among those experts, not the number of activated experts.
So what is temperature useful for in practice? There are two main aspects:
- Training stage: Using a smoother distribution in the early stage of training can allow more experts to receive gradient signals, and this is often used as an exploration method.
- Weight allocation: When the contribution ratio among selected experts needs to be adjusted, temperature can change the weighted result.
If the number of activated experts is to truly vary with the input, the routing strategy itself needs to change, for example by selecting experts according to a cumulative probability threshold or by setting rules for dynamic K. Adjusting temperature alone cannot achieve this goal.
4. Code Example: Top-K Routing with Temperature
def softmax(z):z = z - max(z)e = exp(z)return e / sum(e)def route(hidden, gate_weight, k=2, temperature=1.0):logits = hidden @ gate_weight # shape: [num_experts]probs = softmax(logits / temperature) # temperature only affects probability shapetopk_idx = argsort(probs)[-k:] # selection depends only on orderingtopk_w = probs[topk_idx] / sum(probs[topk_idx])return topk_idx, topk_w
The verification method is simple: call the function with the same hidden vector and temperature values of 0.5 and 2.0. The topk_idx results are exactly the same, and only the topk_w values change.
5. Key Engineering Trade-offs
5.1 Memory and Storage
A low activated parameter count does not mean low storage requirements. The total parameters still need to reside fully in GPU memory or be distributed across multiple devices. The hardware budget should be estimated based on the total parameter count, not only on the per-computation workload.
5.2 Communication Overhead
When experts are distributed across different devices, tokens need to be dispatched across devices according to routing results, and the outputs then need to be collected and merged. Communication latency may offset part of the computational benefit, so the expert placement strategy is important.
5.3 Load Balancing
If most tokens are routed to a small number of experts, some experts become overloaded while others remain idle for long periods. Common practices include adding auxiliary terms to the training loss to encourage balance and setting a capacity limit for each expert, with excess tokens dropped or forwarded according to predefined rules.
5.4 Training Stability
Routing involves discrete selection, so gradient propagation is less smooth than in dense layers. In practice, additional designs are usually needed, such as injecting noise into gating scores or using auxiliary losses to constrain the expert usage distribution.
6. Model Selection and Practical Recommendations
- If the business is mainly characterized by high concurrency and low latency, and memory resources are sufficient, a sparse architecture can be prioritized to gain larger model capacity at a lower unit inference cost.
- If the deployment environment has tight memory or limited inter-device bandwidth, a dense model or a smaller sparse configuration may be more reliable.
- During evaluation, compare per-token computation, peak memory, end-to-end latency, cost per request, and quality metrics on the target task at the same time. Looking only at the total parameter count can easily lead to misleading conclusions.
- When developing in-house or doing secondary development, first validate whether routing is balanced in a small-scale environment, confirm that the expert load distribution is reasonable, and then scale to the full size.
