Repository navigation
dnn: fix the step bookkeeping of the reduce inner projection - #30108
Conversation
573b0dd to
4f34a7f
Compare
|
@abhishek-gola please review. |
There was a problem hiding this comment.
Copilot review overview
🟢 Approval recommended
The focused bookkeeping correction matches the established unprojected-axis logic and is covered by targeted regression and conformance tests.
Review effort: Balanced
Findings: None
What changed in this PR
Fixes incorrect element offsets in DNN Reduce2 reductions spanning three or more axes.
Changes:
- Corrects projected-axis carry bookkeeping.
- Adds regression coverage for non-contiguous reductions.
- Re-enables passing MVN-expanded ONNX conformance tests.
| File | Description |
|---|---|
modules/dnn/src/layers/reduce2_layer.cpp |
Corrects offset calculation during odometer carries. |
modules/dnn/test/test_layers.cpp |
Adds SUM and MEAN regression tests. |
modules/dnn/test/test_onnx_conformance_layer_parser_denylist.inl.hpp |
Re-enables fixed conformance cases and updates the MVN reason. |
💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.
please update |
|
cc: @abhishek-gola |
Reducing over more than two axes walks the reduced sub-block with an odometer
over all of them but the innermost one. When an index carried into the next
axis, the step of that axis was assigned instead of being accumulated, so the
steps of every axis above the one that wrapped were lost and the corresponding
elements were read again and again:
axes [0, 2, 3] on a 3x3x3x3 input, shape (3,3,3,3), steps (27, 9, 3, 1)
projected steps: 0 3 6 27 30 33 27 30 33 <- should end in 54 57 60
Only the first output slice happened to be right, because the carry that
introduced the wrong step is the one that moves to the second one.
The same accounting exists for the unprojected axes just below, which
subtracts the step of the wrapped axis and adds the step of the axis it carries
into; the projected loop now does the same. This fixes test_mvn_expanded and
test_mvn_expanded_ver18 (a MeanVarianceNormalization decomposed into
primitives, denied as "Wrong answer" until now) plus any model that reduces
over three or more axes, since the inner projection also feeds the unprojected
axis steps. The OpenVINO backend still answers test_mvn_expanded_ver18
incorrectly, so it is filtered in
test_onnx_conformance_layer_filter__openvino.inl.hpp.
Found by comparing against onnxruntime: 9 of 200 random (rank, shape, axes set,
keepdims, op) combinations were wrong before and none are now; the two
conformance cases above and a new layer test fail without this change.
4f34a7f to
8556b14
Compare
|
@SavyaSanchi-Sharma thanks for the review. I shortened both comments and added |
|
@SavyaSanchi-Sharma @abhishek-gola Is it ready for merge? |
* dnn: fix Reduce over three or more axes Port of #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. * dnn: parameterize the Reduce multi-axis regression test Pass each (sizes, axes) case to TEST_P instead of looping inside one TEST.
Pull Request Readiness Checklist
Problem
A
Reduce*over three or more axes accumulates the wrong elements, except for the first outputslice:
ReduceMean(axes=[0,2,3], keepdims=1)on3x3x3x30.4448 0.4903 -0.5875 ...0.4446 0.1235 -0.4050 ...That is exactly the shape of the ONNX conformance case
test_mvn_expanded(aMeanVarianceNormalizationdecomposed into primitives), which is in the parser denylist with thecomment
Issues::Wrong answer;test_mvn_expanded_ver18is denied without a reason and fails thesame way.
Root cause
ReduceInvokerwalks the reduced sub-block with an odometer over every reduced axis but theinnermost one (
projected_steps). On a carry the step of the axis that just wrapped is assignedinstead of being accumulated, so the steps of all the axes above it are dropped:
With
axes=[0,2,3]on a3x3x3x3input (steps27 9 3 1) the projection came out as0 3 6 27 30 33 27 30 33where it has to be0 3 6 27 30 33 54 57 60, i.e. the last three outputslices re-read the elements of the second one. The first slice is never affected, which is why the
incorrect results looked plausible and went unnoticed.
The unprojected-axis loop directly below does the same accounting correctly (it subtracts the step
of the wrapped axis and adds the step of the axis it carries into), which is the form the projected
loop now uses:
projected_indices[j] -= shape_src[reduced_axes[j]]; current_step -= shape_src[reduced_axes[j]] * steps_src[reduced_axes[j]]; ++projected_indices[j - 1]; current_step += steps_src[reduced_axes[j - 1]];This also matters for reductions with fewer than three axes indirectly, because the inner
projection drives the outer (unprojected) steps through the same
origin/projected_stepspairing.
Tests
Layer_Test_Reduce.NonContiguousAxes(new,test_layers.cpp):Reduce2withaxes=[0,2,3]andaxes=[0,1,3]on a3x3x3x3input,MEANandSUM, compared against a reference computed inthe test. Fails on the previous code, passes now.
test_mvn_expandedandtest_mvn_expanded_ver18are removed fromtest_onnx_conformance_layer_parser_denylist.inl.hpp; both fail before this change and pass withit (
maxabs2.4e-7against the conformance data).test_mvn(the operator itself, stillunimplemented) stays denied, with its reason corrected to the real one.
Validation
opencv_test_dnn: 5202/5202 with this branch alone (5201 before it), and two fewer skippedconformance tests than before.
Randomised differential sweep against numpy (
scratch/fuzz_reduce.py, 900 combinations of rank2-5, random axis sets,
keepdims, andMEAN/SUM/MAX/MIN/PROD/L1/L2/SUM_SQUARE):0 mismatches. The same sweep on the previous code reports 9 mismatches out of the first 200
combinations, e.g.
Not tested: Windows/MSVC, Clang, ARM/RISC-V, CUDA/OpenVINO/OpenCL targets (no access to those
configurations here). The change is scalar index arithmetic in the CPU fallback path of
Reduce2, which is also what the backends fall back to.Known, deliberately not addressed here: reducing every axis with
keepdims=0asks for arank-0 output and still fails with
output #0 changed during forward(): inferred shape [<scalar>](the shape check comparesMatShape::scalar()fromgetMemoryShapeswithMatShape{1}fromforward). That is a separate problem in the same layer, left for a follow-upso this change stays verifiable on its own; the sweep skips exactly those combinations.