Local Affine 4-Bit KV-Cache Quantization: Fixing Key-Projection Bias Collapse

Local 4-bit KV-cache quantization leaves LLaMA-3.1-8B and Mistral-7B nearly untouched (+0.039 to +0.261 PPL) while catastrophic key-range failures hit Qwen2.5-1.5B (+46,347 PPL) and Qwen3-8B (+35.4 PPL). Research (arXiv:2612.03029) proves persistent rotated key-projection bias consumes shared key ranges; subtracting this analytic offset before rounding restores Layer-0 NLL from 9.037 back to 2.205 (FP16: 2.199).
Local Affine 4-Bit KV-Cache Quantization: Fixing Key-Projection Bias Collapse: Local 4-bit KV-cache quantization leaves LLaMA-3.1-8B and Mistral-7B nearly untouched (+0.039 to +0.261 PPL) while catastrophic key-range failures hit Qwen2.5-1.5B (+46,347 PPL) and Qwen3-8B (+35.4 PPL). Research (arXiv:2612.03029) proves persistent rotated key-projection bias consumes shared key ranges; subtracting this analytic offset before rounding restores Layer-0 NLL from 9.037 back to 2.205 (FP16: 2.199). Designed as a zero-dependency, open-source TypeScript architecture under the MIT License with native Model Context Protocol (MCP) support and deterministic phase state machines.
- Identifies Exact Root Cause of Qwen INT4 Failure: 44 coordinates in Qwen2.5-1.5B hold 99.96% of first-layer key-bias energy; subtracting analytic R_t * b_K leaves only 0.0254% residual energy.
- Near-FP16 Recovery via Centering: Restores Qwen2.5-1.5B Layer-0 zero-preserving 4-bit NLL from 9.037 down to 2.205 (within 0.006 nats of FP16 2.199).
- Separates Grouping from Grid Origin: Shows why channel-grouped INT4 fails unless centered, whereas token-grouped quantization lowers 2-bit key loss across all tested models.
- Eliminates Misleading MSE Calibration: Demonstrates that lower local reconstruction MSE or attention KL divergence can still worsen end-to-end prediction loss if rounding-code flips occur.
import { createAgent } from '@smoke-monkey/harness';// Run long-context local agents with centered 4-bit KV-cache quantization safeguardsconst agent = createAgent({agentId: 'affine-kv4-serving-agent',provider: 'openai-compatible',baseUrl: 'http://127.0.0.1:8000/v1',model: 'Qwen/Qwen2.5-7B-Instruct',workspacePath: process.cwd(),mcpServers: {kvQuantAudit: {command: 'npx',args: ['-y', '@smoke-monkey/mcp-kv-audit', '--bits', '4'],env: {KV_QUANT_GROUP_MODE: 'channel_centered',KV_QUANT_GROUP_SIZE: '32',KV_SUBTRACT_ROTATED_BIAS: 'true',},},},permissions: {read_file: 'allow',write_file: 'ask',run_command: 'ask',},});async function main() {const result = await agent.run('Audit Layer-0 key projection bias energy before 4-bit KV quantization and verify WikiText-103 NLL stays within 0.02 nats of FP16.');console.log('Audit Result:', result.summary);}main().catch(console.error);
Watch: Related Video Guides
How KV Cache Speeds Up LLMs for Faster AI Models on GPUs
The KV Cache: Memory Usage in Transformers
Why Does 4-Bit KV Quantization Break Qwen Models While Sparing Llama-3?
Local affine 4-bit KV-cache quantization (arXiv:2612.03029) compresses attention keys and values to 4 bits per element using per-block scales and zero-points, cutting KV-cache VRAM by 4x. Across eight evaluated models on WikiText-103 (30 windows of 2,048 tokens), value quantization adds negligible perplexity (+0.011 to +0.185 PPL), whereas key quantization varies by five orders of magnitude.
In architectures with biased key projections and no pre-RoPE key RMSNorm (such as Qwen2.5 and StableLM-2), RoPE transforms the static parameter bias into k_t = R_t(W_K h_t) + R_t b_K. Even though a constant vector shift cancels in exact softmax attention, sharing a coarse 4-bit quantization range across biased and unbiased channels converts that constant bias into position-varying rounding noise that flips attention logits.
| Model (WikiText-103, 2048-tok) | FP16 PPL | Worst V-Only Delta PPL | K-Only Min Delta PPL | K-Only Max Delta PPL | g=64 Sym / Aff PPL (K+V 4-bit) |
|---|---|---|---|---|---|
| LLaMA-2-7B | 5.829 | +0.017 | +0.021 | +0.089 | 5.932 / 5.875 |
| LLaMA-3.1-8B | 6.546 | +0.034 | +0.039 | +0.261 | 6.848 / 6.696 |
| Mistral-7B-v0.1 | 5.669 | +0.011 | +0.010 | +0.060 | 5.741 / 5.706 |
| Qwen3-4B | 13.842 | +0.101 | +0.123 | +19.9 | — |
| Qwen3-8B | 10.083 | +0.019 | -0.041 | +35.4 | — |
| Qwen3-1.7B | 17.098 | +0.185 | +1.032 | +3,889 | 4,031 / 1,015 |
| Qwen2.5-1.5B | 9.417 | +0.078 | +60.4 | +46,347 | 4.4e4 / 1.2e4 |
Analytic Rotated-Bias Removal vs Positional Trajectory Tracking
Comparing captured native FP16 keys against analytic FP64 R_t b_K across 38 evaluation texts establishes that parameter bias—not token activations—dominates first-layer key energy:
- Subtracting Analytic Rotated Bias: In Qwen2.5-1.5B, subtracting
R_t b_Kleaves just 0.0254% of native key squared energy (0.503% in StableLM-2-1.6B), with 44 slow-rotating coordinates carrying 99.96% of first-layer bias energy. Centering those selected coordinates before 4-bit quantization drops Layer-0 NLL from 9.037 to 2.205 (FP16: 2.199). - Positional Tracking for Long Contexts: In StableLM, high-energy bias coordinates do not stay static over long sequences. Tracking the positional
R_t b_Ktrajectory outperforms fixed mean removal by +0.1239 nats/token at 4K tokens (and +0.0123 nats/token at 16K in Qwen2.5). - 2-Bit Key Grouping Limit: At 4-bit precision, channel-wise centering repairs channel-grouped KV layouts cleanly; at 2-bit keys, neighbouring unshifted coordinates also matter and per-token grouping outperforms channel grouping.
Do Not Trust Min-Offset Affine Grids Alone on Channel-Grouped Keys
In Table 2, standard min-offset affine quantization at g=16 worsened Qwen2.5-1.5B perplexity to 965 vs 78.9 for symmetric quantization because shared block ranges still shift relative key coordinates unevenly. Always center persistent channel offsets before quantizing.
Reproducible Key-Projection Bias Energy Diagnostic
Inspect any Hugging Face checkpoint's key projection bias norm before enabling 4-bit KV-cache quantization in vLLM or llama.cpp:
# Check whether a model checkpoint has biased key projections vulnerable to uncentered INT4 KV
import json, urllib.request
for repo in ["Qwen/Qwen2.5-7B-Instruct", "meta-llama/Llama-3.1-8B-Instruct"]:
url = f"https://huggingface.co/{repo}/raw/main/config.json"
try:
cfg = json.loads(urllib.request.urlopen(url, timeout=5).read().decode())
print(repo, "attention_bias=", cfg.get("attention_bias"), "qkv_bias=", cfg.get("qkv_bias"))
except Exception as exc:
print(repo, "skipped:", exc)Frequently Asked Questions
Q:Why does standard symmetric or min-offset 4-bit KV quantization fail on Qwen2.5?
Qwen2.5 has a large key-projection bias b_K without pre-RoPE key RMSNorm. Rotating b_K via RoPE creates large persistent channel offsets that consume the shared 4-bit block range, turning rounding noise into position-varying attention logit errors.
Q:Is value (V) cache quantization as sensitive as key (K) cache quantization?
No. Across all eight tested models on WikiText-103, 4-bit value-only quantization changed perplexity by at most +0.185 PPL, whereas 4-bit key-only quantization reached +35.4 PPL on Qwen3-8B and +46,347 PPL on Qwen2.5-1.5B.
Q:Why does Qwen3 still have persistent key coordinates despite having no key bias?
Qwen3 removes linear key-projection bias and applies Q/K RMSNorm before RoPE, yet still exhibits persistent channel means in specific coordinates, making channel-wise centering helpful even without an explicit b_K parameter.
Q:Should I use channel grouping or token grouping for 2-bit vs 4-bit KV caches?
Use channel grouping with per-channel centering for 4-bit KV caches (near-FP16 quality), and prefer token grouping if dropping down to 2-bit keys.
Related Alternatives & Comparisons
Related Architecture Guides
View all guidesBest Open Source Coding Agents in 2026: Free, Local & Fully Hackable Harnesses
MCP Server Security Best Practices: Hardening Model Context Protocol Agents in 2026
Open Source Coding Agent Harness: Build a Forkable, Local AI Engineering Runtime
Build with Smoke Monkey Harness
Zero dependencies. 24 built-in tools. Human-in-the-loop safety. 100% open source under the MIT License.