GPU Setup#

Warning

This tutorial is specifically for Nvidia GPU. Not sure if the codes will run for other GPUs.

Check your gpu version running nvidia-smi from shell. Install the compatible version of libraries. Your pytorch supported version and cudatoolkit version need to match. More on how to get the cuda version is here.

Also reboot the pc after reinstall if needed. For this tutorial, I am using the current latest version.

PyTorch#

Check if torch and cuda is already installed. Using python code,

import torch
torch.cuda.is_available()

Or from command line,

python -c "import torch;print(torch.cuda.is_available())"

If this shows False, install PyTorch with CUDA using the following code collected from here,

# using anaconda
conda install pytorch==1.13.1 pytorch-cuda=11.7 -c pytorch -c nvidia
# if you want to install additional libraries
conda install pytorch==1.13.1 torchvision==0.14.1 torchaudio==0.13.1 pytorch-cuda=11.7 -c pytorch -c nvidia

# or using pip
pip install torch==1.13.1+cu117 -f https://download.pytorch.org/whl/torch_stable.html

# if you want to install additional libraries
pip install torch==1.13.1+cu117 torchvision==0.14.1+cu117 torchaudio==0.13.1+cu117 -f https://download.pytorch.org/whl/torch_stable.html

This will install PyTorch 1.13.1 and cuda 11.7. Recheck if it is properly installed. Related issue.

Note

You won’t have to install cudnn separately for torch because that comes within the torch package. -

Source: Why doesn’t PyTorch install the REAL nvidia cuDNN pip package?

TensorFlow#

Check if already installed#

Using python,

import tensorflow as tf
tf.config.list_physical_devices('GPU')

Or using bash script,

python -c "import tensorflow as tf; print(tf.config.list_physical_devices('GPU'))"

Install#

If the previous step returns an empty list, install the library using the following code (source). Do not install TensorFlow with conda. It may not have the latest stable version. pip is recommended since TensorFlow is only officially released to PyPI. Earlier versions used to come with different library for gpu (tensorflow-gpu), release updates of those have been discontinued.

For windows, Tensorflow 2.10.0 is the last version installable by windows native. Later versions have to be installed by wsl. On linux, there is no major change. The following is for windows.

pip install tensorflow==2.10.*

conda install -c conda-forge cudnn=8.1.0
set CUDA_VISIBLE_DEVICES=1

This installs both cpu and gpu versions. Verify it has been installed. Installing cudnn doesn’t work with pip. It is available on the Anaconda server. It can also be manually installed following the instructions here.

If this still returns an empty list, check the following issue.

pip uninstall protobuf
pip install --upgrade --force-reinstall tensorflow==2.10.*