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").
AssertionError: Torch not compiled with CUDA enabledCommon 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.
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.
pip install torch --index-url https://download.pytorch.org/whl/cu121How 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.