Linking error due to using libtorch and pytorch in same project

David_Rytz: <p>Hi all,</p>
<p>I have a project which requires a cpp library to link to a local libtorch dependency for inference. This cpp library is wrapped using pybind11. The same project also uses Pytorch (for training) which interacts with the cpp library.</p>
<p>LibTorch is linked like so in the CMakeLists.txt<br>
<code>target_link_libraries(${CPP_Library} PUBLIC ${TORCH_LIBRARIES})</code></p>
<p>I also set up a python virtual environment where I install PyTorch like so:<br>
<code>pip install torch</code></p>
<p>In Python, when I call <code>import torch</code>, I get the linking error:</p>
<p><code>from torch._C import *  # noqa: F403</code><br>
<code>ImportError: /home/&lt;path-to&gt;/venv/lib/python3.8/site-packages/torch-1.10.2-py3.8-linux-x86_64.egg/torch/lib/libtorch_python.so: undefined symbol: _ZNK5torch3jit5Graph8toStringEb</code></p>
<p>Both pytorch and libtorch are the same version 10.2 (CUDA).</p>
<p>Is there a recommended way of using both libtorch and pytorch in the same project?</p>
<p>Thank you!</p>

tsoj: <p>Have you found a solution?</p>

ncatelli: <p>Hello,</p>
<p>I ran into a similar issue with a single project that required both libtorch and access to inline-python that was dependent on pytorch. The pytorch dependencies would lead to undefined symbol errors similarly to what you were seeing above with pytorch/libtorch <code>12.1.1</code>.</p>
<p>When looking for the source of that symbol, it seemed to stem from <code>libtorch_cpu.so</code> from pytorch, but was not present in libtorch.</p>
<pre><code class="lang-auto">find /opt/venv/lib/python3.10/site-packages/torch/lib -name \*.so -exec bash -c "nm --defined-only {} 2&gt;/dev/null | grep '_ZNK5torch3jit5Graph8toStringEb' &amp;&amp; echo {}" \;
0000000004d3d9e0 T _ZNK5torch3jit5Graph8toStringEb
000000000141f12c t _ZNK5torch3jit5Graph8toStringEb.cold
/opt/venv/lib/python3.10/site-packages/torch/lib/libtorch_cpu.so
</code></pre>
<p>When comparing pytorch and pytorch</p>
<pre><code class="lang-auto">nm -gC ~/.pyenv/versions/3.10.13/lib/python3.10/site-packages/torch/lib/libtorch_cpu.so | grep 'jit' | grep 'toString'
0000000004f87430 T torch::jit::toString(torch::jit::StrideInput)
0000000004d3d9e0 T torch::jit::Graph::toString(bool) const
0000000004d1b050 T torch::jit::AliasDb::toString() const
</code></pre>
<p>libtorch</p>
<pre><code class="lang-auto">nm -gC ../libtorch/lib/libtorch_cpu.so| grep 'toString' | grep 'jit'
0000000004a3c4c0 T torch::jit::toString[abi:cxx11](torch::jit::StrideInput)
00000000047ea700 T torch::jit::Graph::toString[abi:cxx11](bool) const
00000000047c5cd0 T torch::jit::AliasDb::toString[abi:cxx11]() const 
</code></pre>
<p>Effectively, for us, the issue stemmed from the libtorch bindings targeting the cxx11 abi but the packaged pytorch releases not.</p>
<p>We were able to resolve this issue by following the steps at <a href="https://github.com/pytorch/pytorch?tab=readme-ov-file#install-pytorch" class="inline-onebox" rel="noopener nofollow ugc">GitHub - pytorch/pytorch: Tensors and Dynamic neural networks in Python with strong GPU acceleration</a> to recompile pytorch with the cxx11 abi enabled.</p>