Author: Jianlin Su (苏剑林) | kexue.fm
This is a compilation and AI-generated translation of Jianlin Su’s wonderful series of blog posts “MoE环游记”.
Part 1: Starting from Geometric Interpretation
By Jianlin Su | 2025-02-08
A couple of years ago, in a stroke of inspiration, I started a “Transformer Upgrade Path” series, where I shared various improvements to mainstream Transformer architectures along with my personal reflections, which received recognition from some readers. Starting with this article, we follow the same style to introduce another mainstream architecture: MoE (Mixture of Experts).
The popularity of MoE goes without saying. The recently viral DeepSeek-V3 uses an MoE architecture, GPT-4 is rumored to also be MoE, and many recently released domestic models have adopted MoE as well. However, although research on MoE has a long history, its practical applications remained lukewarm for a long time. It was roughly from early last year’s Mixtral of Experts that MoE began to attract widespread attention. Its notable advantage is a large parameter count while keeping both training and inference costs significantly lower.
At the same time, MoE also has some challenges, such as training instability, load imbalance, and suboptimal performance — these were the main reasons it did not gain popularity in earlier years. However, with the increased attention over the past two years, these problems have been largely resolved, and we will discuss them one by one in subsequent articles.
Problem Definition
First, I should point out that I will introduce MoE using my own line of reasoning. Where necessary, I will attach the corresponding references, but I will not systematically trace the origins of the MoE architecture. I hope readers will bear with me.
We know that Transformer models consist of Attention layers and MLP layers, and MoE replaces the MLP layers. MLP layers come in two varieties: FFN (FeedForward Network) and GLU (Gated Linear Unit). The mainstream choice is GLU, but for simplicity we will use FFN as our example:
where is the input vector (a row vector), are two parameter matrices, and is an element-wise activation function. Let be an integer that divides . Then the above can be equivalently written using block matrices as:
where , with slicing following Python conventions. From this we can see that FFN can be equivalently expressed as the sum of vectors , where each vector represents the output of a small model . Each small model has the same computational cost, and these small models are the “Experts” in MoE.
The question MoE poses is:
Can we select just vectors whose sum approximates the sum of all vectors? This would reduce computation to of the original.
Sorting by Norm
This problem was actually explored in The Road to Low-Rank Approximation (Part 3): CR. Written as a mathematical formula:
Let , then it can be rewritten as:
Finding the exact solution to this problem is quite difficult, but there is a simple approximate solution: when the are pairwise orthogonal, we have:
The optimal solution to the above is obviously to set for the vectors with the smallest norms , which is equivalent to selecting the vectors with the largest norms to approximate the sum of all vectors. When the do not satisfy the pairwise orthogonality condition, we still use this as an approximate solution. Its geometric interpretation is also intuitive: vectors with larger norms are less likely to be cancelled out during summation, and thus play a more prominent role.
Furthermore, in The Road to Low-Rank Approximation (Part 3): CR we also discussed a probabilistic sampling-based approximation process. Under the assumption of minimum variance, the optimal sampling probabilities are also proportional to the norms. So overall, sorting by vector norm is a simple yet effective strategy.
The Emergence of MoE
Now we have a strategy — “select the vectors with the largest norms” — but upon closer reflection we realize it is not practical: to select the vectors with the largest norms, we must compute all the norms, which means computing all of the first. But our original goal was to reduce the computation of !
To resolve this contradiction, we need to redesign each Expert model so that its norm can be computed at low cost. What does this mean? First, we normalize to get , so that every has the same norm. Then we define:
where is a parameter matrix, and is an activation function mapping . In plain terms, this is just a linear transformation from dimensions to dimensions followed by an activation function, so it has relatively low computational cost. This component is called the “Router” in MoE.
What is the role of ? It predicts the norm of each Expert! In other words, we use as the norm of the -th Expert, and is the complete Expert. It is decomposed into two parts: the low-cost norm and the high-cost direction . To save computation, we first compute , select the top , and only then compute the corresponding , finally multiplying by and summing:
This is the fundamental formula of the MoE model. Since only the Top- portion is retained in the computation, it is essentially a Sparse model. The original FFN, or the model when , is typically called the corresponding Dense model.
Summary of the Approach
Whether or not readers are familiar with MoE, the above process may seem somewhat unfamiliar, because this is my own independently conceived route to understanding MoE. However, because its geometric meaning is more explicit, it should fundamentally be easier to understand.
Let us reorganize the entire line of reasoning:
A standard Dense FFN model can be equivalently rewritten as the sum of Expert vectors .
To save computation, we attempt to select vectors whose sum approximates the original sum of vectors.
After formulating this as a mathematical optimization problem, we find that the selection rule is to pick the vectors with the largest norms.
Directly computing the norms of all Experts and then selecting does not actually save computation, so we need to redesign the Experts.
We normalize to get , then use a separate small model (the Router) to predict the norm . The final Expert is .
At this point, we can first compute all , select the top , and only then compute , achieving the goal of saving computation.
Why Go Through All This
Some readers might wonder: why go through this seemingly complex process? Isn’t the standard MoE already easy to understand? The general MoE form is:
That is, there is no normalization of before summation, and does not carry the meaning of a norm — it is purely a scoring model (the Router) used to rank Experts. But why does multiplying onto the Expert enable the Router to learn to correctly rank Experts? I have found that only Sparse Backpropagation for MoE Training provides an explanation for this, but it is still somewhat lacking in intuition.
Under the geometric perspective of this article, however, many questions become immediately clear. After reparameterizing the Expert as , the Dense model corresponds to summing all , while MoE corresponds to summing after selecting the Top- by . This is a theoretically guaranteed approximation of the Dense model. We do not consider how the Router should choose Experts; we simply try to approximate the Dense model as closely as possible at each step. This can be said to be the optimal choice for both having large parameters and having low computation.
Now that the geometric meaning of is a norm rather than a probability, the activation function has no normalization requirement. Besides Softmax, functions like Sigmoid and ReLU can also be considered, as can the smooth Top- approximation introduced in The Sequel to Softmax: Finding a Smooth Approximation to Top-K. Using a non-normalized activation function for the Router helps avoid destructive competition between Experts when , and can sometimes achieve better results.
One final note: we previously defined with the purpose of making all have equal norms. In practice, L2 normalization is not strictly required — other equivalent operations can be used, such as RMS Norm with the gamma parameter fixed at 1, which better fits our output conventions.
Article Summary
This article derives and understands MoE starting from the best approximation of a Dense model, arriving at a specific form of MoE that includes one additional Normalize step compared to existing MoE, but makes the geometric interpretation of MoE much more apparent. Of course, regardless of whether normalization is used or not, the MoE journey has only just begun — more challenges lie ahead.
Part 2: Not Scarcity but Inequality
By Jianlin Su | 2025-02-21
In the previous article, “MoE Odyssey: Part 1, Starting from Geometric Intuition,” we introduced a geometric interpretation of MoE, aiming to derive and understand MoE by starting from the best approximation of a Dense model. At the end of that article, we also mentioned that writing down the MoE computation formula is merely the beginning — training a practically effective MoE model requires filling in many details, such as the Load Balance problem discussed in this article.
Load balance — “not scarcity but inequality”1 — put plainly, means making every Expert work, and making them all do as close to the same amount of work as possible, to avoid some Experts wasting compute. Load balance is both a requirement for fully utilizing training compute and a requirement for maximizing the potential of MoE’s large parameter count.
Requirements Analysis
We know that the basic form of MoE is
For traditional MoE, is a probability distribution (Router), , and is the output of a small FFN (Expert); for the geometric MoE we derived in the previous article, has no normalization requirement — it predicts the magnitude of the Expert — while predicts the direction of the Expert.
Regardless of which format of MoE is used, the actual performance is roughly the same — it is merely a difference in perspective. However, note that although the MoE formula gives the impression of “for each Token encountered, find the corresponding Expert to compute,” during actual training the process is reversed: compute is first allocated to each Expert, and then Tokens are routed to their assigned Expert for parallel computation. This is precisely why the scoring function is called a Router.
Given this, if Expert assignment is imbalanced, the following situation may arise: some Experts (Dead Experts) remain almost permanently idle, wasting compute; other Experts have too many Tokens to process and simply cannot keep up, resorting to Token Drop (i.e., abandoning processing of some Tokens). Theoretically, the emergence of Dead Experts means the MoE has not achieved its expected parameter count — you spend the memory budget of a large-parameter model but end up training only the effective capacity of a small-parameter model.
Therefore, from both the training and performance perspectives, we want to ensure load balance among Experts.
Auxiliary Loss
The standard approach to promoting load balance is to add a related loss function, commonly referred to as “Aux Loss (Auxiliary Loss).” The Aux Loss in mainstream use today can be traced back to the 2020 paper “GShard: Scaling Giant Models with Conditional Computation and Automatic Sharding.”
Before introducing Aux Loss, we need to introduce some new concepts. First, we have already mentioned that for a general MoE, is not necessarily a probability distribution. We denote the normalized as , and its Top- version as , where
Next, we define , where denotes averaging over all Tokens across all samples. It is easy to see that is the current load distribution of the Experts, while serves as a smooth approximation of .
With these notations, we can write the Aux Loss as:
Most literature defines Aux Loss with an additional factor of , i.e., their Aux Loss equals here. Additionally, some large-scale MoE models may compute Aux Loss per device to achieve intra-device balance and reduce inter-device communication — these are case-by-case adaptations. However, more recent experiments have shown that forcing local balance may very likely hurt the model’s final performance.
Straight-Through Estimation
I wonder if anyone has noticed a curious phenomenon: whether in the original source, subsequent literature, or tutorial articles — in all the materials the author has read — Aux Loss is cited without proof, as if everyone agrees that the above Aux Loss promoting balance is a self-evident fact. But is it really so obvious?
The author certainly did not find it obvious, so below I present a derivation approach for Equation , from which we can also customize other forms of Aux Loss. First, define the uniform distribution . We just said that is the current load distribution, so load balance is equivalent to . Then the following is a fairly intuitive Aux Loss:
The problem is that is produced by , which means the above is not a directly usable differentiable objective. How do we solve this problem? The answer is the STE (Straight-Through Estimator) trick — designing separate functions for forward and backward passes. Specifically, is non-differentiable, but as its smooth approximation is differentiable, so we simply replace with during backpropagation:
Here is the stop-gradient operator, which preserves the forward output unchanged but forces the gradient to zero. After this modification, becomes a practically viable Aux Loss. Let us compute its gradient:
Here denotes the model parameters. The final result shows that the gradient of Equation equals the gradient of Equation , which means using Equation as the Aux Loss is gradient-equivalent to Equation . This is how Equation came to be used as the Aux Loss.
However, Equation only has meaning as an equivalent gradient — it does not have meaning as a loss, and is not a true loss function. For example, when , we can compute that Equation equals , but in fact we can construct an not equal to that makes it less than . So Equation is not like a normal loss where smaller is always better, and its minimum is not achieved when .
General Form
The above derivation actually provides a general approach for constructing Aux Losses: first construct a loss based on that meets the requirements, then in implementation replace with .
For example, we know that maximum entropy can also push a distribution toward uniformity, so we can also use the negative entropy to construct an Aux Loss:
The above can be used directly in code implementation. Of course, if we pursue simplification, we can similarly compute the gradient, which yields:
In both gradient simplification steps, we used the following identity:
This relies on the fact that is a probability distribution and that the target distribution is uniform. If we do not pursue a simplified equivalent result but instead directly use the form of the Aux Loss, then we are not constrained by these two conditions.
For example, regarding as a smooth approximation of , we only used the property that “when is large, is typically also large,” so using the non-normalized as usually works fine as well. This point may be critical in certain special scenarios (e.g., when can be both positive and negative), since normalization into a probability distribution is then impossible. Similarly, the objective can obviously push toward any target distribution we desire, not necessarily uniform.
Summary
This article introduced the load balance problem in MoE and presented a general approach for constructing Aux Losses. Besides Aux Loss, there are other methods for promoting load balance, which we will discuss next time.
Part 3: A Different Approach to Assignment
By Jianlin Su | 2025-03-05
In this article, we continue exploring the load balancing problem in MoE. In the previous article, “MoE Odyssey: Part 2 - Not Scarcity but Inequality,” we mainly discussed the approach of promoting load balance through Aux Loss. While Aux Loss is simple and intuitive, it has an obvious drawback — the weight is hard to tune — too low and it fails to promote balance, too high and it easily harms LM Loss. The community has therefore been continuously searching for alternatives.
What we will share in this article is the approach called “Loss-Free,” proposed by DeepSeek in Auxiliary-Loss-Free Load Balancing Strategy for Mixture-of-Experts. Compared to DeepSeek’s many dazzling open-source works, this paper may not seem particularly eye-catching, but in my view, its potential academic influence may far exceed their other work, because the proposed method is not only simple and effective, but also extremely general — a true classic.
General Idea
When facing load imbalance, Aux Loss addresses it by using an additional loss to guide the Router toward balanced scores. Loss-Free instead takes a different approach to assignment: rather than changing the Router’s existing scores, it changes the assignment mechanism itself.
There have been prior efforts in this direction. For example, in 2021 Facebook proposed BASE Layer, which treats expert assignment as a linear assignment problem — finding an assignment that maximizes the Router’s total score subject to load balance constraints. This can be solved using the Hungarian algorithm and similar methods. However, this approach requires knowing the scores of all tokens, so for autoregressive LLMs it is only applicable during training; inference still has to use , creating an inconsistency between training and inference. Furthermore, due to current algorithmic limitations, it only applies to the case.
In contrast, Loss-Free’s approach is very simple and effective. It observes a key fact: we can always introduce a bias term such that the assignment from is balanced. So it modifies the MoE formulation to:
Here is an input-independent vector determined during training. Once training is complete it remains fixed, so it can also be used during inference — in other words, training and inference have a consistent form. Note that the coefficient multiplying is still rather than , meaning only participates in the assignment process and not in the MoE forward computation. Therefore we have no special requirements on the sign of or .
Hand-Crafted Gradients
How do we train ? We know that the optimization direction for should naturally promote load balance. Following the notation from the previous article, we first define :
and , where is naturally the current load distribution of experts under the bias . Next we define the uniform distribution as , so that load balance amounts to minimizing:
This objective is non-differentiable, but with the experience from the previous article, we know that STE (Straight-Through Estimator) can solve this problem. The key to STE is finding a differentiable quantity that has the same monotonic trend as to serve as a smooth approximation. Here our optimization parameter is only , and it happens to have exactly the property we want (increasing makes more likely to be selected, thus making larger). So the answer is readily apparent:
Its gradient is:
So updating with gradient descent (SGD) gives:
where is the learning rate for . However, Loss-Free ultimately chooses a slightly different update rule — Sign Gradient Descent (SignSGD):
This result is actually quite intuitive: if is larger than , then decrease a little; otherwise increase a little.
An Improved Version
Beyond the sign gradient descent with , I have found that directly applying RMS Norm to (i.e., Normalized SGD) often achieves better balance for the same :
where is the “Root Mean Square,” defined as:
It is easy to see that both and have an RMS of 1, so they are roughly the same scale, which means we can use the same .
Put simply, the problem with is that it uses the same update magnitude regardless of how far is from the target . This causes values that are already close to to easily deviate from the balance they had already achieved, producing oscillation. RMS Norm, on the other hand, preserves the relative magnitudes of , making the update magnitudes more adaptive. Theoretically this is more conducive to promoting balance, and in practice it usually performs better.
A Unified Lineage
When the original paper introduces Loss-Free, it does not include the Aux Loss derivation above but directly gives the update rule , giving the impression that it “hand-crafted” the gradient for . This is also the origin of the name “Loss-Free.”
However, as the derivation in this article shows, update rule can be fully derived from the Aux Loss perspective — the two are of a unified lineage. While Loss-Free’s most direct benefit appears to be eliminating the need to tune the Aux Loss weight, it actually still has a learning rate parameter to tune. Although the original paper has already searched and found the default value , this hyperparameter undeniably exists.
In my view, Loss-Free’s essential innovation is not the absence of Aux Loss, but rather the isolation of optimization parameters between Aux Loss and LM Loss, thereby achieving the dual goals of load balance and model capability. The most critical step is recognizing the fact that “a single bias term suffices for load balance,” then letting Aux Loss optimize only the newly introduced bias while LM Loss optimizes the remaining parameters, minimizing the negative impact of Aux Loss on LM Loss.
In contrast, the conventional Aux Loss approach requires all parameters to promote load balance, and LM Loss also optimizes all parameters. The two optimization directions may not be fully compatible, making it relatively more difficult to find an optimal balance point. Therefore, Loss-Free’s approach of isolating the optimization parameters of the two losses based on “a single bias term suffices for load balance” is an elegant solution to the load balancing problem.
Practical Details
Although Loss-Free is already simple and clear enough, there are some details to pay attention to in practice.
First, for each batch of data, we should first update the model parameters according to LM Loss, and then update according to rule . This is because the update of depends on the statistical information over all tokens. If we update before updating the rest of the model parameters, there is in principle a risk of leaking future information. Although intuitively a single vector cannot leak much information, this risk still exists and should be avoided.
Second, we mentioned that the original paper has tuned , but this result may be tied to the paper’s choice of Sigmoid as the Router activation function for . The reason is not hard to see: after Sigmoid, each is relatively independent and lies within , and means the update magnitude per step is about one thousandth. If we switch to Softmax, ReLU, or other activation functions, we may need to re-tune .
To address this issue, my recommendation is to decouple the activation functions used for the Gate and the Bias, namely:
where , is the Sigmoid function, and is any monotonic function with non-negative range. In plain terms, the scores that is added to use Sigmoid activation, allowing us to reuse . As for the Gate that multiplies the Expert output, we can use other activation functions, as long as their monotonicity is consistent with Sigmoid.
Additionally, because update rule uses the function, it is possible to train values with absolute values greater than 1, and the overall absolute values may keep growing. This is all normal and has no impact on model performance. In fact, has a redundant degree of freedom, because adding the same constant to all leaves unchanged. This extra degree of freedom can be used for other interesting purposes (to be continued in the next installment).
Further Reflections
Beyond MoE load balancing, the Loss-Free philosophy can be applied to many similar problems. For example, codebook collapse in VQ-VAE can be solved using the same approach, and compared to previously introduced “rotation tricks” and “linear transformation tricks,” it appears more natural and general. In fact, the opening assessment that “Loss-Free’s potential academic influence may far exceed other work” is precisely based on considerations of Loss-Free’s generality.
Setting aside specific application contexts, from a mathematical perspective, Loss-Free’s contribution can be understood as providing a method for solving assignment problems via gradient descent. A classic linear assignment problem can be expressed as:
where is a given cost function and is a bijection from to itself. In the context of this article, corresponds to the scores of tokens and experts, and the desired is simply a load-balanced assignment. The general approach to solving such problems is to search for an optimal solution within the space satisfying the constraints. Loss-Free takes the opposite approach, first constructing an optimal solution that may not satisfy the constraints:
This solution is certainly optimal in terms of scores but may not satisfy the bijection condition — not satisfying bijection is equivalent to load imbalance. So we introduce a bias:
is initialized to zero and then updated according to rule . The update rule simply says: whichever appears too many times, decrease the corresponding ; otherwise increase it — until a bijection is achieved.
Summary
This article introduced the Loss-Free method for MoE load balancing, proposed by DeepSeek. Its core idea is achieving load balance through introducing a simple bias term. We further explored its connection to Aux Loss, as well as its potential for application to similar mathematical problems.
Part 4: Invest More Where It’s Difficult
By Jianlin Su | 2025-03-28
In the previous two articles we discussed load balancing. When introducing the Loss-Free approach in “MoE Odyssey: Part 3, A Different Approach to Allocation,” I left a cliffhanger: the Bias term it introduces has a redundant degree of freedom, and this degree of freedom can be used for something else that is interesting. In this article, we discuss exactly that.
We know that MoE selects only the top- most-matching Experts for each token to perform computation, thereby increasing parameter count while saving computation. However, upon careful reflection, this strategy clearly has room for improvement: intuitively, not all tokens are equally difficult, so a more reasonable approach would be to allocate more computational resources to difficult tokens and fewer resources to easy tokens. This might maximize performance under the same limited resource budget.
The extra degree of freedom in the Bias term mentioned above happens to provide a simple way to achieve this goal.
Design Philosophy
First, let us review the basic form of MoE:
Load imbalance is a common problem in MoE training. Researchers have proposed Aux Loss to address this, which we covered in “MoE Odyssey: Part 2, Not Scarcity but Inequality.” Additionally, in “MoE Odyssey: Part 3, A Different Approach to Allocation,” we introduced the Loss-Free approach proposed by DeepSeek, which modifies MoE to:
It then achieves load balancing by adjusting the newly introduced Bias term . To allow each token to select a dynamic number of Experts, my proposed approach slightly modifies the Loss-Free formulation:
That is, any Expert satisfying is selected. This way, the number of Experts chosen per token is naturally dynamic, and it eliminates the need for sorting — in some sense, it even becomes simpler.
Optimization Objectives
The optimization of has two objectives: first, like Loss-Free, to achieve load balance; second, to control the average number of Experts selected per token to be , which we can call budget control — otherwise one could simply set to select all Experts, but that is not what we want.
Load balancing still follows the Loss-Free training procedure. Define the notation :
Then let , so is the current Expert distribution, where is the sum of all components of . The update formula proposed by Loss-Free is:
where is the target uniform distribution. As we have mentioned multiple times, has a redundant degree of freedom: adding the same constant to all components of does not change the sorting result. Given this, we can modify the update rule to:
Here, the overline on a vector denotes the mean of all its components (a scalar), and subtracting a scalar from a vector means subtracting it from each component. This ensures that always satisfies , without changing the load-balancing effect. We can then reserve the degree of freedom for budget control.
How should we understand this? Clearly, if we add the same positive number to all , the probability of satisfying increases, thereby increasing the total budget. So the approach is simple: first compute the current average budget, which is easily seen to be . If it is greater than , decrease slightly; otherwise increase it. Integrating this into Equation :
If we only want to ensure the budget does not exceed , rather than requiring it to equal exactly, we can modify it so that no change is made when :
Attempting Simplification
Examining Equation carefully, we see that it does two things: first, it pushes toward ; second, it pushes toward . This appears to be combinable into a single objective: pushing toward . Thus Equation can be simplified to:
I experimented with both Equation and Equation , and found that their final performance is largely similar. However, Equation exhibits much greater fluctuation in both the load-balance and budget-control metrics during early training. Therefore, readers who prioritize stability may prefer Equation or , while those who prefer simplicity can consider Equation .
Since only preserves the sign of while discarding the magnitude, I also tried replacing with RMS Norm:
Here denotes the square root of the sum of squares of the components. Clearly the RMS of is 1, and after RMS Norm the RMS is also 1, so both updates have the same magnitude and can use the same . Since RMS Norm preserves the relative magnitudes of , smaller errors lead to smaller updates, resulting in slightly less fluctuation than — though the improvement is marginal.
Of course, replacing with RMS Norm for increased stability is a general technique. Equations , , , and can all undergo this replacement — it comes down to personal taste, as the stability gain is slight.
Initialization
Having resolved the update rule for , let us consider the initialization of . This is an interesting but not critically important issue.
With the conventional approach of zero-initializing and using Sigmoid activation for , the initial stage will select all Experts, clearly exceeding the budget. This will cause a great deal of token dropping. However, if we are not overly particular about this, it is not a serious problem: the model’s other parameters typically use warmup while typically does not, so within the first few warmup steps the model will automatically resolve this issue.
If we do care about this, we can control the initial budget by adjusting the initialization of . Suppose the router’s input is a -dimensional vector with zero mean and unit variance (approximately true given RMSNorm), and the router’s weight initialization variance is . Then the router’s logits are approximately zero-mean with variance . With this information, we can use a normal approximation simulation plus binary search to estimate an initial :
import numpy as np
def sigmoid(x):
return 1 / (1 + np.exp(-x))
def b_init(n, k, d, sigma, eps=0.1):
b1, b2 = -1, 0
std = sigma * d**0.5
logits = np.random.randn(10000, n) * std
scores = sigmoid(logits)
while True:
b = (b1 + b2) * 0.5
c = ((scores + b) > 0).sum(1).mean()
if -eps < c - k < eps:
return b
elif c > k:
b2 = b
else:
b1 = b
b_init(32, 4, 1024, 6e-3)The code considers Sigmoid activation, so the search interval is . For other activation functions, please adjust accordingly. The recommendation here is the same as in “MoE Odyssey: Part 3, A Different Approach to Allocation”: the to which is added can uniformly use Sigmoid activation, while the that multiplies the Expert outputs can use a different activation function.
Related Work
Before this article, several works have attempted MoE designs with dynamic Expert selection. Below I briefly list some works I found and offer some brief commentary from a personal aesthetic perspective.
The more straightforward approaches are AdaMoE and MoE++. They mix in some low-computation-cost Experts among the standard ones, such as blank Experts, copy Experts, and constant Experts, while also encouraging load balance. When a token selects one of these simple Experts, it is equivalent to selecting fewer standard Experts, thus indirectly achieving a dynamic count. The advantage is that this can reuse the existing Top- MoE infrastructure, but it also lacks some flexibility.
Another straightforward idea is to replace Top- selection with Top-, from “Harder Tasks Need More Experts: Dynamic Routing in MoE Models”. This conversion looks natural, but in practice it has quite a few problems. For instance, it cannot precisely control the average budget, because when approaches a uniform distribution, the Top- proportion becomes very large. The original paper therefore adds an entropy loss to push away from the uniform distribution. Overall, my personal feeling is that the problems it introduces are more apparent than the benefits.
A rather unique approach is Ada-K Routing, which adds a new module to predict the number of Experts to activate and then trains it with reinforcement learning. This is fine in principle, but introducing reinforcement learning undoubtedly increases training complexity. DA-MoE uses attention scores to identify important tokens and allocates more Experts to them, but this feels insufficiently fundamental — since MoE is not in principle limited to FFN layers, once it is applied to the attention layer, there would be no attention scores available.
The work most similar in form to this article is probably ReMoE. It also uses a zero threshold to select Experts, but it relies on Aux Loss to achieve load balance and budget control, while also mixing in hand-crafted gradient ideas to control the Aux Loss weight. Overall it feels somewhat more patchwork. This article instead continues the Loss-Free philosophy, using the extra degree of freedom of to regulate the threshold, thereby achieving dynamic Expert counts with minimal modifications.
Summary
This article proposes an MoE design with dynamic Expert selection. The main idea is to slightly modify the Loss-Free MoE formulation and then adjust the Bias term’s update rule, leveraging its extra degree of freedom to simultaneously achieve load balancing and budget control.
Part 5: Rethinking Uniform Distribution
By Jianlin Su | 2025-05-16
If Meta’s LLaMA series established the standard architecture for dense models, then DeepSeek is perhaps the one who laid the foundation for the standard MoE architecture. Of course, this does not mean DeepSeek invented MoE, nor that its MoE cannot be surpassed, but rather that some of the improvements DeepSeek proposed for MoE likely represent directions with significant performance gains, gradually becoming standard practice for MoE. These include the Loss-Free load balancing scheme introduced in “MoE Odyssey: Part 3, A Different Approach to Allocation,” as well as the Shared Expert and Fine-Grained Expert strategies that this article will introduce.
Speaking of load balancing, it is undoubtedly an extremely important objective for MoE. Parts 2 through 4 of this series can be said to revolve entirely around it. However, some readers have gradually come to realize that there is an essential question here that remains unanswered: Setting aside efficiency requirements, is uniform distribution necessarily the direction that yields the best performance? This article carries that question forward to understand Shared Expert and Fine-Grained Expert.
Shared Expert
Let us once again review the basic form of MoE:
In addition, the Loss-Free method from “MoE Odyssey: Part 3” replaces with , and in “MoE Odyssey: Part 4, Invest More Where It’s Difficult” we generalized it to . However, these variants are all orthogonal to the Shared Expert technique, so we will only use the most basic form as our example going forward.
Shared Expert modifies the above equation to:
That is, the original top- out of selection is changed to top- out of , while the other experts are always selected — these are called “Shared Experts.” When this concept first appeared, we jokingly called them “permanent members of the Security Council.” The remaining experts are called “Routed Experts.” The number of Shared Experts is not large, typically 1 or 2; too many would cause the model to “neglect” the remaining Routed Experts.
It should be noted that with or without Shared Experts, the total number of experts remains and the number of activated experts remains , so Shared Expert in principle does not increase model parameter count or inference cost. Nevertheless, DeepSeekMoE and our own experiments show that Shared Expert can still improve model performance to a certain extent.
Multiple Interpretations
We can understand Shared Expert from multiple perspectives. For example, from the residual perspective, the Shared Expert technique effectively changes the learning target from each individual expert to the residual between each expert and the Shared Expert. This reduces learning difficulty and provides better gradients. In DeepSeek’s words: by compressing common knowledge into these Shared Experts, redundancy among Routed Experts is reduced, parameter efficiency is improved, and each Routed Expert is ensured to focus on unique aspects.
If we compare Routed Experts to subject teachers in a middle school, then the Shared Expert is like the “homeroom teacher.” If a class only has subject teachers, each one will inevitably share some administrative work. Setting up the homeroom teacher role concentrates these common administrative tasks in one person, allowing subject teachers to focus on teaching their subjects and improving teaching efficiency.
We can also understand this from a geometric perspective. The unavoidable commonality among experts means geometrically that their vectors have angles less than 90 degrees, which contradicts the “pairwise orthogonal” assumption for expert vectors that we used when proposing the geometric interpretation of MoE in “MoE Odyssey: Part 1, Starting from Geometric Meaning.” Although this assumption can still be understood as an approximate solution when it does not hold, it is naturally better when it does hold. We can interpret the Shared Expert as the mean of the Routed Experts; by learning the residuals after subtracting the mean, the orthogonality assumption becomes easier to satisfy.
Scaling Factor
We write the Shared Expert equation more generally as:
Because Routed Experts carry weights while Shared Experts do not, and because the number of Routed Experts is typically much larger than the number of Shared Experts (i.e., ), the scale between the two may become imbalanced. Therefore, to prevent one from overwhelming the other, setting an appropriate is particularly important. In this regard, we proposed in “Muon is Scalable for LLM Training” that a proper should make the norms of the two components approximately equal during the initialization phase.
Specifically, we assume that each expert has the same norm during initialization (without loss of generality, we can set it to 1) and satisfies pairwise orthogonality. We then assume the router’s logits follow a standard normal distribution (i.e., zero mean, unit variance; of course, one could consider other variances if deemed necessary). Under these assumptions, the total norm of the Shared Experts is , while the total norm of the Routed Experts is:
By setting this equal to , we can estimate . Since different MoE routers can vary considerably due to choices of activation function, whether to renormalize, etc., we do not attempt to find an analytical solution but instead use numerical simulation directly:
import numpy as np
def sigmoid(x):
return 1 / (1 + np.exp(-x))
def softmax(x):
return (p := np.exp(x)) / p.sum()
def scaling_factor(n, k, s, act='softmax', renorm=False):
factors = []
for _ in range(10000):
logits = np.random.randn(n - s)
p = np.sort(eval(act)(logits))[::-1][:k - s]
if renorm:
p /= p.sum()
factors.append(s**0.5 / (p**2).sum()**0.5)
return np.mean(factors)
scaling_factor(162, 8, 2, 'softmax', False)
scaling_factor(257, 9, 1, 'sigmoid', True)Quite coincidentally, the simulation results from this script align very well with the settings of DeepSeek-V2 and DeepSeek-V3. For DeepSeek-V2 with , using softmax activation without renormalization, the above script’s simulation result is approximately 16, and DeepSeek-V2’s is exactly 16 (source). For DeepSeek-V3 with , using sigmoid activation with renormalization, the script’s result is approximately 2.83, while DeepSeek-V3’s is 2.5 (source).
Non-Uniformity
Returning to the question posed at the beginning: is balance necessarily the direction that yields the best performance? It appears that Shared Expert provides a reference answer: not necessarily. Because Shared Expert can also be interpreted as certain experts always being activated, the overall result is a non-uniform expert distribution:
In fact, non-uniform distributions are ubiquitous in the real world, so it should be easy to accept that uniform distribution is not the optimal direction. Using the middle school teacher analogy again, the number of teachers for each subject within a school is actually non-uniform — there are typically the most for Chinese, math, and English, followed by physics, chemistry, and biology, with even fewer for physical education and art (and they are frequently “sick”). For more examples of non-uniform distributions, one can look up Zipf’s law.
In summary, the non-uniformity of the real world inevitably leads to non-uniformity in natural language, which in turn leads to the sub-optimality of uniform distribution. Of course, from the perspective of training models, uniform distribution is still easier to parallelize and scale. Therefore, separating out some Shared Experts while still hoping the remaining Routed Experts are uniform is a compromise that is friendly to both sides for achieving non-uniformity, rather than directly forcing Routed Experts to align with a non-uniform distribution.
The above discussion concerns training — what about inference? During the inference phase, one can estimate the actual distribution of Routed Experts in advance, and there is no need to consider backpropagation. So with careful optimization, it is theoretically possible to achieve no loss in efficiency. However, since current MoE inference infrastructure is all designed for uniform distribution, and there are practical constraints such as limited single-GPU memory, we still prefer Routed Experts to be uniform for better inference efficiency.
Fine-Grained Expert
Besides Shared Expert, another improvement proposed by DeepSeekMoE is Fine-Grained Expert. It points out that with both total parameters and activated parameters held constant, finer expert granularity generally leads to better performance.
For example, suppose we originally have top- out of Routed Experts. Now we shrink each expert to half its size and switch to top- out of . The total parameters and activated parameters remain the same, but the latter typically performs better. The original paper’s explanation is that this enriches the diversity of expert combinations:
Of course, we can have other interpretations — for instance, by splitting experts into smaller units, each expert can focus on a narrower domain of knowledge, achieving more refined knowledge decomposition, and so on. However, note that Fine-Grained Expert is not without cost: as grows larger, load imbalance among experts tends to worsen, and communication and coordination costs between experts also increase. Thus cannot grow without bound; there is a sweet spot where both performance and efficiency are favorable.
Regarding the effectiveness of Fine-Grained Expert, the author here proposes another less obvious explanation that relates to the theme of this article: A larger number of finer-grained experts can better approximate the non-uniformity of the real world. Consider the illustration below: suppose knowledge can be divided into one large category and one small category, and each expert is a circle. If we use 2 large circles to cover them, there will be some omission and waste. But if we instead use 8 small circles with the same total area, the coverage can be much more precise, and thus performance is better.
[Figure: Fine-grained coverage is more precise]
Article Summary
This article introduced the Shared Expert and Fine-Grained Expert strategies for MoE, and pointed out that both, to some extent, reflect the sub-optimality of load balancing toward uniform distribution.
Part 6: Optimal Assignment for Load Balancing
By Jianlin Su | 2026-02-22
We know that Load Balance is a fundamental and critical component of the MoE architecture, directly affecting model efficiency and performance. This series has already introduced two mainstream approaches to achieving load balancing in two previous articles: the classic Aux Loss approach presented in “MoE Odyssey: 2, Inequality Rather Than Scarcity Is the Problem,” and the Loss-Free approach proposed by DeepSeek in “MoE Odyssey: 3, A Different Approach to Assignment.” Each has its strengths and limitations.
This article will explore a third approach: optimal assignment, which treats load balancing as a linear programming problem under equality constraints. In its final form, it still belongs to the Loss-Free category, but it is based on fundamentally different principles and provides a more accurate, hyperparameter-free update method.
Method Review
Among the two existing methods, the Aux Loss approach is relatively straightforward — its core idea is “penalize wherever imbalance occurs,” imposing penalties on load imbalance through regularization terms. However, Aux Loss has two problems: first, the penalty coefficient is difficult to tune — too large and it interferes with the main loss optimization, too small and the balancing effect is poor; second, Aux Loss relies on STE (Straight-Through Estimator), which means its gradients are suboptimal and may introduce unknown effects beyond load balancing.
To address this, DeepSeek proposed the second approach, Loss-Free, which introduces an additional bias term to assist with ranking, as shown below:
Note that is only used to adjust the ranking of Experts — what gets multiplied onto the Expert output is still — so it does not directly participate in the model’s computation and will not produce interfering gradients. However, the fact that has no gradient means we need to design a custom update rule for it. The intuition is straightforward: the larger is, the higher the probability that the -th Expert gets selected. So it first computes the current load distribution ; if exceeds the expected value , then is decreased, otherwise is increased:
Overall, Loss-Free is less “invasive” to the model and can indeed be called more concise and elegant, but it is not without flaws. Although it does not have the penalty coefficient of Aux Loss, it still has a parameter to tune — this is essentially the learning rate for . The paper recommends , which is actually tightly coupled with using Sigmoid activation for ; once you switch to a different activation function, needs to be re-tuned.
Furthermore, even with Sigmoid activation, some layers have rather “pathological” distributions of , making the model sensitive to , and a constant struggles to achieve load balance. This situation is not uncommon — for instance, if the first few layers of a model use MoE, they are often difficult to balance, which is why the “first_k_dense” operation exists; similarly, when the model is large or the total number of experts is large, individual layers may also be difficult to balance.
Linear Programming
Let us describe the problem more precisely: suppose there are tokens, and the -th token’s router scores for experts are , giving a total of scores. These scores may be positive or negative and are not necessarily within a predefined range. We wish to devise an assignment scheme based on these scores, determining which experts each token should activate.
We stipulate that each token selects experts. A basic approach is to select the experts with the highest scores, but this may lead to load imbalance — certain experts may be activated significantly more or fewer times than others. So we further stipulate that each expert is activated exactly times. Under these two constraints, we seek the assignment scheme that maximizes the total score, formalized as:
Here indicates that the -th token selects the -th expert, and means it does not. Note that must be an integer for both equality constraints to hold strictly — we assume this condition is satisfied. Having each expert activated exactly times represents the ideal perfectly uniform state; in practice one can certainly relax this, but for theoretical derivation we adopt the strictest constraint.
Since can only take values 0 or 1 in the above, this is an integer programming problem. Discrete optimization is generally difficult, so we consider its relaxed version:
Now can take any real value in , with all other conditions unchanged. Since is linear in both the objective function and the constraint equations, this is a linear programming problem over a bounded region.
Minimax
Constrained optimization is generally not easy either, so we consider the unconstrained - form (i.e., the “Lagrange multiplier method”):
If and , then the above is equivalent to equation , and the maximum is a finite value; but if either constraint is violated, the step can drive the value to negative infinity, making the maximum negative infinity. Since negative infinity is not as large as a finite value, only the first case can occur — meaning this is equivalent to equation .
The objective is linear in , , and . A linear function is both convex and concave, and is a convex set, so it satisfies the conditions of the Minimax theorem. We can therefore swap the order of and , obtaining:
In the above, we have separated the terms involving , , and . Upon careful inspection, the step can actually be solved directly: when , we set to maximize the objective; when it is less than 0, we set ; when it equals 0, can take any value with the same result. That is:
The case is a special situation. Assuming its probability of occurrence is negligible, is either 0 or 1. Of course, even when , due to the arbitrariness of , we can set it to 0 or 1 according to the constraints. This shows that although form is a relaxation of the original problem , its optimal solution is also the optimal solution of the original problem — the two are completely equivalent.
Divide and Conquer
Substituting the above into equation , we get , so the optimization objective simplifies to:
We will use alternating minimization to solve this: first fix and solve for , then fix and solve for , alternating between the two. Since and have obvious symmetry, these two steps are essentially the same problem. Let us first look at fixing and solving for ; the problem is equivalent to:
We can also observe that each is accumulated independently, so we can decompose this into independent sub-optimization problems. This means we can temporarily drop the subscript and further simplify the problem to:
Sort all in descending order as , where the -th largest element is . Now suppose we know that ; then the objective function equals:
This shows that whether or , the objective function is amplified. Therefore the minimum can only be achieved at , at which point the result does not depend on the specific value of — meaning can be any number between the -th largest and -th largest elements of . By convention, we take to be the -th largest element.
Alternating Iteration
Restoring the subscript , we obtain: for any given , is the -th element when all are sorted in descending order. Similarly, fixing and solving for yields: for any given , is the -th element when all are sorted in descending order. We alternate between these two steps as the final solution algorithm.
Suppose we have obtained sufficiently accurate and . Then from the preceding analysis, automatically satisfies the 0-or-1 property and the constraints. Since corresponds to , combined with the constraint , we can conclude that for each token , the selected experts are necessarily the Top- of .
This tells us that only is needed during inference — is merely an intermediate variable in the solution process that can be discarded after training. This point is crucial because the size of is a fixed , while the size of is (the global batch size), which changes dynamically and is therefore not a suitable inference format. The solution procedure leveraging this property is shown below, where denotes descending sort.
An additional improvement is that we can use the concept of “quantile” to unify “the -th largest element among numbers” and “the -th largest element among numbers” — they are both the ” quantile” along their respective dimensions. Numerical frameworks such as NumPy, JAX, and PyTorch all have a “quantile” function implementation; using it avoids performing a full sort of the data, saving some computational complexity.
It is precisely for this reason that we call this algorithm Quantile Balancing (QB).
Watch Out for the Trap
But it is not time to celebrate yet — there is an inconspicuous trap here that, if fallen into, may yield fundamentally incorrect results and requires special caution.
As mentioned earlier, QB inference only uses , and storing only during training suffices as well. So from the Loss-Free perspective, QB provides a new bias update method — calling it “Quantile Bias” would not be inappropriate. Whether it is the original SignSGD or the Quantile method in this article, they both depend on the scores of all tokens, so the order must not be confused: one must use the old to select experts for the current batch of data, and only then update the value — this ensures there is no information leakage problem.
Some readers might wonder: how can a bias vector that does not directly participate in forward computation leak anything? Indeed, intuitively the leaked information is very little, but the risk nonetheless exists. Perhaps for training small models we can try the leaking version, but for training large models one should not take this risk — because large models are powerful, powerful enough to amplify any subtle bug. Therefore, maintaining training-inference consistency and eliminating any information leakage risk is a fundamental principle when training large models.
Combining with actual training scenarios, QB can be further adjusted: instead of zero-initializing and iterating times, we can start from the previous step’s bias and iterate only once per step. This avoids overfitting to the current batch while reducing computation. Selecting Top- based on is something the original MoE already does; now it merely changes to selecting Top-, adding almost no cost. So the additional step is using to find the ” quantile” along .
However, even with just one iteration, this additional step is still relatively expensive — it needs to find the -th largest element among elements, where equals “global number of samples times sequence length,” typically starting at millions. Under various parallelism strategies and gradient accumulation, exact implementation is usually unacceptable. A compromise is to divide the samples into the largest mini-batches we can accept, compute a separate for each mini-batch according to the formula, and then average them as the final result.
After resolving these issues, QB is almost entirely advantageous: first, it has no hyperparameters like learning rates to tune; second, it balances extremely fast and is especially good at handling extreme cases — for example, using it to train a full-MoE model, even the first MoE layer becomes very well balanced. However, for layers where the original SignSGD rule already achieves balance, QB typically offers no additional advantage.
Demo Code
Here is a demo code snippet for interested readers to experiment with:
import numpy as np
def quantile_bias(s, k, T=5):
"""Alternating quantile to find optimal bias
Principle: https://kexue.fm/archives/11619
"""
m, n = s.shape
beta = np.zeros((1, n))
for _ in range(T):
alpha = np.quantile(s - beta, 1 - k / n, axis=1, keepdims=True)
# alpha = alpha.clip(0, np.inf) # BIP would add this step
beta = np.quantile(s - alpha, 1 - k / n, axis=0, keepdims=True)
# beta = beta.clip(0, np.inf) # BIP would add this step
return beta
def max_min_avg_vio(s, k):
"""Compute max_vio, min_vio, and avg_vio
Where max_vio >= 0, avg_vio >= 0, -1 <= min_vio <= 0; all three closer to 0 means better balance
"""
m, n = s.shape
topk = np.argsort(-s, axis=1)[:, :k]
f = np.bincount(topk.reshape(-1), minlength=n)
f = f / f.sum() * n - 1
return f.max(), f.min(), np.abs(f).mean()
m, n, k = 100000, 256, 8
s = np.random.rand(m, n) + np.random.rand(n) # Simulate non-uniform scores
b = quantile_bias(s, k, 5)
max_min_avg_vio(s, k) # max_vio, min_vio, avg_vio with direct top-k
max_min_avg_vio(s - b, k) # max_vio, min_vio, avg_vio with top-k after subtracting biasRelated Work
The earliest work viewing MoE load balancing from the optimal assignment perspective is the paper “BASE Layers: Simplifying Training of Large, Sparse Models,” but the complete general solution was proposed in the paper “Binary-Integer-Programming Based Algorithm for Expert Load Balancing in Mixture-of-Experts Models” (abbreviated as BIP). QB is an improvement upon BIP.
Compared to QB’s objective , BIP changes the equality constraints to inequality constraints:
Then repeating the preceding series of derivations, at the - form step, an additional constraint appears — that is, we must consider:
for it to be equivalent to the relaxed version of problem . Next, and can still be swapped, and repeating a similar derivation process, the iteration rules for and gain an additional truncation operation to ensure non-negativity:
However, the author’s testing found that this truncation operation significantly slows down load balancing, and frequently produces cases where it can only “rob the rich” but cannot “help the poor” (experts with extremely high frequency get suppressed, but experts with extremely low frequency cannot be rescued). This indicates that the truncation operation is entirely counterproductive. Therefore, QB directly changes the original problem to equality constraints, which removes the non-negativity constraints on and , making the solution maximally simple.
Additionally, BIP appears to commit the mistake mentioned in the previous section — its approach is to first update and then select Top-, which violates the causal principle (leaking future information) and the training-inference consistency principle (across samples). Nevertheless, BIP’s complete analysis of load balancing from the optimal assignment perspective remains highly instructive.
After searching, the author found that after BIP, several other works have explored this direction. They overlap somewhat with this article but are not entirely identical; we will not discuss each one in detail here:
“Maximum Score Routing For Mixture-of-Experts”2
“Selective Sinkhorn Routing for Improved Sparse Mixture of Experts”3
“MicroMoE: Fine-Grained Load Balancing for Mixture-of-Experts with Token Scheduling”4
“A Theoretical Framework for Auxiliary-Loss-Free Load Balancing of Sparse Mixture-of-Experts in Large-Scale AI Models”5
Gradient Descent
Finally, we introduce a solution approach that lies between Loss-Free and QB. We know that in QB’s iteration format, computing is relatively cheap — what is truly expensive is computing , which requires performing a certain kind of sorting across all tokens. Suppose is given; then the optimization objective for is:
Besides using Quantile to find its optimal solution, is there a cheaper approach to find an approximate solution? Indeed there is! The objective function is clearly differentiable, so we can consider gradient descent. Its gradient is:
where is the indicator function with . Clearly this gradient is also relatively cheap to compute. With the gradient in hand, we can perform gradient descent. To align with Loss-Free, we consider SignSGD:
Using this to replace the Quantile update of in QB achieves cost comparable to Loss-Free, with empirical performance between the two (under the same Sigmoid activation and the same as Loss-Free). In fact, we can further prove that if the -th largest and -th largest elements in each row of the score matrix are never equal, then this approach is strictly equivalent to Loss-Free.
Conclusion
In this article, we explored the load balancing problem of MoE from the optimal assignment perspective and derived a new Aux-Loss-free load balancing algorithm called Quantile Balancing. It is more stable and accurate than existing Loss-Free approaches, is applicable to router scores of arbitrary range, and has no extra hyperparameters to tune.
Part 7: A Minimalist Solution for Dynamic Activation
By Jianlin Su | 2026-02-23
In the previous article, “MoE Odyssey: Part 6, Optimal Assignment for Load Balancing,” we achieved load balancing by solving the following optimal assignment problem:
where means each token activates exactly experts, and means each expert is activated exactly times. However, upon careful reflection, the former constraint is actually unnecessary for both training and inference. What we truly need is the latter, which implies that “on average each token activates experts” along with load balancing across experts. This is sufficient to achieve the goals of MoE, so in this article we consider the simplified problem:
Dynamic Activation
Assuming the reader is already familiar with the previous article, the derivation here will be relatively brief. If any steps are unclear, please refer back to the previous installment for review. As before, we first consider the relaxed version of objective :
Then we consider its equivalent - form:
Similarly, the order of and is interchangeable here. Swapping them and rearranging slightly gives:
The step can be completed first, yielding:
This tells us that any expert with is activated, and the number of activations is not fixed. This is formally consistent with “MoE Odyssey: Part 4, Invest More Where It’s Hard,” except that previously we designed it by intuition, whereas here it is derived from the more fundamental objective .
One-Step Solution
Substituting back into gives , so the optimization objective for simplifies to:
Each term is independent, so this decomposes into independent sub-optimization problems. This means we can temporarily drop the subscript :
Sort all in descending order as , then assume we already know . We can then remove the to get:
This shows that both and increase the objective, so the minimum is attained at . That is, lies between the -th and -th largest elements of . By convention, we take the -th largest element. Restoring the subscript : for a given , is the -th largest element when is sorted in descending order, or equivalently, the ” quantile.”
Note that the only unknown variable is now , so there is no alternating iteration step — a single Quantile computation directly yields the perfectly balanced optimal solution! We continue to call this Quantile Balancing (QB).
Practical Considerations
If the reader is already familiar with “MoE Odyssey: Part 6, Optimal Assignment for Load Balancing,” they will notice that this article is essentially a streamlined version of it. However, simplicity does not mean triviality — it is actually more elegant and efficient than Top- style MoE: first, it activates experts by checking whether is greater than 0, which is more efficient than selecting Top-; second, it obtains the load-balanced optimal solution in a single Quantile step, which is also more efficient and elegant.
Of course, a one-step optimal solution is very likely to overfit the current training batch. To avoid this problem, the improved approach considered here is to apply EMA (Exponential Moving Average) between the current batch’s optimal solution and the historical solution (the Top- version from the previous article can also use EMA). Note that we must still be careful about the information leakage trap: we must first use the old to activate experts, and only then update .
The algorithm flow is as follows (in the author’s experiments, ):
In practice, we similarly face the issue that finding the quantile of is expensive — it requires finding the -th largest element among elements, where equals “global batch size * sequence length.” Due to various parallelism strategies and gradient accumulation constraints, an exact implementation is typically unacceptable. So we again consider a compromise: split the samples into the largest mini-batches we can afford, compute a separate for each mini-batch, and average them as the final result.
Expert Choice
In fact, this dynamic version of QB has a very simple interpretation, namely “Expert Choice”:
You want each expert to be activated exactly times? Then just let the experts choose the tokens! That is, each expert selects its Top- tokens — which is simply finding the largest elements along .
However, naive Expert Choice has a fatal flaw: it requires comparing all tokens globally, leading to cross-sample, cross-sequence selection. This violates causality (seeing future information during training) and breaks train-inference consistency (inference results depend on batch size). This is precisely why this method has been difficult to deploy in practice.
The elegance of this article lies in converting it into a bias form: using each expert’s -th largest element (equivalently, the quantile) as the threshold , we reformulate Expert Choice as Token Choice — from “expert selects Top-” to “activate whenever .” More critically, we delay the update of until after the activation decision, perfectly avoiding the information leakage problem.
Remarkably, this bias form that fixes the defects of Expert Choice emerges naturally from the dual problem of the original linear program — one cannot help but appreciate its aesthetic beauty.
Initialization Strategy
To prevent information leakage, during training we need to first use the old to determine which experts each token activates, and only then update . This makes the first few training steps sensitive to the initialization of . For example, if is entirely positive but is zero-initialized, then on the first training step every token would activate all experts, causing computation to explode.
In “MoE Odyssey: Part 4, Invest More Where It’s Hard” we already provided a simulation script to estimate a suitable initialization, but here we already know that the optimal is the ” quantile.” We can therefore make some reasonable assumptions about the initial scores and derive an analytical initialization formula. Specifically, we assume the router’s initial logits follow , so its quantile is:
This is precisely the optimal for logits from this distribution, and we use it as the initialization for , where is the quantile function (inverse CDF) of the standard normal distribution.
Additionally, the router may include an activation function. If the activation function is element-wise and monotonically increasing, such as Sigmoid, it does not change the ordering, so we simply apply the corresponding activation function to the above expression. For Softmax it is slightly more complex: it first exponentiates then normalizes. Assuming the normalization denominator is approximately constant, it becomes similar to the previous case — we just need to exponentiate the above expression and divide by a denominator estimated via quantile-based simulated sampling:
As for estimating , it is quite straightforward. Assume the router’s input dimension is with RMS Norm applied, and that the router’s weight matrix is initialized from . Then the router logits approximately follow a normal distribution with mean approximately 0 and variance approximately , i.e., . These are classical results from initialization theory.
Demo Code
This section again provides demonstration code for readers to experiment with:
import numpy as np
def quantile_bias(s, k):
"""Dynamic QB: one-step quantile yields the optimal bias
Reference: https://kexue.fm/archives/11626
"""
m, n = s.shape
beta = np.quantile(s, 1 - k / n, axis=0)
return beta
def max_min_avg_vio(s):
"""Compute max_vio, min_vio, and avg_vio
where max_vio >= 0, avg_vio >= 0, -1 <= min_vio <= 0;
all three closer to 0 means better balance
"""
m, n = s.shape
f = (s > 0).mean(0)
f = f / f.sum() * n - 1
return f.max(), f.min(), np.abs(f).mean()
def avg_std_active(s):
"""Compute the mean and std of activation counts per expert
avg closer to k is better, std closer to 0 is better
"""
m, n = s.shape
f = (s > 0).mean(0) * n
return f.mean(), f.std()
m, n, k = 100000, 256, 8
s = np.random.rand(m, n) + np.random.rand(n) # Simulate non-uniform scores
b = quantile_bias(s, k)
max_min_avg_vio(s - b) # Should be all zeros
avg_std_active(s - b) # Should be (k, 0)
from scipy.stats import norm
sigma = 1
s = np.random.randn(m, n) * sigma # Simulate initialization
avg_std_active(s) # Approximately (128, 0.4), far exceeding budget k
b0 = np.zeros(n) + norm.ppf(1 - k / n) * sigma # norm.ppf is the standard normal quantile function
avg_std_active(s - b0) # Approximately (8, 0.1), close to budget k
s2 = 1 / (1 + np.exp(-s)) # Sigmoid activation
b0_2 = 1 / (1 + np.exp(-b0)) # Sigmoid activation
avg_std_active(s2 - b0_2) # Still approximately (8, 0.1), close to budget k
s3 = np.exp(s) / np.exp(s).sum(axis=1, keepdims=True) # Softmax activation
q = (np.arange(n) + 1) / (n + 1) # Uniformly spaced points (quantiles)
b0_3 = np.exp(b0) / np.exp(sigma * norm.ppf(q)).sum() # Exp activation divided by simulated denominator
avg_std_active(s3 - b0_3) # Approximately (7.5, 0.1), close to budget kGradient Descent
Finally, if one wants to avoid Quantile computation entirely, we can still consider gradient descent. First, denote the loss function from objective as :
It is differentiable, with gradient:
where is the indicator function: . Computing this gradient is cheaper than a global Quantile, making it suitable for readers who are more cost-sensitive. With the gradient in hand, we can perform gradient descent. We again consider SignSGD, aligning with Loss-Free:
This can replace the Quantile update step for , and since this is inherently a long-term iterative algorithm, the EMA can also be removed. In fact, this is precisely Equation (9) from “MoE Odyssey: Part 4, Invest More Where It’s Hard” — we have re-derived it from the perspective of “optimal assignment + dual objective + gradient descent.”
Summary
This article continues the exploration of Quantile Balancing (QB) from the previous installment. By removing the constraint “each token can only activate experts” from the optimal assignment problem, the solution form is significantly simplified — only a single Quantile step is needed to achieve load balancing. Each token only needs to check the sign of the biased score to decide which experts to activate, eliminating the overhead of Top- sorting.
Part 8: Where Does DeepSeek V4’s tid2eid Come From?
By Jianlin Su | 2026-05-15
Anyone who has trained MoE models knows that if you replace the entire MLP portion of the model with MoE, the first few MoE layers closest to the embedding are often very difficult to load-balance. To address this, DeepSeek V3, including our Kimi K2, adopted the strategy of “first_k_dense” — as the name suggests, the first layers use conventional dense GLU instead of MoE. By DeepSeek V4, this strategy was changed to “first_k_hash”.
The “hash” here refers to “Hash Routing”, proposed in Hash Layers For Large Sparse Models, which assigns an Expert to each Token via a pre-determined mapping table called tid2eid. This article explores how to generate this tid2eid.
Background
First, it should be pointed out that for the first few MoE layers, if one uses the Quantile Balancing method proposed by the author in “MoE Odyssey: 6. Optimal Assignment for Load Balance” and “MoE Odyssey: 7. A Minimalist Solution for Dynamic Activation”, the imbalance problem can already be largely mitigated.
As for V4’s adoption of Hash Routing, it can be seen as an attempt in a different direction for the same problem. The idea is that for the first few MoE layers, there is not much contextual information, so selecting Experts based on input vectors may not differ much from selecting directly based on Token Id, which carries no contextual information at all. Since this is the case, we might as well hard-code which Experts each Token should activate in the first few layers based on Token Id — this is the origin of “tid2eid (Token Id to Expert Id)“.
So how do we generate this tid2eid table? Can we just do it completely randomly? That doesn’t seem to work, because each Token’s occurrence frequency differs — purely random assignment would actually lead to imbalance. DeepSeek has not published the details of how this table is generated, so we can only speculate based on our own reasoning.
Mathematical Formulation
Suppose there are distinct Tokens, and the -th Token has occurrence frequency . Without loss of generality, assume they are sorted in descending order, i.e., . We use to denote whether Token should activate Expert ( means not activated, means activated). There are Experts in total, and each Token selects Experts. Then we actually need to solve the system of equations:
Note that for the last equation we used the approximate sign . This is because if we change it to , the system of equations may not strictly have a solution. So we keep the notation, meaning the two sides should be as close as possible. We can also write it in optimization form:
A fairly straightforward solution to this problem is a greedy algorithm:
Process Tokens one by one from highest to lowest frequency. First compute each Expert’s currently assigned load, then select the least-loaded Experts as the activated Experts for the current Token, and finally update the Expert load distribution.
Reference Implementation
Although the greedy algorithm is greedy in principle, in most cases it yields a near-optimal solution. A reference implementation is as follows:
import numpy as np
# Simulated distribution
m, n, k = 80000, 128, 4
p = 1 / (10 + np.arange(m))
p /= p.sum()
# Greedy processing
x, f = np.zeros((m, k), dtype='int32'), np.zeros(n)
for i in range(m):
j = f.argsort()[:k] # Select the k least-loaded
f[j] += p[i] / k # Update load distribution
x[i] = j # Record in tid2eid
# Evaluate balance
max_vio, min_vio = f.max() * n - 1, f.min() * n - 1Note that this code contains no randomness whatsoever — it is in principle a deterministic algorithm. What if we want different tid2eid tables for different early layers? We can consider adding some randomness, for example replacing range(m) with np.random.permutation(m), i.e., not processing in descending frequency order. This still yields a usable solution, just with slightly worse balance. We can run it multiple times and pick the solutions with the lowest max_vio.
Extreme Cases
Now let us consider an extreme case: , meaning some Token’s frequency far exceeds the balanced level. In this case, no matter how tid2eid is arranged, load balance is impossible to achieve. In other words, making decisions based solely on a single Token Id can never achieve balance. How should we handle this situation?
The answer is simple: switch to a Hash method that depends on more input information. Previously we made decisions based only on the current Token Id, but in reality each Token exists within a sequence and has context. If the previous approach was “1-gram to expert”, we can now consider combining it with preceding Tokens to form “2-gram to expert”, “3-gram to expert”, and so on.
Taking the 2-gram as an example, a simple Hash method is: choose a prime greater than , compute as the activated Expert, and repeat times with different primes to get Experts. Because increasing the gram count greatly increases the number of input combinations, and these map to a finite set of values, each value is very likely to be “filled up”. So as long as the Hash function is not particularly bad, the result is almost perfectly balanced.
Therefore, the advantage is that there is no need to consider frequencies, nor to store a tid2eid table in advance. As for the disadvantage, the Hash function computation becomes slightly more complex, and the same Token might be assigned to duplicate Experts — but neither of these is a particularly serious problem.
Summary
This article briefly reviews the basic idea of Hash Routing in DeepSeek V4, with a focus on the construction principle of its tid2eid mapping table.
Part 9: The Gate Normalization Debate
By Jianlin Su | 2026-06-28
Looking back at MoE history, we find that in the early years the Router almost universally used Softmax activation when serving as a Gate multiplied onto the Expert output, and it remains one of MoE’s standard forms to this day. However, to accommodate the Loss-Free load balancing scheme, DeepSeek switched the activation function to Sigmoid and demonstrated that this is also a highly competitive approach, sparking deeper exploration and experimentation with the Router’s form.
Even within Softmax there are two subtly different practices: apply Softmax first and then select Top-, or select Top- first and then apply Softmax? The latter can also be understood as re-normalizing after selecting the Top-, i.e., Re-Norm. So: should the Gate’s activation function normalize? And if so, should we normalize before Top- selection or Re-Norm after? This is the topic of the present article.
Problem Statement
We know that the general form of MoE is
Here actually plays two roles: when used to select the Top- Experts, its role is the Router; when multiplied onto the Expert output, its role is the Gate. From the perspective of MoE design, ‘s core role is clearly the Router, while the Gate’s purpose is to provide gradients to the Router during training.
The question we discuss can also be understood as: how to more scientifically construct so that the Router receives better gradients. For a long time, the standard answer has been Softmax:
where are the logits projected directly by a linear layer. However, while this answer is “standard,” the author has not found any real explanation for it — everyone seems to simply accept and perpetuate it, which left the author long puzzled about the training mechanism of MoE.
Other Choices
As mentioned in the introduction, DeepSeek tried Sigmoid activation for Loss-Free load balancing, later using it in DeepSeek-V3. Its success demonstrates that non-Softmax activation can also work well, inspiring people to try more general approaches. For example, ReMoE uses ReLU activation, and the geometric perspective from “MoE Odyssey: Part 1, Starting from Geometric Interpretation” allows any non-negative activation function.
Additionally, regarding MoE’s form, there is the Re-Norm option, which modifies (MoE-1) to:
That is, re-normalizing the selected Top- values. For Softmax, this is equivalent to selecting Top- from , setting the unselected entries to , and then applying Softmax. Re-Norm’s advantage is making the forward computation numerically more stable, but note that with Re-Norm, must be greater than 1 — otherwise will have no gradient at all and cannot be trained.
Taking stock of current practice across various groups, these MoE variants all perform roughly similarly, with none clearly dominant. Since practice cannot distinguish a winner, let us explore theoretically which form is more scientifically justified.
Design Principles
Our goal is to find a first principle that is closer to the essence, and use it to derive the gating mechanism of current MoE.
So the first question is naturally: what is this “principle”? For simplicity, first consider . We know that MoE’s most important feature is sparsity — first determine which Expert to activate via a Router, then compute only that Expert, thereby increasing parameter count while controlling computation. If this were the sole consideration, the naive model would be:
That is, pick the highest-scoring Expert from the Router and activate it. This form works perfectly for inference, but during training the Router receives no gradient and cannot be updated, so we must find a way to design gradients for the Router. How do we design gradients for the Router? To answer this, we must first think clearly about: what kind of Router do we want?
Since we can only activate 1 Expert, we naturally want it to be the best-performing one. If denotes the loss function, our desire can be written as:
This is the design principle we are looking for.
Objective Transformation
However, the target (Target) is not yet a loss function we can directly use for training — it requires further transformation. To this end, we construct two distributions. The first is a target distribution built from the loss function:
This distribution is independent of the Router; for the Router’s learning it serves as the “target distribution.” The second distribution is a predicted distribution built from . There are many possibilities here: itself might already be a distribution (if is already normalized), or could be the Softmax of (treating as logits), or some other normalization scheme. In short, is some probabilistic representation of the Router; let its parameters be .
We transform the target (Target) into bringing closer to , thereby providing gradients for . For this, we consider minimizing the KL divergence:
With slight rearrangement:
This objective has three terms. The first is negative entropy ; minimizing it means maximizing entropy, which encourages the model to explore sufficiently — we can consider that load balancing already plays a similar role, so we set it aside. The third term is independent of (and thus of ). So the effective loss function is:
Straight-Through Estimation
Taking the gradient of this effective loss:
The key step here is using to isolate a factor of , enabling the sum to be converted into an expectation, which can then be estimated via sampling to achieve MoE’s sparse computation goal. Some readers may have already recognized this — it is precisely REINFORCE from policy gradients! (See “Optimization Through Sampling: A Unified View of Differentiable and Non-Differentiable Optimization”, “Policy Gradients and Zeroth-Order Optimization: Converging Paths”)
The problem with REINFORCE is high noise. Intuitively, this is because it places outside the loss function ; if possible, we would prefer a “reparameterization” form with inside . To derive such a form, we use REINFORCE’s invariance to baseline subtraction:
Here the applies a first-order Taylor approximation at , and denotes stop-gradient. We arrive at a Straight-Through Estimator (STE) that “uses 1 in the forward pass and in the backward pass” to provide gradients for the Router.
The Final Form
While STE provides a workable training scheme, the inconsistency between forward and backward passes typically yields only suboptimal results. Here a remarkably elegant improvement is to change each Expert from to ! Repeating the above derivation:
This transformation is exquisite and worth savoring carefully. By changing the Expert from to , we eliminate the stop-gradient, achieving consistency between forward and backward passes, and theoretically raising the ceiling on model performance.
Now we can answer the question posed at the beginning:
If we desire a top-down probabilistic derivation, then the Router when serving as Gate should be normalized, but should not use Re-Norm.
To Sample or Not to Sample
A detail worth noting: implies we should sample from , but in practice we usually just select Top- directly. How should we understand this discrepancy?
This is fundamentally a tradeoff between diversity and stability. Random sampling encourages the model to explore more fully, but introduces additional gradient variance and instability. Directly selecting Top- is more stable, but risks the model falling into suboptimal solutions or even collapsing. Fortunately, modern load balancing strategies are mature enough to encourage comprehensive exploration to some degree, so Top- selection remains mainstream.
If one wants sampling while maintaining stability, one can extend slightly beyond Top- rather than sampling completely freely. For example: first select Top-, then randomly pick from those Experts. Or add mild noise to the logits of before selecting Top-. This increases randomness without straying too far from the original Top-, balancing both concerns.
Related Work
To be clear, the derivation in this article is not entirely new — it was distilled and modified by the author from Liu Liyuan’s paper Sparse Backpropagation for MoE Training. That paper also has a prequel Bridging Discrete and Backpropagation: Straight-Through and Beyond and a sequel GRIN: GRadient-INformed MoE.
Although these are papers from 2023–2024, for anyone wanting to deepen their understanding of the MoE Router, this trilogy is highly recommended reading. They provide a unified probabilistic framework for designing gradients for various discretization operations. Of course, the probabilistic framework also has its limitations — it is rather formalistic and can feel somewhat “constraining” in practice.
For example, when , if we straightforwardly extend the previous results, we should have:
That is, treating the joint distribution and expert-pair sum as the basic unit, reducing Top-2 to Top-1. However, the MoE form we actually use is , which does not lend itself to a clean probabilistic derivation.
In this case, a more “relaxed” interpretation might be to treat it as an analogue of MaxPooling without insisting on a probabilistic explanation — or alternatively, to understand it through the geometric interpretation from “MoE Odyssey: Part 1, Starting from Geometric Interpretation”. In summary, the probabilistic framework proves the viability of a certain approach, but does not in principle rule out the viability of other approaches.
Summary
This article attempts to explore the design of MoE’s Router and Gate from first principles, providing a probabilistic justification for gate normalization.
Footnotes
-
Translator’s Note: This quote and the title of this part allude to a classical Chinese saying from the Analects of Confucius: “不患寡而患不均” — “worry not about scarcity, but about inequality.” ^
-
Selective Sinkhorn Routing for Improved Sparse Mixture of Experts ^
-
MicroMoE: Fine-Grained Load Balancing for Mixture-of-Experts with Token Scheduling ^
-
A Theoretical Framework for Auxiliary-Loss-Free Load Balancing of Sparse Mixture-of-Experts in Large-Scale AI Models ^
