torch.cuda.is_available: Verify GPU from Python
python -c "import torch; print(torch.cuda.is_available())" is the definitive check that PyTorch can actually reach the GPU, not just that a GPU exists.
nvidia-smi proves the driver works; this one-liner proves your Python stack can use it. It is the gate to put before any training step in CI.
What it does
torch.cuda.is_available() returns True only if PyTorch was built with CUDA support and can initialize a compatible driver and GPU at runtime. torch.version.cuda shows which CUDA build PyTorch has, and torch.cuda.get_device_name(0) names the GPU.
Common usage
python -c "import torch; print(torch.cuda.is_available())"
# more detail for debugging
python -c "import torch; print(torch.version.cuda, \
torch.cuda.device_count(), \
torch.cuda.get_device_name(0) if torch.cuda.is_available() else 'no gpu')"Options
| Call | What it returns |
|---|---|
| torch.cuda.is_available() | True if PyTorch can use a GPU |
| torch.version.cuda | The CUDA version PyTorch was built against |
| torch.cuda.device_count() | Number of visible GPUs |
| torch.cuda.get_device_name(i) | Name of GPU i |
| CUDA_VISIBLE_DEVICES | Env var that can hide GPUs from PyTorch |
In CI
Make this one-liner a required step that exits non-zero when it prints False, so a broken GPU stack fails the job before wasting time on training. A common gotcha is installing the CPU-only wheel; check torch.version.cuda is not None.
Common errors in CI
is_available() returning False despite a working nvidia-smi usually means the CPU-only PyTorch wheel is installed (torch.version.cuda is None), CUDA_VISIBLE_DEVICES is empty, or the wheel\u0027s CUDA version needs a newer driver. "UserWarning: CUDA initialization: ... no CUDA-capable device is detected" and "RuntimeError: Found no NVIDIA driver on your system" point at a missing/hidden GPU or driver, often a container started without --gpus all.