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 1 commit
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
4 changes: 4 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 @@ -5152,6 +5153,9 @@ def get_batch_samples(self, epoch_iterator, num_batches):
except (TypeError, AttributeError):
pass

if num_items_in_batch is None:
num_items_in_batch = torch.tensor(0).to(self.args.device)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's do that only inside average_tokens_across_devices as num_items_in_batch should be an int. We need a tensor only for the gather

Copy link
Author

@chiragjn chiragjn Dec 23, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Then can I assume it is okay for this function to return None for num_items_in_batch?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah that's fine

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks! made the change

if self.args.average_tokens_across_devices:
num_items_in_batch = self.accelerator.gather(num_items_in_batch).sum().item()

Expand Down