Repository navigation
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>
Author
|
For the record: this PR was not closed by me on purpose. My fork left the upstream fork network on Aug 5 (repo turned private for unrelated internal use), and GitHub auto-closes open PRs from a detached fork, attributing the close to the repo owner — hence the misleading "htesd closed this". It took #6017 down in the same second. Re-opened as #6339 from a clean fork, on current |
Merged
4 tasks done
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?
CheckpointFunction.backwardinmegatron/core/tensor_parallel/random.pyrecomputesthe forward pass using detached segment inputs, runs backward on the recomputed graph,
and collects the input gradients from the detached leaves:
grads = tuple(inp.grad if isinstance(inp, torch.Tensor) else inp for inp in detached_inputs)The detached leaves can remain reachable after
backwardreturns when the recomputedgraph retains references to them. In that case, their
.gradfields also keep theinput-gradient storage alive longer than required.
This change clears each detached tensor's
.gradafter collecting the returnedgradients. The
gradstuple retains the gradient tensors, so the gradients returned byCheckpointFunction.backwardare unchanged. Nested checkpointing is unaffected: eachcall only touches its own
detached_inputs.Measured impact
On a single H200, a 2-layer GPT configuration with
num_residual_streams=4, bf16,micro-batch size 1, mock data, and
recompute_granularity='full'produced thefollowing 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 thischange, and that clearing those
.gradfields manually released the same allocatorblocks. 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.