Softmax collapse and cancellation: softmax and cross-entropy at their numerical limits

Barron, Gnanapragasam and Tschannen (2026) [barron2026] showed that the standard way of computing softmax, log-softmax and cross-entropy has a numerical defect in the gradient, which they call softmax collapse. SKaiNET had it until issue #1646. This page states the defect, what SKaiNET does about it, and how that is checked. A second defect of the same paper, for distillation, is covered at the end.

The defect

For logits \(z \in \mathbb{R}^C\) and a target class \(t\), let \(q = \operatorname{softmax}(z)\). Cross-entropy and its gradient are

\[L = -\log q_t, \qquad g_k = \frac{\partial L}{\partial z_k} = q_k - [k = t], \qquad \sum_k g_k = 0\]

Define the margin of the target as in [barron2026]:

\[m = z_t - \log \sum_{j \ne t} e^{z_j}\]

Then \(q_t = 1 / (1 + e^{-m})\), and the gradient in terms of the margin is

\[g_t = -\frac{e^{-m}}{1 + e^{-m}}, \qquad g_j = \frac{e^{z_j - z_t}}{1 + e^{-m}} \quad (j \ne t)\]

The usual implementation computes \(g_t\) as \(q_t - 1\). Once \(e^{-m}\) is smaller than half a rounding step at 1, \(1 + e^{-m}\) is exactly 1, so \(q_t\) is exactly 1 and \(g_t\) is exactly 0. The other entries \(g_j\) are small positive numbers and are computed correctly. The gradients of the row then sum to \(e^{-m} / (1 + e^{-m}) > 0\), not to 0.

The margin at which this sets in depends on the float type. The values are from [barron2026]:

Float type Margin above which \(g_t\) is exactly 0

BF16

about 5.5

FP32

about 16.6

FP64

about 36.7

Three results are affected, all for the same reason:

  • logSoftmax returns exactly 0 at the dominating entry, where the value is \(-\operatorname{log1p}(e^{-m})\).

  • The gradient of softmax at its dominating entry contains the factor \(1 - q\), which is 0.

  • Cross-entropy has a loss of exactly 0 and no gradient at the target.

A margin of 17 is not exotic. A classifier that has fitted its training set, or a language model that is sure of the next token, reaches it. [barron2026] connect the unbalanced gradient to loss spikes with Adam and to stalled generalisation after memorisation; SKaiNET has not reproduced those experiments.

What SKaiNET does

Forward of logSoftmax

The largest logit, at index \(i_{\max}\), is taken out of the sum:

\[\operatorname{logsoftmax}(z)_k = (z_k - z_{\max}) - \operatorname{log1p}\Bigl(\sum_{j \ne i_{\max}} e^{z_j - z_{\max}}\Bigr)\]

This is the form of [barron2026]. log1p keeps a sum that is far below 1, which \(\log(1 + \cdot)\) rounds away. The Kotlin kernel, the native kernel (also used by the WebAssembly backend) and the StableHLO export all use it.

The StableHLO export has no index of the maximum, so it counts the entries that are equal to the maximum. With \(n\) such entries and \(s\) the sum of the exponentials of the others:

\[\log \sum_j e^{z_j - z_{\max}} = \log(n + s) = \log n + \operatorname{log1p}(s / n)\]

For \(n = 1\) this is the form above.

The forward of softmax is unchanged. A probability that rounds to 1 is the correct float32 value of that probability.

Backward of softmax and logSoftmax

Both gradients sum to zero along the class dimension (for logSoftmax, \(g = d - qD\) with \(D = \sum_j d_j\) and \(\sum_k q_k = 1\)). The backward pass computes \(g\) from the probabilities as before and then applies

\[g'_k = g_k - q_k \sum_j g_j\]

In exact arithmetic the sum is 0 and \(g' = g\). In floating point, write \(G\) for the true value at the dominating entry \(i\), \(\varepsilon\) for the error of the computed one, and \(O\) for the sum of the other entries, which are accurate. Then \(G = -O\), the computed sum is \(\varepsilon\), and

\[g'_i = (G + \varepsilon) - q_i\,\varepsilon = G + \varepsilon\,(1 - q_i)\]

The error is multiplied by \(1 - q_i\), which is tiny exactly when the defect occurs. The other entries change by \(q_k \varepsilon\), far below their own rounding.

This correction is not the formulation of [barron2026], who restate cross-entropy with the target taken out (\(L = \operatorname{softplus}(-m)\)). It was chosen here because it fixes softmax, logSoftmax and every loss built on them with existing tensor operations, and because a traced backward pass exports it to StableHLO like any other operation.

Cross-entropy

CrossEntropyLoss with class indices is \(-\sum_k [k = t] \operatorname{logsoftmax}(z)_k\). It needs no change of its own: the forward gives \(L = \operatorname{log1p}(e^{-m})\), and the backward pass gives the gradient above.

How it is checked

Suite TS-021 of skainet-ground-truth holds 15 cases with gradients: the worked example of [barron2026], margins from 2 to 60, two equal largest logits, a class dimension that is not the last, each reduction of the loss, and ordinary values.

Its reference is not torch.softmax, which has the same defect in float64 at a larger margin. It is the log1p form in float64, and every case is asserted against 50-digit arithmetic before it is stored. The usual absolute tolerance of 1e-5 cannot see values of 1e-8, so the suite states a relative tolerance of 1e-4, and GroundTruthIntegrationTest uses it.

The worked example, \(z = (9, -9, -9)\) with target 0 (margin 17.3), in FP32:

Before Now, equal to the reference

logSoftmax(z)

[0, -18, -18]

[-3.046e-8, -18, -18]

gradient of softmax(z)[0]

[0, -1.523e-8, -1.523e-8]

[3.046e-8, -1.523e-8, -1.523e-8]

cross-entropy

0

3.046e-8

gradient of cross-entropy

[0, 1.523e-8, 1.523e-8]

[-3.046e-8, 1.523e-8, 1.523e-8]

LogSoftmaxDominantLogitTest holds the forward on every target, CrossEntropyBackwardTest the two gradients, and NativeFusedKernelsTest the native kernel. The exported StableHLO module of logSoftmax was compiled and run with IREE on a row with a margin of 17 and a row with two equal largest logits; it agrees with the exact values to a relative error of 2e-8.

The second defect: softmax cancellation

[barron2026] describe a second defect, for targets that are probabilities, as in knowledge distillation. With \(p = \operatorname{softmax}(z_T)\) from a teacher and \(q = \operatorname{softmax}(z_S)\) from a student, the gradient of the cross-entropy is \(q - p\). When the student is close to the teacher, that is the difference of two nearly equal numbers: the result is the rounding error of the two, and it is not zero when the logits are equal.

A loss that receives \(p\) cannot avoid this. Given \(p\) as float32 numbers, \(q - p\) is only known to the rounding of \(p\), and CrossEntropyLoss with probabilities as targets is within about one rounding step of it. The information that is missing is the teacher’s logits.

DistillationLoss therefore takes both sets of logits. With a temperature \(T\), \(p = \operatorname{softmax}(z_T / T)\), \(q = \operatorname{softmax}(z_S / T)\) and \(\delta = (z_S - z_T) / T\), it uses the forms of [barron2026]:

\[d_k = \delta_k - \sum_i p_i \delta_i, \qquad \operatorname{KL}(p \,\|\, q) = \operatorname{log1p}\Bigl(\sum_i p_i \operatorname{expm1}(d_i)\Bigr), \qquad s_k = d_k - \operatorname{KL} = \log\frac{q_k}{p_k}\]
\[u_k = p_k \operatorname{expm1}(s_k), \qquad q_k - p_k = u_k - q_k \sum_i u_i\]

Every quantity is computed from \(\delta\), which is small where the result is small. For \(\delta = 0\) every step is exactly 0. Where \(|s_k| \ge 1\) the plain difference \(q_k - p_k\) is accurate and is used. The last step makes the gradient sum to zero.

The value of the loss is \(T^2 \operatorname{KL}(p \,\|\, q)\) and its gradient with respect to the student logits is \(T (q - p)\); the factor \(T^2\) is the convention of [hinton2015].

The tape cannot derive this gradient: differentiating the forms above brings the cancellation back. The loss sets it with stopGradient, as \(\operatorname{sg}(v) + (\ell - \operatorname{sg}(\ell))\) with \(\ell = \sum_k \operatorname{sg}(g_k)\, z_{S,k}\): the bracket is exactly 0, and its gradient is \(g\). stopGradient and log1p are tensor operations of their own, so the loss runs on every backend and exports with a traced graph.

The worked example of [barron2026], \(z_T = (2, 1, 0)\) and \(z_S = z_T + (-10^{-7}, 10^{-7}, 0)\), in FP32:

Gradient

CrossEntropyLoss with \(p\) as target

[-5.465e-8, 4.653e-8, 8.121e-9]

DistillationLoss

[-4.596e-8, 4.144e-8, 4.513e-9]

Reference (50-digit arithmetic)

[-4.596e-8, 4.144e-8, 4.513e-9]

With equal logits the first row is [-1.487e-8, 9.43e-9, 5.44e-9] and DistillationLoss gives exactly [0, 0, 0].

Suite TS-022 holds 14 cases: the worked example, equal logits, a student within 1e-6 to 1e-2 of the teacher, a confident teacher, temperatures 1, 2 and 4, each reduction, and a student far from the teacher. Its reference is the form above in float64, asserted against 50-digit arithmetic. A gradient is compared with a relative tolerance of 1e-4 plus 1e-6 of the largest entry of that case, so a case with equal logits demands exactly zero. DistillationLossTest holds the properties on every target. The two operations were compiled and run with IREE in FP32, FP16 and BF16; the loss as a whole was not.

The value of the loss for a student within about 1e-4 of the teacher is below 1e-9 and is rounding noise around zero (never negative). The gradient is not affected.

Limits

  • Margins above about 88 in FP32: \(e^{-m}\) leaves the float range, and the gradient at the target is 0 because no float is closer to its value.

  • CrossEntropyLoss with probabilities as targets is unchanged. Use DistillationLoss when the targets come from the logits of another model.

References

  • [barron2026] Barron, J. T., Gnanapragasam, D., Tschannen, M. (2026). A Fix for Softmax and Cross-Entropy. arXiv:2610.31642 — the defect, the margins per float type, the log1p form of log-softmax, the forms for distillation from logits, and the two worked examples used above.

  • [hinton2015] Hinton, G., Vinyals, O., Dean, J. (2015). Distilling the Knowledge in a Neural Network. arXiv:1503.02531 — distillation with a temperature, and the factor \(T^2\).