Fix GPU memory leak from loss tensor autograd retention#486
Open
Fix GPU memory leak from loss tensor autograd retention#486
Conversation
…tention across microbatches Loss scalars stored in context.losses retained FunctionBackward grad_fn references from wrap_forward_backward, keeping C++ autograd nodes and their CUDA tensor references alive across all microbatches. This caused ~164 MiB/microbatch growth, leading to OOM with depth_first_micro_batches>=128. Fix: .detach() total_loss in head.py and individual losses in loss.py before appending to the losses dict. These values are only used for logging (reduced to .item() at step end), so detaching is safe. Also adds per-microbatch memory logging in the schedule runner and frees batch data after the last forward stage.
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
total_lossinhead.pyand individual loss values inloss.pybefore appending tocontext.losses. These scalar tensors retainedFunctionBackwardgrad_fn references fromwrap_forward_backward, keeping C++ autograd nodes (and their CUDA tensor references) alive across all microbatches in a training step.depth_first_micro_batches >= 128, memory grew ~164 MiB per microbatch, causing OOM on 80 GB H100s. The RL team needs 4K+ microbatches per step.context.batchentries after the last forward stage.Root Cause
total_lossreturned from_logits_loss_forward_backwardis the same tensor object thatwrap_forward_backward()wraps with a custom autogradFunction. PyTorch attaches aFunctionBackwardgrad_fn to the returned tensor in-place afterFunction.forwardcompletes. Sincetotal_lossis stored incontext.losses(for logging), each microbatch'slosses dict entry holds a live grad_fn chain back to that microbatch's backward context (
ctx.contextin theFunction), which references CUDA tensors (stage input/output pairs).These C++ autograd nodes are invisible to Python's
gc.get_objects()but consume real GPU memory — approximately 14 C++ CUDA allocations (~164 MiB) per microbatch. With 128 microbatches: 128 × 164 MiB ≈ 21 GiB of leaked autograd state.The fix is safe because
context.lossesvalues are only used for logging — they are reduced to.item()scalars at step end in_reduce_losses.Test Results
end_allocend_allocTested on Qwen2.5-7B-Instruct, 8× H100, SDP=2, ZeRO-2, 16K sequence length, full recompute.
Test plan