mirror of
https://github.com/wassname/ray.git
synced 2026-07-13 12:19:30 +08:00
[rllib] Cleanup RNN support and make it work with multi-GPU optimizer (#2394)
Cleanup: TFPolicyGraph now automatically adds loss input entries for state_in_*, so that graph sub-classes don't need to worry about it. Multi-GPU support: Allow setting up model tower replicas with existing state input tensors Truncate the per-device minibatch slices so that they are always a multiple of max_seq_len.
This commit is contained in:
@@ -49,7 +49,6 @@ class A3CPolicyGraph(TFPolicyGraph):
|
||||
[-1])
|
||||
self.var_list = tf.get_collection(tf.GraphKeys.TRAINABLE_VARIABLES,
|
||||
tf.get_variable_scope().name)
|
||||
is_training = tf.placeholder_with_default(True, ())
|
||||
|
||||
# Setup the policy loss
|
||||
if isinstance(action_space, gym.spaces.Box):
|
||||
@@ -74,16 +73,13 @@ class A3CPolicyGraph(TFPolicyGraph):
|
||||
("advantages", advantages),
|
||||
("value_targets", v_target),
|
||||
]
|
||||
for i, ph in enumerate(self.model.state_in):
|
||||
loss_in.append(("state_in_{}".format(i), ph))
|
||||
self.state_in = self.model.state_in
|
||||
self.state_out = self.model.state_out
|
||||
TFPolicyGraph.__init__(
|
||||
self, observation_space, action_space, self.sess,
|
||||
obs_input=self.observations, action_sampler=action_dist.sample(),
|
||||
loss=self.loss.total_loss, loss_inputs=loss_in,
|
||||
is_training=is_training, state_inputs=self.state_in,
|
||||
state_outputs=self.state_out,
|
||||
state_inputs=self.state_in, state_outputs=self.state_out,
|
||||
seq_lens=self.model.seq_lens,
|
||||
max_seq_len=self.config["model"]["max_seq_len"])
|
||||
|
||||
|
||||
@@ -46,6 +46,8 @@ COMMON_CONFIG = {
|
||||
"gpu_options": {
|
||||
"allow_growth": True,
|
||||
},
|
||||
"log_device_placement": False,
|
||||
"device_count": {"CPU": 1},
|
||||
"allow_soft_placement": True, # required by PPO multi-gpu
|
||||
},
|
||||
# Whether to LZ4 compress observations
|
||||
|
||||
@@ -262,12 +262,11 @@ class DDPGPolicyGraph(TFPolicyGraph):
|
||||
("dones", self.done_mask),
|
||||
("weights", self.importance_weights),
|
||||
]
|
||||
self.is_training = tf.placeholder_with_default(True, ())
|
||||
TFPolicyGraph.__init__(
|
||||
self, observation_space, action_space, self.sess,
|
||||
obs_input=self.cur_observations,
|
||||
action_sampler=self.output_actions, loss=self.loss.total_loss,
|
||||
loss_inputs=self.loss_inputs, is_training=self.is_training)
|
||||
loss_inputs=self.loss_inputs)
|
||||
self.sess.run(tf.global_variables_initializer())
|
||||
|
||||
# Note that this encompasses both the policy and Q-value networks and
|
||||
|
||||
@@ -171,12 +171,11 @@ class DQNPolicyGraph(TFPolicyGraph):
|
||||
("dones", self.done_mask),
|
||||
("weights", self.importance_weights),
|
||||
]
|
||||
self.is_training = tf.placeholder_with_default(True, ())
|
||||
TFPolicyGraph.__init__(
|
||||
self, observation_space, action_space, self.sess,
|
||||
obs_input=self.cur_observations,
|
||||
action_sampler=self.output_actions, loss=self.loss.loss,
|
||||
loss_inputs=self.loss_inputs, is_training=self.is_training)
|
||||
loss_inputs=self.loss_inputs)
|
||||
self.sess.run(tf.global_variables_initializer())
|
||||
|
||||
def optimizer(self):
|
||||
|
||||
@@ -41,16 +41,10 @@ class PGPolicyGraph(TFPolicyGraph):
|
||||
("advantages", advantages),
|
||||
]
|
||||
|
||||
# LSTM support
|
||||
for i, ph in enumerate(self.model.state_in):
|
||||
loss_in.append(("state_in_{}".format(i), ph))
|
||||
|
||||
is_training = tf.placeholder_with_default(True, ())
|
||||
TFPolicyGraph.__init__(
|
||||
self, obs_space, action_space, sess, obs_input=obs,
|
||||
action_sampler=action_dist.sample(), loss=loss,
|
||||
loss_inputs=loss_in, is_training=is_training,
|
||||
state_inputs=self.model.state_in,
|
||||
loss_inputs=loss_in, state_inputs=self.model.state_in,
|
||||
state_outputs=self.model.state_out,
|
||||
seq_lens=self.model.seq_lens,
|
||||
max_seq_len=config["model"]["max_seq_len"])
|
||||
|
||||
@@ -50,7 +50,7 @@ DEFAULT_CONFIG = with_common_config({
|
||||
"simple_optimizer": False,
|
||||
# Override model config
|
||||
"model": {
|
||||
# Use LSTM model (note: requires simple optimizer for now).
|
||||
# Whether to use LSTM model
|
||||
"use_lstm": False,
|
||||
# Max seq length for LSTM training.
|
||||
"max_seq_len": 20,
|
||||
|
||||
@@ -92,9 +92,10 @@ class PPOPolicyGraph(TFPolicyGraph):
|
||||
dist_cls, logit_dim = ModelCatalog.get_action_dist(action_space)
|
||||
|
||||
if existing_inputs:
|
||||
self.loss_in = existing_inputs
|
||||
obs_ph, value_targets_ph, adv_ph, act_ph, \
|
||||
logits_ph, vf_preds_ph = [ph for _, ph in existing_inputs]
|
||||
logits_ph, vf_preds_ph = existing_inputs[:6]
|
||||
existing_state_in = existing_inputs[6:-1]
|
||||
existing_seq_lens = existing_inputs[-1]
|
||||
else:
|
||||
obs_ph = tf.placeholder(
|
||||
tf.float32, name="obs", shape=(None,)+observation_space.shape)
|
||||
@@ -107,23 +108,20 @@ class PPOPolicyGraph(TFPolicyGraph):
|
||||
tf.float32, name="vf_preds", shape=(None,))
|
||||
value_targets_ph = tf.placeholder(
|
||||
tf.float32, name="value_targets", shape=(None,))
|
||||
existing_state_in = None
|
||||
existing_seq_lens = None
|
||||
|
||||
self.loss_in = [
|
||||
("obs", obs_ph),
|
||||
("value_targets", value_targets_ph),
|
||||
("advantages", adv_ph),
|
||||
("actions", act_ph),
|
||||
("logits", logits_ph),
|
||||
("vf_preds", vf_preds_ph),
|
||||
]
|
||||
|
||||
self.loss_in = [
|
||||
("obs", obs_ph),
|
||||
("value_targets", value_targets_ph),
|
||||
("advantages", adv_ph),
|
||||
("actions", act_ph),
|
||||
("logits", logits_ph),
|
||||
("vf_preds", vf_preds_ph),
|
||||
]
|
||||
self.model = ModelCatalog.get_model(
|
||||
obs_ph, logit_dim, self.config["model"])
|
||||
|
||||
# LSTM support
|
||||
if not existing_inputs:
|
||||
for i, ph in enumerate(self.model.state_in):
|
||||
self.loss_in.append(("state_in_{}".format(i), ph))
|
||||
obs_ph, logit_dim, self.config["model"],
|
||||
state_in=existing_state_in, seq_lens=existing_seq_lens)
|
||||
|
||||
# KL Coefficient
|
||||
self.kl_coeff = tf.get_variable(
|
||||
@@ -155,15 +153,14 @@ class PPOPolicyGraph(TFPolicyGraph):
|
||||
clip_param=self.config["clip_param"],
|
||||
vf_loss_coeff=self.config["kl_target"],
|
||||
use_gae=self.config["use_gae"])
|
||||
self.is_training = tf.placeholder_with_default(True, ())
|
||||
|
||||
TFPolicyGraph.__init__(
|
||||
self, observation_space, action_space,
|
||||
self.sess, obs_input=obs_ph,
|
||||
action_sampler=self.sampler, loss=self.loss_obj.loss,
|
||||
loss_inputs=self.loss_in, is_training=self.is_training,
|
||||
state_inputs=self.model.state_in,
|
||||
state_outputs=self.model.state_out, seq_lens=self.model.seq_lens)
|
||||
loss_inputs=self.loss_in, state_inputs=self.model.state_in,
|
||||
state_outputs=self.model.state_out, seq_lens=self.model.seq_lens,
|
||||
max_seq_len=config["model"]["max_seq_len"])
|
||||
|
||||
self.sess.run(tf.global_variables_initializer())
|
||||
|
||||
|
||||
Reference in New Issue
Block a user