Skip to content

[NNX] Checkpoint: persist and restore rngs/dropout across resumes#4343

Open
ecnal-cienet wants to merge 1 commit into
mainfrom
feat/nnx-ckpt-rng-aux-persist
Open

[NNX] Checkpoint: persist and restore rngs/dropout across resumes#4343
ecnal-cienet wants to merge 1 commit into
mainfrom
feat/nnx-ckpt-rng-aux-persist

Conversation

@ecnal-cienet

@ecnal-cienet ecnal-cienet commented Jul 2, 2026

Copy link
Copy Markdown
Collaborator

Description

A pure_nnx run saves in the Linen on-disk layout, which has no place for NNX-only rngs/dropout state, so that state is stripped on save. On resume it was refilled from a base jax.random.key(0) — every preemption reset the dropout/RNG stream even though the dataset iterator was correctly restored, breaking mathematical continuity of the dropout masks across steps.

This persists the stripped rngs/dropout state as a separate nnx_aux composite item and restores it when present:

  • Save: extract the rngs/dropout subtree (extract_rng_state) before reshaping to the Linen layout, and write it as an nnx_aux item. The items payload stays byte-identical to today's Linen layout, so NNX↔Linen interchangeability is preserved.
  • Restore: read nnx_aux alongside items and merge it back; fall back to the deterministic default when it is absent (Linen-trained or pre-fix checkpoints).
  • create_orbax_checkpoint_manager registers the nnx_aux item handler — required, as Orbax rejects an unregistered composite item at save time (empty/unused for Linen runs). Emergency managers keep the default-fill path.

Tests

  • tests/unit/checkpointing_nnx_load_test.pyTestExtractRngState, TestDeepMerge, TestRngStateAuxPersistence (helper-level: extract keeps only rng/dropout; round-trip restores the saved counter not the base default; no-aux dir falls back; save adds/omits nnx_aux correctly). 14 passed.
  • tests/unit/checkpointing_nnx_roundtrip_test.py — end-to-end through the real create_orbax_checkpoint_manager → maybe_save_checkpoint → load_state_if_possible stack against an on-disk Orbax checkpoint: the dropout rng count survives a save/restore (and nnx_aux/ is written), and a checkpoint without nnx_aux falls back to the base default. 2 passed. (This e2e is what exposed the missing item-handler registration — the mocked unit tests passed without it.)

Checklist

Before submitting this PR, please make sure (put X in square brackets):

  • I have performed a self-review of my code. For an optional AI review, add the gemini-review label.
  • I have necessary comments in my code, particularly in hard-to-understand areas.
  • I have run end-to-end tests tests and provided workload links above if applicable.
  • I have made or will make corresponding changes to the doc if needed, including adding new documentation pages to the relevant Table of Contents (toctree directive) as explained in our documentation.

@codecov

codecov Bot commented Jul 2, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.

📢 Thoughts on this report? Let us know!

@ecnal-cienet ecnal-cienet force-pushed the feat/nnx-ckpt-rng-aux-persist branch 4 times, most recently from 8170ad6 to 1951802 Compare July 2, 2026 22:28
@ecnal-cienet ecnal-cienet reopened this Jul 5, 2026
@ecnal-cienet ecnal-cienet force-pushed the feat/nnx-ckpt-rng-aux-persist branch from 1951802 to 8217f75 Compare July 6, 2026 00:20
The NNX-only rngs/dropout state is stripped from the Linen on-disk layout, so on
resume it was refilled from a base key(0) -- every preemption reset the dropout
RNG stream even though the dataset iterator was restored. Persist that state as a
separate nnx_aux composite item (the items payload stays byte-identical to the
Linen layout, preserving interchangeability) and restore it when present, falling
back to deterministic defaults for Linen-trained or pre-fix checkpoints. Emergency
managers keep the default-fill path.
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