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
fix tests ppo
  • Loading branch information
albertbou92 committed Aug 12, 2024
commit e65e1a3f20b24e38dc7e79de3dce2cad03127ab7
16 changes: 9 additions & 7 deletions test/test_cost.py
Original file line number Diff line number Diff line change
Expand Up @@ -7750,14 +7750,15 @@ def _create_mock_data_ppo(
},
action_key: {"action1": action} if composite_action_dist else action,
sample_log_prob_key: torch.randn_like(action[..., 1]) / 10,
loc_key: loc,
scale_key: scale,
},
device=device,
)
if composite_action_dist:
td[("params", "action1", loc_key)] = loc
td[("params", "action1", scale_key)] = scale
else:
td[loc_key] = loc
td[scale_key] = scale
return td

def _create_seq_mock_data_ppo(
Expand Down Expand Up @@ -7806,15 +7807,16 @@ def _create_seq_mock_data_ppo(
sample_log_prob_key: (
torch.randn_like(action[..., 1]) / 10
).masked_fill_(~mask, 0.0),
"loc": loc,
"scale": scale,
},
device=device,
names=[None, "time"],
)
if composite_action_dist:
td[("params", "action1", "loc")] = loc
td[("params", "action1", "scale")] = scale
else:
td["loc"] = loc
td["scale"] = scale

return td

Expand Down Expand Up @@ -7882,10 +7884,10 @@ def test_ppo(

loss = loss_fn(td)
if isinstance(loss_fn, KLPENPPOLoss):
if "kl" in loss:
kl = loss.pop("kl")
else:
if composite_action_dist:
kl = loss.pop("kl_approx")
else:
kl = loss.pop("kl")
assert (kl != 0).any()

loss_critic = loss["loss_critic"]
Expand Down