Skip to content

Per-head adaptive KV cache quantization: lossless q4_0 on hybrid models, +8% quality on standard models #21385

Description

@SCJedi

Summary

Two findings from production benchmarks of -ctk q4_0 -ctv q4_0:

  1. q4_0 KV cache is completely lossless on hybrid models (Qwen3.5) -- BLEU 1.000 across 10 test configurations at 4x compression
  2. For standard models (Llama, Mistral), per-head entropy-adaptive bit allocation improves quality by +8% BLEU at the same compression ratio

Feature request: per-head KV cache type support to enable adaptive allocation.

Finding 1: Lossless q4_0 on Hybrid Models

A/B benchmarks on RTX 3060 12GB, Qwen3.5-9B, 3-run averages:

Test f16 gen/s q4_0 gen/s Speed Diff
Short (2-turn) 48.2 47.0 -2.4%
Medium (8-turn) 47.0 45.8 -2.6%
Long (20+ turn) 45.9 45.8 -0.1%

Quality: BLEU 1.000 (n=50 completions). Token-level identical output between f16 and q4_0 KV cache.

The 33K-token conversation test ran at 36.5 tok/s on q4_0 -- f16 would OOM on the same 12GB GPU.

Why Hybrid Models Tolerate q4_0

Qwen3.5 uses only 8 of 32 layers for full attention with KV cache. The other 24 layers use linear attention (Gated Delta Net) with no KV cache. Similarly, Gemma 4 uses 10 of 60 layers for global attention.

The linear/sliding attention layers act as error correction -- quantization noise in the few attention layers is absorbed by the surrounding layers. Standard models (Llama, Mistral) have no such correction because all layers use full attention.

Finding 2: Entropy-Adaptive Allocation for Standard Models

For standard architectures where q4_0 does degrade quality, per-head bit allocation based on attention entropy recovers most of the loss:

b_h = b_avg + 0.25 * (H_avg - H_2(h))

Where H_2(h) is the Renyi entropy of head h's attention distribution.

Results on GPT-2 (all at 4x compression):

Config BLEU Token Match PPL Ratio
Uniform 4-bit 0.531 51.3% 1.101
Adaptive 4-bit 0.548 53.8% 1.089
Skip 3 sinks + adaptive 0.574 60.0% 1.062

The key insight: the bottom 2% of heads by entropy ("sink heads") contribute disproportionately to quantization error. Skipping quantization on just 3 out of 144 heads provides more benefit than optimal bit redistribution across all others.

Feature Request: Per-Head KV Cache Types

Currently -ctk and -ctv apply uniformly to all layers and heads. Per-head allocation would enable:

  • Sink heads (lowest entropy, ~2% of heads): keep at f16 or q8_0
  • Standard heads: q4_0 as today
  • High-entropy heads (diffuse attention): could go to q3_0 or lower

This maintains the same average compression ratio but shifts bits to where they matter most. The implementation would need:

  1. A calibration pass to compute per-head entropy (or accept a pre-computed entropy map)
  2. Per-head type selection in the KV cache allocation
  3. Appropriate dequantization dispatch per head during attention

The entropy computation is a one-time cost per model architecture (not per session). The per-head map could be distributed alongside GGUF files or computed on first load.

Production Deployment Data

The full 12x compression stack (q4_0 KV + application-level context eviction) is running in production on RTX 3060:

  • 128K context window (vs 32K with f16 KV)
  • 45-48 tok/s generation speed
  • Infinite conversation length via automatic eviction
  • Zero model or llama.cpp source changes required

References

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions