使用 TensorFlow 2.0.0 时找不到 GPU

GPU not found while using TensorFlow 2.0.0

我正在迁移到 Tensorflow 2.0,我正在研究

Ubuntu 18.04

CUDA 10.2

Python 3.7

ZOTAC GeForce® GTX 1080 Ti Mini (ZT-P10810G-10P)

当我 运行 nvcc -Vnvidia-smi 时,我可以看到 GPU。但是下面的命令没有列出GPU。

tf.test.is_gpu_available(cuda_only=False, min_cuda_compute_capability=None)
Output: False

tf.config.experimental.list_physical_devices(device_type=None)
Output: [PhysicalDevice(name='/physical_device:CPU:0', device_type='CPU'),
PhysicalDevice(name='/physical_device:XLA_CPU:0', device_type='XLA_CPU')]

from keras import backend as K
K.tensorflow_backend._get_available_gpus()
Output: []

编辑 1:在 tensorflow gpu support website 他们有针对 tensorflow 1.15tensorflow 1.14 的说明,但没有针对更高版本的说明。

重启并重新安装 tensorflow-gpu 也没有帮助。

更新 CUDA 和 cudnn 也没有用。

来自link https://www.tensorflow.org/install/source#linux,对于tensorflow-2.0.0,它需要cuDNN 7.4版和CUDA 10.0。不确定你的 cuDNN 版本是什么(你可以检查它:

cat ${CUDNN_H_PATH} | grep CUDNN_MAJOR -A 2

应该return:

#define CUDNN_MAJOR 7
#define CUDNN_MINOR 5
#define CUDNN_PATCHLEVEL 0
--
#define CUDNN_VERSION (CUDNN_MAJOR * 1000 + CUDNN_MINOR * 100 + CUDNN_PATCHLEVEL)

=> 这意味着它的 7.5.0

我读到如果上面的 link 指定 7.4,则您不能使用任何其他版本(甚至 7.5、7.6 等)。因此,请在 cuDNN 下载中查找存档版本。

我建议您卸载 /usr/local 文件夹 中的所有 nvidia 驱动程序、tensorflow-gpu、cuDNN、所有 cuda 库和工具包,然后重新安装。不要同时安装 tensorflow 和 tensorflow-gpu。只需安装 tensorflow-gpu 一个。

卸载 nvidia-cuda-toolkit 及其依赖项的方法如下:

sudo apt-get remove --auto-remove nvidia-cuda-toolkit

记得编辑你的 ~/.bash_profile 文件

正确卸载和清除所有内容后,安装 nvidia-driver-418(我个人使用此版本,但根据 nvidia 文档,对于 CUDA 10,只要 410.xx 没问题 - nvidia docs tensorflow release notes):

sudo add-apt-repository ppa:graphics-drivers/ppa
sudo apt-get update
sudo apt-get install nvidia-driver-418

然后重新启动并使用 nvidia-smi 命令检查它说: NVIDIA-SMI 430.50 驱动程序版本 430.50

然后下载并安装CUDA 10.0

请不要下载任何其他版本(不是 10.1、10.2 等 - 对不起,烦人)

只记得 select n (no) 安装 NVIDIA 加速图形驱动程序。会出现安装不完整的错误提示,可以忽略。

确保 ~/.bashrc 文件包含 cuda-10.0

export PATH=/usr/local/cuda-10.0/bin:$PATH
export LD_LIBRARY_PATH=/usr/local/cuda-10.0/lib64 

然后下载适用于 CUDA 10.0 的 cuDNN v7.4.2。使用tar命令解压tgz文件后,cd进入cuda文件夹,将lib64/目录下的内容及其所有内容复制到/usr/local/cuda/lib64/ 路径。另外,将 include/ 文件夹复制到 /usr/local/cuda/include/ 路径中,如下所示:

sudo cp -P lib64/* /usr/local/cuda/lib64/
sudo cp -P include/* /usr/local/cuda/include/

检查

tf.test.is_gpu_available()

它应该 return 正确。

是的,非常感谢@运行方

TF 2.0 can work on GPU only with CUDA 10.0 and cuDNN 7.4 - You can check the dependencies here

我完成了以上所有操作,目前我的版本是 CUDA 10.0、cuDNN 7.4.1 和 NVIDIA-SMI 410.129。您可以检查TF-CUDA dependencies and NVIDIA Drivers - CUDA dependencies

对我来说,要检查 cuDNN 版本,以下命令适用于 Ubuntu 18.04 -

cat /usr/include/cudnn.h | grep CUDNN_MAJOR -A 2

但是执行上述所有操作并仅安装 tensorflow-gpu 并不能使 GPU 正常工作。

最终起作用的是以下 - 先卸载所有 tensorflow 包,然后只安装 -

pip uninstall tensorflow tf-nightly tensorboard tb-nightly tensorflow-estimator

pip install tf-nightly-gpu-2.0-preview

工作起来很有魅力。

按照 this 教程相应地安装 Tensorflow 2.0 和依赖项。

您也可以通过 this, this and this 文档完全卸载较新的 CUDA 和 cuDNN 版本并安装较旧的 CUDA 10.0 版本。

完成上述所有操作后,我在 运行 model.fit(..) -

之后出现错误

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]

我在此处关注 并关注@RadV 的回答并且成功了。他的回答-

Upgrad per instruction on this TensorFlow GPU instructions page

所以现在我有,

Ubuntu 18.04

when I run nvidia-smi in the terminal, it shows CUDA 10.2

Output of which nvcc in the terminal gives /usr/local/cuda-10.0/bin/nvcc