Now, I know what you might be thinking: “But wait, isn’t it already pre-installed on these things?” And the answer is…kinda sorta? But not really. You see, while PyTorch itself may be available for download from NVIDIA’s website, getting it to work properly can be a bit of a headache.
Before anything else you’ll need to have some basic knowledge of Linux commands and package management, since that’s what we’re going to be using here. If you’re not familiar with these concepts, I highly recommend checking out a tutorial or two before proceeding any further.
Step 1: Update your system packages (because why not)! This is always a good idea when starting any new project on Linux, as it ensures that you have the latest versions of all necessary software installed. To do this, simply open up a terminal window and run the following command:
#!/bin/bash
# This script updates the system packages and upgrades them to the latest versions.
# Use sudo to run the following commands as a superuser, allowing for system updates.
sudo apt-get update # This command updates the list of available packages from the repositories.
sudo apt-get upgrade -y # This command upgrades all installed packages to their latest versions, with the -y flag automatically answering yes to any prompts.
# End of script.
Step 2: Install some prerequisites (because why not)! This is also important for ensuring that PyTorch will work properly on your system. The specific packages you’ll need to install may vary depending on which version of Jetson you have, but here are a few common ones:
# Install necessary prerequisites for PyTorch to work properly on your system
# Packages may vary depending on Jetson version
# Common packages include python3-pip, libopenblas-dev, libatlas-base-dev, and gfortran
sudo apt-get install -y python3-pip libopenblas-dev libatlas-base-dev gfortran
Step 3: Install PyTorch itself! This is where things can get a bit tricky, as there are several different ways to do this depending on which version of Jetson you have and what kind of hardware acceleration you want. For simplicity’s sake, we’re going to assume that you have the latest version (JetPack 5) and that you want to use CUDA for maximum performance. To install PyTorch using these settings, run the following command:
#!/bin/bash
# Add NVIDIA repository key
wget https://developer.download.nvidia.com/compute/cuda/repos/ubuntufocal/x86_64/7fa2af8f.pub # downloads the NVIDIA repository key
sudo apt-key add 7fa2af8f.pub # adds the key to the system's list of trusted keys
# Add NVIDIA repository to sources list
echo "deb https://developer.download.nvidia.com/compute/cuda/repos/ubuntufocal/x86_64 /" | sudo tee -a /etc/apt/sources.list.d/cuda.list # adds the NVIDIA repository to the system's sources list
# Update package list and install CUDA toolkit, cuDNN library, and related packages
sudo apt-get update && sudo apt-get install -y cuda nvidia-cuda-toolkit libnvinfer-dev libnvinfer-tools libnccv-dev nvidia-tensorrt-core nvidia-tensorrt-inference-server python3-nvdnn # updates the package list and installs CUDA toolkit, cuDNN library, and related packages
# Install PyTorch using pip (note: this may take a while)
sudo pip3 install torch==1.8.0+cu111 torchaudio===0.8.0 -f https://download.pytorch.org/whl/torch_stable.html # installs PyTorch using pip and specifies the version and source to use
Step 4: Test your installation! To make sure that everything is working properly, you can run a simple script to check for GPU acceleration and print out the version of PyTorch that’s installed on your system. Here’s an example:
# Import the torch library
import torch
# Print the version of PyTorch installed on the system
print(torch.__version__)
# Check if GPU acceleration is available and assign the device accordingly
device = 'cuda' if torch.cuda.is_available() else 'cpu'
# Create a tensor of ones with a shape of (1, 2) and set requires_grad to True for gradient calculation
x = torch.ones(1, 2, requires_grad=True)
# Move the tensor to the specified device and convert it to float data type
y = x.to(device).float()
# Reshape the tensor to a 1-dimensional tensor
y = y.view(-1)
# Calculate the square of each element in the tensor
z = y.pow(2)
# Print the size of the tensor
print(z.size())
# Output:
# 2
And that’s it! You should now have a fully functional installation of PyTorch on your NVIDIA Jetson platform, complete with GPU acceleration and all the bells and whistles you could ask for (well, maybe not ALL the bells and whistles…but definitely some of them).
Of course, there are still plenty of things to learn about using PyTorch in a production environment, but that’s a topic for another day. For now, let’s just be grateful that we were able to get this far without any major headaches or meltdowns!