-
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 ReportAll modified and coverable lines are covered by tests ✅
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
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:
|
5fcdec4
to
0843e5e
Compare
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.
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) |
@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