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

All modified and coverable lines are covered by tests ✅

Project coverage is 82.75%. Comparing base (be9b002) to head (1b90447).
Report is 18 commits behind head on master.

Additional details and impacted files
@@            Coverage Diff             @@
##           master   #21458      +/-   ##
==========================================
+ Coverage   82.72%   82.75%   +0.02%     
==========================================
  Files         565      565              
  Lines       55219    55350     +131     
  Branches     8608     8635      +27     
==========================================
+ Hits        45682    45805     +123     
- Misses       7427     7443      +16     
+ Partials     2110     2102       -8     
Flag Coverage Δ
keras 82.56% <100.00%> (+0.02%) ⬆️
keras-jax 63.32% <0.00%> (-0.01%) ⬇️
keras-numpy 58.54% <0.00%> (+0.01%) ⬆️
keras-openvino 33.77% <100.00%> (-0.03%) ⬇️
keras-tensorflow 63.76% <0.00%> (+0.01%) ⬆️
keras-torch 63.37% <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 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

@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 +485
def __array__(self):
if self.data is not None and isinstance(self.data, np.ndarray):
return self.data
return convert_to_numpy(self)

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)

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