Containers · .pt

PyTorch torch.save checkpoint (.pt / .pth / .bin / .ckpt)

torch.save writes an uncompressed ZIP (PyTorch's own writer, "inline_container") with one top-level folder holding a pickle of the user's object (data.pkl) and one record per storage (data/<key>), each 64-byte aligned in the file. Tensors in the pickle are reduce-calls to torch._utils._rebuild_tensor_v2/v3 whose storage argument is a persistent id resolved to a data/ record. Because the container is pickle, loading can run arbitrary code unless restricted to an allowlist (weights_only=True, default since torch 2.6). HF "pytorch_model.bin" is this format; sharding uses pytorch_model.bin.index.json (same schema as the safetensors index).

.pt .pth .bin .ckpt

What it is
many tensors
Byte order
file declared
Dimension order
row major
Compression
none
Metadata
pickle
Loading
executes code The usual loader executes code unless that is turned off.

How to read it

Uncompressed ZIP. Record order: data.pkl, .format_version, .storage_alignment, byteorder, data/0..data/n, version, .data/serialization_id, then the central directory.

<archive>/data.pkl
pickle protocol 2. the saved object
<archive>/.format_version
ascii
<archive>/.storage_alignment
ascii
<archive>/byteorder
ascii. 'little' or 'big'
<archive>/data/<key>
bytes. raw storage bytes, key = persistent id key ('0','1',...)
<archive>/version
ascii. '3\n' for current writers
<archive>/.data/serialization_id
ascii. random id string

One tensor

name
path of the tensor in the unpickled object (state_dict key)
dtype
storage_type (v2) or explicit dtype (v3)
shape
size tuple
strides
stride tuple (elements)
data_offset
zip data offset of data/<key> + storage_offset * itemsize
nbytes
contiguous: prod(size)*itemsize; otherwise gather using strides

Example

The specs test vector. torch.save (torch 2.14.1+cu130) wrote an uncompressed ZIP. This viewer reads the archive records and does not unpickle data.pkl. The specs record that a weights_only round trip matched w_f32, b_f16, e_bf16, i_i32, and s_f32.

small.pt3,587 bytes

byteorder
little
.format_version
1
.storage_alignment
64
version
3
members
small/data.pkl 477 bytes small/.format_version 1 bytes small/.storage_alignment 2 bytes small/byteorder 6 bytes small/data/0 1024 bytes small/data/1 16 bytes small/data/2 12 bytes small/data/3 24 bytes small/data/4 4 bytes small/version 2 bytes small/.data/serialization_id 40 bytes
serialization_id
0055190185320359078712506378669613723131

Number formats

The name in the file, and the canonical dtype it maps to.

In the fileCanonicalBits
float32float3232
float64float6464
float16float1616
bfloat16bfloat1616
int64int6464
int32int3232
int16int1616
int8int88
uint8uint88
boolbool8
complex64complex6464
complex128complex128128
float8_e4m3fnfloat8_e4m3fn8
float8_e5m2float8_e5m28
float8_e4m3fnuzfloat8_e4m3fnuz8
float8_e5m2fnuzfloat8_e5m2fnuz8
float8_e8m0fnufloat8_e8m0fnu8
float4_e2m1fn_x2float4_e2m1fn4
uint16uint1616
uint32uint3232
uint64uint6464
quantizednone—

Sources

Nick Reynolds · @goldnode · nicks.net · rev 0.1