Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@akaanirban
Last active March 22, 2022 00:40
Show Gist options
  • Star 26 You must be signed in to star a gist
  • Fork 7 You must be signed in to fork a gist
  • 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)
@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