Intel 맥에다가 libtorch를 설치해보려했지만 실패.
기왕지사 docker로 ubuntu 컨테이너를 열어서 해보기로 한다.
docker run --it ubuntu:20.04
그동안 당연하게 사용했던 여러가지 command들은 당연하게 작동하지 않는다.
기능들을 다운로드 하기도 이전에, apt-get update를 해주어야 install을 할 수 있게된다.
사소한 기능들의 소중함을 제대로 느낀다.
apt-get update
apt-get install software-properties-common
apt-get install -y wget unzip vim build-essential automake cmake cmake-gui
home에 가서 wget libtorch도 다운로드받아주고 unzip으로 신나게 압축해제한다.
cd /home
wget https://download.pytorch.org/libtorch/cpu/libtorch-cxx11-abi-shared-with-deps-2.0.1%2Bcpu.zip
unzip libtorch-cxx11-abi-shared-with-deps-2.0.1+cpu.zip
libtorch 디렉토리에 들어가서 CMakeLists.txt를 작성한다.
cd /libtorch
vim CMakeLists.txt
cmake_minimum_required(VERSION 3.0 FATAL_ERROR)
set(NAME example)
project(${NAME})
find_package(Torch REQUIRED)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${TORCH_CXX_FLAGS}")
add_executable(${NAME} main.cpp)
target_link_libraries(${NAME} "${TORCH_LIBRARIES}")
set_property(TARGET ${NAME} PROPERTY CXX_STANDARD 14)
# 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
${NAME}
POST_BUILD
COMMAND ${CMAKE_COMMAND} -E copy_if_different
${TORCH_DLLS}
$<TARGET_FILE_DIR:${NAME}>
)
endif (MSVC)
CMakeLists.txt 의 NAME에 맞추어 example 디렉토리도 만들고 그 안에 main.cpp 파일도 만든다.
최종적인 구조를 살펴보자면 아래와 같은 형태로 파일들의 경로를 맞춰줄 것이다.
mkdir example
cd example
vim main.cpp
#include <torch/torch.h>
#include <iostream>
int main() {
torch::Tensor tensor = torch::rand({1,3,32,32});
std::cout << tensor << std::endl;
return 0;
}
// 현재 /example 디렉토리 안
// cmake -DCMAKE_PREFIX_PATH={libtorch 설치된 경로} ..
cmake -DCMAKE_PREFIX_PATH=/home/libtorch ..
cmake --build . --config Release
./example
야무지게 잘되는 것을 확인!