Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[BugFix] Allow for composite action distributions in PPO/A2C losses #2391

Merged
merged 19 commits into from
Sep 4, 2024
Prev Previous commit
Next Next commit
format
  • Loading branch information
albertbou92 committed Aug 12, 2024
commit 2865c92554b0fbbcf1dbe655f9c419a6c0b19a1b
4 changes: 2 additions & 2 deletions test/test_cost.py
Original file line number Diff line number Diff line change
Expand Up @@ -8443,8 +8443,8 @@ def test_ppo_notensordict(
f"next_{observation_key}": td.get(("next", observation_key)),
}
if loss_class is KLPENPPOLoss:
loc_key = ("params", "action1", "loc") if composite_action_dist else "loc"
scale_key = ("params", "action1", "scale") if composite_action_dist else "scale"
loc_key = "params" if composite_action_dist else "loc"
scale_key = "params" if composite_action_dist else "scale"
kwargs.update({loc_key: td.get(loc_key), scale_key: td.get(scale_key)})

td = TensorDict(kwargs, td.batch_size, names=["time"]).unflatten_keys("_")
Expand Down
9 changes: 7 additions & 2 deletions torchrl/objectives/ppo.py
Original file line number Diff line number Diff line change
Expand Up @@ -477,9 +477,14 @@ def _log_weight(
raise RuntimeError("tensordict prev_log_prob requires grad.")

if isinstance(dist, CompositeDistribution):
vmoens marked this conversation as resolved.
Show resolved Hide resolved
if tensordict.get(self.tensor_keys.action).batch_size != tensordict.batch_size:
if (
tensordict.get(self.tensor_keys.action).batch_size
!= tensordict.batch_size
):
# This condition can be True in notensordict usage
tensordict.get(self.tensor_keys.action).batch_size = tensordict.batch_size
tensordict.get(
self.tensor_keys.action
).batch_size = tensordict.batch_size
tensordict = dist.log_prob(tensordict)
log_prob = tensordict.get(self.tensor_keys.sample_log_prob)
else:
Expand Down
Loading