Saving and loading in PyTorch
Answer: Instead of saving a module directly, for compatibility reasons it is recommended to instead save only its state dict. (S1)
What gets saved
- Custom data structures that include PyTorch tensors can also be saved if the data structure is pickle-able. (S1)
- When PyTorch saves tensors it saves their storage objects and tensor metadata separately. (S1)
- When saving tensors with fewer elements than their storage objects, the size of the saved file can be reduced by first cloning the tensors. (S1)
- The mmap argument in torch.load() allows for lazy loading of tensor storages. (S1)
File format
- A common PyTorch convention is to save tensors using .pt file extension. (S2)
- The 1.6 release of PyTorch switched torch.save to use a new zipfile-based file format. (S2)
- torch.load still retains the ability to load files in the old format. (S2)
Claims
- id: C1 claim: Save the state dict, not the module. source: S1 evidence: "to instead save only its state dict."
- id: C2 claim: Storage and metadata are saved separately. source: S1 evidence: "When PyTorch saves tensors it saves their storage objects and tensor"
- id: C3 claim: Clone small views to shrink files. source: S1 evidence: "the saved file can be reduced by first cloning the tensors."
- id: C4 claim: torch.save uses a zipfile format since 1.6. source: S2 evidence: "The 1.6 release of PyTorch switched torch.save to use a new"
Sources
- [S1] Serialization semantics — PyTorch 2.15 documentation. PyTorch docs. https://docs.pytorch.org/docs/2.15/notes/serialization.html . License: BSD-style (pytorch/pytorch LICENSE; Copyright (c) 2016-present, Facebook Inc.; https://github.com/pytorch/pytorch/blob/main/LICENSE); docs only, not the tutorials repo; PyTorch name and logo not licensed. Accessed 2026-11-04.
- [S2] torch.save — PyTorch 2.15 documentation. PyTorch docs. https://docs.pytorch.org/docs/2.15/generated/torch.save.html . License: BSD-style (pytorch/pytorch LICENSE; Copyright (c) 2016-present, Facebook Inc.; https://github.com/pytorch/pytorch/blob/main/LICENSE); docs only, not the tutorials repo; PyTorch name and logo not licensed. Accessed 2026-11-04.
License: this note is CC BY 4.0 (https://creativecommons.org/licenses/by/4.0/). Summary by iinventedlight (AI-assisted).
License: CC-BY-4.0 · Summary by iinventedlight (AI-assisted). Sources: PyTorch docs, Serialization semantics — PyTorch 2.15 documentation, BSD-style (pytorch/pytorch LICENSE; Copyright (c) 2016-present, Facebook Inc.; https://github.com/pytorch/pytorch/blob/main/LICENSE); docs only, not the tutorials repo; PyTorch name and logo not licensed; PyTorch docs, torch.save — PyTorch 2.15 documentation, BSD-style (pytorch/pytorch LICENSE; Copyright (c) 2016-present, Facebook Inc.; https://github.com/pytorch/pytorch/blob/main/LICENSE); docs only, not the tutorials repo; PyTorch name and logo not licensed.
Cite this
iinventedlight. 2026. "Saving and loading in PyTorch." iinventedlight Library, iil:note:24vtnypb, version of 2026-11-04. https://iinventedlight.com/library/notes/pytorch-serialization/. License: CC-BY-4.0.
@misc{iil_note_24vtnypb,
author = {{iinventedlight}},
title = {{Saving and loading in PyTorch}},
howpublished = {iinventedlight Library},
year = {2026},
date = {2026-11-04},
url = {https://iinventedlight.com/library/notes/pytorch-serialization/},
note = {Permanent ID iil:note:24vtnypb. License: CC-BY-4.0.},
}
Spotted a mistake? Use the MCP feedback tool or email hello@iinventedlight.com.
For language models and agents: start at /llms.txt. This page in Markdown: /library/notes/pytorch-serialization.md.