Retrieved article excerpt
Open article · Retrieved 2026-10-24T01:29:25.365906+00:00
# A Fix for Softmax and Cross-Entropy
Jonathan T. Barron
Google DeepMind
[email protected]
Dan Gnanapragasam
Google DeepMind
[email protected]
Michael Tschannen
Google DeepMind
[email protected]
Abstract
Standard implementations of softmax, log-softmax, and
cross-entropy have two numerical bugs.
The first bug, *Softmax Collapse*, rounds the target logit's gradient to
zero if that one logit is much larger than the others, resulting in
gradients that no longer sum to zero.
The second bug, *Softmax Cancellation*,
occurs when student logits are close to teacher logits,
and produces non-zero gradients even when the student and teacher logits are
equal.
We present simple implementations of log-softmax, softmax, and
cross-entropy that fix both bugs.
Our fix eliminates the previously observed “slingshot” phenomenon,
speeds up grokking with weight decay, prevents accuracy collapse without weight
decay, stops student networks from drifting away from the teacher during
distillation, and shows that after memorization, logit growth is bounded by an
equilibrium between weight decay and Adam's \(\epsilon\) hyperparameter.
## 1. Introduction[#](https://jonbarron.info/softmaxfix/#sec:intro "Permalink")
paper\_fig2\_legend.png
paper\_fig1a\_hard\_cliff.png
**(a)** Softmax Collapse: \(|g\_{t}|\) vs. margin \(m\)
paper\_fig4a\_floor.png
**(b)** Softmax Cancellation: \(\|\nabla\_{\mathbf{z}\_{\mathrm{S}}}\mathcal{L}\|\_2\) as \(\mathbf{z}\_{\mathrm{S}}
\to \mathbf{z}\_{\mathrm{T}}\)
**Figure 1:** The two bugs in standard softmax and cross-entropy.
**(a) Softmax Collapse:** As a model becomes more confident in the
correct class (thereby increasing the logit margin \(m\)), the true
gradient on the target logit shrinks exponentially, but standard
implementations incorrectly round that gradient to zero once \(m\)
crosses a precision cliff (\(\approx 5.5\) in bf16, \(16.6\) in
fp32, and \(36.7\) in fp64).
**(b) Softmax Cancellation:** In soft-target distillation, as
student logits \(\mathbf{z}\_{\mathrm{S}}\) approach teacher logits \(\mathbf{z}\_{\mathrm{T}}\), the true
gradient shrinks toward zero, but standard implementations lose precision
and plateau at a non-zero noise floor.
Our fix eliminates both bugs and yields accurate gradients across all
precisions.
Naively computing softmax or cross-entropy on logits \(\mathbf{z} \in
\mathbb{R}^{K}\) as \(e^{z\_k} / \sum\_j e^{z\_j}\) overflows if
any logit exceeds the floating-point exponent limit (\(\approx 88\) in
fp32), which is why standard implementations in all deep learning
frameworks (JAX, PyTorch, TensorFlow, etc.) shift the input logits by
subtracting their maximum value \(z\_{\max} = \max\_j z\_j\)
([Higham, 2002](https://jonbarron.info/softmaxfix/#bib-higham2002accuracy); [Blanchard et al., 2021](https://jonbarron.info/softmaxfix/#bib-blanchard2021accurately)):
\[\tilde{z}\_k = z\_k - z\_{\max}\,, \quad
\operatorname{logsoftmax}(\mathbf{z})\_k
= \tilde{z}\_k - \log \sum\_j e^{\tilde{z}\_j}\,,
\quad
q\_k = \operatorname{softmax}(\mathbf{z})\_k
= \frac{e^{\tilde{z}\_k}}
{\sum\_j e^{\tilde{z}\_j}}\,.\]
(1)
For a target distribution \(\mathbf{p} \in \Delta^{K-1}\),
cross-entropy (CE) is \(\mathcal{L}(\mathbf{z}, \mathbf{p}) =
-\sum\_k p\_k\,\operatorname{logsoftmax}(\mathbf{z})\_k\), with exact gradient
\(\mathbf{g} = \nabla\_{\mathbf{z}} \mathcal{L}(\mathbf{z}, \mathbf{p}) = \mathbf{q} -
\mathbf{p}\).
Though subtracting \(z\_{\max}\) prevents overflow, two numerical bugs remain.
The first bug, *Softmax Collapse* ([Figure 1a](https://jonbarron.info/softmaxfix/#fig:fig1a),
[Prieto et al., 2025](https://jonbarron.info/softmaxfix/#bib-prieto2025grokking)), occurs when a
model correctly predicts the target class \(t \in \{1, \dots,
K\}\) with high confidence, so that \(z\_{t} = z\_{\max}\) is
much larger than all non-target logits \(z\_j\) (\(j \neq t\)).
We measure this confidence by the *logit margin*, which is how much larger
the target logit is than the log-sum-exp of all non-target logits:
\[m = z\_{t}
- \log \sum\_{j \neq t} e^{z\_j}\,.\]
(2)
In [Equation 1](https://jonbarron.info/softmaxfix/#eq:std_softmax), subtracting \(z\_{\max} = z\_{t}\) sets
the shifted target logit to \(\tilde{z}\_{t} = 0\) (so
\(e^{\tilde{z}\_{t}} = 1\) in the denominator), while the \(K
- 1\) non-target exponentials sum to a small positive number \(e^{-m} =
\sum\_{j \neq t} e^{z\_j - z\_{t}} \ll 1\).
Floating-point numbers have much finer precision near \(0\) than near \(1\): in
fp32, values near \(0\) can be as small as \({\sim}10^{-38}\), whereas
values added to \(1.0\) are lost to rounding below \({\sim}10^{-7}\).
Because standard implementations form the denominator \(1 + e^{-m}\) by
adding \(e^{-m}\) to \(1.0\), the \(e^{-m}\) term is lost to rounding
once the margin \(m\) exceeds a threshold \(c\_{d}\) that we call the
*precision cliff* (\(c\_{16} \approx 5.5\) in bf16,
\(c\_{32} \approx 16.6\) in fp32, and \(c\_{64} \approx
36.7\) in fp64).
When \(m > c\_{d}\), the denominator \(1 + e^{-m}\) evaluates to
exactly \(1.0\), which causes \(q\_{t} = 1/(1 + e^{-m})\) to
evaluate to \(1.0\) and zeroes out both the loss and the target gradient
\(\tilde{g}\_{t} = q\_{t} - 1 = 0.0\).
However, the non-target gradients \(\tilde{g}\_j = q\_j = e^{z\_j -
z\_{t}} / (1 + e^{-m})\) (\(j \neq t\)) are evaluated
near \(0\) without adding \(1.0\) to their numerators, so they remain positive all
the way down to exponent underflow (\(m \approx 88\)).
Because the target gradient rounds to \(0.0\) while the non-target gradients do
not, the gradients no longer sum to zero (\(\sum\_k \tilde{g}\_k > 0\)).
When training with adaptive optimizers such as Adam and AdamW
([Kingma & Ba, 2015](https://jonbarron.info/softmaxfix/#bib-kingma2015adam); [Loshchilov & Hutter, 2019](https://jonbarron.info/softmaxfix/#bib-loshchilov2019decoupled)), which normalize each gradient
by a moving average of its magnitude, this asymmetry causes the recurring
“slingshot” loss spikes observed by [Thilak et al. (2022)](https://jonbarron.info/softmaxfix/#bib-thilak2022slingshot) and the
grokking failures studied by [Prieto et al. (2025)](https://jonbarron.info/softmaxfix/#bib-prieto2025grokking).
While prior works used surrogate losses ([Prieto et al., 2025](https://jonbarron.info/softmaxfix/#bib-prieto2025grokking)), gradient
projection ([Liu et al., 2026](https://jonbarron.info/softmaxfix/#bib-liu2026grokking)), or fp64 casting
([Nanda et al., 2023](https://jonbarron.info/softmaxfix/#bib-nanda2023progress); [Varma et al., 2023](https://jonbarron.info/softmaxfix/#bib-varma2023explaining)) to ameliorate these issues, we
instead fix log-softmax, softmax, and cross-entropy directly
([Section 2](https://jonbarron.info/softmaxfix/#sec:bug1)).
The second bug, *Softmax Cancellation* ([Figure 1b](https://jonbarron.info/softmaxfix/#fig:fig4a)), occurs in
knowledge distillation ([Hinton et al., 2015](https://jonbarron.info/softmaxfix/#bib-hinton2015distilling)) and KL regularization,
where the target \(\mathbf{p} = \operatorname{softmax}(\mathbf{z}\_{\mathrm{T}})\) comes from teacher logits
\(\mathbf{z}\_{\mathrm{T}}\).
Standard implementations compute \(\mathbf{q} = \operatorname{softmax}(\mathbf{z}\_{\mathrm{S}})\) and
\(\mathbf{p} = \operatorname{softmax}(\mathbf{z}\_{\mathrm{T}})\) separately and subtract them to get
\(\tilde{\mathbf{g}} = \mathbf{q} - \mathbf{p}\).
This means that if \(\mathbf{z}\_{\mathrm{S}}\) is close to \(\mathbf{z}\_{\mathrm{T}}\), subtracting these
nearly equal probabilities loses precision, and slight differences between the
forward-pass softmax and backward-pass log-softmax formulas leave a
non-zero gradient even when \(\mathbf{z}\_{\mathrm{S}} = \mathbf{z}\_{\mathrm{T}}\).
We fix this by computing \(\mathbf{q} - \mathbf{p}\) directly from \(\mathbf{z}\_{\mathrm{S}} -
\mathbf{z}\_{\mathrm{T}}\) in log space ([Section 3](https://jonbarron.info/softmaxfix/#sec:bug2)).
## 2. Softmax Collapse[#](https://jonbarron.info/softmaxfix/#sec:bug1 "Permalink")
When the target logit \(z\_{t} = z\_{\max}\) is the largest logit,
shifting by \(z\_{\max}\) in [Equation 1](https://jonbarron.info/softmaxfix/#eq:std_softmax) sets
\(\tilde{z}\_{t} = 0\) and \(\tilde{z}\_j = z\_j -
z\_{t} < 0\) for \(j \neq t\), so the denominator is
\(\sum\_i e^{\tilde{z}\_i} = 1 + \sum\_{j \neq t} e^{z\_j -
z\_{t}} = 1 + e^{-m}\).
Mathematically, the hard-target loss is \(\mathcal{L}(\mathbf{z}, t) =
-\operatorname{logsoftmax}(\mathbf{z})\_{t} = \log(1 + e^{-m})\), with gradients
\[g\_{t}
= \underbrace{\frac{1}{1 + e^{-m}} - 1}\_{\text{Standard}}
= \underbrace{-\frac{e^{-m}}{1 + e^{-m}}}\_{\text{Fixed}}\,,
\qquad
g\_j = \frac{e^{z\_j - z\_{t}}}{1 + e^{-m}}
\quad \text{for } j \neq t\,,\]
(3)
which sum to zero (\(\sum\_k g\_k = 0\)).
In floating point, however, the two expressions for \(g\_{t}\) in
[Equation 3](https://jonbarron.info/softmaxfix/#eq:hard_exact) behave very differently once \(m > c\_{d}\),
where \(e^{-m}\) is too small to change \(1.0\) when added to it
([Figure 1a](https://jonbarron.info/softmaxfix/#fig:fig1a)).
Standard implementations add \(1\) and \(e^{-m}\) to form the denominator \(1 +
e^{-m}\), which rounds to \(1\) when \(m > c\_{d}\).
In the forward pass, \(\log(1 + e^{-m})\) then evaluates to \(\log(1) = 0\),
zeroing out both \(\operatorname{logsoftmax}(\mathbf{z})\_{t}\) and \(\mathcal{L}(\mathbf{z},
t)\).
In the backward pass, standard cross-entropy implementations compute
\(q\_{t} = 1/(1 + e^{-m}) = 1\) and subtract \(1\) to get
\(\tilde{g}\_{t} = q\_{t} - 1 = 0\).
In contrast, our fixed expression \(-e^{-m}/(1 + e^{-m})\) keeps
\(e^{-m}\) in the numerator: even when the denominator \(1 + e^{-m}\)
rounds to \(1.0\), dividing \(-e^{-m}\) by \(1.0\) preserves \(g\_{t}
\approx -e^{-m}\) down to exponent underflow (\(m \approx 88\)).
While \(\tilde{g}\_{t}\) rounds to zero when \(m > c\_{d}\), the
non-target gradients do not.
For each non-target class \(j \neq t\), standard cross-entropy computes
\(\tilde{g}\_j = q\_j = e^{z\_j - z\_{t}} / (1 +
e^{-m})\).
Because the numerator \(e^{z\_j - z\_{t}}\) is evaluated directly
from the negative difference \(z\_j - z\_{t}\) without adding
\(1\), it remains accurate down to exponent underflow while the denominator rounds
to \(1\).
When \(m > c\_{d}\), standard cross-entropy therefore zeroes out only the
target entry \(t\) while all \(K - 1\) non-target gradients
remain positive:
\[\tilde{g}\_k = \mathbf{1}{[k \neq t]}\,g\_k\,, \qquad
\sum\_k \tilde{g}\_k = -g\_{t}
= \frac{e^{-m}}{1 + e^{-m}} > 0
\quad \text{for } m > c\_{d}\,.\]
(4)
The same bug affects standalone \(\operatorname{softmax}(\mathbf{z})\) ([Qiu & Yao, 2026](https://jonbarron.info/softmaxfix/#bib-qiu2026why)): once
\(m > c\_{d}\), its backward pass rounds \(q\_{i\_{\max}}\) (\(i\_{\max} =
\arg\max\_i z\_i\)) to \(1\) and zeroes \(\tilde{g}\_{i\_{\max}}\) while the other
gradients stay non-zero.
This bug can be fixed by never adding \(1\) to \(e^{-m}\) directly.
In \(\operatorname{logsoftmax}(\mathbf{z})\) and \(\operatorname{softmax}(\mathbf{z})\), only the largest logit
\(z\_{i\_{\max}}\) is shifted to \(0\) (where \(e^0 = 1\) in the denominator).
Instead of including \(e^0 = 1\) in the sum of exponentials, we set
\(\tilde{z}\_{i\_{\max}} = 0\), compute the margin \(m\) by excluding \(i\_{\max}\)
from the log-sum-exp ([Blanchard et al., 2021](https://jonbarron.info/softmaxfix/#bib-blanchard2021accurately)), and combine them with a
numerically stable \(\operatorname{softplus}(x) = \log(1 + e^x)\) (with
\(\operatorname{softmax}(\mathbf{z})\_k = \exp(\operatorname{logsoftmax}(\mathbf{z})\_k)\)):
\[\tilde{z}\_k
= \mathbf{1}{[k \neq i\_{\max}]}{(z\_k - z\_{i\_{\max}})}\,,
\;\;
m = -\operatorname\*{LSE}\_{j \neq i\_{\max}} \tilde{z}\_j\,,
\;\;
\operatorname{logsoftmax}(\mathbf{z})\_k = \tilde{z}\_k - \operatorname{softplus}(-m)\,.\]
(5)
For hard-target cross-entropy with target \(t\), we can use
\(t\) directly in place of \(i\_{\max}\) without an \(\arg\max\