Repository navigation
fix(gemma4): stop unused full-length KV store from OOMing calibration replay - #3245
Merged
Merged
Conversation
… replay Gemma 4 marks the last layer of each attention type before the KV-shared tail with `store_full_length_kv`, so KV-shared layers can reuse its K/V through the `shared_kv_states` dict. Transformers sets the flag even when `num_kv_shared_layers == 0` (e.g. gemma-4-31B-it), where no layer ever reads it. Cached replay keeps one captured `shared_kv_states` dict per calibration batch, so every batch retains a full-length K/V copy of the last sliding and last full attention layer on the GPU. For gemma-4-31B-it with ~1.2M calibration tokens that is ~18 GiB (tokens x 8192 x bf16), which OOMs a 24 GB GPU while quantizing the second-to-last layer (layer 58), no matter how much headroom the other layers had. Disable the store at quantization start when the config has no KV-shared layers. Nothing consumes the stored K/V in that case, so no computation changes; variants with KV-shared layers (E2B/E4B) are left untouched because their shared layers read the stored K/V during replay. Tests build tiny real Gemma4ForCausalLM models and check that the flag is cleared, that a captured shared_kv_states dict no longer accumulates K/V while logits stay identical, and that KV-shared variants keep the store. Co-Authored-By: Claude Opus 5.5 <[email protected]>
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
GPTQ on
google/gemma-4-31B-it(24 GB GPU) always OOMs at layer 58/60. This PRdisables Gemma 4's unused KV-sharing store when
num_kv_shared_layers == 0, whichremoves the OOM without changing any computation.
Root cause
Transformers sets
store_full_length_kv = Trueon the last layer of each attentiontype (58 sliding, 59 full), which then writes its K/V into
shared_kv_stateson everyforward. With
num_kv_shared_layers == 0nothing reads it, so inference is unaffected.GPTQModel, however, captures the first-layer kwargs (including that dict) per
calibration batch and replays each layer with them. Every batch's dict keeps a
full-length K/V copy on the GPU: ~1.18M tokens x 8192 x 2 bytes ≈ 18 GiB, which OOMs
while layer 58 is processed.
Fix
At
pre_quantize_generate_hook_start(both Gemma 4 definitions), clearstore_full_length_kvon all decoder attention modules, only whennum_kv_shared_layers == 0. Outputs are unchanged. KV-shared variants (E2B/E4B) areleft untouched, since their shared layers read the stored K/V during replay.
Tests
New tests in
tests/test_gemma4_support.pyuse tiny realGemma4ForCausalLMmodels on CPU:shared_kv_statesdict no longer accumulates K/V, and logits are identical;pytest tests/test_gemma4_support.py -k "not local_variants": 9 passed (the first twonew tests fail without the fix).
ruff check(0.14.2): clean.Verification (gemma-4-31B-it, RTX 3090 24 GB, 1.18M calibration tokens)
A 2-layer model built from the real checkpoint (layers 58 and 59) reproduces the OOM in
about 2 minutes. A single layer is not possible because Gemma forces the last layer to
be full attention.
gptq.py quantize()Layers 57+59 OOM the same way on main, so the trigger is the layer's position, not its
weights. A full 60-layer run with the same change (applied from the quantization script
on 7.5.0) completes in about 4h12m, where two earlier runs OOMed at layer 58.