13
Could not load library cudnn_cnn_infer64_8.dll. Error code 126
Please make sure cudnn_cnn_infer64_8.dll is in your library path!

I keep getting this error when I try to use TensorFlow with GPU, I've installed CUDA, cuDNN, and all the drivers multiple times according to the instructions. But nothing seems to work. If I use notebook then TensorFlow uses the CPU, with VS code notebook extension i can use the gpu but it stops the session at 1st epoch, when I tried to run it as a normal python file. the above error occurred.

Complete terminal output:

Found 14630 validated image filenames belonging to 3 classes.
Found 1500 validated image filenames belonging to 3 classes.
2021-11-08 11:03:58.000354: I tensorflow/core/platform/cpu_feature_guard.cc:151] This TensorFlow binary is optimized with oneAPI Deep Neural Network Library (oneDNN) to use the following CPU instructions in performance-critical operations:  AVX AVX2
To enable them in other operations, rebuild TensorFlow with the appropriate compiler flags.
2021-11-08 11:03:58.603592: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1525] Created device /job:localhost/replica:0/task:0/device:GPU:0 with 2775 MB memory:  -> device: 0, name: NVIDIA GeForce GTX 1050 Ti, pci bus id: 0000:01:00.0, compute capability: 6.1        
Epoch 1/10
2021-11-08 11:04:07.306011: I tensorflow/stream_executor/cuda/cuda_dnn.cc:366] Loaded cuDNN version 8300
Could not load library cudnn_cnn_infer64_8.dll. Error code 126
Please make sure cudnn_cnn_infer64_8.dll is in your library path!
E:\MyWorkSpace\animal_detect>

The code snippet:

import tensorflow as tf 
from tensorflow.keras.preprocessing.image import ImageDataGenerator 
from tensorflow.keras import layers 
from tensorflow.keras import Model 
from tensorflow.keras.preprocessing.image import ImageDataGenerator
from tensorflow.keras.applications.vgg16 import VGG16
import pandas as pd
import numpy as np

train_df = pd.read_csv('train.csv')
test_df = pd.read_csv('test.csv')
train_gen = ImageDataGenerator(rescale = 1./255.,rotation_range = 40, width_shift_range = 0.2, height_shift_range = 0.2, shear_range = 0.2, zoom_range = 0.2, horizontal_flip = True)
test_gen = ImageDataGenerator( rescale = 1.0/255. )
train_set = train_gen.flow_from_dataframe(train_df,x_col='loc',y_col='label',batch_size=20,target_size=(224,224))
test_set = train_gen.flow_from_dataframe(test_df,x_col='loc',y_col='label',batch_size=20,target_size=(224,224))
base_model = VGG16(input_shape = (224, 224, 3),
include_top = False,
weights = 'imagenet')
for layer in base_model.layers:
    layer.trainable = False
x = layers.Flatten()(base_model.output)
x = layers.Dense(512, activation='relu')(x)
x = layers.Dropout(0.5)(x)
x = layers.Dense(3, activation='sigmoid')(x)

model = tf.keras.models.Model(base_model.input, x)

model.compile(optimizer = tf.keras.optimizers.RMSprop(learning_rate=0.0001), loss = 'categorical_crossentropy',metrics = ['acc'])
vgghist = model.fit(train_set, validation_data = test_set, steps_per_epoch = 100, epochs = 10)

the same code has been used for Jupyter-notebook, VS code notebook extension and as a normal python file

Device specifications:

processor: Intel i5 gpu: Nvidia Geforce 1050ti

Cuda version: 11.5 cuDNN version: 8.3

talonmies
  • 70,661
  • 34
  • 192
  • 269
Hseran
  • 141
  • 1
  • 1
  • 6

2 Answers2

35

For those still having this issue, please make sure you also have completed this step:

enter image description here

Download, unzip and add zlibwapi.dll to your system path. Good luck!

androidu
  • 4,678
  • 6
  • 36
  • 51
  • 2
    Thanks, It worked. I did ignore this installation guide(That's my fault). I have compiled tf2.7 for cuda11.5 and cudnn8.3 vary far days ago but cannot use cudnn8.3 until added zlibwapi.dll to system path. Although zlibwapi. dll does not affect the use of cudnn8.2, it does affect the correct operation of cudnn8.3. – Little Train Dec 26 '21 at 06:12
  • 3
    I think this is the most correct solution, since it is included in the official cuDNN installation guide. Also, there is no need to downgrade the cuDNN version. – Tedpac Dec 31 '21 at 22:29
  • 1
    Wow. There were so many gotchas in getting CuDNN accepted when installing dlib. And after I managed to get it installed, this zlib thing was a final gotcha preventing my python script from getting cuDNN. Thank you. – Doochz Jan 22 '22 at 10:18
  • link is expired – Shirish Bajpai Mar 29 '22 at 13:40
  • 1
    Apologies for not being clear. Link mentioned in the answer works but the link to ZLIB DLL is expired. http://www.winimage.com/zLibDll/zlib123dllx64.zip – Shirish Bajpai Mar 30 '22 at 05:19
  • @ShirishBajpai Aw man. You are right. They didn't maintain their api docs up to date :facepalm: – androidu Mar 30 '22 at 10:48
  • 1
    CUDA 11.8 and CUDNN 8.9 here. I installed zlib, added the zlibwapi.dll into the relevant folder. I still get a similar error, difference being there is no error code. Could not locate cudnn_ops_infer64_8.dll. Please make sure it is in your library path! I also already made sure cudnn_ops_infer64_8.dll is already in the bin folder in the CUDA folder – fatbringer Aug 07 '23 at 06:06
  • 1
    DO NOT download zip files from random websites. I don't know why nvidia is hiding it that good but here's an official answer with matching downlods: https://forums.developer.nvidia.com/t/could-not-load-library-cudnn-cnn-infer64-8-dll-error-code-193/218437/16 – Waescher Aug 16 '23 at 15:18
17

The same "errors" as me. Even though I have re-compiled the tensorflow-gpu 2.6.0 with "Cuda version: 11.5 cuDNN version: 8.3". The "errors" disappeared when I changed cudnn version to 8.2 but kept cuda version as 11.5. (Re-compiled is als needed) So I think this error must on "cuDNN".

Please See androidu's answer. It worked perfectly.

Little Train
  • 707
  • 2
  • 11
  • 6
    that fixed it for me, thx! i went for `Download cuDNN v8.2.2 (July 6th, 2021), for CUDA 11.4` – kritzikratzi Nov 10 '21 at 18:28
  • 6
    it also worked for me: downloaded and installed **CUDA Toolkit 11.5**, then Download the incompatible cuDNN v8.2.4 (September 2nd, 2021), for **CUDA 11.4**, placed the files in correct folders as in https://docs.nvidia.com/deeplearning/cudnn/install-guide/index.html, and voily, THE PAIN IS GONE! THX @Little Train – jib fashr Nov 27 '21 at 18:14
  • Thanks! This helped me too. I have CUDA Toolkit 11.5 and had cuDNN v8.3.0. – Jeffy Nov 29 '21 at 16:20
  • @jib-fashr Works for me. Thank you! – ipid Dec 08 '21 at 17:10
  • Worked like a charm! – Abend Dec 13 '21 at 04:08
  • Thanks. The problem was solved in an unbelievable way. – Alireza Rezaee Dec 17 '21 at 06:45
  • For 10.1 CUDA Tensorflow complained about CUDArt 11 so I downloaded 11.2 CUDA then matching cuDNN 8.3 but it didn't work, until I tried this solution and got cuDNN 8.2 – Zubair Ahmed Dec 21 '21 at 11:12
  • Just try adding `zlibwapi.dll` to your system path, there is no need to downgrade. See my answer below. Good luck. – androidu Dec 28 '21 at 20:53