Error database

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.

The message you saw
AssertionError: Torch not compiled with CUDA enabled

By Updated

The error

Output
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.

python
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:

bash
pip uninstall torch torchvision torchaudio
pip install torch torchvision torchaudio --index-url https://download.pytorch.org/whl/cu126

The --index-url is the crucial part; without it, pip may reinstall the CPU build.

3. On a Mac, use the Apple GPU via mps.

python
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:

python
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.