Skip to content

[OpenVINO] Fix ndim() to return Python int instead of graph tensor#22608

Merged
hertschuh merged 3 commits intokeras-team:masterfrom
goyaladitya05:openvino/ndim
Apr 3, 2026
Merged

[OpenVINO] Fix ndim() to return Python int instead of graph tensor#22608
hertschuh merged 3 commits intokeras-team:masterfrom
goyaladitya05:openvino/ndim

Conversation

@goyaladitya05
Copy link
Copy Markdown
Contributor

ndim() in the OpenVINO backend was building an OV graph node to represent the rank, returning an OpenVINOKerasTensor. However, the number of dimensions is a static, compile-time property of a tensor's type, not a runtime value. It never changes during inference and must be a Python int for use in Python control flow.

All other backends return an int.

This PR adds a fix which reads the rank directly from the OV partial shape, get_partial_shape().rank.get_length() which is always known at graph-build time.

Copy link
Copy Markdown
Contributor

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

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

Code Review

This pull request removes a test from the OpenVINO excluded tests list and refactors the ndim function in the OpenVINO backend to use get_partial_shape().rank.get_length(). While the refactor simplifies the code, the current implementation is fragile because get_length() will raise a RuntimeError if the tensor has a dynamic rank. It is recommended to handle dynamic ranks by checking is_static or returning None to ensure robustness.

@goyaladitya05
Copy link
Copy Markdown
Contributor Author

/gemini review

Copy link
Copy Markdown
Contributor

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

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

Code Review

This pull request refactors the ndim implementation in the OpenVINO backend to use the tensor's partial shape rank and re-enables the test_normalization_kpl test. The review feedback suggests optimizing the ndim function by checking for the attribute directly on the input object to avoid unnecessary backend operations and recommends clarifying the error message regarding static rank requirements.

@codecov-commenter
Copy link
Copy Markdown

codecov-commenter commented Apr 1, 2026

Codecov Report

❌ Patch coverage is 83.33333% with 1 line in your changes missing coverage. Please review.
✅ Project coverage is 83.29%. Comparing base (6c5956d) to head (91935a7).
⚠️ Report is 6 commits behind head on master.

Files with missing lines Patch % Lines
keras/src/backend/openvino/numpy.py 83.33% 0 Missing and 1 partial ⚠️
Additional details and impacted files
@@           Coverage Diff           @@
##           master   #22608   +/-   ##
=======================================
  Coverage   83.28%   83.29%           
=======================================
  Files         596      596           
  Lines       68102    68139   +37     
  Branches    10610    10614    +4     
=======================================
+ Hits        56720    56753   +33     
- Misses       8636     8639    +3     
- Partials     2746     2747    +1     
Flag Coverage Δ
keras 83.09% <83.33%> (+<0.01%) ⬆️
keras-jax 59.66% <16.66%> (+<0.01%) ⬆️
keras-numpy 55.33% <0.00%> (+<0.01%) ⬆️
keras-openvino 53.41% <83.33%> (+0.02%) ⬆️
keras-tensorflow 61.03% <16.66%> (+<0.01%) ⬆️
keras-torch 59.85% <16.66%> (+<0.01%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@goyaladitya05 goyaladitya05 marked this pull request as ready for review April 2, 2026 18:44
Copy link
Copy Markdown
Collaborator

@hertschuh hertschuh left a comment

Choose a reason for hiding this comment

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

Thanks for the fix!

@google-ml-butler google-ml-butler bot added kokoro:force-run ready to pull Ready to be merged into the codebase labels Apr 3, 2026
@hertschuh hertschuh merged commit 8c6062b into keras-team:master Apr 3, 2026
9 checks passed
@goyaladitya05 goyaladitya05 deleted the openvino/ndim branch April 4, 2026 03:21
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

kokoro:force-run ready to pull Ready to be merged into the codebase size:XS

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants