Skip to content

Instantly share code, notes, and snippets.

@Qengineering
Last active August 1, 2022 11:03
Show Gist options
  • Save Qengineering/0aef6533ec7cc82cddc6fbe70e907f33 to your computer and use it in GitHub Desktop.
Save Qengineering/0aef6533ec7cc82cddc6fbe70e907f33 to your computer and use it in GitHub Desktop.
Install PyTorch with TorchText on Jetson Nano

Introduction.

In this gist, we discuss the installation of PyTorch and TorchText on a Jetson Nano.

Dependencies.

TorchText 0.14.0 depends on PyTorch 1.11.0. PyTorch 1.11.0 requires Python 3.18, a version not found in the current JetPack. You can install Python 3.18 along 3.16 on your Nano. Another approach is the installation of Ubuntu 20.04 on your Jetson Nano.

Ubuntu 20.04

  • Get a 32 GB (minimal) SD-card which will hold the image.
  • Download the bare-bones image (5.6 GByte!) from our Sync.
  • Flash the image on the SD card with the Imager or balenaEtcher.
  • Flash the xz directly, not an unzipped img image.
  • Password: jetson

By the way, the Ubuntu version is overclocked at 1900 MHz. See https://qengineering.eu/install-ubuntu-20.04-on-jetson-nano.html for more information.

PyTorch 1.11

Install PyTorch by our wheel.

$ sudo apt-get install python3-pip libjpeg-dev libopenblas-dev libopenmpi-dev libomp-dev
$ sudo -H pip3 install future
$ sudo pip3 install -U --user wheel mock pillow
$ sudo -H pip3 install testresources
$ sudo -H pip3 install setuptools==58.3.0
$ sudo -H pip3 install Cython
$ sudo -H pip3 install gdown
# download the wheel
$ gdown https://drive.google.com/uc?id=1AQQuBS9skNk1mgZXMp0FmTIwjuxc81WY
# install PyTorch 1.11.0
$ sudo -H pip3 install torch-1.11.0a0+gitbc2c6ed-cp38-cp38-linux_aarch64.whl
# clean up
$ rm torch-1.11.0a0+gitbc2c6ed-cp38-cp38-linux_aarch64.whl

See https://qengineering.eu/install-pytorch-on-jetson-nano.html for more information. output image

Installation with pip3.

By far the most convenient way to install TorchText is to use the wheel we generated for you. Please download the wheel here or at https://github.com/Qengineering/PyTorch-Jetson-Nano

$ sudo -H pip3 install spacy==3.4.1
$ sudo -H pip3 install torchtext-0.14.0a0+f450271-cp38-cp38-linux_aarch64.whl
# clean up
$ rm torchtext-0.14.0a0+f450271-cp38-cp38-linux_aarch64.whl

output image

Installation from scratch.

If you want to install TorchText from scratch, you are missing some packages.

CMake 3.18.1

For a start, the Ubuntu has the one too old CMake version. Unfortunately, with no suitable repository, we need to build the new CMake also from scratch.

wget https://github.com/Kitware/CMake/releases/download/v3.18.1/cmake-3.18.1.tar.gz
tar -zxvf cmake-3.18.1.tar.gz
cd cmake-3.18.1
sudo ./bootstrap
sudo make
sudo make install

Clang 8

Compiling Pytorch code on architectures with NEON registers, like your Nano, means using the Clang compiler. The GNU compiler generates unpredictable outcomes. See https://qengineering.eu/install-pytorch-on-jetson-nano.html. Ubuntu 20.04 has Clang 10 on board. We need Clang 8 for the compilation of TorchText.

$ sudo apt-get install clang-8 g++-8
# setup the clang selector
$ sudo update-alternatives --install /usr/bin/clang clang /usr/bin/clang-10 10
$ sudo update-alternatives --install /usr/bin/clang clang /usr/bin/clang-8 8
# setup the clang++ selector
$ sudo update-alternatives --install /usr/bin/clang++ clang++ /usr/bin/clang++-10 10
$ sudo update-alternatives --install /usr/bin/clang++ clang++ /usr/bin/clang++-8 8
# selection version 8 with these commands
$ sudo update-alternatives --config clang
$ sudo update-alternatives --config clang++

Swap memory

The next step is enlarging the memory swap space. We need at least 8 GB during the compilation of the TorchText code.

$ sudo fallocate -l 4G /var/swapfile
$ sudo chmod 600 /var/swapfile
$ sudo mkswap /var/swapfile
$ sudo swapon /var/swapfile
# make the service permantent
$ sudo nano /etc/fstab
# add the following line
/var/swapfile   swap    swap     defaults   0       0
$ sudo reboot
$ swapon -s

output image

TorchText

With all software in place, finally, we can download TorchText. Set some environment variables.

$ export USE_CUDA=ON
$ export USE_CUDNN=ON
$ export TORCH_CUDA_ARCH_LIST="5.3;6.2;7.2"
$ export PATH=/usr/lib/ccache:$PATH
$ export CC=clang
$ export CXX=clang++
$ export CUDACXX=/usr/local/cuda/bin/nvcc

Download and compile TorchText.

$ git clone https://github.com/pytorch/text.git
$ cd text
$ git submodule update --init --recursive
$ sudo -H pip3 install spacy==3.4.1
$ sudo pip3 install -r requirements.txt
$ python3 setup.py clean
$ python3 setup.py bdist_wheel
$ cd dist
$ sudo -H pip3 install torchtext-0.14.0a0+f450271-cp38-cp38-linux_aarch64.whl

Cleanup and remove the large 4 GB swap file. Ready.

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