Skip to content

Validate empty reduction axes - #32156

Open
Akshay Sonawane (apsonawane) wants to merge 2 commits into
mainfrom
fix/reduce-empty-axes-validation
Open

Validate empty reduction axes#32156
Akshay Sonawane (apsonawane) wants to merge 2 commits into
mainfrom
fix/reduce-empty-axes-validation

Conversation

@apsonawane

Copy link
Copy Markdown
Contributor

This pull request improves the handling and validation of empty set reductions in the ONNX Runtime CPU reduction operators. It enforces stricter checks for the axes input, prevents unnecessary memory operations, and adds comprehensive tests to verify correct behavior for various edge cases.

Validation and Error Handling Improvements:

  • Enforces that the axes tensor input must be a 1D vector and only processes it if it is present, improving robustness and error messages for invalid input shapes.
  • Adds a check to ensure that memory copying only occurs when the input tensor is non-empty, avoiding redundant operations.

Logic and Flow Adjustments:

  • Refactors the order of reduction logic to first handle empty axes cases before checking for empty set input, ensuring correct execution flow and output. [1] [2]

Testing Enhancements:

  • Adds new tests to verify:
    • Reduction when the optional axes input is missing (should reduce all dimensions).
    • Validation that the axes tensor must be a vector, with expected error messages for invalid cases.
    • Correct behavior for the noop_with_empty_axes attribute, both when axes are omitted and when an empty axes tensor is provided.

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.

Pull request overview

Improves CPU reduction handling for empty tensors and optional axes.

Changes:

  • Validates axes tensors as one-dimensional.
  • Handles omitted or empty axes before empty-set reduction.
  • Adds edge-case tests and avoids zero-byte copies.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.

File Description
onnxruntime/core/providers/cpu/reduction/reduction_ops.cc Updates empty-set validation and reduction flow.
onnxruntime/test/providers/cpu/reduction/reduction_ops_test.cc Adds empty-axes and optional-axes tests.

💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

Comment on lines +891 to +892
const auto axes_data = axes_tensor->DataAsSpan<int64_t>();
input_axes.assign(axes_data.begin(), axes_data.end());
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.

2 participants