Skip to content
masterclassBuddyTech Buzz5 fresh blogs · 5:00 AM IST

PyTorch · Function

torch.load

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.

Since
2.6 (default weights_only behavior)

How it works

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.

Syntax

torch.load(f, map_location=None, pickle_module=pickle, *, weights_only=True, mmap=None, **pickle_load_args)

Parameters

NameTypeDescription
fstr, os.PathLike, or file-like objectA file-like object, path, or path-like object containing data saved by torch.save.
map_locationoptionalControls how tensor storages are remapped during loading; it may be a device, string, callable, or dictionary.
pickle_modulemoduleModule providing the unpickling implementation.
weights_onlyboolUses the restricted weights-only unpickler when true; defaults to true when pickle_module is omitted.
mmapbool or NoneIf true, memory-map eligible tensor storages from the file for lazy loading.
pickle_load_argskeyword argumentsAdditional keyword arguments passed to the pickle module’s load function.

Returns

The object reconstructed from the serialized file, with tensor storages placed according to the saved locations or map_location.

Key points

  • Use torch.load to reconstruct objects serialized with torch.save.
  • Tensor storage normally passes through CPU memory before returning to its recorded device.
  • Use map_location to redirect loaded tensor storage to another device.
  • A device or device-tag string makes every loaded tensor target that location.
  • mmap maps the checkpoint file and supports lazy storage loading rather than an immediate CPU copy.
  • Serialized storages retain tags identifying their original devices.

Common mistakes

  • Only load files from trusted sources because deserialization uses Python unpickling.

Example: Load a checkpoint safely

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.

When to use it

Checkpoint recovery

Restore a model or optimizer checkpoint during training or evaluation.

Cross-device loading

Load a GPU-created checkpoint on a CPU-only machine or onto a chosen accelerator.

Memory-mapped loading

Read large tensor checkpoints while deferring storage access until data is needed.

Restricted weight deserialization

Load a state dictionary without reconstructing arbitrary custom Python objects.

Interview questions

QWhat does torch.load do with tensor storage devices?

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.

QWhy is weights_only important?

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.

QHow can loading be redirected or made lazy?

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.save
  • torch.serialization
  • torch.device
  • torch.load_state_dict

Source: torch.load — PyTorch main documentation. Checked 2026-10-16.