安装 PyTorch C++ 发行版¶
我们提供了所有依赖 PyTorch 所需的头文件、库和 CMake 配置文件的二进制发行版。我们将此发行版称为 LibTorch,您可以在我们的网站上下载包含最新 LibTorch 发行版的 ZIP 压缩包。下面是一个小例子,展示了如何编写一个依赖 LibTorch 并使用 torch::Tensor
类的最小应用程序,该类随 PyTorch C++ API 一起提供。
最小示例¶
第一步是通过上面的链接下载 LibTorch ZIP 压缩包。例如
wget https://download.pytorch.org/libtorch/nightly/cpu/libtorch-shared-with-deps-latest.zip
unzip libtorch-shared-with-deps-latest.zip
请注意,上面的链接是仅包含 CPU 的 libtorch。如果您想下载启用 GPU 的 libtorch,请在 https://pytorch.ac.cn 上的链接选择器中找到正确的链接
如果您是 Windows 开发者并且不想使用 CMake,您可以跳转到 Visual Studio 扩展部分。
接下来,我们可以编写一个最小的 CMake 构建配置,以开发一个依赖 LibTorch 的小型应用程序。CMake 不是使用 LibTorch 的硬性要求,但它是推荐和官方认可的构建系统,并且在未来将得到良好的支持。最基本的 CMakeLists.txt 文件可能如下所示
cmake_minimum_required(VERSION 3.18 FATAL_ERROR)
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 17)
# The following code block is suggested to be used on Windows.
# According to https://github.com/pytorch/pytorch/issues/25457,
# the DLLs need to be copied to avoid memory errors.
if (MSVC)
file(GLOB TORCH_DLLS "${TORCH_INSTALL_PREFIX}/lib/*.dll")
add_custom_command(TARGET example-app
POST_BUILD
COMMAND ${CMAKE_COMMAND} -E copy_if_different
${TORCH_DLLS}
$<TARGET_FILE_DIR:example-app>)
endif (MSVC)
我们的示例的实现将简单地创建一个新的 torch::Tensor 并打印它
#include <torch/torch.h>
#include <iostream>
int main() {
torch::Tensor tensor = torch::rand({2, 3});
std::cout << tensor << std::endl;
}
虽然您可以包含更细粒度的头文件来仅访问 PyTorch C++ API 的部分内容,但包含 torch/torch.h 是最可靠的方式,可以包含其大部分功能。
最后一步是构建应用程序。为此,假设我们的示例目录布局如下所示
example-app/
CMakeLists.txt
example-app.cpp
我们现在可以运行以下命令,从 example-app/
文件夹中构建应用程序
mkdir build
cd build
cmake -DCMAKE_PREFIX_PATH=/absolute/path/to/libtorch ..
cmake --build . --config Release
其中 /absolute/path/to/libtorch
应该是解压后的 LibTorch 发行版的绝对路径(!)。如果 PyTorch 是通过 conda 或 pip 安装的,则可以使用 torch.utils.cmake_prefix_path 变量查询 CMAKE_PREFIX_PATH。在这种情况下,CMake 配置步骤将如下所示
cmake -DCMAKE_PREFIX_PATH=`python3 -c 'import torch;print(torch.utils.cmake_prefix_path)'` ..
如果一切顺利,它看起来会像这样
root@4b5a67132e81:/example-app# mkdir build
root@4b5a67132e81:/example-app# cd build
root@4b5a67132e81:/example-app/build# cmake -DCMAKE_PREFIX_PATH=/path/to/libtorch ..
-- The C compiler identification is GNU 5.4.0
-- The CXX compiler identification is GNU 5.4.0
-- Check for working C compiler: /usr/bin/cc
-- Check for working C compiler: /usr/bin/cc -- works
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Detecting C compile features
-- Detecting C compile features - done
-- Check for working CXX compiler: /usr/bin/c++
-- Check for working CXX compiler: /usr/bin/c++ -- works
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Looking for pthread.h
-- Looking for pthread.h - found
-- Looking for pthread_create
-- Looking for pthread_create - not found
-- Looking for pthread_create in pthreads
-- Looking for pthread_create in pthreads - not found
-- Looking for pthread_create in pthread
-- Looking for pthread_create in pthread - found
-- Found Threads: TRUE
-- Configuring done
-- Generating done
-- Build files have been written to: /example-app/build
root@4b5a67132e81:/example-app/build# cmake --build . --config Release
Scanning dependencies of target example-app
[ 50%] Building CXX object CMakeFiles/example-app.dir/example-app.cpp.o
[100%] Linking CXX executable example-app
[100%] Built target example-app
执行在 build
文件夹中找到的生成的 example-app
二进制文件现在应该愉快地打印张量(确切输出取决于随机性)
root@4b5a67132e81:/example-app/build# ./example-app
0.2063 0.6593 0.0866
0.0796 0.5841 0.1569
[ Variable[CPUFloatType]{2,3} ]
提示
在 Windows 上,调试和发布版本不是 ABI 兼容的。如果您计划在调试模式下构建项目,请尝试 LibTorch 的调试版本。另外,请确保在上面的 cmake --build .
行中指定正确的配置。
系统要求¶
为确保 LibTorch 的顺利安装和使用,请确保您的系统满足以下要求
GLIBC 版本:
GLIBC 2.29 或更新版本,适用于 cxx11 ABI 版本
GLIBC 2.17 或更新版本,适用于 pre-cxx11 ABI 版本
GCC 版本:
GCC 9 或更新版本,适用于 cxx11 和 pre-cxx11 ABI 版本
Visual Studio 扩展¶
LibTorch 项目模板 可以帮助 Windows 开发者设置所有 libtorch 项目设置以及调试和发布版本的链接选项。它易于使用,您可以查看演示视频。唯一的前提条件是在 https://pytorch.ac.cn 上下载 libtorch
支持¶
如果您在使用此安装和最小使用指南时遇到任何问题,请使用我们的论坛或GitHub issues 与我们联系。