Skip to main content
Ctrl K

PyTorch Performance Analysis

Lessons on PyTorch Performance Analysis: from Profiling to Optimization

1
contributor
Get started
8 commitsLast commit ≈ 1 week ago2 stars0 forks

Description

PyTorch Performance Analysis: from Profiling to Optimization

Hands-on notebooks to learn how to measure, profile, diagnose, and optimize AI models using PyTorch.

NotebookWhat it coversEstimated time
ep1.ipynbTiming one GPU operation correctly, and reading its profile as a timeline and as a table1 hour
ep2.ipynbAnalyse a whole training loop: measure, profile, diagnose, optimise, verify, then next iteration2-3 hours

The lecture slides are available in the slides folder.

For students

Requirements

  • Linux system
  • A GPU
  • Multiple CPUs
  • uv

Setup

uv sync

# Open notebooks in jupyter lab
uv run jupyter lab

# or open notebooks in VSCode, requiring `python` and `jupyter` plugins

If a cell fails on your machine, the references folder has both notebooks already executed with their outputs and traces kept, so you can keep following along.

For instructors

PathWhat it is
notebooks/src/*.pySource of truth.
notebooks/*.ipynbWhat students open. Generated, and shipped with no outputs
references/Both notebooks executed on the reference GPU, with outputs and traces kept
build.py, sync.py, verify.pyBuild, sync back, check. Run each with --help
slidesSlides for the lesson

Before class

uv run python build.py --execute --verify   # rebuild, re-run references/, must report 0 failed

Needs a GPU and takes a few minutes. Teaching on a different GPU? Recalibrate first: run either SETUP cell there, take the anchor it prints (4096x4096 matmul = ... ms/iter), put that number in EXPECTED_MATMUL_MS in both notebooks/src/*.py, then rebuild.

Editing

notebooks/src/*.py is the source of truth; the notebooks are rebuilt from it.

uv run python build.py ep1   # source -> notebook
uv run python sync.py ep1    # notebook -> source, after editing in Jupyter

Never edit a notebook and its source at the same time: sync.py overwrites the source with the notebook. Sync before committing, because verify.py fails on a notebook that does not match its source.

Troubleshooting

torch.cuda.is_available() is False. Check nvidia-smi and that Jupyter runs in the environment uv sync created.

num_workers > 0 hangs in ep2. The Dataset is defined inside the notebook, which needs Linux's fork. On Windows or macOS, stay at num_workers=0 and read the reference traces.

Perfetto will not load a student's file. Use the matching file from references/traces/.

Keywords
Programming languages
  • Jupyter Notebook 70%
  • Python 30%
License
</>Source code

Participating organisations

Netherlands eScience Center

Contributors

Related projects

3D-Vac

Personalized cancer vaccine design through 3D modelling boosted geometric learning

Updated 6 months ago
Finished

Related software

swiftmhc

SW

SwiftMHC is a deep learning algorithm for predicting pMHC structure and binding affinity at the same time.

Updated 6 months ago
2 3