Skip to content

Instantly share code, notes, and snippets.

@akaanirban
Last active March 22, 2022 00:40
Show Gist options
  • Save akaanirban/621e63237e63bb169126b537d7a1d979 to your computer and use it in GitHub Desktop.
Save akaanirban/621e63237e63bb169126b537d7a1d979 to your computer and use it in GitHub Desktop.
Install pyTorch in Raspberry Pi 4 (or any other)

Edit 04/11/2021: This gist is quite old now. The current version of PyTorch is 1.8.1, which is miles ahead of version 1.0.1 that I was trying to install when I wrote this gist. Therefore some of the instructions may not apply, or some dependencies may have changed or bugs taken care of. I do not currently have a Raspberry Pi to verify unfortunately. Please proceed with caution. Further, there are may others who have shared their fixes, and direct links to their wheels down in the comments. Cheers !

Install the prerequisites (the last one for numpy):

sudo apt install libopenblas-dev libblas-dev m4 cmake cython python3-yaml libatlas-base-dev

Increase the swap size:

  • Stop the swap : sudo dphys-swapfile swapoff
  • Modify the size of the swap by editing as root the following file : /etc/dphys-swapfile. Modify the valiable CONF_SWAPSIZE and change its value to CONF_SWAPSIZE=2048
  • Run following from command prompt: dphys-swapfile setup to update the changes.
  • Start the swap back again: sudo dphys-swapfile swapon
  • Check if the changes have taken place: free -m

For raspberry pi 4 there may be an issue with the Gcc and G++ version. Install an older version ans use it

apt-get install gcc-4.9 g++-4.9

Set the environment variables:

  export CC=gcc-4.9
  export CXX=g++-4.9
  export USE_CUDA=0
  export USE_MKLDNN=0
  export USE_NNPACK=0
  export USE_QNNPACK=0
  export USE_NUMPY=1
  export USE_DISTRIBUTED=0
  export NO_CUDA=1
  export NO_DISTRIBUTED=1
  export NO_MKLDNN=1 
  export NO_NNPACK=1
  export NO_QNNPACK=1
  export ONNX_ML=1 ## this is extremely important otherwise you will run into onnx_pb.h error. 
  # other workaround is edit the onnx_pb.h file and add #define ONNX_ML 1 inside it to skip

Download pyTorch:

git clone --recursive https://github.com/pytorch/pytorch
cd pytorch
  • Optionally if you want to install a specific branch e.g. I want to install v1.0.1 because the torchjit is not broken in that branch:
git checkout v1.0.1

Build torch:

sudo -E python3 setup.py build
sudo -E python3 setup.py install

Life Hack : Always build a wheel and keep it somewhere safe because you have already build from source.

sudo -E python3 setup.py bdist_wheel

The .whl file will be in pytorch/dist folder.

There can be multiple errors. The most irritating one is `fatal error: onnx/onnx.pb.h: No such file or directory

compilation terminated.This is a [bug](https://github.com/onnx/onnx/issues/1947) which is recent. That is why it is imperative to set the flagONNX_ML=, this bypasses the onnx_pb.hfile search. The other hard coded way would be to manually edit the fileonnx_pb.hand set# DEFINE ONNX_ML 1` at the beginning. This is a hack, other features might break because of this.

Everytime you face an error do python3 setup.py clean --all to clean up the corrupted builds.

If you face some procol buffer problem, do this git submodule update --remote third_party/protobuf (pytorch/pytorch#22564 (comment))

Check successful installation:

cd 
python3
>>> import torch
>>> import numpy as np
>>> a = torch.from_numpy(np.random.randn(1, 100))
>>> print(a)
@MR901
Copy link

MR901 commented Feb 17, 2021

If someone wants the compiled whl file version 1.9 for raspberrypi4. link

@pifparfait
Copy link

Hi all, I got this issue from trying to install torch to my raspberry Pi 4, following these steps. Many thanks for any help.

@akaanirban
[ 72%] Building CXX object caffe2/CMakeFiles/conv_to_nnpack_transform_test.dir/transforms/conv_to_nnpack_transform_test.cc.o
[ 73%] Building CXX object caffe2/CMakeFiles/depthwise3x3_conv_op_test.dir/share/contrib/depthwise/depthwise3x3_conv_op_test.cc.o
[ 73%] Building CXX object caffe2/CMakeFiles/pattern_net_transform_test.dir/transforms/pattern_net_transform_test.cc.o
[ 73%] Building CXX object caffe2/CMakeFiles/caffe2_pybind11_state.dir/python/pybind_state.cc.o
In file included from /tmp/pytorch/caffe2/onnx/helper.h:4:0,
from /tmp/pytorch/caffe2/onnx/backend.h:5,
from /tmp/pytorch/caffe2/python/pybind_state.cc:19:
/tmp/pytorch/third_party/onnx/onnx/onnx_pb.h:52:26: fatal error: onnx/onnx.pb.h: No such file or directory
#include "onnx/onnx.pb.h"
^
compilation terminated.
make[2]: *** [caffe2/CMakeFiles/caffe2_pybind11_state.dir/build.make:63: caffe2/CMakeFiles/caffe2_pybind11_state.dir/python/pybind_state.cc.o] Error 1
make[1]: *** [CMakeFiles/Makefile2:2802: caffe2/CMakeFiles/caffe2_pybind11_state.dir/all] Error 2
make[1]: *** Waiting for unfinished jobs....
[ 73%] Linking CXX executable ../bin/conv_to_nnpack_transform_test
[ 73%] Built target conv_to_nnpack_transform_test
[ 73%] Linking CXX executable ../bin/depthwise3x3_conv_op_test
[ 73%] Built target depthwise3x3_conv_op_test
[ 73%] Linking CXX executable ../bin/pattern_net_transform_test
[ 73%] Built target pattern_net_transform_test
make: *** [Makefile:141: all] Error 2
Failed to run 'bash ../tools/build_pytorch_libs.sh caffe2'

@akaanirban
Copy link
Author

@pifparfait Sorry, this gist is probably very outdated, since the current pytorch is many many versions ahead. There are many other comments in this page of other developers who have kindly shared the links to their compiled versions which you can probably directly used. Can you check those? Perhaps the one from here: https://gist.github.com/akaanirban/621e63237e63bb169126b537d7a1d979#gistcomment-3634301

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment