Mixture-of-experts layers
Replace the FFN with E expert FFNs and a small learned router that sends each token to the top-k of them. Distinguish total parameters (memory) from active parameters (FLOPs per token). Explain the extra load-balancing loss added during training to keep experts evenly used.
Builds on: FLOPs per token: 2N forward, 6N training, The LLM training loop
What the animation says
Every token pays for every weight
In a dense transformer, every token passes through every weight. Each weight costs a multiply and an add, so a forward pass is about two FLOPs per parameter, per token. More parameters means proportionally more compute. And most parameters live in the feed-forward layers: about two-thirds of each block. So could a model hold far more feed-forward weights, while each token pays for only a few?
Experts and a router
A mixture-of-experts layer replaces the one feed-forward network with several, called experts. Here, four. A router, one tiny linear layer, scores the token against each expert. A softmax turns the scores into gates. The token goes only to its top two experts. Their outputs are mixed using those gates, rescaled to add up to one. Here are this token's gates. Tap the two experts it is sent to.
Stored versus active
All four experts must be stored. But this token computes through, and sends gradient through, only two. Now scale up: thirty-two blocks, width four thousand ninety-six, eight experts, top two. Each block holds attention, about sixty-seven million, plus eight experts of about a hundred thirty-four million each. Stored in total: about thirty-six and a half billion. Used by one token: attention plus two experts, about ten point seven billion. So roughly how much forward compute does one token cost?
Memory follows total, compute follows active
About twenty-one gigaflops: two FLOPs for each active parameter, not for every stored one. Memory is different. All thirty-six and a half billion must be stored: at two bytes each, about seventy-three gigabytes. The dense model, with one feed-forward network per block, has about six point four billion parameters. So this MoE stores almost six times as much, yet each token costs well under twice the compute. The rule: storage counts every expert. Compute counts attention plus the chosen experts, times two.
Collapse and the balance loss
Left alone, the router collapses: experts favoured early get more tokens, train faster, and get picked even more. The fix is a balance loss. Over a batch, f is each expert's share of assignments, and P is its average gate. Multiply f by P for each expert, sum, and times the number of experts. With even usage, it hits its minimum: one. It's added to the training loss with a small weight, about one percent. Here's a collapsed batch. Tap the experts whose gates this loss pushes down.
Many FFNs, a router picks a few
Routing rule and the balance loss
Total vs active parameters, in numbers
An MoE layer and its balance loss
import numpy as np
T, d, E, k, dff = 6, 8, 4, 2, 32; rng = np.random.default_rng(0)
X = rng.normal(size=(T, d)); Wr = rng.normal(size=(d, E))
W1 = rng.normal(size=(E, d, dff)) / d**.5; W2 = rng.normal(size=(E, dff, d)) / dff**.5
g = np.exp(X @ Wr); g /= g.sum(1, keepdims=True) # router probs [T, E]
top = np.argsort(-g, 1)[:, :k] # chosen experts [T, k]
Y = np.zeros_like(X)
for t in range(T):
w = g[t, top[t]] / g[t, top[t]].sum() # renormalised gates
for i, wi in zip(top[t], w): Y[t] += wi * np.maximum(X[t] @ W1[i], 0) @ W2[i]
f = np.bincount(top.ravel(), minlength=E) / (T * k); P = g.mean(0)
print("balance loss:", E * (f * P).sum()) # 1.0 if perfectly even
Remember
On Bytewise this lesson comes in small steps with a check at the end, and the app brings it back right before you would forget it.
Bytewise
All courses