mirror of
https://github.com/pytorch/pytorch.git
synced 2026-11-20 09:04:25 +08:00
19da3da3d3
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
59 lines
1.4 KiB
Markdown
59 lines
1.4 KiB
Markdown
---
|
|
myst:
|
|
html_meta:
|
|
description: PyTorch C++ serialization API — saving and loading models and tensors.
|
|
keywords: 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:**
|
|
|
|
```cpp
|
|
#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
|
|
|
|
```{toctree}
|
|
:maxdepth: 1
|
|
|
|
save_load
|
|
archives
|
|
checkpoints
|
|
```
|