A Fix for Softmax and Cross-Entropy
Paper

A Fix for Softmax and Cross-Entropy

Dan Gnanapragasam
2026.10.26
·Arxiv·by Homin.Lee
#Cross-Entropy#Deep Learning#Gradients#Numerical Stability#Softmax

Key Points

  • 1Standard implementations of softmax and cross-entropy suffer from numerical "Softmax Collapse" and "Softmax Cancellation," which lead to vanishing target gradients and precision errors in distillation.
  • 2The authors introduce stable implementations using the `softplus` function and `log1p` arithmetic that prevent gradient rounding and ensure target-non-target gradient symmetry across all floating-point precisions.
  • 3This fix eliminates the "Slingshot" phenomenon in training, improves grokking stability under adaptive optimizers like Adam, and replaces the reliance on empirical floating-point cliffs with predictable equilibria dictated by weight decay and Adam's $\epsilon$.

The paper "A Fix for Softmax and Cross-Entropy" identifies two fundamental numerical instabilities in standard machine learning implementations of softmax and cross-entropy—*Softmax Collapse* and *Softmax Cancellation*—and proposes robust, numerically stable alternatives.

1. Softmax Collapse

Softmax Collapse occurs during high-confidence training where the target logit ztz_t is significantly larger than the non-target logits, resulting in a large logit margin m=zt−log⁡∑j≠tezjm = z_t - \log \sum_{j \neq t} e^{z_j}. In standard implementations, 1+e−m1 + e^{-m} is computed by adding e−me^{-m} to 1.01.0. Once mm exceeds a precision cliff (cd≈16.6c_d \approx 16.6 in fp32), e−me^{-m} is rounded to zero, causing the loss and the target gradient g~t\tilde{g}_t to become exactly zero. Crucially, non-target gradients remain non-zero, causing the total gradient to not sum to zero (∑kg~k>0\sum_k \tilde{g}_k > 0). This asymmetry drives the "Slingshot" phenomenon in Adam/AdamW optimizers, where weight decay and gradient updates compete until a sudden spike in loss occurs.

Methodology: The authors fix this by avoiding the direct addition of 11 in the denominator. Instead, they define the margin mm excluding the target logit and utilize softplus⁡(−m)=log⁡(1+e−m)\operatorname{softplus}(-m) = \log(1 + e^{-m}). By computing L(z,t)=softplus⁡(−m)\mathcal{L}(\mathbf{z}, t) = \operatorname{softplus}(-m), the target gradient is derived as gt=−e−m/(1+e−m)g_t = -e^{-m} / (1 + e^{-m}), which remains accurate even when m>cdm > c_d.

2. Softmax Cancellation

Softmax Cancellation occurs in knowledge distillation or KL regularization where a student model's logits zS\mathbf{z}_S are forced to match teacher logits zT\mathbf{z}_T. Standard implementations compute p=softmax⁡(zT)\mathbf{p} = \operatorname{softmax}(\mathbf{z}_T) and q=softmax⁡(zS)\mathbf{q} = \operatorname{softmax}(\mathbf{z}_S) separately. As zS→zT\mathbf{z}_S \to \mathbf{z}_T, subtracting these probabilities leads to precision loss, leaving a residual noise floor in the gradient even when the logits are equal.

Methodology: The authors reformulate the gradient calculation in log-space using expm1⁡(x)=ex−1\operatorname{expm1}(x) = e^x - 1 and log1p⁡(x)=log⁡(1+x)\operatorname{log1p}(x) = \log(1 + x). Given the logit difference δ=zS−zT\boldsymbol{\delta} = \mathbf{z}_S - \mathbf{z}_T, the difference u=q−p\mathbf{u} = \mathbf{q} - \mathbf{p} is computed as:
dk=δk−∑ipiδid_k = \delta_k - \sum_i p_i \delta_i
sk=dk−log1p⁡(∑ipiexpm1⁡(di))s_k = d_k - \operatorname{log1p}\left(\sum_i p_i \operatorname{expm1}(d_i)\right)
uk=pkexpm1⁡(sk)u_k = p_k \operatorname{expm1}(s_k)
This approach ensures the gradient g=u−q∑iui\mathbf{g} = \mathbf{u} - \mathbf{q} \sum_i u_i is identically zero when zS=zT\mathbf{z}_S = \mathbf{z}_T, preventing the drift observed in standard implementations.

3. Margin Equilibria and Slingshot

The authors demonstrate that when using AdamW, the logit margin does not grow indefinitely but reaches an equilibrium m⋆(ϵ)≈log⁡(1/ϵ)m^\star(\epsilon) \approx \log(1/\epsilon) where the normalized gradient step balances weight decay. In standard implementations, the precision cliff often forces an earlier, unstable termination of this growth. The proposed fix allows models to reach this natural equilibrium smoothly. Experimental results across bf16, fp32, and fp64 formats confirm that these fixes eliminate Slingshot spikes, accelerate grokking, and improve test accuracy by allowing the model to cross precision boundaries without destructive floating-point rounding errors.