Fix/nnx unroll scan axis#4369
Conversation
b47961f to
f432f83
Compare
Codecov Report❌ Patch coverage is
📢 Thoughts on this report? Let us know! |
ff411f7 to
06f4b2a
Compare
There was a problem hiding this comment.
Do we need handle the scan axis here?
We should promote get_scan_axis from a nested local closure to a general module-level function, and reuse it here.
There was a problem hiding this comment.
params = nnx.split_state(state.model, nnx.Param)
num_params = max_utils.calculate_num_params_from_pytree(params)
There was a problem hiding this comment.
Done. Refactored the parameter counting in _read_train_checkpoint using nnx.split_state to isolate the nnx.Param subtree.
| `state.model.decoder[layer_name]` into per-index `layer_name_0..N` siblings | ||
| and removes the original collection. Mirrors the same operation on | ||
| `state_mesh_shardings` so downstream sharding stays correct. | ||
| """ |
There was a problem hiding this comment.
here, we should use the param_only_state:
param_only_state = nnx.split_state(state, nnx.Param)
with this, you will be able to 1. save the trainable params only 2. avoid handling of other variables in the model.
There was a problem hiding this comment.
Done. Implemented parameter-only state splitting and unrolling.
bd07a31 to
2702fd9
Compare
2702fd9 to
c6128dd
Compare
Description
This PR fixes a critical crash in the
generate_param_only_checkpoint.pyutility when checkpoint unrolling is executed with Flax NNX enabled (enable_nnx=true).The Problem / Root Cause
In Flax NNX, the model state variables are partitioned differently:
nnx.Param) are scanned overconfig.param_scan_axis(typicallyaxis 1).axis 0(as defined innnx_decoders.py).Prior to this fix, the unrolling utility’s slicing helper
slice_ithblindly appliedjnp.take(x, i, axis=config.param_scan_axis)to every node in the PyTree. For rank-1 non-parameter variables, attempting to take alongaxis 1caused theValueError: axis 1 is out of bounds for array of dimension 1crash.The Solution
We updated
slice_ithto dynamically resolve the scan axis for each array in the PyTree:axis 0.config.param_scan_axis(normallyaxis 1).This ensures robust and correct dynamic slicing across both parameters and non-parameters during Flax NNX checkpoint unrolling.
Tests
We reproduced the failure on the
mainbranch and verified the fix across both CPU and TPU platforms (using a physicalv5p-8TPU slice).1. Reproduction on
mainRunning the unrolling command on the
mainbranch withenable_nnx=truereproduced the exact crash from the Airflow DAG logs:logs
2. Verification with Fix Branch
Running the identical unrolling command on this branch successfully unrolled the checkpoint and completed with exit code 0:
logs
Checklist
gemini-reviewlabel.