Skip to content

Instantly share code, notes, and snippets.

@awwong1
Last active March 27, 2024 20:52
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save awwong1/8728f03cef08e057bd9ae1375a97d798 to your computer and use it in GitHub Desktop.
Save awwong1/8728f03cef08e057bd9ae1375a97d798 to your computer and use it in GitHub Desktop.
PyTorch Installation for Raspberry Pi 3B

PyTorch Installation for Raspberry Pi 3B

Updated version of gist.github.com/fgolemo/b973a3fa1aaa67ac61c480ae8440e754 for my personal build.

Baseline configuration:

  • $ cat /etc/debian_version
10.1
  • $ uname -a
Linux pi 4.19.75-v7+ #1270 SMP Tue Sep 24 18:45:11 BST 2019 armv7l GNU/Linux

$ cat /proc/cpuinfo 
processor       : 0
model name      : ARMv7 Processor rev 4 (v7l)
BogoMIPS        : 38.40
Features        : half thumb fastmult vfp edsp neon vfpv3 tls vfpv4 idiva idivt vfpd32 lpae evtstrm crc32 
CPU implementer : 0x41
CPU architecture: 7
CPU variant     : 0x0
CPU part        : 0xd03
CPU revision    : 4

processor       : 1
model name      : ARMv7 Processor rev 4 (v7l)
BogoMIPS        : 38.40
Features        : half thumb fastmult vfp edsp neon vfpv3 tls vfpv4 idiva idivt vfpd32 lpae evtstrm crc32 
CPU implementer : 0x41
CPU architecture: 7
CPU variant     : 0x0
CPU part        : 0xd03
CPU revision    : 4

processor       : 2
model name      : ARMv7 Processor rev 4 (v7l)
BogoMIPS        : 38.40
Features        : half thumb fastmult vfp edsp neon vfpv3 tls vfpv4 idiva idivt vfpd32 lpae evtstrm crc32 
CPU implementer : 0x41
CPU architecture: 7
CPU variant     : 0x0
CPU part        : 0xd03
CPU revision    : 4

processor       : 3
model name      : ARMv7 Processor rev 4 (v7l)
BogoMIPS        : 38.40
Features        : half thumb fastmult vfp edsp neon vfpv3 tls vfpv4 idiva idivt vfpd32 lpae evtstrm crc32 
CPU implementer : 0x41
CPU architecture: 7
CPU variant     : 0x0
CPU part        : 0xd03
CPU revision    : 4

Hardware        : BCM2835
Revision        : a22082
Serial          : 000000000b91277e
Model           : Raspberry Pi 3 Model B Rev 1.2
  • $ df -h
Filesystem      Size  Used Avail Use% Mounted on
/dev/root        59G  4.2G   52G   8% /
devtmpfs        484M     0  484M   0% /dev
tmpfs           488M     0  488M   0% /dev/shm
tmpfs           488M  6.5M  482M   2% /run
tmpfs           5.0M  4.0K  5.0M   1% /run/lock
tmpfs           488M     0  488M   0% /sys/fs/cgroup
/dev/mmcblk0p1  253M   52M  201M  21% /boot
tmpfs            98M     0   98M   0% /run/user/1001
  • $ cat /etc/dphys-swapfile
# ...
CONF_SWAPSIZE=4096  # increased swap size for PyTorch installation
CONF_MAXSWAP=4096  # increased swap size for PyTorch installation
  • $ free -h
              total        used        free      shared  buff/cache   available
Mem:          975Mi       102Mi       693Mi       6.0Mi       179Mi       816Mi
Swap:         4.0Gi          0B       4.0Gi

Install Required Packages

sudo apt install libopenblas-dev cython m4 python3-dev cmake python3-yaml libatomic-ops-dev libatlas3-base

Get PyTorch Source and Compile

For PyTorch 1.3.0:

git clone --recursive https://github.com/pytorch/pytorch
cd pytorch
git fetch && git fetch --tags
git checkout v1.3.0
# or
git checkout origin/v1.3.0 -b v1.3.0

Modify CMakeLists.txt, add -latomic to CMAKE_CXX_FLAGS:

# somewhere in CMakeLists.txt
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -latomic")

Compilation steps, set required environment flags and run the setup.py function:

export USE_MKLDNN=0      # disable using MKL-DNN (https://github.com/intel/mkl-dnn)
export USE_CUDA=0        # disable compiling CUDA
export USE_DISTRIBUTED=0 # disable distributed (c10d, gloo, mpi, etc.) build
export USE_FBGEMM=1      # enable using FBGEMM (https://github.com/pytorch/FBGEMM)
export USE_NNPACK=1      # enable compiling with NNPACK
export USE_QNNPACK=1     # enable QNNPACK build (quantized 8-bit operators)
export MAX_JOBS=1        # limit to 1 concurrent compiler job
python3 setup.py build

Install PyTorch and Sanity Check Install

sudo -E python3 setup.py install
python3
>>> import torch
>>> x = torch.rand(5, 3)
>>> print(x)

Reference files

My entire PyTorch directory, for successful setup.py installation.

$ sha256sum pytorch_v1.3.0_rpi3B.tar.gz 
7a9d85f1620e39e37d5c8f36874002f73bd40e9685e8271ffef389ac6d65940b  pytorch_v1.3.0_rpi3B.tar.gz
@awwong1
Copy link
Author

awwong1 commented Dec 6, 2022

Hi, this gist is quite out of date as Pytorch is now running version 1.13. Maybe follow the official docs? https://pytorch.org/tutorials/intermediate/realtime_rpi.html#installing-pytorch-and-opencv

@mahesaecha
Copy link

are you using 32-bit or 64-bit raspbian OS ?

@Aniket29-shiv
Copy link

Hi, I'm using 32-bit raspbian OS of raspberrypi 3B model. Not able to build the pytorch because it gets stuck. Is there any way it can be fixed?

@tonmoy-TS
Copy link

Hi, I'm using 32-bit raspbian OS of raspberrypi 3B model. Not able to build the pytorch because it gets stuck. Is there any way it can be fixed?

Looking for the same solution.

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