Repository navigation
Conversation
…shared KV When activation checkpointing is enabled in Gemma4TextModel, passing the mutable shared_kv_states dictionary to decoder layers that write full-length KV states causes PyTorch's non-reentrant checkpointing frame to retain references to the newly computed tensors via kwargs in the recompute closure. This forms an uncollectable cycle between the checkpoint frame and autograd nodes when forward passes are executed without a subsequent backward pass. Fix this by returning newly stored KV states directly from producer decoder layers and updating shared_kv_states in Gemma4TextModel outside of the checkpointed call, only passing shared_kv_states to consumer layers that read from it. Fixes huggingface#51213
Contributor
|
Thank you for your contribution 🤗! CI Security Gate — waiting for maintainer approvalCI is approved automatically only for authors who already have a contribution merged in this repository. A maintainer will review this PR and approve CI to run on it. |
Contributor
|
[For maintainers] Suggested jobs to run (before merge) run-slow: gemma4 |
|
Please don't use AI to scam the code. Also you should respect to the issue creator. |
This branch has not been deployed
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?
Fixes #51213
Root Cause
When activation checkpointing is enabled in
Gemma4TextModel, the mutable containershared_kv_states(KVSharingDict) was passed inkwargsto every decoder layer. For producer layers (store_full_length_kv == True), the layer directly assigned its newly computed(key_states, value_states)tensors intoshared_kv_states.Under PyTorch's non-reentrant activation checkpointing (
use_reentrant=False),_NoopSaveInputssaves non-tensor arguments (likekwargs) as strong references in the recomputation context. Becauseshared_kv_stateswas mutated in-place during the checkpointed forward pass to hold tensors produced within that block, an uncollectable cycle was formed between the checkpoint frame (_CheckpointFrame/unpack_hook), the dictionary, the tensors, and the autograd computation graph. Consequently, whenever forward passes were executed under gradient tracking without callingbackward()(e.g. evaluation/inference loops or RL rollout phases), intermediate decoder-layer activations accumulated indefinitely.Solution
Gemma4TextAttention, producer layers returnstored_kv = (key_states, value_states)as an output of attention rather than mutatingshared_kv_statesin-place inside the checkpointed block.Gemma4TextDecoderLayerreturns(hidden_states, stored_kv)whenreturn_stored_kv=True(preserving backward compatibility for callers expecting justhidden_states).Gemma4TextModel.forward,shared_kv_statesis updated outside the checkpointed decoder layer call.is_kv_shared_layer == True) receiveshared_kv_states, and only read from it, ensuring no checkpoint frame captures tensors produced inside its own block.test_activation_checkpointing_no_memory_leaktotests/models/gemma4/test_modeling_gemma4.py.