How to check if CUDA is available in PyTorch
Quick answer
Use
torch.cuda.is_available() to check if CUDA is available in PyTorch. It returns True if a compatible GPU and CUDA driver are detected, otherwise False.PREREQUISITES
Python 3.8+pip install torch
Setup
Install PyTorch with CUDA support by following the official instructions at PyTorch official site. For most users, installing the latest stable version with CUDA support is recommended.
Example install command for CUDA 11.8:
pip install torch torchvision torchaudio --index-url https://download.pytorch.org/whl/cu118 Step by step
Run the following Python code to check if CUDA is available on your system. This code imports torch and prints the CUDA availability status.
import torch
if torch.cuda.is_available():
print("CUDA is available. You can use GPU acceleration.")
else:
print("CUDA is not available. Using CPU.") output
CUDA is available. You can use GPU acceleration.
Common variations
You can also check the number of available CUDA devices and their names for more detailed info:
import torch
def print_cuda_info():
if torch.cuda.is_available():
print(f"Number of CUDA devices: {torch.cuda.device_count()}")
for i in range(torch.cuda.device_count()):
print(f"Device {i}: {torch.cuda.get_device_name(i)}")
else:
print("CUDA is not available.")
print_cuda_info() output
Number of CUDA devices: 1 Device 0: NVIDIA GeForce RTX 3080
Troubleshooting
If torch.cuda.is_available() returns False but you expect CUDA to be available, check the following:
- Ensure your GPU drivers and CUDA toolkit are properly installed and compatible with your PyTorch version.
- Verify that you installed the PyTorch version with CUDA support, not the CPU-only version.
- Restart your machine after installing drivers or CUDA toolkit.
- Check environment variables like
CUDA_VISIBLE_DEVICESthat might disable GPU visibility.
Key Takeaways
- Use
torch.cuda.is_available()to quickly verify CUDA support in PyTorch. - Check device count and names with
torch.cuda.device_count()andtorch.cuda.get_device_name()for detailed GPU info. - Make sure to install the PyTorch version with CUDA support matching your CUDA toolkit and GPU drivers.
- If CUDA is not detected, verify driver installation and environment variables before troubleshooting further.