如何在没有分配的情况下列出可见的 Tensorflow 设备?

How to list visible Tensorflow devices without allocation?

可以通过以下方式列出当前对 Tensorflow 可见的设备:

from tensorflow.python.client import device_lib
LOCAL_DEVICES = device_lib.list_local_devices()

(参见:

但是,这似乎是对这些 GPU 进行初始化和分配。我们如何在不分配设备的情况下列出设备?

对于 GPU 设备,TensorFlow list_local_devices() 函数是 CUDA APIcuDeviceGetCount()cuDeviceGet()cuDeviceGetProperties() 函数的包装器。 (此外,通常有一个 "CPU device" 使用本地系统中的所有内核。)

最简单的解决方案可能是直接调用这些 CUDA API。此 script 展示了如何使用 ctypes.

从 Python 执行此操作