[Good First Issue][Keras 3 OpenVINO backend]: Support vectorize and fix power operation
Context
🚀 A great opportunity to contribute to two popular AI projects with just one PR: Keras 3 and OpenVINO.
Keras 3 enables seamless switching between supported backends—PyTorch, TensorFlow, and JAX. Since Keras 3.8.0, the OpenVINO backend allows developers to leverage OpenVINO for model predictions directly within Keras 3 workflows. Currently, the OpenVINO backend lacks support for some operations. Our goal is to resolve this gap and to optimize it for inference on Intel devices by supporting as many models as possible while delivering optimal performance.
What needs to be done?
Implement vectorize and resolve existing issues with the power operation in the OpenVINO backend.
The vectorize operation is required to map custom functions over arrays, while the power implementation currently exists but fails Keras suite tests, likely due to broadcasting logic or dtype handling.
These should be implemented/fixed in keras/src/backend/openvino/numpy.py and must align with NumPy-equivalent behavior.
Steps to Contribute
First use .take to get the issue assigned to you.
Since this involves cross-repo work between Keras and OpenVINO, please follow these steps carefully:
-
Set up your developer environment:
- Fork and clone the Keras repository.
- Install the requirements:
pip install -r requirements.txt.
-
Implement the operation:
- Provide the decomposition in Python for the targeted operation in
keras/src/backend/openvino/numpy.py. - Use the OpenVINO Operation Set for the implementation.
- Provide the decomposition in Python for the targeted operation in
-
Enable and verify tests:
- Remove the line corresponding to the implemented operation from the
excluded_concrete_tests.txtfile (located in the OpenVINO backend directory). - Create a
pytest.inifile in the root directory of your cloned repository with the following content to set the backend:[pytest] env = KERAS_BACKEND=openvino - Run the tests to ensure everything is passing:
pytest -c ./pytest.ini ./keras/src/ops/numpy_test.py
- Remove the line corresponding to the implemented operation from the
-
Submit your PR:
- Commit your changes and create a Pull Request in the Keras repository.
- Mention @rkazants in the PR description for review.
Example Pull Requests
- https://github.com/keras-team/keras/pull/20883
Resources
- Contribution guide - start here!
- Intel DevHub Discord channel - engage in discussions, ask questions and talk to OpenVINO developers
- How to link your Pull Request to an issue
Contact points
@rkazants
AI notice - Important!
We encourage contributors to use AI tools responsibly when creating Pull Requests. While AI can be a valuable aid, it is essential to ensure that your contributions meet the task requirements, build successfully, include relevant tests, and pass all linters. Submissions that do not meet these standards may be closed without warning to maintain the quality and integrity of the project. Please take the time to understand the changes you are proposing and their impact.
Ticket
#34017
.take
These good first issues are created specifically for new contributors. I request anybody who has contributed to this roadmap before, to not work on these. Good luck ! If you have any questions, please feel free to ask here.
.take
@Tamilawre this issue is already assigned to a contributor.
@Tamilawre Hi, I'm currently working on this and I planned to finish within this week. Please move on to other issues but if I cannot finish it I will let you know.
Alright.
@rkazants Hi, I have a question for implementing vectorize and I have looked into other backend implementations:
- Numpy Backend: directly used
np.vectorize - JAX Backend: directly used
jnp.vectorize - Pytorch, Tensorflow: use their implemented
vectorize_implwhich requiresvmapequivalent
Since OpenVINO doesn't have features similar to vmap and cannot trace arbitrary function in python into computational graph, I'm planning to convert to numpy array and use np.vectorize (similar to the NumPy backend). Is this approach acceptable?
Since OpenVINO doesn't have features similar to
vmapand cannot trace arbitrary function in python into computational graph, I'm planning to convert to numpy array and usenp.vectorize(similar to the NumPy backend). Is this approach acceptable?
The implementation needs to be done using OpenVINO opsets specifically. If there is not a direct function for this, then we use the existing functions to implement it. You can refer to other implementations in the keras/src/backend/openvino directory.
Thank you for your guidance! I have looked into other implementations and found out that most of the existing functions like knp.trace, which is used in the test case, already handle batching natively as OpenVINO ops. So I implemented a simple wrapper that delegates directly to pyfunc, and it passes the test.
But I'm still unsure how to handle truly arbitrary Python functions like:
def myfunc(a, b):
return a + b
vfunc = knp.vectorize(myfunc)
vfunc([1, 2, 3, 4], 2) # TypeError: can only concatenate list to list
The simple wrapper fails here because it passes the full list to myfunc instead of applying it element-wisely. From my understanding, OpenVINO is implemented to improve the existing computational graph so we don't have feature for tracing back python function and control flow into graph nodes so we may need the numpy fallback for this case.
So for clarification, should vectorize only support functions already implemented as OpenVINO ops (like knp.trace), or should we also provide a numpy fallback for arbitrary Python functions? If you have a preferred approach in mind, would you mind sharing it?
So for clarification, should vectorize only support functions already implemented as OpenVINO ops (like
knp.trace), or should we also provide a numpy fallback for arbitrary Python functions? If you have a preferred approach in mind, would you mind sharing it?
You're right that OpenVINO can't trace arbitrary Python control flow. But for arithmetic functions like myfunc(a, b): return a + b, the TypeError is just because raw Python lists are passed without conversion, OpenVINOKerasTensor already has __add__, __mul__ etc. that dispatch to OV ops. Adding a convert_to_tensor step in the wrapper before calling pyfunc would handle that case correctly via broadcasting, without needing any numpy fallback.
Adding a convert_to_tensor step in the wrapper before calling pyfunc would handle that case correctly via broadcasting, without needing any numpy fallback.
I see. Thank you so much for your guidance! I've updated the implementation with convert_to_tensor and tested it with several arithmetic functions. All of them are working correctly via OpenVINO's operator overloading. PR is open here: keras-team/keras#22404
.take
@Bigdreams415 this issue is already assigned to a contributor.
.take
@MDerazNasr this issue is already assigned to a contributor.
Thank you for the contributions @Passavee-Losripat! Closing the issue since the linked PR has been merged.
Thank you @goyaladitya05 for opening the issue and the guidance on the implementation approach! It helped me understand the right direction.