Files
opencv-MIRROR/modules
Muhammad Usman Mateen efe734517f dnn: compute LogSoftmax without taking the log of softmax 🤖🤖🤖 (#30545)
* dnn: test LogSoftmax far below the maximum

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. See #30544.

* dnn: compute LogSoftmax without taking the log of softmax

logSoftmax() took the log of the softmax output, so elements whose exp(x - max) underflows came out as -inf. The softmax kernel already has the row maximum and the sum of exp(x - max), so the log path now writes (x - max) - log(sum) directly. Subtracting max first keeps rows of large, close values exact, as in test_logsoftmax_large_number.

Fixes #30544

* dnn: pass the LogSoftmax flag to softmax() instead of a separate function

Address review: softmax() takes a logSoftmax flag with a default of false, and logSoftmax() calls it with true.

* dnn: run the LogSoftmax underflow test on the OpenCL targets

The test now runs for every target of the OpenCV backend, and uses the default FP16 thresholds for the FP16 targets.

* dnn: compute LogSoftmax in the OpenCL kernels without taking the log of softmax

With LOG_SOFTMAX, the kernels kept exp(x - max) and returned log(exp(x - max) / sum), which is -inf once exp underflows. They now keep x - max, add up exp(x - max), and return x - max - log(sum), like the CPU path. This covers the generic kernels in softmax.cl and the subgroup kernels in softmax_loss.cl.
2026-11-13 15:42:48 +05:30
..
2026-04-28 12:30:06 +02:00