Top-K Gating and Sparse Routing
Instead of running every 'expert' sub-network on every input, top-k gating scores all the experts and routes each input to only the handful that scored highest — getting a huge model's capacity at a small model's compute cost.
Prerequisites: Mixture-of-Experts Layers, Squeeze-and-Excitation Blocks
A mixture-of-experts layer contains many separate sub-networks ("experts"), each capable of specializing in a different kind of input. Running every expert on every input would give you the benefit of all that specialized capacity, but at a cost scaling with the total number of experts — defeating much of the point, since you'd rather have hundreds of experts available without paying for all of them every forward pass. Top-k gating is the routing rule that makes this affordable: score every expert on relevance, then only actually run the handful (typically 1 or 2) that scored highest, leaving the rest of the model's capacity unused — and uncosted — for that input.
The analogy: routing a support ticket to the right specialists
A large company might employ hundreds of specialists, but a single support ticket doesn't get read by all of them — a triage system scores the ticket against each specialist's expertise and routes it to the top two or three most relevant. The company still benefits from hundreds of specialists across its entire ticket volume, but no individual ticket costs more than a couple of people's time. Top-k gating is that triage system, run automatically inside a network: it decides, per input, which small subset of experts is worth consulting.
The mechanics: score, select top-k, weight, combine
Given experts and an input , a small learned gating network first produces a relevance score for every expert:
In words: a linear layer followed by a softmax turns the input into scores summing to 1, one per expert — a relevance ranking. Top-k gating keeps only the largest scores (commonly or ), zeroes the rest, and typically renormalizes the surviving to sum to 1. Only experts with a nonzero gate are actually run — a zero-gated expert's output would be wasted work, so implementations skip it entirely. The layer's output is the weighted combination of just those experts:
In words: only the top-k experts run at all, and their outputs are blended in proportion to how strongly the gate favored each of them.
Worked example 1: scoring and selecting from 6 experts
Suppose a gating network scores 6 experts as (softmax-normalized, summing to 1). With , keep only the two largest — expert 2 at , expert 4 at — and zero the rest. Renormalizing: and . Only experts 2 and 4 run; . The other 4 experts, despite existing and being fully trained, contribute nothing to this input and are never even evaluated.
Worked example 2: the compute savings, concretely
Suppose each expert costs 1 million FLOPs to run, with experts. Running all 64 densely costs FLOPs per input. With top-k gating at , only 2 run, costing roughly FLOPs — a reduction, even though the layer's total parameter count (all 64 experts, fully stored) is unchanged. This is the tradeoff sparse routing buys: parameter count scales with , while compute cost per input scales only with .
Picture the curve above as compute cost against with held fixed at a large number — cost grows only with the small exponent , not with the much larger base , which is exactly why keeping small while grows is the source of the savings.
Top-k gating scores every expert on relevance to the current input, keeps only the top scores (zeroing and skipping the rest), and blends just those experts' outputs — giving a model the total representational capacity of all experts while paying compute proportional only to the small actually used per input.
What this means in practice
Top-k gating is the mechanism behind mixture-of-experts architectures that scale to enormous total parameter counts, many times larger than would be practical to run densely, while keeping the compute cost of any single forward pass close to a much smaller dense network. The gating network must be trained carefully — left unconstrained, it tends to collapse onto favoring a few popular experts, starving the rest of training signal, so real implementations add a load-balancing penalty encouraging routing to spread more evenly across all experts.
Practice
- With 8 experts scored and , which experts are selected, and what are their renormalized gate weights?
- If each expert costs 2M FLOPs and , , compute the FLOPs saved per input versus running all experts densely.
- Why does an untrained or poorly-regularized gating network risk routing almost every input to the same one or two experts, and why is that a problem?
The common confusion is assuming top-k gating makes the model smaller. It does the opposite for parameter count — a sparsely-gated model typically has far more total parameters than a comparable dense model, precisely because it can afford to store many specialized experts as long as only a few run per input. What shrinks is the compute cost per input, not the model's size on disk or in memory; a sparse mixture-of-experts model can be enormous to store while still being cheap to run for any single example.
Related concepts
Practice in interviews
Further reading
- Shazeer et al., Outrageously Large Neural Networks: The Sparsely-Gated Mixture-of-Experts Layer (2017)
- Fedus, Zoph & Shazeer, Switch Transformers (2022)