- Notifications
You must be signed in to change notification settings - Fork19.7k
Orbax Loading and Sharding Support feature#21903
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
Open
amitsrivastava78 wants to merge18 commits intokeras-team:masterChoose a base branch fromamitsrivastava78:orbax-pending-features
base:master
Could not load branches
Branch not found:{{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline, and old review comments may become outdated.
Uh oh!
There was an error while loading.Please reload this page.
Open
Changes from1 commit
Commits
Show all changes
18 commits Select commitHold shift + click to select a range
bc9060c Added Load method for orbax
amitsrivastava7843d45d0 Added Sharding Support
amitsrivastava784125ae0 Fix memory corruption in Model.load() by simplifying checkpoint loading
amitsrivastava7877689d9 Fix bare except clauses in orbax_checkpoint_test.py
amitsrivastava78ece275d Refactor duplicated tensor-to-numpy conversion code
amitsrivastava780464c3b Multi-host feature support
amitsrivastava78d8a86e8 Fixed CI failure
amitsrivastava7843fbecd Implement JAX-only multi-host checkpointing with proper Orbax APIs
amitsrivastava7882b345f Re-run CI
amitsrivastava789e35729 Re-run CI
amitsrivastava785a5c810 Fixed review comments
amitsrivastava78b503b79 Fixed review comments
amitsrivastava78e45c186 Fixed review comments
amitsrivastava78f027ba9 Refactor Orbax integration to use LazyModule consistently
amitsrivastava78802d785 Fix Orbax checkpoint preservation policy and asset functionality
amitsrivastava78f98e27c Fix Orbax checkpoint preservation policy and asset functionality
amitsrivastava785ff095a Integrate assets into Orbax pytree as checkpointables
amitsrivastava781f28e74 Implement asset loading support in model.load_weights for Orbax check…
amitsrivastava78File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Jump to
Jump to file
Failed to load files.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Diff view
Diff view
Fix Orbax checkpoint preservation policy and asset functionality
- Remove manual directory cleanup in _save_checkpoint that interfered with Orbax preservation policies- Simplify preservation policy setup to use LatestN directly instead of AnyPreservationPolicy wrapper- Update asset directory structure to use checkpoint_dir/assets/step/ format- Add comprehensive asset saving/loading tests for both sync and async modes- Make test_save_freq_epoch more robust by checking for numeric checkpoint names rather than specific epoch- Fix asset loading to handle new directory structure in saving_api.pyAll Orbax checkpoint tests now pass on both JAX and TensorFlow backends.
- Loading branch information
Uh oh!
There was an error while loading.Please reload this page.
commit802d7853e1e9604644c0a2d1c3e88654643c6897
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
43 changes: 38 additions & 5 deletionskeras/src/callbacks/orbax_checkpoint.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,3 +1,4 @@ | ||
| import os | ||
| import warnings | ||
| import numpy as np | ||
| @@ -8,6 +9,7 @@ | ||
| from keras.src.callbacks.monitor_callback import ( | ||
| MonitorCallback, # For metric monitoring logic | ||
| ) | ||
| from keras.src.saving.saving_lib import DiskIOStore | ||
| from keras.src.utils.module_utils import ocp | ||
| # Context and AsyncOptions are accessed through the lazy-loaded ocp module | ||
| @@ -158,14 +160,18 @@ def __init__( | ||
| ocp.training.preservation_policies.LatestN(max_to_keep) | ||
| ) | ||
| # Use AnyPreservationPolicy to combine them, or use directly | ||
| # if single policy | ||
| preservation_policy = None | ||
| if policies: | ||
| if len(policies) == 1: | ||
| preservation_policy = policies[0] | ||
| else: | ||
| preservation_policy = ( | ||
| ocp.training.preservation_policies.AnyPreservationPolicy( | ||
| policies | ||
| ) | ||
| ) | ||
| # Create the V1 Checkpointer with direct parameter passing | ||
| # Orbax will handle directory creation on all processes as needed | ||
| @@ -270,6 +276,33 @@ def _save_checkpoint(self, step, logs=None): | ||
| else: | ||
| self.checkpointer.save_pytree(step, composite_state) | ||
| # Save assets separately since PyTree can't handle binary data | ||
| if not self.save_weights_only: | ||
| self._save_assets(step) | ||
| def _save_assets(self, step): | ||
| """Save model assets to a separate directory.""" | ||
| from keras.src.saving.saving_lib import _save_state | ||
| assets_dir = os.path.join(self.directory, "assets", str(step)) | ||
amitsrivastava78 marked this conversation as resolved. OutdatedShow resolvedHide resolvedUh oh!There was an error while loading.Please reload this page. | ||
| try: | ||
| assets_store = DiskIOStore(assets_dir, mode="w") | ||
| except FileExistsError: | ||
| # Directory already exists, skip asset saving | ||
| return | ||
| try: | ||
| # Use the same recursive saving logic as _save_state | ||
| visited = set() | ||
| _save_state( | ||
| self.model, | ||
| None, # No weights store | ||
| assets_store, # Assets store | ||
| "", # Root path | ||
| visited, | ||
| ) | ||
| finally: | ||
| assets_store.close() | ||
| def on_train_batch_end(self, batch, logs=None): | ||
| if self._should_save_on_batch(batch): | ||
| # Handle save_best_only logic for batch-level saving | ||
Oops, something went wrong.
Uh oh!
There was an error while loading.Please reload this page.
Oops, something went wrong.
Uh oh!
There was an error while loading.Please reload this page.
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.