Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix immediate StopIteration in DataLoaderShard #3368

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
Open
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
32 changes: 18 additions & 14 deletions src/accelerate/data_loader.py
Original file line number Diff line number Diff line change
Expand Up @@ -558,30 +558,34 @@ def __iter__(self):

self.set_epoch(self.iteration)
dataloader_iter = self.base_dataloader.__iter__()

# We iterate one batch ahead to check when we are at the end
try:
current_batch = next(dataloader_iter)
next_batch = next(dataloader_iter)
except StopIteration:
yield
self.end_of_dataloader = True
# Need to call update to set "_iterator_finished"
self._update_state_dict()

batch_index = 0
while True:
while not self.end_of_dataloader:
current_batch = next_batch
# But we still move it to the device so it is done before `StopIteration` is reached
if self.device is not None:
current_batch = send_to_device(current_batch, self.device, non_blocking=self._non_blocking)

# We need to update the state dict before iterating again
self._update_state_dict()
try:
# But we still move it to the device so it is done before `StopIteration` is reached
if self.device is not None:
current_batch = send_to_device(current_batch, self.device, non_blocking=self._non_blocking)
self._update_state_dict()
next_batch = next(dataloader_iter)
if batch_index >= self.skip_batches:
yield current_batch
batch_index += 1
current_batch = next_batch
except StopIteration:
self.end_of_dataloader = True
# Need to call update again to set "_iterator_finished"
self._update_state_dict()
if batch_index >= self.skip_batches:
yield current_batch
break

if batch_index >= self.skip_batches:
yield current_batch
batch_index += 1

self.iteration += 1
self.end()
Expand Down