.SAFETENSORS
Safetensors
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.
Software
- safetensors, the reference library for Python and Rust
- Hugging Face Transformers and other Hugging Face libraries
- PyTorch, through the safetensors library
- ComfyUI, for image generation models (Windows, macOS, Linux)
- Stable Diffusion web UI (Windows, macOS, Linux)
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.