Repository navigation
dnn: compute LogSoftmax without taking the log of softmax (5.x) 🤖🤖🤖 - #30816
Merged
abhishek-gola merged 2 commits intoNov 15, 2026
Merged
Conversation
LogSoftmax must give x - max - log(sum(exp(x - max))) for values about 104 or more below the row maximum, where exp underflows in float32. Covers a short row and one long enough for the SIMD loops, on the CPU and OpenCL targets. Port of the test from opencv#30545.
logSoftmax() took the log of the softmax output, so elements whose exp(x - max) underflows came out as -inf. The dispatched softmax kernel already has the row maximum and the sum of exp((x - max) * scale), so softmax() takes a logSoftmax flag and the log path writes (x - max) * scale - log(sum) directly. The OpenCL kernels had the same problem with LOG_SOFTMAX: they now keep x - max, add up exp(x - max) and return x - max - log(sum). This covers the generic kernels in softmax.cl and the subgroup kernels in softmax_loss.cl. Port of opencv#30545 to 5.x.
5 of 6 tasks
abhishek-gola
approved these changes
Nov 14, 2026
There was a problem hiding this comment.
Status: 🟡 Yellow
The 5.x port computes log-softmax directly from centered logits on the CPU and in both OpenCL kernel families, so values whose exponent underflows remain finite. The adapted CPU path preserves the existing scale argument, and the parameterized regression covers both a short row and the SIMD loop on the OpenCV CPU and OpenCL targets. I found no actionable source issue; exact-head CI is still in progress.
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.
Port of #30545 to 5.x, as asked in #30545 (comment). Fixes #30544 for 5.x.
logSoftmax()tooklogof thesoftmaxoutput, so every element whoseexp(x - max)underflows in float32 came out as-inf(e.g.[-1000, 0, 1000]gave[-inf, -inf, 0]instead of[-2000, -1000, 0]). The OpenCL kernels withLOG_SOFTMAXhad the same problem.softmax_kernel inactivation_kernels.simd.hpp, which takes ascale. As in the 4.x review,softmax()takes alogSoftmaxflag (defaultfalse), and the log path writes(x - max) * scale - log(sum)from the row maximum and sum the kernel already computes.logSoftmax()calls it withtrue. The plain softmax path is unchanged.softmax.cl,softmax_loss.clandsoftmax_layer.cppchanges from dnn: compute LogSoftmax without taking the log of softmax 🤖🤖🤖 #30545 applied to 5.x as they are.The test is the one merged in #30545 (
Layer_Test_LogSoftmax.DoesNotUnderflow, a 2x3 input and a 1x17 input to cover the SIMD loops, compared with a double-precision reference, on every OpenCV backend target). On 5.x before this change it fails on OCV/CPU, OCV/OCL and OCV/OCL_FP16 with aninferror, and passes after.Tested on Ubuntu 24.04 (WSL2), x86-64, GCC 15.2.0, Release,
WITH_OPENCL=ON, OpenCL through PoCL on the CPU. I ran theopencv_test_dnntests matching*Softmax*:*softmax*:Layer_Test_*:Layer_*Region*:*Attention*:Test_Graph_Simplifier.*with and without the patch. The only difference is the three new LogSoftmax cases, which now pass. The other failures in that set fail the same way on unpatched 5.x in my setup (myopencv_extracheckout is on 4.x).Pull Request Readiness Checklist
See details at https://github.com/opencv/opencv/wiki/How_to_contribute#making-a-good-pull-request
Patch to opencv_extra has the same branch name.