Skip to content

[OpenVINO backend] Support numpy.power and numpy.vectorize#22404

Open
Passavee-Losripat wants to merge 3 commits intokeras-team:masterfrom
Passavee-Losripat:openvino-power-vectorize
Open

[OpenVINO backend] Support numpy.power and numpy.vectorize#22404
Passavee-Losripat wants to merge 3 commits intokeras-team:masterfrom
Passavee-Losripat:openvino-power-vectorize

Conversation

@Passavee-Losripat
Copy link

Fixes openvinotoolkit/openvino#34558

Details:

  • power: Fixed dtype promotion by using dtypes.result_type(t1, t2) following the pattern from add, ensuring correct type promotion for all dtype combinations
  • vectorize: Implemented a wrapper that converts inputs to tensors via convert_to_tensor before delegating to pyfunc, allowing OpenVINO ops to handle batching natively via operator overloading

cc @rkazants

@google-cla
Copy link

google-cla bot commented Mar 12, 2026

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.

@gemini-code-assist
Copy link
Contributor

Summary of Changes

Hello, 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 numpy.power and numpy.vectorize operations. These changes address previous limitations by implementing robust dtype promotion for power and a flexible tensor-converting wrapper for vectorize, thereby improving compatibility and enabling more complex numerical computations within the OpenVINO environment.

Highlights

  • Numpy.power support: The numpy.power function in the OpenVINO backend now correctly handles dtype promotion by utilizing dtypes.result_type, ensuring accurate type handling across various dtype combinations.
  • Numpy.vectorize implementation: The numpy.vectorize function has been implemented with a wrapper that converts input arguments to tensors before passing them to the underlying Python function (pyfunc), allowing OpenVINO operations to natively manage batching through operator overloading.
  • Test re-enablement: Previously excluded tests for numpy.power and numpy.vectorize (NumpyDtypeTest::test_power and NumpyOneInputOpsCorrectnessTest::test_vectorize) have been re-enabled, indicating their successful integration and functionality.

🧠 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
  • keras/src/backend/openvino/excluded_concrete_tests.txt
    • Removed NumpyDtypeTest::test_power from the exclusion list.
    • Removed NumpyOneInputOpsCorrectnessTest::test_vectorize from the exclusion list.
  • keras/src/backend/openvino/numpy.py
    • Replaced NotImplementedError in vectorize with a functional wrapper that converts inputs to tensors.
    • Updated the power function to use dtypes.result_type for more accurate element type determination and dtype promotion.
Activity
Using Gemini Code Assist

The 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 /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

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 .gemini/ folder in the base of the repository. Detailed instructions can be found here.

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

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

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 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.

Comment on lines 4107 to +4112
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
Copy link
Contributor

Choose a reason for hiding this comment

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

high

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.

Suggested change
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
  1. 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)
  2. The style guide suggests that interchangeable objects should have identical or very close APIs. The current implementation of vectorize deviates from numpy.vectorize by silently ignoring some of its key arguments. (link)

Copy link
Author

Choose a reason for hiding this comment

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

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-commenter
Copy link

codecov-commenter commented Mar 12, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 83.03%. Comparing base (80bad7a) to head (8e53601).

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           
Flag Coverage Δ
keras 82.86% <100.00%> (+<0.01%) ⬆️
keras-jax 60.55% <0.00%> (-0.01%) ⬇️
keras-numpy 54.78% <0.00%> (-0.01%) ⬇️
keras-openvino 49.92% <100.00%> (+<0.01%) ⬆️
keras-tensorflow 61.80% <0.00%> (-0.01%) ⬇️
keras-torch 60.62% <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.

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.

[Good First Issue][Keras 3 OpenVINO backend]: Support vectorize and fix power operation

3 participants