Title: Archives — PyTorch main documentation
Description: Serialization archives in PyTorch C++ — InputArchive and OutputArchive for model serialization.
Keywords:
Domain: docs.pytorch.org
{
"@context": "https://schema.org",
"@type": "Article",
"name": "Archives",
"headline": "Archives",
"description": "Serialization archives in PyTorch C++ \u2014 InputArchive and OutputArchive for model serialization.",
"url": "/api/serialize/archives.html",
"articleBody": "Archives# Archives provide a lower-level interface for serialization, allowing you to save multiple values to a single file with named keys. OutputArchive# class OutputArchive# Public Functions explicit OutputArchive(std::shared_ptr\u003cjit::CompilationUnit\u003e cu)# inline explicit OutputArchive()# OutputArchive(OutputArchive\u0026\u0026) = default# OutputArchive \u0026operator=(OutputArchive\u0026\u0026) = default# OutputArchive(OutputArchive\u0026) = delete# OutputArchive \u0026operator=(OutputArchive\u0026) = delete# inline std::shared_ptr\u003cjit::CompilationUnit\u003e compilation_unit() const# void write(const std::string \u0026key, const c10::IValue \u0026ivalue)# Writes an IValue to the OutputArchive. void write(const std::string \u0026key, const Tensor \u0026tensor, bool is_buffer = false)# Writes a (key, tensor) pair to the OutputArchive, and marks it as being or not being a buffer (non-differentiable tensor). void write(const std::string \u0026key, OutputArchive \u0026nested_archive)# Writes a nested OutputArchive under the given key to this OutputArchive. void save_to(const std::string \u0026filename)# Saves the OutputArchive into a serialized representation in a file at filename. void save_to(std::ostream \u0026stream)# Saves the OutputArchive into a serialized representation into the given stream. void save_to(const std::function\u003csize_t(const void*, size_t)\u003e \u0026func)# Saves the OutputArchive into a serialized representation using the given writer function. template\u003ctypename ...Ts\u003einline void operator()(Ts\u0026\u0026... ts)# Forwards all arguments to write(). Useful for generic code that can be reused for both OutputArchive and InputArchive (where operator() forwards to read()). Example: torch::serialize::OutputArchive archive; archive.write(\"tensor1\", tensor1); archive.write(\"tensor2\", tensor2); archive.save_to(\"multi_tensor.pt\"); InputArchive# class InputArchive# A recursive representation of tensors that can be deserialized from a file or stream. In most cases, users should not have to interact with this class, and should instead use torch::load. Public Functions InputArchive()# Default-constructs the InputArchive. InputArchive(InputArchive\u0026\u0026) = default# InputArchive \u0026operator=(InputArchive\u0026\u0026) = default# InputArchive(InputArchive\u0026) = delete# InputArchive \u0026operator=(InputArchive\u0026) = delete# ~InputArchive() = default# void read(const std::string \u0026key, c10::IValue \u0026ivalue)# Reads an IValue associated with a given key. bool try_read(const std::string \u0026key, c10::IValue \u0026ivalue)# Reads an IValue associated with a given key. If there is no IValue associated with the key, this returns false, otherwise it returns true. bool try_read(const std::string \u0026key, Tensor \u0026tensor, bool is_buffer = false)# Reads a tensor associated with a given key. If there is no tensor associated with the key, this returns false, otherwise it returns true. If the tensor is expected to be a buffer (not differentiable), is_buffer must be true. void read(const std::string \u0026key, Tensor \u0026tensor, bool is_buffer = false)# Reads a tensor associated with a given key. If the tensor is expected to be a buffer (not differentiable), is_buffer must be true. bool try_read(const std::string \u0026key, InputArchive \u0026archive)# Reads a InputArchive associated with a given key. If there is no InputArchive associated with the key, this returns false, otherwise it returns true. void read(const std::string \u0026key, InputArchive \u0026archive)# Reads an InputArchive associated with a given key. The archive can thereafter be used for further deserialization of the nested data. void load_from(const std::string \u0026filename, std::optional\u003ctorch::Device\u003e device = std::nullopt)# Loads the InputArchive from a serialized representation stored in the file at filename. Storage are remapped using device option. If device is not specified, the module is loaded to the original device. void load_from(std::istream \u0026stream, std::optional\u003ctorch::Device\u003e device = std::nullopt)# Loads the InputArchive from a serialized representation stored in the given stream. Storage are remapped using device option. If device is not specified, the module is loaded to the original device. void load_from(const char *data, size_t size, std::optional\u003ctorch::Device\u003e device = std::nullopt)# void load_from(const std::function\u003csize_t(uint64_t pos, void *buf, size_t nbytes)\u003e \u0026read_func, const std::function\u003csize_t(void)\u003e \u0026size_func, std::optional\u003ctorch::Device\u003e device = std::nullopt)# std::vector\u003cstd::string\u003e keys()# template\u003ctypename ...Ts\u003einline void operator()(Ts\u0026\u0026... ts)# Forwards all arguments to read(). Useful for generic code that can be reused for both InputArchive and OutputArchive (where operator() forwards to write()). Example: torch::serialize::InputArchive archive; archive.load_from(\"multi_tensor.pt\"); torch::Tensor tensor1, tensor2; archive.read(\"tensor1\", tensor1); archive.read(\"tensor2\", tensor2); Saving Multiple Values# Archives are useful when you need to save multiple related values together: // Save multiple tensors and metadata torch::serialize::OutputArchive out_archive; out_archive.write(\"weights\", model_weights); out_archive.write(\"biases\", model_biases); out_archive.write(\"epoch\", torch::tensor(current_epoch)); out_archive.write(\"loss\", torch::tensor(best_loss)); out_archive.save_to(\"training_state.pt\"); // Load them back torch::serialize::InputArchive in_archive; in_archive.load_from(\"training_state.pt\"); torch::Tensor weights, biases, epoch_tensor, loss_tensor; in_archive.read(\"weights\", weights); in_archive.read(\"biases\", biases); in_archive.read(\"epoch\", epoch_tensor); in_archive.read(\"loss\", loss_tensor); int epoch = epoch_tensor.item\u003cint\u003e(); float loss = loss_tensor.item\u003cfloat\u003e();",
"author": {
"@type": "Organization",
"name": "PyTorch Contributors",
"url": "https://pytorch.org"
},
"image": "https://pytorch.org/docs/stable/_static/img/pytorch_seo.png",
"mainEntityOfPage": {
"@type": "WebPage",
"@id": "/api/serialize/archives.html"
},
"datePublished": "2023-01-01T00:00:00Z",
"dateModified": "2023-01-01T00:00:00Z"
}
| docsearch:language | en |
| llm:site-type | documentation |
| llm:framework | PyTorch |
| llm:description | Serialization archives in PyTorch C++ — InputArchive and OutputArchive for model serialization. |
| llm:navigation-file | https://pytorch.org/docs/stable/llms.txt |
| llm:sitemap | https://pytorch.org/docs/stable/sitemap.xml |
| llm:version | main |
| llm:project | PyTorch |
| llm:page-type | documentation |
| og:image | https://docs.pytorch.org/docs/stable/_static/img/pytorch_seo.png |
| None | 3 |
Links:
Viewport: width=device-width, initial-scale=1