為此頁評分

安裝 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.com.tw 的連結選擇器中找到正確的連結。

如果您是 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.com.tw 下載 libtorch。

支援#

如果您在使用本安裝和最小使用指南時遇到任何問題,請透過我們的論壇GitHub issues 聯絡我們。