Categories
PyTorch Python

How to Check PyTorch CUDA Version Easily

Here you will learn how to check NVIDIA CUDA version for PyTorch and other frameworks like TensorFlow. The 3 methods are nvcc from CUDA toolkit, nvidia-smi from NVIDIA driver, and simply checking a file.

Prerequisite

Before we begin, you should have installed NVIDIA driver on your system as well as Nvidia CUDA toolkit, aka, CUDA. We also assume you have PyTorch installed. If you haven’t done so, check out our guide to install PyTorch on Ubuntu 20.04, with CUDA 10.0, or CUDA 10.1.

PyTorch & CUDA

PyTorch is an open-source Deep Learning framework that is scalable and flexible for training, stable and support for deployment. It enables simple, flexible experimentation with a self-grading framework designed for easy and python-like execution. Usually, PyTorch is developed with specific CUDA version in mind, so this article will let know how to check it.

Under the hood, PyTorch is a Tensor library (torch), similar to NumPy, which primarily includes an automated classification library (torch.autograd) and a neural network library (torch.nn). This also includes 2 data processing components: torch.multiprocessing allows memory sharing between torch Tensors and processors, and torch.utils offers the DataLoder class. Finally, PyTorch also provides a module (torch.jit) to serialize and customize models from your PyTorch application.

CUDA a general parallel computation architecture and programming model developed by NVIDIA for its graphics cards, i.e., graphical processing units (GPUs). Using CUDA, PyTorch developers can significantly improve the speed of training PyTorch models, by efficiently using GPU resources.

In GPU-accelerated code, the sequential part of the task runs on the CPU for optimized single-threaded performance, while the computed-intensive section, such as PyTorch code, runs on thousands of GPU cores in parallel through CUDA. While using CUDA, developers can code in common languages such as C, C++ , Python and enforce parallelism can extensions in the form of a few simple keywords.

PyTorch has a torch.cuda package to set up and execute CUDA operations effectively. The module keeps track of the currently selected GPU, and all the CUDA tensors you created will be allocated on that system by default. You can modify the chosen device with a context manager for torch.cuda.app.

You check the CUDA sementics for a full explanation of how PyTorch support CUDA.

Method 1 — Use nvcc to check CUDA version for PyTorch

If you have installed the cuda-toolkit package either from Ubuntu’s or NVIDIA’s official Ubuntu repository through sudo apt install nvidia-cuda-toolkit, or by downloading from NVIDIA’s official website and install it manually, you will have nvcc in your path ($PATH) and its location would be /usr/bin/nvcc (by running which nvcc).

nvcc command from the cuda toolkit package 1

To use nvcc to check CUDA version, run

nvcc --version

You will see the output similar in the following screenshot. The last line will show you the CUDA version. Here the CUDA version is 10.1. Your may vary and can be 10.0 or 10.2. You will also find the full text output after the screenshot.

Use nvcc version to check cuda version
vh@varhowto-com:~$ nvcc --version
nvcc: NVIDIA (R) Cuda compiler driver
Copyright (c) 2005-2019 NVIDIA Corporation
Built on Sun_Jul_28_19:07:16_PDT_2019
Cuda compilation tools, release 10.1, V10.1.243

What is nvcc?

nvcc is the NVIDIA CUDA Compiler, thus the name. It is the main wrapper for the CUDA compiler suite. For nvcc‘s other usage, you can use it to compile and link both host and GPU code.

Check out nvcc‘s manpage for more information.

Method 2 — Use nvidia-smi from Nvidia Linux driver

The second way to check CUDA version for PyTorch is to run nvidia-smi that comes from your NVIDIA driver installation, specifically the NVIDIA-utils package. You can either install Nvidia driver from Ubuntu’s official repository or NVIDIA website.

nvidia smi command from nvidia util package
$ which nvidia-smi
/usr/bin/nvidia-smi
$ dpkg -S /usr/bin/nvidia-smi
nvidia-utils-440: /usr/bin/nvidia-smi

To use nvidia-smi to check CUDA version, directly run

nvidia-smi

You will see output similar to the following screenshot. The CUDA version information is on the top right of the output. Here my CUDA version is 10.2. Again, yours might vary if you installed 10.0, 10.1 or even have the older CUDA 9.0.

Interestingly, except for CUDA version. you can also find more information from nvidia-smi, such as the driver version (440.100), GPU name, GPU fan percentage, power usage/capability, memory usage. You can also find the processes that are currently using the GPU. This is useful if you want to see if your PyTorch model or program is using GPU.

Use nvidia smi to check cuda version

Here is the full text output:

vh@varhowto-com:~$ nvidia-smi
Tue Jul 07 10:07:26 2020
+-----------------------------------------------------------------------------+
| NVIDIA-SMI 440.100 Driver Version: 440.100 CUDA Version: 10.2 |
|-------------------------------+----------------------+----------------------+
| GPU Name Persistence-M| Bus-Id Disp.A | Volatile Uncorr. ECC |
| Fan Temp Perf Pwr:Usage/Cap| Memory-Usage | GPU-Util Compute M. |
|===============================+======================+======================|
| 0 GeForce GTX 1070 Off | 00000000:01:00.0 On | N/A |
| 31% 48C P0 35W / 151W | 2807MiB / 8116MiB | 1% Default |
+-------------------------------+----------------------+----------------------+
+-----------------------------------------------------------------------------+
| Processes: GPU Memory |
| GPU PID Type Process name Usage |
|=============================================================================|
| 0 1582 G /usr/lib/xorg/Xorg 262MiB |
| 0 2481 G /usr/lib/xorg/Xorg 1646MiB |
| 0 2686 G /usr/bin/gnome-shell 563MiB |
| 0 3244 G …AAAAAAAAAAAACAAAAAAAAAA= --shared-files 319MiB |
+-----------------------------------------------------------------------------+

What is nvidia-smi?

nvidia-smi (NVSMI) is NVIDIA System Management Interface program. It is also known as NVSMI. nvidia-smi offers monitoring and maintenance capabilities for any of Fermi’s Tesla, Quadro, GRID and GeForce NVIDIA GPUs and higher architecture families. GeForce Titan Series products are supported for most functions with only little detail given for the rest of the Geforce range.

NVSMI is also a cross-platform application that supports both common NVIDIA driver-supported Linux distros and 64-bit versions of Windows starting with Windows Server 2008 R2. Metrics may be used directly by users via stdout, or stored via CSV and XML formats for scripting purposes.

For more information, check out the man page of nvidia-smi.

Method 3 — cat /usr/local/cuda/version.txt

cat /usr/local/cuda/version.txt

Note that this method might not work on Ubuntu 20.04 if you install Nvidia driver and CUDA from Ubuntu 20.04’s own official repository.

cat usr local cuda version.txt

3 ways to check CUDA version for PyTorch and others

Time Needed : 5 minutes

There are three ways to check CUDA version, which are not really specific to PyTorch.

  1. The simplest way is probably just to check a file

    Run cat /usr/local/cuda/version.txt

    Note: this may not work on Ubuntu 20.04cat usr local cuda version.txt

  2. Another approach is through the nvcc command from the cuda-toolkit package.

    nvcc –versionUse nvcc version to check cuda version

  3. The other method is through the nvidia-smi command from the NVIDIA driver you have installed.

    Simply run nvidia-smiUse nvidia smi to check cuda version

Tools
  • nvcc
  • nvidia-smi
Materials
  • Ubuntu
  • PyTorch

+2

By VarHowto Editor

Welcome to VarHowto!

Leave a Reply

Your email address will not be published. Required fields are marked *