I have Miniconda3 on a Linux system (Ubuntu 22.04). The environment has Python 3.10 as well as a functioning (in Python) installation of PyTorch (installed following official instructions).
I would like to setup a CMake project that uses PyTorch C++ API. The reason is not important and also I am aware that it’s beta (the official documentation states that), so instability and major changes are not excluded.
Currently I have this very minimal CMakeLists.txt
:
cmake_minimum_required(VERSION 3.19) # or whatever version you use
project(PyTorch_Cpp_HelloWorld CXX)
set(PYTORCH_ROOT "/home/$ENV{USER}/miniconda/envs/ML/lib/python3.10/site-packages/torch")
list(APPEND CMAKE_PREFIX_PATH "${PYTORCH_ROOT}/share/cmake/Torch/")
find_package(Torch REQUIRED CONFIG)
...
# Add executable
# Link against PyTorch library
When I try to configure the project I’m getting error:
CMake Error at CMakeLists.txt:21 (message): message called with
incorrect number of arguments— Could NOT find Protobuf (missing: Protobuf_LIBRARIES Protobuf_INCLUDE_DIR)
— Found Threads: TRUE CMake Warning at /home/USER/miniconda/envs/ML/lib/python3.10/site-packages/torch/share/cmake/Caffe2/public/protobuf.cmake:88
(message): Protobuf cannot be found. Depending on whether you are
building Caffe2 or a Caffe2 dependent library, the next warning /
error will give you more info. Call Stack (most recent call first):
/home/USER/miniconda/envs/ML/lib/python3.10/site-packages/torch/share/cmake/Caffe2/Caffe2Config.cmake:56
(include)
/home/USER/miniconda/envs/ML/lib/python3.10/site-packages/torch/share/cmake/Torch/TorchConfig.cmake:68
(find_package) CMakeLists.txt:23 (find_package)CMake Error at
/home/USER/miniconda/envs/ML/lib/python3.10/site-packages/torch/share/cmake/Caffe2/Caffe2Config.cmake:58
(message): Your installed Caffe2 version uses protobuf but the
protobuf library cannot be found. Did you accidentally remove it,
or have you set the right CMAKE_PREFIX_PATH? If you do not have
protobuf, you will need to install protobuf and set the library path
accordingly. Call Stack (most recent call first):
/home/USER/miniconda/envs/ML/lib/python3.10/site-packages/torch/share/cmake/Torch/TorchConfig.cmake:68
(find_package) CMakeLists.txt:23 (find_package)
I installed libprotobuf
(again via conda
) but, while I can find the library files, I can’t find any *ProtobufConfig.cmake
or anything remotely related to protobuf and its CMake setup.
Before I go fight against wind mills I would like to ask here what the proper setup would be. I am guessing building from source is always an option, however this will pose a huge overhead on people, who I collaborate with.
2
Answers
As an alternative to using
conda
, I would suggest to just grab the pre-built library using the link https://download.pytorch.org/libtorch/nightly/cpu/libtorch-shared-with-deps-latest.zip Unzipping that will give you a folderlibtorch
which you can put next to your CMakeLists.txt. With that, all you need to have in your cmake (see here) would be something like this:Using this conda env:
I copied the small example from here and got it to run. The key was to set the correct library directories (both
torch
insite-packages
, but also thelib
folder of the environment). The cmake file is written so that the folders are automatically found :example-app.cpp
CMakeLists.txt:
Exact environment (in case there are problems with reproducibility):