-
Notifications
You must be signed in to change notification settings - Fork 19.6k
[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
base: master
Are you sure you want to change the base?
[OpenVINO backend] add __array__ method for OpenVINOKerasTenor #21458
Conversation
9a3f338
to
5fcdec4
Compare
Codecov ReportAttention: Patch coverage is
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
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
0843e5e
to
1b90447
Compare
/gemini review |
There was a problem hiding this 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.
keras/src/backend/openvino/core.py
Outdated
def __array__(self): | ||
if self.data is not None and isinstance(self.data, np.ndarray): | ||
return self.data | ||
return convert_to_numpy(self) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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)
.
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) |
8219e27
to
a9679d5
Compare
a9679d5
to
75647be
Compare
@rkazants
@fchollet
In https://github.com/keras-team/keras-hub/blob/master/keras_hub/src/layers/preprocessing/masked_lm_mask_generator_test.py#L46-L59
This test and many other tests need this method to run