我只有一个 gpu,我想在那个 gpu 上运行很多演员。以下是我使用的内容ray
,遵循https://ray.readthedocs.io/en/latest/actors.html
- 首先在gpu上定义网络
class Network():
def __init__(self, ***some args here***):
self._graph = tf.Graph()
os.environ['CUDA_VISIBLE_DIVICES'] = ','.join([str(i) for i in ray.get_gpu_ids()])
with self._graph.as_default():
with tf.device('/gpu:0'):
# network, loss, and optimizer are defined here
sess_config = tf.ConfigProto(allow_soft_placement=True)
sess_config.gpu_options.allow_growth=True
self.sess = tf.Session(graph=self._graph, config=sess_config)
self.sess.run(tf.global_variables_initializer())
atexit.register(self.sess.close)
self.variables = ray.experimental.TensorFlowVariables(self.loss, self.sess)
- 然后定义工人阶级
@ray.remote(num_gpus=1)
class Worker(Network):
# do something
- 定义学习者类
@ray.remote(num_gpus=1)
class Learner(Network):
# do something
- 训练功能
def train():
ray.init(num_gpus=1)
leaner = Learner.remote(...)
workers = [Worker.remote(...) for i in range(10)]
# do something
当我不尝试使其在 gpu 上运行时,此过程运行良好。也就是说,当我删除所有with tf.device('/gpu:0')
和(num_gpus=1)
. 当我保留它们时,问题就出现了:似乎只有learner
被创建,但没有一个workers
被构造。我应该怎么做才能让它工作?