Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions src/accelerate/data_loader.py
Original file line number Diff line number Diff line change
Expand Up @@ -871,6 +871,11 @@ def __iter__(self):
self._stop_iteration = False
first_batch = None
next_batch, next_batch_info = self._fetch_batches(main_iterator)
if self._stop_iteration and next_batch_info[0] is None:
# No batch to dispatch (e.g. empty data): end the iteration like a plain DataLoader would.
self.iteration += 1
self.end()
return
batch_index = 0
while not stop_iteration:
batch, batch_info = next_batch, next_batch_info
Expand Down
8 changes: 8 additions & 0 deletions tests/test_data_loader.py
Original file line number Diff line number Diff line change
Expand Up @@ -524,6 +524,14 @@ def test_iterable_dataset_using_none_batch_size(self):
for d in dataloader:
assert isinstance(d, torch.Tensor)

@parameterized.expand([(0, False), (3, True)])
def test_iterable_dataset_without_full_batch(self, num_samples, drop_last):
# Like a plain DataLoader, the dispatcher yields nothing when the data holds no complete batch.
accelerator = Accelerator()
dataloader = DataLoader(SimpleIterableDataset(num_samples), batch_size=4, drop_last=drop_last)
assert list(accelerator.prepare_data_loader(dataloader)) == []
assert not accelerator.gradient_state.in_dataloader

def test_iterable_dataset_with_non_tensor_samples(self):
dataset = SimpleIterableDataset(10)

Expand Down