我在Linux系统上有Miniconda3(Ubuntu 22.04)。该环境具有Python 3.10以及Pytorch的功能(按照Python)安装(按照官方说明安装)。
我想设置使用Pytorch C ++ API的CMAKE项目。原因并不重要,我也知道它是beta(官方文档指出),因此不排除不稳定和重大变化。
目前,我有这个非常最小的 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
当我尝试配置项目时,我会遇到错误:
cmakelists.txt上的cmake错误:21(消息):拨打电话
不正确的参数
- 找不到ProtoBuf(缺少:ProtoBuf_libraries Protobuf_include_dir)
- 找到线程:true Cmake警告,请注意/home/user/miniconda/envs/ml/lib/python3.10/site-packages/torch/share/share/cmake/caffe2/public/protic/protobuf.cmake.cmake:88888888888888888
(消息):找不到Protobuf。取决于您是否是
构建caffe2或caffe2依赖图书馆,下一个警告 /
错误将为您提供更多信息。致电Stack(最新通话首先):
/home/user/miniconda/envs/ml/lib/python3.10/site-packages/torch/share/cmake/caffe2/caffe2config.cmake:56
(包括)
/home/user/miniconda/envs/ml/lib/python3.10/site-packages/torch/share/cmake/torch/torch/torchconfig.cmake:68
(find_package)cmakelists.txt:23(find_package)
cmake错误
/home/user/miniconda/envs/ml/lib/python3.10/site-packages/torch/share/cmake/caffe2/caffe2config.cmake:58
(消息):您已安装的Caffe2版本使用Protobuf,但
找不到Protobuf库。你不小心删除它吗?
还是您设置了正确的cmake_prefix_path?如果你没有
Protobuf,您需要安装ProtoBuf并设置库路径
因此。致电堆栈(首先是最近的电话):
/home/user/miniconda/envs/ml/lib/python3.10/site-packages/torch/share/cmake/torch/torch/torchconfig.cmake:68
(find_package)cmakelists.txt:23(find_package)
我安装了 libprotobuf
(再次通过 conda
),但是,虽然我可以找到库文件,但我找不到任何<代码>*ProtoBufConfig.cmake 或与Protobuf及其CMake设置有关的任何内容。
在我与风厂作斗争之前,我想在这里询问适当的设置。我猜想从源头构建始终是一种选择,但是这将对我合作的人构成巨大的开销。
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)
作为使用
conda
的替代方法,我建议只使用链接 https://download.pytorch.org/libtorch/limbtorch/nightly/cpu/cpu/cpu/libtorch-shared-with-with-with-with-with-with-deps-latest.zip.zip.zip unzipping unzipping unzipping这将为您提供一个文件夹libtorch
,您可以将其放在cmakelists.txt旁边。这样,您需要在cmake中拥有的一切(请参阅在这里) :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:使用此conda env:
我从 torch in
site-packages
,以及环境的lib
文件夹)。编写CMAKE文件,以便自动找到文件夹:example-app.cpp.cpp
cmakelists.txt :
精确的环境(如果可重复性存在问题):
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):