PyTorch on ARC

From RCSWiki
Jump to navigation Jump to search


PyTorch is a Python package that provides two high-level features:

  • Tensor computation (like NumPy) with strong GPU acceleration
  • Deep neural networks built on a tape-based autograd system

You can reuse your favorite Python packages such as NumPy, SciPy, and Cython to extend PyTorch when needed.

Installing PyTorch

You will need a working local Conda install in your home directory first. If you do not have it yet, please follow these instructions to have it installed.

Once you have your own Conda, activate it with

$ source ~/software/init-conda

We will install PyTorch into its own conda environment.

$ conda create -n pytorch 

Now we activate our new environment

$ conda activate pytorch

Check for the available versions of pytorch on the conda-forge channel and decide on what you want to install. Please make sure that there is GPU or cuda build for it (3rd column in the output table).

(pytorch) $ conda search -c conda-forge pytorch
pytorch                        2.1.2 cpu_mkl_py39h9c325db_100  conda-forge         
pytorch                        2.1.2 cuda112_py310hb684afd_301  conda-forge         

For example, we would like to get the version 2.1.2, then we see that there are CPU and GPU/cuda version.

If we try to install this version on the login node, Conda will detect that there is no GPU available and will install the CPU version. We have to use the CONDA_OVERRIDE_CUDA variable to override auto-detection and force installation of the GPU version. Like this:

(pytorch) $ CONDA_OVERRIDE_CUDA="12.2" conda install -c conda-forge pytorch=2.1.2 python pip 

Before confirming the installation, check the list of the packages to be installed to make sure that the correct version and build are selected.

Once it is done, your PyTorch environment is ready.

At this point you can add more packages to the environment using conda.

(pytorch) $ CONDA_OVERRIDE_CUDA="12.2" conda install -c conda-forge torchvision cuda-nvcc

Note that you will have to use the override variable again, if the additional packages depend on the GPU presence.


You can test with the script shown below. Copy and paste the text into a file and run if from the command line:

(pytorch) $ python 

If you try this on the login node, it should tell you that GPUs are not available. It is normal, as the login node does not have any. You will need a GPU node to test the GPUs.

To deactivate the environment (and conda itself) the

(pytorch) $ conda deactivate 
(base) $ conda deactivate 


Test script

#! /usr/bin/env python 
# -------------------------------------------------------
import torch
# -------------------------------------------------------
print("Defining torch tensors:")
x = torch.Tensor(5, 3)
y = torch.rand(5, 3)

# -------------------------------------------------------
# let us run the following only if CUDA is available
if torch.cuda.is_available():
    print("CUDA is available.")
    x = x.cuda()
    y = y.cuda()
    print(x + y)
    print("CUDA is NOT available.")

# -------------------------------------------------------

Test script 2

#! /usr/bin/env python 
# -------------------------------------------------------
import os
import sys
import socket
import torch
# -------------------------------------------------------

dev = os.environ['CUDA_VISIBLE_DEVICES']

host = socket.gethostname()
tdev = torch.cuda.current_device()
tavail = torch.cuda.is_available()
tcount = torch.cuda.device_count()
tname = torch.cuda.get_device_name()

print("Host: %s\nENV Devices: %s\nCudaDev: %s\nCUDA is available: %s\nDevice count: %d\nDevice: %s" % \
        (host, dev, tdev, tavail, tcount, tname))

print(os.popen("/usr/bin/nvidia-smi -L").read().strip())
print(os.popen("env | grep CUDA").read().strip())
# -------------------------------------------------------

Using PyTorch on ARC

Requesting GPU Resources for PyTorch Jobs

For interactive use see this How-To: How to request an interactive GPU on ARC.

An example of the job script torch_job.slurm:

#! /bin/bash
# ====================================
#SBATCH --job-name=torch-test
#SBATCH --nodes=1
#SBATCH --ntasks=1
#SBATCH --cpus-per-task=4
#SBATCH --mem=16GB
#SBATCH --time=0-04:00:00
#SBATCH --gres=gpu:1
#SBATCH --partition=gpu-v100
# ====================================

source ~/software/init-conda
conda activate pytorch



Refer to the checkpointing tutorial at


ARC Software pages