Skip to content

Instantly share code, notes, and snippets.

@fgolemo
Created March 7, 2018 14:28
Show Gist options
  • Save fgolemo/b973a3fa1aaa67ac61c480ae8440e754 to your computer and use it in GitHub Desktop.
Save fgolemo/b973a3fa1aaa67ac61c480ae8440e754 to your computer and use it in GitHub Desktop.
How to install PyTorch v0.3.1 on RaspberryPi 3B - Tutorial

How to install PyTorch v0.3.1 on RaspberryPi 3B

Prerequisites

Important I'd recommend you use at least a 16GB sd card. I tried it with an 8GB card and it baaaaarely fits. I had to uninstall a lot of packages and regularly clean up.

Go to https://www.raspberrypi.org/downloads/raspbian/ and download the Raspbian Stretch image (either one).

Use Etcher (from https://etcher.io/) to "burn" the image onto the SD card.

Before ejecting the SD card: if you want to use the Raspberry Pi (RPi from here on out) headlessly (without screen plugged in), you can just create an empty text file with the filename ssh on the boot volume of the SD card. This will turn on the ssh server and delete the file on next boot.

Login to your RPi with

# password is raspberry
ssh RASPBERRYPI.LOCAL -l pi

Update everything

sudo apt-get update && sudo apt-get upgrade

Set up SWAP and dependencies

Create a SWAP file of size 2GB

sudo dd if=/dev/zero of=/swap1 bs=1M count=2048

Make this empty file into a swap-compatible file

sudo mkswap /swap1

Then disable the old swap space and enable the new one

sudo nano /etc/fstab

This above command will open a text editor on your /etc/fstab file. You may already have a swap file. If this is the case, your fstab will have a line that looks like this:

/swap0 swap swap

If you have an existing SWAP file

In this line, please change the /swap0 to /swap1.

If you don't have an existing SWAP file

Add the following line at the end of the /etc/fstab file:

/swap1 swap swap

In both cases

Then save the file with CTRL+o and ENTER. Close the editor with CTRL+x.

Now your system knows about the new swap space, and it will change it upon reboot, but if you want to use it right now, without reboot, you can manually turn off and empty the old swap space and enable the new one:

# ONLY USE THIS IF YOU HAVE AN EXISTING SWAP FILE:
sudo swapoff /swap0

sudo swapon /swap1

Install required packages

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

Get the PyTorch code and compile

Export compilation flags

export NO_CUDA=1
# this will disable CUDA components of PyTorch,
# because the little RaspberriPi doesn't have a
# GPU that supports CUDA

export NO_DISTRIBUTED=1
# disable distributed computing...
# unless you want to glue like 100 RPis together
# and make a cluster or something

Move into a download directory (let's say ~/Downloads) and clone the pytorch repo:

cd ~/Downloads
git clone --recursive https://github.com/pytorch/pytorch
cd pytorch

Start the build process and grab a coffee - this took me about 3 hours.

python3 setup.py build

Once this finished (make sure there are no error of course) install the whole shebang

sudo -E python3 setup.py install
# the -E is important here to preserve
# the environmental variables (NO_DISTRIBUTED and NO_CUDA)

Et voila, this should have worked without any errors. To test the installation, please change into a different directory first

cd
python3
>>> import torch
>>> x = torch.rand(5, 3)
>>> print(x)
@vfgeert
Copy link

vfgeert commented Apr 25, 2018

  • @timonbimon The swap is necessary because otherwise you'll get the error "virtual memory exhausted: cannot allocate memory."
  • @briandw Did you do "export NO_DISTRIBUTED=1" beforehand like instructed?

@stocyr
Copy link

stocyr commented May 28, 2018

I had to apply the patch for issue pytorch/pytorch#4472 as well using git cherry-pick -n af8b64a
Working on a RaspberryPi 3B+ with 2018-04-18-raspbian-stretch and also used the dphys-swap system instead of /etc/fstab.

@jinyu121
Copy link

jinyu121 commented Jun 7, 2018

If someone can read Chinese, here is also the solution https://haoyu.love/blog509.html

@manoharvhr
Copy link

Hi, does this work on PYNQ Z1 boards as well?

@manoharvhr
Copy link

https://wormtooth.com/20180617-pytorch-on-raspberrypi/ used this and it worked like a charm

@iainmelvin
Copy link

iainmelvin commented Nov 27, 2018

I needed to add "export NO_MKLDNN=1" too because mkldnn did not support a 32bit OS
It looked like it was necessary. If it isn't, I would like to know so I can get the speedup...

@vovcia
Copy link

vovcia commented Dec 5, 2018

Hint: export MAX_JOBS=2 to limit parallel number of cc1, compilation won't hit the swap and will be 2x faster.

@kawashimaken
Copy link

Wonder if there are any information about how to install PyTorch v1.0.0 to Raspberry Pi.

@NikhilKanda
Copy link

I tried the above process for a couple of times and every time I run the setup.py file, there's an error

error: right shift count >= width of type [-Werror=shift-count-overflow]
CP(32); CP(16); CP(8); CP(4); CP(2); CP(1);

Anyone solution to this issue is greatly appreciated

@NikhilKanda
Copy link

I'm wondering to know if there's a way to cross compile Pytorch on Allwinner platform (AArch64 processor). Leads are greatly appreciated.

@drscotthawley
Copy link

Thanks for sharing this!
In my case, the command to make the swap space,sudo dd if=/dev/zero of=/swap1 bs=1M count=2048, never seemed to terminate.

@bitcores
Copy link

Regarding NO_DISTRIBUTED, just don't bother trying to build it with =0 even if you have a cluster of Pis.
MAYBE, if your Pis are 64bit, it might work but the compile time is considerably longer and you may still have problems with Gloo.

@phil123456
Copy link

  • etcher does not work, it stops , hangs at 11%, while diskimager works fine
  • libatlas-dev has no instalation candidate

@phil123456
Copy link

pytorch undefined reference to __atomic_fetch_add_8
fucking piece of raspbery shit

@codedev99
Copy link

pytorch undefined reference to __atomic_fetch_add_8
fucking piece of raspbery shit

same effing problem!

@codedev99
Copy link

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