RuntimeError
torch.cuda.CudaError: CUDA initialization error: no GPU found
Stack trace
Traceback (most recent call last):
File "script.py", line 10, in <module>
torch.cuda.current_device()
RuntimeError: CUDA error: initialization error: no GPU found Why it happens
This error occurs because PyTorch attempts to initialize CUDA but fails to find any compatible GPU devices on the machine. It can happen if the system has no GPU, the GPU drivers or CUDA toolkit are not installed or configured properly, or if the CUDA device is disabled or inaccessible.
Detection
Check for RuntimeError exceptions when calling torch.cuda functions like torch.cuda.current_device() or torch.cuda.is_available() returning False before running GPU-dependent code.
Causes & fixes
No physical GPU hardware is present or accessible on the machine.
Run the code on a machine with a CUDA-compatible GPU installed and properly connected.
NVIDIA GPU drivers or CUDA toolkit are missing, outdated, or incompatible.
Install or update the NVIDIA drivers and CUDA toolkit to versions compatible with your PyTorch and GPU hardware.
CUDA device is disabled in BIOS or blocked by system policies.
Enable the GPU device in BIOS settings and ensure no OS-level restrictions prevent CUDA access.
PyTorch is installed without CUDA support or running in CPU-only mode.
Install the PyTorch version with CUDA support matching your CUDA toolkit version, e.g., torch with 'cu117' or 'cu118' wheels.
Code: broken vs fixed
import torch
device = torch.device('cuda')
print(torch.cuda.current_device()) # RuntimeError: CUDA initialization error: no GPU found import os
import torch
# Ensure environment is set up with CUDA drivers and GPU available
os.environ['CUDA_VISIBLE_DEVICES'] = '0' # Use GPU 0 if available
def check_cuda():
if not torch.cuda.is_available():
print('CUDA not available, falling back to CPU')
return torch.device('cpu')
return torch.device('cuda')
device = check_cuda()
print(f'Using device: {device}')
if device.type == 'cuda':
print(torch.cuda.current_device()) # Should print 0 or GPU index Workaround
Wrap CUDA calls in try/except RuntimeError, and fallback to CPU device if CUDA initialization fails to allow code to run without GPU.
Prevention
Always verify CUDA environment setup and GPU availability with torch.cuda.is_available() before running GPU-dependent PyTorch code to avoid runtime crashes.