fn

save_safetensors

None
save_safetensors(state_dict: dict[str, object], path: str, metadata: dict[str, str] | None = None)
source

Save a flat state dict as a SafeTensors file.

SafeTensors is the recommended interchange format for sharing model weights: the file layout is a small JSON header followed by raw tensor bytes, so loading is fast, zero-copy where possible, and free of pickle's code-execution surface area. Use this whenever a checkpoint may be shared with untrusted parties.

Parameters

state_dictdict of str to Tensor
Flat mapping from parameter name to Tensor. Nested dicts or non-tensor values raise TypeError.
pathstr
Destination file path; use a .safetensors suffix by convention.
metadatadict of str to str= None
Free-form string metadata stored in the file header (model version, training framework, etc.).

Notes

Requires the optional safetensors Python package (pip install safetensors). The numpy backend used here does not accept bfloat16 — cast such tensors to float32 first. Zero-rank scalars are promoted to shape (1,) on write and squeezed back to () on load via a private metadata key.

Examples

>>> import lucid
>>> sd = {"w": lucid.randn(3, 3)}
>>> lucid.serialization.save_safetensors(sd, "weights.safetensors")