Checkpoint recovery
Restore a model or optimizer checkpoint during training or evaluation.
PyTorch · Function
Loads an object saved by torch.save, restoring tensor storages to their saved devices or a location selected with map_location. Optional mmap enables lazy storage loading, while weights_only provides restricted deserialization.
torch.load reconstructs an object saved by torch.save. Tensor storage is initially deserialized on the CPU, then restored to its saved device unless map_location redirects it; mmap can defer storage loading until tensor data is accessed.
torch.load(f, map_location=None, pickle_module=pickle, *, weights_only=True, mmap=None, **pickle_load_args)| Name | Type | Description |
|---|---|---|
f | str, os.PathLike, or file-like object | A file-like object, path, or path-like object containing data saved by torch.save. |
map_location | optional | Controls how tensor storages are remapped during loading; it may be a device, string, callable, or dictionary. |
pickle_module | module | Module providing the unpickling implementation. |
weights_only | bool | Uses the restricted weights-only unpickler when true; defaults to true when pickle_module is omitted. |
mmap | bool or None | If true, memory-map eligible tensor storages from the file for lazy loading. |
pickle_load_args | keyword arguments | Additional keyword arguments passed to the pickle module’s load function. |
The object reconstructed from the serialized file, with tensor storages placed according to the saved locations or map_location.
import torch
checkpoint = torch.load("checkpoint.pt", map_location="cpu", weights_only=True)
print(checkpoint)Loads a checkpoint onto the CPU while restricting deserialization to weights-only behavior.
Restore a model or optimizer checkpoint during training or evaluation.
Load a GPU-created checkpoint on a CPU-only machine or onto a chosen accelerator.
Read large tensor checkpoints while deferring storage access until data is needed.
Load a state dictionary without reconstructing arbitrary custom Python objects.
torch.load reads an object produced by torch.save. Tensor storages are deserialized on the CPU first and then moved to their saved devices unless map_location changes the destination.
Follow-up: How would you load a GPU checkpoint on a CPU-only host? Pass map_location="cpu" so all tensors are loaded on the CPU.
weights_only=True uses a restricted unpickler intended for tensors, primitive types, dictionaries, and explicitly allowlisted types. It is the safer choice when loading a state dictionary or other trusted weights representation.
Follow-up: When might weights_only=False be required? A checkpoint containing custom Python classes may require regular unpickling, but that should only be done for a trusted file.
map_location can be a device, a device-tag string, or a callable. A device or string places all tensors there; a callable receives each storage and its original location tag and can return a replacement storage or None.
Follow-up: What is the purpose of mmap? It maps the file instead of eagerly copying storages to CPU memory, allowing storage data to load lazily when accessed.
torch.savetorch.serializationtorch.devicetorch.load_state_dict