fix: make Megatron one-shot train() assumptions idempotent across slime rollouts#1857
Open
leofan-lab wants to merge 2 commits intoTHUDM:mainfrom
Open
fix: make Megatron one-shot train() assumptions idempotent across slime rollouts#1857leofan-lab wants to merge 2 commits intoTHUDM:mainfrom
leofan-lab wants to merge 2 commits intoTHUDM:mainfrom
Conversation
Slime calls train() once per rollout, but Megatron's upstream code asserts `config.no_sync_func is None` on entry — written for a one-shot trainer. After rollout 1 we've installed `model.no_sync` ourselves, so rollout 2 trips the assert. Replace the assert with `if ... is None: install`. Same first-time behavior, idempotent on re-entry. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
Second symptom of the same root cause as the previous commit: Megatron's upstream code assumes a one-shot train() call, but slime invokes train() per rollout. At the end of rollout N, disable_forward_pre_hook() removes the forward pre-hooks from each DDP chunk and clears the chunk's `remove_forward_pre_hook_handles` list. On rollout N+1, slime calls disable_forward_pre_hook again; with no hooks left to disable, Megatron's internal Float16Module lookup fails with KeyError. Guard the call: only disable if any chunk still has an active hook. Encountered when I enabled --overlap-param-gather alongside --overlap-grad-reduce. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
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.
Megatron's upstream
train()is written for one-shot invocation. Slime invokestrain()once per rollout, which trips two separate per-rollout idempotency bugs inslime/backends/megatron_utils/model.py. This PR fixes both.Fix 1:
no_sync_funcinstall (commit 14abe6a)Megatron asserts
config.no_sync_func is Noneon entry. Slime installsmodel.no_syncat the end of rollout 1, so rollout 2 trips the assert.Replace the assert with
if ... is None: install. Same first-time behavior, idempotent on re-entry. Encountered when I enabled--overlap-grad-reduce.Fix 2:
disable_forward_pre_hookre-entry (commit a9243e4)At the end of rollout N,
disable_forward_pre_hook()removes forward pre-hooks from each DDP chunk and clears that chunk'sremove_forward_pre_hook_handleslist. On rollout N+1, slime callsdisable_forward_pre_hookagain; with no hooks left to disable, Megatron's internal Float16Module lookup fails withKeyError.Guard the call: only disable if any chunk still has an active hook. Encountered when I enabled
--overlap-param-gatheralongside--overlap-grad-reduce.Scope
Both fixes are only triggered when overlap flags are on (
--overlap-grad-reduce,--overlap-param-gather). Without them, the relevant code paths don't execute and the existing assert is unreachable. So these changes are no-ops for users who don't enable overlap, and make overlap usable for slime's multi-rollout model.