Local Inference & VRAM

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

layers
heads
heads
dim
tokens
streams
GiB
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 LengthFP16 KV (2B)FP8 KV (1B)Q4 KV (0.5B)Legacy MHA (No GQA)
4,096 tokens0.50 GiB0.25 GiB0.13 GiB2.00 GiB
16,384 tokens2.00 GiB1.00 GiB0.50 GiB8.00 GiB
32,768 tokens4.00 GiB2.00 GiB1.00 GiB16.00 GiB
65,536 tokens8.00 GiB4.00 GiB2.00 GiB32.00 GiB
131,072 tokens16.00 GiB8.00 GiB4.00 GiB64.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

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.

Related Engineering Calculators

ComputeSizer runs 100% locally in your browser with open formulas and zero account gates. If these VRAM and GPU cost tools saved your team money, support us via Fourthwall or sponsor an ad-free developer placement.