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

Handle None before gathering num items in batch across devices #35399

Open
wants to merge 2 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
3 changes: 3 additions & 0 deletions src/transformers/trainer.py
Original file line number Diff line number Diff line change
Expand Up @@ -2465,6 +2465,7 @@ def _inner_training_loop(
if remainder == 0:
remainder = args.gradient_accumulation_steps
update_step = -1
# Note: total_updates can be larger than dataloader length
total_updates = steps_in_epoch // args.gradient_accumulation_steps + 1
for _ in range(total_updates):
update_step += 1
Expand Down Expand Up @@ -5153,6 +5154,8 @@ def get_batch_samples(self, epoch_iterator, num_batches):
pass

if self.args.average_tokens_across_devices:
if num_items_in_batch is None:
num_items_in_batch = torch.tensor(0).to(self.args.device)
num_items_in_batch = self.accelerator.gather(num_items_in_batch).sum().item()

if torch.is_tensor(num_items_in_batch):
Expand Down