core: support CV_16F in the legacy reduce overload 🤖🤖🤖 (#31032)

* core: support CV_16F in the legacy reduce overload

Delegate half-precision CPU input to the existing N-D reduction kernels.
Preserve the 2-D result for a one-dimensional legacy input.

* core: use CV_CheckEQ for the 1D dim check in CV_16F reduce

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>

---------

Co-authored-by: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
Vladimir Iglovikov
2026-11-24 17:00:49 +09:00
committed by GitHub
parent 5014b24db5
commit ebf8a55d6c
4 changed files with 71 additions and 0 deletions

View File

@@ -895,6 +895,7 @@ obtained. For example, the function can be used to compute horizontal and vertic
raster image. In case of #REDUCE_MAX and #REDUCE_MIN, the output image should have the same type as the source one.
In case of #REDUCE_SUM, #REDUCE_SUM2 and #REDUCE_AVG, the output may have a larger element bit-depth to preserve accuracy.
And multi-channel arrays are also supported in these two reduction modes.
On the CPU, CV_16F inputs use the n-dimensional reduction kernels (see cv::ReduceParams).
The following code demonstrates its usage for a single channel matrix.
@snippet snippets/core_reduce.cpp example

View File

@@ -8,6 +8,22 @@ CV_ENUM(ROp, REDUCE_SUM, REDUCE_AVG, REDUCE_MAX, REDUCE_MIN, REDUCE_SUM2)
typedef tuple<Size, MatType, ROp> Size_MatType_ROp_t;
typedef perf::TestBaseWithParam<Size_MatType_ROp_t> Size_MatType_ROp;
typedef perf::TestBaseWithParam<tuple<Size, int> > Size_Axis;
PERF_TEST_P(Size_Axis, reduce_fp16_sum,
testing::Combine(testing::Values(szVGA, sz1080p), testing::Values(0, 1)))
{
const Size size = get<0>(GetParam());
const int dim = get<1>(GetParam());
Mat wide(size, CV_32FC(9)), src, dst;
randu(wide, 0, 1);
wide.convertTo(src, CV_16F);
TEST_CYCLE() cv::reduce(src, dst, dim, REDUCE_SUM, CV_64F);
SANITY_CHECK_NOTHING();
}
PERF_TEST_P(Size_MatType_ROp, reduceR,
testing::Combine(

View File

@@ -822,6 +822,17 @@ void cv::reduce(InputArray _src, OutputArray _dst, int dim, int op, int dtype)
src.convertTo(_dst, dtype);
return;
}
if (sdepth == CV_16F)
{
if (src.dims <= 1)
{
CV_CheckEQ(dim, 0, "1D input can only be reduced along dim 0");
src = src.reshape(cn, {1, src.cols});
dim = 1;
}
cv::reduce(src, _dst, cv::ReduceParams(op, {dim}, true, ddepth));
return;
}
if (src.dims <= 1) {
CV_Assert(dim == 0);
dim = 1;

View File

@@ -1183,6 +1183,49 @@ protected:
TEST(Core_Reduce, accuracy) { Core_ReduceTest test; test.safe_run(); }
typedef testing::TestWithParam<testing::tuple<int, int, int> > Core_ReduceFP16;
TEST_P(Core_ReduceFP16, matches_widened_reference)
{
const int channels = get<0>(GetParam());
const int dim = get<1>(GetParam());
const int op = get<2>(GetParam());
Mat1f values(9, 11 * channels);
for (int row = 0; row < values.rows; ++row)
for (int col = 0; col < values.cols; ++col)
values(row, col) = ((row * 11 + col) % 49 - 24) * 0.0625f;
Mat storage;
values.reshape(channels).convertTo(storage, CV_16F);
Mat src = storage(Rect(1, 1, 9, 7));
const bool extrema = op == REDUCE_MIN || op == REDUCE_MAX;
const int dtype = extrema ? CV_16F : CV_64F;
Mat dst, wide, expected;
cv::reduce(src, dst, dim, op, dtype);
src.convertTo(wide, CV_64F);
cv::reduce(wide, expected, dim, op, CV_64F);
ASSERT_EQ(dst.size(), expected.size());
ASSERT_EQ(dst.type(), CV_MAKETYPE(dtype, channels));
dst.convertTo(dst, CV_64F);
EXPECT_LE(cvtest::norm(dst, expected, NORM_INF), 1e-12);
}
INSTANTIATE_TEST_CASE_P(/**/, Core_ReduceFP16,
testing::Combine(testing::Values(1, 3, 9), testing::Values(0, 1),
testing::Values(REDUCE_SUM, REDUCE_AVG, REDUCE_SUM2, REDUCE_MIN, REDUCE_MAX)));
TEST(Core_Reduce, fp16_vector_keeps_matrix_output)
{
const Mat1f values({4}, {1, 2, 3, 4});
Mat src, dst;
values.convertTo(src, CV_16F);
ASSERT_EQ(src.dims, 1);
cv::reduce(src, dst, 0, REDUCE_SUM, CV_32F);
ASSERT_EQ(dst.dims, 2);
ASSERT_EQ(dst.size(), Size(1, 1));
ASSERT_EQ(dst.type(), CV_32F);
EXPECT_EQ(dst.at<float>(0, 0), 10.f);
}
// Core_ReduceTest fills with randu(0, 100), so it never reaches the range where a 32-bit
// accumulator would overflow. CV_32S reduces through a double accumulator for that reason.
TEST(Core_Reduce, int32_accumulator_does_not_overflow)