Write post-training checkpoints in MaxText's on-disk layout - #4700
Draft
ecnal-cienet wants to merge 1 commit into
Draft
Write post-training checkpoints in MaxText's on-disk layout#4700ecnal-cienet wants to merge 1 commit into
ecnal-cienet wants to merge 1 commit into
Conversation
|
Caution The consumer version of Gemini Code Assist on GitHub has been sunset. All code review activity has officially ceased. |
Post-training saved through Tunix's checkpoint manager, which stores
nnx.state(model) verbatim under a model_params item. MaxText's on-disk
layout is the Linen one: weights in params/params, the optimizer in
opt_state and step, and NNX-only state such as rngs in nnx_aux. The two
never matched, so a checkpoint from an SFT, DPO, RL or distillation run
could not be loaded by pre-training, and loaders had started growing
branches to read the post-training shape instead.
Add MaxTextLayoutCheckpointManager, which converts in both directions and
writes the same items tree everything else in MaxText reads. It lives
under trainers/post_train rather than common/checkpointing because it
subclasses Tunix's manager, and common/checkpointing is imported by
pre-training and inference, which run without Tunix installed. Older
checkpoints are still in the Tunix layout, so maybe_restore falls back to
the base class for those, and load_params_from_path learns to read them
by restoring into the NNX state itself.
DPO and RL train through TunixMaxTextAdapter, whose base level would
otherwise reach the checkpoint. Strip it from the weights and from the
optimizer accumulators that mirror them, and put it back on restore.
Two optimizer differences also blocked a full-state resume:
- train_dpo passed gradient_accumulation_steps unconditionally, so
Tunix wrapped the optimizer in optax.MultiSteps even at 1. Pass None
below 2, as train_sft already did.
- post-training chained optax.clip_by_global_norm into the optimizer,
nesting its state a level deeper than pre-training, which clips raw
gradients in its train step. add_gradient_clipping applies the same
math inside the update and keeps the optimizer's own state tree.
A DPO checkpoint now resumes into pre-training with its weights, its
optimizer state and its step counter. RL and distillation additionally
wrap in optax.inject_hyperparams so Tunix can log the learning rate,
which keeps their full-state resume out of reach; their weights load.
ecnal-cienet
force-pushed
the
feat/post-train-checkpoint-maxtext-layout
branch
from
August 3, 2026 02:39
7890810 to
35dd42f
Compare
Codecov Report❌ Patch coverage is 📢 Thoughts on this report? Let us know! |
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Post-training saved through Tunix's checkpoint manager, which stores nnx.state(model) verbatim under a model_params item. MaxText's on-disk layout is the Linen one: weights in params/params, the optimizer in opt_state and step, and NNX-only state such as rngs in nnx_aux. The two never matched, so a checkpoint from an SFT, DPO, RL or distillation run could not be loaded by pre-training, and loaders had started growing branches to read the post-training shape instead.
Add MaxTextLayoutCheckpointManager, which converts in both directions and writes the same items tree everything else in MaxText reads. It lives under trainers/post_train rather than common/checkpointing because it subclasses Tunix's manager, and common/checkpointing is imported by pre-training and inference, which run without Tunix installed. Older checkpoints are still in the Tunix layout, so maybe_restore falls back to the base class for those, and load_params_from_path learns to read them by restoring into the NNX state itself.
DPO and RL train through TunixMaxTextAdapter, whose base level would otherwise reach the checkpoint. Strip it from the weights and from the optimizer accumulators that mirror them, and put it back on restore.
Two optimizer differences also blocked a full-state resume:
A DPO checkpoint now resumes into pre-training with its weights, its optimizer state and its step counter. RL and distillation additionally wrap in optax.inject_hyperparams so Tunix can log the learning rate, which keeps their full-state resume out of reach; their weights load.
Description
Start with a short description of what the PR does and how this is a change from
the past.
The rest of the description includes relevant details and context, examples:
If the change fixes a bug or a Github issue, please include a link, e.g.,:
FIXES: b/123456
FIXES: #123456
You can also provide a comma-separated list. If you don't want to close a bug but
simply to reference it, use BUGS, e.g.:
BUGS: b/123456
Notice 1: Once all tests pass, the "pull ready" label will automatically be assigned.
This label is used for administrative purposes. Please do not add it manually.
Notice 2: For external contributions, our settings currently require an approval from a MaxText maintainer to trigger CI tests.
Tests
Please describe how you tested this change, and include any instructions and/or
commands to reproduce.
Checklist
Before submitting this PR, please make sure (put X in square brackets):
gemini-reviewlabel.