feat(nnx): support native Flax NNX PEFT/LoRA training loop#4501
Open
RexBearIU wants to merge 1 commit into
Open
feat(nnx): support native Flax NNX PEFT/LoRA training loop#4501RexBearIU wants to merge 1 commit into
RexBearIU wants to merge 1 commit into
Conversation
RexBearIU
requested review from
A9isha,
NuojCheng,
RissyRan,
SurbhiJainUSC,
abhinavclemson,
aireenmei,
bvandermoon,
darisoy,
dipannita08,
gagika,
gobbleturk,
hengtaoguo,
huytransformer,
igorts-git,
jiangjy1982,
khatwanimohit,
parambole,
richjames0,
shralex,
shuningjin,
suexu1025,
vipannalla and
xibinliu
as code owners
July 16, 2026 01:29
Codecov Report❌ Patch coverage is 📢 Thoughts on this report? Let us know! |
RexBearIU
force-pushed
the
nnx-lora-support
branch
from
July 16, 2026 07:44
7ee8331 to
d54e2a3
Compare
RexBearIU
force-pushed
the
nnx-lora-support
branch
from
July 17, 2026 09:45
d54e2a3 to
108f993
Compare
SurbhiJainUSC
approved these changes
Jul 17, 2026
RexBearIU
force-pushed
the
nnx-lora-support
branch
2 times, most recently
from
July 22, 2026 15:41
282a533 to
558f0f2
Compare
RexBearIU
force-pushed
the
nnx-lora-support
branch
28 times, most recently
from
July 23, 2026 18:19
b4cabc6 to
1886031
Compare
- Resolve pjit sharding metadata mismatches by re-evaluating state_mesh_shardings after parameter restoration and LoRA/QLoRA module injection. - Update l2norm_pytree to skip non-inexact leaves (e.g. packed void arrays or WithAux wrappers). - Implement normalized key translation (0 <-> layers_0) in checkpointing and model creation utilities for seamless parameter merging across NNX and Linen models. - Add mesh axis shape sanitization for non-divisible dimensions. - Include unit tests for l2norm_pytree void quantization, key translation, and upfront LoRA state restoration.
RexBearIU
force-pushed
the
nnx-lora-support
branch
from
July 23, 2026 18:21
1886031 to
e310c2a
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.
Description
This PR implements full native parameter-efficient fine-tuning (PEFT), LoRA, and QLoRA training support and complete checkpoint lifecycle resume/restoration within Flax NNX training in MaxText (
train.py,train_sft.py,maxtext_utils.py,checkpointing.py,train_state_nnx.py,lora_utils.py).Key Implementation Highlights:
Dynamic Parameter Filter Target:
wrtparameter filtering is dynamically selected based on configuration (nnx.LoRAParamifconfig.lora.enable_lorais True, otherwisennx.Param), ensuring optimizer states and parameter updates affect only LoRA adapters when LoRA is enabled.Checkpoint Resume & Clean Utility Integration:
_norm_path_key,quantize_params_safe,_unwrap_restored_params,_update_nnx_state_from_pure_dict) directly intosrc/maxtext/common/checkpointing.pywhile strictly preserving baseline pre-training and native SFT checkpointing code paths.pjitsharding mismatches during checkpoint resume by re-evaluatingstate_mesh_shardingsafter parameter restoration and LoRA/QLoRA module injection insetup_initial_state.split_for_checkpointintrain_state_nnx.pyto route bothnnx.Paramandnnx.LoRAParaminto theparamscollection, ensuring Orbax saves and restores complete PyTree metadata for LoRA adapter runs.0<->"layers_0") across structural variations in layer naming.Dynamic Quantization & PyTree Sanitization:
quantize_params_safe) incheckpointing.pyso unquantized base checkpoints are dynamically quantized when loading into QLoRA models.l2norm_pytreeinmax_utils.pyto safely skip non-inexact leaves (e.g., packedvoidorQArrayquantized arrays andWithAuxwrappers) by checkingjnp.issubdtype(leaf.dtype, jnp.inexact).sharding.py(get_leaf_pspec) for non-divisible dimensions (e.g. Gemma 4 / Qwen 3 head dimensions vs TPU mesh shapes).Multi-Architecture Support:
gemma4-26b(MoE) andqwen3-4bfor bothscan_layers=Trueandscan_layers=Falsemodes across Base Training, Resume Training, and Standalone Restore.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
1. Comprehensive Unit Test Suite
Ran full test suite (
checkpointing_test.py,lora_utils_test.py,model_creation_utils_test.py) with 100% passing results (103/103 tests passed):2. Pre-Commit Verification
Ran pre-commit hooks across all changed files with 100% passing results:
git diff --name-only --diff-filter=d origin/main...HEAD | xargs pre-commit run --files3. Multi-Device TPU E2E Verification (
gemma4-26b&qwen3-4b)Verified full 4-step E2E checkpoint lifecycle for
gemma4-26bandqwen3-4bacross bothscan_layers=Falseandscan_layers=True:gemma4-26bpre_train/train.pyFalsegemma4-26bpre_train/train.pyTrueChecklist
Before submitting this PR, please make sure (put X in square brackets):
gemini-reviewlabel.