Python is one of the easiest programming languages that even beginners can pick up and understand. At the same time, it’s also incredibly powerful, meaning it can be used for many applications.
However, as far as programming has come in 2022, it’s still hard, and random bugs and glitches are the order of the day. In this article, we’re going over the “Runtimeerror: cudnn error: cudnn_status_not_initialized” error in Python when using PyTorch and giving you four ways to fix the problem.
Also read: How to solve the Tower of Hanoi problem using Python?
Use the right CUDA version
The simplest way to fix this issue is by using the right CUDA version (11.1). You can use the pip command below to install CUDA before running your program.
pip install torch==1.8.0+cu111 torchvision==0.9.0+cu111 torchaudio==0.8.0 -f https://download.pytorch.org/whl/torch_stable.html
Downgrade to Torch-1.7.1
If you’re using another CUDA version and can’t upgrade (or downgrade) it, you can try downgrading Torch to version 1.7.1 and torchvision to version 0.8.2 by using the commands below.
pip install torch==1.7.1 pip install torchvision==0.8.2
Check your GPU memory
Another thing you need to keep in mind is that you should have enough GPU memory to run the program beforehand. You can do this by running the nvidia-smi command to see what processes are using GPU resources and then close them using the kill command and the process’ PID.
Force cuDNN initialisation
In some cases, you might find that you don’t have enough GPU memory available because PyTorch itself takes up a lot of GPU resources. To fix this, you can manually force cuDNN initialisation at the beginning of your script using a mock convolution as follows.
def force_cudnn_initialization(): s = 32 dev = torch.device('cuda') torch.nn.functional.conv2d(torch.zeros(s, s, s, s, device=dev), torch.zeros(s, s, s, s, device=dev))
Calling the aforementioned function forces cuDNN initialisation ignoring GPU limits and can help you fix the error.
Also read: Fix: Python was not found; run without arguments to install