Skip to content

Implement numpy.gcd using Euclidean algorithm for OpenVINO backend#22155

Merged
hertschuh merged 3 commits intokeras-team:masterfrom
andersendsa:feat/openvino-numpy-gcd
Feb 13, 2026
Merged

Implement numpy.gcd using Euclidean algorithm for OpenVINO backend#22155
hertschuh merged 3 commits intokeras-team:masterfrom
andersendsa:feat/openvino-numpy-gcd

Conversation

@andersendsa
Copy link
Copy Markdown
Contributor

@andersendsa andersendsa commented Feb 11, 2026

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

@google-cla
Copy link
Copy Markdown

google-cla Bot commented Feb 11, 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
Copy Markdown
Contributor

Summary of Changes

Hello @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 numpy.gcd. By leveraging OpenVINO's while_loop to execute the Euclidean algorithm, this change not only provides essential mathematical functionality but also unblocks previously excluded tests, ensuring the new feature's reliability and broad applicability across different data types.

Highlights

  • numpy.gcd Implementation: Implemented the numpy.gcd function for the OpenVINO backend, utilizing OpenVINO's while_loop operation to apply the Euclidean algorithm.
  • Test Enablement: Enabled corresponding tests for numpy.gcd by removing its entries from the excluded_concrete_tests.txt file.
  • Robustness: Verified the gcd implementation for correctness across various data types and element-wise operations.

🧠 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_gcd from the list of excluded tests.
    • Removed NumpyTwoInputOpsCorrectnessTest::test_gcd from the list of excluded tests.
    • Removed NumpyTwoInputOpsDynamicShapeTest::test_gcd and NumpyTwoInputOpsStaticShapeTest::test_gcd from the list of excluded tests.
  • keras/src/backend/openvino/numpy.py
    • Imported the while_loop function from keras.src.backend.openvino.core.
    • Implemented the gcd function using the Euclidean algorithm, handling absolute values, broadcasting, and a while_loop with conditional logic for b != 0.
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
Copy Markdown
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 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.

Comment on lines +1344 to +1358
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)
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

medium

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.

Suggested change
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-commenter
Copy link
Copy Markdown

codecov-commenter commented Feb 11, 2026

Codecov Report

❌ Patch coverage is 97.22222% with 1 line in your changes missing coverage. Please review.
✅ Project coverage is 82.79%. Comparing base (441d184) to head (ed16dc7).

Files with missing lines Patch % Lines
keras/src/backend/openvino/core.py 50.00% 0 Missing and 1 partial ⚠️
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           
Flag Coverage Δ
keras 82.61% <97.22%> (+<0.01%) ⬆️
keras-jax 62.29% <2.77%> (-0.04%) ⬇️
keras-numpy 56.46% <0.00%> (-0.04%) ⬇️
keras-openvino 37.53% <97.22%> (+0.05%) ⬆️
keras-tensorflow 63.51% <2.77%> (-0.04%) ⬇️
keras-torch 62.33% <2.77%> (-0.04%) ⬇️

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.

andersendsa and others added 2 commits February 11, 2026 14:58
Co-authored-by: andersendsa <199610634+andersendsa@users.noreply.github.com>
…andling

Co-authored-by: andersendsa <199610634+andersendsa@users.noreply.github.com>
@andersendsa
Copy link
Copy Markdown
Contributor Author

andersendsa commented Feb 13, 2026

@rkazants , @hertschuh This PR is ready for review, please have a look when you have the time.

Copy link
Copy Markdown
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 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.

Comment on lines +1349 to +1361
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)
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

medium

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)

Copy link
Copy Markdown
Collaborator

@hertschuh hertschuh left a comment

Choose a reason for hiding this comment

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

Thank you for implementing this!

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.

4 participants