Load tensors saved in python from c++ and vice versa

Hello,
I noticed that I can’t read tensor from .pt file saved in python with torch.save() from c++ with torch::load() and I can’t read tensor from file saved in c++ with torch::save() from python with torch.load()
I read that PyTorch uses different formats to save tensors in python with pickle and in c++ it seems to be zip with tensors inside, but maybe are there any ways to transfer tensors between python and c++?
Thanks a lot

2 Likes

As far as I know, C++ uses the torch.jit serialization. Have you tried saving/loading it via torch.jit?

It is not possible. torch.jit.save() can only save ScriptModule but not a tensor

Any idea about this? Very important feature for integration

1 Like

Joining the question, important feature indeed.

So I think we’re lacking a bit here, but you could wrap your tensors into a module and save and load that though JIT.
As this is clumsy, it would be nice to have something better. Options seem to be

  • Read pickle. Unfortunately, the Python pickle format torch.save is Python-specific. There seems to be an a library capable of reading some pickle files, but I’m not sure about it’s usability here or the maintenance status.
  • Or you could write a model’s state dict to a standard format, say HDF5, and use that to read and write tensors. HDF5 is generally well supported.

Ideally, we would have a ready utility for this, but I’m afraid we don’t at this point.

Best regards

Thomas

This seems seems to be a plan:

Below code works.

python code:

import torch
from torch import nn
 
x = torch.ones(4,5)
m = nn.Module()
par = nn.Parameter(x)
m.register_parameter("0",par)
tensors = torch.jit.script(m)
tensors.save('x.ts')

c++ code:

try
{
    torch::Tensor x;
    torch::load(x, "x.ts");
    std::cout << x;
    return 0;
}
catch (const c10::Error& e) {
    std::cerr << "error loading the tensor " << std::endl;
    std::cerr << e.msg() << std::endl;
    return 0;
}
1 Like

another solution.

python code:

import torch
from torch import nn
 
class TensorContainer(nn.Module):
    def __init__(self, tensor_dict):
        super().__init__()
        for key,value in tensor_dict.items():
            setattr(self, key, value)
 
x = torch.ones(4, 4)
tensor_dict = {'x': x}
tensors = TensorContainer(tensor_dict)
tensors = torch.jit.script(tensors)
tensors.save('x.pth')

c++ code:

try
{
    torch::jit::script::Module tensors = torch::jit::load("x.pth");
    c10::IValue iv = tensors.attr("x");
    torch::Tensor ts = iv.toTensor();
    std::cout << ts;
}catch (const c10::Error& e) {
    std::cerr << "error loading the tensor " << std::endl;
    std::cerr << e.msg() << std::endl;
}
1 Like