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 !
sudo apt install libopenblas-dev libblas-dev m4 cmake cython python3-yaml libatlas-base-dev
- 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 valiableCONF_SWAPSIZE
and change its value toCONF_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
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
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
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 flag
ONNX_ML=, this bypasses the
onnx_pb.hfile search. The other hard coded way would be to manually edit the file
onnx_pb.hand set
# DEFINE ONNX_ML 1` at the beginning. This is a hack, other features might break because of this.
If you face some procol buffer problem, do this git submodule update --remote third_party/protobuf
(pytorch/pytorch#22564 (comment))
cd
python3
>>> import torch
>>> import numpy as np
>>> a = torch.from_numpy(np.random.randn(1, 100))
>>> print(a)
@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