Improved numerical accuracy for log_sum_exp, softmax, and log_softmax - #3371
Open
helske wants to merge 7 commits into
Open
Improved numerical accuracy for log_sum_exp, softmax, and log_softmax#3371helske wants to merge 7 commits into
helske wants to merge 7 commits into
Conversation
helske
marked this pull request as draft
August 24, 2026 12:19
Member
|
Hi @helske -- it looks like the actual changes to the source may be missing from your branch, only the test changes seem to have been checked in |
Contributor
Author
|
Yes I run out of time earlier today. Here's the actual changes. Note: I did not change the current two-scalar versions of If |
helske
marked this pull request as ready for review
August 24, 2026 18:16
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
This PR concerns #2802, I already started to work on this on Stancon hackathon with @SteveBronder.
The current primitives of
softmax,log_softmaxandlog_sum_expare numerically stable according to this paper. However, thefwdandrevspecializations are numerically less stable, and their handling of non-finite values is not consistent with the primitives.The new
fwdandrevspecializations of these functions rely on the stablesoftmaxandlog_sum_expprimitives rather than independently reimplementing them. To make this possible, I relaxed therequire_*constraints on thesoftmaxandlog_softmaxprimitives so that they support also also matrix input (operating over all elements of the matrix). If this is not a good idea, alternativelyfwdandrevcould use reshaping/flattening or reimplement the stable softmax (see also below).There is still potential room for improvement:
fwd::log_softmax,fwd::log_sum_exp, andrev::log_sum_expnow compute bothsoftmax(x)andlog_sum_exp(x)which have overlap in computations which could be avoided by computing both in a single pass.max + std::log((v_ref.array() - max).exp().sum());is safe, their suggested algorithm 4.1 excludes the maximum from the sum and useslog1p, which, based on some standalone tests I made could make a difference in some cases. Basically we could do something likeTests
The new tests covers numerical stability and non-finite-value handling in the
fwdandrevspecializations oflog_softmaxandlog_sum_exp. The tests cover inputs with large dynamic ranges, many small probabilities, and infinite values. These tests fail with the current implementations and pass with the proposed changes. At least on my setup, few of the tests have tolerances of 1e-12 which I'm not sure are actually portable. For writing the tests, I used help from LLM.Side Effects
I guess this now exposes support for matrices in
softmaxandlog_softmax?Release notes
Improved numerical accuracy of
log_sum_exp,softmaxandlog_softmaxfunctions.Checklist
Copyright holder: Jouni Helske
The copyright holder is typically you or your assignee, such as a university or company. By submitting this pull request, the copyright holder is agreeing to the license the submitted work under the following licenses:
- Code: BSD 3-clause (https://opensource.org/licenses/BSD-3-Clause)
- Documentation: CC-BY 4.0 (https://creativecommons.org/licenses/by/4.0/)
the basic tests are passing
./runTests.py test/unit)make test-headers)make test-math-dependencies)make doxygen)make cpplint)the code is written in idiomatic C++ and changes are documented in the doxygen
the new changes are tested