Skip to content

[OpenVINO backend] fix numpy conversions #21498

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged

Conversation

Mohamed-Ashraf273
Copy link
Contributor

@Mohamed-Ashraf273 Mohamed-Ashraf273 commented Jul 21, 2025

@rkazants
@fchollet

I've noticed that this test (and several similar ones) pass on all backends except OpenVINO:
https://github.com/keras-team/keras-hub/blob/master/keras_hub/src/layers/preprocessing/masked_lm_mask_generator_test.py#L46-L59

After investigating, I found that the following minimal example works correctly on TensorFlow, JAX, and Torch backends, but fails with OpenVINO:

import os
os.environ["CUDA_VISIBLE_DEVICES"] = "-1"
os.environ["KERAS_BACKEND"] = "openvino"

from keras import ops
import numpy as np

tensor = np.array([[1, 2, 3], [4, 5, 6]], dtype="float32")
tensor = ops.convert_to_tensor(tensor)
print(np.array(tensor, dtype="int32"))  # Works on other backends, fails here for OpenVINO
(env) mohamed-ashraf@mohamed-ashraf-LOQ-15IRX9:~/Desktop/GSoC2025$ python test_array.py 
/home/mohamed-ashraf/Desktop/GSoC2025/env/lib/python3.12/site-packages/openvino/runtime/__init__.py:10: DeprecationWarning: The `openvino.runtime` module is deprecated and will be removed in the 2026.0 release. Please replace `openvino.runtime` with `openvino`.
  warnings.warn(
[[1 2 3]
 [4 5 6]]

To confirm the fix, I also created a reproducer that explicitly simulates the failure when an OpenVINOKerasTensor is used as a symbolic placeholder and .numpy() conversion is attempted:

import os
os.environ["CUDA_VISIBLE_DEVICES"] = "-1"
os.environ["KERAS_BACKEND"] = "openvino"

import numpy as np
import openvino as ov
from openvino import opset14 as ov_opset
from keras.src.backend.openvino.core import OpenVINOKerasTensor

# Construct a symbolic tensor manually
x = OpenVINOKerasTensor(ov_opset.parameter(shape=(3,), dtype=ov.Type.f32).output(0))
y = x * 2

print(np.array(y))  # Expected to raise ValueError for symbolic tensors
(env) mohamed-ashraf@mohamed-ashraf-LOQ-15IRX9:~/Desktop/GSoC2025$ python test_array.py 
/home/mohamed-ashraf/Desktop/GSoC2025/env/lib/python3.12/site-packages/openvino/runtime/__init__.py:10: DeprecationWarning: The `openvino.runtime` module is deprecated and will be removed in the 2026.0 release. Please replace `openvino.runtime` with `openvino`.
  warnings.warn(
Traceback (most recent call last):
  File "/home/mohamed-ashraf/Desktop/GSoC2025/keras/keras/src/backend/openvino/core.py", line 685, in convert_to_numpy
    ov_model = Model(results=[ov_result], parameters=[])
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/mohamed-ashraf/Desktop/GSoC2025/env/lib/python3.12/site-packages/openvino/_ov_api.py", line 81, in __init__
    self.__model = ModelBase(**kwargs)
                   ^^^^^^^^^^^^^^^^^^^
RuntimeError: Check 'unregistered_parameters.str().empty()' failed at src/core/src/model.cpp:58:
Model references undeclared parameters: opset1::Parameter Parameter_1 () -> (f32[3])



The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "/home/mohamed-ashraf/Desktop/GSoC2025/keras/keras/src/backend/openvino/core.py", line 498, in __array__
    return convert_to_numpy(tensor)
           ^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/mohamed-ashraf/Desktop/GSoC2025/keras/keras/src/backend/openvino/core.py", line 689, in convert_to_numpy
    raise RuntimeError(
RuntimeError: `convert_to_numpy` failed to convert the tensor.

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "/home/mohamed-ashraf/Desktop/GSoC2025/test_array.py", line 26, in <module>
    print(np.array(y))
          ^^^^^^^^^^^
  File "/home/mohamed-ashraf/Desktop/GSoC2025/keras/keras/src/backend/openvino/core.py", line 500, in __array__
    raise RuntimeError(
RuntimeError: An OpenVINOKerasTensor is symbolic: it's a placeholder for a shape and a dtype.
It doesn't have any actual numerical value.
You cannot convert it to a NumPy array.

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.

Summary of Changes

Hello @Mohamed-Ashraf273, 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 addresses a critical compatibility issue with the OpenVINO backend where direct conversion of Keras tensors to NumPy arrays was not functioning as expected, unlike other Keras backends. My changes enable seamless NumPy conversion for concrete tensors and provide clearer error messages for symbolic tensors, while also improving the internal data type handling for casting operations.

Highlights

  • NumPy Conversion for OpenVINO Tensors: I've added __array__ and numpy() methods to the OpenVINOKerasTensor class. This allows concrete OpenVINO tensors to be directly converted to NumPy arrays using np.array(tensor) or tensor.numpy(), aligning its behavior with other Keras backends. Symbolic tensors will now raise a clear RuntimeError explaining that they cannot be converted to NumPy.
  • Improved Error Handling: The convert_to_numpy utility function now catches general exceptions during the conversion process and re-raises a more informative RuntimeError, making debugging easier when a tensor fails to convert.
  • Standardized Data Type Casting: I've introduced a call to standardize_dtype(dtype) within the cast function. This ensures that the input dtype is consistently processed before being mapped to an OpenVINO type, improving robustness.
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 in your pull request via creating an issue comment (i.e. comment on the pull request page) using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands.

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 issue 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 or fill out our survey to provide feedback.

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 addresses NumPy conversion issues for the OpenVINO backend. The main change is the implementation of __array__ and numpy methods for OpenVINOKerasTensor, which now correctly handle dtype conversions. The error handling for converting symbolic tensors to NumPy arrays is also significantly improved, with better exception handling and more informative, user-facing error messages. The changes are well-implemented and enhance both the correctness and usability of the OpenVINO backend.

Co-authored-by: gemini-code-assist[bot] <176961590+gemini-code-assist[bot]@users.noreply.github.com>
@codecov-commenter
Copy link

codecov-commenter commented Jul 21, 2025

Codecov Report

Attention: Patch coverage is 27.27273% with 8 lines in your changes missing coverage. Please review.

Project coverage is 82.84%. Comparing base (55263ca) to head (5d64b63).

Files with missing lines Patch % Lines
keras/src/backend/openvino/core.py 27.27% 8 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##           master   #21498      +/-   ##
==========================================
- Coverage   82.85%   82.84%   -0.01%     
==========================================
  Files         565      565              
  Lines       55694    55703       +9     
  Branches     8689     8689              
==========================================
+ Hits        46144    46147       +3     
- Misses       7434     7440       +6     
  Partials     2116     2116              
Flag Coverage Δ
keras 82.65% <27.27%> (-0.01%) ⬇️
keras-jax 63.34% <0.00%> (-0.02%) ⬇️
keras-numpy 58.54% <0.00%> (-0.01%) ⬇️
keras-openvino 34.00% <27.27%> (-0.01%) ⬇️
keras-tensorflow 63.82% <0.00%> (-0.02%) ⬇️
keras-torch 63.41% <0.00%> (-0.02%) ⬇️

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.

@Mohamed-Ashraf273
Copy link
Contributor Author

Mohamed-Ashraf273 commented Jul 22, 2025

Copy link
Contributor

@rkazants rkazants left a comment

Choose a reason for hiding this comment

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

@fchollet, looks good to me. Recommend to merge.

@fchollet fchollet merged commit eecfe90 into keras-team:master Jul 24, 2025
8 checks passed
@Mohamed-Ashraf273 Mohamed-Ashraf273 deleted the fix-numpy-array-conversions branch July 24, 2025 22:38
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants