Skip to content

[BugFix] Fix DataLoader batching for ragged lazy stacks - #1772

Open
gtnv wants to merge 1 commit into
pytorch:mainfrom
gtnv:fix-1296-lazy-stack-dataloader
Open

[BugFix] Fix DataLoader batching for ragged lazy stacks#1772
gtnv wants to merge 1 commit into
pytorch:mainfrom
gtnv:fix-1296-lazy-stack-dataloader

Conversation

@gtnv

@gtnv gtnv commented Aug 25, 2026

Copy link
Copy Markdown
Contributor

TLDR

DataLoader batches map datasets through __getitems__. LazyStackedTensorDict inherited TensorDictBase.__getitems__, so batching bypassed its own indexing logic and tried to stack unequal tensors.

Point __getitems__ at the existing lazy-stack __getitem__, keeping ragged batches lazy.

Fixes #1296.

Reproduction
import torch
from torch.utils.data import DataLoader

from tensordict import LazyStackedTensorDict, TensorDict, lazy_stack

dataset = lazy_stack([TensorDict(x=torch.zeros(i)) for i in range(10)])
batches = list(
    DataLoader(dataset, batch_size=4, collate_fn=lambda batch: batch)
)

assert [len(batch) for batch in batches] == [4, 4, 2]
assert all(isinstance(batch, LazyStackedTensorDict) for batch in batches)
assert [len(td["x"]) for batch in batches for td in batch] == list(range(10))

On main, DataLoader raises while stacking the unequal tensors. With this change, all assertions pass.

@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 Aug 25, 2026
@github-actions github-actions Bot added Test bug Something isn't working labels Aug 25, 2026

@vmoens vmoens 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.

Please remove DataLoader references from the entire PR artifact.

It appears in the title, body/reproduction, branch name, commit subject, and [new test](

def test_dataloader_ragged_lazy_stack(self):
dataset = lazy_stack([TensorDict(x=torch.zeros(i)) for i in range(10)])
batches = list(
torch.utils.data.DataLoader( # noqa: TOR401
dataset, batch_size=4, collate_fn=lambda batch: batch
)
)
assert [len(batch) for batch in batches] == [4, 4, 2]
assert all(isinstance(batch, LazyStackedTensorDict) for batch in batches)
assert [len(td["x"]) for batch in batches for td in batch] == list(range(10))
). Frame this as batched indexing instead, and test __getitems__ directly. Suggested title: [BugFix] Preserve ragged lazy stacks during batched indexing.

The implementation itself is correct so I am approving it but there should not be cross-reference between rl and td in PR/issues unless it provides some context and helps discovery.

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. Test

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[BUG] DataLoader with LazyStackedTensorDict of different sizes

2 participants