Skip to content

Conversation

@hertschuh
Copy link
Collaborator

Previously assign would incorrectly cast the value to assign to the autocast dtype instead of the true dtype of the variable.

Because on JAX and OpenVino variables are just a reference to an array, this would cause the variable value to change dtypes.

Previously `assign` would incorrectly cast the value to assign to the autocast dtype instead of the true dtype of the variable.

Because on JAX and OpenVino variables are just a reference to an array, this would cause the variable value to change dtypes.
@gemini-code-assist
Copy link
Contributor

Summary of Changes

Hello @hertschuh, 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 addresses a critical bug in the assign method of Keras variables when operating within an autocast scope. Previously, the method would incorrectly cast the assigned value to the active autocast dtype rather than preserving the variable's original, intended dtype. This behavior could lead to unexpected dtype changes, particularly problematic for backends like JAX and OpenVino where variables are essentially references to arrays. The implemented fix ensures that assignments always respect the variable's true dtype, thereby maintaining data integrity and consistency across different backend implementations.

Highlights

  • Corrected Variable Assignment in Autocast Scope: The assign method for variables now correctly casts the input value to the variable's true _dtype instead of the potentially autocasted dtype.
  • Prevented Unintended Dtype Changes: This fix resolves an issue where variables in JAX and OpenVino backends could have their dtypes unintentionally altered due to incorrect casting during assignment within an autocast scope.
  • Enhanced Test Coverage: New test cases (test_autocasting_float_assign) have been added to specifically validate the assign method's behavior under autocasting conditions, ensuring the fix works as expected.
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.

@hertschuh
Copy link
Collaborator Author

@yangliyl-g

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 correctly fixes a bug where assigning a value to a variable within an autocast scope would incorrectly use the autocast dtype instead of the variable's true dtype. The change is simple and effective. The associated tests have been refactored for better clarity and a new test has been added to specifically cover the bug fix. I've added a few suggestions to make the tests even more explicit and robust. Overall, this is a solid pull request.

@codecov-commenter
Copy link

codecov-commenter commented Nov 21, 2025

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 82.57%. Comparing base (edbf8f5) to head (ca425be).
⚠️ Report is 1 commits behind head on master.

Additional details and impacted files
@@            Coverage Diff             @@
##           master   #21864      +/-   ##
==========================================
+ Coverage   82.47%   82.57%   +0.09%     
==========================================
  Files         577      577              
  Lines       59508    59568      +60     
  Branches     9332     9345      +13     
==========================================
+ Hits        49080    49187     +107     
+ Misses       8015     7975      -40     
+ Partials     2413     2406       -7     
Flag Coverage Δ
keras 82.38% <100.00%> (+0.08%) ⬆️
keras-jax 62.86% <100.00%> (-0.04%) ⬇️
keras-numpy 57.51% <100.00%> (-0.04%) ⬇️
keras-openvino 34.33% <100.00%> (-0.02%) ⬇️
keras-tensorflow 64.39% <100.00%> (+0.26%) ⬆️
keras-torch 63.56% <100.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.

@google-ml-butler google-ml-butler bot added kokoro:force-run ready to pull Ready to be merged into the codebase labels Nov 21, 2025
@hertschuh hertschuh merged commit 529e162 into keras-team:master Nov 21, 2025
13 of 14 checks passed
@hertschuh hertschuh deleted the autocast_var_assign branch November 21, 2025 17:13
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

ready to pull Ready to be merged into the codebase size:S

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants