From 40bda9a918a20eb585caf640e69dd9077e6da3e1 Mon Sep 17 00:00:00 2001 From: Manuel Date: Sun, 6 Feb 2022 15:27:12 -0500 Subject: [PATCH] Remove explict forward calls (#753) * Remove explict forward calls * Changelog and commit checks. * Reverted test forward removal for super call. Co-authored-by: Anssi --- docs/misc/changelog.rst | 3 ++- stable_baselines3/common/on_policy_algorithm.py | 2 +- stable_baselines3/dqn/policies.py | 2 +- stable_baselines3/sac/policies.py | 2 +- stable_baselines3/sac/sac.py | 2 +- stable_baselines3/td3/policies.py | 2 +- 6 files changed, 7 insertions(+), 6 deletions(-) diff --git a/docs/misc/changelog.rst b/docs/misc/changelog.rst index db4078d..db6b519 100644 --- a/docs/misc/changelog.rst +++ b/docs/misc/changelog.rst @@ -26,6 +26,7 @@ Bug Fixes: - Fixed a bug in ``HumanOutputFormat``. Distinct keys truncated to the same prefix would overwrite each others value, resulting in only one being output. This now raises an error (this should only affect a small fraction of use cases with very long keys.) +- Routing all the nn.Module calls through implicit rather than explict forward as per pytorch guidelines (@manuel-delverme) Deprecations: ^^^^^^^^^^^^^ @@ -917,4 +918,4 @@ And all the contributors: @benblack769 @bstee615 @c-rizz @skandermoalla @MihaiAnca13 @davidblom603 @ayeright @cyprienc @wkirgsn @AechPro @CUN-bjy @batu @IljaAvadiev @timokau @kachayev @cleversonahum @eleurent @ac-93 @cove9988 @theDebugger811 @hsuehch @Demetrio92 @thomasgubler @IperGiove @ScheiklP -@simoninithomas @armandpl +@simoninithomas @armandpl @manuel-delverme diff --git a/stable_baselines3/common/on_policy_algorithm.py b/stable_baselines3/common/on_policy_algorithm.py index 062db93..48cb365 100644 --- a/stable_baselines3/common/on_policy_algorithm.py +++ b/stable_baselines3/common/on_policy_algorithm.py @@ -166,7 +166,7 @@ class OnPolicyAlgorithm(BaseAlgorithm): with th.no_grad(): # Convert to pytorch tensor or to TensorDict obs_tensor = obs_as_tensor(self._last_obs, self.device) - actions, values, log_probs = self.policy.forward(obs_tensor) + actions, values, log_probs = self.policy(obs_tensor) actions = actions.cpu().numpy() # Rescale and perform action diff --git a/stable_baselines3/dqn/policies.py b/stable_baselines3/dqn/policies.py index 6a8e6e1..099a4e3 100644 --- a/stable_baselines3/dqn/policies.py +++ b/stable_baselines3/dqn/policies.py @@ -66,7 +66,7 @@ class QNetwork(BasePolicy): return self.q_net(self.extract_features(obs)) def _predict(self, observation: th.Tensor, deterministic: bool = True) -> th.Tensor: - q_values = self.forward(observation) + q_values = self(observation) # Greedy action action = q_values.argmax(dim=1).reshape(-1) return action diff --git a/stable_baselines3/sac/policies.py b/stable_baselines3/sac/policies.py index 68133d1..0bd1382 100644 --- a/stable_baselines3/sac/policies.py +++ b/stable_baselines3/sac/policies.py @@ -182,7 +182,7 @@ class Actor(BasePolicy): return self.action_dist.log_prob_from_params(mean_actions, log_std, **kwargs) def _predict(self, observation: th.Tensor, deterministic: bool = False) -> th.Tensor: - return self.forward(observation, deterministic) + return self(observation, deterministic) class SACPolicy(BasePolicy): diff --git a/stable_baselines3/sac/sac.py b/stable_baselines3/sac/sac.py index f7059d7..5f3a833 100644 --- a/stable_baselines3/sac/sac.py +++ b/stable_baselines3/sac/sac.py @@ -253,7 +253,7 @@ class SAC(OffPolicyAlgorithm): # Compute actor loss # Alternative: actor_loss = th.mean(log_prob - qf1_pi) # Mean over all critic networks - q_values_pi = th.cat(self.critic.forward(replay_data.observations, actions_pi), dim=1) + q_values_pi = th.cat(self.critic(replay_data.observations, actions_pi), dim=1) min_qf_pi, _ = th.min(q_values_pi, dim=1, keepdim=True) actor_loss = (ent_coef * log_prob - min_qf_pi).mean() actor_losses.append(actor_loss.item()) diff --git a/stable_baselines3/td3/policies.py b/stable_baselines3/td3/policies.py index 44f80d0..264c760 100644 --- a/stable_baselines3/td3/policies.py +++ b/stable_baselines3/td3/policies.py @@ -80,7 +80,7 @@ class Actor(BasePolicy): def _predict(self, observation: th.Tensor, deterministic: bool = False) -> th.Tensor: # Note: the deterministic deterministic parameter is ignored in the case of TD3. # Predictions are always deterministic. - return self.forward(observation) + return self(observation) class TD3Policy(BasePolicy):