Home › Python

AssertionError: Torch not compiled with CUDA enabled (Python) — how to fix

Traceback (most recent call last):
  File "train.py", line 12, in <module>
    model = model.to("cuda")
  ...
AssertionError: Torch not compiled with CUDA enabled

Quick fix: Check with python -c "import torch; print(torch.__version__, torch.version.cuda)" (CPU builds show +cpu / None). Reinstall a CUDA build using the command from pytorch.org, e.g. pip uninstall -y torch torchvision torchaudio then pip install torch torchvision torchaudio --index-url https://download.pytorch.org/whl/cu121. On a Mac, use "mps" instead of "cuda".

What it means

PyTorch is published in several builds: CPU-only, and builds for specific CUDA versions. The CPU build has no CUDA code inside, so moving a tensor or model to "cuda" raises this assertion. It is not a driver problem (a missing driver gives torch.cuda.is_available() == False with a CUDA build, but not this message).

Check what you have, using the same interpreter that runs your script:

python -c "import torch; print(torch.__version__, torch.version.cuda, torch.cuda.is_available())"
# 2.3.1+cpu None False        <- CPU build
# 2.3.1+cu121 12.1 True       <- CUDA build working

Fix 1: install a CUDA build of PyTorch

On Windows the default pip install torch from PyPI is CPU-only. Uninstall and install from PyTorch's CUDA index. Pick the exact command for your OS and CUDA version from the selector on pytorch.org ("Get Started"); it looks like:

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

Notes:

Fix 2: a dependency pulled in the CPU build again

Installing another package afterwards (or pip install -r requirements.txt with a plain torch==x.y.z line) can replace your CUDA build with the PyPI one. Pin the index in requirements:

--extra-index-url https://download.pytorch.org/whl/cu121
torch==2.3.1+cu121

Re-run the version check after every install step in Docker images and CI.

Fix 3: macOS has no CUDA

Macs don't have NVIDIA GPUs supported by PyTorch. On Apple Silicon, use the Metal backend:

device = "mps" if torch.backends.mps.is_available() else "cpu"
model.to(device)

Fix 4: write device-agnostic code

Code copied from tutorials often hard-codes .cuda(). Choose the device once and fall back to CPU:

import torch

if torch.cuda.is_available():
    device = torch.device("cuda")
elif torch.backends.mps.is_available():
    device = torch.device("mps")
else:
    device = torch.device("cpu")

model = model.to(device)
x = x.to(device)

When loading checkpoints saved on a GPU machine, map them to the device you have:

state = torch.load("model.pt", map_location=device)

Fix 5: no NVIDIA GPU

AMD GPUs on Linux use the ROCm builds of PyTorch (also listed on pytorch.org), which still use the "cuda" device name. Intel and other GPUs have their own backends. If there is no supported GPU, run on CPU with Fix 4.

Last updated 2026-11-28.