Repository navigation
Fix empty CPU reductions with negative axes - #33232
Merged
Merged
Conversation
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>
Contributor
There was a problem hiding this comment.
🟢 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
keepdimsmodes.
| 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
self-requested a review
October 9, 2026 14:35
mirounga
approved these changes
Oct 9, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description
Normalize negative reduction axes before constructing CPU output shapes for empty tensors.
Add regression coverage for
ReduceSum,ReduceProd, andReduceMeanwith axes supplied as an input, bothkeepdimssettings, and the legacy attribute-based axes path.Tests:
cmake --build build/Linux/Release --target onnxruntime_provider_test -j 8— passedcd 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
-1was never reduced and produced an incorrect output shape. The normal non-empty path already normalizes axes.Fixes #33230.