Checking CUDA Availability in PyTorch
You'll learn how to programmatically detect GPU availability and handle different hardware environments in your PyTorch code.
Why This Matters
Not every machine has a GPU, and even when one exists, CUDA might not be properly configured. Before attempting GPU-accelerated training, you need to verify that CUDA is available—otherwise, your code will crash or run inefficiently. Think of it like checking if your car has gas before starting a road trip.
Core Detection Methods
PyTorch provides simple functions to check your hardware setup:
import torch
# Check if CUDA is available
cuda_available = torch.cuda.is_available()
print(f"CUDA available: {cuda_available}")
# Get the number of GPUs
gpu_count = torch.cuda.device_count()
print(f"Number of GPUs: {gpu_count}")
# Get CUDA version (if available)
if cuda_available:
print(f"CUDA version: {torch.version.cuda}")
Graceful Fallback Pattern
The professional approach is to write code that works on both GPU and CPU:
# Set device dynamically
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
print(f"Using device: {device}")
# Now use this device consistently
tensor = torch.randn(3, 3).to(device)
This pattern ensures your code runs everywhere—fast on GPUs when available, but still functional on CPU-only machines like laptops or budget cloud instances.
Why Check Instead of Assume?
Different environments have different capabilities: your local machine might have a GPU, but your CI/CD pipeline might not. Development servers may share GPUs. Checking availability lets your code adapt automatically without manual configuration changes.
Key Takeaway: Always use torch.cuda.is_available() to detect GPU support and set your device dynamically with the fallback pattern—this makes your code portable and prevents runtime errors across different hardware environments.