Skip to content

[OpenVINO] Implement cond operator and enable tests for Hinge losses#22409

Open
goyaladitya05 wants to merge 3 commits intokeras-team:masterfrom
goyaladitya05:openvino-squared-hinge
Open

[OpenVINO] Implement cond operator and enable tests for Hinge losses#22409
goyaladitya05 wants to merge 3 commits intokeras-team:masterfrom
goyaladitya05:openvino-squared-hinge

Conversation

@goyaladitya05
Copy link
Contributor

This PR implements cond operator for the OpenVINO backend using ov_opset.select, which evaluates both branches as graph nodes and selects the result based on pred.

Enabled 9 previously excluded tests for cond, Hinge, and SquaredHinge.

Closes: openvinotoolkit/openvino/issues/34672

@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's capabilities by introducing support for conditional operations and expanding test coverage for critical loss functions. The core change involves implementing the cond operator, which allows for dynamic graph execution paths based on a predicate. This integration, coupled with the re-enabling of related tests, ensures greater robustness and broader functionality for Keras models deployed with OpenVINO.

Highlights

  • OpenVINO cond operator implementation: The cond operator has been fully implemented for the OpenVINO backend, replacing the previous NotImplementedError. This implementation leverages ov_opset.select to evaluate both true and false branches as graph nodes and select the result based on the predicate.
  • Enabled tests for cond and Hinge losses: Nine previously excluded tests have been re-enabled. These include tests for the cond operator, as well as various tests for Hinge and SquaredHinge losses, indicating improved compatibility and functionality with the OpenVINO backend.

🧠 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/core.py
    • Implemented the cond operator using ov_opset.select to handle conditional execution.
    • Added logic to convert the predicate to a boolean OpenVINO constant or output.
    • Included type conversion for true and false branch outputs to ensure compatibility.
    • Enabled support for lists and tuples as return values for the true and false functions.
  • keras/src/backend/openvino/excluded_concrete_tests.txt
    • Removed CoreOpsCorrectnessTest::test_cond from the exclusion list.
    • Removed all HingeTest entries (e.g., test_dtype_arg, test_unweighted, test_weighted, test_zero_weighted) from the exclusion list.
    • Removed all SquaredHingeTest entries (e.g., test_dtype_arg, test_unweighted, test_weighted, test_zero_weighted) from the exclusion list.
Activity
  • No human activity has been recorded on this pull request yet.
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 implements the cond operator for the OpenVINO backend and enables several previously excluded tests. While the implementation enables new functionality, it has a critical issue: the cond operator eagerly evaluates both true_fn and false_fn branches, which is inconsistent with other Keras backends and can lead to incorrect behavior or performance degradation. This should be addressed by using a proper control flow operator like OpenVINO's If operator. Additionally, there's a medium-severity issue regarding one-sided type promotion that could be improved for robustness.

@codecov-commenter
Copy link

codecov-commenter commented Mar 12, 2026

Codecov Report

❌ Patch coverage is 86.66667% with 2 lines in your changes missing coverage. Please review.
✅ Project coverage is 83.04%. Comparing base (d8e68b0) to head (d1760a7).

Files with missing lines Patch % Lines
keras/src/backend/openvino/core.py 86.66% 1 Missing and 1 partial ⚠️
Additional details and impacted files
@@           Coverage Diff           @@
##           master   #22409   +/-   ##
=======================================
  Coverage   83.04%   83.04%           
=======================================
  Files         596      596           
  Lines       66693    66708   +15     
  Branches    10383    10387    +4     
=======================================
+ Hits        55382    55395   +13     
- Misses       8675     8676    +1     
- Partials     2636     2637    +1     
Flag Coverage Δ
keras 82.86% <86.66%> (+<0.01%) ⬆️
keras-jax 60.53% <0.00%> (-0.02%) ⬇️
keras-numpy 54.76% <0.00%> (-0.02%) ⬇️
keras-openvino 50.03% <86.66%> (+0.10%) ⬆️
keras-tensorflow 61.78% <0.00%> (-0.02%) ⬇️
keras-torch 60.60% <0.00%> (-0.02%) ⬇️

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.

Comment on lines -65 to -71
CosineDecayRestartsTest::test_alpha
CosineDecayRestartsTest::test_decay
CosineDecayRestartsTest::test_float64
CosineDecayRestartsTest::test_mmul
CosineDecayRestartsTest::test_tmul
CosineDecayTest::test_warmup
CosineDecayTest::test_warmup_decay
Copy link
Contributor Author

@goyaladitya05 goyaladitya05 Mar 12, 2026

Choose a reason for hiding this comment

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

These have no use case for OpenVINO backend (it being inference-only), these will never be used. But after the implementation of cond, the tests are passing.

After this change, the tests will run in the CI (since they do not have @pytest.mark.requires_trainable_backend decorator) and pass, since they are mathematical tests.

Should I keep them excluded or enable them ?
Or maybe we could have @pytest.mark.requires_trainable_backend decorators for LR schedule releted tests, since they will be used in training only.

Copy link
Collaborator

Choose a reason for hiding this comment

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

If they pass, let's run them.

The @pytest.mark.requires_trainable_backend was really a way to say "requires auto differentiation (gradients)" support. I think it's fine to still run tests that are training related but don't use gradients like LR schedules.

Comment on lines +853 to +854
true_val = true_fn()
false_val = false_fn()
Copy link
Collaborator

Choose a reason for hiding this comment

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

Hmm... so the whole point of cond is that you should never have to evaluate the value of both true_fn and false_fn.

But I suppose this is a limitation of OpenVino, right?

Comment on lines +856 to +857
if true_val is None:
return None
Copy link
Collaborator

Choose a reason for hiding this comment

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

Is this really needed?

Comment on lines -65 to -71
CosineDecayRestartsTest::test_alpha
CosineDecayRestartsTest::test_decay
CosineDecayRestartsTest::test_float64
CosineDecayRestartsTest::test_mmul
CosineDecayRestartsTest::test_tmul
CosineDecayTest::test_warmup
CosineDecayTest::test_warmup_decay
Copy link
Collaborator

Choose a reason for hiding this comment

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

If they pass, let's run them.

The @pytest.mark.requires_trainable_backend was really a way to say "requires auto differentiation (gradients)" support. I think it's fine to still run tests that are training related but don't use gradients like LR schedules.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Keras 3 OpenVINO backend]: Support cond operator and hinge losses

4 participants