Critical severity intermediate · Fix: 5-15 min

RuntimeError

torch.cuda.CudaError: CUDA initialization error: no GPU found

What this error means
PyTorch throws a CUDA initialization error when it cannot detect any available GPU on the system.

Stack trace

traceback
Traceback (most recent call last):
  File "script.py", line 10, in <module>
    torch.cuda.current_device()
RuntimeError: CUDA error: initialization error: no GPU found
QUICK FIX
Verify torch.cuda.is_available() returns True and install correct NVIDIA drivers and CUDA toolkit before running GPU code.

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

1

No physical GPU hardware is present or accessible on the machine.

✓ Fix

Run the code on a machine with a CUDA-compatible GPU installed and properly connected.

2

NVIDIA GPU drivers or CUDA toolkit are missing, outdated, or incompatible.

✓ Fix

Install or update the NVIDIA drivers and CUDA toolkit to versions compatible with your PyTorch and GPU hardware.

3

CUDA device is disabled in BIOS or blocked by system policies.

✓ Fix

Enable the GPU device in BIOS settings and ensure no OS-level restrictions prevent CUDA access.

4

PyTorch is installed without CUDA support or running in CPU-only mode.

✓ Fix

Install the PyTorch version with CUDA support matching your CUDA toolkit version, e.g., torch with 'cu117' or 'cu118' wheels.

Code: broken vs fixed

Broken - triggers the error
python
import torch

device = torch.device('cuda')
print(torch.cuda.current_device())  # RuntimeError: CUDA initialization error: no GPU found
Fixed - works correctly
python
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
Added a check for CUDA availability before using GPU device and set CUDA_VISIBLE_DEVICES to ensure GPU is accessible, preventing initialization error.

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.

Python 3.7+ · torch >=1.0.0 · tested on 2.0.x
Verified 2026-04
Verify ↗

Community Notes

No notes yetBe the first to share a version-specific fix or tip.