Introduction
Safetensors is a simple format for storing the weights of a neural network, made by Hugging Face in 2022. Earlier PyTorch checkpoints used Python’s pickle format, which can run code when a file is loaded. A safetensors file holds only a description of each tensor and its raw numbers, so loading one is safe, and the data can be mapped straight into memory. It is now the usual format for models shared on Hugging Face, including image generation checkpoints.
The same format also stores fine-tuning adapters, which are much smaller than a model. A LoRA adapter is a safetensors file that holds only the extra low-rank matrices to add to a base model’s layers. Large models are often split into several numbered safetensors files, with a small JSON index that says which file holds each tensor.
Structure
0Header size an unsigned 64-bit little-endian number, N8 bytes8Header a UTF-8 JSON objectN bytes8 + NData the tensors’ bytes, one after anotherthe rest
The header maps each tensor’s name to an object with its dtype (such as
F32, F16, BF16 or I8), its
shape, and data_offsets: the start and end of its bytes,
counted from the start of the data. An optional __metadata__ entry holds
free text pairs, such as the format of the original checkpoint. The header may be padded
with spaces at the end.
Also used for
- LoRA adapter – Small low-rank A and B matrices that adjust a base model’s layers.
Links
Software
- Hugging Face Transformers – loads and saves model weights as safetensors, like Hugging Face’s other libraries (Python)
- PyTorch – machine learning framework that reads and writes safetensors through the safetensors library (Windows, macOS, Linux)
- ComfyUI – node-based image generation app that loads safetensors checkpoints (Windows, macOS, Linux)
- Stable Diffusion web UI – browser interface for image generation models (Windows, macOS, Linux)
Developers
- safetensors – the reference library, with bindings for PyTorch, TensorFlow, JAX and NumPy (Rust, Python)
Reference
- Safetensors documentation – Hugging Face’s description of the format and library