torch.load
- torch.load(f, map_location=None, pickle_module=pickle, *, weights_only=True, mmap=None, **pickle_load_args
Loads an object saved with torch.save() from a file.
torch.load() uses Python’s unpickling facilities but treats storages,
which underlie tensors, specially. They are first deserialized on the
CPU and are then moved to the device they were saved from. If this fails
(e.g. because the run time system doesn’t have certain devices), an exception
is raised. However, storages can be dynamically remapped to an alternative
set of devices using the map_location argument.
If map_location is a callable, it will be called once for each serialized
storage with two arguments: storage and location. The storage argument
will be the initial deserialization of the storage, residing on the CPU.
Each serialized storage has a location tag associated with it which
identifies the device it was saved from, and this tag is the second
argument passed to map_location. The builtin location tags are 'cpu'
for CPU tensors and 'cuda:device_id' (e.g. 'cuda:2') for CUDA tensors.
map_location should return either None or a storage. If
map_location returns a storage, it will be used as the final deserialized
object, already moved to the right device. Otherwise, torch.load() will
fall back to the default behavior, as if wasn’t specified.
If map_location is a torch.device object or a string containing
a device tag, it indicates the location where all tensors should be loaded.
Otherwise, if map_location is a dict, it will be used to remap location tags
appearing in the file (keys), to ones that specify where to put the
storages (values).
User extensions can register their own location tags and tagging and
deserialization methods using torch.serialization.register_package().
See Layout Control for more advanced tools to manipulate a checkpoint.
>>> torch.load("tensors.pt", weights_only=True)
# Load all tensors onto the CPU
>>> torch.load(
... "tensors.pt",
... map_location=torch.device("cpu"),
... weights_only=True,
...
map_location (Callable[[Storage, str], Storage] | device | str | dict[str, str] | None) – a function, torch.device, string or a dict specifying how to remap storage
locations
pickle_module (Any | None) – module used for unpickling metadata and objects (has to
match the pickle_module used to serialize file)
weights_only (bool | None) – Indicates whether unpickler should be restricted to
loading only tensors, primitive types, dictionaries
and any types added via torch.serialization.add_safe_globals().
See torch.load with weights_only=True for more details. When weights_only=True
and the checkpoint contains sparse tensors, their invariants (e.g.
index bounds) are always validated to prevent malformed indices from
causing out-of-bounds reads later; this is an O(nnz) scan per sparse
tensor and may be slow for large checkpoints.
mmap (bool | None) – Indicates whether the file should be mapped rather than loading all the storages into memory.
Typically, tensor storages in the file will first be moved from disk to CPU memory, after which they
are moved to the location that they were tagged with when saving, or specified by map_location. This
second step is a no-op if the final location is CPU. When the mmap flag is set, instead of copying the
tensor storages from disk to CPU memory in the first step, f is mapped, which means tensor storages
will be lazily loaded when their data is accessed.
pickle_load_args (Any) – optional keyword arguments passed over to
pickle_module.load() and pickle_module.Unpickler(),
only works if weights_only=False, e.g., errors=....