TL;DR
A mixture of experts (MoE) layer replaces one big network with many smaller ones, called experts, and a small router network. For each input the router produces a score for every expert, and only the top few experts run. Their outputs are combined using the router's weights. The model can hold many times more parameters than a dense model, but each input pays for only the few experts it uses. The cost is that the router must be trained to spread the work evenly, or some experts go unused.
Where you'll see it: inside the feedforward blocks of large language models such as Mixtral and Switch Transformer, and in the scaling recipes that let labs grow model capacity faster than their compute budget.
Intuition
A clinic with specialists
Picture a hospital with a front desk and twenty specialists. The desk does not send every patient to every doctor. It looks at the patient's symptoms and sends them to the two specialists most likely to help, and it combines their advice. The hospital has the knowledge of twenty doctors, but each patient only occupies the time of two.
A dense network works the other way. Every input passes through every weight, so doubling the number of parameters roughly doubles the work per input. A mixture of experts breaks that link. Parameters can keep growing, because each new expert only costs time on the inputs routed to it.
flowchart LR X[Input token] --> R[Router scores each expert] R -->|top 2 of 8| E2[Expert 2] R -->|top 2 of 8| E5[Expert 5] R -.->|not selected| E1[Experts 1, 3, 4, 6, 7, 8 idle] E2 --> S[Weighted sum] E5 --> S S --> Y[Output]
Why the router should be soft
The router does not make a hard choice that cannot be trained. It produces a set of weights that sum to one, so the output is a smooth blend of the chosen experts. Gradients can then flow back into the router, and the router learns which expert suits which kind of input. The plot below shows how the weight on one expert rises as its score grows, when two other experts have scores of zero.
- weight on expert 1
The math
The dense mixture
Let there be experts, each a function mapping an input to an output vector. The router computes a score for each expert, , where is a learned matrix with one row per expert. The gate is the Softmax of those scores:
The layer output is the weighted sum
Here is the input vector, is expert , and is its share of the output. The gates are positive and sum to one, so is a convex combination of the expert outputs. This is fully differentiable, but it runs every expert on every input, so it saves no compute. It is only useful as a baseline.
Sparse top-k gating
The sparse version keeps only the largest scores and sets the rest to minus infinity before the softmax, so their gates become exactly zero:
Only experts run, so the compute per input is roughly expert evaluations, whatever is. Shazeer and colleagues in 2017 also added tunable noise to the scores during training, so that the router explores different experts, and they added an importance loss to balance the load. Most later systems use plain top- with an auxiliary loss, which is the version described here.
Gradients still flow through the surviving gate values, so the router learns, even though the choice of which experts run is discrete. With this formula would give a gate of exactly 1 and no gradient to the router, so top-1 systems such as Switch Transformer instead take the softmax over all scores and multiply the chosen expert's output by its (unrenormalised) probability.
The load-balancing loss
Without any intervention, the router tends to send most inputs to a few favourite experts. Those experts get trained more, become better, and attract even more inputs. The rest starve. To prevent this, Fedus and colleagues added an auxiliary loss. Let be the fraction of tokens in a batch whose top choice is expert , and let be the average gate probability that the router gives expert over the batch. With experts and a small coefficient , the loss is
The loss is built so that perfectly uniform routing, , is the balanced target: there the sum equals and the loss equals its baseline value . Skewed routing, where the same experts get both a large share of tokens and a large gate probability, pushes the sum above that baseline. This is a heuristic that encourages balance, not a strict minimum over arbitrary distributions. The loss is added to the main training loss, and its gradient pushes the router toward spreading tokens evenly. The Switch Transformer paper uses .
Worked examples
Example 1: one token through a top-2 layer
A layer has three experts and . For one input , the router produces scores . The three expert outputs at this input are , and .
- Full softmax of the scores. The exponentials are , and . Their sum is . So the gates are .
- Top-2 selection. The two largest scores are for experts 1 and 3. Expert 2 is dropped, so it is not evaluated at all.
- Renormalise the two kept gates. Their sum is . The weights become for expert 1 and for expert 3.
- Combine the expert outputs: .
Interpretation: the output is about 2.46. It is close to expert 1's answer, because the router favours it, but expert 3 still contributes. Expert 2 gave the most extreme output, , and it had no effect at all, because the router did not select it. The compute spent was two expert calls out of three.
Example 2: parameters versus compute
Take a Transformer layer with model width and a feedforward hidden size of . A dense feedforward block has two weight matrices, one of size and one of size , so about parameters (ignoring biases). Every token uses all of them.
- Make each expert an identical feedforward block, so each has parameters.
- Use 64 experts with top-1 routing. The total is , about 537 million parameters in the layer.
- The router adds parameters, a small change.
- Each token runs one expert, so its feedforward compute is about million parameters' worth, the same as the dense block.
Interpretation: the layer holds 64 times the parameters of the dense block at about the same cost per token. The price is memory, because all 537 million parameters must be stored, and communication, because in a large cluster the tokens must be sent to the devices that host their experts.
Tradeoffs and failure modes
Mixture of experts buys capacity at a price, and most of the difficulty is in the routing.
| Situation | What goes wrong | Better choice |
|---|---|---|
| Router favours a few experts | Those experts overfit and others starve, so capacity is wasted | Auxiliary load-balancing loss, plus capacity limits per expert |
| Experts spread across many devices | Every layer needs an all-to-all exchange of tokens, which can dominate run time | Place experts to minimise traffic, or use fewer, larger experts |
| Small batches or fine-tuning on little data | Each expert sees very few examples, and the router is noisy | Fewer experts, or a dense model at this scale |
| Serving on limited memory | All experts must be resident even though few run per token | Quantisation, or offloading to a dense model if memory is the constraint |
| Training instability | Large router logits can make the softmax saturate and gradients erratic | Router z-loss, or a lower learning rate for the router |
A common confusion is between total and active parameters. A model described as having 47 billion parameters with 13 billion active per token is a sparse model, and its per-token cost matches a 13-billion-parameter dense model, not a 47-billion one. The memory footprint, however, matches the full count.
Routing also introduces dropped tokens. Systems that cap how many tokens each expert accepts per batch will drop the overflow, which changes the output. Designs differ in how they handle this, and it is worth checking before comparing results.
History
Jacobs, Jordan, Nowlan and Hinton proposed adaptive mixtures of local experts in 1991, with a gating network that learned to divide the input space among networks. Jordan and Jacobs extended it to hierarchies in 1994. For many years it stayed a niche method, because training everything at once was hard and the compute saving of sparsity was not yet needed.
Shazeer and colleagues brought it back in 2017 with a sparsely gated layer inside a recurrent language model. They showed that a model with far more parameters could be trained at a similar compute cost, and that the load-balancing problem had to be solved directly. Later work at Google, including GShard in 2020 and Switch Transformer in 2021, scaled top-1 and top-2 routing to very large transformers. Open models such as Mixtral 8x7B in 2023 applied top-2 routing among eight experts per layer and showed that the idea worked in widely used models.
Check yourself
QuestionWhy does the top-k router make the compute per token roughly independent of the number of experts?Show answer
Only experts are evaluated for each token, and the others are skipped entirely. Adding more experts adds parameters that a given token does not use, so the cost per token depends on and the size of each expert, not on the total count. The router itself does grow with the number of experts, but only by a small matrix.
QuestionA router gives logits over four experts. What is the gate weight for expert 3 under a full softmax, and what is it under top-1 routing?Show answer
The exponentials are , , and . Their sum is . The full softmax gives expert 3 a weight of . Under top-1 routing expert 3 is the only one that runs. If the gate is renormalised over the kept experts its weight is exactly 1; Switch-style routing instead keeps the full-softmax value 0.644 as the scale on its output.
QuestionWhy does the load-balancing loss use the product of and , rather than just penalising ?Show answer
The dispatch fraction is computed from a hard choice, so it is not differentiable. The gate probability is differentiable, so the product lets the gradient reach the router through , while tells the loss which experts are actually loaded. Minimising the sum encourages both toward uniform, so the router learns to route more evenly.
QuestionA team has 8 experts of size 2 billion parameters each in a layer, routes top-2, and trains with a 0.01 load-balancing coefficient. Roughly how many parameters does the layer hold, and how many run per token?Show answer
The layer holds billion parameters, and each token runs billion of them, plus a small router. The layer is eight times the size of a single expert in capacity but costs about twice a single expert per token. The team must still store all 16 billion parameters, which is the main memory cost.
Sources
- Shazeer et al., Outrageously Large Neural Networks: The Sparsely-Gated Mixture-of-Experts Layer, 2017. arxiv.org/abs/1701.06538
- Fedus, Zoph and Shazeer, Switch Transformers: Scaling to Trillion Parameter Models with Simple and Efficient Sparsity, 2021. arxiv.org/abs/2101.03961
- Mixture of experts, Wikipedia. en.wikipedia.org/wiki/Mixture_of_experts