Skip to content

[OpenVINO] Fix array(), maximum(), minimum(), reshape() and split() ops in the numpy suite#22343

Draft
goyaladitya05 wants to merge 12 commits intokeras-team:masterfrom
goyaladitya05:openvino/fix-numpy-array-reshape-maximum-minimum
Draft

[OpenVINO] Fix array(), maximum(), minimum(), reshape() and split() ops in the numpy suite#22343
goyaladitya05 wants to merge 12 commits intokeras-team:masterfrom
goyaladitya05:openvino/fix-numpy-array-reshape-maximum-minimum

Conversation

@goyaladitya05
Copy link
Contributor

This PR focuses on improving the implementations for array(), maximum(), minimum(), reshape() and split() for the OpenVINO Backend in the numpy suite.

  • array() - replaced np.array() with convert_to_tensor() so the return value is always an OpenVINOKerasTensor instead of a raw NumPy array, making it consistent with other ops in the backend.
  • maximum() / minimum() - added the t1/t2 dtype-extraction step (same pattern as add(), subtract(), multiply()) so Python int/float literals are promoted using JAX weak-type semantics instead of being unconditionally cast to int32/float32
  • reshape() - added a guard for integer newshape (e.g. Reshape(-1)), wrapping it in a list before passing to ov_opset.constant() which requires a 1-D shape tensor
  • split() - handle OpenVINOKerasTensor as indices_or_sections by extracting the underlying constant data via get_node().get_data(); this was a side-effect of the array() fix and also covers hsplit() / vsplit() which delegate to split()

Duplicate of #22319

The CI was getting stuck, so I looked deeper and found the main issue.
OpenVINO tensors rely on sequence style iteration via __getitem__, but __getitem__ never raises IndexError, so loops like zip(mask, mask2) become unbounded and CI appears stuck. I’m adding a bounded iter on OpenVINOKerasTensor to make iteration finite and deterministic.

Closes: openvinotoolkit/openvino/issues/34409

@gemini-code-assist
Copy link
Contributor

Summary of Changes

Hello, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

This pull request significantly enhances the OpenVINO backend's NumPy suite by refining the implementation of several core operations, including array(), maximum(), minimum(), reshape(), and split(). These improvements ensure more consistent type handling, correct behavior with various input types, and better alignment with JAX weak-type semantics. Additionally, a critical issue causing CI to hang due to unbounded iteration in OpenVINOKerasTensor was identified and resolved, leading to improved stability and test coverage.

Highlights

  • Consistent array() behavior: The array() function now consistently returns OpenVINOKerasTensor by using convert_to_tensor() instead of np.array(), ensuring uniformity across the backend.
  • Improved type promotion for maximum() and minimum(): The maximum() and minimum() operations now correctly promote Python int/float literals using JAX weak-type semantics, aligning their behavior with add(), subtract(), and multiply().
  • Robust reshape() handling: The reshape() operation now properly handles integer newshape arguments (e.g., Reshape(-1)) by wrapping them in a list before passing to ov_opset.constant().
  • Enhanced split() functionality: The split() operation can now process OpenVINOKerasTensor when provided as indices_or_sections by extracting the underlying constant data.
  • Resolved CI hanging issue: A bounded __iter__ method was introduced to OpenVINOKerasTensor to prevent infinite loops during sequence-style iteration, resolving a critical CI issue.
  • Re-enabled tests: Several previously excluded tests related to array, maximum, minimum, and reshape were re-enabled following the implementation of these fixes.

🧠 New Feature in Public Preview: You can now enable Memory to help Gemini Code Assist learn from your team's feedback. This makes future code reviews more consistent and personalized to your project's style. Click here to enable Memory in your admin console.

Changelog
  • keras/src/backend/openvino/core.py
    • Added __iter__ method to OpenVINOKerasTensor to provide bounded iteration.
  • keras/src/backend/openvino/excluded_concrete_tests.txt
    • Removed NumpyDtypeTest::test_array from exclusions.
    • Removed NumpyDtypeTest::test_maximum_python_types from exclusions.
    • Removed NumpyDtypeTest::test_minimum_python_types from exclusions.
    • Removed NumpyOneInputOpsCorrectnessTest::test_array from exclusions.
    • Removed NumpyOneInputOpsCorrectnessTest::test_reshape from exclusions.
    • Removed ReshapeTest::test_reshape_with_dynamic_batch_size_and_minus_one from exclusions.
  • keras/src/backend/openvino/numpy.py
    • Extracted common type promotion logic into a new private helper function _promote_binary_types.
    • Updated add, subtract, multiply, maximum, and minimum functions to utilize _promote_binary_types for consistent type handling.
    • Modified array function to use convert_to_tensor for consistent OpenVINOKerasTensor output.
    • Added a conditional check in reshape to handle integer newshape arguments by converting them to a list.
    • Implemented logic in split to extract data from OpenVINOKerasTensor when used as indices_or_sections.
Activity
Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point by creating a comment using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here.

You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

Copy link
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 introduces several important fixes and refactorings for the OpenVINO numpy backend, improving consistency and correctness for array, maximum, minimum, reshape, and split operations. A key improvement is the _promote_binary_types helper for consistent type promotion. The addition of __iter__ to OpenVINOKerasTensor is a critical fix for an infinite loop issue. I've identified a bug in the reshape implementation where tensor-shaped newshape arguments are not handled, and a limitation in split where it only supports constant tensor inputs for indices_or_sections.

@codecov-commenter
Copy link

codecov-commenter commented Mar 4, 2026

Codecov Report

❌ Patch coverage is 13.04348% with 20 lines in your changes missing coverage. Please review.
✅ Project coverage is 76.41%. Comparing base (0e7dd66) to head (9ce42d0).
⚠️ Report is 2 commits behind head on master.

Files with missing lines Patch % Lines
keras/src/backend/openvino/numpy.py 10.00% 18 Missing ⚠️
keras/src/backend/openvino/core.py 33.33% 2 Missing ⚠️

❗ There is a different number of reports uploaded between BASE (0e7dd66) and HEAD (9ce42d0). Click for more details.

HEAD has 2 uploads less than BASE
Flag BASE (0e7dd66) HEAD (9ce42d0)
keras 5 4
keras-openvino 1 0
Additional details and impacted files
@@            Coverage Diff             @@
##           master   #22343      +/-   ##
==========================================
- Coverage   83.01%   76.41%   -6.60%     
==========================================
  Files         596      596              
  Lines       66595    66591       -4     
  Branches    10369    10372       +3     
==========================================
- Hits        55281    50886    -4395     
- Misses       8677    13273    +4596     
+ Partials     2637     2432     -205     
Flag Coverage Δ
keras 76.25% <13.04%> (-6.59%) ⬇️
keras-jax 60.62% <13.04%> (+<0.01%) ⬆️
keras-numpy 54.85% <0.00%> (+<0.01%) ⬆️
keras-openvino ?
keras-tensorflow 61.85% <13.04%> (+<0.01%) ⬆️
keras-torch 60.69% <13.04%> (+<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 draft March 4, 2026 03:49
@goyaladitya05 goyaladitya05 force-pushed the openvino/fix-numpy-array-reshape-maximum-minimum branch from 92d9735 to f5a4432 Compare March 5, 2026 19:18
@goyaladitya05
Copy link
Contributor Author

/gemini review

Copy link
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 introduces several important fixes and improvements for the OpenVINO backend. The addition of __iter__ to OpenVINOKerasTensor is a critical fix to prevent potential infinite loops. The refactoring of binary operations into a _promote_binary_types helper cleans up the code and correctly handles type promotion for maximum and minimum with Python scalars. Other fixes for array, reshape, and split enhance consistency and functionality. Overall, these are great changes. I have one suggestion to make the reshape implementation even more robust.

@goyaladitya05 goyaladitya05 marked this pull request as ready for review March 5, 2026 19:43
@goyaladitya05
Copy link
Contributor Author

goyaladitya05 commented Mar 5, 2026

I've rebased the PR, and all tests are passing. Please review. Thanks!

goyaladitya05 and others added 10 commits March 10, 2026 23:34
…plit() numpy ops

- array(): use convert_to_tensor() instead of np.array() so the result
  is always an OpenVINOKerasTensor rather than a raw NumPy array
- maximum() / minimum(): apply the same t1/t2 dtype-extraction pattern
  used by add() / subtract() / multiply() so that Python int/float
  literals are promoted according to JAX weak-type semantics
- reshape(): wrap a bare integer newshape in a list before passing it
  to ov_opset.constant(), which requires a 1-D shape tensor
- split(): extract constant data from an OpenVINOKerasTensor passed as
  indices_or_sections (side-effect of the array() fix); also fixes
  hsplit() and vsplit() which delegate to split()
@goyaladitya05 goyaladitya05 force-pushed the openvino/fix-numpy-array-reshape-maximum-minimum branch from f5b72ed to 4ea4368 Compare March 10, 2026 18:04
@hertschuh
Copy link
Collaborator

Duplicate of #22319

Closing since the other one was submitted. Please re-open if this is not the case.

@hertschuh hertschuh closed this Mar 10, 2026
@goyaladitya05
Copy link
Contributor Author

Closing since the other one was submitted. Please re-open if this is not the case.

We merged it in #22319, then we reverted that submission in #22342, since the CI was getting stuck. So effectively, no changes were merged earlier.

@goyaladitya05
Copy link
Contributor Author

@hertschuh Could you reopen this please? I'm not getting an option to reopen it myself.

@hertschuh hertschuh reopened this Mar 11, 2026
@hertschuh
Copy link
Collaborator

Done. Please rebase.

Removed several ReversibleEmbeddingTest cases from the excluded tests list.
@goyaladitya05 goyaladitya05 marked this pull request as draft March 11, 2026 18:56
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Keras 3 OpenVINO backend]: Improve support for array(), maximum(), minimum(), reshape() and split() operations

4 participants