如何设置CMAKE项目使用Pytorch C++通过Conda安装的API

发布于 2025-02-05 07:50:37 字数 1913 浏览 2 评论 0 原文

我在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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

浅黛梨妆こ 2025-02-12 07:50:37

作为使用 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中拥有的一切(请参阅在这里) :

project(example-app)

find_package(Torch REQUIRED)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${TORCH_CXX_FLAGS}")

add_executable(example-app example-app.cpp)
target_link_libraries(example-app "${TORCH_LIBRARIES}")
set_property(TARGET example-app PROPERTY CXX_STANDARD 14)

list(APPEND CMAKE_PREFIX_PATH "libtorch")

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 folder libtorch 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:

project(example-app)

find_package(Torch REQUIRED)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${TORCH_CXX_FLAGS}")

add_executable(example-app example-app.cpp)
target_link_libraries(example-app "${TORCH_LIBRARIES}")
set_property(TARGET example-app PROPERTY CXX_STANDARD 14)

list(APPEND CMAKE_PREFIX_PATH "libtorch")
荒路情人 2025-02-12 07:50:37

使用此conda env:

name: pytorch_latest
channels:
  - pytorch
  - conda-forge
  - defaults
dependencies:
  - pytorch=1.11.0
  - torchvision
  - torchaudio
  - cpuonly

我从 torch in site-packages ,以及环境的 lib 文件夹)。编写CMAKE文件,以便自动找到文件夹:

example-app.cpp.cpp

#include <torch/torch.h>
#include <iostream>

int main() {
  torch::Tensor tensor = torch::rand({2, 3});
  std::cout << tensor << std::endl;
}

cmakelists.txt

cmake_minimum_required(VERSION 3.0 FATAL_ERROR)
project(example-app)

#Add the torch library directory
list(APPEND CMAKE_PREFIX_PATH "$ENV{CONDA_PREFIX}/lib/python3.10/site-packages/torch")

#This is needed to be able to find the mkl and other dependent libraries
link_directories("$ENV{CONDA_PREFIX}/lib")
set(ENV{MKLROOT} "$ENV{CONDA_PREFIX}/lib")
find_package(Torch REQUIRED)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${TORCH_CXX_FLAGS}")

add_executable(example-app example-app.cpp)

#We need to add pthread and omp manually here
target_link_libraries(example-app "${TORCH_LIBRARIES}" pthread omp)

set_property(TARGET example-app PROPERTY CXX_STANDARD 14)

精确的环境(如果可重复性存在问题):

name: pytorch_latest
channels:
  - pytorch
  - conda-forge
  - defaults
dependencies:
  - _libgcc_mutex=0.1=conda_forge
  - _openmp_mutex=4.5=2_kmp_llvm
  - blas=2.115=mkl
  - blas-devel=3.9.0=15_linux64_mkl
  - brotlipy=0.7.0=py310h5764c6d_1004
  - bzip2=1.0.8=h7f98852_4
  - ca-certificates=2022.5.18.1=ha878542_0
  - certifi=2022.5.18.1=py310hff52083_0
  - cffi=1.15.0=py310h0fdd8cc_0
  - charset-normalizer=2.0.12=pyhd8ed1ab_0
  - cpuonly=2.0=0
  - cryptography=37.0.1=py310h9ce1e76_0
  - ffmpeg=4.3=hf484d3e_0
  - freetype=2.10.4=h0708190_1
  - giflib=5.2.1=h36c2ea0_2
  - gmp=6.2.1=h58526e2_0
  - gnutls=3.6.13=h85f3911_1
  - idna=3.3=pyhd8ed1ab_0
  - jpeg=9e=h166bdaf_1
  - lame=3.100=h7f98852_1001
  - lcms2=2.12=hddcbb42_0
  - ld_impl_linux-64=2.36.1=hea4e1c9_2
  - lerc=3.0=h9c3ff4c_0
  - libblas=3.9.0=15_linux64_mkl
  - libcblas=3.9.0=15_linux64_mkl
  - libdeflate=1.10=h7f98852_0
  - libffi=3.4.2=h7f98852_5
  - libgcc-ng=12.1.0=h8d9b700_16
  - libgfortran-ng=12.1.0=h69a702a_16
  - libgfortran5=12.1.0=hdcd56e2_16
  - libiconv=1.17=h166bdaf_0
  - liblapack=3.9.0=15_linux64_mkl
  - liblapacke=3.9.0=15_linux64_mkl
  - libnsl=2.0.0=h7f98852_0
  - libpng=1.6.37=h21135ba_2
  - libstdcxx-ng=12.1.0=ha89aaad_16
  - libtiff=4.4.0=h0fcbabc_0
  - libuuid=2.32.1=h7f98852_1000
  - libuv=1.43.0=h7f98852_0
  - libwebp=1.2.2=h3452ae3_0
  - libwebp-base=1.2.2=h7f98852_1
  - libxcb=1.13=h7f98852_1004
  - libzlib=1.2.12=h166bdaf_0
  - llvm-openmp=14.0.4=he0ac6c6_0
  - lz4-c=1.9.3=h9c3ff4c_1
  - mkl=2022.1.0=h84fe81f_915
  - mkl-devel=2022.1.0=ha770c72_916
  - mkl-include=2022.1.0=h84fe81f_915
  - ncurses=6.3=h27087fc_1
  - nettle=3.6=he412f7d_0
  - numpy=1.22.4=py310h4ef5377_0
  - openh264=2.1.1=h780b84a_0
  - openjpeg=2.4.0=hb52868f_1
  - openssl=3.0.3=h166bdaf_0
  - pillow=9.1.1=py310he619898_1
  - pip=22.1.2=pyhd8ed1ab_0
  - pthread-stubs=0.4=h36c2ea0_1001
  - pycparser=2.21=pyhd8ed1ab_0
  - pyopenssl=22.0.0=pyhd8ed1ab_0
  - pysocks=1.7.1=py310hff52083_5
  - python=3.10.4=h2660328_0_cpython
  - python_abi=3.10=2_cp310
  - pytorch=1.11.0=py3.10_cpu_0
  - pytorch-mutex=1.0=cpu
  - readline=8.1=h46c0cb4_0
  - requests=2.27.1=pyhd8ed1ab_0
  - setuptools=62.3.2=py310hff52083_0
  - sqlite=3.38.5=h4ff8645_0
  - tbb=2021.5.0=h924138e_1
  - tk=8.6.12=h27826a3_0
  - torchaudio=0.11.0=py310_cpu
  - torchvision=0.12.0=py310_cpu
  - typing_extensions=4.2.0=pyha770c72_1
  - tzdata=2022a=h191b570_0
  - urllib3=1.26.9=pyhd8ed1ab_0
  - wheel=0.37.1=pyhd8ed1ab_0
  - xorg-libxau=1.0.9=h7f98852_0
  - xorg-libxdmcp=1.1.3=h7f98852_0
  - xz=5.2.5=h516909a_1
  - zlib=1.2.12=h166bdaf_0
  - zstd=1.5.2=h8a70e8d_1

Using this conda env:

name: pytorch_latest
channels:
  - pytorch
  - conda-forge
  - defaults
dependencies:
  - pytorch=1.11.0
  - torchvision
  - torchaudio
  - cpuonly

I copied the small example from here and got it to run. The key was to set the correct library directories (both torch in site-packages, but also the lib folder of the environment). The cmake file is written so that the folders are automatically found :

example-app.cpp

#include <torch/torch.h>
#include <iostream>

int main() {
  torch::Tensor tensor = torch::rand({2, 3});
  std::cout << tensor << std::endl;
}

CMakeLists.txt:

cmake_minimum_required(VERSION 3.0 FATAL_ERROR)
project(example-app)

#Add the torch library directory
list(APPEND CMAKE_PREFIX_PATH "$ENV{CONDA_PREFIX}/lib/python3.10/site-packages/torch")

#This is needed to be able to find the mkl and other dependent libraries
link_directories("$ENV{CONDA_PREFIX}/lib")
set(ENV{MKLROOT} "$ENV{CONDA_PREFIX}/lib")
find_package(Torch REQUIRED)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${TORCH_CXX_FLAGS}")

add_executable(example-app example-app.cpp)

#We need to add pthread and omp manually here
target_link_libraries(example-app "${TORCH_LIBRARIES}" pthread omp)

set_property(TARGET example-app PROPERTY CXX_STANDARD 14)

Exact environment (in case there are problems with reproducibility):

name: pytorch_latest
channels:
  - pytorch
  - conda-forge
  - defaults
dependencies:
  - _libgcc_mutex=0.1=conda_forge
  - _openmp_mutex=4.5=2_kmp_llvm
  - blas=2.115=mkl
  - blas-devel=3.9.0=15_linux64_mkl
  - brotlipy=0.7.0=py310h5764c6d_1004
  - bzip2=1.0.8=h7f98852_4
  - ca-certificates=2022.5.18.1=ha878542_0
  - certifi=2022.5.18.1=py310hff52083_0
  - cffi=1.15.0=py310h0fdd8cc_0
  - charset-normalizer=2.0.12=pyhd8ed1ab_0
  - cpuonly=2.0=0
  - cryptography=37.0.1=py310h9ce1e76_0
  - ffmpeg=4.3=hf484d3e_0
  - freetype=2.10.4=h0708190_1
  - giflib=5.2.1=h36c2ea0_2
  - gmp=6.2.1=h58526e2_0
  - gnutls=3.6.13=h85f3911_1
  - idna=3.3=pyhd8ed1ab_0
  - jpeg=9e=h166bdaf_1
  - lame=3.100=h7f98852_1001
  - lcms2=2.12=hddcbb42_0
  - ld_impl_linux-64=2.36.1=hea4e1c9_2
  - lerc=3.0=h9c3ff4c_0
  - libblas=3.9.0=15_linux64_mkl
  - libcblas=3.9.0=15_linux64_mkl
  - libdeflate=1.10=h7f98852_0
  - libffi=3.4.2=h7f98852_5
  - libgcc-ng=12.1.0=h8d9b700_16
  - libgfortran-ng=12.1.0=h69a702a_16
  - libgfortran5=12.1.0=hdcd56e2_16
  - libiconv=1.17=h166bdaf_0
  - liblapack=3.9.0=15_linux64_mkl
  - liblapacke=3.9.0=15_linux64_mkl
  - libnsl=2.0.0=h7f98852_0
  - libpng=1.6.37=h21135ba_2
  - libstdcxx-ng=12.1.0=ha89aaad_16
  - libtiff=4.4.0=h0fcbabc_0
  - libuuid=2.32.1=h7f98852_1000
  - libuv=1.43.0=h7f98852_0
  - libwebp=1.2.2=h3452ae3_0
  - libwebp-base=1.2.2=h7f98852_1
  - libxcb=1.13=h7f98852_1004
  - libzlib=1.2.12=h166bdaf_0
  - llvm-openmp=14.0.4=he0ac6c6_0
  - lz4-c=1.9.3=h9c3ff4c_1
  - mkl=2022.1.0=h84fe81f_915
  - mkl-devel=2022.1.0=ha770c72_916
  - mkl-include=2022.1.0=h84fe81f_915
  - ncurses=6.3=h27087fc_1
  - nettle=3.6=he412f7d_0
  - numpy=1.22.4=py310h4ef5377_0
  - openh264=2.1.1=h780b84a_0
  - openjpeg=2.4.0=hb52868f_1
  - openssl=3.0.3=h166bdaf_0
  - pillow=9.1.1=py310he619898_1
  - pip=22.1.2=pyhd8ed1ab_0
  - pthread-stubs=0.4=h36c2ea0_1001
  - pycparser=2.21=pyhd8ed1ab_0
  - pyopenssl=22.0.0=pyhd8ed1ab_0
  - pysocks=1.7.1=py310hff52083_5
  - python=3.10.4=h2660328_0_cpython
  - python_abi=3.10=2_cp310
  - pytorch=1.11.0=py3.10_cpu_0
  - pytorch-mutex=1.0=cpu
  - readline=8.1=h46c0cb4_0
  - requests=2.27.1=pyhd8ed1ab_0
  - setuptools=62.3.2=py310hff52083_0
  - sqlite=3.38.5=h4ff8645_0
  - tbb=2021.5.0=h924138e_1
  - tk=8.6.12=h27826a3_0
  - torchaudio=0.11.0=py310_cpu
  - torchvision=0.12.0=py310_cpu
  - typing_extensions=4.2.0=pyha770c72_1
  - tzdata=2022a=h191b570_0
  - urllib3=1.26.9=pyhd8ed1ab_0
  - wheel=0.37.1=pyhd8ed1ab_0
  - xorg-libxau=1.0.9=h7f98852_0
  - xorg-libxdmcp=1.1.3=h7f98852_0
  - xz=5.2.5=h516909a_1
  - zlib=1.2.12=h166bdaf_0
  - zstd=1.5.2=h8a70e8d_1
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文