Tensorflow 识别 GPU,但未在设备列表下识别它们

Tensorflow identifying GPUs, but not recognizing them under the list of devices

我在集群上的配置文件中安装了 tensorflow-gpu 版本 1.15,它可以访问 2 个 GPU。我能够通过 运行ning

验证这一点
from tensorflow.python.client import device_lib
device_lib.list_local_devices()

以上语句生成的本地设备列表为:

[name: "/device:CPU:0"
 device_type: "CPU"
 memory_limit: 268435456
 locality {
 }
 incarnation: 17161457237421390575,
 name: "/device:XLA_CPU:0"
 device_type: "XLA_CPU"
 memory_limit: 17179869184
 locality {
 }
 incarnation: 2136131381156225295
 physical_device_desc: "device: XLA_CPU device",
 name: "/device:XLA_GPU:0"
 device_type: "XLA_GPU"
 memory_limit: 17179869184
 locality {
 }
 incarnation: 5626920946153973344
 physical_device_desc: "device: XLA_GPU device",
 name: "/device:XLA_GPU:1"
 device_type: "XLA_GPU"
 memory_limit: 17179869184
 locality {
 }
 incarnation: 1069390960246559975
 physical_device_desc: "device: XLA_GPU device"]

清楚地显示了列出的 GPU 设备。在进一步搜索中,我了解到 XLA_GPU 与能够支持张量流线性代数例程的 GPU 相关。但是,当我 运行 GPU 测试函数

tf.test.is_gpu_available()

输出为假。我很困惑这里是否未检测到 GPU,或者 tensorflow-gpu 安装(通过 pip)存在问题。 如有任何意见,我们将不胜感激。

推荐的方法是检查 TensorFlow 是否正在使用 GPU,如下所示:

tf.config.list_physical_devices('GPU') 

输出:

[PhysicalDevice(name='/physical_device:GPU:0', device_type='GPU')]

以下还将return您的 GPU 设备的名称。

import tensorflow as tf
tf.test.gpu_device_name()

如果安装了 non-GPU 版本的软件包,该功能也会 return False。使用 tf.test.is_built_with_cuda 验证 TensorFlow 是否是使用 CUDA 支持构建的。

注意tf.test.is_gpu_available 已弃用。请参考 here

警告:此功能已弃用。它将在未来的版本中被删除。更新说明:改用tf.config.list_physical_devices('GPU')。

最好的测试方法是 运行 编码并检查 GPU 是否正在使用 Matias Valdenegro 提到的 nvidia-smi 或 运行 简单代码如下

import tensorflow as tf
with tf.device('/GPU:0'):
    a = tf.constant([1.0, 2.0, 3.0, 4.0, 5.0, 6.0], shape=[2, 3], name='a')
    b = tf.constant([1.0, 2.0, 3.0, 4.0, 5.0, 6.0], shape=[3, 2], name='b')
    c = tf.matmul(a, b)

with tf.compat.v1.Session() as sess:
    print (sess.run(c))

输出:

[[22. 28.]
 [49. 64.]]