[NNX] NNX migration prep (4/N): sharding tools, Linen↔NNX checkpoint utilities, and post-training fixes#3652
Draft
ecnal-cienet wants to merge 3 commits intomainfrom
Draft
[NNX] NNX migration prep (4/N): sharding tools, Linen↔NNX checkpoint utilities, and post-training fixes#3652ecnal-cienet wants to merge 3 commits intomainfrom
ecnal-cienet wants to merge 3 commits intomainfrom
Conversation
5430e95 to
86e20ea
Compare
Codecov Report❌ Patch coverage is 📢 Thoughts on this report? Let us know! |
a48bdd5 to
8239dd2
Compare
d8cde29 to
05aede5
Compare
2fbc8c2 to
d88224e
Compare
af8d71f to
6b6e61b
Compare
4 tasks
5f067bb to
7f06c99
Compare
The NNX-migrated code lives in src/maxtext/ (lower-case) but .coveragerc only listed the upper-case MaxText package in [run] source. coverage.py therefore never instrumented the new files, so added lines showed up on Codecov with no coverage data and suppressed patch coverage. The existing [paths] aliasing only merges already-collected data across filesystems; it does not control what is traced in the first place.
- Add TrainStateNNX (layers/train_state_nnx.py) with checkpoint and unit tests - Refactor model_creation_utils with create_nnx_abstract_model(); add NNX support to muon_utils - Add get_abstract_state_nnx() and get_nnx_named_sharding_with_scan_axis() to maxtext_utils.py - Wire NNX train state into train.py and train_utils.py with pure_nnx dispatch
…raining fixes Part 1 — sharding diagnostics and Linen<->NNX checkpoint utilities: - modify print_shardings_params to support NNX (maxtext_utils.py) - add --pure_nnx flag to run_sharding_dump.py - add bidirectional Linen<->NNX checkpoint conversion utility (linen_nnx_converter.py) - add checkpoint comparison utility for Linen vs NNX validation (compare_linen_nnx_checkpoint.py) Part 2 — post-training bug fixes: - models.py: unpack MultimodalInput before passing to NNXDecoder (was passing the whole object as multimodal_input= kwarg; NNXDecoder only accepts individual fields) - optimizers.py: guard adam_pax against scalar LR from optax.inject_hyperparams (callable() check before invoking learning_rate_fn) - train_distill.py: fix nested NNX transform issue (nnx.value_and_grad inside nnx.jit raises conflicting outer_index error); refactored to jax.value_and_grad + explicit nnx.split/merge pattern; teacher inference moved outside value_and_grad
7f06c99 to
28b1e4a
Compare
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.
NNX Migration Route Map
pure_nnxflag,init_state_fn,TrainStateNNX, NNX utils. Linen workflow unchanged. (PR #3427)get_abstract_state_nnx,get_named_sharding_nnx,set_named_sharding_nnx,get_partition_spec_nnx,get_mesh_from_config. (PR #3470)TrainStateNNX, model creation, gradient accumulation, checkpointing, and training loop dispatch. (PR #3500)Description
This PR combines two closely related changes — NNX tooling additions and post-training bugfixes — into a single reviewable unit.
Part 1: Sharding diagnostics and Linen↔NNX checkpoint utilities
src/maxtext/utils/maxtext_utils.pyprint_shardings_paramsto support NNX state trees in addition to Linen param dicts.src/maxtext/tools/run_sharding_dump.py--pure_nnxflag to select the NNX sharding path.src/maxtext/utils/linen_nnx_converter.py(new)src/maxtext/tools/compare_linen_nnx_checkpoint.py(new)Part 2: Post-training bug fixes
src/maxtext/models/models.pyTransformer.__call__was passingmultimodal_input=MultimodalInput(...)toNNXDecoder, which only accepts individual fields (image_embeddings,image_masks,audio_embeddings,audio_masks,bidirectional_mask). Fixed by unpacking the object at the call site.src/maxtext/optimizers/optimizers.pyadam_paxcalledlearning_rate_fn(count)unconditionally, failing whenoptax.inject_hyperparams(used by the distillation trainer) passes a pre-evaluated scalar instead of a callable schedule. Fixed with acallable()guard.src/maxtext/trainers/post_train/sft/train_sft.pyPeftTrainer._train_stepnestsnnx.value_and_gradinsidennx.jit, causing Flax NNX to assign conflictingouter_indexvalues and raisingValueError: The graph structure of a node added to cached_partial was mutated. Fixed by addingMaxTextPeftTrainersubclass that overridescreate_train_step_fnto usejax.value_and_gradwith an explicitnnx.split/nnx.mergepattern (matching MaxText's pre-training NNX train step).src/maxtext/trainers/post_train/distillation/train_distill.pyMaxTextDistillationTrainer._train_step. Additionally, teacher inference is now run outsidevalue_and_grad(it's frozen viastop_gradientanyway) to avoid tracing it unnecessarily. Fixed with the samejax.value_and_grad+ explicit split/merge pattern.src/maxtext/trainers/post_train/rl/train_rl.pywith_sharding_constraintasserts instead of reshards under Explicit mesh axes — patched with atry/except AssertionErrorfallback tojax.sharding.reshard. (2)tpu_inferenceinitializes weights asfloat32by default; during weight sync,tunix._apply_dtype_castwas upcasting incomingbfloat16MaxText weights tofloat32, causing a dtype mismatch in the ragged paged attention kernel — patched to skipbfloat16→float32upcasts so synced weights staybfloat16. Also passesdtypeexplicitly to vLLM init.src/maxtext/utils/model_creation_utils.pyNonecheckpoint metadata increate_nnx_modelwith a descriptive error message when the checkpoint directory is empty or the save did not complete.Tests
--pure_nnxflag.compare_linen_nnx_checkpoint.pyon gemma2-2b.Checklist
Before submitting this PR, please make sure (put X in square brackets):
gemini-reviewlabel.