Skip to content

Preserve typed PRNG keys when serializing Flax state - #5658

Open
emme1t wants to merge 1 commit into
google:mainfrom
emme1t:fix/serialize-typed-prng-keys-4130
Open

emme1t wants to merge 1 commit into
google:mainfrom
emme1t:fix/serialize-typed-prng-keys-4130

Conversation

@emme1t

@emme1t emme1t commented Nov 21, 2026

Copy link
Copy Markdown

What does this PR do?

Fixes #4130. A Flax dataclass containing jax.random.key() currently fails during MessagePack serialization when the typed key is converted to a NumPy array.

Store a typed key's registered PRNG implementation name and its underlying key data in the state dictionary. Restoring the saved implementation matters for rbg and unsafe_rbg, which have the same key shape and generate different streams. Existing uint32 checkpoints continue to load as arrays. A key created with an unregistered custom implementation gets an explicit error when saving.

The seven new CPU tests passed on JAX 0.12.1. They exercise scalar and batched keys, restoration through flax.struct.dataclass, a template with a different implementation, legacy arrays and unregistered implementations. The complete serialization module had 94 passing tests. The same four extended NumPy dtype cases failed on both revisions on Windows. Ruff and git diff --check passed.

Checklist

This branch has not been deployed

No deployments
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.

Flax dataclasses do not support new-style PRNGKeyArray in serialisation

1 participant