Skip to content

Yield no batches from the dispatcher when an iterable dataset has no complete batch - #4621

Open
sohailshk wants to merge 2 commits into
huggingface:mainfrom
sohailshk:fix/dispatcher-empty-iterable
Open

sohailshk wants to merge 2 commits into
huggingface:mainfrom
sohailshk:fix/dispatcher-empty-iterable

Conversation

@sohailshk

Copy link
Copy Markdown

What does this PR do?

Accelerator.prepare wraps a DataLoader over an IterableDataset in DataLoaderDispatcher. When the data holds no complete batch (it is empty, or has fewer samples than batch_size with drop_last=True), iterating the prepared loader raises, while the plain DataLoader yields nothing:

import torch
from torch.utils.data import DataLoader, IterableDataset
from accelerate import Accelerator

class Stream(IterableDataset):
    def __iter__(self):
        return iter(torch.arange(3).float().unsqueeze(1))

print(len(list(DataLoader(Stream(), batch_size=4, drop_last=True))))   # 0
dl = Accelerator(cpu=True).prepare(DataLoader(Stream(), batch_size=4, drop_last=True))
print(len(list(dl)))

On main the last line raises ValueError: Batch does not contain any data; expected 0. Same for an empty dataset with any drop_last. With two processes it is a TypeError from broadcast.

The first _fetch_batches call already reports the end with no data, but __iter__ still broadcasts and splits the None batch. This returns early instead, as DataLoaderShard.__iter__ already does for an empty loader. The ValueError came from #1531, which made this crash readable; a similar early return was part of #4374, closed by its author without review.

Tests: test_iterable_dataset_without_full_batch fails on main (2 failed) and passes with the fix; python -m pytest tests/test_data_loader.py: 29 passed, 13 skipped. torchrun --nproc_per_node 2 on CPU (gloo), empty/short/full iterable datasets x split_batches x drop_last: both ranks yield the expected batch count, no hang. make quality passes. Not run: GPU, rest of the suite.

The implementation and tests were written with AI assistance.

Use case

I don't have a use case

Who can review?

cc @SunMarc

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant