Skip to content

Conversation

@sshekhar563
Copy link
Contributor

@sshekhar563 sshekhar563 commented Jan 3, 2026

Summary

This PR addresses issue #33261 by deferring axis validation for reduce operations from node creation time to compile/runtime, improving the user experience when building models with the Python opset API.

Problem

Previously, when creating reduce operations (ReduceMean, ReduceMax, ReduceMin, etc.) with out-of-range axes via the Python opset API, validation would fail immediately during node construction with a RuntimeError. This was surprising behavior since users typically expect validation to occur at core.compile_model() time, not during model building.

Before (problematic behavior):

import openvino.opset1 as ops
x = ops.parameter([2], name="x")  # 1D tensor
y = ops.reduce_mean(x, axes=[1])  # axis 1 is out of range for rank 1
# RuntimeError: Axis 1 out of the tensor rank range [-1, 0]

**After (improved behavior):**

import openvino.opset1 as ops
x = ops.parameter([2], name="x")  # 1D tensor  
y = ops.reduce_mean(x, axes=[1])  # Node created successfully with dynamic output shape
model = core.compile_model(model, "CPU")  # Validation happens here with clear error message


Fixes #33261

- Modify reduce_shape_infer() to check axis validity before normalizing
- When invalid axes are detected, return dynamic output shape instead of throwing
- Validation still occurs at runtime via try_get_normalized_axis_set() in evaluate()
- Affects all reduce operations: ReduceMean, ReduceMax, ReduceMin, ReduceProd, etc.
- Update tests to reflect new behavior where invalid axes result in dynamic shapes

Fixes openvinotoolkit#33261
@sshekhar563 sshekhar563 requested a review from a team as a code owner January 3, 2026 15:15
@github-actions github-actions bot added the category: Core OpenVINO Core (aka ngraph) label Jan 3, 2026
@sys-openvino-ci sys-openvino-ci added the ExternalPR External contributor label Jan 3, 2026

if (data_rank.is_static() && axes_val) {
ov::util::try_normalize_axes(*axes_val, data_rank, *op);
// Check if all axes are valid before normalizing - if not, defer validation to runtime
Copy link
Contributor

Choose a reason for hiding this comment

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

This is not correct.
In OpenVINO if output shape cannot be calculated correctly because of invalid inputs the exception must be thrown.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

"Understood. I attempted to defer the validation to improve the user experience as requested in the issue, but I see now that core shape inference requires strict validation when inputs are known. I will revert this change.
I will instead update the PR to add a documentation warning (docstring) and a unit test to formalize this 'fail-fast' behavior, which addresses the original issue's request for better clarity."

Per reviewer feedback - OpenVINO requires immediate validation when
output shape cannot be calculated correctly due to invalid inputs.

Reverted changes to reduce_shape_inference.hpp and reduce_ops.hpp
to restore original behavior where invalid axes throw immediately.
@sshekhar563
Copy link
Contributor Author

@praasz I have reverted the changes to reduce_shape_inference.hpp. Instead, I am converting this PR to a Documentation & Test fix:
Add a unit test (reduce_invalid_axis_out_of_range) to strictly enforce that this validation throws the correct exception.
Updated the Python docstrings to warn users that invalid static axes will raise a RuntimeError at node creation time.

- Add docstring documentation to Python reduce operations (reduce_mean,
  reduce_max, reduce_min, reduce_sum, reduce_prod, reduce_logical_and,
  reduce_logical_or) explaining:
  - Valid axis range: [-rank, rank-1]
  - Validation occurs at node creation time when axes are constant
  - RuntimeError is raised immediately for out-of-range axes

- Add Python unit tests documenting the expected 'fail-fast' behavior:
  - test_reduce_invalid_axis_raises_immediately
  - test_reduce_logical_invalid_axis_raises_immediately

This addresses issue openvinotoolkit#33261 by improving documentation clarity about
when axis validation occurs, as requested by the issue author.

Fixes openvinotoolkit#33261
@sshekhar563 sshekhar563 requested a review from a team as a code owner January 5, 2026 16:53
@github-actions github-actions bot added category: Python API OpenVINO Python bindings and removed category: Core OpenVINO Core (aka ngraph) labels Jan 5, 2026
@sshekhar563 sshekhar563 requested a review from praasz January 5, 2026 16:53
@praasz
Copy link
Contributor

praasz commented Jan 8, 2026

@praasz I have reverted the changes to reduce_shape_inference.hpp. Instead, I am converting this PR to a Documentation & Test fix: Add a unit test (reduce_invalid_axis_out_of_range) to strictly enforce that this validation throws the correct exception. Updated the Python docstrings to warn users that invalid static axes will raise a RuntimeError at node creation time.

The unit test can be added to show there is exception on invalid inputs.

Docstring update is not required as this is default action for OV nodes.
@p-wysocki could you look at it?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

category: Python API OpenVINO Python bindings ExternalPR External contributor

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug]: ReduceMean with out-of-range axis raises RuntimeError during node creation (axis validation)

4 participants