Skip to content

[OpenVINO backend] add __array__ method for OpenVINOKerasTenor #21458

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

Mohamed-Ashraf273
Copy link
Contributor

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

@codecov-commenter
Copy link

codecov-commenter commented Jul 8, 2025

Codecov Report

Attention: Patch coverage is 50.00000% with 1 line in your changes missing coverage. Please review.

Project coverage is 82.81%. Comparing base (d55a767) to head (75647be).

Files with missing lines Patch % Lines
keras/src/backend/openvino/core.py 50.00% 1 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##           master   #21458      +/-   ##
==========================================
- Coverage   82.81%   82.81%   -0.01%     
==========================================
  Files         565      565              
  Lines       55520    55522       +2     
  Branches     8664     8664              
==========================================
+ Hits        45977    45978       +1     
- Misses       7428     7429       +1     
  Partials     2115     2115              
Flag Coverage Δ
keras 82.62% <50.00%> (-0.01%) ⬇️
keras-jax 63.38% <0.00%> (-0.01%) ⬇️
keras-numpy 58.58% <0.00%> (-0.01%) ⬇️
keras-openvino 33.99% <50.00%> (+<0.01%) ⬆️
keras-tensorflow 63.83% <0.00%> (-0.01%) ⬇️
keras-torch 63.50% <0.00%> (-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.

@Mohamed-Ashraf273 Mohamed-Ashraf273 force-pushed the add_array_method branch 2 times, most recently from 0843e5e to 1b90447 Compare July 8, 2025 17:30
@Mohamed-Ashraf273 Mohamed-Ashraf273 changed the title [OpenVINO backend] add __array__ method for OpenVINOKerasTenor by rai… [OpenVINO backend] add __array__ method for OpenVINOKerasTenor Jul 8, 2025
@divyashreepathihalli
Copy link
Collaborator

/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 adds the __array__ method to OpenVINOKerasTensor to improve compatibility with NumPy. A suggestion has been made to handle scalar tensors more efficiently.

Comment on lines 482 to 496
def __array__(self):
if self.data is not None and isinstance(self.data, np.ndarray):
return self.data
return convert_to_numpy(self)
Copy link
Contributor

Choose a reason for hiding this comment

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

medium

The current implementation checks if self.data is a numpy.ndarray. However, when convert_to_tensor is called with a scalar, self.data is set to that scalar value, and the check isinstance(self.data, np.ndarray) will be False. This results in calling convert_to_numpy(self), which compiles and runs an OpenVINO model to retrieve a scalar value that is already available. Consider optimizing for scalar types as well by checking if self.data is not None and then using np.asarray(self.data).

Suggested change
def __array__(self):
if self.data is not None and isinstance(self.data, np.ndarray):
return self.data
return convert_to_numpy(self)
def __array__(self):
if self.data is not None:
return np.asarray(self.data)
return convert_to_numpy(self)

@Mohamed-Ashraf273 Mohamed-Ashraf273 force-pushed the add_array_method branch 5 times, most recently from 8219e27 to a9679d5 Compare July 12, 2025 12:18
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.

4 participants