TLDR : Compile libtorch in 1.6.0 and that works fine.
How to compile Libtorch for Raspberry and use in my C++ Project ?
Prepare to build
Increase RBPi SWAP
First of all, if you have a Raspberry PI 3 or lower, you need to increase the SWAP, since the build is a RAM eater.
If you have a RBPi 4 or higher with more than 3GB of RAM, skip this step.
Modify the file /etc/dphys-swapfile
:
CONF_SWAPFILE=2048M
Then call the following command to update changes.
sudo dphys-swapfile setup
Install base packages
Install the following packages:
sudo apt install build-essential make cmake git python3-pip libatlas-base-dev
Libtorch needs CMake>=3.15
to be built properly, check cmake version with
cmake --version``
If it's lower than 3.15, follow the following commands to build a newer version and remove the previous one:
wget https://github.com/Kitware/CMake/releases/download/v3.18.0-rc1/cmake-3.18.0-rc1.tar.gz
tar -xzf cmake-3.18.0-rc1.tar.gz
cd cmake<version>
mkdir build
cd build
cmake ..
make
sudo make install
sudo apt remove cmake
sudo ln -s /usr/local/bin/cmake /usr/bin/cmake
sudo ldconfig
Building PyTorch from source to get libtorch backend for ARM
Don't forget to increase the SWAP to 2048M if you don't have 3GB or RAM.
Getting all needed libraries:
sudo apt-get update
sudo apt-get install build-essential tk-dev libncurses5-dev libncursesw5-dev libreadline6-dev libdb5.3-dev libgdbm-dev libsqlite3-dev libssl-dev libbz2-dev libexpat1-dev liblzma-dev zlib1g-dev
Getting PyTorch sources:
git clone --recursive https://github.com/pytorch/pytorch --branch=release/1.6
cd pytorch
Init all the submodules :
git submodule update --init --recursive
git submodule update --remote third_party/protobuf # To prevent a bug I had
Getting all needed libraries:
sudo apt-get update
sudo apt-get install build-essential tk-dev libncurses5-dev libncursesw5-dev libreadline6-dev libdb5.3-dev libgdbm-dev libsqlite3-dev libssl-dev libbz2-dev libexpat1-dev liblzma-dev zlib1g-dev
Getting PyTorch sources:
git clone --recursive https://github.com/pytorch/pytorch --branch=release/1.6
cd pytorch
Setting up environment variables for the build.
Add the following lines to the ~/.bashrc
file.
export NO_CUDA=1
export NO_DISTRIBUTED=1
export NO_MKLDNN=1
export NO_NNPACK=1
export NO_QNNPACK=1
Log in as root, and use the .bashrc file to setup the environment variables
sudo su
source /home/<user>/.bashrc
Install python dependencies
pip3 install setuptools pyyaml numpy
Build and install PyTorch, time to grab a :coffee:, it make take a while.
Don't forget the -E
that forces the environment variables to be used.
sudo -E python3 setup.py install
Check the installation worked:
cd
python3
import torch
torch.__version__
Building your program with Torch
In your CMakeLists.txt
:
cmake_minimum_required(VERSION 2.6)
project(projectName)
set(CMAKE_PREFIX_PATH "/home/pi/pytorch/torch") # Adding the directory where torch as been installed
set(CMAKE_CXX_STANDARD 14) # C14 required to compile Torch
set(CMAKE_CXX_STANDARD_REQUIRED TRUE)
add_compile_definitions(_GLIBCXX_USE_CXX11_ABI=0) # Torch is compiled with CXX11_ABI, so your program needs to be also, or you may have conflicts in some libraries (such as GTest for example)
# Specifying we are using pthread for UNIX systems.
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${TORCH_CXX_FLAGS} -pthread -Wall")
find_package(Torch REQUIRED)
if(NOT Torch_FOUND)
message(FATAL_ERROR "Pytorch Not Found!")
endif(NOT Torch_FOUND)
message(STATUS "Pytorch status :")
message(STATUS " libraries: ${TORCH_LIBRARIES}")
message(STATUS " Torch Flags: ${TORCH_CXX_FLAGS}")
# Program executable
add_executable(projectName <sources>)
target_link_libraries(projectName PRIVATE pthread dl util ${TORCH_LIBRARIES})