Skip to content

fix(nnx): Fix Linen checkpoint restoration into NNX Gemma4 model - #4619

Draft
hsuan-lun-chiang wants to merge 7 commits into
mainfrom
fix/gemma4-nnx-linen-ckpt-mapping
Draft

fix(nnx): Fix Linen checkpoint restoration into NNX Gemma4 model#4619
hsuan-lun-chiang wants to merge 7 commits into
mainfrom
fix/gemma4-nnx-linen-ckpt-mapping

Conversation

@hsuan-lun-chiang

Copy link
Copy Markdown
Collaborator

Summary

This PR fixes parameter structure mapping and restore issues when restoring Linen checkpoints into NNX models for Gemma4.

Key Changes

  1. Unwrap flax.nnx.Variable / Param in nnx_extract_named_sharding: Prevent JAX JIT pytree structure errors during sharded model creation by returning raw NamedSharding leaves.
  2. Linen to NNX Layer Key Conversion (layers_N <-> layers["N"]): Map target NNX attribute keys decoder.layers_N into decoder.layers["N"] dictionary keys for Orbax target_for_restore in _lookup_stored_meta and _adjust_target_for_moe_fusion, and unroll restored keys back to layers_N before calling nnx.update(model, checkpoint).
  3. Handle int, string, and list layer lookups: Enable robust layer metadata resolution across int, str, and list structures.
  4. Remove manual _free_device_memory buffer deletion: Avoid deleting active Variable array references prior to restore to prevent RuntimeError: Array has been deleted during inference compilation.

Testing

  • Tested on TPU v6e cluster with gemma4-26b and vllm_decode. Weights restored in 9.94s and inference output rendered successfully.

Update layer path assertion in test_scan_layers_mismatch_tpu from
decoder/layers/0/ to decoder/layers_0/ to match the unscanned layer
naming structure introduced in PR #4504.
…ted array errors for non-restored parameters
@gemini-code-assist

Copy link
Copy Markdown

Caution

The consumer version of Gemini Code Assist on GitHub has been sunset. All code review activity has officially ceased.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant