Linking error due to using libtorch and pytorch in same project

Hi all,

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.

LibTorch is linked like so in the CMakeLists.txt
target_link_libraries(${CPP_Library} PUBLIC ${TORCH_LIBRARIES})

I also set up a python virtual environment where I install PyTorch like so:
pip install torch

In Python, when I call import torch, I get the linking error:

from torch._C import * # noqa: F403
ImportError: /home/<path-to>/venv/lib/python3.8/site-packages/torch-1.10.2-py3.8-linux-x86_64.egg/torch/lib/ undefined symbol: _ZNK5torch3jit5Graph8toStringEb

Both pytorch and libtorch are the same version 10.2 (CUDA).

Is there a recommended way of using both libtorch and pytorch in the same project?

Thank you!