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

PyTorch · Function

torch.save

Serializes a PyTorch object to a file path or file-like destination. It uses Python pickling and defaults to a newer ZIP-based file format.

How it works

PyTorch serializes the supplied object with Python pickling and writes it either to a filesystem path or to a file-like object. The default format is a newer ZIP-based archive; the pickle implementation and protocol can be customized, and the legacy format can be selected explicitly with the private compatibility flag.

Syntax

torch.save(obj, f, pickle_module=pickle, pickle_protocol=2, _use_new_zipfile_serialization=True)

Parameters

NameTypeDescription
objobjectObject to serialize and save.
ffile-like object | str | os.PathLikeA file-like object implementing write and flush, or a string or os.PathLike filename.
pickle_modulemoduleModule used for pickling metadata and objects.
pickle_protocolintOptional pickle protocol override.
_use_new_zipfile_serializationboolSelects the newer ZIP-based serialization when true; set false for the old format.

Key points

  • torch.save serializes an object for persistence.
  • The pickle_module parameter controls which module performs pickling.
  • pickle_protocol lets callers replace the default pickle protocol.
  • The documentation recommends the .pt suffix as a conventional tensor-file extension.
  • A BytesIO buffer can be used as an in-memory destination instead of a disk path.

Common mistakes

  • When passing a file-like destination, it must provide both write and flush methods.
  • The default serialization format changed in PyTorch 1.6; older files remain loadable through torch.load.
  • Use the private compatibility keyword only when an older serialization format is specifically required.

Example: Save a tensor

import io
import torch

x = torch.tensor([0, 1, 2, 3, 4])
torch.save(x, "tensor.pt")

buffer = io.BytesIO()
torch.save(x, buffer)

Saves the same tensor both to a conventional .pt file and to an in-memory file-like buffer.

When to use it

Save tensors and objects

Persist a tensor or other serializable object to disk for later loading.

Save to a memory buffer

Write serialized data into an in-memory stream for further handling.

Store checkpoints

Create a checkpoint containing model state or training data.

Interview questions

QWhat can the destination argument be?

It accepts the object to serialize and a destination, which may be a path-like value or a file-like object. A file-like destination must implement write and flush.

Follow-up: What format does it use by default? It uses the newer ZIP-based serialization format; setting _use_new_zipfile_serialization=False selects the old format.

QWhat do pickle_module and pickle_protocol control?

pickle_module chooses the module used to pickle metadata and objects. pickle_protocol overrides the default pickle protocol when supplied.

Follow-up: Why might the pickle protocol matter? It controls the pickle representation and can affect compatibility with consumers that support only particular protocol versions.

QWhat serialization format does torch.save use?

torch.save uses a ZIP-based file format by default, a behavior introduced in PyTorch 1.6. The saved object can be a tensor or another Python object supported by the selected pickling mechanism.

Follow-up: How would you preserve compatibility with code expecting the old format? Pass _use_new_zipfile_serialization=False, then load the result with torch.load.

  • torch.load
  • pickle
  • io.BytesIO

Source: torch.save — PyTorch 2.8 documentation. Checked 2026-10-16.