This page follows the format description in the
README of
Hugging Face’s safetensors repository and the data types in its
safetensors/src/tensor.rs, both read 2026-11-12.
Layout
| Offset | Size | Field |
|---|---|---|
0 | 8 | N: size of the header, unsigned 64-bit little-endian. |
8 | N | The header: a UTF-8 JSON object that must start with {. It may be padded at the end with spaces (20). |
| 8 + N | rest | The byte buffer holding every tensor. |
Header
Each key is a tensor name; its value is an object:
| Key | Value |
|---|---|
dtype | A data type string. |
shape | Array of dimension sizes; [] is a single value, and a 0 in the shape is an empty tensor, which takes no bytes in the buffer. |
data_offsets | [begin, end]: byte positions within the byte buffer, end exclusive. |
The special key __metadata__ may hold a free-form object whose keys
and values are all strings; other JSON values are not allowed there. An example
(the metadata content is made up):
{"__metadata__":{"note":"example"},
"weight":{"dtype":"F16","shape":[2,3],"data_offsets":[0,12]}}
Data types
| dtype | Type | Bytes |
|---|---|---|
BOOL | Boolean | 1 |
U8, I8 | 8-bit integers | 1 |
F4 | 4-bit float | ½ |
F6_E2M3, F6_E3M2 | 6-bit floats | ¾ |
F8_E5M2, F8_E4M3, F8_E8M0, F8_E4M3FNUZ, F8_E5M2FNUZ | 8-bit floats | 1 |
U16, I16 | 16-bit integers | 2 |
F16 | IEEE half precision | 2 |
BF16 | bfloat16 | 2 |
U32, I32 | 32-bit integers | 4 |
F32 | IEEE single precision | 4 |
U64, I64 | 64-bit integers | 8 |
F64 | IEEE double precision | 8 |
C64 | 64-bit complex | 8 |
The library counts sizes in bits, so the sub-byte types pack several values per byte; the README notes they make alignment tricky. Values are little-endian, in row-major (C) order. NaN and infinities are not checked for.
Sub-byte types
Sources read on 2026-11-14: tensor.rs and the README of the safetensors repository, the
PyTorch source and documentation, the ml_dtypes README, and the OCP Microscaling Formats specification 1.0.
- Sizes.
tensor.rsgivesF44 bits and bothF6types 6 bits. The number of bits (elements times bits) must be a whole number of bytes, or the library reports a misaligned slice. So anF4tensor needs an even number of values and anF6tensor a multiple of four (4 × 6 = 24 bits = 3 bytes). - Encodings.
F4is E2M1 (sign, 2 exponent bits, 1 mantissa bit);F6_E2M3andF6_E3M2are the OCP MXFP6 elements (the doc comments intensor.rslink to the MX specification). F4order. Neither safetensors file says which half of a byte comes first. PyTorch, whosetorch.float4_e2m1fn_x2type is “two 4-bit values packed into a byte”, defines it inFloat4_e2m1fn_x2.h: the first element is in bits 3 to 0 (low nibble) and the second in bits 7 to 4. The viewer uses this order.F6order. safetensors does not define it:tensor.rsonly gives the size (bitsize()6) andslice.rsonly computes byte offsets asstart * span / 8and rejects slices that do not start and end on a byte boundary (MisalignedSlice); the Python bindingtorch.pyhas no float6 entry at all, and the repository has no PR or issue mentioningF6_E2M3. The MX specification says the memory layout “is not prescribed”, and ml_dtypes stores one value per byte. Code that actually packs six-bit values does agree on one order, found in three places (read 2026-11-14):- vLLM compressed-tensors (Apache-2.0),
src/compressed_tensors/compressors/mxfp6/helpers.pylines 35 to 40,pack_fp6_to_uint8:word = c0 | c1 << 6 | c2 << 12 | c3 << 18, then the bytesword,word >> 8,word >> 16(each masked with 0xFF); unpacking, line 49 to 51, isword = b0 | b1 << 8 | b2 << 16andcode_i = word >> (6 × i) & 0x3F. Its pull request (vllm-project/compressed-tensors #941) says the layout is the same as the AMD Quark / OCP export. These areuint8tensors, notF6_*dtypes. - PyTorch pull requests #202841 and #202851 (
float6_e2m3fn_x4, a proposal): three bytes form a little-endian 24-bit word and value i is at bits [6i, 6i + 6). - refkv (d-v-b/refkv,
spec/zarrify/safetensors.md§3.2), which readsF6_E2M3andF6_E3M2from safetensors: element i occupies bits [6i, 6i + 6) of the data and bit j is bit j mod 8 of byte j div 8. It states that no writer of F6 safetensors files was found and that this order is the library’s F4 order extended, so it is not checked against a real file. - torchao
custom_fp_utils.pykeeps one value per byte (bits 2 to 7), so it is no packing; its tensor-core fp6 layout is a different, kernel-specific permutation that was not read.
c0 | (c1 & 3) << 6,c1 >> 2 | (c2 & 15) << 4,c2 >> 4 | c3 << 2(each masked to 8 bits). Worked example: the codes 1, 2, 3, 4 (000001 000010 000011 000100) give the word 1 + 128 + 12288 + 1048576 = 0x103081, so the bytes are81 30 10; asF6_E2M3they read 0.125, 0.25, 0.375, 0.5. The viewer uses this order (value i at bits [6i, 6i + 6), least significant bit first). It is the only order found, and the three that write or read it (vLLM compressed-tensors, the PyTorch proposal, refkv) agree with none disagreeing; the safetensors library itself only requires byte alignment. No file written as safetensorsF6by a real producer exists yet, so the order is labelled as the common convention, not as defined by safetensors.- vLLM compressed-tensors (Apache-2.0),
Validity rules
- The library limits the header to 100 MB, so a huge JSON cannot be used for a denial-of-service attack.
- No duplicate keys (the README notes not all parsers check this).
- The byte buffer must be entirely indexed, with no holes (which prevents polyglot files), and the tensors’ ranges must not overlap.
- The library rejects a tensor whose range length differs from its element count times the dtype’s size.
The README describes the format as a way to store tensors safely, as opposed to pickle.