为此页评分

安装 PyTorch C++ 分发版#

我们提供了依赖于 PyTorch 所需的所有头文件、库和 CMake 配置文件的二进制分发版。我们将此分发版称为 LibTorch,您可以从我们的网站下载包含最新 LibTorch 分发版的 ZIP 压缩包。下面是一个编写依赖于 LibTorch 并使用 PyTorch C++ API 附带的 torch::Tensor 类的最小示例。

最小示例#

第一步是点击上面的链接下载 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 上,debug 和 release 构建不是 ABI 兼容的。如果您打算在 debug 模式下构建项目,请尝试使用 debug 版本的 LibTorch。另外,请确保在上面的 cmake --build . 命令行中指定正确的配置。

系统要求#

为确保 LibTorch 的顺利安装和使用,请确保您的系统满足以下要求:

  1. GLIBC 版本:

  • cxx11 ABI 版本需要 GLIBC 2.29 或更高版本

  1. GCC 版本:

  • cxx11 需要 GCC 9 或更高版本

Visual Studio 扩展#

LibTorch 项目模板 可以帮助 Windows 开发者为 debug 和 release 配置所有 libtorch 项目设置和链接选项。它易于使用,您可以查看演示视频。唯一的前提条件是从 https://pytorch.ac.cn 下载 libtorch。

支持#

如果您在使用本安装和最小使用指南时遇到任何问题,请通过我们的论坛GitHub issues 联系我们。