Skip to content

fix(gemma4): stop unused full-length KV store from OOMing calibration replay - #3245

Merged
Qubitium merged 1 commit into
ModelCloud:mainfrom
okdshin:fix/gemma4-shared-kv-states
Sep 29, 2026
Merged

Qubitium merged 1 commit into
ModelCloud:mainfrom
okdshin:fix/gemma4-shared-kv-states

Conversation

@okdshin

@okdshin okdshin commented Sep 29, 2026

Copy link
Copy Markdown
Contributor

Summary

GPTQ on google/gemma-4-31B-it (24 GB GPU) always OOMs at layer 58/60. This PR
disables Gemma 4's unused KV-sharing store when num_kv_shared_layers == 0, which
removes the OOM without changing any computation.

Root cause

Transformers sets store_full_length_kv = True on the last layer of each attention
type (58 sliding, 59 full), which then writes its K/V into shared_kv_states on every
forward. With num_kv_shared_layers == 0 nothing 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), clear
store_full_length_kv on all decoder attention modules, only when
num_kv_shared_layers == 0. Outputs are unchanged. KV-shared variants (E2B/E4B) are
left untouched, since their shared layers read the stored K/V during replay.

Tests

New tests in tests/test_gemma4_support.py use tiny real Gemma4ForCausalLM models on CPU:

  • the flags are cleared when there are no KV-shared layers;
  • a captured shared_kv_states dict no longer accumulates K/V, and logits are identical;
  • the flags are kept for a KV-shared config.

pytest tests/test_gemma4_support.py -k "not local_variants": 9 passed (the first two
new 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.

Peak GPU memory Result
main (1d9716a) 23.2 GiB (device full) OOM in gptq.py quantize()
this PR 11.4 GiB completes

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.

… 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]>

@Qubitium Qubitium left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM!

@Qubitium
Qubitium merged commit 480c3d7 into ModelCloud:main Sep 29, 2026
1 check passed
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.

2 participants