When working with NVIDIA GPUs and CUDA for deep learning (e.g., with PyTorch), the primary concern is ensuring compatibility between your GPU driver and the CUDA version used by PyTorch. The CUDA Toolkit installed via conda or system-wise is often a secondary consideration unless building custom CUDA code.
nvidia-smi shows the maximum CUDA version that your NVIDIA GPU driver supports. This is the CUDA Driver API version. It reflects the highest CUDA runtime version the driver can work with and is key for compatibility.nvcc --version shows the CUDA Toolkit version installed on your system—that is, the version of the CUDA runtime API used for compiling CUDA programs.To ensure PyTorch runs properly with CUDA support, the driver CUDA version must be equal to or higher than the CUDA version PyTorch was built with. PyTorch bundles its own CUDA runtime (usually from conda cudatoolkit or pip wheels), so system CUDA Toolkit version often does not impact PyTorch except when building extensions.
You can use this Python code snippet to fetch and print the driver CUDA version, toolkit CUDA version, and PyTorch CUDA version:
import subprocess
import torch
import re
def get_driver_version():
try:
output = subprocess.check_output(['nvidia-smi']).decode()
match = re.search(r'CUDA Version: (\\d+\\.\\d+)', output)
if match:
return float(match.group(1))
except Exception as e:
return f"Error getting driver version: {e}"
def get_toolkit_version():
try:
output = subprocess.check_output(['nvcc', '--version']).decode()
match = re.search(r'release (\\d+\\.\\d+)', output)
if match:
return float(match.group(1))
except Exception as e:
return f"Error getting toolkit version: {e}"
driver_version = get_driver_version()
toolkit_version = get_toolkit_version()
torch_cuda_version = torch.version.cuda
torch_cuda_is_available = torch.cuda.is_available()
print(f"CUDA Driver Version (nvidia-smi): {driver_version}")
print(f"CUDA Toolkit Version (nvcc): {toolkit_version}")
print(f"PyTorch CUDA Version: {torch_cuda_version}")
print(f"Is CUDA available for PyTorch? {torch_cuda_is_available}")
if isinstance(driver_version, float) and torch_cuda_version is not None:
if driver_version < float(torch_cuda_version):
print("Warning: CUDA driver version is older than PyTorch CUDA version. Update your GPU driver.")
else:
print("CUDA driver version is compatible with or newer than PyTorch CUDA version.")
| Component | Role in CUDA Compatibility |
|---|---|
NVIDIA Driver (nvidia-smi) |
Must support CUDA version ≥ PyTorch CUDA version to run models |
| PyTorch CUDA Version | CUDA runtime PyTorch uses for GPU operations |
| System/Conda CUDA Toolkit | Used primarily for compiling CUDA code, not runtime by PyTorch |