【tf.keras】Linux 非 root 用户安装 CUDA 和 cuDNN

TensorFlow 2.0 for Linux 使用时报错:(cuDNN 版本低了)

E tensorflow/stream_executor/cuda/cuda_dnn.cc:319] Loaded runtime CuDNN library: 7.4.1 but source was compiled with: 7.6.0.  CuDNN library major and minor version needs to match or have higher minor version in case of CuDNN 7.0 or later version. If using a binary install, upgrade your CuDNN library.  If building from sources, make sure the library loaded at runtime is compatible with the version specified during compile configuration.
...
tensorflow.python.framework.errors_impl.UnknownError: Failed to get convolution algorithm. This is probably because cuDNN failed to initialize, so try looking to see if a warning log message was printed above. [Op:Conv2D]

解决方法:升级 cuDNN。TF 2.0 和 CUDA 10.0、cuDNN 7.6.4 是匹配的。

root 用户

Linux 服务器的用户如果有 root 权限,那直接删除原来的 cuDNN,然后再重装新版本。
删除已有的 cuDNN:

sudo rm -rf /usr/local/cuda/include/cudnn.h
sudo rm -rf /usr/local/cuda/lib64/libcudnn*

进入 cuDNN 解压后的 cuda 文件夹,安装新版本:

sudo cp include/cudnn.h /usr/local/cuda/include/
sudo cp lib64/lib* /usr/local/cuda/lib64/

建立软连接:(以 7.6.4 版本为例)

cd /usr/local/cuda/lib64/
sudo chmod +r libcudnn.so.7.6.4
sudo ln -sf libcudnn.so.7.6.4 libcudnn.so.7
sudo ln -sf libcudnn.so.7 libcudnn.so   
sudo ldconfig

非 root 用户

如果没有 root 权限,一种做法是自己重新装 CUDA 和 cuDNN。

在用户目录下安装 CUDA

从官网 https://developer.nvidia.com/cuda-10.0-download-archive 下载 ubuntu 使用的 cuda_10.0.130_410.48_linux.run,安装指令 sh cuda_10.0.130_410.48_linux.run,之后:

# 按q退出协议说明. 
 
Do you accept the previously read EULA?
accept/decline/quit: accept

Install NVIDIA Accelerated Graphics Driver for Linux-x86_64 410.48?
(y)es/(n)o/(q)uit: no

Install the CUDA 10.0 Toolkit?
(y)es/(n)o/(q)uit: yes

# 改成自己的用户名
Enter Toolkit Location
 [ default is /usr/local/cuda-10.0 ]: /home/wuliyttaotao/cuda-10.0

Do you want to install a symbolic link at /usr/local/cuda?
(y)es/(n)o/(q)uit: n

Install the CUDA 10.0 Samples?
(y)es/(n)o/(q)uit: y

# 使用默认路径,回车
Enter CUDA Samples Location
 [ default is /home/wuliyttaotao ]:    

配置 cuDNN

复制 cuDNN 文件到 CUDA 安装目录:(cuDNN 解压到 ~/cuda 目录了,~/cuda-10.0 为自己设定的 CUDA 安装目录,~ 代表 /home/wuliyttaotao)

cp ~/cuda/include/cudnn.h ~/cuda-10.0/include
cp ~/cuda/lib64/lib* ~/cuda-10.0/lib64

chmod a+r ~/cuda-10.0/include/cudnn.h ~/cuda-10.0/lib64/libcudnn*

建立软连接:

cd ~/cuda-10.0/lib64
ln -sf libcudnn.so.7.6.4 libcudnn.so.7
ln -sf libcudnn.so.7 libcudnn.so
ldconfig -v

配置用户环境变量

修改 ~/.bashrc 文件,将下面两行加进去:(将 wuliyttaotao 改成自己的用户名)

export PATH=/home/wuliyttaotao/cuda-10.0/bin${PATH:+:${PATH}}  
export LD_LIBRARY_PATH=/home/wuliyttaotao/cuda-10.0/lib64${LD_LIBRARY_PATH:+:${LD_LIBRARY_PATH}}

保存 ~/.bashrc 文件后,source ~/.bashrc 使其生效。

References

Linux之cudnn升级方法 -- ZONG_XP
非root用户在linux下安装多个版本的CUDA和cuDNN(cuda 8、cuda 10.1 等)-- 随性拂尘倾心

你可能感兴趣的:(【tf.keras】Linux 非 root 用户安装 CUDA 和 cuDNN)