How a Token Selects Its Experts: Top-2 Routing, Shared Experts, and Load Balancing
A dense MLP uses the same feed-forward parameters for every token in a layer. A sparse Mixture of Experts (MoE) stores several feed-forward networks. A router selects some of them from the current token representation, then combines their outputs.
“Sparsity” describes which experts are active for a token. It does not split the input vector into pieces or require sparse matrices inside an expert. We use row vectors multiplying weights on the right, omit biases, and use teaching matrices rather than trained weights from a real model.
Figure one: branch from the same z, return to the same shape
The shared starting point is the first residual after attention: R=[2.8,−0.4]. Set RMSNorm’s γ=[1,1] and ignore ε for this hand calculation:
RMS(R) = √((2.8² + (−0.4)²)/2) = 2z = R / 2 = [1.4,−0.2]R is the residual stream after attention. RMSNorm produces z for the feed-forward branch, and R is carried forward for the next addition. The dense branch sends all of z to one SwiGLU. The MoE branch sends all of z to each selected expert. Routing is repeated for each token at each MoE layer.
The router scores four experts first
Experts are numbered 0–3. Router weights Wr have shape [2,4]: one column per expert and one row per input feature.
E0 E1 E2 E3Feature 0 0 ln 3 ln 2 0Feature 1 0 ln 2 0 0
s = z Wr ≈ [0,1.3994,0.9704,0]p = softmax(s) ≈ [0.1150,0.4663,0.3036,0.1150]E1’s score is s[t,1]=z₀Wr[0,1]+z₁Wr[1,1]. Each token contributes its input feature times its score gradient to the corresponding weight gradient. Summing over the batch gives
w₁ = p₁/(p₁+p₂) ≈ 0.6056w₂ = p₂/(p₁+p₂) ≈ 0.3944E0 and E3 receive router scores but their feed-forward computations are skipped for this token. Top-2, softmax, and selected-entry renormalization are this example’s routing choices; not every MoE implementation uses identical rules.
Wr is a learned matrix shared across tokens. The scores s, probabilities p, and mixing weights w are computed anew from each token’s z.
Why the same input produces two different outputs
Each expert has its own Wg, Wu, and Wd:
a = zWg g = SiLU(a)v = zWu u = g ⊙ ve = uWdThe complete matrices in the figure are:
Expert 1 Expert 2Wg [2,3] [[1,2,0],[0,−1,−1]] [[0,1,2],[1,−2,−1]]Wu [2,3] [[1,2,1],[1,−1,2]] [[0,2,1],[1,1,1]]Wd [3,2] [[1,0],[0,1],[1,1]] [[1,1],[1,0],[0,1]]Following the lower half of the figure:
| Intermediate | Expert 1 | Expert 2 |
|---|---|---|
| a=zWg | [1.4,3.0,0.2] | [−0.2,1.8,3.0] |
| g=SiLU(a) | [1.123,2.858,0.110] | [−0.090,1.545,2.858] |
| v=zWu | [1.2,3.0,1.0] | [−0.2,2.6,1.2] |
| u=g⊙v | [1.348,8.573,0.110] | [0.018,4.016,3.429] |
| e=uWd | [1.458,8.683] | [4.034,3.447] |
For example, expert 1’s second a component is 1.4×2+(−0.2)×(−1)=3. Expert 2’s first u component is positive because two negative values multiply. Wr selects experts; an expert’s Wg modulates its internal features. Both operations involve gating, but at different levels.
Mix expert outputs before adding the residual
Δ = w₁e₁ + w₂e₂ ≈ [2.4737,6.6183]Y = R + Δ ≈ [5.2737,6.2183]The dense route uses expert 1’s matrices for comparison, giving Y≈[4.258,8.283]. This isolates routing and mixing; a dense model is not required to reuse any particular expert.
Both routes deliver one two-dimensional vector to the next layer. They do not concatenate two expert outputs into four dimensions. Outputs are computed from unrounded intermediate values; table entries are rounded for display.
Saving computation relative to what?
For input width D and expert intermediate width F, one bias-free SwiGLU contains 3DF weights. Its three projections also require 3DF scalar multiplications per token.
Here D=2 and F=3, so one expert requires 18:
| Configuration | Expert sets stored | Sets executed per token | Expert projection multiplications |
|---|---|---|---|
| Same-width dense MLP | 1 | 1 | 18 |
| 2-of-4 MoE | 4 | 2 | 36 |
| Execute all four experts | 4 | 4 | 72 |
Top-2 halves expert projection work relative to executing all four, while doing more work than one same-width dense MLP. Router scoring adds 2×4=8 multiplications, plus softmax, selection, and mixing overhead omitted from the table.
Total expert count controls parameter capacity; active expert count and width control per-token expert work. Inactive weights still require storage. Deployment gains also depend on memory, communication, load, and quality targets; “only two experts” does not imply a fixed speedup.
Figure two: the shared expert also receives all of z
Shared expert S has its own Wg, Wu, and Wd and processes every token. The routed branch still selects two of four candidates. Token A may execute S, E1, E2 while token B executes S, E0, E3. They share S’s parameters, not an identical output.
The shared branch provides parameters for transformations useful across tokens. Here its output is added directly; see the shared-expert design in DeepSeekMoE §3.2.
S uses:
Wg = [[1,0,1],[2,1,2]]Wu = [[1,0,1],[0,1,1]]Wd = [[1,0],[0,1],[0,1]]The same z=[1.4,−0.2] follows both branches:
a = zWg = [1,−0.2,1]g = SiLU(a) ≈ [0.7311,−0.0900,0.7311]v = zWu = [1.4,−0.2,1.2]u = g⊙v ≈ [1.0235,0.0180,0.8773]S(z) = uWd = [u₁,u₂+u₃] ≈ [1.0235,0.8953]Return to the upper diagram:
Δ_routed = w₁e₁ + w₂e₂ ≈ [2.4737,6.6183]Δ_total = Δ_routed + S(z) ≈ [3.4972,7.5136]Y = R + Δ_total ≈ [6.2972,7.1136]Combine feed-forward branches first, then add R. Routing weights apply only to their corresponding routed outputs; do not arbitrarily multiply S by w₁ or w₂.
Adding S executes three same-width experts per token, raising projection multiplications from 36 to 54. A shared expert is not free. A budget for two experts could instead use one shared and one routed expert, but that is a different configuration.
Figure three: Top-2 does not balance an entire batch
Top-2 limits each token to two experts. It does not stop a whole batch from concentrating on the same few experts. Persistent imbalance gives some experts little task gradient and can overload devices holding popular experts.
The first two figures followed one token through the forward pass. Figure three starts a separate training example with a new router matrix and eight tokens. Its input Z has shape [8,2]. T1–T6 have features [1,0] and T7–T8 have features [0,1].
Old Wr E0 E1 E2 E3Feature 0 ln 4 ln 3 ln 2 0Feature 1 ln 4 0 ln 3 ln 2Compute s=ZWr and apply softmax per row:
| Tokens | Probabilities p | Top-2 |
|---|---|---|
| T1–T6, computed independently | [0.4,0.3,0.2,0.1] | E0, E1 |
| T7–T8, computed independently | [0.4,0.1,0.3,0.2] | E0, E2 |
Step 02: f counts assignments; P averages probabilities
Eight tokens selecting two experts produce 16 assignments. Counts are [8,6,2,0], so:
f = [8,6,2,0] / 16 = [0.5,0.375,0.125,0]P averages each expert’s pre-Top-2 probabilities over the eight tokens:
P₁ = (6×0.3 + 2×0.1) / 8 = 0.25P = [0.4,0.25,0.225,0.125]E1’s f₁=37.5% is its share of all assignments. P₁=25% is its mean probability. Even tokens that do not select E1 contribute their pre-selection probability to P₁. The always-used shared expert is counted separately from these four routed experts.
Step 03: turn the statistics into an auxiliary loss
This example uses an expert-level balancing objective with f normalized to sum to one:
L_bal = 4 × Σᵢ fᵢPᵢ = 4 × (0.20 + 0.09375 + 0.028125 + 0) = 1.2875J = α L_bal = 0.1 × 1.2875 = 0.12875L_total = L_language_model + JEqual assignment gives each fᵢ=1/4, so the factor four makes the reference value one. This is the equal-assignment reference, not necessarily the minimum over every feasible routing pattern.
Small score changes that preserve the Top-2 selection leave the assignment counts unchanged. The backward pass therefore treats f as fixed and uses the mean probability P as its differentiable path. Gradients flow through continuous P, p, and s to Wr. With f fixed, moving probability from a high-f expert to a low-f expert reduces this term. Moving 0.01 of P from E1 to E3 reduces the product sum by 0.01×(0.375−0)=0.00375. This corresponds to the expert-level balancing loss in DeepSeekMoE §3.3. Our f is divided by all assignments, so the expert count four sits outside the sum.
Step 04: an unselected expert can still receive a router gradient
Follow T1’s probability for E1:
∂J/∂P₁ = 0.1×4×0.375 = 0.15∂P₁/∂p[T1,1] = 1/8∂J/∂p[T1,1] = 0.01875Backpropagating through softmax requires four paths: changing E1’s score changes all four probabilities.
| Affected probability | ∂J/∂pᵢ | ∂pᵢ/∂s₁ | Product |
|---|---|---|---|
| E0 | 0.025 | −0.4×0.3 | −0.0030000 |
| E1 | 0.01875 | 0.3×0.7 | +0.0039375 |
| E2 | 0.00625 | −0.2×0.3 | −0.0003750 |
| E3 | 0 | −0.1×0.3 | 0 |
The sum is G[T1,1]=∂J/∂s[T1,1]=0.0005625, identical for T1–T6. Combining the four terms gives, for token t and expert j:
G[t,j] = (α×4/8) × p[t,j] × (fⱼ − Σᵢ p[t,i]fᵢ)For T7, the weighted term is 0.4×0.5+0.1×0.375+0.3×0.125=0.275, giving:
G[T7,1] = 0.05×0.1×(0.375−0.275) = 0.0005For T7, E1’s feed-forward branch was skipped. The nonzero derivative above is with respect to E1’s router score. Its pre-selection probability contributes to P₁, so T7 still helps update the E1 column of Wr. E1’s own Wg, Wu, and Wd receive task gradients from tokens that actually run that branch.
Steps 05–06: eight contributions update two shared weights
E1’s score is s[t,1]=z₀Wr[0,1]+z₁Wr[1,1]. A weight’s contribution is its input feature multiplied by the score gradient:
∂J/∂Wr[0,1] = 6×0.0005625 = 0.003375∂J/∂Wr[1,1] = 2×0.0005 = 0.001∂J/∂Wr = ZᵀG [2,8] × [8,4] → [2,4]The factor 1/8 already entered through mean-probability backpropagation. Sum here; do not divide by eight again. Wr is unchanged when backward finishes. Only the optimizer writes new values.
The figure isolates auxiliary term J using SGD with learning rate η=0.5:
Wr_new = Wr_old − 0.5 × ∂J/∂Wr
New Wr E0 E1 E2 E3Feature 0 1.3765444 1.0969248 0.6995222 0.0050625Feature 1 1.3817944 −0.0005000 1.1008623 0.6958972E1’s second weight updates even though it started at zero: T7/T8 have a second input feature of one, giving a nonzero derivative with respect to that weight. Real training also includes the language-model loss gradient. Isolating J in this example does not mean training only optimizes expert usage.
Step 07: a lower score need not mean a lower probability
Recomputing the same Z with new Wr lowers T1’s E1 score, yet its probability rises slightly from 0.30000 to about 0.30027 because E0’s score decreases more. Softmax depends on relative scores.
J falls from 0.12875 to about 0.12836. For T1–T6 the top two are still E0 and E1, and for T7–T8 they are still E0 and E2. The probabilities have moved, but the assignment counts stay at [8,6,2,0]. The counts change only when a different expert enters the top two.