Benchmark Study #6

Long-Context (8K to 128K) KV Cache Memory Scaling Benchmark

Loading a model at 4K default context hides the memory cliff waiting at 64K and 128K tokens. Inspect exact GQA KV cache growth with and without cache quantization.

Rate Card & Spec Audit: Published by: ShiftFrameLabs Data Processing: 100% Local Client-Side Browser Math

How Much Extra VRAM Does a 128K Context Window Consume?

Direct Answer Summary (Verified 2026-12-01)

Even with Grouped-Query Attention (GQA, 8 KV heads, head_dim = 128), an 8B model requires 16.0 GiB of FP16 KV cache at 128K context—more than 3x the size of its Q4 weights! Enabling Q8_0 KV cache quantization with Flash Attention cuts that 128K KV buffer to ~8.5 GiB with <0.15% perplexity drift.

KV Cache Buffer Size (GiB) by Context Length

Model Architecture (GQA Heads, d_head=128) 8K (FP16) 32K (FP16) 128K (FP16) 128K (Q8_0 KV)
8B Dense (32 Layers, 8 KV Heads) 1.0 GiB 4.0 GiB 16.0 GiB ~8.5 GiB
32B Dense (64 Layers, 8 KV Heads) 2.0 GiB 8.0 GiB 32.0 GiB ~17.0 GiB
70B Dense (80 Layers, 8 KV Heads) 2.5 GiB 10.0 GiB 40.0 GiB ~21.3 GiB
# Exact FP16 GQA KV Cache Formula:
# KV_GiB = (2 * n_layers * n_kv_heads * head_dim * 2_bytes * context_tokens) / 2^30

# Halve KV cache VRAM on modern llama-server without losing retrieval precision:
llama-server -m model-32b-q4_k_m.gguf -c 65536 -fa on -ctk q8_0 -ctv q8_0

Notice how concurrent slots multiply KV RAM linearly: setting --parallel 4 with -c 65536 splits into four 16K slots unless sized explicitly. Check your exact ceiling in the Local Memory Checker.

Frequently Asked Technical Questions

Does Ollama allocate the full 128K KV cache upfront?

Ollama defaults to a compact 2K–4K context window unless overridden via num_ctx in your Modelfile or API payload. Setting num_ctx: 65536 immediately allocates the full KV buffer in VRAM.

Is Q4_0 KV cache safe for code generation?

Q4_0 KV quantization saves 70%+ KV RAM, but community needle-in-a-haystack evaluations show occasional line-number and exact-identifier drift beyond 48K tokens. Stick to Q8_0 for coding and RAG.

Why did older non-GQA models need 4x to 8x more KV cache memory?

Multi-Head Attention (MHA) stored separate Key and Value tensors for every attention head (e.g., 64 heads), whereas GQA shares 8 KV heads across 64 query heads—slashing KV footprint by 8x.