【问题标题】:Does TensorFlow by default use all available GPUs in the machine?默认情况下,TensorFlow 是否使用机器中所有可用的 GPU?
【发布时间】:2016-04-22 10:37:29
【问题描述】:

我的机器中有 3 个 GTX Titan GPU。我使用 cifar10_train.py 运行 Cifar10 中提供的示例并得到以下输出:

I tensorflow/core/common_runtime/gpu/gpu_init.cc:60] cannot enable peer access from device ordinal 0 to device ordinal 1
I tensorflow/core/common_runtime/gpu/gpu_init.cc:60] cannot enable peer access from device ordinal 1 to device ordinal 0
I tensorflow/core/common_runtime/gpu/gpu_init.cc:127] DMA: 0 1 
I tensorflow/core/common_runtime/gpu/gpu_init.cc:137] 0:   Y N 
I tensorflow/core/common_runtime/gpu/gpu_init.cc:137] 1:   N Y 
I tensorflow/core/common_runtime/gpu/gpu_device.cc:694] Creating TensorFlow device (/gpu:0) -> (device: 0, name: GeForce GTX TITAN, pci bus id: 0000:03:00.0)
I tensorflow/core/common_runtime/gpu/gpu_device.cc:694] Creating TensorFlow device (/gpu:1) -> (device: 1, name: GeForce GTX TITAN, pci bus id: 0000:84:00.0)

在我看来,TensorFlow 正在尝试在两个设备(gpu0 和 gpu1)上进行自我初始化。

我的问题是为什么它只在两台设备上这样做,有什么办法可以防止这种情况发生吗? (我只希望它像只有一个 GPU 一样运行)

【问题讨论】:

  • 这真的很有帮助。但是为什么 TensorFlow 会自动为机器上的所有设备初始化呢?顺便说一句,我会接受的答案。
  • TensorFlow 的目标是“从研究到生产”。使用所有计算能力的默认设置似乎满足了尽快完成工作的期望。很好,实际上可以调整。但是您提到了 3 个 GPU,而您的日志中只显示了 2 个。为什么会这样?
  • 我刚刚发现这是因为第三个 GPU 没有运行(出于某种我还不知道的原因),所以我猜如果是这样,TensorFlow 也会使用它。跨度>

标签: machine-learning computer-vision gpu tensorflow


【解决方案1】:

见:Using GPUs

手动放置设备

如果您希望特定操作在您选择的设备上运行,而不是自动为您选择的设备,您可以使用 tf.device 创建一个设备上下文,以便该上下文中的所有操作都具有相同的设备分配。

# Creates a graph.
with tf.device('/cpu: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)
# Creates a session with log_device_placement set to True.
sess = tf.Session(config=tf.ConfigProto(log_device_placement=True))
# Runs the op.
print(sess.run(c))

您将看到现在 a 和 b 已分配给 cpu:0。由于没有为MatMul 操作明确指定设备,TensorFlow 运行时将根据操作和可用设备(本例中为 gpu:0)选择一个设备,并在需要时自动在设备之间复制张量。

Device mapping:
/job:localhost/replica:0/task:0/gpu:0 -> device: 0, name: Tesla K40c, pci bus
id: 0000:05:00.0
b: /job:localhost/replica:0/task:0/cpu:0
a: /job:localhost/replica:0/task:0/cpu:0
MatMul: /job:localhost/replica:0/task:0/gpu:0
[[ 22.  28.]
 [ 49.  64.]]

较早的答案 2。

见:Using GPUs

在多 GPU 系统上使用单个 GPU

如果您的系统中有多个 GPU,则默认选择 ID 最低的 GPU。如果您想在不同的 GPU 上运行,则需要明确指定首选项:

# Creates a graph.
with tf.device('/gpu:2'):
  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)
# Creates a session with log_device_placement set to True.
sess = tf.Session(config=tf.ConfigProto(log_device_placement=True))
# Runs the op.
print sess.run(c)

较早的答案 1。

来自CUDA_VISIBLE_DEVICES – Masking GPUs

您的 CUDA 应用程序是否需要针对特定​​的 GPU?如果你是 编写启用 GPU 的代码时,您通常会使用设备查询来 选择所需的 GPU。然而,一个快速简单的解决方案 测试是使用环境变量CUDA_VISIBLE_DEVICES来 限制您的 CUDA 应用程序看到的设备。这可以是 如果您尝试在节点上共享资源或您想要 您的 GPU 启用可执行文件以针对特定 GPU。

环境变量语法

结果

CUDA_VISIBLE_DEVICES=1 只会看到设备 1 CUDA_VISIBLE_DEVICES=0,1 设备 0 和 1 将可见 CUDA_VISIBLE_DEVICES=”0,1” 同上,引号可选 CUDA_VISIBLE_DEVICES=0,2,3 设备 0、2、3 将可见;设备 1 被屏蔽了

CUDA 将从零开始枚举可见设备。在最后 在这种情况下,设备 0、2、3 将显示为设备 0、1、2。如果您更改 字符串的顺序为“2,3,0”,将枚举设备2,3,0 分别为 0,1,2。如果 CUDA_VISIBLE_DEVICES 设置为一个设备 不存在,所有设备都将被屏蔽。您可以指定混合 有效和无效的设备号。无效值之前的所有设备 将被枚举,而无效值之后的所有设备将被 蒙面。

要确定系统中可用硬件的设备 ID, 您可以运行 CUDA SDK 中包含的 NVIDIA 的 deviceQuery 可执行文件。 编程愉快!

克里斯·梅森

【讨论】:

  • 我也是这么做的。但是当我执行nvidia-smi 时,我看到所有 gpu 设备使用了相同数量的内存。
  • 链接到 tensorflow gpu 的信息是旧的/损坏的;相反(截至 2017/10)尝试:tensorflow.org/tutorials/using_gpu
  • @Michael 谢谢。更新了答案。
  • @GuyCoder :我对运行 tensorflow-gpu 代码也有类似的疑问。它使用所有 gpu,但为什么不像 multiprocessing 模块那样使用所有内核?你能检查一下吗? stackoverflow.com/questions/54402154/…
猜你喜欢
  • 2019-04-03
  • 1970-01-01
  • 2018-09-10
  • 1970-01-01
  • 2011-05-31
  • 1970-01-01
  • 2011-04-17
  • 2015-07-01
  • 2022-12-31
相关资源
最近更新 更多