Serialization utilities for Jax.
All Flax classes that carry state (e.g., Optimizer) can be turned into a state dict of numpy arrays for easy serialization.
- flax.serialization.from_state_dict(target, state, name='.')#
Restores the state of the given target using a state dict.
This function takes the current target as an argument. This lets us know the exact structure of the target, as well as lets us add assertions that shapes and dtypes don’t change.
In practice, none of the leaf values in target are actually used. Only the tree structure, shapes and dtypes.
target – the object of which the state should be restored.
state – a dictionary generated by to_state_dict with the desired new state for target.
name – name of branch taken, used to improve deserialization error messages.
A copy of the object with the restored state.
Returns a dictionary with the state of the given target.
- flax.serialization.register_serialization_state(ty, ty_to_state_dict, ty_from_state_dict, override=False)#
Register a type for serialization.
ty – the type to be registered
ty_to_state_dict – a function that takes an instance of ty and returns its state as a dictionary.
ty_from_state_dict – a function that takes an instance of ty and a state dict, and returns a copy of the instance with the restored state.
override – override a previously registered serialization handler (default: False).
Serialization with MessagePack#
- flax.serialization.msgpack_serialize(pytree, in_place=False)#
Save data structure to bytes in msgpack format.
Low-level function that only supports python trees with array leaves, for custom objects use to_bytes. It splits arrays above MAX_CHUNK_SIZE into multiple chunks.
pytree – python tree of dict, list, tuple with python primitives and array leaves.
in_place – boolean specifying if pytree should be modified in place.
msgpack-encoded bytes of pytree.
Restore data structure from bytes in msgpack format.
Low-level function that only supports python trees with array leaves, for custom objects use from_bytes.
encoded_pytree – msgpack-encoded bytes of python tree.
Python tree of dict, list, tuple with python primitive and array leaves.
Save optimizer or other object as msgpack-serialized state-dict.
target – template object with state-dict registrations to be serialized to msgpack format. Typically a flax model or optimizer.
Bytes of msgpack-encoded state-dict of target object.
- flax.serialization.from_bytes(target, encoded_bytes)#
Restore optimizer or other object from msgpack-serialized state-dict.
target – template object with state-dict registrations that matches the structure being deserialized from encoded_bytes.
encoded_bytes – msgpack serialized object structurally isomorphic to target. Typically a flax model or optimizer.
A new object structurally isomorphic to target containing the updated leaf data from saved data.