Many-to-Many and Low-Resource Translation › Mixture-of-Experts Translation

Michael BrenndoerferNovember 25, 202610 min read
Language AI HandbookAll chapters
Part 27 of 67Machine Translation and Speech Translation
Chapter 193Many-to-Many and Low-Resource Translation › Mixture-of-Experts Translation
Reading section 5 of 11 · Mixture-of-Experts Translation · 10 minSection 5 of 11 · 10 min

Section 5 of 11 · 10 min read

Reading settings

Definitions

Choose how many terms have tooltips.

Article links

Make inline references clickable

Notes & comments

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-kk example, the router uses a learned affine map.

r(x)=Wrx+br,E(x)=TopK⁡(r(x),k)r(x) = W_r x + b_r, \qquad \mathcal{E}(x) = \operatorname{TopK}(r(x), k)

where:

  • x∈Rdx \in \mathbb{R}^d is one token representation of width dd.
  • EE is the number of experts. The router uses weights Wr∈RE×dW_r \in \mathbb{R}^{E \times d} and bias br∈REb_r \in \mathbb{R}^E.
  • r(x)∈REr(x) \in \mathbb{R}^E contains one router logit per expert. Its ii-th entry is ri(x)r_i(x).
  • E(x)\mathcal{E}(x) contains the indices of the kk largest logits, with 1≤k≤E1 \le k \le E.

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.

gi(x)=exp⁡(ri(x))∑j∈E(x)exp⁡(rj(x)),i∈E(x),y=∑i∈E(x)gi(x) Fi(x).\begin{aligned} g_i(x) &= \frac{\exp(r_i(x))}{\sum_{j \in \mathcal{E}(x)} \exp(r_j(x))}, \quad i \in \mathcal{E}(x), \\ y &= \sum_{i \in \mathcal{E}(x)} g_i(x) \, F_i(x). \end{aligned}

where:

  • gi(x)g_i(x) is the gate for selected expert ii. The denominator sums over selected indices jj, so the selected gates sum to one.
  • Fi(x)∈RdF_i(x) \in \mathbb{R}^d is expert ii's output, and y∈Rdy \in \mathbb{R}^d 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 kk in a sparse implementation, while stored expert weights scale with EE for equal-width experts. Holding kk 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-kk example below uses the same auxiliary expression with shares of all selected assignments.

Laux=α E∑i=1Efi Pi\mathcal{L}_{\text{aux}} = \alpha \, E \sum_{i=1}^{E} f_i \, P_i

where:

  • fif_i is the fraction of selected assignments going to expert ii before any capacity limit. With MM tokens and kk selected experts per token, the denominator is kMkM, so ∑ifi=1\sum_i f_i = 1. For Switch's top-1 case, each token makes one assignment.
  • PiP_i is the mean probability for expert ii across the batch, computed from the softmax over all EE router logits for each token.
  • α\alpha weights the auxiliary term relative to the task loss.

On a gradient update, the hard assignment shares fif_i act as fixed weights. Increasing PiP_i is more costly for an expert with a large assignment share. Under uniform routing and router probabilities, fi=Pi=1/Ef_i = P_i = 1/E, giving E∑ifiPi=1E\sum_i f_i P_i = 1 and Laux=α\mathcal{L}_{\text{aux}} = \alpha. This is a normalization reference. For example, with two experts and three tokens assigning softmax probabilities (0.51,0.49)(0.51,0.49), (0.51,0.49)(0.51,0.49), and (0.01,0.99)(0.01,0.99), the top-1 assignment shares are (2/3,1/3)(2/3,1/3). The unweighted loss is about 0.8960.896, 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]
CollapseExpand
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_EXPERTS
VisualizationOut [5]
CollapseExpand
Scatter of auxiliary loss against the busiest expert's share of assignments.
Auxiliary load-balancing loss (alpha = 0.1, eight experts, one assignment per token) as routing concentrates. Each point is one router-logit scale from a fixed seeded sweep; the horizontal line is the uniform-routing reference alpha and the vertical line marks the uniform assignment share of 1/8. In this sweep, the busiest share ranges from about 13.1% to 83.5%, and the loss rises at successive scale settings.

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 α=0.1\alpha=0.1. 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 fif_i and positive α\alpha, this inner product assigns a larger coefficient to PiP_i for experts with higher hard shares. The example above gives 403/450403/450 before multiplying by α\alpha 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.

C=⌈γkME⌉C = \left\lceil \gamma \frac{kM}{E} \right\rceil

where:

  • CC is the number of assignments the expert can accept for this routing batch.
  • γ\gamma is a capacity multiplier relative to the uniform load.
  • MM is the number of tokens in the routing batch, and kk is the number of experts picked per token.
  • EE is the number of experts, and the ceiling rounds up to an integer buffer size.

With top-1 routing, the uniform load is M/EM/E. With top-2 routing and one shared buffer per expert, each token can contribute two assignments, so the corresponding load is 2M/E2M/E. 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 C=⌈γkM/E⌉C=\lceil\gamma kM/E\rceil. For fixed counts, which assignments drop can depend on their order, while the total follows from the counts and the buffer size.

PythonIn [6]
CollapseExpand
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.0
VisualizationOut [7]
CollapseExpand
Line chart: dropped assignments versus capacity factor, marking gamma equal to one.
Fraction of dropped assignments for one seeded vector of eight expert counts totaling 2,048 assignments. The example illustrates aggregate top-2 load. The reference at gamma = 1 is the buffer implied by a uniform split. Drops reach zero once the buffer can hold the largest count in this vector.

At the uniform-split buffer, some expert counts exceed capacity and about 29.5% of assignments are dropped. In this synthetic draw, γ=2.4\gamma=2.4 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!

Newsletter

Stay up to date

Get new articles, book updates, and notes on AI and technology.

Unsubscribe at any time. Privacy policy