-
Notifications
You must be signed in to change notification settings - Fork 5.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[RLlib] RLModule: Add
TargetNetworkAPI
and implement for APPO and S…
…AC. (#46656)
- Loading branch information
Showing
18 changed files
with
316 additions
and
384 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,29 +1,32 @@ | ||
""" | ||
This file holds framework-agnostic components for APPO's RLModules. | ||
""" | ||
|
||
import abc | ||
from typing import Any, Dict, List, Tuple | ||
|
||
from ray.rllib.algorithms.ppo.ppo_rl_module import PPORLModule | ||
from ray.rllib.core.rl_module.rl_module_with_target_networks_interface import ( | ||
RLModuleWithTargetNetworksInterface, | ||
) | ||
from ray.rllib.utils.annotations import ExperimentalAPI | ||
from ray.rllib.algorithms.appo.appo import OLD_ACTION_DIST_LOGITS_KEY | ||
from ray.rllib.core.learner.utils import make_target_network | ||
from ray.rllib.core.models.base import ACTOR | ||
from ray.rllib.core.models.tf.encoder import ENCODER_OUT | ||
from ray.rllib.core.rl_module.apis.target_network_api import TargetNetworkAPI | ||
from ray.rllib.utils.typing import NetworkType | ||
|
||
from ray.rllib.utils.annotations import override | ||
|
||
# TODO (simon): Write a light-weight version of this class for the `TFRLModule` | ||
|
||
class APPORLModule(PPORLModule, TargetNetworkAPI, abc.ABC): | ||
@override(TargetNetworkAPI) | ||
def make_target_networks(self): | ||
self._old_encoder = make_target_network(self.encoder) | ||
self._old_pi = make_target_network(self.pi) | ||
|
||
@ExperimentalAPI | ||
class APPORLModule(PPORLModule, RLModuleWithTargetNetworksInterface, abc.ABC): | ||
def setup(self): | ||
super().setup() | ||
@override(TargetNetworkAPI) | ||
def get_target_network_pairs(self) -> List[Tuple[NetworkType, NetworkType]]: | ||
return [ | ||
(self.encoder, self._old_encoder), | ||
(self.pi, self._old_pi), | ||
] | ||
|
||
# If the module is not for inference only, create the target networks. | ||
if not self.config.inference_only: | ||
catalog = self.config.get_catalog() | ||
# Old pi and old encoder are the "target networks" that are used for | ||
# the stabilization of the updates of the current pi and encoder. | ||
self.old_pi = catalog.build_pi_head(framework=self.framework) | ||
self.old_encoder = catalog.build_actor_critic_encoder( | ||
framework=self.framework | ||
) | ||
@override(TargetNetworkAPI) | ||
def forward_target(self, batch: Dict[str, Any]) -> Dict[str, Any]: | ||
old_pi_inputs_encoded = self._old_encoder(batch)[ENCODER_OUT][ACTOR] | ||
old_action_dist_logits = self._old_pi(old_pi_inputs_encoded) | ||
return {OLD_ACTION_DIST_LOGITS_KEY: old_action_dist_logits} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,57 +1,9 @@ | ||
from typing import Dict, List | ||
|
||
from ray.rllib.algorithms.appo.appo import OLD_ACTION_DIST_LOGITS_KEY | ||
from ray.rllib.algorithms.appo.appo_rl_module import APPORLModule | ||
from ray.rllib.algorithms.ppo.tf.ppo_tf_rl_module import PPOTfRLModule | ||
from ray.rllib.core.columns import Columns | ||
from ray.rllib.core.models.base import ACTOR | ||
from ray.rllib.core.models.tf.encoder import ENCODER_OUT | ||
from ray.rllib.core.rl_module.rl_module_with_target_networks_interface import ( | ||
RLModuleWithTargetNetworksInterface, | ||
) | ||
from ray.rllib.utils.annotations import override | ||
from ray.rllib.utils.framework import try_import_tf | ||
|
||
_, tf, _ = try_import_tf() | ||
|
||
|
||
class APPOTfRLModule(PPOTfRLModule, APPORLModule): | ||
@override(PPOTfRLModule) | ||
def setup(self): | ||
super().setup() | ||
|
||
# If the module is not for inference only, set up the target networks. | ||
if not self.config.inference_only: | ||
self.old_pi.set_weights(self.pi.get_weights()) | ||
self.old_encoder.set_weights(self.encoder.get_weights()) | ||
self.old_pi.trainable = False | ||
self.old_encoder.trainable = False | ||
|
||
@override(RLModuleWithTargetNetworksInterface) | ||
def sync_target_networks(self, tau: float) -> None: | ||
for target_network, current_network in [ | ||
(self.old_pi, self.pi), | ||
(self.old_encoder, self.encoder), | ||
]: | ||
for old_var, current_var in zip( | ||
target_network.variables, current_network.variables | ||
): | ||
updated_var = tau * current_var + (1.0 - tau) * old_var | ||
old_var.assign(updated_var) | ||
|
||
@override(PPOTfRLModule) | ||
def output_specs_train(self) -> List[str]: | ||
return [ | ||
Columns.ACTION_DIST_INPUTS, | ||
Columns.VF_PREDS, | ||
OLD_ACTION_DIST_LOGITS_KEY, | ||
] | ||
|
||
@override(PPOTfRLModule) | ||
def _forward_train(self, batch: Dict): | ||
outs = super()._forward_train(batch) | ||
batch = batch.copy() | ||
old_pi_inputs_encoded = self.old_encoder(batch)[ENCODER_OUT][ACTOR] | ||
old_action_dist_logits = tf.stop_gradient(self.old_pi(old_pi_inputs_encoded)) | ||
outs[OLD_ACTION_DIST_LOGITS_KEY] = old_action_dist_logits | ||
return outs | ||
pass |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.