TensorFlow 的自动设备放置是否处理多 GPU 情况?

Does TensorFlow's automatic device placement handles the multi-GPU case?

我知道使用 TensorFlow 可以手动控制我们声明变量的位置(例如:神经网络的一半在 GPU 上,其余部分在另一个 GPU 上)。

尽管可以肯定的是我们可以手动将变量放置在多个GPU上,但是它们可以自动放置在那些多个GPU上吗,例如使用自动变量放置? TensorFlow 的文档似乎从未明确提及自动设备放置是否处理多 GPU 情况。

例如,假设我使用带有 TensorFlow 后端的 Keras 构建了一个模型。如果模型因为太大而不能全部放入第一个 GPU,TensorFlow 会自动使用第二个 GPU(可能还有更多 GPU)来声明神经网络的变量吗?

谢谢。

不,参考 official doc

如果您的系统中有多个 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))

No, you are responsible for device placement:

If you have more than one GPU in your system, the GPU with the lowest ID will be selected by default.

我认为这样做是因为将数据从 GPU 转移到另一个 GPU 的成本可能很高,而且系统并不真正知道在哪种情况下移动数据是合理的。