Scaling dense Transformer architectures encounters a fundamental computational constraint: increasing total parameter capacity quadratically expands training compute and linearly increases inference latency per token. Sparse Mixture of Experts (MoE) architectures decouple parameter count from per-token compute by replacing monolithic feed-forward network (FFN) blocks with an ensemble of specialized sub-networks, dynamically activating only a small subset of parameters per token.
First popularized in modern deep learning by Shazeer et al. (2017) and refined for massive scale by Fedus et al. (2021) in the Switch Transformer, MoE architectures underpin frontier systems such as Mixtral 8x7B, DBRX, Grok-1, and DeepSeek-V3. However, conditional execution introduces complex routing dynamics, representation collapse, and distributed hardware bottlenecks that require precise mathematical formulation and auxiliary regularization.
The Sparse MoE Formulation: Decoupling Compute from Capacity
In a standard dense Transformer layer, every input token passes through identical multi-head self-attention and feed-forward sub-layers. The dense FFN computes:
where , , and is a non-linear activation function such as GeLU or SwiGLU.
A Sparse MoE layer substitutes the singular FFN with a collection of independent expert networks , coordinated by a parameterized router (gating network) . The layer output is the weighted linear combination of expert outputs:
where is a sparse gating vector satisfying and . Sparsity requires that for any given token , at most entries of are non-zero. When or , the computational FLOPs per token remain equivalent to a small dense model, while the parameter footprint scales with .
+-------------------------------------------------------------------+
| Input Token Vector x in R^d |
+-------------------------------------------------------------------+
|
+------------------------+------------------------+
| |
v v
+-----------------+ +-----------------+
| Router G(x) | | Shared Expert |
| x * W_g | | E_shared(x) |
+-----------------+ +-----------------+
| |
| Top-k Dispatch (k of E) |
+-------------+-------------+ |
| | | |
v v v |
+-----------+ +-----------+ +-----------+ |
| Expert 1 | | Expert 2 | | Expert E | |
| E_1(x) | | E_2(x) | | E_E(x) | |
+-----------+ +-----------+ +-----------+ |
| | | |
+------+------+------+------+ |
| |
v v
Weighted Sum: Sum(G(x)_i * E_i(x)) + E_shared(x)
| |
+--------------------+---------------------+
|
v
Layer Output Vector yRouter Gating Topologies: From Noisy Top-k to Softmax Renormalization
The router function maps continuous hidden states to discrete expert selections and continuous combination weights. Multiple routing formulations have evolved to balance computational efficiency and gradient propagation.
1. Noisy Top-k Gating
In Shazeer et al. (2017), gating decisions incorporate tunable Gaussian noise to promote exploratory routing during initial training stages:
where . The top- elements are preserved while all other logits are masked to :
The sparse gating weights are computed via Softmax:
2. Deterministic Top-k with Softmax Renormalization
Modern large language models such as Mixtral 8x7B (Jiang et al., 2024) remove the noise parameterization during standard forward passes. The router computes unnormalized logits , applies a Softmax over all experts, selects the top- indices , and renormalizes the weights:
Renormalization ensures that , preventing gradient attenuation when non-selected experts accumulate significant probability mass.
3. Single-Expert Routing (Switch Transformer)
The Switch Transformer (Fedus et al., 2021) sets . Each token routes to exactly one expert:
Top-1 routing reduces communication overhead during distributed expert parallelism, as each token requires a single point-to-point transfer rather than multi-cast fan-out.
Capacity Factors, Buffer Sizing, and Token Dropping
In distributed training and inference, tokens are partitioned across accelerator devices hosting different experts using all-to-all collective communications (all_to_all_single). Because static tensor shapes are required for efficient hardware execution and non-blocking CUDA kernels, each expert is allocated a fixed batch buffer size termed Expert Capacity ().

Given a batch of tokens and experts, the expert capacity is defined by:
where is the Capacity Factor.
- (Exact Capacity): Allocates buffer space assuming perfectly uniform token distribution across all experts.
- (Slack Capacity): Allocates additional buffer headroom (such as or ) to accommodate routing variance across batches.
- Token Dropping: If the router assigns tokens to expert , the excess tokens cannot fit into the expert's static execution buffer. These dropped tokens bypass the expert layer entirely, passing through the residual connection unchanged:
Token dropping degrades model perplexity if unmitigated. However, setting too high wastes accelerator memory and induces communication padding overhead for under-utilized experts.
Representation Collapse and the Differentiable Load Balancing Loss
A fundamental pathology in naive MoE training is expert collapse (winner-take-all routing). If a small subset of experts initially receives slightly more tokens, their parameter gradients update faster, improving their representation capacity relative to unselected experts. The router subsequently assigns even more tokens to these dominant experts, starving the remaining sub-networks and reducing the effective capacity to that of a dense model.
To enforce uniform expert utilization, MoE architectures optimize an auxiliary load balancing loss alongside the primary language modeling cross-entropy loss .
Total Loss = L_LM(tokens) + alpha * L_aux(routing) + c_z * L_z(logits)Mathematical Derivation of Switch Auxiliary Loss
Let denote a batch of tokens. For each expert :
- Fraction of Tokens Dispatched (): The discrete empirical fraction of tokens routed to expert :
- Average Routing Probability (): The continuous expected probability mass assigned to expert across the batch:
The auxiliary loss is defined as the scaled dot product between the dispatch vector and probability vector :
where is a balancing coefficient (typically ).
Why the Formulation Works
The objective achieves its global minimum when token routing is uniformly distributed across all experts:
- By Cauchy-Schwarz and Jensen's inequality:
- At uniform balance ( and for all ):
- At complete collapse ( and ):
Because is computed via discrete argmax, it is non-differentiable and treated as a constant during backpropagation. However, is smooth and differentiable with respect to the router weights .
The gradient with respect to the router logit is:
When expert is overloaded (), the gradient , penalizing to decrease for subsequent tokens. Conversely, underutilized experts receive negative gradient pressure, driving their selection probabilities upward.
Numerical Stability: The Router z-loss
During long pre-training runs with FP16 or BF16 mixed precision, router logits tend to grow in magnitude. Large positive logits produce extreme values in , triggering floating-point overflow and numerical instability in the Softmax backward pass.
To stabilize router optimization, Zoph et al. (2022) introduced the Router z-loss in ST-MoE:
where is a regularization hyperparameter (typically ). The z-loss penalizes large values of the log partition function , forcing router logits toward zero without constraining the relative differences between expert scores.
Structural Evolutions: Fine-Grained Segmentation and Shared Experts
Recent architectures move beyond standard Top-2 MoE routing by re-architecting the expert granularity and parameter partitioning.
Traditional MoE (8 Large Experts, Top-2):
Combinations = C(8, 2) = 28 expert combinations
DeepSeekMoE Fine-Grained (64 Small Sub-Experts, Top-8 + Shared):
Combinations = C(64, 8) = 4,426,165,368 combinations (Identical Active FLOPs)Structural Comparison Across Paradigms
- Standard Top-k MoE (e.g., Mixtral 8x7B): 8 large experts per layer ( standard FFN), routing top-2 via renormalized softmax, combinatorial capacity paths, capacity factor , balanced via continuous-discrete auxiliary loss .
- Switch Transformer: 64 to 2048 experts per layer ( standard FFN), routing top-1 via deterministic argmax, combinatorial capacity paths, capacity factor , balanced via linear-scaling auxiliary loss .
- Fine-Grained MoE with Shared Experts (DeepSeekMoE / DeepSeek-V3): 64 to 256 routed sub-experts ( or standard FFN size) plus 1 to 2 unconditionally shared experts, routing top-6 to top-8 sub-experts, combinatorial capacity paths, dropless dynamic buffer routing, balanced via step-wise logit bias calibration () without auxiliary loss backpropagation.
1. Fine-Grained Expert Segmentation
Introduced in DeepSeekMoE (Dai et al., 2024), fine-grained segmentation divides each standard expert of intermediate dimension into smaller sub-experts of dimension . Rather than selecting large experts out of , the router selects sub-experts out of .
This preserves identical computational FLOPs and parameter memory while expanding the combinatorial representation space from to . Finer granularity enables more nuanced functional specialization across token domains.
2. Isolated Shared Experts
In standard MoE routing, common linguistic patterns, syntactic markers, and general task knowledge are redundantly learned across multiple specialized experts. DeepSeekMoE isolates shared experts that are unconditionally active for all tokens:
Shared experts capture invariant foundational representations, allowing the routed experts to focus exclusively on domain-specific features without parameter redundancy.
3. Auxiliary-Loss-Free Dynamic Bias Routing
In large-scale training runs, balancing auxiliary losses against the primary task loss introduces a gradient conflict: strong auxiliary loss forces uniform utilization at the expense of optimal task routing, while weak auxiliary loss allows expert collapse.
DeepSeek-V3 (DeepSeek-AI, 2024) eliminates from the backward gradient graph entirely. Instead, the router adds an explicit bias term to the routing logits:
The bias terms are dynamically adjusted at the end of each training step based on expert load:
where is a step update hyperparameter. Because is updated outside the optimizer backward graph, the language model gradients remain uncorrupted by artificial balance objectives, maximizing cross-entropy optimization while maintaining balanced hardware utilization.
Sources
- Outrageously Large Neural Networks: The Sparsely-Gated Mixture-of-Experts Layer (Shazeer et al., 2017)
- Switch Transformers: Scaling to Trillion Parameter Models with Simple and Efficient Sparsity (Fedus et al., 2021)
- GShard: Scaling Giant Models with Conditional Computation and Automatic Sharding (Lepikhin et al., 2020)
- ST-MoE: Designing Stable and Transferable Sparse Expert Models (Zoph et al., 2022)
- Mixtral of Experts (Jiang et al., 2024)
- DeepSeekMoE: Towards Ultimate Expertise in Shared and Fine-Grained Routing (Dai et al., 2024)
- DeepSeek-V3 Technical Report (DeepSeek-AI, 2024)



