Repository navigation
fix(awq): supply KV-shared state for Gemma consumer replay - #6317
Merged
Merged
Conversation
Gemma 3n / Gemma 4 / Gemma 4-Unified replay their KV-sharing tail with the first decoder layer's captured kwargs. That capture holds an empty (or last-batch) shared_kv_states dict, so a consumer attention raised KeyError: '<layer_type>'. The definitions now rebuild the producer layer's K/V for the exact packed replay rows and never substitute the empty/stale dict. The consumer's input-norm group is also re-anchored on q_proj: o_proj reads the attention output, so folding the norm into it changed the decoder layer output.
Qubitium
marked this pull request as ready for review
November 29, 2026 07:57
This was referenced Nov 29, 2026
Qubitium
added a commit
that referenced
this pull request
Nov 29, 2026
…folds Rebased onto main after the 07:55-08:05 merge wave. `main` already carries #6317's KV-shared fold retarget (`retarget_kv_shared_attention_groups`). This branch's `_retarget_attention_output_folds` is the broader form of the same rule -- it also covers `attention_k_eq_v` layers, which own `q_proj`/`k_proj` but no `v_proj` -- so the merged code keeps #6317's `awq_module_replay_inputs` replay wiring and uses the broader retarget for `awq_get_modules_for_scaling`. The two are idempotent with respect to each other: once `o_proj` is gone the second pass is a no-op.
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
Fixes #6316.
AWQ replays the first decoder layer's captured kwargs. The Gemma text stacks
create one
KVSharingDictbefore the decoder loop and pass it to every layer asshared_kv_states; the first-layer pre-hook sees it empty, so the capturedvalue is
{}. Replaying a KV-shared consumer attention then executesshared_kv_states[self.layer_type]and raisesKeyError. Gemma 3n defaults tonum_kv_shared_layers=15, so a default-config quantize has a 15-layer consumertail.
What Changed
gptqmodel/models/definitions/_kv_shared_replay.py:rebuild_kv_shared_replay_states: for a KV-shared consumer attention,resolve its producer layer from
config.kv_sharing_modes/layer_types,project the packed replay rows through the producer's
k_proj/k_norm/v_proj/v_norm, rotate with the replay's own positions,and return a
KVSharingDictaligned to those rows. It never returns{};if the producer is unavailable (meta/offload, missing rotary) it raises
NotImplementedError, whichAWQProcessor._prepare_module_forwardalreadyconverts to
_AWQScaleGroupErrorso the configured fallback applies.retarget_kv_shared_attention_groups: keeps only the consumer projectionthat actually reads the fold source (
q_proj) in the input-norm group andre-anchors
module2inspecton that linear.gemma3n.py,gemma4.py(text + composite),gemma4_unified.py(text +composite): add
awq_module_replay_inputsandawq_get_modules_for_scalinghooks delegating to the shared helper.tests/test_awq_shared_kv_states_replay.py: 11 CPU-only tests coveringGemma 3n, Gemma 4 text + composite, and Gemma 4-Unified (sliding and full
consumers), plus the norm-fold neutrality invariant and one end-to-end tiny
AWQ quantize.
Which option, and why
The task offered (a) supply usable
shared_kv_statesderived from the packedreplay rows, (b) exclude consumer layers from the packed replay, or (c) capture
the producer layer too. This PR does (a) + (b), because (a) alone leaves a
silent numerics bug:
activation is not in the first-layer capture, so the only row-aligned source
is the replay's own input. Applying the producer's K/V projections to those
rows reproduces exactly what the producer layer would store for the same
tokens (verified: the rebuilt tensors equal a producer attention forward on
the same
x/positions). This keeps the consumer's shared-KV behaviour andnever passes
{}. This hook is the crash fix for any consumer-attentionreplay.
consumer the tree tracker cannot anchor
o_projon a missingv_proj, so itfalls back to
input_layernormand mergeso_projintoq_proj's group.o_projreads the attention output, not the norm, so folding a non-uniformscale into it changes the decoder layer output (measured
1.839e-01relative change on a tiny Gemma 4 consumer with
scales = 1 + 0.05*arange(hidden)). Re-anchoring onq_projmakes the foldexactly neutral (
~2e-07) and removes the attention-block replay from theAWQ path;
o_projis still AWQ-quantized plainly (asserted end-to-end).This follows the Hymba single-member precedent ([BUG] #5019 (fix of #4891) incomplete: Hymba AWQ still always crashes on the real checkpoint at layer 2 - HymbaBlock replay lacks kv_last_layer for KV-reuse layers (AssertionError, no fallback) #5098).
Option (c) was rejected: capturing every producer layer would change the
first-layer-only capture contract and keep full-length K/V for every calibration
batch (the OOM class #3245 already dealt with).
Tests
Red at
origin/main89dbcf91(fix files reverted, new test kept):KeyError tracebacks (origin/main):
Green after the fix:
The two skips are pre-existing in
test_awq_scaling_groups.py(
opt350/muse_glimmerare not buildable CausalLM families there). The newtest file alone is
11 passed in 4.05s.Also run:
Skips / unverified
CUDA_VISIBLE_DEVICES=""); no GPU end-to-end checkpoint wasquantized.
tests/models/test_gemma3n.pywas not run: it quantizes the 11 GB/monster/data/model/gemma-3n-E2B-itcheckpoint on CPU and was still runningafter several minutes (killed). The tiny native-port fixtures plus the
module-tree/AWQ suites cover the changed code paths.
Gemma3nForConditionalGenerationGPTQ) has the sametwo-line hooks as the text class but was not instantiated in the tests;
Gemma 4's composite class is covered.
rebuild uses the producer module as-is (dequantizing forward), but that path
is not exercised end-to-end here because the group retarget removes the
consumer-attention replay from the real AWQ flow.
Model Support / Module Plan
No module-tree changes:
awq_get_modules_for_scalingonly narrows the emittedscaling group for KV-shared consumer layers. Every quantizable linear still
quantizes; the end-to-end test asserts the consumer
q_projando_projareAwqTorchLinearafterquantize().