research26 min studyLesson 571 in Engineering

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

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.

pi=ezi∑j=1Eezj,y=∑i∈Sk(z)pi Fi(x)p_i=\frac{e^{z_i}}{\sum_{j=1}^{E}e^{z_j}},\qquad y=\sum_{i\in S_k(z)}p_i\,F_i(x)
E≥1, 1≤k≤E, logits z are finite dimensionless values, and F_i(x) are compatible expert output vectors. This teaching rule retains full-softmax weights without renormalizing the selected set. Ties require an explicit selection rule; capacity admission is not included yet.↔ Scroll wide equations horizontally.

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.

∂pi∂zj=pi(1i=j−pj),∂y∂zj∣S fixed=∑i∈SFi(x) pi(1i=j−pj)\frac{\partial p_i}{\partial z_j}=p_i(\mathbf1_{i=j}-p_j),\qquad \left.\frac{\partial y}{\partial z_j}\right|_{S\ {\rm fixed}}=\sum_{i\in S}F_i(x)\,p_i(\mathbf1_{i=j}-p_j)
This derivative holds with the selected set S and expert values fixed, isolating the gate path. Vector outputs are differentiated componentwise. It does not differentiate a tie-breaking decision or claim a smooth derivative across a top-k boundary.↔ Scroll wide equations horizontally.

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.

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.

  1. The probabilities are [3/4,1/4], so top one selects the first expert and the retained weight is 0.75.

  2. The scalar branch output is 0.75×8=6. Its local derivative is 8×0.75×0.25=1.5.

  3. 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 result

The derivative 1.5 belongs to the explicitly retained full-softmax gate. It does not differentiate the integer selection itself.

Explore the model

Interactive lab

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.

Inspect the differentiable binary gate onlyOne inspectable sigmoid unit, σ(wx − 0.5). Playback traces the computation graph. There is no training, attention mechanism or hidden deep network. Current readings: Product · wx: 1.2; Logit · wx − 0.5: 0.7; Sigmoid output: 0.6682. All parameters are adjustable with the labeled controls below.One input. One weight. One sigmoid.x1.20× w1.20+ b0.70σ0.668output0.668w = 1.00b = −0.50σ(z) = 1 / (1 + exp(−z))
Computation graphSignal trace
Product · wx
1.2
Logit · wx − 0.5
0.7
Sigmoid output
0.6682
t = 0 s

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.

Check your understanding

Why can an unselected router logit receive a task gradient under retained full-softmax top-one weighting?

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.

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.