NNX migration prep (1/N): pure_nnx flag and init_state_fn scaffolding#3427
Open
ecnal-cienet wants to merge 1 commit intomainfrom
Open
NNX migration prep (1/N): pure_nnx flag and init_state_fn scaffolding#3427ecnal-cienet wants to merge 1 commit intomainfrom
ecnal-cienet wants to merge 1 commit intomainfrom
Conversation
b76f427 to
3fc5161
Compare
Codecov Report❌ Patch coverage is 📢 Thoughts on this report? Let us know! |
ed69422 to
cd34a4d
Compare
cd34a4d to
77ba3df
Compare
77ba3df to
ac4408a
Compare
- pure_nnx: a flag to to choose pure NNX logic when NNX and linen models co-exist. - init_state_fn: a function to initialize the model state for the training. It will be set to different function for NNX and Linen.
ac4408a to
97e2ab7
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
Description
This PR introduces two abstractions that enable incremental NNX migration while keeping existing Linen code fully functional.
pure_nnxconfig flagA boolean (
configs/base.yml,configs/types.py) that will route all major code paths — training, compilation, inference, RL, and utilities — to pure-NNX logic whenTrue, falling back to Linen otherwise. Defaults toFalseso all existing behaviour is unchanged.init_state_fnA pluggable callable for initializing the model training state, threaded through
create_checkpoint_manager,setup_training_state,setup_decode_state, andget_abstract_state. This decouples stateinitialization from shared infrastructure so future NNX and Linen paths can provide their own implementations without forking utilities.
Other structural changes
create_training_toolsis split intocreate_training_optimizerandcreate_checkpoint_managerfor cleaner separation of concerns.jit_train_stepgains ameshparameter to accommodate NNX callers wheremodelis aGraphDefwith no.meshattribute.get_shaped_inputsintrain_compile.pyadds apure_nnxbranch that omitsexample_rng, matching the future NNXtrain_step(state, batch)signature.get_first_steprestored to the two-argument(model, state)form to support both LinenTrainStateand NNXTrainStateNNXstep retrieval.grpo_trainer,maxengine,generate_param_only_checkpoint,layerwise_quantization,lora_utils,standalone_checkpointer, integration tests) updated to accept and passinit_state_fn.New files
src/maxtext/layers/train_state_nnx.py— NNXTrainStateNNXcontainer wrappingnnx.Module+nnx.Optimizer(mirrors LinenTrainState).src/maxtext/utils/maxtext_utils_nnx.py— NNX-specific utilities: abstract state, named sharding helpers, and sharded model creation.Lint / test fixes
maxtext_utils.py— remove unusedShardModeimport.maxtext_utils_test.py— fix duplicateAnyimport; restoreTransformeralias; updateget_abstract_statecall to new(config, mesh, init_state_fn)signature.sharding_compare_test.py— addpure_nnx=False/enable_nnx=False/pure_nnx_decoder=Falseto config params; updateget_abstract_stateandget_logical_annotationscalls to new API.state_dtypes_test.py— updateget_abstract_statecall to new API.Tests
Checklist
Before submitting this PR, please make sure (put X in square brackets):
gemini-reviewlabel.