fn

load_safetensors

dict of str to Tensor
load_safetensors(path: str, device: str = 'cpu')
source

Load a SafeTensors file into a flat state dict.

Reads the entire file lazily through the safetensors Python package, converts each tensor to a Lucid Tensor, optionally relocates to the requested device, and restores any zero-rank scalars that were promoted to shape (1,) during save_safetensors.

Parameters

pathstr
Path to a .safetensors file.
devicestr= 'cpu'
Target device for every loaded tensor: "cpu" (default) or "metal".

Returns

dict of str to Tensor

Flat state dict suitable for model.load_state_dict().

Notes

Requires the optional safetensors Python package (pip install safetensors). The header metadata is consulted to recover the original 0-d shape of scalar entries written through save_safetensors.

Examples

>>> import lucid
>>> sd = lucid.serialization.load_safetensors("weights.safetensors")