From c415e4fcefdca6a3811206f5e0a948e8e2d3206b Mon Sep 17 00:00:00 2001 From: Shiven Solanki Date: Thu, 29 Oct 2026 13:35:30 +0800 Subject: [PATCH] Merge pull request #30526 from sshivensolanki:fix-minmax-fp8 core: support fp8 depths in minMaxLoc and minMaxIdx - #30526 Fixes #30525 ### Problem `minMaxIdxTab` (`modules/core/src/minmax.simd.hpp`) has no entries for `CV_8F_E4M3FN` and `CV_8F_E4M3FNUZ`, so `cv::minMaxLoc` and `cv::minMaxIdx` fail with `func != 0` for fp8 input. ### Fix - `modules/core/include/opencv2/core/private.hpp`: new `CV_TOGGLE_FP8(x)`, the 8-bit version of `CV_TOGGLE_FLT`: it flips the 7 magnitude bits of negative values, so sign-magnitude fp8 bytes compare in the same order as `int8`. - `modules/core/src/minmax.simd.hpp`: new `minMaxIdx8f`, used for both fp8 depths. It toggles the input in blocks of 1024 bytes and runs the vectorized `minMaxIdx8s` kernel on them. - `modules/core/src/minmax.dispatch.cpp`: the fp8 result is toggled back and decoded with `fp8_t::decodeLUT()` / `fp8a_t::decodeLUT()`. - `modules/core/include/opencv2/core.hpp`: removed the outdated `@note` saying that `CV_16F`/`CV_16BF`/`CV_Bool`/`CV_64U`/`CV_64S`/`CV_32U` are not supported for `minMaxLoc` / `minMaxIdx`. On 1920x1080 input (single thread, Apple M1 Pro), `minMaxLoc` takes 0.14 ms for both fp8 depths (0.11 ms for `CV_8U`), compared with 2.8 ms for the first version of this PR, which converted every element to `float`. NaN values are not handled specially: like any other byte, they are ordered by their toggled bit pattern. ### Tests `MinMaxSupportedMatDepth` and `MinMaxAccuracyNewTypes` (`accuracy` and `accuracy_with_mask`) now also run on both fp8 depths, including each format's largest value (448 / 240) and masked-out outliers. The 8 new cases fail on current 5.x (`func != 0`) and pass with this patch. I also compared the results with a float reference on 1600 random fp8 inputs of different sizes, with and without a mask: values and locations match. All `*MinMax*` / `*minMax*` tests in `opencv_test_core` pass locally on Apple M1 Pro (3113 tests), with OpenCL enabled and with `OPENCV_OPENCL_DEVICE=disabled`. The full `opencv_test_core` passes, except 7 tests that need `opencv_extra` test data, which I didn't have locally (FileStorage base64 and globbing). ### Pull Request Readiness Checklist See details at https://github.com/opencv/opencv/wiki/How_to_contribute#making-a-good-pull-request - [x] I agree to contribute to the project under Apache 2 License. - [x] 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 - [x] The PR is proposed to the proper branch - [x] There is a reference to the original bug report and related work - [x] 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 --- modules/core/include/opencv2/core.hpp | 2 -- modules/core/include/opencv2/core/private.hpp | 1 + modules/core/src/minmax.dispatch.cpp | 7 +++++++ modules/core/src/minmax.simd.hpp | 19 +++++++++++++++++++ modules/core/test/test_arithm.cpp | 14 ++++++++++++-- 5 files changed, 39 insertions(+), 4 deletions(-) diff --git a/modules/core/include/opencv2/core.hpp b/modules/core/include/opencv2/core.hpp index f58abd78fd..ccaf12f54c 100644 --- a/modules/core/include/opencv2/core.hpp +++ b/modules/core/include/opencv2/core.hpp @@ -799,7 +799,6 @@ In Python, multi-channel input is not supported at all due to a limitation in th binding generation process (there is no way to set minLoc and maxLoc to NULL). A workaround is to operate on each channel individually or to use NumPy to achieve the same functionality. -@note CV_16F/CV_16BF/CV_Bool/CV_64U/CV_64S/CV_32U are not supported for src. @param src input single-channel array. @param minVal pointer to the returned minimum value; NULL is used if not required. @param maxVal pointer to the returned maximum value; NULL is used if not required. @@ -857,7 +856,6 @@ a single-row or single-column matrix. In OpenCV (following MATLAB) each array ha dimensions, i.e. single-column matrix is Mx1 matrix (and therefore minIdx/maxIdx will be (i1,0)/(i2,0)) and single-row matrix is 1xN matrix (and therefore minIdx/maxIdx will be (0,j1)/(0,j2)). -@note CV_16F/CV_16BF/CV_Bool/CV_64U/CV_64S/CV_32U are not supported for src. @param src input single-channel array. @param minVal pointer to the returned minimum value; NULL is used if not required. @param maxVal pointer to the returned maximum value; NULL is used if not required. diff --git a/modules/core/include/opencv2/core/private.hpp b/modules/core/include/opencv2/core/private.hpp index 0ccacd6c4f..a1b7f41a89 100644 --- a/modules/core/include/opencv2/core/private.hpp +++ b/modules/core/include/opencv2/core/private.hpp @@ -122,6 +122,7 @@ namespace cv /* IEEE754 constants and macros */ #define CV_TOGGLE_FLT(x) ((x)^((int)(x) < 0 ? 0x7fffffff : 0)) #define CV_TOGGLE_DBL(x) ((x)^((int64)(x) < 0 ? CV_BIG_INT(0x7fffffffffffffff) : 0)) +#define CV_TOGGLE_FP8(x) ((x)^((schar)(x) < 0 ? 0x7f : 0)) inline void* cvAlignPtr( const void* ptr, int align = 32 ) { diff --git a/modules/core/src/minmax.dispatch.cpp b/modules/core/src/minmax.dispatch.cpp index c70ed0facb..e607fb6280 100644 --- a/modules/core/src/minmax.dispatch.cpp +++ b/modules/core/src/minmax.dispatch.cpp @@ -371,6 +371,13 @@ static void minMaxIdx_(cv::InputArray _src, double* minVal, dminval = minval.i, dmaxval = maxval.i; else if( depth == CV_32F || depth == CV_16F || depth == CV_16BF ) dminval = minval.f, dmaxval = maxval.f; + else if( depth == CV_8F_E4M3FN || depth == CV_8F_E4M3FNUZ ) + { + // the fp8 kernel returns toggled values (see minMaxIdx8f): toggle them back and decode + const float* lut = depth == CV_8F_E4M3FN ? fp8_t::decodeLUT() : fp8a_t::decodeLUT(); + dminval = lut[(uchar)CV_TOGGLE_FP8(minval.i)]; + dmaxval = lut[(uchar)CV_TOGGLE_FP8(maxval.i)]; + } else if( depth == CV_64F ) dminval = minval.d, dmaxval = maxval.d; else if( depth == CV_64S || depth == CV_32U ) diff --git a/modules/core/src/minmax.simd.hpp b/modules/core/src/minmax.simd.hpp index d61a86f749..e39665fabf 100644 --- a/modules/core/src/minmax.simd.hpp +++ b/modules/core/src/minmax.simd.hpp @@ -374,6 +374,23 @@ DEFINE_MINMAXIDX_FUNC_NOSIMD(minMaxIdx64u, uint64, uint64) DEFINE_MINMAXIDX_FUNC_NOSIMD(minMaxIdx64s, int64, int64) DEFINE_MINMAXIDX_FUNC_NOSIMD(minMaxIdx32u, unsigned, int64) +// fp8 values are sign-magnitude, like float: CV_TOGGLE_FP8 maps them to int8 values in the +// same order, so the vectorized s8 kernel can find the extrema. The values it returns are +// toggled; minMaxIdx() toggles them back and decodes them. +static void minMaxIdx8f(const uchar* src, const uchar* mask, int* minVal, int* maxVal, + size_t* minIdx, size_t* maxIdx, int len, size_t startIdx) +{ + const int BLOCK_SIZE = 1024; + schar buf[BLOCK_SIZE]; + for (int i = 0; i < len; i += BLOCK_SIZE) + { + const int n = std::min(len - i, BLOCK_SIZE); + for (int j = 0; j < n; j++) + buf[j] = (schar)CV_TOGGLE_FP8(src[i + j]); + minMaxIdx8s(buf, mask ? mask + i : nullptr, minVal, maxVal, minIdx, maxIdx, n, startIdx + i); + } +} + MinMaxIdxFunc getMinMaxIdxFunc(int depth) { static MinMaxIdxFunc minMaxIdxTab[CV_DEPTH_MAX] = @@ -391,6 +408,8 @@ MinMaxIdxFunc getMinMaxIdxFunc(int depth) minMaxIdxWrap, minMaxIdxWrap, minMaxIdxWrap, + minMaxIdxWrap, + minMaxIdxWrap, 0 }; diff --git a/modules/core/test/test_arithm.cpp b/modules/core/test/test_arithm.cpp index 706054342f..aa03a2b807 100644 --- a/modules/core/test/test_arithm.cpp +++ b/modules/core/test/test_arithm.cpp @@ -4167,7 +4167,7 @@ TEST_P(MinMaxSupportedMatDepth, minMaxIdx) INSTANTIATE_TEST_CASE_P( MinMaxLoc, MinMaxSupportedMatDepth, - testing::Values(perf::MatDepth(CV_16F), CV_16BF, CV_Bool, CV_64U, CV_64S, CV_32U) + testing::Values(perf::MatDepth(CV_16F), CV_16BF, CV_Bool, CV_64U, CV_64S, CV_32U, CV_8F_E4M3FN, CV_8F_E4M3FNUZ) ); typedef testing::TestWithParam MinMaxAccuracyNewTypes; @@ -4189,6 +4189,8 @@ TEST_P(MinMaxAccuracyNewTypes, accuracy) max_val = (double)std::numeric_limits::max() - 1.0; break; case CV_64U: fill_val = 1e15; min_val = 0.0; max_val = 1e18; break; case CV_64S: fill_val = 0.0; min_val = -1e17; max_val = 1e17; break; + case CV_8F_E4M3FN: fill_val = 1.5; min_val = -96.0; max_val = 448.0; break; + case CV_8F_E4M3FNUZ: fill_val = 1.5; min_val = -96.0; max_val = 240.0; break; default: FAIL() << "Unexpected depth " << depth; } @@ -4205,6 +4207,8 @@ TEST_P(MinMaxAccuracyNewTypes, accuracy) case CV_32U: src.at(p.y, p.x) = (uint32_t)v; break; case CV_64U: src.at(p.y, p.x) = (uint64_t)v; break; case CV_64S: src.at(p.y, p.x) = (int64_t)v; break; + case CV_8F_E4M3FN: src.at(p.y, p.x) = cv::fp8_t((float)v); break; + case CV_8F_E4M3FNUZ: src.at(p.y, p.x) = cv::fp8a_t((float)v); break; default: FAIL() << "Unexpected depth " << depth; } }; @@ -4270,6 +4274,10 @@ TEST_P(MinMaxAccuracyNewTypes, accuracy_with_mask) case CV_64S: fill_val = 0.0; masked_min = -1e10; masked_max = 1e10; outlier_min = -1e17; outlier_max = 1e17; break; + case CV_8F_E4M3FN: + case CV_8F_E4M3FNUZ: + fill_val = 1.0; masked_min = -10.0; masked_max = 10.0; + outlier_min = -200.0; outlier_max = 200.0; break; default: FAIL() << "Unexpected depth " << depth; } @@ -4284,6 +4292,8 @@ TEST_P(MinMaxAccuracyNewTypes, accuracy_with_mask) case CV_32U: src.at(p.y, p.x) = (uint32_t)v; break; case CV_64U: src.at(p.y, p.x) = (uint64_t)v; break; case CV_64S: src.at(p.y, p.x) = (int64_t)v; break; + case CV_8F_E4M3FN: src.at(p.y, p.x) = cv::fp8_t((float)v); break; + case CV_8F_E4M3FNUZ: src.at(p.y, p.x) = cv::fp8a_t((float)v); break; default: FAIL() << "Unexpected depth " << depth; } }; @@ -4315,7 +4325,7 @@ TEST_P(MinMaxAccuracyNewTypes, accuracy_with_mask) INSTANTIATE_TEST_CASE_P( MinMaxAcc, MinMaxAccuracyNewTypes, - testing::Values(CV_16BF, CV_Bool, CV_64U, CV_64S, CV_32U) + testing::Values(CV_16BF, CV_Bool, CV_64U, CV_64S, CV_32U, CV_8F_E4M3FN, CV_8F_E4M3FNUZ) ); typedef testing::TestWithParam LUTAccuracyNewDstTypes;