Safetensors stores ML weights in a pickle-free binary layout with JSON metadata and mmap-friendly tensor blobs. Python and Rust libraries read and write the format.
Overview
Safetensors defines a file format and libraries for saving and loading numeric tensors without Python pickle, while keeping tensor data accessible with minimal extra copies. A small header records dtype, shape, and byte offsets; payload bytes follow in one buffer with little-endian layout.
The design emphasizes safety when loading untrusted files, lazy loading of individual tensors in large checkpoints, layout control for distributed training, support for large files, and native bfloat16 and fp8 dtypes. Header size is capped to reduce JSON parsing abuse, and offsets are validated so mapped memory stays within file bounds.
Documentation contrasts the format with pickle, MsgPack, Protobuf, NumPy npz, and other stacks on dimensions like zero-copy, lazy loading, and layout. PyTorch integrations can load via untyped storage from file; partial GPU loading is supported in multi-device setups.
Python packages install through pip or conda; Rust sources are available for native consumers. The format is adopted across Hugging Face ecosystems and numerous open ML tools for model weight distribution.
Key features
- Pickle-free tensor files
- JSON header with byte offsets
- Lazy per-tensor loading
- Little-endian tensor payload
- Header size safety limits
- Python and Rust implementations
- PyTorch save and load helpers
- bfloat16 and fp8 dtype support
Use cases
- Sharing Hugging Face model weights
- Avoiding pickle in model hubs
- Multi-GPU partial checkpoint load
- Cross-language tensor exchange
- Large checkpoint mmap on CPU