Skip to content

Conversation

Mohamed-Ashraf273
Copy link
Contributor

@Mohamed-Ashraf273 Mohamed-Ashraf273 commented Sep 16, 2025

@rkazants
@fchollet
Solve openvino issue for the undeterministic output of randomuniform, by implementing it like ops.normal above.
Issue: openvinotoolkit/openvino#32045
Discovred from: keras-team/keras-hub#2389.
running pytest keras_hub/src/models/gemma/gemma_causal_lm_test.py::GemmaCausalLMTest::test_causal_lm_basics -v -s
without fix gives:

dtype = float32, shape = (2, 8, 11)
Mismatched elements: 176 / 176 (100%)
Max absolute difference among violations: 2.3952749
Max relative difference among violations: 132.00833
 ACTUAL: array([[[ 1.845687e-01,  4.551761e-02, -6.935402e-01,  1.822043e+00,
         -3.864409e-02,  3.144747e-01,  4.418140e-01, -1.933735e-01,
         -5.509340e-01, -3.518625e-03,  9.936208e-01],...
 DESIRED: array([[[-0.207675, -0.208218,  0.331816, -0.279717, -0.265288,
         -0.159267,  0.363558, -0.456418,  1.059087,  1.110469,
          1.437717],...
=============================================================================== 1 failed in 5.20s ===============================================================================

with fix:

1/1 ━━━━━━━━━━━━━━━━━━━━ 0s 240ms/step
1/1 ━━━━━━━━━━━━━━━━━━━━ 0s 77ms/step
1/1 ━━━━━━━━━━━━━━━━━━━━ 0s 79ms/step
SKIPPED (Model.fit() not supported on OpenVINO backend)

============================================================================== 1 skipped in 5.05s ===============================================================================

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.

Summary of Changes

Hello @Mohamed-Ashraf273, 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 aims to enhance the reliability of random number generation within the Keras OpenVINO backend. It addresses a known issue where the randomuniform operation yielded inconsistent results, ensuring that random operations are now deterministic and reproducible for improved model stability and debugging.

Highlights

  • Addressing randomuniform indeterminism: This PR resolves an issue where the randomuniform operation in the OpenVINO backend produced undeterministic outputs, as reported in OpenVINO issue #32045 and discovered via keras-team/keras-hub/pull/2389.
  • Switching to NumPy for random number generation: The implementation of uniform in the OpenVINO backend has been updated to use numpy.random.default_rng for generating random values, replacing the direct use of ov_opset.random_uniform.
  • Simplified seed handling: The seed handling logic within the uniform function has been streamlined to directly pass seed_data to the NumPy random number generator, improving consistency.
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 aims to fix a non-determinism issue with random.uniform in the OpenVINO backend by switching from ov_opset.random_uniform to a NumPy-based implementation.

While this correctly addresses the determinism for concrete shapes, it introduces a critical regression by breaking support for symbolic shapes, which is a common use case in Keras. I've left a detailed comment with a suggested fix that uses a hybrid approach to support both concrete and symbolic shapes.

Additionally, the random.categorical function also uses ov_opset.random_uniform and is likely affected by the same non-determinism bug. This should also be investigated and fixed to fully resolve the issue.

@codecov-commenter
Copy link

codecov-commenter commented Sep 16, 2025

Codecov Report

❌ Patch coverage is 71.42857% with 2 lines in your changes missing coverage. Please review.
✅ Project coverage is 82.56%. Comparing base (efb24b2) to head (b131554).
⚠️ Report is 8 commits behind head on master.

Files with missing lines Patch % Lines
keras/src/backend/openvino/random.py 71.42% 1 Missing and 1 partial ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##           master   #21670      +/-   ##
==========================================
+ Coverage   82.55%   82.56%   +0.01%     
==========================================
  Files         571      571              
  Lines       57626    57713      +87     
  Branches     9001     9016      +15     
==========================================
+ Hits        47572    47650      +78     
- Misses       7759     7765       +6     
- Partials     2295     2298       +3     
Flag Coverage Δ
keras 82.36% <71.42%> (+0.01%) ⬆️
keras-jax 63.47% <0.00%> (-0.05%) ⬇️
keras-numpy 57.82% <0.00%> (-0.04%) ⬇️
keras-openvino 34.42% <71.42%> (+0.04%) ⬆️
keras-tensorflow 64.19% <0.00%> (-0.04%) ⬇️
keras-torch 63.68% <0.00%> (-0.05%) ⬇️

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.

@Mohamed-Ashraf273 Mohamed-Ashraf273 changed the title [OpenVINO backend] solve randomuniform issue [OpenVINO backend] fix randomuniform issue Sep 16, 2025
@github-actions github-actions bot added the Gemma Gemma model specific issues label Sep 16, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Gemma Gemma model specific issues size:S
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants