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
-
inline explicit OutputArchive()#
-
OutputArchive(OutputArchive&&) = default#
-
OutputArchive &operator=(OutputArchive&&) = default#
-
OutputArchive(OutputArchive&) = delete#
-
OutputArchive &operator=(OutputArchive&) = delete#
-
inline std::shared_ptr<jit::CompilationUnit> compilation_unit() const#
-
void write(const std::string &key, const c10::IValue &ivalue)#
Writes an
IValueto theOutputArchive.
-
void write(const std::string &key, const Tensor &tensor, bool is_buffer = false)#
Writes a
(key, tensor)pair to theOutputArchive, and marks it as being or not being a buffer (non-differentiable tensor).
-
void write(const std::string &key, OutputArchive &nested_archive)#
Writes a nested
OutputArchiveunder the givenkeyto thisOutputArchive.
-
void save_to(const std::string &filename)#
Saves the
OutputArchiveinto a serialized representation in a file atfilename.
-
void save_to(std::ostream &stream)#
Saves the
OutputArchiveinto a serialized representation into the givenstream.
-
void save_to(const std::function<size_t(const void*, size_t)> &func)#
Saves the
OutputArchiveinto a serialized representation using the given writer function.
-
template<typename ...Ts>
inline void operator()(Ts&&... ts)# Forwards all arguments to
write().Useful for generic code that can be reused for both
OutputArchiveandInputArchive(whereoperator()forwards toread()).
-
inline explicit OutputArchive()#
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&&) = default#
-
InputArchive &operator=(InputArchive&&) = default#
-
InputArchive(InputArchive&) = delete#
-
InputArchive &operator=(InputArchive&) = delete#
-
~InputArchive() = default#
-
void read(const std::string &key, c10::IValue &ivalue)#
Reads an
IValueassociated with a givenkey.
-
bool try_read(const std::string &key, c10::IValue &ivalue)#
Reads an
IValueassociated with a givenkey.If there is no
IValueassociated with thekey, this returns false, otherwise it returns true.
-
bool try_read(const std::string &key, Tensor &tensor, bool is_buffer = false)#
Reads a
tensorassociated with a givenkey.If there is no
tensorassociated with thekey, this returns false, otherwise it returns true. If the tensor is expected to be a buffer (not differentiable),is_buffermust betrue.
-
void read(const std::string &key, Tensor &tensor, bool is_buffer = false)#
Reads a
tensorassociated with a givenkey.If the tensor is expected to be a buffer (not differentiable),
is_buffermust betrue.
-
bool try_read(const std::string &key, InputArchive &archive)#
Reads a
InputArchiveassociated with a givenkey.If there is no
InputArchiveassociated with thekey, this returns false, otherwise it returns true.
-
void read(const std::string &key, InputArchive &archive)#
Reads an
InputArchiveassociated with a givenkey.The archive can thereafter be used for further deserialization of the nested data.
-
void load_from(const std::string &filename, std::optional<torch::Device> device = std::nullopt)#
Loads the
InputArchivefrom a serialized representation stored in the file atfilename.Storage are remapped using device option. If device is not specified, the module is loaded to the original device.
-
void load_from(std::istream &stream, std::optional<torch::Device> device = std::nullopt)#
Loads the
InputArchivefrom a serialized representation stored in the givenstream.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<torch::Device> device = std::nullopt)#
-
void load_from(const std::function<size_t(uint64_t pos, void *buf, size_t nbytes)> &read_func, const std::function<size_t(void)> &size_func, std::optional<torch::Device> device = std::nullopt)#
-
std::vector<std::string> keys()#
-
template<typename ...Ts>
inline void operator()(Ts&&... ts)# Forwards all arguments to
read().Useful for generic code that can be reused for both
InputArchiveandOutputArchive(whereoperator()forwards towrite()).
-
InputArchive()#
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<int>();
float loss = loss_tensor.item<float>();