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. The header and data
types page gives every dtype and the validity rules.
In the explorer
Drop a .safetensors file on this site. Only the 8-byte size and the JSON
header are read when it opens, so a file of several gigabytes opens at once. The structure
tree shows the size, the header with each tensor’s entry (dtype, shape, data offsets), the
metadata and any space padding, and the byte buffer with each tensor’s bytes; bytes that no
tensor covers are labelled. The Tensors table lists every tensor with its dtype, shape,
number of values and byte range, and the Data types table counts them. The options bar
picks a tensor, and the Values table lists its numbers (every one, a page at a time, read
from the file when shown) with their position in the tensor, for every dtype on the
header page, the 8-bit and smaller
floats included. The validity rules are each checked: duplicate keys, ranges that do not
match the shape and type, holes and overlaps in the byte buffer, a bad dtype or shape, metadata
that is not text, a header that does not start with { or is larger than 100 MB, an
F4 tensor with an odd number of values and an F6 tensor whose number of values is not a
multiple of four (the library rejects both as MisalignedSlice). The Metadata table shows every value
in full.
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