Correctly support resuming from checkpoint with a dataset without length #33544
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
What does this PR do?
There is an inconsistency in
Trainer
's behavior between training from scratch and resuming from checkpoint when the given dataset has no length likedatasets.IterableDataset
. For a reproducible example, see #26413 (comment) . This PR fixes the inconsistency by correctly supporting resuming from checkpoint with such a dataset.Fixes #26413
Current behavior
When training starts with a dataset without length, Trainer assumes one epoch is equal to
max_steps
steps and tries to train for that many steps. There are two possible scenarios.max_steps
steps, Trainer increments the current epoch and re-iterate the dataset.When resuming from a checkpoint, Trainer simply skips the first batches until
global_step
of the checkpoint. In scenario A, there is no problem. In scenario B, the dataset raises StopIteration during the skipping, but Trainer does not re-iterate the dataset. Instead, it just finishes training with a warning. This is inconsistent from what happens in training from scratch, and it contradicts with what the documents aboutmax_steps
says:transformers/src/transformers/training_args.py
Lines 301 to 304 in ac5a055
Solution
This PR modifies the skipping behavior so that Trainer now re-iterates the dataset until it catches up
global_step
. A caveat is that it does not support theignore_data_skip
option, as Trainer does not know what epoch to start from. I am also concerned that the logic is becoming too complicated.Before submitting
Pull Request section?
to it if that's the case.
documentation guidelines, and
here are tips on formatting docstrings.
Who can review?
Anyone in the community is free to review the PR once the tests have passed. Feel free to tag
members/contributors who may be interested in your PR.
@ArthurZucker