評價此頁

安裝 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 是透過 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 可執行檔案,現在應該會愉快地打印出 tensor(確切輸出可能因隨機性而異)。

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 的順利安裝和使用,請確保您的系統滿足以下要求:

  1. GLIBC 版本:

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

  1. GCC 版本:

  • cxx11 需要 GCC 9 或更高版本。

Visual Studio 擴充套件#

LibTorch 專案模板 可以幫助 Windows 開發者設定所有 libtorch 專案設定和除錯/釋出連結選項。它易於使用,您還可以檢視演示影片。唯一的先決條件是到 https://pytorch.com.tw 下載 libtorch。

支援#

如果您在此安裝和最小使用指南中遇到任何問題,請使用我們的論壇GitHub issue與我們聯絡。