Hi-
I cannot load a tensor saved with pytorch torch.save()
in cpp. I am getting the following error:
terminate called after throwing an instance of 'c10::Error'
what(): [enforce fail at inline_container.cc:145] . PytorchStreamReader failed reading zip archive: not a ZIP archive
frame #0: c10::ThrowEnforceNotMet(char const*, int, char const*, std::string const&, void const*) + 0x47 (0x7f39e02e76a7 in /opt/project_cpp/test/include/libtorch/lib/libc10.so)
frame #1: caffe2::serialize::PyTorchStreamReader::valid(char const*, char const*) + 0x8b (0x7f39be4f272b in /opt/project_cpp/test/include/libtorch/lib/libtorch_cpu.so)
frame #2: caffe2::serialize::PyTorchStreamReader::init() + 0x9e (0x7f39be4f4a9e in /opt/project_cpp/test/include/libtorch/lib/libtorch_cpu.so)
frame #3: caffe2::serialize::PyTorchStreamReader::PyTorchStreamReader(std::unique_ptr<caffe2::serialize::ReadAdapterInterface, std::default_delete<caffe2::serialize::ReadAdapterInterface> >) + 0x57 (0x7f39be4f7527 in /opt/project_cpp/test/include/libtorch/lib/libtorch_cpu.so)
frame #4: torch::jit::pickle_load(std::vector<char, std::allocator<char> > const&) + 0xa3 (0x7f39bf7c07d3 in /opt/project_cpp/test/include/libtorch/lib/libtorch_cpu.so)
frame #5: torch::pickle_load(std::vector<char, std::allocator<char> > const&) + 0x9 (0x7f39bfa27df9 in /opt/project_cpp/test/include/libtorch/lib/libtorch_cpu.so)
frame #6: <unknown function> + 0x6e10 (0x560f66cbce10 in ./trt_sample)
frame #7: <unknown function> + 0x8019 (0x560f66cbe019 in ./trt_sample)
frame #8: __libc_start_main + 0xe7 (0x7f397ea80b97 in /lib/x86_64-linux-gnu/libc.so.6)
frame #9: <unknown function> + 0x64da (0x560f66cbc4da in ./trt_sample)
I reinstalled LibTorch with cuda (Pre-cxx11 ABI & cxx11 ABI) but still not working.
I can use the torch library normally, but cannot load models/tensors/dicts saved with torch.save()
.
Code to load .pt
file:
torch::IValue loadPytorchTensor(string fname) {
std::ifstream input_stream(fname);
input_stream >> std::noskipws;
std::vector<char> input;
input.insert(
input.begin(),
std::istream_iterator<char>(input_stream),
std::istream_iterator<char>());
cout << "Before -----" << endl; //Prints as expected
torch::IValue ivalue = torch::pickle_load(input);
cout << "After -----" << endl; // Currently this never prints
return ivalue;
}
string tensor_fn = "input.pt";
torch::IValue ivalue = loadPytorchTensor(tensor_fn);