How to compile my C++ project against PyTorch source dir?

Hi!

I’ve contributed some small Python fixes/additions to PyTorch and now I’m trying to start with a deeper layer of C++ core.

To do that I’m trying to run my C++ experimental code linked with PyTorch built from sources with debug info enabled. And I got somewhat stuck with getting it built.

My configuration is as following:

At ~/pytorch I have the PyTorch repo cloned from github and successfully compiled.

At ~/projects/my-torch-project I have my own C++ project that is going to use the PyTorch library compiled in ~/pytorch.

E.g. my main.cpp right now is just trying to repeat the example from PyTorch C++ API:

#include <ATen/ATen.h>

at::Tensor a = at::ones({2, 2}, at::kInt);
at::Tensor b = at::randn({2, 2});
auto c = a + b.to(at::kInt);

My question is: how exactly can I address this in my CMakeLists.txt so that my main.cpp gets compiled and linked?

I’m aware of the “USING THE PYTORCH C++ FRONTEND” and their example, yet it looks like that’s not what I need - the example uses the downloaded official package of PyTorch, while I need to use the source base that I can modify and recompile many times in a row.

Thank you!

I assume you’ve build PyTorch in the ~/pytorch folder and installed it via pip or conda?
In that case, could you try to use:

cmake -DCMAKE_PREFIX_PATH=`python -c 'import torch;print(torch.utils.cmake_prefix_path)'`

instead of the absolute path to the downloaded libtorch?
The small Python inline script should return the path to your locally built cmake files.

1 Like