research27 min studyLesson 577 in Engineering

Router precision: protect the selection margin as well as softmax

Separate stable probability evaluation from robust expert ordering, and evaluate selective precision, logit regularization, and near-tie sensitivity.

On this page

What you’ll understand

  • Derive a sufficient top-k margin condition under bounded logit perturbations.
  • Distinguish max-shift stability, router dtype, and logit z-loss.
  • Test routing identities alongside loss and gradient behavior.

A tiny numerical change in a dense layer may slightly change its output. In a sparse router, the same change can choose a different expert and alter communication, capacity admission, and subsequent gradients. Stable exponentials therefore solve only part of the problem. Investigate both probability evaluation and the margin separating selected from unselected logits, under the exact dtype and training configuration.

A stable softmax does not restore a lost ordering

For finite logits, subtracting their maximum before exponentiation avoids positive exponential overflow in the mathematical normalization. It does not undo errors already introduced while computing or storing the logits, and tiny probabilities can still underflow. Top-k ordering can be computed from logits without exponentiating because exact softmax preserves their order. In finite arithmetic, however, rounding can collapse nearby values into ties. Define nonfinite handling, tie policy, and where precision promotion occurs rather than declaring the router stable because it subtracts a maximum.

Switch's selective-precision design uses higher precision locally in routing while keeping other work and communicated tensors at lower precision under its stated implementation. Megatron-Core 0.15.0 exposes router dtype choices with their own scope. Promoting a previously rounded low-precision logit cannot recover its missing bits; performing the relevant projection and normalization in a wider path is a different intervention. Compare these choices with matched model parameters and inputs, and inspect the generated execution path rather than assuming a configuration name proves every router operation uses that dtype.

pe=exp⁡(ze−m)∑jexp⁡(zj−m),m=max⁡jzjp_e=\frac{\exp(z_e-m)}{\sum_j\exp(z_j-m)},\qquad m=\max_j z_j
The row is nonempty with finite dimensionless logits. The common shift preserves exact probabilities and ensures shifted exponentials do not exceed one, but it does not guarantee finite-precision equality, preserved ordering after earlier rounding, or a policy for NaN/infinity.↔ Scroll wide equations horizontally.

Margins and logit penalties answer different questions

Suppose every computed logit differs from a reference by at most epsilon. If the gap between the kth and next logit exceeds twice epsilon, no selected logit can fall below an unselected one under that bound. This is a sufficient ordering condition, not an estimate of the actual error or a quality guarantee. Near the boundary, compare selected identities explicitly. A probability difference that looks negligible can change capacity survivors or route a representation to an expert with very different parameters.

Router z-loss, introduced in ST-MoE, penalizes squared log-sum-exp under a stated coefficient and batch reduction. It controls a logit-scale-related quantity rather than directly enforcing uniform hard assignments. Adding a common constant leaves softmax probabilities unchanged but changes log-sum-exp and thus this penalty. It is distinct from entropy regularization and the hard-count/soft-mass auxiliary objective. If several regularizers are combined, log each scaled component and inspect their gradients; improved numerical behavior does not establish that the expert workload became balanced.

Δ=z(k)−z(k+1)>2ϵ ⟹ Sk(z+δ)=Sk(z),∥δ∥∞≤ϵ\Delta=z_{(k)}-z_{(k+1)}>2\epsilon\ \Longrightarrow\ S_k(z+\delta)=S_k(z),\qquad \|\delta\|_\infty\leq\epsilon
Sorted reference logits are descending, 1≤k<E, and Δ>0 excludes a boundary tie. The perturbation bound applies to every logit in the same finite candidate set. This guarantees selected membership under that bound, not identical weights, gradients, or subsequent capacity admission.↔ Scroll wide equations horizontally.

Trace numerical changes through routing and training

Test well-separated logits, exact ties, near ties, large common offsets, and the declared nonfinite cases. Compare reference logits, selected expert identities, selected weights, overflow masks, auxiliary terms, and router-gradient norms. A count histogram can miss swapped token identities. Use fixed routing in one ablation to isolate expert numerical error, then restore live routing to measure selection sensitivity. For a stability intervention, compare convergence at matched data order and token budget as well as immediate throughput and memory cost.

The network lab only evaluates a stable binary sigmoid. Reset, set x=1 and w=1, predict p≈0.622459, then Play or Step. Separately Reset, set x=1 and w=1.05, predict p≈0.634136, then Play or Step. Both control values are reachable. The instrument has no dtype conversion, top-k boundary, logit projection, or z-loss. It shows a continuous probability response; the hand example establishes how a separate finite representation can collapse ordering before such normalization occurs.

Lz=βT∑t=1T(log⁡∑e=1Eexp⁡zt,e)2L_z=\frac{\beta}{T}\sum_{t=1}^{T}\left(\log\sum_{e=1}^{E}\exp z_{t,e}\right)^2
This stated mean z-loss uses T>0 eligible tokens, finite dimensionless logits, and β≥0. Evaluate log-sum-exp stably. Actual frameworks may apply additional distributed or main-loss scaling; the expression is not an entropy or hard-load metric.↔ Scroll wide equations horizontally.

A near tie can disappear before normalization

Two reference logits are [1,1.001]. Round each directly to IEEE bfloat16 with round-to-nearest, ties-to-even, and use a stipulated lowest-index winner for exact ties. Consider top one.

  1. Near one, bfloat16 spacing is 2⁻⁷=0.0078125. The increment 0.001 is below half that spacing, so both stored logits become exactly one.

  2. The reference ordering selects the second expert because 1.001>1. The rounded pair is tied, so the stipulated tie rule selects the first expert instead.

  3. Subtracting the rounded maximum or later promoting those two ones to a wider dtype cannot reconstruct the lost distinction. Wider evaluation before this rounding, or an explicit tolerance study, addresses a different stage of the problem.

The result

An apparently small logit error changes expert identity in this scoped hand example. The result does not prescribe any library's undocumented tie order.

Explore the model

Interactive lab

Interactive lab · network

A continuous probability response is only one part of routing

Reset, set x=1 and w=1, predict p≈0.622459, then Play or Step. Separately Reset, set x=1 and w=1.05, predict p≈0.634136, then Play or Step. The fixed sigmoid does not round logits, choose experts, or implement router z-loss.

A continuous probability response is only one part of routingOne 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 forProbability stability and discrete selection robustness require different evidence.

Check your understanding

What does max-shifting softmax guarantee about logits already rounded into an exact tie?

Try it yourself

Each logit has a known absolute error bound 0.002. Does a boundary gap 0.006 satisfy the stated sufficient condition? What about gap 0.003?

Take a moment to make a prediction or work it out on paper. Open a hint when you need one.

Give me a hint

Compare each gap with twice the error bound, using a strict inequality.

Compare with the solution

Twice the bound is 0.004. Gap 0.006 exceeds it and guarantees unchanged selected membership under the assumptions. Gap 0.003 does not satisfy the condition; a flip is possible but not inevitable. The guarantee does not imply identical probabilities or identical later capacity admission.

Take it with you

  • Stable exponentials do not repair already rounded router logits.
  • Selection margins provide a scoped robustness condition.
  • Selective precision, z-loss, and load balancing target different mechanisms.

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.