# (LibTorch -\> PyTorch tensors) How to save a C++ Libtorch Tensor and load it into a Python Pytorch project?

**URL:** <https://discuss.pytorch.org/t/libtorch-pytorch-tensors-how-to-save-a-c-libtorch-tensor-and-load-it-into-a-python-pytorch-project/55288>\
**Category:** C++\
**Created:** [September 5, 2019, 6:24pm UTC](https://discuss.pytorch.org/t/libtorch-pytorch-tensors-how-to-save-a-c-libtorch-tensor-and-load-it-into-a-python-pytorch-project/55288 "2019-09-05T18:24:27Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![need\_libtorch\_help](https://discuss.pytorch.org/letter_avatar_proxy/v4/letter/n/71e660/32.png) [@need\_libtorch\_help](https://discuss.pytorch.org/u/need_libtorch_help)\
**Post date:** [September 5, 2019, 6:24pm UTC](https://discuss.pytorch.org/t/libtorch-pytorch-tensors-how-to-save-a-c-libtorch-tensor-and-load-it-into-a-python-pytorch-project/55288/1 "2019-09-05T18:24:27Z")

</div>

Hey, I’m simply trying to save a vector of LibTorch (C++) tensors to file and then load those tensors back into PyTorch (Python) for post-processing reasons.

On the C++ side, I have the following sample code:

```auto
  const auto new_tensor = torch::rand({2, 3, 4});
  const auto new_tensor2 = torch::rand({1, 125, 13, 13});
  torch::save({new_tensor, new_tensor2}, "tensor_vector.pt");

```

I then copy the generated file into my python project directory and call the following script:

```auto
import torch  
tensors = torch.load('./tensor_vector.pt')

```

Which returns the following python error message:

```auto
Traceback (most recent call last):
  File "C:\Users\admin\.conda\envs\py37_torch\lib\tarfile.py", line 189, in nti
    n = int(s.strip() or "0", 8)
ValueError: invalid literal for int() with base 8: 'ZZZZZZZZ'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "C:\Users\admin\.conda\envs\py37_torch\lib\tarfile.py", line 2297, in next
    tarinfo = self.tarinfo.fromtarfile(self)
  File "C:\Users\admin\.conda\envs\py37_torch\lib\tarfile.py", line 1093, in fromtarfile
    obj = cls.frombuf(buf, tarfile.encoding, tarfile.errors)
  File "C:\Users\admin\.conda\envs\py37_torch\lib\tarfile.py", line 1035, in frombuf
    chksum = nti(buf[148:156])
  File "C:\Users\admin\.conda\envs\py37_torch\lib\tarfile.py", line 191, in nti
    raise InvalidHeaderError("invalid header")
tarfile.InvalidHeaderError: invalid header

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "C:\Users\admin\.conda\envs\py37_torch\lib\site-packages\torch\serialization.py", line 524, in _load
    return legacy_load(f)
  File "C:\Users\admin\.conda\envs\py37_torch\lib\site-packages\torch\serialization.py", line 448, in legacy_load
    with closing(tarfile.open(fileobj=f, mode='r:', format=tarfile.PAX_FORMAT)) as tar, \
  File "C:\Users\admin\.conda\envs\py37_torch\lib\tarfile.py", line 1589, in open
    return func(name, filemode, fileobj, **kwargs)
  File "C:\Users\admin\.conda\envs\py37_torch\lib\tarfile.py", line 1619, in taropen
    return cls(name, mode, fileobj, **kwargs)
  File "C:\Users\admin\.conda\envs\py37_torch\lib\tarfile.py", line 1482, in __init__
    self.firstmember = self.next()
  File "C:\Users\admin\.conda\envs\py37_torch\lib\tarfile.py", line 2309, in next
    raise ReadError(str(e))
tarfile.ReadError: invalid header

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "C:/Users/admin/PycharmProjects/TestProject/importLibtorchTensor.py", line 2, in <module>
    tensors = torch.load('./tensor_vector.pt')
  File "C:\Users\admin\.conda\envs\py37_torch\lib\site-packages\torch\serialization.py", line 368, in load
    return _load(f, map_location, pickle_module)
  File "C:\Users\admin\.conda\envs\py37_torch\lib\site-packages\torch\serialization.py", line 528, in _load
    raise RuntimeError("{} is a zip archive (did you mean to use torch.jit.load()?)".format(f.name))
RuntimeError: ./tensor_vector.pt is a zip archive (did you mean to use torch.jit.load()?)

```

Additionally, If I instead try to use the `jit` torch package to load:

```auto
import torch  
tensors = torch.jit.load('./tensor_vector.pt')

```

Then I get the following error:

```auto
Traceback (most recent call last):
  File "C:/Users/admin/PycharmProjects/TestProject/importLibtorchTensor.py", line 2, in <module>
    tensors = torch.jit.load('./tensor_vector.pt')
  File "C:\Users\admin\.conda\envs\py37_torch\lib\site-packages\torch\jit\ __init__.py", line 132, in load
    torch._C.import_ir_module(module_lookup, f, map_location)
RuntimeError: INVALID_ARGUMENT:tensors[1]: Cannot find field. (deserialize at ..\torch\csrc\jit\import.cpp:95)
(no backtrace available)

```

Is there any way to save a C++ Libtorch tensor and load it in as Python Pytorch tensor? I don’t need to save/load entire models, just specific tensors.

---

<div class="post-metadata">

**Author:** ![yf225](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/yf225/32/3418_2.png) [@yf225](https://discuss.pytorch.org/u/yf225)\
**Post date:** [September 5, 2019, 6:56pm UTC](https://discuss.pytorch.org/t/libtorch-pytorch-tensors-how-to-save-a-c-libtorch-tensor-and-load-it-into-a-python-pytorch-project/55288/2 "2019-09-05T18:56:05Z")

</div>

We are working on the feature of saving a C++ libtorch Tensor and loading it into PyTorch Python frontend. There is an open issue for it [https://github.com/pytorch/pytorch/issues/20356](https://github.com/pytorch/pytorch/issues/20356) and we will have an update as soon as possible.

---

<div class="post-metadata">

**Author:** ![ShangxuanWu](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/shangxuanwu/32/12362_2.png) [@ShangxuanWu](https://discuss.pytorch.org/u/ShangxuanWu)\
**Post date:** [November 19, 2019, 4:33pm UTC](https://discuss.pytorch.org/t/libtorch-pytorch-tensors-how-to-save-a-c-libtorch-tensor-and-load-it-into-a-python-pytorch-project/55288/3 "2019-11-19T16:33:22Z")

</div>

Is there any update to this? I am also trying to load some tensors dumped in LibTorch into Pytorch but failed.

---

<div class="post-metadata">

**Author:** ![solvingPuzzles](https://discuss.pytorch.org/letter_avatar_proxy/v4/letter/s/f14d63/32.png) [@solvingPuzzles](https://discuss.pytorch.org/u/solvingPuzzles)\
**Post date:** [January 26, 2020, 11:44pm UTC](https://discuss.pytorch.org/t/libtorch-pytorch-tensors-how-to-save-a-c-libtorch-tensor-and-load-it-into-a-python-pytorch-project/55288/4 "2020-01-26T23:44:19Z")

</div>

I see that there’s been some progress on this. On the issue that was linked above ([https://github.com/pytorch/pytorch/issues/20356](https://github.com/pytorch/pytorch/issues/20356)), there was a new comment in Dec 2019 that shows a way to export a torch tensor from C++ to disk to Python.

---

<div class="post-metadata">

**Author:** ![Edwardmark](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/edwardmark/32/22460_2.png) [@Edwardmark](https://discuss.pytorch.org/u/Edwardmark)\
**Post date:** [April 14, 2020, 7:58am UTC](https://discuss.pytorch.org/t/libtorch-pytorch-tensors-how-to-save-a-c-libtorch-tensor-and-load-it-into-a-python-pytorch-project/55288/5 "2020-04-14T07:58:38Z")

</div>

@solvingPuzzles And how to load pytorch tensor in c++ libtorch?  
[https://github.com/pytorch/pytorch/issues/36554](https://github.com/pytorch/pytorch/issues/36554)

---

<div class="post-metadata">

**Author:** ![neilmehta87](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/neilmehta87/32/25835_2.png) [@neilmehta87](https://discuss.pytorch.org/u/neilmehta87)\
**Post date:** [May 29, 2020, 1:39am UTC](https://discuss.pytorch.org/t/libtorch-pytorch-tensors-how-to-save-a-c-libtorch-tensor-and-load-it-into-a-python-pytorch-project/55288/6 "2020-05-29T01:39:49Z")

</div>

Is there a way to do this operation (C++ tensor to pytorch pthon) directly on a GPU memory? Will using pybind11 be useful?  
Thanks!  
Edit: Would pickle\_load and pickle\_save work on the GPUs?
