具有多个GPU的分布式tensorflow

Distributed tensorflow with multiple gpu

似乎 tf.train.replica_device_setter 不允许指定与之一起工作的 gpu。

我想做的事情如下:

 with tf.device(
   tf.train.replica_device_setter(
   worker_device='/job:worker:task:%d/gpu:%d' % (deviceindex, gpuindex)):
     <build-some-tf-graph>

如果您的参数没有被分片,您可以使用 replica_device_setter 的简化版本,如下所示:

def assign_to_device(worker=0, gpu=0, ps_device="/job:ps/task:0/cpu:0"):
    def _assign(op):
        node_def = op if isinstance(op, tf.NodeDef) else op.node_def
        if node_def.op == "Variable":
            return ps_device
        else:
            return "/job:worker/task:%d/gpu:%d" % (worker, gpu)
    return _assign

with tf.device(assign_to_device(1, 2)):
  # this op goes on worker 1 gpu 2
  my_op = tf.ones(())

以前的版本我没有查,但是在Tensorflow 1.4/1.5中,你可以在replica_device_setter(worker_device='job:worker/task:%d/gpu:%d' % (FLAGS.task_index, i), cluster=self.cluster)中指定设备。

参见 tensorflow/python/training/device_setter.py 第 199-202 行:

if ps_ops is None: # TODO(sherrym): Variables in the LOCAL_VARIABLES collection should not be # placed in the parameter server. ps_ops = ["Variable", "VariableV2", "VarHandleOp"]

感谢@Yaroslav Bulatov 提供的代码,但他的协议与replica_device_setter不同,在某些情况下可能会失败。