Merge pull request #30157 from abhishek-gola:core-reduce-nd

core: reduceND, used by the DNN Reduce2 layer - #30157

Split out of #30082, which now carries only the resize half.

Adds `cv::reduceND`, which reduces an n-dimensional array over a set of axes with any of the `ReduceTypes`, extended with `REDUCE_PROD`, `REDUCE_L1`, `REDUCE_L2`, `REDUCE_LOG_SUM` and `REDUCE_LOG_SUM_EXP`. `cv::reduce` accepts n-dimensional input through it.

`sum`, `mean` and `minMaxIdx` now share one parallel driver (`stat_parallel.hpp`), so they parallelize over large arrays the same way `reduceND` does.

The new graph engine's `Reduce2` layer calls `cv::reduceND` instead of its own kernels, so `reduce2_kernels.simd.hpp` and the hand-written reduction loops in `reduce2_layer.cpp` go away.

### 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.
- [x] The feature is well documented and sample code can be built with the project CMake
This commit is contained in:
Abhishek Gola
2026-10-12 14:38:20 +05:30
committed by GitHub
parent 1aab5099d2
commit 25beda402a
15 changed files with 1717 additions and 719 deletions

View File

@@ -7,6 +7,7 @@
#include "opencl_kernels_core.hpp"
#include "stat.hpp"
#include "stat_parallel.hpp"
#include "sum.simd.hpp"
#include "sum.simd_declarations.hpp" // defines CV_CPU_DISPATCH_MODES_ALL=AVX2,...,BASELINE based on CMakeLists.txt content
@@ -118,26 +119,17 @@ bool ocl_sum( InputArray _src, Scalar & res, int sum_op, InputArray _mask,
#endif
Scalar sum(InputArray _src)
// serial implementation, also used for the pieces of the parallel version
static Scalar sum_(const Mat& src)
{
CV_INSTRUMENT_REGION();
Scalar _res;
#ifdef HAVE_OPENCL
CV_OCL_RUN_(OCL_PERFORMANCE_CHECK(_src.isUMat()) && _src.dims() <= 2,
ocl_sum(_src, _res, OCL_OP_SUM),
_res);
#endif
Mat src = _src.getMat();
int cn = src.channels();
CV_CheckLE( cn, 4, "cv::sum does not support more than 4 channels" );
if (_src.dims() <= 2)
if (src.dims <= 2)
{
CALL_HAL_RET2(sum, cv_hal_sum, _res, src.data, src.step, src.type(), src.cols, src.rows, &_res[0]);
}
else if (_src.isContinuous())
else if (src.isContinuous())
{
CALL_HAL_RET2(sum, cv_hal_sum, _res, src.data, 0, src.type(), (int)src.total(), 1, &_res[0]);
}
@@ -201,4 +193,33 @@ Scalar sum(InputArray _src)
return _res;
}
Scalar sum(InputArray _src)
{
CV_INSTRUMENT_REGION();
Scalar _res;
#ifdef HAVE_OPENCL
CV_OCL_RUN_(OCL_PERFORMANCE_CHECK(_src.isUMat()) && _src.dims() <= 2,
ocl_sum(_src, _res, OCL_OP_SUM),
_res);
#endif
Mat src = _src.getMat();
CV_CheckLE( src.channels(), 4, "cv::sum does not support more than 4 channels" );
std::vector<StatChunk> chunks;
if (!splitForParallelStat(src, Mat(), chunks))
return sum_(src);
std::vector<Scalar> part(chunks.size());
parallel_for_(Range(0, (int)chunks.size()), [&](const Range& r)
{
for (int i = r.start; i < r.end; i++)
part[i] = sum_(chunks[i].src);
});
for (const Scalar& v : part)
_res += v;
return _res;
}
} // namespace