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
docstrings and minor fixes
  • Loading branch information
albertbou92 committed Aug 14, 2024
commit ad32b909d3338932245100a36907b64250887310
14 changes: 12 additions & 2 deletions test/test_cost.py
Original file line number Diff line number Diff line change
Expand Up @@ -8950,14 +8950,24 @@ def test_a2c_separate_losses(self, separate_losses, composite_action_dist):
separate_losses=separate_losses,
)

def set_requires_grad(tensor, requires_grad):
tensor.requires_grad = requires_grad
return tensor

# Check error is raised when actions require grads
td["action"].requires_grad = True
if composite_action_dist:
td["action"].apply_(lambda x: set_requires_grad(x, True))
else:
td["action"].requires_grad = True
with pytest.raises(
RuntimeError,
match="tensordict stored action requires grad.",
):
_ = loss_fn._log_probs(td)
td["action"].requires_grad = False
if composite_action_dist:
td["action"].apply_(lambda x: set_requires_grad(x, False))
else:
td["action"].requires_grad = False

td = td.exclude(loss_fn.tensor_keys.value_target)
loss = loss_fn(td)
Expand Down