Skip to content

[BugFix] Accumulate offline RewardSum along the named time dimension - #4539

Merged
bsprenger merged 1 commit into
pytorch:mainfrom
aalquwayfili:fix-reward-sum-time-axis
Oct 15, 2026
Merged

bsprenger merged 1 commit into
pytorch:mainfrom
aalquwayfili:fix-reward-sum-time-axis

Conversation

@aalquwayfili

@aalquwayfili aalquwayfili commented Oct 8, 2026 •

Copy link
Copy Markdown
Contributor

Description

Use the actual index of the TensorDict batch dimension named time when computing offline RewardSum totals. Add six numeric regression cases covering unbatched, batch-first and time-first trajectories, flat and nested reward keys, and independent reward components.

Motivation and Context

Fixes #4538.

RewardSum.forward subtracts one from the named time dimension. For rewards shaped [time, 1], this accumulates over the reward feature; for [batch, time, 1], it accumulates across independent batch entries. For example, rewards [1, 2, 3] remain [1, 2, 3] instead of producing [1, 3, 6]. The existing one-dimensional reward test cannot distinguish axes 0 and -1.

TensorDict batch dimensions are the leading dimensions of their tensors; looking up a reward leaf does not remove an axis. Using that index directly restores the documented time accumulation. Standard environment step/reset accumulation is unchanged; direct batch and replay calls observe the corrected totals.

  • I have raised an issue to propose this change.

Types of changes

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

Validation

All six added regressions fail numerically before the fix. The full TestRewardSum class passes on CPU: 40 passed, 1 skipped. The skip is the existing unsupported inverse-transform test. The two changed files pass the pinned upstream ufmt and flake8 checks.

Additional local checks cover batch-axis permutations, nested agent dimensions, multiple reward keys, gradients, empty/singleton time axes, and two calls through torch.compile(..., backend="eager", fullgraph=True). This compile check covers eager graph capture, not Inductor or GPU execution.

A further public-API check used the same three-step CartPole rollout for online and offline accumulation, with one environment and two serial environments. The unchanged online accumulator and patched offline transform agree; the original offline transform does not. These rollouts contain no episode boundary.

Validation used Python 3.13, PyTorch 2.14.0 and TensorDict 0.14.3 with imports from this checkout. Full repository tests, GPU checks and the documentation build were not run.

Checklist

  • I have read the CONTRIBUTION guide.
  • My change requires a change to the documentation.
  • I have updated the tests accordingly.
  • I have updated the documentation accordingly.

@pytorch-bot

pytorch-bot Bot commented Oct 8, 2026 •

Copy link
Copy Markdown

🔗 Helpful Links

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

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

⏳ No Failures, 8 Pending

As of commit da61c81 with merge base 264a3c2 (image):
💚 Looks good so far! There are no failures yet. 💚

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

@meta-cla

meta-cla Bot commented Oct 8, 2026

Copy link
Copy Markdown

Hi @aalquwayfili!

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!

@meta-cla

meta-cla Bot commented Oct 8, 2026

Copy link
Copy Markdown

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

@meta-cla meta-cla 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 Oct 8, 2026
@meta-cla

meta-cla Bot commented Oct 8, 2026

Copy link
Copy Markdown

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

@github-actions github-actions Bot added the BugFix label Oct 8, 2026
@bsprenger
bsprenger force-pushed the fix-reward-sum-time-axis branch from 4fe8739 to da61c81 Compare October 15, 2026 22:14

@bsprenger bsprenger left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

LGTM thanks for contributing!

@bsprenger
bsprenger merged commit 603c269 into pytorch:main Oct 15, 2026
122 of 123 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

BugFix CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. Transforms

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Offline RewardSum accumulates along the wrong dimension

2 participants