πŸ”₯ PyTorch C++ FrontEnd κ°œλ°œν™˜κ²½ λ§₯μ—μ„œ μ„ΈνŒ…

TorchScript μ‚¬μš©λ²•μ„ μ²΄ν¬ν•΄λ³΄λ©΄μ„œ, PyTorch C++ FrontEnd ν™˜κ²½μ„ ꡬ성해볼 ν•„μš”κ°€ μžˆμ—ˆλŠ”λ°, λ§₯ ν™˜κ²½μ—μ„œλŠ” 생각보닀 μ‰½κ²ŒλŠ” 진행이 λ˜μ§€ μ•Šμ•„μ„œ 정리해본닀.

+ 정리해놓고 λ³΄λ‹ˆ ꡉμž₯히 순쑰둭게 μ§„ν–‰ν•œ κ²ƒμ²˜λŸΌ μ“°μ—¬μ Έ 버렸닀. μ΄λ ‡κ²Œ μ¨λ†“μ•˜μ§€λ§Œ μ™œ λΉŒλ“œκ°€ μ•ˆ λŒμ§€β€¦? ν•˜λ©΄μ„œ λͺ‡μ‹œκ°„을 썼닀.

LibTorch λ‹€μš΄λ‘œλ“œ

LibTorchλΆ€ν„° λ‹€μš΄λ‘œλ“œν•΄μ„œ λ‘œμ»¬μ—μ„œ μ‚¬μš©ν•  수 있게 μ„ΈνŒ…ν•΄μ•Ό ν•œλ‹€. LibTorchλ₯Ό λ‘œμ»¬μ—μ„œ ꡬ성할 수 μžˆλŠ” 방법은 두가지가 μžˆλŠ” κ²ƒμœΌλ‘œ λ³΄μ΄λŠ”λ°, ν•˜λ‚˜λŠ” 직접 λΉŒλ“œν•˜λŠ” 것이고, λ‘λ²ˆμ§ΈλŠ” λ‹€μš΄λ‘œλ“œν•˜λŠ” 것이닀. μ²˜μŒμ— MacOSμ—μ„œ linker 였λ₯˜κ°€ λ‚˜μ„œ 직접 λΉŒλ“œλ₯Ό μ‹œλ„ν•΄λ³΄μ•˜λŠ”λ°, 생각보닀 μ‹œκ°„μ΄ 였래 κ±Έλ €μ„œ λ‹€μš΄λ‘œλ“œ ν•˜λŠ” 방법을 μΆ”μ²œν•œλ‹€.

μ²˜μŒμ— ꡉμž₯히 μ‚½μ§ˆμ„ ν–ˆλ˜ 뢀뢄이 PyTorch νŠœν† λ¦¬μ–Όμ—λŠ” 이런 언급이 λ‚˜μ™€μžˆμ§€ μ•Šκ³ , λ°”μ΄λ„ˆλ¦¬λ₯Ό λ°”λ‘œ wget으둜 λ°›μ•„μ„œ μ§„ν–‰ν•˜λ„λ‘ λ‚˜μ™€μžˆλŠ”λ°, 이게 url에 linuxμš©μ΄λΌλŠ” λͺ…μ‹œκ°€ μ—†μ–΄μ„œ ν•œμ°Έ ν—€λ§Έλ‹€.

https://pytorch.orgμ—μ„œ QUICK START LOCALLY μ„Ήμ…˜μ—μ„œ λ‹€μš΄λ‘œλ“œλ₯Ό μ§„ν–‰ν•˜λ©΄ λ˜λŠ”λ°, MacOSμ—μ„œλŠ” CUDAκ°€ μ§€μ›λ˜μ§€ μ•Šμ•„ μ•„λž˜μ²˜λŸΌ λ‚˜μ™€μžˆλ‹€.

MacOS binaries do not support CUDA. Download CPU libtorch here:

https://download.pytorch.org/libtorch/cpu/libtorch-macos-1.3.0.zip

1.3.0 버전을 λ°›μœΌλ €λ©΄ μ•„λž˜μ²˜λŸΌ μ‹œλ„ν•˜λ©΄ λœλ‹€.

wget https://download.pytorch.org/libtorch/cpu/libtorch-macos-1.3.0.zip
unzip libtorch-macos-1.3.0.zip

νŠœν† λ¦¬μ–Ό 진행해보기

λ‹€μš΄λ‘œλ“œ λ°›κ³  λ‚˜μ„œ PyTorch Docs - INSTALLING C++ DISTRIBUTIONS OF PYTORCHλ₯Ό μ°Έκ³ ν•΄μ„œ μ•„λž˜μ™€ 같은 νŒŒμΌμ„ μž‘μ„±ν–ˆλ‹€.

main.cc

#include <torch/torch.h>
#include <iostream>

int main() {
  torch::Tensor tensor = torch::rand({2, 3});
  std::cout << tensor << std::endl;
}

CMakeLists.txt

cmake_minimum_required(VERSION 3.0 FATAL_ERROR)
project(example-project)

find_package(Torch REQUIRED)

add_executable(example-ap main.cc)

target_link_libraries(example-ap "${TORCH_LIBRARIES}")

set_property(TARGET example-ap PROPERTY CXX_STANDARD 11)

그리고 μ•„λž˜μ²˜λŸΌ μ‹€ν–‰ν–ˆλ‹€. (λ‚˜λŠ” libtorchλ₯Ό ~/libs/libtorch에 λ„£μ–΄λ†“μ•˜λ‹€)

$ mkdir build & build
$ cmake -DCMAKE_PREFIX_PATH=~/libs/libtorch  ..
-- The C compiler identification is AppleClang 11.0.0.11000033
-- The CXX compiler identification is AppleClang 11.0.0.11000033
-- Check for working C compiler: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/cc
-- Check for working C compiler: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/cc -- works
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
...
$ make
Scanning dependencies of target server
[ 50%] Building CXX object ....
...
$ ./example-app

???

그런데, Cmake둜 λΉŒλ“œν•˜κ³  μ‹€ν–‰ν•˜λ‹€λ³΄λ©΄ λ§‰νžˆλŠ” 뢀뢄이 μžˆλŠ”λ°, μ•„λž˜μ²˜λŸΌ λœ¨λ©΄μ„œ μ•ˆλœλ‹€.

$ ./example-app
dyld: Library not loaded: @rpath/libiomp5.dylib
  Referenced from: /libs/libtorch/lib/libtorch.dylib
  Reason: image not found

libiomp5.dylib이 μ—†λ‹€λŠ” 것인데 이게 intel/mkl-dnn λ ˆν¬μ§€ν† λ¦¬μ—μ„œ λ‹€μš΄λ‘œλ“œλ°›μ„ 수 μžˆλ‹€. κ΄€λ ¨ μ΄μŠˆλŠ” pytorch/pytorch#14727λ₯Ό 참고히면 될 것 κ°™λ‹€. libiomp5.dylib, libmklml.dylib 두 라이브러리 νŒŒμΌμ„ libtorchκ°€ μžˆλŠ” ν΄λ”μ˜ libμ•ˆμ— λ„£μ–΄μ£Όλ©΄ λœλ‹€.

β€”

μ΄λ ‡κ²Œ κ²°κ΅­ 잘 λŒμ•„κ°„λ‹€.

$ ./example-app
 0.2055  0.5863  0.5734
 0.1759  0.7157  0.3312
[ Variable[CPUFloatType]{2,3} ]

μ°Έκ³ 

October 21, 2019 에 μž‘μ„±
Tags: pytorch