[NNX] Clean up transient intermediates at setup#4274
Open
xibinliu wants to merge 1 commit into
Open
Conversation
Codecov Report❌ Patch coverage is 📢 Thoughts on this report? Let us know! |
…ing as nnx.Intermediate
This change cleans up transient variables sowed during model creation/tracing from the persistent sharding state and checkpoint layout.
1. Clean Quantization Setup:
- Updated maybe_quantize_model in quantizations.py to pop sowed nnx.Intermediate variables in-place immediately after Qwix tracing.
- This cleans the initial state returned by the model factory fn, allowing us to revert the setup-time nnx.Not(nnx.Intermediate) filters in maxtext_utils.py and train_compile.py.
- Optimized create_nnx_abstract_model in model_creation_utils.py to reuse abs_model.mesh and avoid calling eval_shape twice.
2. Metrics Sowing Uniformity:
- Changed all self.sow("intermediates", ...) calls to self.sow(nnx.Intermediate, ...) in all pure NNX models (gemma.py, llama2.py, etc.).
- Sowing string-based "intermediates" was dynamically creating custom Variable types that did not inherit from nnx.Intermediate, leading to parameter/checkpoint bloat.
- train_step in train.py now strips all intermediates via nnx.Not(nnx.Intermediate) before returning.
3. Testing:
- Added MaybeQuantizeModelTest in quantizations_test.py to assert that sowed intermediates are popped from the model state and that abstract model state contains no intermediates.
1585ea1 to
99ffcf6
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
nnx.Intermediate in meshed sharding caused two errors:
llama3_1_70b_131072_fp8_4x4x4:
llama3_1_70b_131072_fp8_4x8x8:
Changes:
This change cleans up transient variables sowed during model creation/tracing from the persistent sharding state and checkpoint layout.
Clean Quantization Setup:
Metrics Sowing Uniformity:
Testing:
Tests
Validated that the NNX training step compiles successfully without structure or sharding layout mismatches::
Command: python3 src/maxtext/trainers/pre_train/train_compile.py
Configuration: model_name=deepseek3-tiny + attention=dot_product + pure_nnx=true + use_qwix_quantization=true + use_qk_clip=true
Checklist
Before submitting this PR, please make sure (put X in square brackets):
gemini-reviewlabel.