KV Cache & Long-Context Memory Trap Calculator
Measure how fast long contexts (32K–128K+) and concurrent batches consume GPU VRAM—and see exact memory saved by GQA and FP8 KV quantization.
Input Parameters
Calculated Output
Primary Result
—
Adjust inputs to recalculate immediately.
KV Cache Growth Across Context Lengths (32L, 8 KV Heads, Batch=1)
Comparison for an 8B-class GQA architecture (131 KB/token in FP16 vs 65 KB/token in FP8).
| Context Length | FP16 KV (2B) | FP8 KV (1B) | Q4 KV (0.5B) | Legacy MHA (No GQA) |
|---|---|---|---|---|
| 4,096 tokens | 0.50 GiB | 0.25 GiB | 0.13 GiB | 2.00 GiB |
| 16,384 tokens | 2.00 GiB | 1.00 GiB | 0.50 GiB | 8.00 GiB |
| 32,768 tokens | 4.00 GiB | 2.00 GiB | 1.00 GiB | 16.00 GiB |
| 65,536 tokens | 8.00 GiB | 4.00 GiB | 2.00 GiB | 32.00 GiB |
| 131,072 tokens | 16.00 GiB | 8.00 GiB | 4.00 GiB | 64.00 GiB |
Formula & Engineering Methodology
Every generated or ingested prompt token writes both Key and Value tensors across all layers and KV attention heads. Grouped-Query Attention (GQA) shares each KV head across q_heads / kv_heads query heads, cutting memory proportionally.
gqa_ratio = q_heads / kv_heads
kv_bytes_per_token = 2 * layers * kv_heads * head_dim * kv_precision
total_kv_gib = (kv_bytes_per_token * context_len * batch_size) / 1073741824
mha_equivalent_gib = total_kv_gib * gqa_ratio
Variable Definitions
2: Stores both Key (K) and Value (V) vectors per token per layer.kv_heads: Number of key-value heads (typically 4–8 in GQA vs 32–64 in legacy MHA).kv_precision: Bytes per scalar element (2 for FP16/BF16, 1 for FP8/Q8, 0.5 for Q4).
Practical Note: DeepSeek MLA (Multi-Head Latent Attention) compresses KV representations even further into low-rank latent vectors (~5–10x smaller than standard GQA).
Frequently Asked Questions
Why does 128K context trigger OOM on a 24GB GPU even for an 8B model?
At 128K context, an 8B model with 8 KV heads requires 16 GiB strictly for FP16 KV cache alone. Combined with 5–6 GiB of weights and CUDA buffers, total VRAM exceeds 24 GiB.
How does --kv-cache-dtype fp8 help concurrency?
Switching KV cache from FP16 to FP8 cuts per-token KV memory exactly in half, doubling either your maximum context window or concurrent batch size with minimal accuracy impact.
What is the difference between GQA and MHA?
Legacy Multi-Head Attention (MHA) assigns one KV head per query head (e.g., 32 KV heads). Grouped-Query Attention (GQA) groups multiple query heads onto shared KV heads (e.g., 8 KV heads), slashing memory bandwidth and capacity by 4x.