Repository navigation
Conversation
brianyu43
force-pushed
the
fix/kv-quantize-skip-rotating-cache
branch
from
November 22, 2026 15:01
0074a7d to
9899e92
Compare
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.
Why
Calling
maybe_quantize_kv_cache()on hybrid sliding/full-attention models such as Gemma 3 and Gemma 4 crashes withNotImplementedError: RotatingKVCache Quantization NYIas soon asoffset >= quantized_kv_start, blocking KV cache quantization on those architectures.In Gemma 4 26B-A4B (30 layers), almost all long-context KV growth comes from the few full-attention layers, while the sliding-window layers stay bounded and benefit from fused unquantized SDPA:
bf16KV memorysliding_attention(window=1024, 8 KV heads,dim=256)RotatingKVCachefull_attention(2 KV heads,dim=512)KVCacheSkipping
RotatingKVCachewhen a cache also contains quantizable layers eliminates the crash, captures virtually all long-context memory savings, and avoids quantized-matmul overhead on the sliding layers.Behavior (Before vs. After)
KVCache+RotatingKVCache)NotImplementedErroratquantized_kv_startKVCachelayers; skipsRotatingKVCache/BatchRotatingKVCache--max-kv-size+--kv-bits)NotImplementedErrorNotImplementedError(prevents silent no-op)num_kv_shared_layers > 0) with quantized donor cacheTypeErrorinscaled_dot_product_attention(cache=None)cache[prev_idx]metadata to SDPA without mutating donor cacheChanges
mlx_lm/generate.py— Updatemaybe_quantize_kv_cache()to skipRotatingKVCache/BatchRotatingKVCacheonly whenprompt_cachehas at least one quantizable layer (QuantizedKVCacheor non-rotating cache withto_quantized).mlx_lm/models/gemma4_text/model.py— Passlayer_cache = cache[prev_idx] if kvs is not None else cinGemma4TextModel.__call__so KV-shared layers expose donor cache quantization metadata (bits,group_size).mlx_lm/models/gemma4_text/layers.py— Guardcache.update_and_fetch(keys, values)withif shared_kv is None and cache is not None:inAttention.__call__.tests/test_generate.py— AddTestKVQuantizationcovering hybrid skip (before, at, and afterquantized_kv_start), rotating-onlyNotImplementedError, and tiny Gemma 4 (num_kv_shared_layers=2) prefill + quantization + decode.Verification
Tested on Apple Silicon (M5), Python 3.11,
mlx 0.32.4:Refs #1573