Files
pytorch/docs/cpp/source/api/serialize/index.md
T
Svetlana Karslioglu 19da3da3d3 Restructure C++ docs (#174096)
The fix-cpp-build branch is a major restructuring that:

- Drops Exhale (which auto-generated API docs but caused OOM in CI) in favor of hand-written RST pages using Breathe directives
- Reorganizes notes into a hierarchical API reference with proper categories
- Adds new content: XPU docs, stable ABI docs, DeviceGuard/Stream docs
- Always runs Doxygen instead of gating on an env var

Pull Request resolved: https://github.com/pytorch/pytorch/pull/174096
Approved by: https://github.com/janeyx99, https://github.com/seemethere, https://github.com/albanD
2026-04-07 17:24:38 +00:00

1.4 KiB

myst
myst
html_meta
description keywords
PyTorch C++ serialization API — saving and loading models and tensors. PyTorch, C++, serialization, save, load, checkpoint, model

Serialization (torch::serialize)

The torch::serialize namespace provides utilities for saving and loading model weights, tensors, and optimizer state. This enables checkpointing during training and deployment of trained models.

When to use torch::serialize:

  • When saving trained models to disk
  • When implementing training checkpoints
  • When loading pre-trained weights
  • When transferring models between C++ and Python

Basic usage:

#include <torch/torch.h>

// Save a model
auto model = std::make_shared<Net>();
// ... train the model ...
torch::save(model, "model.pt");

// Load a model
auto loaded_model = std::make_shared<Net>();
torch::load(loaded_model, "model.pt");

// Save and load tensors
torch::Tensor tensor = torch::randn({2, 3});
torch::save(tensor, "tensor.pt");

torch::Tensor loaded_tensor;
torch::load(loaded_tensor, "tensor.pt");

Header Files

  • torch/csrc/api/include/torch/serialize.h - Main serialization header
  • torch/csrc/api/include/torch/serialize/archive.h - Archive classes
  • torch/csrc/api/include/torch/serialize/input-archive.h - Input archive
  • torch/csrc/api/include/torch/serialize/output-archive.h - Output archive

Serialization Categories

:maxdepth: 1

save_load
archives
checkpoints