Skip to content

Conversation

@divyashreepathihalli
Copy link
Collaborator

This PR updates the Flax dependency to 0.12.1 and JAX to 0.8.1.

It addresses test failures in the NNX integration tests (keras/src/backend/jax/core_test.py) caused by changes in Flax's nnx.Variable implementation.

nnx.Variable now relies heavily on slots and metadata, causing Keras-specific attributes stored in __dict__ to be lost during nnx.split/merge.
Fix: Registering NnxVariable as a custom Pytree node with JAX. This ensures that the instance's __dict__ (excluding the raw value) is treated as auxiliary data (static metadata) and preserved in the GraphDef.

@gemini-code-assist
Copy link
Contributor

Summary of Changes

Hello @divyashreepathihalli, 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 critical test failures in the Keras NNX integration tests by updating core dependencies, Flax and JAX, to their latest versions. The primary fix involves adapting to changes in Flax's nnx.Variable behavior, which previously caused Keras-specific metadata to be lost. By registering NnxVariable as a custom JAX Pytree node, the PR ensures proper serialization and deserialization of Keras attributes, thereby restoring test stability and compatibility with the updated libraries.

Highlights

  • Dependency Updates: The pull request updates the Flax dependency to version 0.12.1 and JAX to 0.8.1.
  • NNX Test Failures Resolution: It resolves test failures in NNX integration tests (keras/src/backend/jax/core_test.py) that arose from changes in Flax's nnx.Variable implementation.
  • Custom Pytree Registration: To address the issue, NnxVariable is now registered as a custom Pytree node with JAX, ensuring Keras-specific attributes stored in __dict__ are preserved during nnx.split/merge operations.
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 effectively addresses test failures in NNX integration by updating JAX and Flax dependencies and registering NnxVariable as a custom JAX PyTree node. This ensures Keras-specific attributes are preserved during NNX operations. The changes are well-reasoned. I have two suggestions for improvement: one to update an outdated comment in requirements.txt, and another to enhance the robustness of the new __setattr__ fallback mechanism in keras/src/backend/jax/core.py to prevent potential state inconsistencies.

Comment on lines +16 to +17
jax[cpu]==0.8.1
flax==0.12.1
Copy link
Contributor

Choose a reason for hiding this comment

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

medium

With the JAX version update, the comment on line 14 explaining the pin to 0.5.0 is now outdated. Please update or remove it to avoid confusion for future developers.

@codecov-commenter
Copy link

codecov-commenter commented Dec 1, 2025

Codecov Report

❌ Patch coverage is 3.70370% with 26 lines in your changes missing coverage. Please review.
✅ Project coverage is 61.46%. Comparing base (f2c00fe) to head (9fedd6d).
⚠️ Report is 3 commits behind head on master.

Files with missing lines Patch % Lines
keras/src/backend/jax/core.py 0.00% 25 Missing ⚠️
keras/src/export/tf2onnx_lib.py 50.00% 0 Missing and 1 partial ⚠️

❗ There is a different number of reports uploaded between BASE (f2c00fe) and HEAD (9fedd6d). Click for more details.

HEAD has 6 uploads less than BASE
Flag BASE (f2c00fe) HEAD (9fedd6d)
keras 5 2
keras-torch 1 0
keras-tensorflow 1 0
keras-jax 1 0
Additional details and impacted files
@@             Coverage Diff             @@
##           master   #21884       +/-   ##
===========================================
- Coverage   82.57%   61.46%   -21.11%     
===========================================
  Files         577      578        +1     
  Lines       59599    59813      +214     
  Branches     9351     9387       +36     
===========================================
- Hits        49213    36767    -12446     
- Misses       7978    20720    +12742     
+ Partials     2408     2326       -82     
Flag Coverage Δ
keras 61.46% <3.70%> (-20.93%) ⬇️
keras-jax ?
keras-numpy 57.43% <3.70%> (-0.09%) ⬇️
keras-openvino 34.31% <3.70%> (-0.03%) ⬇️
keras-tensorflow ?
keras-torch ?

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.

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

Thanks for the PR!

The Flax tests are failing though

Comment on lines +277 to +288
# Mirror Keras attributes to _var_metadata to ensure persistence
# if the Pytree registration is not respected by NNX.
if (
name != "_var_metadata"
and name not in ("_raw_value", "_trace_state")
and hasattr(self, "_var_metadata")
):
self._var_metadata[name] = value

object.__setattr__(self, name, value)

NnxVariable.__setattr__ = __setattr__
Copy link
Collaborator

Choose a reason for hiding this comment

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

Why do you do NnxVariable.__setattr__ = __setattr__ instead of simply declaring def __setattr__(self, name, value): directly within the NnxVariable class?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

This is needed to mirror Keras attributes into _var_metadata, so that they persist during NNX's Pytree registration, which might otherwise be missed if defined directly within the class due to the complex multiple inheritance

# Note that we test against the latest JAX on GPU.
jax[cpu]==0.5.0
flax
jax[cpu]==0.8.1
Copy link
Collaborator

Choose a reason for hiding this comment

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

This is promising, but the TensorFlow CPU tests are failing. Upgrade TF version too?

@google-ml-butler google-ml-butler bot added kokoro:force-run ready to pull Ready to be merged into the codebase labels Dec 3, 2025
@google-ml-butler google-ml-butler bot removed the ready to pull Ready to be merged into the codebase label Dec 4, 2025
@divyashreepathihalli divyashreepathihalli merged commit 9acaaf5 into keras-team:master Dec 4, 2025
18 of 20 checks passed
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.

5 participants