Introduction
A .pt or .pth file is whatever
PyTorch wrote with its torch.save
function. Most often it is a model’s state_dict, a dictionary from layer
names to weight tensors, but it can be any Python object that contains tensors: a whole
model, an optimiser’s state or a training checkpoint. The
two extensions mean the same thing. Hugging Face models from before safetensors are
the same format named pytorch_model.bin.
Loading one uses Python’s pickle module, which can run code hidden in the file, so a
model from an untrusted source is a risk. Since version 2.6, torch.load
loads only tensors and plain data unless told otherwise. Shared models have largely
moved to safetensors, which cannot run code.
Python itself also uses .pth for unrelated path configuration files in
its site-packages folder.
Structure
Since PyTorch 1.6 the file is a ZIP archive whose entries are stored without compression, so tensors can be read in place. Everything sits in one top folder:
data.pkl: the saved object, pickled, with each tensor replaced by a reference to a storage recorddata/0,data/1and so on: the raw bytes of each storage, one entry per storageversion: the version of the serialisation formatbyteorderin newer files:littleorbig
Older files, from before 1.6, are a series of pickles one after another followed by
the storage bytes, with no ZIP wrapper. TorchScript models saved with
torch.jit.save are ZIP archives too, with the model’s code in a
code/ folder beside the data.
Also used for
- GGML model file .bin
Links
Software
- PyTorch (Windows, macOS, Linux)
- Hugging Face Transformers, which loads
pytorch_model.binfiles - Netron, a viewer that lists the tensors (Windows, macOS, Linux, web)
Developers
- PyTorch, Python, with LibTorch for C++
- safetensors, for converting weights to the safetensors format
Reference
- Serialization semantics, PyTorch documentation