Skip to content

[BugFix] Let TensorDictMaxValueWriter extend with batches of trajectories - #4713

Open
rishi02102017 wants to merge 1 commit into
pytorch:mainfrom
rishi02102017:fix/max-value-writer-trajectories
Open

rishi02102017 wants to merge 1 commit into
pytorch:mainfrom
rishi02102017:fix/max-value-writer-trajectories

Conversation

@rishi02102017

Copy link
Copy Markdown

Description

TensorDictMaxValueWriter.extend ranks each element along the first dimension of the data, so a [batch, time] rollout is stored one trajectory per slot, as described in the class docstring. The index it returns was allocated with the full data shape, so writing the per-trajectory storage indices into it raised a shape mismatch whenever more than one trajectory was kept:

RuntimeError: shape mismatch: value tensor of shape [2] cannot be broadcast to indexing result of shape [2, 5]

This PR allocates the returned index along the first dimension only, in both the insertion and the no-insertion branches, which matches what RoundRobinWriter returns for a one-dimensional storage. One-dimensional data behaves as before.

A regression test in test/rb/test_writers.py extends a buffer of capacity 2 with four 5-step trajectories and checks the returned index, that the two trajectories with the highest summed key are kept, and that sampling returns whole trajectories.

Motivation and Context

The trajectory example in the TensorDictMaxValueWriter docstring (rb.extend(rollout) followed by sample.shape == (5, 10)) currently fails on main and on torchrl 0.14.1.

close #4712

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

Types of changes

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

Checklist

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

…ries

TensorDictMaxValueWriter ranks each element along the first dimension of
the data passed to extend(), so a [batch, time] rollout is stored one
trajectory per slot, as described in the class docstring. The returned
index was allocated with the full data shape, though, so writing the
per-trajectory storage indices into it failed with a shape mismatch as
soon as more than one trajectory was kept.

Allocate the index along the first dimension only, which matches what
RoundRobinWriter returns for a one-dimensional storage.
@pytorch-bot

pytorch-bot Bot commented Nov 9, 2026 •

Copy link
Copy Markdown

🔗 Helpful Links

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

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

⚠️ 16 Awaiting Approval

As of commit c6cb96f with merge base a711ef9 (image):

AWAITING APPROVAL - The following workflows need approval before CI can run:

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

@meta-cla

meta-cla Bot commented Nov 9, 2026

Copy link
Copy Markdown

Hi @rishi02102017!

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 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 Nov 9, 2026
@meta-cla

meta-cla Bot commented Nov 9, 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!

This branch has not been deployed

No deployments
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. ReplayBuffers

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[BUG] TensorDictMaxValueWriter.extend fails with [batch, time] trajectories

1 participant