Skip to content
fn

save_safetensors

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

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_dictMapping of str to Tensor
Flat mapping from parameter name to Tensor — a dict, or any read-only mapping such as a dict[str, Tensor] typed as one. 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.), read back with safe_open(path).metadata(). Keys and values must be strings; "__lucid_scalar_keys__" is reserved.

Notes

Every Lucid dtype is written as itself, bfloat16 included, so a checkpoint round-trips bit for bit and other SafeTensors readers see the dtype it was trained in. 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, "/tmp/weights.safetensors")

Used by 3