A mixture-of-experts layer has 4 experts, each a feed-forward block with dmodel=1024 and hidden width 4096 (two weight matrices, no biases), and a router that picks the top 2 experts per token. For router logits (2,1,0.5,−1) over the four experts, what are the gate weights of the top-2 experts? Compare the total and active parameters of the layer with one dense feed-forward block, and compute the load-balancing loss N∑ifiPi for f=(0.4,0.3,0.2,0.1) and P=(0.35,0.30,0.20,0.15) for these N=4 experts?

Sequence models and transformers, Transformers
Level L3 Intermediate. Length Medium. Nature Mat Mathematical.

What's being asked

Whether you understand how sparse routing gives many parameters at a fixed per-token compute, and how the auxiliary loss pushes the router towards balanced use.

Key idea

The layer output is y=∑i∈top-kgi Ei(x) with gates from a softmax over the selected logits. Parameters grow with the number of experts, but per-token compute grows only with k. The auxiliary loss N∑ifiPi (token fraction times mean router probability) equals 1 when use is uniform, which is the balanced target (when P tracks f, as it does in training, it cannot go lower).

Steps

  1. Top 2 of logits (2,1,0.5,−1) are 2 and 1. Softmax over just these gives e2=7.389 and e1=2.718, total 10.107, so gates (0.731,0.269). Taking the softmax over all four and renormalising the top two gives the same numbers, since the common denominator cancels.
  2. One expert: 2×1024×4096=8,388,608 parameters (≈8.4M). The 4 experts hold 33,554,432 (≈33.6M), plus a router with 1024×4=4096 weights, about 33.6M in total.
  3. Active per token: 2 experts =16,777,216 (≈16.8M), plus the tiny router. So the layer stores 4× the parameters of a dense block but uses only 2× the compute per token.
  4. Load-balancing loss. fi is the fraction of tokens sent to expert i and Pi the average router probability for it. ∑ifiPi=0.14+0.09+0.04+0.015=0.285, so the loss is 4×0.285=1.14.
  5. For perfectly uniform use fi=Pi=1/4, so ∑fiPi=1/4 and the loss is 1. Here 1.14>1, so the router is imbalanced. Gradients flow through Pi (differentiable), pushing probability from busy experts to idle ones.
  6. In practice experts also get a capacity limit per batch, and tokens beyond it are dropped or sent to the next choice.

Common mistakes

How long your answer should be

Half a page; 4–5 minutes.

Last checked on 4 December 2026.

Read this

Previous in Sequence models and transformers: A unigram language-model tokeniser has the vocabulary and probabilities h 0.10, u 0.10, g 0.10, s 0.10, hu 0.10, ug 0.10, hug 0.20, gs 0.05, ugs 0.15. Which segmentation of "hugs" does it choose, how is it found efficiently, and how does this differ from BPE?
Next in Sequence models and transformers: An encoder gives states h1=(1,0), h2=(0,1) and h3=(−1,1). The decoder state is s=(1,1). Bahdanau (additive) attention scores each state as ej=v⊤tanh⁡(Wss+Whhj), here with Ws=Wh=I and v=(1,−1). Compute the attention weights and the context vector, and say when additive attention is preferred to dot-product scoring?