Unify the NNX and Linen restore paths in load_state_if_possible#4566
Open
ecnal-cienet wants to merge 1 commit into
Open
Unify the NNX and Linen restore paths in load_state_if_possible#4566ecnal-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. |
ecnal-cienet
force-pushed
the
feat/checkpointing-load-state-dedup
branch
3 times, most recently
from
July 22, 2026 20:21
1db2586 to
e533a50
Compare
Codecov Report❌ Patch coverage is
📢 Thoughts on this report? Let us know! |
xibinliu
reviewed
Jul 22, 2026
The NNX path had two early-return blocks that each duplicated the Linen restore flow, one for the standard manager and one for the emergency managers. Any change to restore logic had to be made in three places, and they had already drifted: the NNX grain branch dropped the iterator element that the Linen branch returns. pure_nnx saves in the Linen on-disk layout, so both paths restore the same tree. Convert the NNX abstract to that layout going in, run the existing match statement unchanged, and reshape what comes back on the way out. Linen behavior is unchanged. The weights-only branch's if/else folds into `_abstract_params`, which uses nnx.split_state rather than nnx.split -- the State-native API, without the unused GraphDef. That leaves the safetensors paths, which had the same duplication problem and three bugs behind it: The dynamic safetensors loader received the whole abstract state rather than the weights. For an NNX state that is fatal before any weight is read: the optimizer's opt_state is keyed by integer chain indices, and the loader flattens its target with a "." separator, so the load died in flatten_dict with `TypeError: sequence item 2: expected str instance, int found`. The loader now receives the weights, the way the weights-only Orbax load already does, and its result is unwrapped back into the NNX params state. A weight that no mapping covered came back as the unmaterialized leaf it went in as and reached the model as an untrained init value. Orbax does not flag this -- a shape conflict it rejects itself, but a weight the checkpoint simply does not carry it leaves as a placeholder. The weights-only Orbax load already makes that check; the dynamic load now makes it too, for both state types. `checkpoint_conversion_fn` arrives from config as a dotted string but was called directly, so it raised TypeError for any value that was set. It is now imported, and resolved before the checkpoint is read so a bad config fails immediately. Full-state safetensors also handles NNX now: the conversion fn produces MaxText's on-disk layout, which is what pure_nnx reads, so it goes through the same reshape as every other NNX restore.
ecnal-cienet
force-pushed
the
feat/checkpointing-load-state-dedup
branch
from
July 23, 2026 16:04
e533a50 to
82ef7bf
Compare
ecnal-cienet
marked this pull request as ready for review
July 23, 2026 17:29
|
Caution The consumer version of Gemini Code Assist on GitHub has been sunset. All code review activity has officially ceased. |
ecnal-cienet
requested review from
A9isha,
NuojCheng,
RissyRan,
SurbhiJainUSC,
abhinavclemson,
aireenmei,
bvandermoon,
dipannita08,
gagika,
gobbleturk,
hengtaoguo,
igorts-git,
jiangjy1982,
khatwanimohit,
parambole,
richjames0,
shralex,
shuningjin,
suexu1025 and
vipannalla
as code owners
July 23, 2026 17:29
xibinliu
approved these changes
Jul 23, 2026
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.
Description
The NNX path had two early-return blocks that each duplicated the Linen restore flow, one for the standard manager and one for the emergency managers. Any change to restore logic had to be made in three places, and they had already drifted: the NNX grain branch dropped the iterator element that the Linen branch returns.
pure_nnx saves in the Linen on-disk layout, so both paths restore the same tree. This converts the NNX abstract to that layout going in, runs the existing
matchstatement unchanged, and reshapes what comes back on the way out. Linen behavior is unchanged.Bugs fixed behind that duplication
opt_stateis keyed by integer chain indices, and the loader flattens its target with a"."separator, so it died inflatten_dictwithTypeError: sequence item 2: expected str instance, int found.checkpoint_conversion_fnwas unusable. It arrives from config as a dotted string but was called directly, raisingTypeErrorfor any value that was set. It is now imported, and resolved before the checkpoint is read.orbaxlayout.Testing
92 unit tests pass across the six suites that touch this code:
End-to-end on a v6e-8: 74 assertions across gpt3-52k / gemma2-2b / llama2-7b, 0 failures.
restoring from this run's directory step 9, resumed 10 → 19Restoring Linen-layout checkpoint into NNX state, continued from the Linen step 19 → 22load_parameters_path)load_full_state_path)Requested shape (2048, 16) is not compatible with the stored shape (1024, 16), zero training steps'decoder/layers/2/self_attention/qkv_proj/kernel': missing (model expects (16, 3, 2, 8) float32), zero training stepssafetensorswithout a conversion fn'NoneType' object is not callableSuccessfully mapped 13 parameters, trained 3 steps. Pre-fix, same command:TypeError, zero parameters mapped, zero stepsThe resume also matches Linen numerically. Two independently trained gemma2-2b runs, across the save and the restore:
Checklist
Before submitting this PR, please make sure (put X in square brackets):
gemini-reviewlabel.