How to serialize a tensor or vector of tensors in libtorch?

Hello everyone,
I wanted to serialize a vector<std::tuple<std::string, torch::Tensor>> and a vector of that meaning a std::vector<std::vector<std::tuple<std::string, torch::Tensor>>> in libtorch.
What are my options here? does libtorch offer a serialization capability such as pickle or not? what should I be doing in this case?
Thanks a lot in advance

Seems like both tensor and vector of tensors are supported and can be easily serialized using torch::save and torch::load respectively.

how it works!
torch::save(tensor, "");
torch::save(tensor_vec "")

for loading :

torch::load(tensor_vec, "")

However, I could really appreciate if anyone could give me a hint on how to add support for more types such as the one I mentioned above.

I could save the file using but I have difficulty loading from it :
for saving I simply did :

template <typename... SaveToArgs>
void save(const std::vector<std::tuple<std::string, torch::Tensor>>& tensor_vec, SaveToArgs&&... args) 
    torch::serialize::OutputArchive archive(std::make_shared<torch::jit::CompilationUnit>());
    for (size_t i = 0; i < tensor_vec.size(); i+=2) 
        auto& key_value = tensor_vec[i];
        auto key_name= std::get<0>(key_value);
        auto value_tensor = std::get<1>(key_value);

        archive.write(c10::to_string(i), key_name);
        archive.write(c10::to_string(i+1), value_tensor);


and similarly for load:

template <typename... LoadFromArgs>
void load(std::vector<std::tuple<std::string, torch::Tensor>>& tensor_vec, LoadFromArgs&&... args)
    torch::serialize::InputArchive archive;

    // NOTE: The number of elements in the serialized `std::vector<torch::Tensor>`
    // is not known ahead of time, so we need a while-loop to increment the index,
    // and use `archive.try_read(...)` to check whether we have reached the end of
    // the serialized `std::vector<torch::Tensor>`.
    size_t index = 0;
    torch::Tensor value;
    std::string key_value;
    while (archive.try_read(c10::to_string(index), key_value))
        archive.try_read(c10::to_string(index+1), value);
        tensor_vec.push_back(std::make_tuple(key_value, std::move(value)));
        value = torch::Tensor();
        key_value = "";

the prblem that I’m facing here is that the InputArchive currently doesnt have support for reading std::string and the InputArchive is final and I can not extend it.
any help in this regard is greatly appreciated.
@ptrblck @albanD


If the issue is only with InputArchive not having the features you want, you can open an feature request on github.
I am not very familiar with c++ serialization, but it sound like something we want to support! (or at least make it so that users can add support on their side).

Thanks really appreciate your feedback. I ultimately ended up switching to protobuf for serialization but it’d be great to see much better support for this in libtorch natively.

1 Like

What is the type of tensor_vec in your example?

std::vector<std::tuple<std::string, torch::Tensor>>& tensor_vec

You can see its type in the function definition, up in the example I provided at the time.