安裝 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