Fix inter-iteration activation retention in HyperConnectionTransformerLayer - #6338
Open
htesd wants to merge 1 commit into
Open
Fix inter-iteration activation retention in HyperConnectionTransformerLayer#6338htesd wants to merge 1 commit into
htesd wants to merge 1 commit into
Conversation
…rLayer Release mlp_norm_manager after group_offload, mirroring the base class _forward_post_mlp. FineGrainedActivationOffloadingInterface stores the wrapped tensor unconditionally (even with offload=False), so a manager left attached to the layer keeps one microbatch's pre-MLP-norm input alive across iterations; with recompute_granularity='full' its autograd history additionally pins the layer's recomputation subgraph. Signed-off-by: iiap <1471127927@qq.com>
This was referenced Aug 7, 2026
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 #6017, rebased onto current
dev(c76ff61). The code change is identical — one line.Why the original PR disappeared: #6017 was not withdrawn by me. On Aug 5 my fork left the upstream fork network (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". Apologies for the churn, @yaox12 @hxbai: this happened ~3 hours after @yaox12 merged
devinto the branch and re-triggered CI. This PR comes from a fresh fork (htesd/Megatron-LM-oss) that is properly attached to the network.Prior review status on #6017, for reference:
/claude strict-review: passed, 0 findings (CRITICAL 0 / IMPORTANT 0 / SUGGESTION 0)/ok to testThe bug
HyperConnectionTransformerLayer._forward_post_mlp_with_fused_hyper_connection(transformer_layer.py:2305) never releasesself.mlp_norm_managerafter callinggroup_offload(), unlike the base classTransformerLayer._forward_post_mlp(transformer_layer.py:1134-1138), which setsself.mlp_norm_manager = Noneright after use.Because
FineGrainedActivationOffloadingInterface.__init__stores the wrapped tensor unconditionally (self.tensor = tensor, even whenoffload=False), the manager left attached to the layer module keeps a reference to one microbatch's pre-MLP-norm input — assigned unconditionally in_forward_mlp(transformer_layer.py:2151) on every forward — until the next forward pass overwrites it. The memory is retained across the idle window between iterations.The effect is strongly amplified with
recompute_granularity='full': the retained tensor is the one produced during the recomputation pass, and its autograd history keeps the layer's entire recomputation subgraph alive (stream-expanded residual buffers, h_post/BDA outputs, input grads). Holding one(s, b, h)tensor ends up pinning(s, b, n*h)-sized buffers several times over.Measured impact
Numbers below are from #6017 (measured on that PR's base commit); the affected code path is unchanged on current
dev.2-layer GPT config with hyper connections (
num_residual_streams=4), bf16, mbs=1, mock data,recompute_granularity='full', single GPU (H200), measuringtorch.cuda.memory_allocated()in the idle window between iterations:Extrapolated to a 9-layer pipeline stage at 16k sequence length, the retention is roughly 7 GB of wasted per-GPU memory.
The one-line fix mirrors the existing cleanup in the base class
_forward_post_mlp.A possible follow-up hardening (not included here, to keep this minimal): make
FineGrainedActivationOffloadingInterfacenot store the tensor whenoffload=False.