[Megatron] Preserve RNG state across checkpoint resume - #9935
Open
taking-lying-flat wants to merge 1 commit into
Open
[Megatron] Preserve RNG state across checkpoint resume#9935taking-lying-flat wants to merge 1 commit into
taking-lying-flat wants to merge 1 commit into
Conversation
taking-lying-flat
marked this pull request as ready for review
August 17, 2026 12:10
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.
Summary
save_safetensors=true,no_save_optim=true, andno_save_rng=falseRoot cause
After checkpoint loading restored Python, NumPy, Torch, CUDA, and Megatron CUDA-tracker state, GRPO and GKD resampling initialization called
set_random_seed(args.seed + 1)and thenset_random_seed(args.seed). This replaced the restored training RNG position with the initial seed.For safetensors-only checkpoints,
BaseMegatronTrainerintentionally passes an empty model list to the MCore checkpoint writer when optimizer state is omitted. RNG collection was conditional on that list being non-empty, so RNG was silently omitted even whenno_save_rng=false.Fix
Resampling now receives its private seed through the batch sampler and DataLoader generators without modifying global or model RNG state. The default sampler seed remains unchanged for existing training DataLoaders.
RNG collection now follows
no_save_rnginstead of the model list. A model-less checkpoint containing sharded RNG state uses the distributed checkpoint path so the existing load path can restore it.Impact
GRPO/GKD resume no longer resets restored RNG state while creating dynamic/deletion resampling iterators. Safetensors checkpoints without optimizer state retain RNG whenever the user has not disabled RNG saving.