Repository navigation
Conversation
When resuming training from a checkpoint, the Trainer attempts to load \`trainer_state.json\` to recover the train batch size. However, if the file does not exist, a \`FileNotFoundError\` is raised, causing resume to fail. This patch adds a check using \`os.path.isfile\` before loading the state, consistent with other parts of the codebase. If the file is missing, a warning is logged and batch size recovery is skipped, allowing training to continue. Fixes a potential crash and improves robustness when resuming from incomplete or custom checkpoint directories.
|
Thanks for picking this up. I tested this change with a crash-injection harness and want to share what it did, because the outcome differs from the crash it replaces. Setup: transformers v5.18.0 (
Across the 18 kill points I injected one by one in a single save (before and with the file cut in half, at every file event), the current code raises on 17. With this PR, 8 of those 17 turn into the resume-from-step-0 case and 9 still raise (a half-written model/optimizer file fails to load). So it trades a crash for a silent restart in the cases where the model file happens to load. An alternative that held up in the same test: make Multi-rank caveat (2 gloo ranks): the same change still leaves 3 of 21 kill points, all when a rank other than 0 dies mid-save. Rank 0 writes Limits: tiny model, CPU, Windows, a process kill (not power loss), one run per kill point. The harness and the exact commands are at https://github.com/rkb32/ckpt-chaos (MIT). I am happy to turn the |
|
Closing: as rkb32 measured, this turns the crash into a silent restart from step 0. Skipping incomplete checkpoints in |
What does this PR do?
When resuming training from a checkpoint, the Trainer attempts to load `trainer_state.json` to recover the train batch size. However, if the file does not exist, a `FileNotFoundError` is raised, causing resume to fail.
This patch adds a check using `os.path.isfile` before loading the state, consistent with other parts of the codebase. If the file is missing, a warning is logged and batch size recovery is skipped, allowing training to continue.
Fixes a potential crash and improves robustness when resuming from incomplete or custom checkpoint directories.
Related PR #27568
Before submitting
Pull Request section?
to it if that's the case.
documentation guidelines, and
here are tips on formatting docstrings.
Who can review?
trainer: @zach-huggingface, @SunMarc and @qgallouedec
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.