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 4eb413d9bfb3f7ea715ce4cfee7073a53cf0b7aa
12 changes: 10 additions & 2 deletions test/test_cost.py
Original file line number Diff line number Diff line change
Expand Up @@ -8393,6 +8393,7 @@ def test_ppo_tensordict_keys_run(
@pytest.mark.parametrize("reward_key", ["reward", "reward2"])
@pytest.mark.parametrize("done_key", ["done", "done2"])
@pytest.mark.parametrize("terminated_key", ["terminated", "terminated2"])
@pytest.mark.parametrize("composite_action_dist", [True, False])
def test_ppo_notensordict(
self,
loss_class,
Expand All @@ -8402,6 +8403,7 @@ def test_ppo_notensordict(
reward_key,
done_key,
terminated_key,
composite_action_dist,
):
torch.manual_seed(self.seed)
td = self._create_mock_data_ppo(
Expand All @@ -8411,10 +8413,14 @@ def test_ppo_notensordict(
reward_key=reward_key,
done_key=done_key,
terminated_key=terminated_key,
composite_action_dist=composite_action_dist,
)

actor = self._create_mock_actor(
observation_key=observation_key, sample_log_prob_key=sample_log_prob_key
observation_key=observation_key,
sample_log_prob_key=sample_log_prob_key,
composite_action_dist=composite_action_dist,
action_key=action_key,
)
value = self._create_mock_value(observation_key=observation_key)

Expand All @@ -8437,7 +8443,9 @@ def test_ppo_notensordict(
f"next_{observation_key}": td.get(("next", observation_key)),
}
if loss_class is KLPENPPOLoss:
kwargs.update({"loc": td.get("loc"), "scale": td.get("scale")})
loc_key = ("params", "action1", "loc") if composite_action_dist else "loc"
scale_key = ("params", "action1", "scale") 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