Skip to content
Latchkey

PyTorch "Torch not compiled with CUDA enabled" in CI

The installed PyTorch is the CPU-only wheel, yet the code moves tensors to CUDA. Without CUDA support compiled in, .cuda() or device="cuda" cannot work and PyTorch asserts.

What this error means

A run fails with "AssertionError: Torch not compiled with CUDA enabled" or "RuntimeError: ... CUDA ... not available" when calling .cuda() or .to("cuda").

python
AssertionError: Torch not compiled with CUDA enabled

Common causes

The CPU-only torch wheel is installed

pip resolved the default CPU wheel (or +cpu build), which has no CUDA, but the code unconditionally targets the GPU.

Code assumes a GPU is always present

Hardcoding device="cuda" fails on CPU runners that have no GPU and only the CPU build installed.

How to fix it

Select the device dynamically

Pick CUDA only when it is available so the same code runs on CPU and GPU runners.

train.py
device = "cuda" if torch.cuda.is_available() else "cpu"
model.to(device)

Install a CUDA build on GPU runners

For GPU jobs, install the CUDA-enabled torch wheel from the matching index.

Terminal
pip install torch --index-url https://download.pytorch.org/whl/cu121

How to prevent it

  • Always gate GPU code behind torch.cuda.is_available().
  • Install the CUDA build only on runners that have a GPU.
  • Keep a CPU code path for CI jobs without GPUs.

Related guides

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