使用 CMake 在 MSVC 中使用 Pytorch Cuda c++ 编译 C++/Cuda 扩展时出错
Error Compiling C++/Cuda extension with Pytorch Cuda c++ in MSVC using CMake
我正在尝试按照教程 here, (with instructions how to use pytorch with c++ here) 使用 Pytorch 构建一个 c++/cuda 扩展。我的环境详细信息是:
- 使用 Microsoft Visual Studio 2019 版本 16.6.5
- Windows 10
- libtorch c++ 调试 1.70,从 pytorch 网站安装了 cuda 11.0
我正在使用此 cmake 代码,我在其中设置 python 3.6 的包含目录和 python36.lib
的库
cmake_minimum_required (VERSION 3.8)
project ("DAConvolution")
find_package(Torch REQUIRED)
# Add source to this project's executable.
add_executable (DAConvolution "DAConvolution.cpp" "DAConvolution.h")
include_directories("C:/Users/James/Anaconda3/envs/masters/include")
target_link_libraries(DAConvolution "${TORCH_LIBRARIES}" "C:/Users/James/Anaconda3/envs/masters/libs/python36.lib")
if (MSVC)
file(GLOB TORCH_DLLS "${TORCH_INSTALL_PREFIX}/lib/*.dll")
add_custom_command(TARGET DAConvolution
POST_BUILD
COMMAND ${CMAKE_COMMAND} -E copy_if_different
${TORCH_DLLS}
$<TARGET_FILE_DIR:DAConvolution>)
endif (MSVC)
我将 CMake 命令参数设置为 -DCMAKE_PREFIX_PATH=C:\libtorch(我上面提到的 libtorch 调试路径)。我正在使用 MSVC 版本中的 x64-debug 选项构建(因为使用 x-64 Release 选项构建会给我一个 torch-NOTFOUND 错误)。
The example DAConvolution.cpp file is:
#ifdef _DEBUG
#undef _DEBUG
#include <python.h>
#define _DEBUG
#else
#include <python.h>
#endif
#include <torch/extension.h>
我取消了 _DEBUG 标志的定义,这样链接器就不会查找 python36_d.lib 文件(我没有)。
我收到链接错误:
简单地包含 torch.h 就可以正常工作,但是当我想包含扩展头时,我就会遇到这些问题,因为我相信它使用的是 Pybind 11。非常感谢任何见解。我已尽力包含所有可能的信息,但很乐意提供更多信息。
对于 Windows 和 Visual studio,你最好使用 Visual Studio 而不是 CMake。
只需创建一个简单的控制台应用程序,转到项目的属性,将 Configuration type
更改为 Dynamic Library (dll)
,配置包含和库目录,将所需的条目添加到 [=13] 中的链接器=](例如 torch.lib
、torch_cpu.lib
等),您可以点击构建,如果您已正确完成所有操作,您将获得一个可以使用的 dll(例如加载它使用 Python 中的 torch.classes.load_library
并使用它。
Python 调试版本未随 Anaconda/正常 python 发行版一起提供,但如果您安装 Microsoft Python 发行版,我相信可以从 downloaded/installed Visual Studio 安装程序,可用。
同样从 Python 3.8 开始,我想调试二进制文件也已发布。
如果不是,请参阅 this.
对于 cmake 部分,您可以按照以下内容进行操作。这是我前段时间为 python 扩展制作的我自己的 cmake 中的屠宰版本。
阅读它并根据您自己的要求进行更改它应该是直截了当的:
# NOTE:
# TORCH_LIB_DIRS needs to be set. When calling cmake you can specify them like this:
# cmake -DCMAKE_PREFIX_PATH="somewhere/libtorch/share/cmake" -DTORCH_LIB_DIRS="/somewhere/lib" ..
cmake_minimum_required(VERSION 3.1 FATAL_ERROR)
project(DAConvolution)
find_package(Torch REQUIRED)
# we are using the C++17, if you are not change this or remove it altogether
set(CMAKE_CXX_STANDARD 17)
#define where your headers and libs are, specify for example where your DaConvolution.h resides!
include_directories( somewhere/Yourinclude_dir ${TORCH_INCLUDE_DIRS})
set(DAConvolution_SRC ./DAConvolution.cpp )
LINK_DIRECTORIES(${TORCH_LIB_DIRS})
add_library(
DAConvolution
SHARED
${DAConvolution_SRC}
)
# if you use some custom libs, you previously built, specify its location here
# target_link_directories(DAConvolution PRIVATE somewhere/your_previously_built_stuff/libs)
target_link_libraries(DAConvolution ${TORCH_LIB_DIRS}/libc10.so)
target_link_libraries(DAConvolution ${TORCH_LIB_DIRS}/libtorch_cpu.so)
install(TARGETS DAConvolution LIBRARY DESTINATION lib )
旁注:
我只为 Linux 制作了 cmake,所以在 Windows 下,我总是使用 Visual Studio(准确地说是 2019),就像我之前解释的一样。它是迄今为止最好/最简单的方法恕我直言。适合自己,选择最适合你的问题。
我正在尝试按照教程 here, (with instructions how to use pytorch with c++ here) 使用 Pytorch 构建一个 c++/cuda 扩展。我的环境详细信息是:
- 使用 Microsoft Visual Studio 2019 版本 16.6.5
- Windows 10
- libtorch c++ 调试 1.70,从 pytorch 网站安装了 cuda 11.0
我正在使用此 cmake 代码,我在其中设置 python 3.6 的包含目录和 python36.lib
的库cmake_minimum_required (VERSION 3.8)
project ("DAConvolution")
find_package(Torch REQUIRED)
# Add source to this project's executable.
add_executable (DAConvolution "DAConvolution.cpp" "DAConvolution.h")
include_directories("C:/Users/James/Anaconda3/envs/masters/include")
target_link_libraries(DAConvolution "${TORCH_LIBRARIES}" "C:/Users/James/Anaconda3/envs/masters/libs/python36.lib")
if (MSVC)
file(GLOB TORCH_DLLS "${TORCH_INSTALL_PREFIX}/lib/*.dll")
add_custom_command(TARGET DAConvolution
POST_BUILD
COMMAND ${CMAKE_COMMAND} -E copy_if_different
${TORCH_DLLS}
$<TARGET_FILE_DIR:DAConvolution>)
endif (MSVC)
我将 CMake 命令参数设置为 -DCMAKE_PREFIX_PATH=C:\libtorch(我上面提到的 libtorch 调试路径)。我正在使用 MSVC 版本中的 x64-debug 选项构建(因为使用 x-64 Release 选项构建会给我一个 torch-NOTFOUND 错误)。
The example DAConvolution.cpp file is:
#ifdef _DEBUG
#undef _DEBUG
#include <python.h>
#define _DEBUG
#else
#include <python.h>
#endif
#include <torch/extension.h>
我取消了 _DEBUG 标志的定义,这样链接器就不会查找 python36_d.lib 文件(我没有)。
我收到链接错误:
简单地包含 torch.h 就可以正常工作,但是当我想包含扩展头时,我就会遇到这些问题,因为我相信它使用的是 Pybind 11。非常感谢任何见解。我已尽力包含所有可能的信息,但很乐意提供更多信息。
对于 Windows 和 Visual studio,你最好使用 Visual Studio 而不是 CMake。
只需创建一个简单的控制台应用程序,转到项目的属性,将 Configuration type
更改为 Dynamic Library (dll)
,配置包含和库目录,将所需的条目添加到 [=13] 中的链接器=](例如 torch.lib
、torch_cpu.lib
等),您可以点击构建,如果您已正确完成所有操作,您将获得一个可以使用的 dll(例如加载它使用 Python 中的 torch.classes.load_library
并使用它。
Python 调试版本未随 Anaconda/正常 python 发行版一起提供,但如果您安装 Microsoft Python 发行版,我相信可以从 downloaded/installed Visual Studio 安装程序,可用。
同样从 Python 3.8 开始,我想调试二进制文件也已发布。
如果不是,请参阅 this.
对于 cmake 部分,您可以按照以下内容进行操作。这是我前段时间为 python 扩展制作的我自己的 cmake 中的屠宰版本。
阅读它并根据您自己的要求进行更改它应该是直截了当的:
# NOTE:
# TORCH_LIB_DIRS needs to be set. When calling cmake you can specify them like this:
# cmake -DCMAKE_PREFIX_PATH="somewhere/libtorch/share/cmake" -DTORCH_LIB_DIRS="/somewhere/lib" ..
cmake_minimum_required(VERSION 3.1 FATAL_ERROR)
project(DAConvolution)
find_package(Torch REQUIRED)
# we are using the C++17, if you are not change this or remove it altogether
set(CMAKE_CXX_STANDARD 17)
#define where your headers and libs are, specify for example where your DaConvolution.h resides!
include_directories( somewhere/Yourinclude_dir ${TORCH_INCLUDE_DIRS})
set(DAConvolution_SRC ./DAConvolution.cpp )
LINK_DIRECTORIES(${TORCH_LIB_DIRS})
add_library(
DAConvolution
SHARED
${DAConvolution_SRC}
)
# if you use some custom libs, you previously built, specify its location here
# target_link_directories(DAConvolution PRIVATE somewhere/your_previously_built_stuff/libs)
target_link_libraries(DAConvolution ${TORCH_LIB_DIRS}/libc10.so)
target_link_libraries(DAConvolution ${TORCH_LIB_DIRS}/libtorch_cpu.so)
install(TARGETS DAConvolution LIBRARY DESTINATION lib )
旁注:
我只为 Linux 制作了 cmake,所以在 Windows 下,我总是使用 Visual Studio(准确地说是 2019),就像我之前解释的一样。它是迄今为止最好/最简单的方法恕我直言。适合自己,选择最适合你的问题。