Check if Your CUDA, cuDNN, and PyTorch Are Functional

When installing PyTorch, many of you might encounter issues like mismatched versions of CUDA, cuDNN, or PyTorch itself.

Today, I’ll show you how to verify if your CUDA, cuDNN, and PyTorch are functional.

Let’s get started!

Verification Code

Copy and run the following code in your IDE:

1
2
3
4
5
6
7
8
9
10
11
import torch
print('CUDA version:', torch.version.cuda)
print('PyTorch version:', torch.__version__)
print('Is GPU available:', 'Yes' if torch.cuda.is_available() else 'No')
print('Number of GPUs:', torch.cuda.device_count())
print('Supports BF16 format:', 'Yes' if torch.cuda.is_bf16_supported() else 'No')
print('Current GPU model:', torch.cuda.get_device_name())
print('CUDA compute capability:', torch.cuda.get_device_capability())
print('Total GPU memory:', torch.cuda.get_device_properties(0).total_memory / 1024 / 1024 / 1024, 'GB')
print('Supports TensorCore:', 'Yes' if torch.cuda.get_device_properties(0).major >= 7 else 'No')
print('GPU memory usage:', torch.cuda.memory_allocated(0) / torch.cuda.get_device_properties(0).total_memory * 100, '%')

If everything is set up correctly, the output should look similar to this:

CUDA version: 12.1  
PyTorch version: 2.5.1+cu121
Is GPU available: Yes
Number of GPUs: 1
Supports BF16 format: Yes
Current GPU model: NVIDIA GeForce RTX 4060 Ti
CUDA compute capability: (8, 9)
Total GPU memory: 7.9954833984375 GB
Supports TensorCore: Yes
GPU memory usage: 1.1835104352738208 %

If your setup isn’t configured properly, only the first three lines might be displayed.

Line-by-Line Code Analysis

1. torch.version.cuda

Displays the current CUDA version, e.g., 12.1 or 12.4.
If you’re using an older version, it’s recommended to update via the official NVIDIA website.

2. torch.__version__

Shows the current PyTorch version, such as 2.5.1+cu121.

  • 2.5.1: Indicates the major PyTorch version.
    Mainstream versions range from 1.13.0 to 2.5.0. Managing multiple PyTorch versions simultaneously is often necessary for code reproduction.
  • cu121: Indicates that the PyTorch version uses CUDA 12.1.
    If it says cp311, it’s a CPU version compatible with Python 3.11.X.

Using Conda for virtual environment management is highly recommended.
If you’re interested in a tutorial on managing Conda environments, give a thumbs-up so I can consider creating one next time!

3. torch.cuda.is_available()

Checks if the GPU is available. Returns True if available, otherwise False.

4. torch.cuda.device_count()

Returns the number of GPUs on your machine. Most PCs have only 1.
> Note: If you have multiple GPUs, this script only retrieves details about the first one. Use a loop to list all GPUs.

5. torch.cuda.is_bf16_supported()

Checks if the GPU supports BF16 (BFloat16) computation.

BF16 is an optimized format for deep learning, widely adopted in AI accelerators by companies like Google, Intel, and NVIDIA. Popular NVIDIA GPUs like RTX 3060 and RTX 3070 support BF16.

6. torch.cuda.get_device_capability()

Displays the CUDA compute capability of your GPU.

GPUs with compute capability ≥5.0 are recommended for training neural networks.

For details, visit NVIDIA’s CUDA GPUs page.

7. torch.cuda.get_device_properties(0).total_memory

Fetches the total memory size of the GPU.

Refer to the PyTorch documentation for more details.

8. TensorCore Support

GPUs with compute capability ≥7.0 feature TensorCores, which accelerate matrix calculations for deep learning.

TensorCore significantly outperforms traditional CUDA cores in tasks like inference.

For a deeper understanding, explore articles or videos on CUDA and TensorCore technology.