I am trying to read tensors saved with torch.save() from C++. I tried reproducing this test case:

  
      github.com
  
  
    pytorch/pytorch/blob/2a7a39c1af96fc7816c886644eaf127c5ab53b89/test/cpp/jit/test_misc.cpp#L503-L524 18

void testSerializationInterop() {
  if (isSandcastle()){
    // The module file to load is not generated in Sandcastle
    return;
  }

  // This should be generated by `test/cpp/jit/tests_setup.py`
  std::ifstream input_stream("ivalue.pt");
  std::vector<char> input;
  input.insert(
      input.begin(),
      std::istream_iterator<char>(input_stream),
      std::istream_iterator<char>());
  IValue ivalue = pickle_load(input);

  auto elements = ivalue.toTuple()->elements();
  auto ones = torch::ones({2, 2});
  ASSERT_TRUE(ones.equal(elements.at(0).toTensor()));

  auto twos = torch::ones({3, 5}) * 2;


  This file has been truncated. show original

  
  
    
    
  
  


That is reading a file created with:

  
      github.com
  
  
    pytorch/pytorch/blob/2a7a39c1af96fc7816c886644eaf127c5ab53b89/test/cpp/jit/tests_setup.py#L43-L51

path = 'ivalue.pt'

def setup(self):
    ones = torch.ones(2, 2)
    twos = torch.ones(3, 5) * 2

    value = (ones, twos)

    torch.save(value, self.path, _use_new_zipfile_serialization=True)



  
  
    
    
  
  


I modified the test case example to:
#include <torch/torch.h>
#include <iostream>

int main()
{
  std::ifstream input_stream("ivalue.pt");
    std::vector<char> input;
    input.insert(
        input.begin(),
        std::istream_iterator<char>(input_stream),
        std::istream_iterator<char>());
    std::cout << input.size() << std::endl;
    torch::IValue ivalue = torch::pickle_load(input);
    auto elements = ivalue.toTuple()->elements();
    torch::Tensor a = elements.at(0).toTensor();
    std::cout << a << std::endl; 
}

However I get the following error when running the program:
dfalbel@Daniels-MBP build % ./example-app  
1079
libc++abi.dylib: terminating with uncaught exception of type c10::Error: [enforce fail at inline_container.cc:144] . PytorchStreamReader failed reading zip archive: invalid header or archive is corrupted
frame #0: c10::ThrowEnforceNotMet(char const*, int, char const*, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&, void const*) + 191 (0x10aec51bf in libc10.dylib)
frame #1: caffe2::serialize::PyTorchStreamReader::valid(char const*, char const*) + 131 (0x110883f03 in libtorch_cpu.dylib)
frame #2: caffe2::serialize::PyTorchStreamReader::init() + 315 (0x110882f9b in libtorch_cpu.dylib)
frame #3: caffe2::serialize::PyTorchStreamReader::PyTorchStreamReader(std::__1::unique_ptr<caffe2::serialize::ReadAdapterInterface, std::__1::default_delete<caffe2::serialize::ReadAdapterInterface> >) + 133 (0x110883df5 in libtorch_cpu.dylib)
frame #4: torch::jit::pickle_load(std::__1::vector<char, std::__1::allocator<char> > const&) + 186 (0x11207761a in libtorch_cpu.dylib)
frame #5: torch::pickle_load(std::__1::vector<char, std::__1::allocator<char> > const&) + 14 (0x11229790e in libtorch_cpu.dylib)
frame #6: main + 318 (0x10ae65b4e in example-app)
frame #7: start + 1 (0x7fff72a1dcc9 in libdyld.dylib)
frame #8: 0x0 + 1 (0x1 in ???)

Any idea of what I am doing wrong?
Thanks a lot!
Edit: this might be releated: https://github.com/pytorch/pytorch/issues/42651 9