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

Try to fix training Loss inconsistent after resume from old checkpoint #25872

Merged
merged 10 commits into from
Sep 7, 2023
29 changes: 27 additions & 2 deletions src/transformers/trainer.py
Original file line number Diff line number Diff line change
Expand Up @@ -1782,8 +1782,33 @@ def _inner_training_loop(
# Skip the first epochs_trained epochs to get the random state of the dataloader at the right point.
if not args.ignore_data_skip:
for epoch in range(epochs_trained):
for _ in train_dataloader:
break
is_random_sampler = (
(hasattr(train_dataloader, "sampler") and isinstance(train_dataloader.sampler, RandomSampler))
or (
hasattr(train_dataloader, "batch_sampler")
and isinstance(train_dataloader.batch_sampler.sampler, RandomSampler)
)
or (
hasattr(train_dataloader, "batch_sampler")
and hasattr(train_dataloader.batch_sampler, "batch_sampler")
and isinstance(train_dataloader.batch_sampler.batch_sampler.sampler, RandomSampler)
)
)
if not is_random_sampler:
dumpmemory marked this conversation as resolved.
Show resolved Hide resolved
# We just need to begin an iteration to create the randomization of the sampler.
for _ in train_dataloader:
break
else:
# Otherwise we need to call the whooooole sampler cause there is some random operation added
# AT THE VERY END!
sampler = []

if hasattr(train_dataloader, "sampler") and isinstance(train_dataloader.sampler, RandomSampler):
sampler = train_dataloader.sampler
else:
sampler = train_dataloader.batch_sampler
dumpmemory marked this conversation as resolved.
Show resolved Hide resolved

_ = list(sampler)
dumpmemory marked this conversation as resolved.
Show resolved Hide resolved

total_batched_samples = 0
for epoch in range(epochs_trained, num_train_epochs):
Expand Down