Tensorflow-GPU: How to Install Tensorflow with NVIDIA CUDA,cuDNN and GPU support on Windows

Ilekura Idowu
Analytics Vidhya
Published in
9 min readJun 24, 2021

--

Source

This article will walk you through installing TensorFlow and making it compatible with the NVIDIA GPU on your system. Before we dive into the installation process, let me recap the motivation behind the article.

Motivation

I recently bought a new PC (around April though), the laptop is equipped with a NVIDIA GEFORCE GTX 1650 GPU (4GB RAM). This was to speed up my Machine Learning and Deep Learning projects. The laptop is pretty fast for basic deep learning and machine learning task, but quite slow for computationally expensive training (well what do you expect, when training takes place on the CPU).

The frustration led me to search for methods of leveraging the system’s GPU. I came across a great medium article, Installing Tensorflow with CUDA,cuDNN and GPU support on Windows 10, unfortunately after meticulously following the instructions in the Article. I was still not able to get my GPU compatible with Tensorflow. After several types of research on the internet, I successfully got my GPU compatible with Tensorflow. I hope this article will be of help to you and help you avoid errors that I encountered. Okay, enough of the story, let us dive into the installation procedures. Before we dive into the installation procedures, it will be cool to understand what NVIDIA CUDA, cuDNN and Tensorflow is right??

Tensorflow

“TensorFlow is an end-to-end open-source platform for machine learning/deep learning. It has a comprehensive, flexible ecosystem of tools, libraries and community resources that lets researchers push the state-of-the-art in ML and developers easily build and deploy ML-powered applications.” Source

NVIDIA CUDA

‘“CUDA is a parallel computing platform and programming model developed by Nvidia for general computing on its own GPUs (graphics processing units). CUDA enables developers to speed up compute-intensive applications by harnessing the power of GPUs for the parallelizable part of the computation.” Source. CUDA speeds up machine learning and Deep Learning training time by utilizing what is called Parallel computing ( i.e instead of training on just a single compute instance, it divides the task and trains on different compute instances). CPU computes on a single instance while GPU computes on multiple instances, which makes operations on GPU faster than CPU. CUDA is what enables your GPU to function, there are other CUDA alternative toolkits like OpenCL but at the moment Tensorflow is more compatible with NVIDIA ( one of the reasons why I bought a NVIDIA powered laptop).

cuDNN

“The NVIDIA CUDA® Deep Neural Network library (cuDNN) is a GPU-accelerated library of primitives for deep neural networks. cuDNN provides highly tuned implementations for standard routines such as forward and backward convolution, pooling, normalization, and activation layers. Deep learning researchers and framework developers worldwide rely on cuDNN for high-performance GPU acceleration.” Source

Installation Steps

Kindly note that I will be lifting some contents from this great article by Dr Joanne Kitson, the article was helpful to me. The only issue is that the article is old and you will encounter errors with the latest version of NVIDIA CUDA.

STEP 1: Softwares that will be installed

To install the software, you must have an NVIDIA CUDA-compatible GPU installed on your PC ( it is very important). You can use this link to find out what GPU is installed on your system.

  • Microsoft Visual Studio
  • NVIDIA CUDA Toolkit
  • NVIDIA cuDNN
  • Python (if it is not installed)
  • Tensorflow

Step 2: Download Visual Studio Express

Visual Studio is a Prerequisite for CUDA Toolkit

According to NVIDIA, the visual studio must be installed on your System before you can use the NVIDIA CUDA Toolkit(You can use this link to access the prerequisite).

Source

If you attempt to download and install CUDA Toolkit for Windows without having first installed Visual Studio. This error notification will pop up “No supported version of Visual Studio was found. Some components of the CUDA Toolkit will not work properly”. There is Visual Studio Code and Visual Studio( Kindly note the difference)

Selecting and downloading Visual Studio Express

The most recent version of Visual Studio ( which is free) is the Visual Studio Express Community Version 2019. You can access the software with this link.

Pictorial Overview of Visual Studio Express (Image by author)

Kindly click on the download community 2019 link, your download will start. Once your download has completed, you can install it like any other software. Once you click on install/open, you will be presented with this image

Installation window for Visual Studio Community 2019. (Image by author)

Kindly click on Available and install Visual Studio Community 2019( I have installed Visual Studio Community 2017). During installation, Visual Studio prompts you to either continue without workloads or add workloads. Kindly click on ‘Continue’

Source

Kindly reboot/restart your PC after installing Visual Studio, this will prevent Visual Studio was still operating and was preventing the installation of CUDA Toolkit error.

Step 3: Download CUDA Toolkit for Windows 10

Image by author

Tensorflow ≥2.5.0 requires CUDA Toolkit 11.2 Version, you can access the software with this link

Image by author

Click on CUDA Toolkit 11.2.0 and download CUDA Toolkit 11.2.0).

Image by author

Select your target platform, click on the red circled symbols above and click on Download.

Image by author

Running the base installer which has been downloaded will produce the CUDA Setup window, as shown below

CUDA Setup Packages for CUDA base installer. Image by author

Click on ok ( don’t change the directory), CUDA installer will be extracted to your PC. NVIDIA CUDA Toolkit installation will begin, with a confirmation message. The installer will check if your system meets the requirement, and you will be presented with the screen below.

Click on the Express Installation option and click on the Next button. Source

Just keep clicking on the Next button until you get to the last step( Finish), and click on launch Samples.

Step 5: Download and Install cuDNN

Now that CUDA 11.2 has been installed, the next step is to find a compatible version of cuDNN. Tensorflow ≥ 2.5.0 requires the cuDNN SDK 8.1.0 version. Use this link to access NVIDIA cuDNN.

Click on Download cuDNN. Image by author

You will need to become a member of the NVIDIA Developer Program to download cuDNN( it is free to join).

Image by author

Click on Join now and fill the registration form. Once you are done, you will be automatically taken to the download page as shown below.

Image by author

Click on the link and download the file. After downloading the file, kindly extract the file (kindly note the path).

Since CUDA depends on cuDNN, you will need to copy some files from cuDNN into CUDA.

They are

  • cudnn64_8.dll
  • cudnn.h
  • cudnn.lib

5.1 cudnn64_8.dll

How to locate cudnn64_8.dll file. Image by author

You will need to copy cudnn64_8.dll directly from the bin sub-folder path. The bin sub-folder is located inside cuda which is a subfolder inside cuDNN.

After copying the file, you will need to paste the file inside the bin subfolder located inside CUDA Folder. CUDA folder is located inside NVIDIA Computing Toolkit.

Paste the file into this directory(no need to create any folder). Image by author

5.2 cudnn.h

Like what was done above, you will need to copy cudnn.h from the include sub-folder which is located inside cuda folder. The cuda folder is located inside cuDNN.

Copy cudnn.h file. Image by author

You will need to copy cudnn.h into include subfolder located inside CUDA. The CUDA folder is located NVIDIA GPU Computing Toolkit Folder.

Paste the copied cudnn.h file into this folder. Image by author

5.3 cudnn.lib

You will need to copy cudnn.lib from the x64 subfolder which is located inside lib subfolder. The lib subfolder is located inside cuDNN.

Copy cudnn.lib. Image by author
Paste the copied cudnn.lib into the lib subfolder located inside CUDA. Image by author

Renaming cursolver64_11.dll

You will need to rename cusolver64_11.dll to cusolver64_10.dll, else you will get this error “Could not load dynamic library ‘cusolver64_10.dll’; dlerror: cusolver64_10.dll not found’ when you try to leverage your GPU for Tensorflow training.

I have renamed cursolver64_11.dll to cursolver64_10.dll( you need to do this). Image by author

Step 5.3: Confirming that CUDA environment variables are set in Windows

For CUDA to function properly, you will need to ensure that CUDA environment variables are set in your PC‘s Path. To confirm, you will need to check the Environment Variables. This can be found by following these steps

Control Panel -> System and Security -> System-> Advanced System settings.

This will open up a window, click on the Advanced tab. as shown below

Image by author

These paths are automatically added, but in case you can’t find them. Just type in C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v11.2\bin and C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\V11.2\libnvvp.

Step 6: Install Python (In case you haven't done that before)

I recommend using Anaconda to download python, you can access anaconda with this link.

Image by author

It is pretty easy to install anaconda, instructions to easily install will be displayed once you click on install.

Step 7: Installing Tensorflow (If it is not installed)

Open your terminal, activate conda and pip install TensorFlow. Image by author

Step 8: Test Installation of TensorFlow and its access to GPU

Open your terminal( command prompt), type conda activate and type python ( to enter python interactive mode)

# importing tensorflow package

import tensorflow as tf

Run this code to test CUDA support for your Tensorflow installation,

tf.test.is_built_with_cuda()

To confirm that the GPU on the system is accessible by Tensorflow, you can test with this code

tf.test.is_gpu_available(cuda_only=False,min_cuda_compute_capability=None

It will return True after few minutes.

Image by author

Optional Step

You will likely encounter this error “Not creating XLA devices, tf_xla_enable_xla_devices not set”. XLA helps to speed up training on GPU, to set tf_xla_enable_xla_devices you can follow this step

Image by author

Navigate to Environment Variables, navigate to System Variables and click on the New button. Fill in the Variable name and Variable value as shown above.

Conclusions

This article aimed to build on this old article by Dr Joanne Kitson, and provide an updated instruction for installing TensorFlow with NVIDIA CUDA, cuDNN and GPU support on windows without errors. I might write on how to Install CUDA,cuDNN and GPU on Linux Ubuntu Distro in my next article. Thanks for reading up to this point, kindly subscribe to my newsletter for early access to my articles.

References

--

--

Ilekura Idowu
Analytics Vidhya

Christian| Machine Learning Engineer with a keen interest in NLP and Computer Vision| Currently Building Etacude