[OpenVINO backend] Support numpy.power and numpy.vectorize#22404
[OpenVINO backend] Support numpy.power and numpy.vectorize#22404Passavee-Losripat wants to merge 3 commits intokeras-team:masterfrom
Conversation
|
Thanks for your pull request! It looks like this may be your first contribution to a Google open source project. Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA). View this failed invocation of the CLA check for more information. For the most up to date status, view the checks section at the bottom of the pull request. |
Summary of ChangesHello, 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 significantly enhances the OpenVINO backend by adding full support for Highlights
🧠 New Feature in Public Preview: You can now enable Memory to help Gemini Code Assist learn from your team's feedback. This makes future code reviews more consistent and personalized to your project's style. Click here to enable Memory in your admin console. Changelog
Activity
Using Gemini Code AssistThe 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 by creating a comment using either
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 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. If you're interested in giving your feedback about your experience with Gemini Code Assist for GitHub and other Google products, sign up here. 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
|
There was a problem hiding this comment.
Code Review
This pull request adds support for numpy.power and numpy.vectorize to the OpenVINO backend. The implementation for power correctly handles dtype promotion by using dtypes.result_type, which is a good improvement. For vectorize, I've suggested an improvement to explicitly handle unsupported arguments to avoid silent failures and make the API more robust for users.
| def vectorize(pyfunc, *, excluded=None, signature=None): | ||
| raise NotImplementedError( | ||
| "`vectorize` is not supported with openvino backend" | ||
| ) | ||
| def wrapper(*args, **kwargs): | ||
| converted_args = tuple(convert_to_tensor(arg) for arg in args) | ||
| return pyfunc(*converted_args, **kwargs) | ||
|
|
||
| return wrapper |
There was a problem hiding this comment.
The current implementation of vectorize accepts excluded and signature arguments but silently ignores them. This can lead to unexpected behavior for users who rely on these parameters as they would in numpy.vectorize.
The Keras API design guidelines state:
- "Catch user errors early and anticipate common mistakes." (line 139)
- "Objects that do interchangeable things should have identical or very close APIs." (line 60)
To make the API more robust, prevent silent failures, and align better with the guidelines, it's better to explicitly notify the user that these parameters are not supported.
Please consider raising a NotImplementedError if excluded or signature are provided.
| def vectorize(pyfunc, *, excluded=None, signature=None): | |
| raise NotImplementedError( | |
| "`vectorize` is not supported with openvino backend" | |
| ) | |
| def wrapper(*args, **kwargs): | |
| converted_args = tuple(convert_to_tensor(arg) for arg in args) | |
| return pyfunc(*converted_args, **kwargs) | |
| return wrapper | |
| def vectorize(pyfunc, *, excluded=None, signature=None): | |
| if excluded is not None: | |
| raise NotImplementedError( | |
| "`vectorize` with the `excluded` argument is not supported with " | |
| "the OpenVINO backend." | |
| ) | |
| if signature is not None: | |
| raise NotImplementedError( | |
| "`vectorize` with the `signature` argument is not supported with " | |
| "the OpenVINO backend." | |
| ) | |
| def wrapper(*args, **kwargs): | |
| converted_args = tuple(convert_to_tensor(arg) for arg in args) | |
| return pyfunc(*converted_args, **kwargs) | |
| return wrapper |
References
- The style guide suggests catching user errors early and anticipating common mistakes. The current implementation silently ignores unsupported parameters, which can lead to user error. The suggestion is to explicitly raise an error for unsupported parameters. (link)
- The style guide suggests that interchangeable objects should have identical or very close APIs. The current implementation of
vectorizedeviates fromnumpy.vectorizeby silently ignoring some of its key arguments. (link)
There was a problem hiding this comment.
For this Gemini code assist's suggestion, the signature argument is intentionally supported. In the test case, signature='(d,d)->()' with knp.trace is used and the current code passes correctly. Raising NotImplementedError for signature would break this test. The current implementation correctly handles signature when used with OpenVINO ops.
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## master #22404 +/- ##
=======================================
Coverage 83.03% 83.03%
=======================================
Files 596 596
Lines 66684 66686 +2
Branches 10382 10380 -2
=======================================
+ Hits 55371 55373 +2
Misses 8676 8676
Partials 2637 2637
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:
|
Fixes openvinotoolkit/openvino#34558
Details:
power: Fixed dtype promotion by usingdtypes.result_type(t1, t2)following the pattern fromadd, ensuring correct type promotion for all dtype combinationsvectorize: Implemented a wrapper that converts inputs to tensors viaconvert_to_tensorbefore delegating topyfunc, allowing OpenVINO ops to handle batching natively via operator overloadingcc @rkazants