Skip to main content

Search Docs by Keyword

Table of Contents

PyTorch

Description

PyTorch, developed by Facebook’s AI Research lab, is an open-source machine learning library that offers a flexible platform for building deep learning models. It features a Python front end and integrates seamlessly with Python libraries like NumPy, SciPy, and Cython to extend its functionality. Unique for its use of dynamic computational graphs, unlike TensorFlow’s static graphs, PyTorch allows for greater flexibility in model design. This is particularly advantageous for research applications involving novel architectures.

The library supports GPU acceleration, enhancing performance significantly, which is vital for tackling high-level research tasks in areas such as climate change modeling, DNA sequence analysis, and AI research that involve large datasets and complex architectures. Automatic differentiation in PyTorch is handled through a tape-based system at both the functional and neural network layers, offering both speed and flexibility as a deep learning framework.

Installing PyTorch

These instructions are intended to help you install PyTorch on the FASRC cluster.

GPU Support:  For general information on running GPU jobs refer to our user documentation. To set up PyTorch with GPU support in your user environment, please follow the below steps:

PyTorch with CUDA 13.2 in a mamba environment

Note: mamba is a drop-in substitute for conda, but it’s faster and more robust.

These instructions set up a mamba virtual environment with PyTorch version 2.14.1 and CUDA version 13.2. The instructions below follow the recommendation by the official PyTorch installation guide.

Start an interactive job requesting GPUs, e.g., (Note: you will want to start a session on the same type of hardware as what you will run on)

salloc -p gpu -t 0-06:00 --mem=8000 --gres=gpu:1

Load required software modules, e.g.,

module load python

Create a mamba environment, e.g.,

mamba create -n pt2.14.1_cuda13.2 pip wheel -y

Activate the new mamba virtual environment:

source activate pt2.14.1_cuda13.2

Install PyTorch with pip

pip3 install --no-cache-dir torch torchvision --index-url https://download.pytorch.org/whl/cu132

Install additional Python packages, if needed, e.g.,

mamba install -c conda-forge numpy scipy pandas matplotlib seaborn h5py jupyterlab jupyterlab-spellchecker scikit-learn -y

Installing PyG (torch geometry)

After you create the conda environment pt2.14.1_cuda13.2 and activated it, you can install PyG in your environment with the command:

(pt2.14.1_cuda13.2) [username@holygpu7c26103 ~]$ pip install torch_geometric

Running PyTorch:

If you are running PyTorch on GPU with multi-instance GPU (MIG) mode on (e.g. gpu_test partition), see PyTorch on MIG mode

PyTorch checks

You can run the following tests to ensure that PyTorch was installed properly and can find the GPU card. Example output of PyTorch checks:

(pt2.14.1_cuda13.2) [jharvard@holygpu8a12102 ~]$ python
Python 3.15.0 | packaged by conda-forge | (main, Oct 12 2026, 17:36:43) [GCC 15.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import torch;
>>> print(torch.__version__)
2.14.1+cu132
>>> print(torch.cuda.is_available())
True
>>> print(torch.cuda.device_count())
1
>>> print(torch.cuda.current_device())
0
>>> print(torch.cuda.device(0))
<torch.cuda.device object at 0x146606f70ec0>
>>> print(torch.cuda.get_device_name(0))
NVIDIA H200
>>> exit()

Run PyTorch Interactively

For an interactive session to work with the GPUs you can use following:

salloc -p gpu -t 0-06:00 --mem=8000 --gres=gpu:1

Load required software modules and source your PyTorch conda environment.

[jharvard@holygpu8a12102 ~]$ module load python
[jharvard@holygpu8a12102 ~]$ source activate pt2.14.1_cuda13.2
(pt2.14.1_cuda13.2) [jharvard@holygpu8a12102 ~]$

Test PyTorch interactively:

(pt2.14.1_cuda13.2) [jharvard@holygpu8a12102 pytorch]$ python check_gpu.py 
Using device: cuda

NVIDIA H200
Memory Usage:
Allocated: 0.0 GB
Reserved: 0.0 GB

tensor([[ 0.1388, -1.1285, 0.1722, -0.0027]], device='cuda:0')

check_gpu.py: checks if GPUs are available and if available sets up the device to use them.

Run PyTorch with Batch Jobs

An example batch-job submission script is included below:

#!/bin/bash
#SBATCH -c 1
#SBATCH -N 1
#SBATCH -t 0-00:30
#SBATCH -p gpu
#SBATCH --gres=gpu:1
#SBATCH --mem=4G
#SBATCH -o pytorch_%j.out 
#SBATCH -e pytorch_%j.err 

# Load software modules and source conda environment
module load python
source activate pt2.14.1_cuda13.2

# Run program
srun -c 1 --gres=gpu:1 python check_gpu.py

If you name the above batch-job submission script run.sbatch, for instance, the job is submitted with:

sbatch run.sbatch

Best Practices

PyTorch and Jupyter Notebook on Open OnDemand

To use PyTorch in Jupyter Notebook on Open OnDemand/VDI, install ipykernel and ipywidgets:

(pt2.14.1_cuda13.2) [jharvard@holygpu8a12102 ~]$ mamba install ipykernel ipywidgets

Pull a PyTorch Singularity Container

Alternatively, you can pull and use a PyTorch singularity container:

singularity pull docker://pytorch/pytorch:2.1.0-cuda12.1-cudnn8-runtime

Other PyTorch/cuda versions

To install other versions, refer to the PyTorch compatibility chart:

Examples

For example scripts covering installation, and use cases, see our User Codes > AI > PyTorch repo.

External Resources:

  • Various PyTorch/CUDA version compatibility chart.
  • PyTorch Official Documentation – Comprehensive resource for all functionalities including tutorials and API reference.
  • PyTorch Tutorials – Practical tutorials covering basic to advanced topics, specifically tailored for deep learning and high-performance computing tasks.
  • PyTorch Discussion Forums – A community forum for discussing specific issues, sharing solutions, and collaborating on projects.
  • Introduction to Distributed Deep Learning – A detailed guide on implementing distributed deep learning models in PyTorch.
  • Efficient PyTorch – An article by PyTorch developers on best practices for optimizing deep learning models for production.
© The President and Fellows of Harvard College. | Harvard Digital Accessibility Policy
Except where otherwise noted, this content is licensed under Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International license.