Skip to content
Latchkey

PyTorch "torchvision were compiled with different CUDA versions" in CI

torchvision checks at import time that its CUDA build matches torch's, and they differ. One was installed as a cu118 wheel and the other as cu121 (or one CPU, one CUDA), so torchvision refuses to load.

What this error means

Importing torchvision raises "RuntimeError: Detected that PyTorch and torchvision were compiled with different CUDA versions" and prints both CUDA versions.

python
RuntimeError: Detected that PyTorch and torchvision were compiled with different CUDA
versions. PyTorch has CUDA Version=11.8 and torchvision has CUDA Version=12.1.
Please reinstall the torchvision that matches your PyTorch install.

Common causes

Wheels from different CUDA channels

pip pulled torch from one CUDA index (cu118) and torchvision from another (cu121), so their bundled CUDA versions disagree.

A partial upgrade left mismatched cu suffixes

Upgrading only one of the two packages, or letting the default index supply one, mixes CUDA builds.

How to fix it

Reinstall both from one CUDA channel

  1. Choose a single CUDA suffix (for example cu121) for the whole job.
  2. Install torch and torchvision together from that one index.
  3. Confirm import succeeds and both report the same CUDA version.
Terminal
pip install --index-url https://download.pytorch.org/whl/cu121 \
  torch torchvision

Inspect the installed CUDA build

Print what each package was built with so a mismatch is obvious in logs.

Terminal
python -c "import torch, torchvision; print(torch.version.cuda, torchvision.version.cuda)"

How to prevent it

  • Install all torch-family packages from the same CUDA index.
  • Pin matching versions with identical cu suffixes in the lockfile.
  • Log torch.version.cuda in CI so drift is caught early.

Related guides

Tired of flaky CI? Latchkey auto-heals failed jobs and retries them for you. Start free →