Skip to content

dnn: compute LogSoftmax without taking the log of softmax (5.x) 🤖🤖🤖 - #30816

Merged
abhishek-gola merged 2 commits into
opencv:5.xfrom
usmanmateen:fix/logsoftmax-underflow-5.x
Nov 15, 2026
Merged

abhishek-gola merged 2 commits into
opencv:5.xfrom
usmanmateen:fix/logsoftmax-underflow-5.x

Conversation

@usmanmateen

Copy link
Copy Markdown
Contributor

Port of #30545 to 5.x, as asked in #30545 (comment). Fixes #30544 for 5.x.

logSoftmax() took log of the softmax output, so every element whose exp(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 with LOG_SOFTMAX had the same problem.

  • CPU: on 5.x, softmax goes through the dispatched softmax_ kernel in activation_kernels.simd.hpp, which takes a scale. As in the 4.x review, softmax() takes a logSoftmax flag (default false), and the log path writes (x - max) * scale - log(sum) from the row maximum and sum the kernel already computes. logSoftmax() calls it with true. The plain softmax path is unchanged.
  • OpenCL: the softmax.cl, softmax_loss.cl and softmax_layer.cpp changes 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 an inf error, 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 the opencv_test_dnn tests 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 (my opencv_extra checkout 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

  • I agree to contribute to the project under Apache 2 License.
  • To the best of my knowledge, the proposed patch is not based on a code under GPL or another license that is incompatible with OpenCV
  • The PR is proposed to the proper branch
  • There is a reference to the original bug report and related work
  • There is accuracy test, performance test and test data in opencv_extra repository, if applicable
    Patch to opencv_extra has the same branch name.
  • The feature is well documented and sample code can be built with the project CMake

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.
@abhishek-gola abhishek-gola self-assigned this Nov 14, 2026
@abhishek-gola abhishek-gola added port/backport done Label for maintainers. Authors of PR can ignore this category: dnn labels Nov 14, 2026
@abhishek-gola abhishek-gola added this to the 5.2 milestone Nov 14, 2026
@abhishek-gola abhishek-gola added bug category: dnn (onnx) ONNX suport issues in DNN module labels Nov 14, 2026

@codex-abhishek-gola codex-abhishek-gola Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@abhishek-gola
abhishek-gola merged commit d87ac83 into opencv:5.x Nov 15, 2026
28 of 29 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug category: dnn (onnx) ONNX suport issues in DNN module category: dnn port/backport done Label for maintainers. Authors of PR can ignore this

Projects

None yet

Development

Successfully merging this pull request may close these issues.

dnn: LogSoftmax returns -inf where the exact result is finite 🤖🤖🤖

2 participants