Skip to content

dnn: fix Reduce over three or more axes - #30781

Merged
abhishek-gola merged 2 commits into
opencv:4.xfrom
sohailshk:fix/dnn-reduce-multiple-axes
Nov 13, 2026
Merged

abhishek-gola merged 2 commits into
opencv:4.xfrom
sohailshk:fix/dnn-reduce-multiple-axes

Conversation

@sohailshk

@sohailshk sohailshk commented Nov 12, 2026 •

Copy link
Copy Markdown

Port of #30108 (5.x, reduce2_layer.cpp, by officialarghya29) to the 4.x Reduce layer. The 5.x reduce_layer.cpp still has the same lines (about 376 and 392) and the same bug.

The Reduce layer returns wrong values when it reduces three or more axes, for example ReduceMean(axes=[0, 2, 3]) of an NCHW tensor. ReduceInvoker walks the reduced block with an odometer over all reduced axes but the innermost one. On a carry it assigned the step of the next outer axis (current_step = steps_src[reduced_axes[j - 1]]) instead of removing the offset of the axis that wrapped and adding one step of the next. projected_steps is shared by every output element, so once an outer reduced axis has three or more elements the outputs are wrong. The loop for the unreduced axes just below already carries this way; the same lines are used.

Layer_Test_Reduce.three_reduced_axes sums random tensors (4-D with four sets of three reduced axes, and two 5-D cases with four reduced axes) and compares with a straightforward reference. It fails on 4.x without the change (case 0: normInf 14.6) and passes with it. The ONNX conformance case test_mvn_expanded (a MeanVarianceNormalization decomposed into ReduceMean over three axes) fails on 4.x and passes with the change, so it is removed from test_onnx_conformance_layer_parser_denylist.inl.hpp; I ran it with only its three files from opencv_extra. Other ONNX Reduce tests need opencv_extra and were not run.

Pull Request Readiness Checklist

See details at https://github.com/opencv/opencv/wiki/How_to_contribute#making-a-good-pull-request

  • I agree to contribute to the project under Apache 2 License.
  • 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
  • The PR is proposed to the proper branch
  • There is a reference to the original bug report and related work
  • 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

Port of opencv#30108 (5.x, reduce2_layer.cpp) to the 4.x Reduce layer.

ReduceInvoker walks the reduced block with an odometer over every reduced axis but the innermost one. On a carry it assigned the step of the next outer axis (j - 1) instead of removing the offset of the axis that wrapped and adding one step of the next. projected_steps is shared by all output elements, so once an outer reduced axis has three or more elements every output was wrong. Carry the same way as the unprojected axes below. This also makes test_mvn_expanded pass, so it is removed from the parser denylist.

@codex-abhishek-gola codex-abhishek-gola Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Status: 🟡 Yellow

The projected-axis carry now removes the wrapped-axis offset and adds the next outer step, matching the already-correct unreduced-axis odometer and the merged 5.x Reduce2 fix. The regression covers several three- and four-axis layouts, and re-enabling test_mvn_expanded exercises the reported ONNX path. No actionable issue found; LGTM pending exact-head CI and conformance-data validation.

@varun-jaiswal17
varun-jaiswal17 self-requested a review November 12, 2026 05:46
@asmorkalov asmorkalov added the category: dnn (onnx) ONNX suport issues in DNN module label Nov 12, 2026
@asmorkalov asmorkalov added this to the 4.15.0 milestone Nov 12, 2026
Comment thread modules/dnn/test/test_layers.cpp Outdated
Comment on lines +1929 to +1936
struct Case { std::vector<int> sizes; std::vector<int> axes; };
const Case cases[] = {
{ {4, 3, 5, 4}, {0, 2, 3} }, { {4, 3, 5, 4}, {0, 1, 3} },
{ {4, 3, 5, 4}, {1, 2, 3} }, { {4, 3, 5, 4}, {0, 1, 2} },
{ {2, 3, 2, 3, 2}, {0, 1, 2, 4} }, { {3, 3, 3, 2, 3}, {0, 2, 3, 4} }
};
for (size_t c = 0; c < sizeof(cases) / sizeof(cases[0]); c++)
{

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the multiple (sizes, axes) cases are in a for loop inside one TEST. Please pass them as parameters using TEST_P for each case.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done in dca3d75. The loop is now a TEST_P (Layer_Test_Reduce_MultiAxes) with each (sizes, axes) pair as its own INSTANTIATE_TEST_CASE_P value. All six cases are kept; each fails without the reduce_layer.cpp change and passes with it.

Pass each (sizes, axes) case to TEST_P instead of looping inside one TEST.

@codex-abhishek-gola codex-abhishek-gola Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Status: 🟡 Yellow

The update converts all six multi-axis Reduce cases into separately reported parameterized tests without changing their reference calculation or coverage. This resolves the outstanding review request, and I found no remaining actionable issue in the delta. Exact-head CI and the re-enabled ONNX conformance case have not run yet.

@abhishek-gola abhishek-gola self-assigned this Nov 13, 2026
@abhishek-gola
abhishek-gola merged commit d73b2f9 into opencv:4.x Nov 13, 2026
31 of 32 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

category: dnn (onnx) ONNX suport issues in DNN module

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants