Skip to content

Conversation

@SamanehSaadat
Copy link
Member

No description provided.

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 @SamanehSaadat, 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 introduces a new guide for Keras users, demonstrating how to implement Orbax checkpointing when training models with the JAX backend. This is particularly beneficial for multi-host training scenarios where the default .keras format may not be suitable.

Highlights

  • New Orbax Checkpointing Guide: A comprehensive guide (guides/orbax_checkpoint.py) has been added, detailing the process of using Orbax for checkpointing in Keras with the JAX backend.
  • Custom Checkpointing Utilities: The guide introduces and explains two custom Python classes, KerasOrbaxCheckpointManager and OrbaxCheckpointCallback, which wrap Orbax's functionality for seamless integration with Keras models.
  • Practical Example: The guide includes a runnable example demonstrating how to set up and use the OrbaxCheckpointCallback during model training.
  • Guide Integration: The new guide has been registered in scripts/guides_master.py to ensure it appears in the documentation.
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 in your pull request via creating an issue comment (i.e. comment on the pull request page) using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands.

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 issue 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 adds a new guide for using Orbax for checkpointing in Keras with the JAX backend. The guide is well-structured and provides useful utility classes. I've provided some suggestions to improve the robustness and usability of the provided OrbaxCheckpointCallback and KerasOrbaxCheckpointManager utilities, including automatically inferring steps_per_epoch, handling potential None values during state restoration, and improving code style and logging practices.

Copy link
Contributor

@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! This is very useful.

"""

"""
## Introduction
Copy link
Contributor

Choose a reason for hiding this comment

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

nitpick: add an empty line after each header, like:

## Introduction

Orbax is...

This guide explains how to do Orbax checkpointing when training a model in
the Jax backend.

The default `.keras` format doesn't support multi-host checkpointing so if
Copy link
Contributor

Choose a reason for hiding this comment

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

Let's rephrase this slightly, it's not intrinsically the file format that's wrong. How about:

Note that you should use Orbax checkpointing for multi-host training as the default Keras checkpointing currently does not support multi-host.

Copy link
Member Author

Choose a reason for hiding this comment

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

Sounds good! Done

"""

"""
We need to set the Keras backend to Jax as this guide is intended for the
Copy link
Contributor

Choose a reason for hiding this comment

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

I see both JAX and Jax. Let's stay consistent and use JAX everywhere.

"""

"""shell
pip install -q -u orbax-checkpoint
Copy link
Contributor

Choose a reason for hiding this comment

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

I think it's -U uppercase for upgrade.

Copy link
Member Author

Choose a reason for hiding this comment

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

Right :D fixed

super().__init__(checkpoint_dir, options=options)

def _get_state(self):
"""Gets the model state and metrics"""
Copy link
Contributor

Choose a reason for hiding this comment

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

Add Returns: to docstring

if keras.config.backend() != "jax":
raise ValueError(
"`OrbaxCheckpointCallback` is only supported on a "
"`jax` backend. Provided backend is %s." % keras.config.backend()
Copy link
Contributor

Choose a reason for hiding this comment

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

Use f-string here.

"""
Now if you look at the Orbax checkpoint directory, you can see all the files
saved as part of Orbax checkpointing.
"""
Copy link
Contributor

Choose a reason for hiding this comment

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

Oh maybe you can add:

"""shell
ls -l /tmp/ckpt
"""

Does that work?

Copy link
Member Author

Choose a reason for hiding this comment

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

Orbax checkpoint has nested directories so I initially had this:

"""shell
ls -R /tmp/ckpt
"""

Which outputs:
Image
I wasn't sure if it looks good or not so I removed it! Added it back! Let me know if it looks okay.

@hertschuh
Copy link
Contributor

Can you also run it and generate the ipynb and md files?

@SamanehSaadat
Copy link
Member Author

Can you also run it and generate the ipynb and md files?

Added ipynb and md files.

Copy link
Contributor

@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.

Great, thanks!

@SamanehSaadat
Copy link
Member Author

Great, thanks!

Thank you for the review!

@SamanehSaadat SamanehSaadat merged commit d3908eb into keras-team:master Aug 21, 2025
3 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants