gensbi.utils.serialization#
Portable safetensors save/load for flax nnx models.
Exports the weights of any flax.nnx.Module to a single,
framework-neutral .safetensors file, and loads them back into a model the
caller has already reconstructed from its Params. The file stores a flat
{str: array} table (nnx state paths joined with ".") plus a small
provenance metadata blob; it does not carry enough information to rebuild
the model architecture (an explicit non-goal).
Attributes#
Functions#
|
Flatten model state to |
|
Join an nnx state-path tuple into a safetensors string key. |
|
Load weights from a |
|
Save |
Module Contents#
- gensbi.utils.serialization._flat_arrays(model, wrt)[source]#
Flatten model state to
{tuple_path: array}(ints preserved).- Return type:
dict[tuple, Any]
- gensbi.utils.serialization._join_key(path)[source]#
Join an nnx state-path tuple into a safetensors string key.
Integer
nnx.Listindices are stringified; a non-integer component that contains the"."separator is unrepresentable and raisesValueError.- Parameters:
path (tuple)
- Return type:
str
- gensbi.utils.serialization.load_safetensors(model, path, *, strict=True)[source]#
Load weights from a
.safetensorsfile intomodelin place.The caller must have reconstructed
modelfrom itsParamsfirst; that model is the structural schema.- Parameters:
model (nnx.Module) – Target model, rebuilt from its
Params.path (str | os.PathLike) – Source
.safetensorsfile.strict (bool, optional) – If True (default), the file’s key set must equal the model’s and every shared key must match shape (
ValueErrorotherwise). If False, only the intersection is loaded; model leaves absent from the file keep their current values and file keys absent from the model are ignored.
- Returns:
model – The same
modelobject, updated in place with the loaded weights.- Return type:
nnx.Module
- gensbi.utils.serialization.save_safetensors(model, path, *, metadata=None, wrt=None)[source]#
Save
model’s weights to a single.safetensorsfile.- Parameters:
model (nnx.Module) – Any flax nnx module.
path (str | os.PathLike) – Destination
.safetensorsfile.metadata (mapping, optional) – Extra provenance, stringified and merged over (overriding) the defaults
format/version/framework/model_class.wrt (nnx filter, optional) – Restrict the saved variable collections (e.g.
nnx.Param). Default saves the full state.
- Return type:
None