[Fix] Reduce ChunkLoss memory usage with detached LM heads - #2060
Open
matrix72c wants to merge 2 commits into
Open
[Fix] Reduce ChunkLoss memory usage with detached LM heads#2060matrix72c wants to merge 2 commits into
matrix72c wants to merge 2 commits into
Conversation
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
ChunkLosscurrently allocates and retains a full-sized LM-head gradient tensor even when the suppliedhead_weightis detached and does not require gradients. This happens in the MTP path whendetach_mtp_lm_head_weight=Trueand can waste a substantial amount of accelerator memory because the tensor scales withvocab_size * hidden_size.This change makes the custom autograd function respect
head_weight.requires_gradthroughout forward and backward:Nonefor the head-weight gradient when no gradient was requested.The trainable-head path keeps its existing loss and gradient behavior.
Problem
Before this change,
ChunkLoss.forward()unconditionally createdgrad_weight = torch.zeros_like(head_weight). Each chunk also produced or allocated a tensor with the same shape, and the accumulated tensor was saved for backward.For a detached LM head this work cannot contribute to optimization: PyTorch does not request a gradient for that input, and the custom backward should return
None. Nevertheless, the implementation still paid the memory cost of one or more vocabulary-sized weight-gradient buffers. Depending on model dimensions and dtype, this can consume hundreds of MiB or more.The existing MTP loss intentionally exercises this case by detaching the LM-head weight when
detach_mtp_lm_head_weightis enabled.Implementation
head_weightrequires gradients at the start of the custom forward.grad_weightonly for the trainable-head path.grad_inputsalone for the detached-head path and record the condition on the autograd context.Noneforhead_weight.This is deliberately local to
ChunkLoss: it does not alter the chunking algorithm, numerical loss definition, MTP configuration, or public API.Tests
Added focused regression tests covering both modes:
head_weight.gradunset, and verifies that no full-sized zero head-gradient buffer is allocated.Validation performed: