arXiv(opens in a new tab)cs.LGOct 2026Cited by 3 papers on PaperVantage

Manuscript Offprint

arXiv:2610.31642

A Fix for Softmax and Cross-Entropy

Jonathan T. Barron, Dan Gnanapragasam, Michael Tschannen

Open access. PaperVantage converted this article to text, so layout and figures may differ from the original.

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

Naively computing softmax or cross-entropy on logits z∈RK\mathbf{z} \in \mathbb{R}^K as ezk/∑jezje^{z_k} / \sum_j e^{z_j} overflows if any logit exceeds the floating-point exponent limit ( ≈88\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 zmax⁡=max⁡jzjz_{\max} = \max_j z_j (Higham, 2002):

z~k=zk−zmax⁡,logsoftmax(z)k=z~k−log⁡∑jez~j,qk=softmax(z)k=ez~k∑jez~j.(1)\tilde{z}_{k}=z_{k}-z_{\max},\quad\mathrm{logsoftmax}(\mathbf{z})_{k}=\tilde{z}_{k}-\log\sum_{j}e^{\tilde{z}_{j}},\quad q_{k}=\mathrm{softmax}(\mathbf{z})_{k}=\frac{e^{\tilde{z}_{k}}}{\sum_{j}e^{\tilde{z}_{j}}}. \tag{1}

For a target distribution p∈ΔK−1\mathbf{p} \in \Delta^{K-1}, cross-entropy is then computed as L(z,p)=−∑kpklog⁡softmax(z)k\mathcal{L}(\mathbf{z}, \mathbf{p}) = -\sum_k p_k \log \text{softmax}(\mathbf{z})_k, whose exact gradient is g=∇zL(z,p)=q−p\mathbf{g} = \nabla_{\mathbf{z}} \mathcal{L}(\mathbf{z}, \mathbf{p}) = \mathbf{q} - \mathbf{p}. Though subtracting zmax⁡z_{\max} prevents overflow, two numerical bugs remain.

Standard CE, float64

Fixed CE, float64

Standard CE, float32

Fixed CE, float32

Standard CE, bfloat16

Fixed CE, bfloat16

Figure

(a) Softmax Collapse: ∣gt∣|g_t| vs. margin m.

(b) Softmax Cancellation: ∥∇zSL∥2\|\nabla_{\mathbf{z}_S} \mathcal{L}\|_2 as zS→zT\mathbf{z}_S \to \mathbf{z}_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 ( ≈5.5\approx 5.5 in bf16, 16.6 in fp32, and 36.7 in fp64). (b) Softmax Cancellation: In soft-target distillation, as student logits zSz_S approach teacher logits zTz_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.

The first bug, Softmax Collapse (Figure 1a), occurs when a model correctly predicts the target class t∈{1,…,K}t \in \{1, \ldots, K\} with high confidence, so that zt=zmax⁡z_t = z_{\max} is much larger than all non-target logits zjz_j ( j≠tj \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=zt−log⁡∑j≠tezj.(2)m=z_{t}-\log\sum_{j\neq t}e^{z_{j}}. \tag{2}

In Equation 1, subtracting zmax⁡=ztz_{\max} = z_t sets the shifted target logit to z~t=0\tilde{z}_t = 0 (so ez~t=1e^{\tilde{z}_t} = 1 in the denominator), while the K−1K - 1 non-target exponentials sum to a small positive number e−m=∑j≠tezj−zt≪1e^{-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 ∼10−38\sim 10^{-38}, whereas values added to 1.0 are lost to rounding below ∼10−7\sim 10^{-7}. Because standard implementations form the denominator 1+e−m1 + e^{-m} by adding e−me^{-m} to 1.0, the e−me^{-m} term is lost to rounding once the margin mm exceeds a threshold cdc_d that we call the precision cliff ( c16≈5.5c_{16} \approx 5.5 in bf16, c32≈16.6c_{32} \approx 16.6 in fp32, and c64≈36.7c_{64} \approx 36.7 in fp64). When m>cdm > c_d, the denominator 1+e−m1 + e^{-m} evaluates to exactly 1.0, which causes qt=1/(1+e−m)q_t = 1/(1 + e^{-m}) to evaluate to 1.0 and zeroes out both the loss and the target gradient g~t=qt−1=0.0\tilde{g}_t = q_t - 1 = 0.0. However, the non-target gradients g~j=qj=ezj−zt/(1+e−m)\tilde{g}_j = q_j = e^{z_j - z_t} / (1 + e^{-m}) ( j≠tj \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≈88m \approx 88). Because the target gradient rounds to 0.0 while the non-target gradients do not, the gradients no longer sum to zero ( ∑kg~k>0\sum_k \tilde{g}_k > 0). When training with adaptive optimizers such as Adam and AdamW (Kingma & Ba, 2015; Loshchilov & Hutter, 2019), 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) and the grokking failures studied by Prieto et al. (2025). While prior works used surrogate losses (Prieto et al., 2025), gradient projection (Liu et al., 2026), or fp64 casting (Nanda et al., 2023; Varma et al., 2023) to mitigate these issues, we instead fix log-softmax, softmax, and cross-entropy directly (Section 2).

The second bug, Softmax Cancellation (Figure 1b), occurs in knowledge distillation (Hinton et al., 2015) and KL regularization, where the target p=softmax(zT)\mathbf{p} = \text{softmax}(\mathbf{z}_{\mathrm{T}}) comes from teacher logits zT\mathbf{z}_{\mathrm{T}}. Standard implementations compute q=softmax(zS)\mathbf{q} = \text{softmax}(\mathbf{z}_{\mathrm{S}}) and p=softmax(zT)\mathbf{p} = \text{softmax}(\mathbf{z}_{\mathrm{T}}) separately and subtract them to get g~=q−p\tilde{\mathbf{g}} = \mathbf{q} - \mathbf{p}. This means that if zS\mathbf{z}_{\mathrm{S}} is close to zT\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 zS=zT\mathbf{z}_{\mathrm{S}} = \mathbf{z}_{\mathrm{T}}. We fix this by computing q−p\mathbf{q} - \mathbf{p} directly from zS−zT\mathbf{z}_{\mathrm{S}} - \mathbf{z}_{\mathrm{T}} in log space (Section 3).

References (19)