DeepSpeed
6282053f - Restore the data sampler's own RNG when resuming from a checkpoint (#8788)

Commit
2 days ago
Restore the data sampler's own RNG when resuming from a checkpoint (#8788) Fixes #8787 ## The bug `DeepSpeedDataSampler` makes all of its random choices with `self.np_rng`, a `np.random.default_rng(seed)` created in `__init__`. Its checkpoint state didn't include that generator, though. `state_dict()` saved `np.random.get_state()`, numpy's global RNG, and `load_state_dict()` restored that one. After a resume, `self.np_rng` started over from the seed. Cluster sampling, the per-batch shuffle and the cluster reshuffle all repeated the choices from the beginning of training, so the batches stopped matching an uninterrupted run. ## The fix `state_dict()` now also saves `self.np_rng.bit_generator.state` under a new key, `sampler_rng_state`, and `load_state_dict()` restores it. The existing `np_rng_state` field keeps saving and restoring numpy's global RNG exactly as before. Checkpoints written before this change don't have `sampler_rng_state`. For those, `load_state_dict()` restores the global RNG as it always did and logs a warning that sampling after the resume won't match an uninterrupted run, since there's nothing to restore the sampler's generator from. Old checkpoints still load without errors, whatever bit generator the global RNG was using. ## Testing I added three tests to `tests/unit/runtime/test_data_efficiency.py`. - `TestCurriculumCheckpointResume` is the end-to-end one. It trains a `SimpleModel` for 16 steps with curriculum learning on. Then it trains 8 steps, calls `save_checkpoint()`, calls `load_checkpoint()` on a fresh engine, and trains 8 more. Every step has to train on the same batch, and the losses have to match exactly. Since 16 steps of 2 from 20 samples crosses the epoch boundary after the resume, the cluster reshuffle is covered too. - `test_data_sampler_resumes_where_it_left_off` does the same thing on the sampler alone, in a single process with `barrier` and `broadcast` stubbed. It also reseeds numpy's global RNG between saving and loading, to show the result no longer depends on it. - `test_data_sampler_loads_checkpoints_from_before_the_rng_fix` loads a checkpoint without `sampler_rng_state`, once with MT19937 and once with Philox as the global generator. It checks that the next global draw is the one from save time and that the sampler's generator is left as freshly seeded. I ran these on an Apple M4 (macOS 26.6, torch 2.14.0, world size 1): | | with this PR | on master | | --- | --- | --- | | `TestCurriculumCheckpointResume`, `mps` | passes | fails: step 9 trained on a different batch | | `TestCurriculumCheckpointResume`, `cpu` | passes | fails: step 9 trained on a different batch | | `test_data_sampler_resumes_where_it_left_off` | passes | fails: 14 of 15 batches after the resume differ | The rest of the file passes, except the two multi-process `TestDataEfficiency` cases, which need 2 GPUs and skip on this machine. I don't have CUDA hardware, so nothing has run on a GPU yet. The new end-to-end test only needs one device in fp32, so CI should be able to run it as is. `pre-commit` is clean. cc @tjruwase (CODEOWNERS for `deepspeed/runtime/data_pipeline/`) --------- Signed-off-by: rakshit-gen <sisodiarakshit456@gmail.com>
Author
Parents
Loading