[Checkpointing] Persist DataLoaderShard epoch counter across save/load_state#4012
Open
s-zx wants to merge 1 commit intohuggingface:mainfrom
Open
[Checkpointing] Persist DataLoaderShard epoch counter across save/load_state#4012s-zx wants to merge 1 commit intohuggingface:mainfrom
s-zx wants to merge 1 commit intohuggingface:mainfrom
Conversation
…d_state Fixes huggingface#3996. accelerator.save_state / load_state previously round-tripped samplers and (optionally) stateful-dataloader state_dicts, but did not preserve the per-epoch `iteration` counter on DataLoaderShard / DataLoaderDispatcher. That counter is what seeds SeedableRandomSampler for the next epoch (via set_epoch() -> sampler.set_epoch(epoch) -> deterministic shuffle seed), so a resumed run replayed the epoch-0 shuffle order instead of continuing with the correct epoch's permutation. Extend save_accelerator_state / load_accelerator_state to write a small `dl_iteration.bin` sibling file per dataloader when the dataloader exposes an `iteration` attribute, and restore it on load via set_epoch so samplers and dataset hooks that listen for epoch changes stay in sync. The load path checks file existence, keeping backward compatibility with checkpoints produced by older versions of accelerate. Also adds unit tests covering the round-trip, missing-file compatibility, and multi-dataloader filename handling.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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?
Fixes #3996.
accelerator.save_state/accelerator.load_statepreviously round-trippedsamplers and (when
use_stateful_dataloader=True) the dataloaderstate_dict,but did not persist the per-epoch
iterationcounter onDataLoaderShard/DataLoaderDispatcher.That counter is what seeds
SeedableRandomSampleron the next epoch (viaset_epoch()→sampler.set_epoch(epoch)→ deterministic shuffle seed), so aresumed run replayed the epoch-0 shuffle order instead of the correct epoch's
permutation. This reproduces exactly the "shuffle sequence replays from
iteration 0" behavior reported in the issue with
use_seedable_sampler=True.Fix
Extend
save_accelerator_state/load_accelerator_stateto write a smalldl_iteration.binsibling file per dataloader when the dataloader exposes aniterationattribute, and restore it on load viaset_epochso samplers andIterableDataset.set_epochhooks stay in sync.hasattr(dataloader, "iteration")— so it alsocovers the non-stateful path (
use_stateful_dataloader=False).input_iteration_file.exists(), keeping backwardcompatibility with checkpoints produced by older accelerate versions.
set_epoch(iteration)rather than direct assignment sodownstream samplers / datasets receive the epoch change.
Tests
Added unit tests to
tests/test_state_checkpointing.py:test_dataloader_iteration_counter_is_persisted— round-tripsiterationthrough save/load.test_load_state_tolerates_missing_iteration_file— backward compatibility.Before submitting
Pull Request section?
to it if that's the case.
documentation guidelines, and
here are tips on formatting docstrings.
Who can review?
@muellerzr @BenjaminBossan