Many-to-Many and Low-Resource Translation › Mixture-of-Experts Translation
Language AI HandbookAll chapters A mixture-of-experts (MoE) layer replaces a dense feed-forward network with a set of expert feed-forward networks. A router scores the experts separately for each token, then selects a small subset of them. In this top- example, the router uses a learned affine map.
where:
- is one token representation of width .
- is the number of experts. The router uses weights and bias .
- contains one router logit per expert. Its -th entry is .
- contains the indices of the largest logits, with .
The feed-forward block is a natural place to add experts because its inputs are token states processed independently. The Switch Transformer, for example, keeps the existing attention sublayer and replaces the ordinary feed-forward sublayer with routed experts.
Once the router has picked the experts, this gate normalizes their logits into weights and combines their outputs.
where:
- is the gate for selected expert . The denominator sums over selected indices , so the selected gates sum to one.
- is expert 's output, and is their weighted sum.
For example, if the router selects exactly two experts and their logits are equal, each receives half the weight. The expert arithmetic per token scales with in a sparse implementation, while stored expert weights scale with for equal-width experts. Holding and expert width fixed lets you add experts without increasing the selected-expert arithmetic. Scoring the experts and dispatching their inputs add work of their own, so per-token expert arithmetic alone cannot determine serving latency.
Load balancing
A router can send most tokens to a small set of experts, leaving other experts with few assignments. In a sparsely executed MoE, a token that sends no accepted assignment to an expert contributes no task gradient to that expert’s weights through that path. Assignments can change on later batches. Switch Transformer adds a load-balancing term to the task loss. Switch picks one expert and weights its output by that expert’s probability from the full router softmax. The selected-only softmax above would give a single selected expert weight one. The top- example below uses the same auxiliary expression with shares of all selected assignments.
where:
- is the fraction of selected assignments going to expert before any capacity limit. With tokens and selected experts per token, the denominator is , so . For Switch's top-1 case, each token makes one assignment.
- is the mean probability for expert across the batch, computed from the softmax over all router logits for each token.
- weights the auxiliary term relative to the task loss.
On a gradient update, the hard assignment shares act as fixed weights. Increasing is more costly for an expert with a large assignment share. Under uniform routing and router probabilities, , giving and . This is a normalization reference. For example, with two experts and three tokens assigning softmax probabilities , , and , the top-1 assignment shares are . The unweighted loss is about , below the uniform reference. Monitor actual expert loads alongside the loss.
The sweep below tests how the auxiliary loss responds as one router becomes more concentrated. It scales a fixed per-expert logit bias while holding the seeded token-level logit noise fixed. For each scale it records the top-1 assignment shares and the mean router probabilities, then evaluates the auxiliary expression above. The monotonic trend is a result for this controlled sweep. The two-expert example above gives a loss below the uniform reference.
PythonIn [4]Expand
import numpy as np
_bal_rng = np.random.default_rng(0)
NUM_EXPERTS = 8
NUM_TOKENS = 4096
ALPHA = 0.1
# fixed per-expert logit offset; scaling it concentrates the router
router_bias = _bal_rng.normal(size=NUM_EXPERTS)
token_logit_noise = _bal_rng.normal(size=(NUM_TOKENS, NUM_EXPERTS))
sweep_scales = np.linspace(0.0, 4.0, 41)
aux_losses = []
busiest_shares = []
for _t in sweep_scales:
logits = _t * router_bias[None, :] + token_logit_noise
probs = np.exp(logits - logits.max(axis=1, keepdims=True))
probs /= probs.sum(axis=1, keepdims=True)
chosen = logits.argmax(axis=1)
counts = np.bincount(chosen, minlength=NUM_EXPERTS)
f = counts / NUM_TOKENS
P = probs.mean(axis=0)
aux_losses.append(ALPHA * NUM_EXPERTS * float((f * P).sum()))
busiest_shares.append(float(f.max()))
aux_losses = np.array(aux_losses)
busiest_shares = np.array(busiest_shares)
uniform_share = 1.0 / NUM_EXPERTSVisualizationOut [5]Expand

The smallest busiest-expert share in this sweep is about 13.1%, slightly above the 12.5% reference. The auxiliary loss there is close to . At the other end the busiest share is about 83.5% and the loss is about 0.462. The loss alone does not identify which experts got the assignments.
For fixed hard shares and positive , this inner product assigns a larger coefficient to for experts with higher hard shares. The example above gives before multiplying by even though the assignments are skewed. Inspect per-expert assignment counts alongside the auxiliary loss to see where the tokens went.
The original Shazeer et al. MoE paper experimented with machine translation and language modeling. The GShard paper reports a roughly 600-billion-parameter model in a translation experiment from 100 source languages into English. The NLLB-200 54.5B model routes each token to up to two of 128 experts per MoE layer. The NLLB technical report compares its FLOPs with those of a 3.3B dense model. The full parameter count matters for checkpoint storage, while the FLOP estimate describes the operations evaluated for a given input.
Expert specialization
Do experts specialize by language? BASE Layers reported associations between token routing and local syntactic context in a language model. The NLLB technical report found that related languages tended to use similar expert sets in some early encoder and late decoder layers on FLORES dev. That is evidence about routing distributions in those layers.
Training and serving costs
Three practical costs matter alongside the FLOP count.
- Memory. Storing the full expert bank includes experts that may be inactive for a given token. At two bytes per weight, NLLB-200’s 54.5 billion weights require approximately 109 GB of raw storage before activations and other overhead.
- All-to-all communication. One expert-parallel layout sends token representations to devices that hold the selected experts and brings the outputs back. When comparing two layouts, measure this traffic alongside computation.
- Load balancing. If a router sends too many assignments to one expert, a fixed buffer can overflow while other experts have unused slots. Check assignment counts, dropped assignments, and the balance term together.
When an expert has a fixed buffer, assignments exceeding that buffer may be skipped. A capacity rule can start from the number of assignments each expert would receive under a uniform split.
where:
- is the number of assignments the expert can accept for this routing batch.
- is a capacity multiplier relative to the uniform load.
- is the number of tokens in the routing batch, and is the number of experts picked per token.
- is the number of experts, and the ceiling rounds up to an integer buffer size.
With top-1 routing, the uniform load is . With top-2 routing and one shared buffer per expert, each token can contribute two assignments, so the corresponding load is . If an assignment is skipped, that expert contributes no output or expert gradient for that assignment. A token can still pass through a residual connection or receive another selected expert’s output. A larger buffer can reduce dropped assignments and can use extra memory or pad the computation with idle slots.
To measure the effect of the capacity rule, draw one seeded vector of aggregate expert counts. The total equals two assignments per token, while the draw models counts across experts without recording which two experts a token uses. The simulation compares each count with . For fixed counts, which assignments drop can depend on their order, while the total follows from the counts and the buffer size.
PythonIn [6]Expand
import numpy as np
_cap_rng = np.random.default_rng(1)
CAP_EXPERTS = 8
CAP_TOKENS = 1024
CAP_K = 2
# a deliberately skewed router preference over experts
expert_preference = np.array([0.30, 0.22, 0.16, 0.11, 0.08, 0.06, 0.04, 0.03])
assignments_total = CAP_TOKENS * CAP_K
assignment_counts = _cap_rng.multinomial(assignments_total, expert_preference)
capacity_gammas = np.linspace(0.5, 2.5, 41)
dropped_fraction = []
for _g in capacity_gammas:
capacity = int(np.ceil(_g * assignments_total / CAP_EXPERTS))
kept = np.minimum(assignment_counts, capacity).sum()
dropped_fraction.append(1.0 - kept / assignments_total)
dropped_fraction = np.array(dropped_fraction)
uniform_gamma = 1.0VisualizationOut [7]Expand

At the uniform-split buffer, some expert counts exceed capacity and about 29.5% of assignments are dropped. In this synthetic draw, is the first value on the tested grid that accepts every assignment. Measure representative batches with the actual dispatch implementation to choose headroom for a target drop rate.
Comments
Add to the conversation
A question, a correction, or an example from your own work.
No comments yet. Be the first to share your thoughts!