---
title: "PyTorch"
id: "27662"
type: "epkb_post_type_1"
slug: "pytorch"
published_at: "2024-09-20T23:53:59+00:00"
modified_at: "2026-10-20T14:16:06+00:00"
url: "https://docs.rc.fas.harvard.edu/kb/pytorch/"
markdown_url: "https://docs.rc.fas.harvard.edu/kb/pytorch.md"
excerpt: "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..."
taxonomy_epkb_post_type_1_category:
  - "AI"
---

# PyTorch

## Description

PyTorch, developed by Facebook’s AI Research lab, is an open-source [machine learning library](https://github.com/pytorch/pytorch)
 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](https://www.tensorflow.org/)
 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](https://www.rc.fas.harvard.edu/resources/documentation/gpgpu-computing-on-the-cluster)
. 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](https://pytorch.org/get-started/locally/)
.

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](https://mamba.readthedocs.io/en/latest/user_guide/mamba.html)
, 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](https://pytorch-geometric.readthedocs.io/en/latest/install/installation.html)
 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](https://github.com/fasrc/User_Codes/edit/master/AI/PyTorch/README.md#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](https://github.com/fasrc/User_Codes/blob/master/AI/PyTorch/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](https://docs.sylabs.io/guides/3.5/user-guide/index.html)
 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](https://pytorch.org/)
:

## Examples

For example scripts covering installation, and use cases, see our [User Codes > AI > PyTorch](https://github.com/fasrc/User_Codes/tree/master/AI/PyTorch)
 repo.

## External Resources:

- Various PyTorch/CUDA version [compatibility chart.](https://pytorch.org/get-started/previous-versions/)
- [PyTorch Official Documentation](https://pytorch.org/docs/stable/index.html) – Comprehensive resource for all functionalities including tutorials and API reference.
- [PyTorch Tutorials](https://pytorch.org/tutorials/) – Practical tutorials covering basic to advanced topics, specifically tailored for deep learning and high-performance computing tasks.
- [PyTorch Discussion Forums](https://discuss.pytorch.org/) – A community forum for discussing specific issues, sharing solutions, and collaborating on projects.
- [Introduction to Distributed Deep Learning](https://pytorch.org/tutorials/intermediate/dist_tuto.html) – 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.

Bookmarkable Links

- [1. Description](#Description)
- [2. Installing PyTorch](#Installing_PyTorch)
  - [2.1. PyTorch with CUDA 13.2 in a mamba environment](#PyTorch_with_CUDA_132_in_a_mamba_environment)
  - [2.2. Installing PyG (torch geometry)](#Installing_PyG_torch_geometry)

- [3. Running PyTorch:](#Running_PyTorch)
  - [3.1. PyTorch checks](#PyTorch_checks)
  - [3.2. Run PyTorch Interactively](#Run_PyTorch_Interactively)
  - [3.3. Run PyTorch with Batch Jobs](#Run_PyTorch_with_Batch_Jobs)

- [4. Best Practices](#Best_Practices)
  - [4.1. PyTorch and Jupyter Notebook on Open OnDemand](#PyTorch_and_Jupyter_Notebook_on_Open_OnDemand)
  - [4.2. Pull a PyTorch Singularity Container](#Pull_a_PyTorch_Singularity_Container)
  - [4.3. Other PyTorch/cuda versions](#Other_PyTorchcuda_versions)

- [5. Examples](#Examples)
- [6. External Resources:](#External_Resources)

Last UpdatedOctober 20, 2026
