Skip to content

Skip RotatingKVCache in hybrid KV quantization and support quantized donor caches in Gemma 4 - #2092

Open
brianyu43 wants to merge 1 commit into
ml-explore:mainfrom
brianyu43:fix/kv-quantize-skip-rotating-cache
Open

brianyu43 wants to merge 1 commit into
ml-explore:mainfrom
brianyu43:fix/kv-quantize-skip-rotating-cache

Conversation

@brianyu43

Copy link
Copy Markdown

Why

Calling maybe_quantize_kv_cache() on hybrid sliding/full-attention models such as Gemma 3 and Gemma 4 crashes with NotImplementedError: RotatingKVCache Quantization NYI as soon as offset >= 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:

Layer group Count Cache type bf16 KV memory
sliding_attention (window=1024, 8 KV heads, dim=256) 25 RotatingKVCache 200 MiB fixed (any context length)
full_attention (2 KV heads, dim=512) 5 KVCache 20 KiB/token (640 MiB @ 32K, 2.5 GiB @ 128K, 5.0 GiB @ 256K)

Skipping RotatingKVCache when 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)

Scenario Before After
Hybrid cache (e.g., Gemma 3/4 with KVCache + RotatingKVCache) Crashes with NotImplementedError at quantized_kv_start Quantizes KVCache layers; skips RotatingKVCache / BatchRotatingKVCache
Rotating-only cache (e.g., --max-kv-size + --kv-bits) Crashes with NotImplementedError Still raises NotImplementedError (prevents silent no-op)
Gemma 4 KV-shared layers (num_kv_shared_layers > 0) with quantized donor cache Crashes with TypeError in scaled_dot_product_attention (cache=None) Passes donor cache[prev_idx] metadata to SDPA without mutating donor cache

Changes

  • mlx_lm/generate.py — Update maybe_quantize_kv_cache() to skip RotatingKVCache / BatchRotatingKVCache only when prompt_cache has at least one quantizable layer (QuantizedKVCache or non-rotating cache with to_quantized).
  • mlx_lm/models/gemma4_text/model.py — Pass layer_cache = cache[prev_idx] if kvs is not None else c in Gemma4TextModel.__call__ so KV-shared layers expose donor cache quantization metadata (bits, group_size).
  • mlx_lm/models/gemma4_text/layers.py — Guard cache.update_and_fetch(keys, values) with if shared_kv is None and cache is not None: in Attention.__call__.
  • tests/test_generate.py — Add TestKVQuantization covering hybrid skip (before, at, and after quantized_kv_start), rotating-only NotImplementedError, 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:

python3 -m unittest tests.test_generate.TestKVQuantization -v
# Ran 3 tests in 1.445s — OK

MLX_ENABLE_TF32=0 python3 -m unittest tests/test_prompt_cache.py tests/test_generate.py -v
# Ran 68 tests in 3.961s — OK

python3 -m pytest tests/test_models.py tests/test_utils.py tests/test_tuner_trainer.py -k "gemma4" -v
# 11 passed, 154 deselected in 1.73s

python3 -m pytest tests/test_server.py -q
# 43 passed in 20.59s

Note: On M5 without MLX_ENABLE_TF32=0, 8 pre-existing TestGenerate float32 tolerance checks fail due to TF32 precision; all 68 tests pass with MLX_ENABLE_TF32=0.

Refs #1573

@brianyu43
brianyu43 force-pushed the fix/kv-quantize-skip-rotating-cache branch from 0074a7d to 9899e92 Compare November 22, 2026 15:01

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.

1 participant