From fcfa24dfb7cf7db39a26de3fb5a06ff514a4494a Mon Sep 17 00:00:00 2001 From: Shiven Solanki Date: Fri, 13 Nov 2026 10:47:17 +0800 Subject: [PATCH] core: support 1D Mat inputs in gemm (#30704) * core: support 1D Mat inputs in gemm A 1D Mat is a single row, but its step[0] is the element size, not the row size, so gemm passed a too-small row step to the HAL kernels and failed with "_step >= minstep". Give 1D A, B and C an ordinary 2D one-row header before calling the kernels. Fixes #30703 * core: support preallocated 1D output in gemm When the caller passes a 1D Mat of the right size as the output, _matD.create() keeps it as is, so D still had step[0] == elemSize() and the kernel failed with "_step >= minstep". Give a 1D D a 2D one-row header too. The result is still written into the caller's buffer. * core: use TEST_P for the 1D gemm test --- modules/core/src/matmul.dispatch.cpp | 7 +++++ modules/core/test/test_math.cpp | 38 ++++++++++++++++++++++++++++ 2 files changed, 45 insertions(+) diff --git a/modules/core/src/matmul.dispatch.cpp b/modules/core/src/matmul.dispatch.cpp index ab6c60fa45..b4ab73905e 100644 --- a/modules/core/src/matmul.dispatch.cpp +++ b/modules/core/src/matmul.dispatch.cpp @@ -350,6 +350,11 @@ void gemm(InputArray matA, InputArray matB, double alpha, #endif Mat A = matA.getMat(), B = matB.getMat(), C = beta != 0.0 ? matC.getMat() : Mat(); + // A 1D Mat is one row, but its step[0] is the size of one element, not of the whole row. + // Give it an ordinary 2D one-row header so the row step passed to the kernels is right. + if (A.dims == 1) A = A.reshape(0, 1); + if (B.dims == 1) B = B.reshape(0, 1); + if (C.dims == 1) C = C.reshape(0, 1); Size a_size = A.size(), d_size; int len = 0, type = A.type(); @@ -388,6 +393,8 @@ void gemm(InputArray matA, InputArray matB, double alpha, _matD.create( d_size.height, d_size.width, type ); Mat D = _matD.getMat(); + // A preallocated 1D output keeps its 1D header after create(), so normalize it like A, B and C. + if (D.dims == 1) D = D.reshape(0, 1); if( (flags & GEMM_3_T) != 0 && C.data == D.data ) { transpose( C, C ); diff --git a/modules/core/test/test_math.cpp b/modules/core/test/test_math.cpp index 942bc7a08a..3325f65268 100644 --- a/modules/core/test/test_math.cpp +++ b/modules/core/test/test_math.cpp @@ -2502,6 +2502,44 @@ TEST(Core_GEMM, inplace_complex) } } +// A 1D Mat is a single row, so gemm should treat it the same as a 1 x N matrix +typedef testing::TestWithParam Core_GEMM_1D; + +TEST_P(Core_GEMM_1D, input) +{ + const int type = GetParam(); + const Mat v64 = Mat_({5}, {1, 2, 3, 4, 5}); // 1D Mat: dims == 1 + Mat v; + v64.convertTo(v, type); + ASSERT_EQ(1, v.dims); + const Mat row = v.reshape(0, 1); // same values as a 2D 1 x 5 Mat + + Mat inner, expectedInner; // 1x5 * (1x5)^T = 1x1 + gemm(v, v, 1, noArray(), 0, inner, GEMM_2_T); + gemm(row, row, 1, noArray(), 0, expectedInner, GEMM_2_T); + EXPECT_EQ(0., cvtest::norm(inner, expectedInner, NORM_INF)) << "inner product"; + + Mat outer, expectedOuter; // (1x5)^T * 1x5 = 5x5 + gemm(v, v, 1, noArray(), 0, outer, GEMM_1_T); + gemm(row, row, 1, noArray(), 0, expectedOuter, GEMM_1_T); + EXPECT_EQ(0., cvtest::norm(outer, expectedOuter, NORM_INF)) << "outer product"; + + Mat withC, expectedWithC; // v * I + v, where C is 1D too + const Mat I = Mat::eye(5, 5, type); + gemm(v, I, 1, v, 1, withC); + gemm(row, I, 1, row, 1, expectedWithC); + EXPECT_EQ(0., cvtest::norm(withC, expectedWithC, NORM_INF)) << "1D C"; + + Mat preallocated({5}, type); // 1D output that already has the right size + const uchar* oldData = preallocated.data; + gemm(v, I, 1, noArray(), 0, preallocated); // v * I = v + EXPECT_EQ(1, preallocated.dims) << "preallocated 1D D"; + EXPECT_EQ(oldData, preallocated.data) << "preallocated 1D D"; + EXPECT_EQ(0., cvtest::norm(preallocated, v, NORM_INF)) << "preallocated 1D D"; +} + +INSTANTIATE_TEST_CASE_P(/*nothing*/, Core_GEMM_1D, testing::Values(CV_32F, CV_64F)); + TEST(Core_Invert, accuracy) { Core_InvertTest test; test.safe_run(); } TEST(Core_Mahalanobis, accuracy) { Core_MahalanobisTest test; test.safe_run(); } TEST(Core_MulTransposed, accuracy) { Core_MulTransposedTest test; test.safe_run(); }