多gpu分布式tensorflow

多gpu分布式tensorflow,tensorflow,distributed,Tensorflow,Distributed,似乎tf.train.replica\u device\u setter不允许指定使用的gpu 我想做的事情如下: with tf.device( tf.train.replica_device_setter( worker_device='/job:worker:task:%d/gpu:%d' % (deviceindex, gpuindex)): <build-some-tf-graph> def assign_to_device(worker=0, g

似乎
tf.train.replica\u device\u setter
不允许指定使用的gpu

我想做的事情如下:

 with tf.device(
   tf.train.replica_device_setter(
   worker_device='/job:worker:task:%d/gpu:%d' % (deviceindex, gpuindex)):
     <build-some-tf-graph>
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(())
使用tf.device(
tf.train.replica\u设备设置器(
worker\u device='/job:worker:task:%d/gpu:%d%%(deviceindex,gpuindex)):

如果您的参数没有切分,您可以使用简化版的
复制设备设置器进行切分,如下所示:

 with tf.device(
   tf.train.replica_device_setter(
   worker_device='/job:worker:task:%d/gpu:%d' % (deviceindex, gpuindex)):
     <build-some-tf-graph>
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\u device\u setter(worker\u device='job:worker/task:%d/gpu:%d'%中指定设备
(FLAGS.task_index,i),cluster=self.cluster)

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

如果ps_ops为无:
#TODO(sherrym):局部变量集合中的变量不应为
#放置在参数服务器中。
ps_ops=[“Variable”、“VariableV2”、“VarHandleOp”]

感谢@Yaroslav Bulatov提供的代码,但他的协议不同于
复制设备设置器
,在某些情况下可能会失败