Manuscript Offprint

arXiv:2610.36427

Property-Based Testing for Robust Softmax and Cross-Entropy

Muhammad Maaz

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

Abstract

A recent paper of Barron et al. (2026) identified two numerical issues with the softmax and cross-entropy functions in virtually all standard machine learning libraries (e.g., PyTorch and JAX). We show that these issues can be recovered through simple property-based tests in Hypothesis. However, our tests identify an additional edge case where their implementation fails. We then propose an even more robust softmax implementation which passes these tests and also supports use in causal attention masking. We compare our implementation with theirs and discuss some of the differences. Overall, this work shows that property-based testing can be a powerful tool for discovering and fixing numerical issues in machine learning libraries. Our implementations and property-based tests can be found at https://github.com/mmaaz-git/softmax-fix.

1 Introduction

Suppose we have logits z∈RKz \in \mathbb{R}^K. It is common to compute softmax⁡(z)=ez/∑i=1Kezi\operatorname{softmax}(z) = e^z / \sum_{i=1}^K e^{z_i} by subtracting the maximum logit (Higham, 2002), which is more numerically stable. More precisely, we compute the log-softmax as follows:

logsoftmax(z)=(z−max⁡izi)−log⁡∑i=1Kezi−max⁡izi\mathrm{logsoftmax}(z)=(z-\max_{i}z_{i})-\log\sum_{i=1}^{K}e^{z_{i}-\max_{i}z_{i}}

Then, we can compute the softmax as softmax(z)=elogsoftmax(z)\mathrm{softmax}(z)=e^{\mathrm{logsoftmax}(z)}, or directly by applying the max-shift trick:

softmax(z)=ez−max⁡izi/∑i=1Kezi−max⁡izi\mathrm{softmax}(z)=e^{z-\max_{i}z_{i}}/\sum_{i=1}^{K}e^{z_{i}-\max_{i}z_{i}}

The max-shift trick is widely used in practice, e.g., in PyTorch and JAX.

Given logits zz and a target probability vector p∈ΔK−1\boldsymbol{p} \in \Delta^{K-1}, the soft-target cross-entropy is CE(z,p)=−∑i=1Kpi[log⁡softmax(z)]i\mathrm{CE}(z, \boldsymbol{p}) = -\sum_{i=1}^K p_i[\log \mathrm{softmax}(z)]_i. When p=et\boldsymbol{p} = e_t is a one-hot vector corresponding to target class tt, this simplifies to the hard-target cross-entropy: CE(z,et)=−[log⁡softmax(z)]t\mathrm{CE}(z, e_t) = -[\log \mathrm{softmax}(z)]_t.

Recently, Barron et al. (2026) observed that even with the max-shift trick, two possible numerical issues can arise: (1) softmax collapse, where, as the model becomes more confident in the correct class, the gradient of the hard-target cross-entropy with respect to the target logit incorrectly rounds to 0, and (2) softmax cancellation, where the L2L^2 norm of the gradient of the soft-target cross-entropy incorrectly plateaus at a non-zero value as the student logits approach the teacher logits. They claim that these issues were found and fixed using Gemini Flash 3.7 (Barron, 2026). The problem of softmax collapse in grokking was previously studied by Prieto et al. (2025).

Preprint.

However, we show that these issues can also be found via property-based testing. Property-based testing is a type of software testing where an abstract property of the system under test is specified, and then random inputs are generated and checked against this property. Properties can pertain to single functions (e.g., that for all inputs, the output should be non-negative), or, more powerfully, to multiple functions (e.g., that encoding and decoding yields the original input). Property-based testing began with QuickCheck in Haskell (Claessen and Hughes, 2000), and has since been adopted by many other programming languages, including in Python, in the Hypothesis library (MacIver et al., 2019). Both of these issues expose failures of key mathematical properties of the softmax and cross-entropy functions, which take no more than a few lines of code to test. Property-based testing is increasingly being adopted in the test suites of popular software libraries, like NumPy, and in fact has been used to discover bugs in NumPy and other libraries (Maaz et al., 2025).

However, we will show that Barron et al. (2026)'s fix still fails. We propose a better fix which is even more robust, and show that this allows the new softmax to be used in the setting of causal attention masking. We release our implementations and tests at the codebase at https://github.com/mmaaz-git/softmax-fix.