
A Fix for Softmax and Cross-Entropy
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 is significantly larger than the non-target logits, resulting in a large logit margin . In standard implementations, is computed by adding to . Once exceeds a precision cliff ( in fp32), is rounded to zero, causing the loss and the target gradient to become exactly zero. Crucially, non-target gradients remain non-zero, causing the total gradient to not sum to zero (). 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 in the denominator. Instead, they define the margin excluding the target logit and utilize . By computing , the target gradient is derived as , which remains accurate even when .
2. Softmax Cancellation
Softmax Cancellation occurs in knowledge distillation or KL regularization where a student model's logits are forced to match teacher logits . Standard implementations compute and separately. As , 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 and . Given the logit difference , the difference is computed as:
This approach ensures the gradient is identically zero when , preventing the drift observed in standard implementations.