AssertionError: Torch not compiled with CUDA enabled
The installed PyTorch build is CPU-only, and your code asked for CUDA anyway. Install the CUDA build from pytorch.org's selector — or, on a Mac, use the mps device instead.
Updated
The error
AssertionError: Torch not compiled with CUDA enabled
It fires on the first .to("cuda"), .cuda() or CUDA tensor creation.
What it means
PyTorch ships as several distinct builds: CPU-only, and one per supported CUDA version. Your installed build contains no CUDA code at all, so any request for the GPU is impossible — regardless of what hardware the machine has. This differs from torch.cuda.is_available() returning False, which can also be a driver problem; this message is purely about the installed build.
Why it happens
On Windows, a plain pip install torch installs the CPU-only build — the CUDA builds live on PyTorch's own package index and must be requested explicitly. Conda environments and requirements files pinned to +cpu wheels do the same. On a Mac the situation is stricter: no CUDA build exists for Apple hardware at all, and code copied from CUDA tutorials hits this error immediately.
How to fix it
1. Confirm what you have.
import torch
print(torch.__version__) # e.g. 2.8.0+cpu <- the +cpu suffix is the tell
print(torch.cuda.is_available())2. On a machine with an NVIDIA GPU, install the CUDA build. Get the exact command from the selector at pytorch.org — the CUDA tag changes between releases. It looks like:
pip uninstall torch torchvision torchaudio
pip install torch torchvision torchaudio --index-url https://download.pytorch.org/whl/cu126The --index-url is the crucial part; without it, pip may reinstall the CPU build.
3. On a Mac, use the Apple GPU via mps.
device = torch.device("mps" if torch.backends.mps.is_available() else "cpu")
model = model.to(device)4. On a machine with no NVIDIA GPU, run on CPU — deliberately. Learning-sized models train fine on CPU. Make the code fall back instead of assuming:
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")5. Check nothing hard-codes "cuda". Search the project for .cuda() and "cuda" literals and route them all through the device variable.
How to prevent it
Write device-agnostic code from day one: one device variable at the top, chosen by availability, used everywhere. In requirements files, document the install command including the index URL, since torch alone under-specifies which build a new machine gets.
Related errors
- torch.cuda.is_available() returns False — the wider diagnosis tree, including driver problems
- no kernel image is available for execution — a CUDA build that does not cover your GPU's generation
- Expected all tensors to be on the same device
- ModuleNotFoundError: No module named 'torch'