Top-k routing: separate a discrete choice from its differentiable weights
Derive router gradients inside a fixed selection region, distinguish gate normalization conventions, and test what changes when expert membership changes.
On this page
The question for today
What you’ll understand
- Differentiate a weighted expert output while holding its selected set fixed.
- Distinguish full-softmax gates from selected-set renormalization.
- Design routing tests around ties and selection boundaries.
A router makes two coupled decisions: which experts execute and how their outputs are weighted. The first is discrete; the second can carry an ordinary gradient. Treating both as an unspecified top-k operation hides important differences between sparse models. Derive one precise forward rule, identify the region where its selected set stays fixed, and test separately what happens at the boundary of that region.
A forward equation must state its normalization
Let a token representation produce one finite logit per routed expert. A full softmax gives a probability for every candidate, after which a deterministic top-k rule selects distinct experts. One possible layer output is the sum of selected expert outputs multiplied by their original full-softmax probabilities. These retained weights need not sum to one. Switch uses a selected full-softmax gate for its top-one branch. This is different from selecting logits first and normalizing only the selected entries, a convention supported by other implementations.
Selected-set renormalization divides retained probabilities by their retained sum. For top one, that produces weight one, so an otherwise isolated branch has no task gradient through its gate weight inside a fixed selection region. That observation does not apply to an unnormalized selected full-softmax gate. Auxiliary losses, other differentiable paths, noise, or alternative estimators can provide additional router learning signals. A model description must specify these paths rather than infer all training behavior from the integer k alone.
Differentiate within a fixed selection region
Away from ties and rank crossings, the selected membership can remain constant under a small logit perturbation. Ordinary differentiation then propagates through the selected gate values and expert computations. The derivative of the hard membership indicator is not a smooth substitute for evaluating the experts that were not executed. In a full-softmax gate, an unselected logit can still receive a gate-related gradient through the denominator, even though its expert weights receive no task update from that unexecuted branch for this token.
For a scalar illustration, hold expert outputs fixed and differentiate only the router logits. With top one selecting expert a, output y=p_a F_a has derivative p_a(1−p_a)F_a with respect to z_a. It also has derivative −p_a p_j F_a with respect to another logit z_j. These are local derivatives of the stated rule. At a membership change, the branch output can jump or change derivative depending on expert values and weighting. Do not validate a local derivative with a finite-difference step that silently crosses that change.
An experiment should preserve the routing question
Compare gate conventions with the same logits, expert parameters, token order, and capacity policy before drawing a learning conclusion. Inspect gradients of router weights separately from gradients of expert weights. A zero gradient in one toy configuration may result from renormalization, identical expert outputs, a saturated probability, or a detached tensor. Record selected identities and retained weight sums alongside the loss. For gradient checks, choose well-separated logits, then add explicit boundary tests whose expected result is a selection change rather than a smooth finite difference.
The network lab computes only a binary probability σ(wx−0.5). Reset, set x=0.50 and w=1, predict output 0.5, then Play or Step. Separately Reset, set x=1 and w=1, predict about 0.622, then Play or Step. It has no expert selection, capacity, or backward engine. Interpret its output as one component of logits [0,wx−0.5], and calculate the gate derivative p(1−p) by hand. This isolates a real normalization mechanism without presenting the instrument as a sparse-training simulator.
Work it through
A top-one gate can still carry a task derivative
Use two logits [ln(3),0], retain the full-softmax gate of the selected first expert, and hold its scalar output at eight. Differentiate with respect to the first logit within the same selected region.
The probabilities are [3/4,1/4], so top one selects the first expert and the retained weight is 0.75.
The scalar branch output is 0.75×8=6. Its local derivative is 8×0.75×0.25=1.5.
If instead the selected gate is renormalized to one, this isolated output becomes eight and its gate-weight derivative is zero inside the fixed selection region. The two forward conventions therefore answer different gradient questions.
The derivative 1.5 belongs to the explicitly retained full-softmax gate. It does not differentiate the integer selection itself.
The living notebook
Explore the model
Interactive lab · network
Inspect the differentiable binary gate only
Reset, set x=0.50 and w=1, predict p=0.5, then Play or Step. Separately Reset, set x=1 and w=1, predict p≈0.622, then Play or Step. The fixed sigmoid has no top-k selection, experts, auxiliary loss, or gradients.
- Product · wx
- 1.2
- Logit · wx − 0.5
- 0.7
- Sigmoid output
- 0.6682
One inspectable sigmoid unit, σ(wx − 0.5). Playback traces the computation graph. There is no training, attention mechanism or hidden deep network. Parameter changes pause playback and recompute the model at the current elapsed time. Reset restores both parameters and returns time to zero. Playback starts paused. Adjust either parameter, then choose Play or Step.
Look forA smooth probability and a discrete routing identity need separate analysis.
Pause & reason
Check your understanding
Your turn
Try it yourself
For the same retained-gate convention, let p_a=0.8 and fixed scalar expert output F_a=5. Find output and derivative with respect to z_a. Explain the scope of the calculation.
Take a moment to make a prediction or work it out on paper. Open a hint when you need one.
Give me a hint
Use p_a F_a and F_a p_a(1−p_a), keeping membership fixed.
Compare with the solution
The output is four and the local derivative is 5×0.8×0.2=0.8. This isolates the selected gate path for unchanged membership and fixed expert output. It says nothing about a perturbation crossing a tie or about an implementation that renormalizes the selected gate to one.
Keep the idea
Take it with you
- Specify whether gates normalize over all candidates or only selected experts.
- Hard membership and differentiable gate values are separate paths.
- Gradient checks need fixed-region cases and explicit boundary cases.
Read further
Original sources and deeper explanations to follow at your own pace.
Optional progress, saved in this browser when available. Revisit any lesson at any time.
Lesson not marked complete.