KV Cache Compression and Dynamic Pruning in Production Serving: Comparing StreamingLLM, H2O, SnapKV, PyramidKV, and DuoAttention Architecture, Memory Bandwidth Reduction, and Needle Retrieval Retention
As context windows in production large language models expand from 8K tokens to 128K tokens and beyond, the Key-Value (KV) cache replaces model weights as the primary consumer of GPU High Bandwidth Memory (HBM). In high-throughput serving systems, the KV cache grows linearly with sequence length and batch size, creating a dual bottleneck: capacity exhaustion that caps concurrent batch size, and memory bandwidth saturation during autoregressive decoding.
While quantization techniques (such as FP8, INT4, and vector quantization) compress individual KV entries, dynamic KV cache pruning and eviction algorithms modify the context structure itself by selectively retaining or discarding token states. Modern approaches, including StreamingLLM, H2O (Heavy Hitter Oracle), SnapKV, PyramidKV, and DuoAttention, leverage distinct empirical properties of transformer attention: attention sinks, cumulative power-law attention scores, observation-window clustering, layer-wise information funneling, and head-level functional specialization.
This article examines the mathematical foundations, system architectures, runtime kernel interactions, and empirical retrieval trade-offs of these five KV cache pruning strategies in production serving environments.
1. The KV Cache Memory and Bandwidth Wall
During autoregressive generation, each token generated requires reading the full history of cached keys and values for all preceding tokens across all attention layers. The total memory consumption (in bytes) for a standard transformer model is defined by:
Where:
- is the batch size.
- is the active context length (in tokens).
- is the number of transformer layers.
- is the number of key-value heads (e.g., in Multi-Head Attention, or in Grouped-Query Attention).
- is the hidden dimension per head ().
- is the storage precision in bytes (e.g., 2 for FP16/BF16, 1 for FP8).
The Dual Serving Bottlenecks
- HBM Capacity Exhaustion: For a 70B parameter model using Grouped-Query Attention (such as LLaMA-3-70B with , , , and bytes), a single sequence of 128,000 tokens consumes approximately 41.9 GB of GPU memory for the KV cache alone. A batch of 4 concurrent requests at 128K context requires over 167 GB of memory, exceeding the capacity of two 80 GB NVIDIA A100/H100 GPUs solely for intermediate KV tensors.
- Memory Bandwidth Saturation: During autoregressive decoding, generating each new token performs matrix-vector operations with an arithmetic intensity of less than 1 FLOP per byte transferred. The GPU memory controller must stream the entire accumulated KV cache from HBM to on-chip SRAM for every decoding step. Reducing the active token count in the cache directly decreases the memory traffic per step, yielding near-linear decoding speedups in memory-bandwidth-bound regimes.

2. Attention Sparsity Mechanics: Sinks, Heavy Hitters, and Funnels
Empirical analysis of transformer attention maps reveals that attention distribution across long contexts is neither uniform nor strictly recency-biased:
- Attention Sinks: As established by Xiao et al. (2023), the first few tokens in a sequence (the initial 1 to 4 tokens, typically including the start-of-sequence delimiter) receive disproportionately high attention scores regardless of their semantic relevance. This occurs because the softmax operator requires attention weights to sum to 1. When a query finds no relevant tokens in context, the model routes excess probability mass into the initial tokens. Evicting these initial tokens causes catastrophic attention collapse and unbounded perplexity spikes.
- Heavy Hitter Tokens (): Zhang et al. (2023) demonstrated that a small subset of tokens (often 5% to 20% of the sequence) accounts for more than 80% of the cumulative attention weight across generation steps. These "heavy hitters" correspond to structural anchors, punctuation, key entities, and instruction prefixes.
- Observation-Window Consistency: Li et al. (2024) discovered that attention heads establish their long-context focus patterns during the prefill phase. Specifically, the attention weights computed by the final tokens of the prompt (the "observation window") reliably predict which prompt tokens will be attended to during subsequent autoregressive generation.
- Pyramidal Information Funneling: Cai et al. (2024) identified that attention breadth varies systematically with transformer depth. Lower layers exhibit broad, diffuse attention distributions across the context to extract general syntactic relationships, while upper layers concentrate attention into sharp, localized semantic representations.
- Head Specialization: Xiao et al. (2024) proved that attention heads split into two primary functional categories: Retrieval Heads, which actively execute cross-context needle retrieval across the full sequence, and Streaming Heads, which only attend to local context and attention sinks.
3. Comparing Dynamic KV Pruning Architectures
StreamingLLM: Sink-Augmented Sliding Windows
StreamingLLM addresses the infinite-stream failure mode of naive sliding-window attention. When standard sliding-window attention drops the initial tokens, softmax normalizers destabilize, causing generation to degrade into repetitive loops.
StreamingLLM constructs an active KV cache consisting of two contiguous segments:
- Attention Sinks (): The first initial tokens of the sequence ().
- Rolling Window (): The most recent tokens ( to ).
At each decoding step , the oldest non-sink token is evicted from the cache. Positional embeddings (such as RoPE) are adjusted dynamically based on relative token positions within the cache rather than absolute sequence indices.
- Advantages: Zero decoding latency overhead; fixed memory footprint regardless of sequence length; no fine-tuning required.
- Limitations: Discards all intermediate context; fails entirely on long-context retrieval, document synthesis, and multi-hop reasoning tasks where query targets reside outside the local window.
H2O (Heavy Hitter Oracle): Cumulative Attention Eviction
H2O introduces dynamic tracking of token importance during autoregressive generation. At each generation step , the cumulative attention score for each cached token across all attention heads is updated:
Where is the attention weight assigned to token by head at generation step . When the cache size reaches capacity budget , H2O evicts tokens with the lowest cumulative attention scores while preserving the initial sink tokens and a local window .
def h2o_evict(kv_cache, cumulative_scores, capacity, sink_size, window_size):
seq_len = kv_cache.shape[2]
if seq_len <= capacity:
return kv_cache, cumulative_scores
# Protect sink tokens and local sliding window
candidate_mask = torch.ones(seq_len, dtype=torch.bool, device=kv_cache.device)
candidate_mask[:sink_size] = False
candidate_mask[-window_size:] = False
# Determine number of tokens to keep from candidate region
k_middle = capacity - sink_size - window_size
candidate_indices = torch.nonzero(candidate_mask).squeeze(1)
candidate_scores = cumulative_scores[candidate_indices]
# Select top-k heavy hitters
_, topk_relative_indices = torch.topk(candidate_scores, k=k_middle, largest=True)
kept_middle_indices = candidate_indices[topk_relative_indices]
# Construct final index set
final_indices = torch.cat([
torch.arange(sink_size, device=kv_cache.device),
kept_middle_indices.sort().values,
torch.arange(seq_len - window_size, seq_len, device=kv_cache.device)
])
return kv_cache[:, :, final_indices, :], cumulative_scores[final_indices]- Advantages: Retains contextually salient tokens dynamically as the conversation evolves; maintains high perplexity retention on general generation tasks.
- Limitations: Incurs sorting and bookkeeping overhead at every decoding step; uniform per-layer allocation does not account for layer depth; susceptible to evicting dormant needles that are only queried late in the generation phase.
SnapKV: Observation-Window Clustered Selection
SnapKV shifts compression from continuous decode-time eviction to a single prefill-time operation. SnapKV observes that when a prompt is processed, the final tokens (the observation window, typically to ) generate attention distributions that highlight the critical prefix tokens required for downstream generation.
To preserve semantic integrity and prevent fragmented token retention, SnapKV applies 1D max-pooling across the observation attention scores with kernel size (typically 5 to 7):
For each attention head independently, SnapKV selects the top- pooled positions, extracts the corresponding Keys and Values from the full prompt context, appends the attention sinks and the observation window, and permanently discards the remaining prompt KV pairs.
- Advantages: Per-head independent selection captures head-specific features; 1D pooling retains contiguous phrases rather than isolated punctuation; compression occurs once during prefill, leaving the autoregressive decoding loop unmodified and fast.
- Limitations: Compression decisions are fixed at prefill; cannot adapt if the generation trajectory diverges significantly from the initial prompt framing.
PyramidKV: Layer-Wise Pyramidal Allocation
PyramidKV builds upon the observation-window selection mechanism of SnapKV, but replaces the uniform per-layer cache budget with an adaptive pyramidal allocation profile.
In standard architectures, if the target average cache capacity per layer is , uniform methods assign for all layers . PyramidKV recognizes that lower transformer layers require broad historical receptive fields to build contextualized representations, whereas deeper layers operate on compressed semantic concepts and only attend to localized anchors.
PyramidKV distributes the total cache budget across layers using a decaying linear or geometric schedule:
Subject to the constraint:
On benchmarks such as LongBench and 128K Needle-in-a-Haystack tests, PyramidKV retains near 100% retrieval accuracy with an average cache size of only 12% ( tokens per head on 16K+ contexts), whereas uniform SnapKV and H2O experience accuracy degradation below 25% cache capacity.
DuoAttention: Head-Specialized Static Allocation
DuoAttention addresses the physical memory management inefficiency of per-token dynamic pruning. Rather than maintaining dynamic sparse index tables or variable-length KV buffers per sequence, DuoAttention classifies each attention head at deployment time into either a Retrieval Head or a Streaming Head.
- Identification Phase: Using a lightweight optimization-based distillation step on synthetic needle-retrieval and long-context datasets, DuoAttention assigns a learnable scalar to each head, penalizing attention outside the sink and local window. Heads where pruning causes loss degradation are designated as Retrieval Heads ( to of total heads).
- Execution Phase:
- Retrieval Heads: Allocate and maintain a full KV cache across the entire sequence length.
- Streaming Heads: Allocate a fixed-size circular buffer of size (e.g., tokens).
Because head categorization is static, memory allocation per layer is deterministic and requires no runtime sorting, scoring, or dynamic memory reallocations. DuoAttention achieves up to a 2.55x memory reduction for Multi-Head Attention models and 1.67x for Grouped-Query Attention models with 2.18x decoding speedups.
4. Comparative Architectural Analysis
StreamingLLM
- Primary Reference: Xiao et al. (2023)
- Compression Timing: Continuous (evicts at every autoregressive decoding step)
- Selection Unit: Temporal heuristic (initial attention sink tokens + rolling local window)
- Layer Budget Profile: Uniform across all transformer layers
- Typical KV Retention: Less than 5% of long sequences
- Needle Retrieval Retention: Near 0% for target tokens residing outside the local window
- Decoding Latency Overhead: Zero (fixed circular ring buffer)
- Serving Allocator Fit: High (direct ring buffer mapping without memory fragmentation)
H2O (Heavy Hitter Oracle)
- Primary Reference: Zhang et al. (2023)
- Compression Timing: Continuous (evicts at every autoregressive decoding step)
- Selection Unit: Token-level cumulative attention sum across all previous decoding steps
- Layer Budget Profile: Uniform across all transformer layers
- Typical KV Retention: 15% to 30% of total sequence length
- Needle Retrieval Retention: Moderate (40% to 70% retention depending on query timing)
- Decoding Latency Overhead: High (requires runtime top-k score updates and sorting per step)
- Serving Allocator Fit: Low (causes intra-block memory fragmentation in fixed-page allocators)
SnapKV
- Primary Reference: Li et al. (2024)
- Compression Timing: Prefill completion (one-shot compression before decoding begins)
- Selection Unit: Clustered tokens selected via 1D max-pooling over prompt observation window
- Layer Budget Profile: Uniform across all transformer layers
- Typical KV Retention: 10% to 25% of prompt context
- Needle Retrieval Retention: High (85% to 95% retention across LongBench tasks)
- Decoding Latency Overhead: Zero (compressed KV cache remains static during generation)
- Serving Allocator Fit: Medium (requires ragged-tensor kernel dispatch for variable head lengths)
PyramidKV
- Primary Reference: Cai et al. (2024)
- Compression Timing: Prefill completion (one-shot compression before decoding begins)
- Selection Unit: Clustered tokens selected via 1D max-pooling with depth-aware budgeting
- Layer Budget Profile: Pyramidal (large allocation at bottom layers, tapering down to top layers)
- Typical KV Retention: 8% to 15% average across the model
- Needle Retrieval Retention: Very High (95% to 100% on 128K Needle-in-a-Haystack benchmarks)
- Decoding Latency Overhead: Zero (static KV size during generation)
- Serving Allocator Fit: Medium (layer-wise non-uniform block allocation)
DuoAttention
- Primary Reference: Xiao et al. (2024)
- Compression Timing: Static configuration (offline head profiling prior to serving)
- Selection Unit: Head-level binary classification (Retrieval Heads vs. Streaming Heads)
- Layer Budget Profile: Layer-specific head allocation based on optimization distillation
- Typical KV Retention: 30% to 60% of full cache (architecture and GQA dependent)
- Needle Retrieval Retention: Near 100% across all evaluated long-context benchmarks
- Decoding Latency Overhead: Zero (eliminates runtime sorting and dynamic indexing)
- Serving Allocator Fit: High (static block allocation per head without memory fragmentation)
5. Systems Engineering Challenges in Production Serving
Integrating dynamic KV cache pruning into production LLM serving engines (such as vLLM, SGLang, and TensorRT-LLM) introduces systems-level challenges that do not appear in naive PyTorch implementations:
1. PagedAttention Block Fragmentation
Modern inference engines manage KV memory in fixed-size contiguous blocks (typically 16 or 32 tokens) to eliminate memory fragmentation, as formalized in vLLM's PagedAttention.
- The Mismatch: Fine-grained token eviction (as in H2O and SnapKV) creates sparse token masks within 16-token physical blocks. If an engine evicts 12 out of 16 tokens in a block, the remaining 4 tokens prevent the physical page from being returned to the free memory pool.
- Production Resolution: Eviction algorithms must operate at block granularity rather than individual token granularity, or execute a memory compaction kernel that coalesces surviving tokens into dense physical blocks.
2. Heterogeneous Sequence Lengths and Kernel Dispatch
In methods like SnapKV and PyramidKV, different attention heads and transformer layers retain different numbers of tokens. Standard fused attention kernels (such as FlashAttention-2) assume uniform sequence lengths across all heads within a layer.
- When head retains 512 tokens and head retains 128 tokens, standard batched matrix multiplication kernels cannot be dispatched directly.
- Serving engines utilize segmented reduction kernels and ragged-tensor indexed operators (such as those provided by FlashInfer) to execute non-uniform attention lookups without padding the shorter heads back to maximum length.
+-------------------------------------------------------------------------+
| PagedAttention Block Allocation |
| |
| Full Prompt: [Block 0] [Block 1] [Block 2] [Block 3] [Block 4]|
| (80 Tokens) (Tok 0-15) (Tok 16-31) (Tok 32-47) (Tok 48-63) (Tok 64-79)|
| | | | | |
| Pruning Engine: v (Sink) v (Evicted) v (Kept) v |
| +-------+ +-------+ +-------+ +-------+|
| Retained KV: |Block 0| ------------> |Block 2| ->|Block 3| ->|Block 4||
| Dense Mapping: [Tok 0-15] [Tok 32-47] [Tok 48-63] [Tok 64-79|
+-------------------------------------------------------------------------+3. GQA and KV-Head Sharing Constraints
Under Grouped-Query Attention (GQA), multiple Query heads (, typically 4 to 8) share a single Key-Value head.
- If a pruning algorithm makes independent eviction decisions for each Query head, it cannot prune the underlying physical KV head unless all associated Query heads agree to evict those tokens.
- SnapKV and DuoAttention resolve this by computing pooled importance metrics across all Query heads sharing the same KV head group, ensuring consistent eviction masks across the group.
6. Synthesis and Deployment Recommendations
For engineering teams deploying long-context models in production, the choice of KV compression depends on workload semantics and latency constraints:
- Unbounded Streaming and Chat Assistants: When requests involve indefinite conversational turns without explicit cross-turn factual retrieval requirements, StreamingLLM provides guaranteed memory bounds with zero computational overhead.
- Fixed-Prompt Long-Document RAG and Summarization: When serving large context prompts where decoding latency must remain minimal, PyramidKV delivers the highest compression ratio (8x to 12x reduction) while preserving full multi-hop retrieval and needle retention.
- High-Concurrency Multi-Tenant Serving Systems: When integration with PagedAttention and high batching density are the primary objectives, DuoAttention avoids dynamic block fragmentation and ragged-tensor kernel dispatch by statically allocating dedicated cache sizes to retrieval and streaming heads.
Sources
- StreamingLLM: Efficient Streaming Language Models with Attention Sinks (Xiao et al., 2023)
- H2O: Heavy-Hitter Oracle for Efficient Generative Inference of Large Language Models (Zhang et al., NeurIPS 2023)
- SnapKV: LLM Knows What You are Looking for Before Generation (Li et al., NeurIPS 2024)
- PyramidKV: Dynamic KV Cache Compression based on Pyramidal Information Funneling (Cai et al., 2024)
- DuoAttention: Efficient Long-Context LLM Inference with Retrieval and Streaming Heads (Xiao et al., 2024)
- vLLM: Efficient Memory Management for Large Language Model Serving with PagedAttention (Kwon et al., SOSP 2023)
- FlashInfer: High-Performance GPU Kernel Library for LLM Inference



