[tmva][sofie] Compute LogSoftmax without taking log of softmax - #23554
Merged
Merged
Conversation
LogSoftmax took the log of the normalized softmax. Where exp(x - max) underflows to 0 in float, this gave -inf instead of a large negative value, and it lost precision for values close to underflow. Compute (x - max) - log(sum) instead, in both the last-axis and the generic code paths. Add regression tests LogSoftmaxLargeRange and LogSoftmaxLargeRangeAxis0, the first LogSoftmax tests. Their expected values come from a NumPy reference, since onnx.reference also takes the log of the softmax.
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.
This Pull request:
Changes or fixes:
LogSoftmax was computed as the log of the normalized softmax. Where
exp(x - max)underflows to 0 in float (inputs about 100 or more below the maximum), this gave-infinstead of a large negative value, and values close to the underflow lost precision (e.g. -99.98 instead of -100). Both code paths ofROperator_Softmax(last axis and generic axis) now compute(x - max) - log(sum), which never takes the log of an underflowed value. Softmax itself is unchanged.Adds the first LogSoftmax tests,
LogSoftmaxLargeRange(last axis) andLogSoftmaxLargeRangeAxis0(generic path), each with one row of far-apart and one row of ordinary values. Their expected values come from a new_logsoftmax_referenceingenerate_input_models.py, becauseonnx.referencealso takes the log of the softmax and returns-infhere (onnxruntime gives the correct values). Both tests fail without the fix and pass with it.Checklist:
ctest -R sofie: all 8 pass)This PR fixes #23547
AI disclosure: I found this bug with an AI-assisted differential-testing harness (SOFIE vs onnxruntime).