Skip to content

Fix empty CPU reductions with negative axes - #33232

Merged
mirounga merged 1 commit into
mainfrom
fix-empty-reduction-negative-axes
Oct 9, 2026
Merged

mirounga merged 1 commit into
mainfrom
fix-empty-reduction-negative-axes

Conversation

@xadupre

Copy link
Copy Markdown
Member

Description

Normalize negative reduction axes before constructing CPU output shapes for empty tensors.

Add regression coverage for ReduceSum, ReduceProd, and ReduceMean with axes supplied as an input, both keepdims settings, and the legacy attribute-based axes path.

Tests:

  • cmake --build build/Linux/Release --target onnxruntime_provider_test -j 8 — passed
  • cd build/Linux/Release && ./onnxruntime_provider_test --gtest_filter=ReductionOpTest.EmptySetNegativeAxis — passed (1 test)
  • cd build/Linux/Release && ./onnxruntime_provider_test --gtest_filter='ReductionOpTest.*EmptySet*:ReductionOpTest.empty_set_*' — passed (28 tests)

Motivation and Context

The empty-input reduction path compared raw negative axes with non-negative dimension indices, so an axis such as -1 was never reduced and produced an incorrect output shape. The normal non-empty path already normalizes axes.

Fixes #33230.

Normalize axes before constructing output shapes for empty CPU reductions, and cover input- and attribute-based axes with keepdims enabled and disabled.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Copilot AI balanced review requested due to automatic review settings October 9, 2026 09:38

Copilot AI left a comment

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.

🟢 Approval recommended

The focused fix matches normal reduction behavior and has adequate regression coverage.

0 open findings

What changed in this PR

Fixes CPU empty-tensor reductions by normalizing negative axes before output-shape construction.

Changes:

  • Normalizes negative axes using existing validation logic.
  • Adds regression coverage for input- and attribute-based axes with both keepdims modes.
File Description
onnxruntime/​core/​providers/​cpu/​reduction/​reduction_ops.cc Normalizes axes in the empty-input reduction path.
onnxruntime/​test/​providers/​cpu/​reduction/​reduction_ops_test.cc Tests negative-axis empty reductions across affected operators.

🧠 Review effort: Balanced


💡 Configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

@mirounga
mirounga self-requested a review October 9, 2026 14:35
@mirounga
mirounga merged commit 9a6089e into main Oct 9, 2026
@mirounga
mirounga deleted the fix-empty-reduction-negative-axes branch October 9, 2026 14:36
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug] CPU reduction operators return incorrect output shapes for empty tensors with negative axes

3 participants