The safetensors format, explained
A practical reference for reading, editing and converting .safetensors files.
Safetensors is a file format for storing tensors, created by Hugging Face as a safe and fast replacement for PyTorch’s pickle-based .bin and .ckpt files. Loading a pickle can run arbitrary code; loading a safetensors file only parses JSON and copies bytes.
Byte layout
Every file has exactly three parts, in this order:
- Header length. The first 8 bytes are an unsigned 64-bit little-endian integer
N. - JSON header. The next
Nbytes are a JSON object. Writers pad it with spaces so the data section starts on an 8-byte boundary. Safetensors Studio shows this size as “JSON header” in the file bar. - Data. Everything after byte
8 + Nis tensor data. Each tensor’sdata_offsetsare relative to this point, so its absolute position in the file is8 + N + begin.
The header
{
"__metadata__": { "format": "pt" },
"model.embed_tokens.weight": {
"dtype": "BF16",
"shape": [128256, 4096],
"data_offsets": [0, 1050673152]
},
...
}
Each key is a tensor name. dtype is the element type, shape lists the dimensions in row-major (C) order and data_offsets is a half-open byte range [begin, end). The byte length must equal the product of the shape times the element size, and the ranges must cover the data section without gaps or overlaps.
The optional __metadata__ entry is a flat map of string keys to string values. Training tools store things like the base model, LoRA rank, trigger words or a JSON-encoded config there. Structured values must be serialized to strings.
Data types
| Code | Type | Bytes | Notes |
|---|---|---|---|
| F64 / F32 | IEEE float | 8 / 4 | Full precision |
| F16 | IEEE half | 2 | Range ±65504; can overflow on large activations |
| BF16 | bfloat16 | 2 | Same range as F32 with 8 bits of precision; the usual choice for LLM weights |
| F8_E4M3 | float8 e4m3fn | 1 | Max ±448, no infinity; used for FP8 inference weights |
| F8_E5M2 | float8 e5m2 | 1 | Wider range, less precision |
| I8–I64, U8 | Integers | 1–8 | Indices, quantized weights, counters |
| BOOL | Boolean | 1 | Masks |
Estimating memory from the file
A model’s weights take roughly parameters × bytes per element: a 7B model is about 14 GB in BF16, 7 GB in FP8 and close to 4 GB at 4-bit. Inference adds activations and, for language models, a KV cache that grows with context length: 2 × layers × kv_dim × tokens × 2 bytes. The Overview tab in the viewer works this out from the tensor shapes.
Converting other formats
PyTorch .bin, .pt, .pth, .ckpt
Drop the file into the viewer and choose Save .safetensors. The pickle is read by an interpreter that only understands tensor reconstruction, so nothing in the file is executed. In Python the equivalent is:
import torch
from safetensors.torch import save_file
state = torch.load("model.bin", map_location="cpu", weights_only=True)
state = {k: v.contiguous() for k, v in state.items()}
save_file(state, "model.safetensors", metadata={"format": "pt"})
Tensors that share memory (tied weights) must be cloned before save_file; the browser converter writes each tensor separately, so this happens automatically.
NumPy .npz and .npy
Each array in the archive becomes a tensor named after its entry. Big-endian and Fortran-ordered arrays are converted to little-endian, row-major order.
GGUF
F32, F16, BF16 and integer tensors are copied as they are. Q4_0, Q4_1, Q5_0, Q5_1, Q8_0, Q4_K, Q5_K and Q6_K blocks are dequantized to F16. Tensor names and layouts stay in llama.cpp form, so the result is mainly useful for inspection and analysis.
Hashes you will see
- SHA-256 of the whole file is what Hugging Face lists for each file.
- AutoV2 is the first 10 hex characters of that SHA-256, used by Civitai and AUTOMATIC1111.
- AutoV1 is a legacy hash of the 64 KB starting at offset 1 MB.
- Weights-only SHA-256 skips the header, so it stays the same when only metadata changes.
Common problems
- “Invalid header length”: the file is not safetensors (often a pickle renamed to .safetensors) or the download stopped early.
- Tensor points past the end of the file: a truncated download. Compare the file size with the source.
- Wrong model loads with missing keys: tensor names differ from what the loader expects. Rename with regex rules, for example strip
^model\.diffusion_model\..