Fix cross-iteration gradient retention in CheckpointFunction.backward - #6339
Open
htesd wants to merge 1 commit into
Open
Fix cross-iteration gradient retention in CheckpointFunction.backward#6339htesd wants to merge 1 commit into
htesd wants to merge 1 commit into
Conversation
After harvesting the recomputation-leaf gradients into the returned grads tuple, clear the leaves .grad attributes. When the detached leaves outlive the call (e.g. kept alive by saved-variable references from the recomputation subgraph), the stale .grad otherwise retains a full copy of the segment input gradients across iterations. Signed-off-by: iiap <1471127927@qq.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.
What does this PR do?
Re-submission of #6117 on current
dev(c76ff61). The change is byte-identical to the original —megatron/core/tensor_parallel/random.pyis unchanged since that PR was opened, so it applies cleanly.Why the original PR disappeared: #6117 was not withdrawn by me. My fork left the upstream fork network on Aug 5 (the repository was turned private for unrelated internal use), and GitHub auto-closes any open PR coming from a detached fork, attributing the close to the repo owner — which is why the timeline reads "htesd closed this". It took #6017 down in the same second; that one is re-submitted as #6338. This PR comes from a fresh, properly attached fork (
htesd/Megatron-LM-oss).#6117 never got past
copy-pr-bot, so it has no prior review or CI history. @yaox12 — sorry to ping you directly, you handled the sibling PR; would you mind vetting this one with/ok to testso CI can run?The bug
CheckpointFunction.backwardrecomputes the forward pass using detached segment inputs, runs backward on the recomputed graph, and collects the input gradients from the detached leaves:The detached leaves can remain reachable after
backwardreturns when the recomputed graph retains references to them. In that case, their.gradfields also keep the input-gradient storage alive longer than required.This change clears each detached tensor's
.gradafter collecting the returned gradients. Thegradstuple retains the gradient tensors, so the gradients returned byCheckpointFunction.backwardare unchanged. Nested checkpointing is unaffected: each call only touches its owndetached_inputs.Measured impact
Numbers below are from #6117 (measured on that PR's base commit); the affected code path is unchanged on current
dev.On a single H200, a 2-layer GPT configuration with
num_residual_streams=4, bf16, micro-batch size 1, mock data, andrecompute_granularity='full'produced the following allocator floor, in otherwise identical runs differing only by this change:This is a reduction of 0.27 GB for the measured configuration.
Weakref instrumentation confirmed that the detached recomputation inputs and their
.gradstorage remained reachable during the between-iteration interval before this change, and that clearing those.gradfields manually released the same allocator blocks. With this change, the gradient references are cleared immediately after collection.Assuming linear scaling with checkpointed segment count and sequence length, the measured result corresponds to an estimated reduction of approximately 2.4 GB for a 9-layer pipeline stage at sequence length 16384. This figure is an extrapolation, not a direct measurement.