Sub topic 7
Inside a PyTorch checkpoint
Prerequisites and the checkpoint model
Assume you know basic Python, classes, tensors, and PyTorch. You should also know that a model's weights are numerical tensor values, while the model code defines how those values are used.
A PyTorch checkpoint is a file containing data needed to restore training or inference. It may contain:
- A model's
state_dict, which is a dictionary mapping parameter names to tensors. - An optimizer state, such as momentum values.
- The current training step or epoch.
- A loss value, configuration, or other metadata.
- Sometimes the complete model object.
The filename extension, such as .pt or .pth, does not identify a secure format. It only describes a common naming convention.
The internal layout
Modern PyTorch uses a ZIP-based container for its default serialization format. The container usually includes entries similar to these:
| Entry | Purpose |
|---|---|
| `data.pkl` | Pickled object structure and references to tensor storage |
| `data/0`, `data/1` | Raw bytes for tensor storages |
| `byteorder` | Byte-order information |
| `version` | Serialization format version |
The important boundary is between the pickle metadata and the tensor storage. The tensor values are stored as data, but data.pkl can describe Python objects. During loading, PyTorch may unpickle that structure and reconstruct objects from it.
Saving a state dictionary
A state dictionary is usually preferable to saving a complete model object. It keeps the checkpoint focused on tensor values and requires you to provide the model class separately when loading.
import zipfile
import torch
from torch import nn
model = nn.Linear(3, 2)
path = "linear_state.pth"
torch.save(model.state_dict(), path)
with zipfile.ZipFile(path) as archive:
print("\n".join(archive.namelist()))
loaded_state = torch.load(
path,
map_location="cpu",
weights_only=True,
)
restored_model = nn.Linear(3, 2)
restored_model.load_state_dict(loaded_state)
print(restored_model.weight.shape)The state_dict itself is still serialized through PyTorch's checkpoint machinery. The weights_only=True option asks PyTorch to restrict the objects that the unpickler will reconstruct. It reduces the attack surface, but it does not make an unknown file trustworthy. Use current PyTorch versions and verify the file's source.
Complete objects versus weights
Saving a complete model with torch.save(model, path) can record class information and other Python object details. Loading it may require importing the original class, and ordinary pickle loading can execute attacker-controlled behavior.
Saving model.state_dict() is more portable and easier to review. You still need compatible model code, matching parameter names, and compatible tensor shapes.
Exercise
Why is saving a state_dict generally safer and more portable than saving a complete PyTorch model object?
Show answer
A state_dict primarily stores named tensor values, while a complete model can store Python class and object information that depends on imports and may trigger unsafe pickle behavior during loading.