安裝 CUDA Deep Neural Network (cuDNN) 遇到 test.c:1:10: fatal error: FreeImage.h: No such file or directory 解決方法

安裝 CUDA 的過程感覺一波未平一波又起,安裝完 CUDA 後要安裝 cuDNN 也是問題一堆。

安裝文件可以直接參考官方的 NVIDIA Deep Learning cuDNN Documentation 文件,本文以 cuDNN 版本 8.3.3.40 作為範例。

安裝 cuDNN

安裝完 cuDNN Lib 後會做驗證 cuDNN 有沒有安裝成功,所以會使用 cuDNN sample 來驗證

$ sudo apt-get install libcudnn8=8.3.3.40-1+cuda11.5
$ sudo apt-get install libcudnn8-dev=8.3.3.40-1+cuda11.5
$ sudo apt-get install libcudnn8-samples=8.3.3.40-1+cuda11.5

$ cp -r /usr/src/cudnn_samples_v8/ $HOME

發生問題的原因

在 make 的時候通常都會失敗因為系統預設不會裝 FreeImage 這個 Library

[email protected]:~/cudnn_samples_v8/mnistCUDNN$ make clean && make
rm -rf *o
rm -rf mnistCUDNN
CUDA_VERSION is 11060
Linking agains cublasLt = true
CUDA VERSION: 11060
TARGET ARCH: x86_64
HOST_ARCH: x86_64
TARGET OS: linux
SMS: 35 50 53 60 61 62 70 72 75 80 86 87
test.c:1:10: fatal error: FreeImage.h: No such file or directory
 #include "FreeImage.h"
          ^~~~~~~~~~~~~
compilation terminated.
>>> WARNING - FreeImage is not set up correctly. Please ensure FreeImage is set up correctly. <<<

補上 FreeImage

不過這個 Library 是可以直接使用 apt 直接安裝,所以只要使用 apt 安裝 libfreeimage3 與 libfreeimage-dev。不過我自己在測試的時候系統沒有辦法安裝它的因為缺少 libraw16 我也不知道為什麼

[email protected]:~/cudnn_samples_v8/mnistCUDNN$ sudo apt install libfreeimage3 libfreeimage-dev
Reading package lists... Done
Building dependency tree
Reading state information... Done
Some packages could not be installed. This may mean that you have
requested an impossible situation or if you are using the unstable
distribution that some required packages have not yet been created
or been moved out of Incoming.
The following information may help to resolve the situation:

The following packages have unmet dependencies:
 libfreeimage3 : Depends: libraw16 (>= 0.16.0) but it is not installable
E: Unable to correct problems, you have held broken packages.

補上 libraw16

所以我就試著單獨安裝 libraw16 不過可想而知還是會錯誤,我安裝後得到了以下錯誤,看來 apt 就是缺東西不給我裝

[email protected]:~/cudnn_samples_v8/mnistCUDNN$ sudo apt install libraw16
Reading package lists... Done
Building dependency tree
Reading state information... Done
Package libraw16 is not available, but is referred to by another package.
This may mean that the package is missing, has been obsoleted, or
is only available from another source

E: Package 'libraw16' has no installation candidate

手動補上 libraw16

最後我就找了 libraw16 的 deb 載點直接下載下來安裝,如果運氣好其實就可以再使用 apt 把 libfreeimage3 與 libfreeimage-dev 裝上去,不過我的機器這段還是壞的。

[email protected]:~/download$ wget http://archive.ubuntu.com/ubuntu/pool/main/libr/libraw/libraw16_0.18.8-1ubuntu0.3_amd64.deb
[email protected]:~/download$ sudo dpkg -i libraw16_0.18.8-1ubuntu0.3_amd64.deb
Selecting previously unselected package libraw16:amd64.
(Reading database ... 233536 files and directories currently installed.)
Preparing to unpack libraw16_0.18.8-1ubuntu0.3_amd64.deb ...
Unpacking libraw16:amd64 (0.18.8-1ubuntu0.3) ...
Setting up libraw16:amd64 (0.18.8-1ubuntu0.3) ...
Processing triggers for libc-bin (2.27-3ubuntu1.5) ...

手動補上 libfreeimage3 與 libfreeimage-dev

因此我就再找一次缺少的兩個套件再使用 deb 直接安裝,如此系統就擁有了 FreeImage 這個 Library

[email protected]:~/download$ wget http://archive.ubuntu.com/ubuntu/pool/universe/f/freeimage/libfreeimage3_3.17.0+ds1-5build2_amd64.deb
[email protected]:~/download$ sudo dpkg -i libfreeimage3_3.17.0+ds1-5build2_amd64.deb
[email protected]:~/download$ wget http://archive.ubuntu.com/ubuntu/pool/universe/f/freeimage/libfreeimage-dev_3.17.0+ds1-5build2_amd64.deb
[email protected]:~/download$ sudo dpkg -i libfreeimage-dev_3.17.0+ds1-5build2_amd64.deb

再次測試 cuDNN sample

Library 安裝成功後就可以再使用 make 試試看有沒有正常了,很高興後來安裝就正常了!

[email protected]:~/cudnn_samples_v8/mnistCUDNN$ make clean && make
rm -rf *o
rm -rf mnistCUDNN
CUDA_VERSION is 11060
Linking agains cublasLt = true
CUDA VERSION: 11060
TARGET ARCH: x86_64
HOST_ARCH: x86_64
TARGET OS: linux
SMS: 35 50 53 60 61 62 70 72 75 80 86 87
/usr/local/cuda/bin/nvcc -I/usr/local/cuda/include -I/usr/local/cuda/include -IFreeImage/include  -ccbin g++ -m64    -gencode arch=compute_35,code=sm_35 -gencode arch=compute_50,code=sm_50 -gencode arch=compute_53,code=sm_53 -gencode arch=compute_60,code=sm_60 -gencode arch=compute_61,code=sm_61 -gencode arch=compute_62,code=sm_62 -gencode arch=compute_70,code=sm_70 -gencode arch=compute_72,code=sm_72 -gencode arch=compute_75,code=sm_75 -gencode arch=compute_80,code=sm_80 -gencode arch=compute_86,code=sm_86 -gencode arch=compute_87,code=sm_87 -gencode arch=compute_87,code=compute_87 -o fp16_dev.o -c fp16_dev.cu
nvcc warning : The 'compute_35', 'compute_37', 'sm_35', and 'sm_37' architectures are deprecated, and may be removed in a future release (Use -Wno-deprecated-gpu-targets to suppress warning).
g++ -I/usr/local/cuda/include -I/usr/local/cuda/include -IFreeImage/include   -o fp16_emu.o -c fp16_emu.cpp
g++ -I/usr/local/cuda/include -I/usr/local/cuda/include -IFreeImage/include   -o mnistCUDNN.o -c mnistCUDNN.cpp
/usr/local/cuda/bin/nvcc   -ccbin g++ -m64      -gencode arch=compute_35,code=sm_35 -gencode arch=compute_50,code=sm_50 -gencode arch=compute_53,code=sm_53 -gencode arch=compute_60,code=sm_60 -gencode arch=compute_61,code=sm_61 -gencode arch=compute_62,code=sm_62 -gencode arch=compute_70,code=sm_70 -gencode arch=compute_72,code=sm_72 -gencode arch=compute_75,code=sm_75 -gencode arch=compute_80,code=sm_80 -gencode arch=compute_86,code=sm_86 -gencode arch=compute_87,code=sm_87 -gencode arch=compute_87,code=compute_87 -o mnistCUDNN fp16_dev.o fp16_emu.o mnistCUDNN.o -I/usr/local/cuda/include -I/usr/local/cuda/include -IFreeImage/include -L/usr/local/cuda/lib64 -L/usr/local/cuda/lib64 -L/usr/local/cuda/lib64 -lcublasLt -LFreeImage/lib/linux/x86_64 -LFreeImage/lib/linux -lcudart -lcublas -lcudnn -lfreeimage -lstdc++ -lm
nvcc warning : The 'compute_35', 'compute_37', 'sm_35', and 'sm_37' architectures are deprecated, and may be removed in a future release (Use -Wno-deprecated-gpu-targets to suppress warning).

結論

以上就是這次測試 cuDNN 錯誤的紀錄,如果要找以上的 deb 可以由下面附件找到它們

參考資料