Writing Tensors to disk with c++, to be loaded by python

I have a c++ process that constructs torch Tensor’s and writes their numerical values to datasets in an hdf5 file. It takes advantages of hdf5’s parallel write capabilities by using multiple threads, each of which writes to a part of the hdf5 file. After the file is written, a python process loads the hdf5 data and converts it into torch Tensor’s.

I am wondering if I can eliminate the hdf5 middleman here. When I google for phrases like “c++ pytorch write tensor disk”, I don’t see clear tutorials or examples of how to do this. Is this type of workflow supported? Where can I find clear documentation on this?

1 Like

Save C++:

auto tensor = torch::randn({ 1, 5 });
auto pickled = torch::pickle_save(tensor);
std::ofstream fout("input.pt", std::ios::out | std::ios::binary);
fout.write(pickled.data(), pickled.size());
fout.close();

Load python:

tensor = torch.load('input.pt')

From this GitHub thread:

2 Likes

Thanks, that works.

I also found this resource which shows usage of the torch::save() API: LibTorch (PyTorch C++ Frontend) | Ye Shu

1 Like