fn
save_safetensors
→Nonesave_safetensors(state_dict: dict[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_dictdict of str to TensorFlat mapping from parameter name to
Tensor. Nested
dicts or non-tensor values raise TypeError.pathstrDestination file path; use a
.safetensors suffix by
convention.metadatadict of str to str= NoneFree-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")