Learn what a tensor is, how its numbers are laid out, and how a safetensors file stores them.
A tensor is a list of numbers with a shape. The shape says how to read the list as rows and columns.
A model like GPT-2 is a file of tensors, 160 of them. In this concept, you'll learn how a tensor's numbers are laid out, and how a safetensors file stores them.
A tensor is a list of numbers with a shape. The shape says how to read the list as rows and columns.
A model like GPT-2 is a file of tensors, 160 of them. In this concept, you'll learn how a tensor's numbers are laid out, and how a safetensors file stores them.
Here are 6 numbers:
1, 2, 3, 4, 5, 6
With shape [2, 3], they're 2 rows of 3:
[[1, 2, 3],
[4, 5, 6]]
With shape [3, 2], the same numbers are 3 rows of 2:
[[1, 2],
[3, 4],
[5, 6]]
A tensor holds as many numbers as its shape's sizes multiplied together. Shape [2, 3] holds 6.
A shape can have one size, like [768], which is a single list of 768 numbers. GPT-2's token table, wte.weight, has shape [50257, 768]: one row of 768 numbers for each of its 50,257 tokens. That's 38,597,376 numbers.
A tensor stores its numbers as one flat list, with its rows one after another. Nothing in the list marks where a row ends. The shape is what tells you.
So in a tensor with c numbers per row, row r starts at index r * c:
shape [2, 3] 1, 2, 3, 4, 5, 6
└─row 0─┘└─row 1─┘
row 1 starts at index 1 * 3 = 3
In wte.weight, the row for token 464, "The", starts at index 464 * 768 = 356,352.
A tensor's dtype, short for data type, says what kind of number it holds and how many bytes each one takes:
| dtype | Kind of number | Bytes each |
|---|---|---|
F32 |
32-bit float | 4 |
I32 |
32-bit integer | 4 |
GPT-2's tensors are all F32. So wte.weight takes 38,597,376 × 4 = 154,389,504 bytes, about 154 MB of GPT-2's 548 MB.
A safetensors file has three parts, one after another:
┌─────────┬──────────────────┬──────────────────────────────┐
│ 8 bytes │ header │ data │
│ the │ JSON, as long as │ every tensor's numbers, │
│ header's│ the first 8 │ back to back │
│ length │ bytes say │ │
└─────────┴──────────────────┴──────────────────────────────┘
The header says where each tensor's numbers are in the data.
Here's part of GPT-2's header:
{
"__metadata__": { "format": "pt" },
"ln_f.bias": { "dtype": "F32", "shape": [768], "data_offsets": [85362688, 85365760] },
"wte.weight": { "dtype": "F32", "shape": [50257, 768], "data_offsets": [287209472, 441598976] }
}
Each key is a tensor's name, except __metadata__, which holds notes about the file. data_offsets is the tensor's byte range in the data, counted from the end of the header. The end is not included.
ln_f.bias runs from byte 85,362,688 to 85,365,760. That's 3,072 bytes, or 768 numbers of 4 bytes each, which matches its shape.
Aside: The 8-byte length and every number are stored little-endian, with the lowest byte first.
GPT-2's header is 14,283 bytes long, which is 37cb in hexadecimal. So its file starts with these bytes:
cb 37 00 00 00 00 00 00
[2, 3] are 2 rows of 3.[50257, 768] holds 38,597,376.r of a tensor with c numbers per row starts at index r * c.dtype says what kind of number it holds. F32 and I32 take 4 bytes each.data_offsets are its byte range in the data.For every detail of the format, see the safetensors format.