Tensorflow-GPU: How to Install Tensorflow with NVIDIA CUDA,cuDNN and GPU support on Windows
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).
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.
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
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’
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
Tensorflow ≥2.5.0 requires CUDA Toolkit 11.2 Version, you can access the software with this link
Click on CUDA Toolkit 11.2.0 and download CUDA Toolkit 11.2.0).
Select your target platform, click on the red circled symbols above and click on Download.
Running the base installer which has been downloaded will produce the CUDA Setup window, as shown below
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.
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.
You will need to become a member of the NVIDIA Developer Program to download cuDNN( it is free to join).
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.
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
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.
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.
You will need to copy cudnn.h into include subfolder located inside CUDA. The CUDA folder is located NVIDIA GPU Computing Toolkit Folder.
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.
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.
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
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.
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)
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.
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
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
- https://towardsdatascience.com/installing-tensorflow-with-cuda-cudnn-and-gpu-support-on-windows-10-60693e46e781 ( Inspired the write-up and served as a guide while installing TensorFlow)
- Python — Tensorflow GPU Could not load dynamic library ‘cusolver64_10.dll’; dlerror: cusolver64_10.dll not found — Stack Overflow
- cuda_install