Skip to content

Fix Gemma 4 activation checkpointing memory leak (#51213) - #51221

Open
shotsan wants to merge 1 commit into
huggingface:mainfrom
shotsan:fix-gemma4-checkpointing-leak-51213
Open

shotsan wants to merge 1 commit into
huggingface:mainfrom
shotsan:fix-gemma4-checkpointing-leak-51213

Conversation

@shotsan

@shotsan shotsan commented Nov 25, 2026 •

Copy link
Copy Markdown

🤗 Transformers CI

What does this PR do?

Fixes #51213

Root Cause

When activation checkpointing is enabled in Gemma4TextModel, the mutable container shared_kv_states (KVSharingDict) was passed in kwargs to every decoder layer. For producer layers (store_full_length_kv == True), the layer directly assigned its newly computed (key_states, value_states) tensors into shared_kv_states.

Under PyTorch's non-reentrant activation checkpointing (use_reentrant=False), _NoopSaveInputs saves non-tensor arguments (like kwargs) as strong references in the recomputation context. Because shared_kv_states was 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 calling backward() (e.g. evaluation/inference loops or RL rollout phases), intermediate decoder-layer activations accumulated indefinitely.

Solution

  1. In Gemma4TextAttention, producer layers return stored_kv = (key_states, value_states) as an output of attention rather than mutating shared_kv_states in-place inside the checkpointed block.
  2. Gemma4TextDecoderLayer returns (hidden_states, stored_kv) when return_stored_kv=True (preserving backward compatibility for callers expecting just hidden_states).
  3. In Gemma4TextModel.forward, shared_kv_states is updated outside the checkpointed decoder layer call.
  4. Only consumer layers (is_kv_shared_layer == True) receive shared_kv_states, and only read from it, ensuring no checkpoint frame captures tensors produced inside its own block.
  5. Added unit test test_activation_checkpointing_no_memory_leak to tests/models/gemma4/test_modeling_gemma4.py.

…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
@github-actions

Copy link
Copy Markdown
Contributor

Thank you for your contribution 🤗!

CI Security Gate — waiting for maintainer approval

CI 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.

@github-actions

Copy link
Copy Markdown
Contributor

[For maintainers] Suggested jobs to run (before merge)

run-slow: gemma4

@haoxu0

haoxu0 commented Nov 25, 2026

Copy link
Copy Markdown

Please don't use AI to scam the code. Also you should respect to the issue creator.
And at last, the solution isn't good and more of adhoc fix.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Gemma 4: memory leak

2 participants