Implement numpy.gcd using Euclidean algorithm for OpenVINO backend#22155
Implement numpy.gcd using Euclidean algorithm for OpenVINO backend#22155hertschuh merged 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 @andersendsa, 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's compatibility with NumPy operations by introducing a robust implementation of 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 implements numpy.gcd for the OpenVINO backend. The implementation correctly uses the Euclidean algorithm with OpenVINO's loop operation. The changes look good, and the corresponding tests are enabled. I have one suggestion to simplify the loop condition logic for better readability and maintainability.
| def cond(a, b): | ||
| b = get_ov_output(b) | ||
| zero = ov_opset.constant(0, b.get_element_type()).output(0) | ||
| not_zero = ov_opset.not_equal(b, zero).output(0) | ||
|
|
||
| shape_b = ov_opset.shape_of(b, Type.i64).output(0) | ||
| rank_b = ov_opset.shape_of(shape_b, Type.i64).output(0) | ||
| axes = ov_opset.range( | ||
| ov_opset.constant(0, Type.i64).output(0), | ||
| rank_b, | ||
| ov_opset.constant(1, Type.i64).output(0), | ||
| Type.i64, | ||
| ).output(0) | ||
|
|
||
| return ov_opset.reduce_logical_or(not_zero, axes, False).output(0) |
There was a problem hiding this comment.
The cond function can be simplified by reusing the any function from this file. This would make the logic for checking if any element in b is non-zero more concise and readable.
| def cond(a, b): | |
| b = get_ov_output(b) | |
| zero = ov_opset.constant(0, b.get_element_type()).output(0) | |
| not_zero = ov_opset.not_equal(b, zero).output(0) | |
| shape_b = ov_opset.shape_of(b, Type.i64).output(0) | |
| rank_b = ov_opset.shape_of(shape_b, Type.i64).output(0) | |
| axes = ov_opset.range( | |
| ov_opset.constant(0, Type.i64).output(0), | |
| rank_b, | |
| ov_opset.constant(1, Type.i64).output(0), | |
| Type.i64, | |
| ).output(0) | |
| return ov_opset.reduce_logical_or(not_zero, axes, False).output(0) | |
| def cond(a, b): | |
| b = get_ov_output(b) | |
| zero = ov_opset.constant(0, b.get_element_type()).output(0) | |
| not_zero = ov_opset.not_equal(b, zero).output(0) | |
| return any(OpenVINOKerasTensor(not_zero)) |
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## master #22155 +/- ##
=======================================
Coverage 82.78% 82.79%
=======================================
Files 592 592
Lines 63357 63393 +36
Branches 9941 9942 +1
=======================================
+ Hits 52452 52488 +36
Misses 8354 8354
Partials 2551 2551
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:
|
Co-authored-by: andersendsa <199610634+andersendsa@users.noreply.github.com>
…andling Co-authored-by: andersendsa <199610634+andersendsa@users.noreply.github.com>
|
@rkazants , @hertschuh This PR is ready for review, please have a look when you have the time. |
There was a problem hiding this comment.
Code Review
This pull request implements the numpy.gcd operation for the OpenVINO backend using the Euclidean algorithm within a while_loop. The changes look solid, and enabling the corresponding tests is a great step forward for backend parity. I have one suggestion to improve the readability and maintainability of the cond function within the gcd implementation.
| shape_b = ov_opset.shape_of(b, Type.i64).output(0) | ||
| rank_b = ov_opset.shape_of(shape_b, Type.i64).output(0) | ||
| rank_b_scalar = ov_opset.squeeze( | ||
| rank_b, ov_opset.constant(0, Type.i32) | ||
| ).output(0) | ||
| axes = ov_opset.range( | ||
| ov_opset.constant(0, Type.i64).output(0), | ||
| rank_b_scalar, | ||
| ov_opset.constant(1, Type.i64).output(0), | ||
| Type.i64, | ||
| ).output(0) | ||
|
|
||
| return ov_opset.reduce_logical_or(not_zero, axes, False).output(0) |
There was a problem hiding this comment.
The logic to determine all axes for reduction is a bit verbose. A simpler and more readable approach is to flatten the not_zero tensor and then perform the reduction over the single flattened axis (axis 0). This pattern is also used elsewhere in this file (e.g., in _resolve_axis when axis is None).
flatten_shape = ov_opset.constant([-1], Type.i32).output(0)
not_zero_flat = ov_opset.reshape(not_zero, flatten_shape, False).output(0)
axis = ov_opset.constant(0, Type.i32).output(0)
return ov_opset.reduce_logical_or(not_zero_flat, axis, False).output(0)
hertschuh
left a comment
There was a problem hiding this comment.
Thank you for implementing this!
Details:
Implemented numpy.gcd in keras/src/backend/openvino/numpy.py using OpenVINO's loop operation (Euclidean algorithm).
Enabled corresponding tests in keras/src/backend/openvino/excluded_concrete_tests.txt.
Verified that the implementation works correctly with various dtypes and element-wise operations.
closes issue : openvinotoolkit/openvino#34056
ticket : #34017