安装 PyTorch C++ API libtorch 及一个最小例子

本文介绍如何安装PyTorch的C++API LibTorch,并提供了一个简单的示例程序,演示如何创建并打印一个torch::Tensor。文章还详细介绍了使用CMake进行构建的过程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

安装 PyTorch C++ API libtorch 及一个最小例子

翻译自:https://pytorch.org/cppdocs/installing.html

我们提供依赖 PyTorch 所需的所有头文件、库和 CMake 配置文件的二进制分发版。我们将此发行版称为 LibTorch,您可以在我们的网站上下载包含最新 LibTorch 发行版的 ZIP 档案。下面是编写一个依赖 LibTorch 并使用torch::TensorPyTorch 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-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(译者注:这里是指如果本机 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-appbuild 文件夹中找到的结果二进制文件现在应该可以打印张量(具体输出受随机性影响):

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值