安装 PyTorch C++ API libtorch 及一个最小例子
翻译自:https://pytorch.org/cppdocs/installing.html
我们提供依赖 PyTorch 所需的所有头文件、库和 CMake 配置文件的二进制分发版。我们将此发行版称为 LibTorch,您可以在我们的网站上下载包含最新 LibTorch 发行版的 ZIP 档案。下面是编写一个依赖 LibTorch 并使用torch::Tensor
PyTorch C++ API 附带的类的最小应用程序的小示例。
安装
先安装预编译的 libtorch :
wget https://download.pytorch.org/libtorch/nightly/cpu/libtorch-shared-with-deps-latest.zip
unzip libtorch-shared-with-deps-latest.zip
该链接仅支持 CPU,若要支持 GPU,请到官网找到对应版本
直接解压,就可以了。
最小例子
接下来,我们可以编写一个最小的 CMake 构建配置来开发一个依赖 LibTorch 的小型应用程序。CMake 不是使用 LibTorch 的必须要求,但它是我们推荐的构建系统,并在将来会得到很好的支持。最基本的 CMakeLists.txt 文件可能如下所示:
cmake_minimum_required(VERSION 3.0 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 14)# 以下代码建议在Windows上使用
if (MSVC)file(GLOB TORCH_DLLS "${TORCH_INSTALL_PREFIX}/lib/*.dll")add_custom_command(TARGET example-appPOST_BUILDCOMMAND ${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.txtexample-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(译者注:这里是指如果本机 pip 或 conda 安装过 PyTorch,那可以不用再装 libtorch,因为在 PyTorch 中是有的)。在这种情况下,CMake 配置步骤将如下所示:
cmake -DCMAKE_PREFIX_PATH=`python -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
执行 example-app
在 build
文件夹中找到的结果二进制文件现在应该可以打印张量(具体输出受随机性影响):
root@4b5a67132e81:/example-app/build# ./example-app
0.2063 0.6593 0.0866
0.0796 0.5841 0.1569
[ Variable[CPUFloatType]{2,3} ]
https://pytorch.org/cppdocs/installing.html