【问题标题】:Tensorflow identifying GPUs, but not recognizing them under the list of devicesTensorflow 识别 GPU,但在设备列表下无法识别它们
【发布时间】:2020-05-05 18:52:23
【问题描述】:

我在集群上的配置文件中安装了 tensorflow-gpu 1.15 版,该集群可以访问 2 个 GPU。我能够通过运行来验证这一点

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 与能够支持 tensorflow 线性代数例程的 GPU 相关。但是,当我运行 GPU 测试功能时

tf.test.is_gpu_available()

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

【问题讨论】:

  • 你是唯一使用这些 GPU 的人吗?你有安装日志吗?如果没有,您可以在虚拟环境中重复该过程吗?什么操作系统和CUDA?可以用 conda 吗?
  • 我是唯一使用这些 GPU 的人。不幸的是,我没有安装日志文件,也没有管理员权限。操作系统是 RedHat Linux 7。我确实使用了 conda 虚拟环境(对应于 Python 3.7)和 CUDA 10.0。
  • 那你为什么用pip安装呢?创建一个新的 conda 环境并使用 conda 进行安装。
  • 其实我认为问题只是 tf.test.is_gpu_available() 函数中的一个错误,因为它检查 device_type "GPU,而不是 "XLA_GPU"。我认为你的 GPU 工作正常,它是只是该功能具有误导性(无论如何它已被弃用)。最好的测试是运行代码并检查 GPU 是否与 nvidia-smi 一起使用
  • @LukaszTracewski 我实际上也尝试使用 conda 安装 tf-gpu,但这有问题。在这种情况下,GPU 根本没有被识别,只是 CPU 出现在可用设备列表中。

标签: python tensorflow deep-learning gpu python-3.7


【解决方案1】:

推荐的方法是检查TensorFlow是否使用GPU如下:

tf.config.list_physical_devices('GPU') 

输出:

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

以下内容还将返回您的 GPU 设备的名称。

import tensorflow as tf
tf.test.gpu_device_name()

如果安装了non-GPU 版本的软件包,该函数还将返回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.]]

【讨论】:

    猜你喜欢
    • 2020-01-14
    • 2023-03-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-10-24
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多