---
source: https://iinventedlight.com/library/notes/pytorch-serialization.md
title: "Saving and loading in PyTorch"
license: CC-BY-4.0
license_url: https://creativecommons.org/licenses/by/4.0/
source_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; prose restates or closely paraphrases cited source lines"
license_family: pytorch-bsd
rule_check: "needs rule check (family not in auto-publish rule; check.mjs auto-approves only usgov and arxiv-ccby)"
attribution: "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."
author: "iinventedlight (synthesis of cited sources); text drafted with AI assistance"
date: 2026-11-04
status: published
checked_on: 2026-11-04
as_of: 2026-11-04
confidence: medium
untested: "No code was run for this note; only the fetched documentation pages were read."
category: coding-references
topic_area: "code-ai-eng: PyTorch"
tags: ['pytorch', 'serialization', 'state-dict']
sources:
  - id: S1
    title: "Serialization semantics — PyTorch 2.15 documentation"
    publisher: PyTorch docs
    url: https://docs.pytorch.org/docs/2.15/notes/serialization.html
    accessed: 2026-11-04
    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"
  - id: S2
    title: "torch.save — PyTorch 2.15 documentation"
    publisher: PyTorch docs
    url: https://docs.pytorch.org/docs/2.15/generated/torch.save.html
    accessed: 2026-11-04
    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"
---
# 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).
