DEV Community

Karan Yadav
Karan Yadav

Posted on Originally published at nandai.org

Asymmetric KV-Cache Quantization in llama.cpp (--cache-type-k vs --cache-type-v): Exact GGML Byte Math on Apple Silicon

Most guides on llama.cpp KV-cache quantization treat Keys (K) and Values (V) identically, swapping symmetric FP16/FP16 down to Q8_0/Q8_0 or Q4_0/Q4_0. Yet transformer attention puts very different numerical sensitivity on those two projections:

  • Keys (K) feed every Q · K^T dot-product softmax score, where quantization noise shifts attention routing across long prompts.
  • Values (V) are weighted averages summed after softmax, tolerating tighter quantization cleanly on many GQA workloads.

Because llama.cpp exposes --cache-type-k and --cache-type-v independently, mixing K = Q8_0 with V = Q4_0 gives you high-precision attention matching while cutting KV memory by nearly 60% across every concurrent sequence (n_parallel).


1. Exact GGML Asymmetric Byte Math: K = Q8_0, V = Q4_0 = 0.40625× FP16

In GGML, quantized caches pack 32-element blocks prefixed by a 2-byte fp16 block scale (d):

  • block_q8_0: 2 B (fp16 scale) + 32 B (int8 values) = 34 B / 32 (1.0625 B/elem = 0.53125× FP16)
  • block_q4_0: 2 B (fp16 scale) + 16 B (packed 4-bit nibbles) = 18 B / 32 (0.5625 B/elem = 0.28125× FP16)

Once --cache-type-k and --cache-type-v differ, the symmetric 2 × factor in the standard KV formula drops out cleanly into separate sums over n_parallel (N_seq) concurrent slots:

KV_Bytes = N_seq × (bytes_K + bytes_V)

Ratio vs. symmetric FP16/FP16 (64 B + 64 B = 128 B per 32-element K+V pair):
  K=Q8_0, V=Q4_0 -> (34 + 18) / 128 = 52 / 128 = 0.40625× FP16  (59.375% reduction)
Enter fullscreen mode Exit fullscreen mode

For multi-slot local serving (e.g., n_parallel = 4 on 24 GB–48 GB Apple Silicon), that drops combined KV cache below Q8_0/Q8_0 (0.53125×) while avoiding the softmax routing drift of uniform Q4_0/Q4_0 (0.28125×).


2. Sizing Unified Memory Alongside Static Weights & Metal Scratch

Total Apple Silicon Unified Memory allocation separates into three independent components:

Total Unified Memory = Static Weights + KV Cache (bytes_K + bytes_V × n_parallel) + Metal Compute / Scratch
Enter fullscreen mode Exit fullscreen mode

You can dial independent --cache-type-k and --cache-type-v selectors alongside n_parallel (1..64) right inside browser RAM at nandai.org/tools/gguf-vram-calculator, and check full bandwidth/roofline trade-offs across Mac tiers in nandai.org/academy/apple-silicon-memory-bandwidth-roofline-and-kv-cache-quantization and nandai.org/apple-silicon/macbook-pro-24gb-36gb-48gb-workstation.

Audited M4 Pro 24 GB hardware receipts (78.62 tok/s decode, 962.31 tok/s prefill) are archived under CERN Zenodo DOI 10.5281/zenodo.24642737, and the 271 MB notarized macOS desktop suite is available at nandai.org/download.

Top comments (0)