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] Fixed shape for MultiStep returns + Distributional loss #2270

Closed
wants to merge 0 commits into from

Conversation

roger-creus
Copy link

Description

I encountered an error when combining MultiStep returns + Distributional DQN Loss. Because this line assumes discount is an integer but if using MultiStep returns it is a tensor of size (batch_size,).

This PR only changes that line to:

Tz = reward + (1 - terminated.to(reward.dtype)) * discount.unsqueeze(-1) * support.repeat(batch_size, 1)

which gives the same results if the discount is an integer but fixes the shape error if it is a batch.

Solves issue #2269

  • I have raised an issue to propose this change (required for new features and bug fixes)

Types of changes

What types of changes does your code introduce? Remove all that do not apply:

  • Bug fix (non-breaking change which fixes an issue)

Checklist

  • I have read the CONTRIBUTION guide (required)
  • My change requires a change to the documentation.
  • I have updated the tests accordingly (required for a bug fix or a new feature).
  • I have updated the documentation accordingly.

Copy link

pytorch-bot bot commented Jul 5, 2024

🔗 Helpful Links

🧪 See artifacts and rendered test results at hud.pytorch.org/pr/pytorch/rl/2270

Note: Links to docs will display an error until the docs builds have been completed.

✅ You can merge normally! (18 Unrelated Failures)

As of commit 25e8bd2 with merge base 25e8bd2 (image):

BROKEN TRUNK - The following jobs failed but were present on the merge base:

👉 Rebase onto the `viable/strict` branch to avoid these failures

This comment was automatically generated by Dr. CI and updates every 15 minutes.

@facebook-github-bot
Copy link

Hi @roger-creus!

Thank you for your pull request and welcome to our community.

Action Required

In order to merge any pull request (code, docs, etc.), we require contributors to sign our Contributor License Agreement, and we don't seem to have one on file for you.

Process

In order for us to review and merge your suggested changes, please sign at https://code.facebook.com/cla. If you are contributing on behalf of someone else (eg your employer), the individual CLA may not be sufficient and your employer may need to sign the corporate CLA.

Once the CLA is signed, our tooling will perform checks and validations. Afterwards, the pull request will be tagged with CLA signed. The tagging process may take up to 1 hour after signing. Please give it that time before contacting us about it.

If you have received this in error or have any questions, please contact us at cla@meta.com. Thanks!

@facebook-github-bot facebook-github-bot added the CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. label Jul 5, 2024
@facebook-github-bot
Copy link

Thank you for signing our Contributor License Agreement. We can now accept your code for this (and any) Meta Open Source project. Thanks!

@vmoens vmoens added the bug Something isn't working label Jul 8, 2024
@vmoens vmoens changed the title Fixed shape for MultiStep returns + Distributional loss [BugFix] Fixed shape for MultiStep returns + Distributional loss Jul 9, 2024
Copy link
Contributor

@vmoens vmoens left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for this contribution! I left a couple of comments, would you have time to address them?

@@ -560,7 +560,7 @@ def forward(self, input_tensordict: TensorDictBase) -> TensorDict:
support = support.to("cpu")
pns_a = pns_a.to("cpu")

Tz = reward + (1 - terminated.to(reward.dtype)) * discount * support
Tz = reward + (1 - terminated.to(reward.dtype)) * discount.unsqueeze(-1) * support.repeat(batch_size, 1)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good
I think we're making a couple of assumptions about discount and support here.
I would feel safer if we had a block before this where we check explicitly the shapes:

if not check(discount.shape):
    raise RuntimeError(f"Expected `discount` to have either shape X or Y, got {discount.shape}")
if not other_check(support.shape):
    raise RuntimeError(f"Expected `support` to have either shape X or Y, got {support.shape}")

(check and other_check are just placeholders)
If possible I would also make an instance of this loss class with the support and discount shape that you are using to test that it works ok (see test_cost.py:TestDQN:test_distributional_dqn)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should also make sure that discount is a tensor:

discount = torch.as_tensor(discount)

should do it (it's a no-op if it's already a tensor)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants