Before the KV Cache Explodes: Trade-offs of Three Strategies for Reducing Attention Bottlenecks in Long-Context Inference
Run inference on Llama-3 8B with a 128K context, and the KV cache alone consumes roughly 16 GB of an A100 80 GB. The math is straightforward. With num_layers=32, num_kv_heads=8 (GQA), head_dim=128, and bf16 (2 bytes), the per-token KV cache is 32 × 8 × 128 × 2 × 2 = 131,072 bytes — 128 KB. At 128K tokens, that's 128K × 128 KB ≈ 16 GB. Scale the batch size to 4 and the KV cache alone hits 64 GB; add the model weights at ~16 GB and a single A100 80 GB is full.
FlashAttention-2 genuinely helps here. By reducing HBM↔SRAM I/O it removes the memory-bandwidth bottleneck and cuts TTFT severalfold at the same context length. But there is one thing FlashAttention does not touch — the O(n²) growth of attention FLOPs itself. Double the context length and FLOPs quadruple. Going from 32K to 128K is a 4× length increase and a 16× increase in prefill FLOPs. TTFT follows that same slope.
As 128K- and 1M-context models move into production serving, how to handle this O(n²) wall has become both an architecture decision and a direct lever on serving cost. There are three broad approaches: Sliding Window Attention, Sparse Attention, and Linear Attention. All three target the same bottleneck, but each gives up something different.
The Cost Structure of Full Attention
Attention cost breaks down along two axes: FLOPs and memory.
FLOPs are O(n²). During prefill the query–key matrix multiplication runs at n × n, which is the most direct driver of TTFT. In the decode phase each step attends a single query vector over the entire KV cache, so the FLOPs per step are O(n), but as n grows, the bandwidth cost of reading the KV cache out of HBM grows linearly too.
Memory: the KV cache grows O(n). On Llama-3 8B, that is ~4 GB at 32K context and ~16 GB at 128K. The bigger issue is how it multiplies with batch size. Higher throughput requires larger batches, but KV cache scales as batch × sequence length, which structurally caps batch size at long contexts.
Sliding Window Attention: Where the Locality Assumption Breaks
SWA, introduced in the Mistral 7B paper, limits each token's attention span to the preceding W tokens — W=4096 for Mistral 7B. The KV cache is bounded at O(W), so even with a 128K input the per-layer KV cache stays around 512 MB. From a throughput perspective this gives you predictable memory usage, which is a significant operational advantage.
Within a single layer, tokens beyond W cannot be attended to directly. But with 32 stacked layers the theoretical receptive field is W × k = 4096 × 32 = 131,072 tokens. Each layer's hidden state compresses and carries forward the local context from the layer below.
The core limitation is that this indirect propagation does not guarantee faithful information recovery. Consider a task that requires recalling a specific value at token position 1K from token position 128K — needle-in-a-haystack is the canonical example. Layer 1 cannot see both positions simultaneously (the gap is 127K). That information has to be relayed through intermediate layers, and it gets diluted in the aggregation.
The RULER benchmark quantifies this. At 128K context, Meta-Llama-3.1 8B (full attention) scores 81.3 on the RULER average, while SWA-based models show a meaningful accuracy drop at the same context length. RULER goes beyond simple needle-in-a-haystack and covers 13 tasks including multi-hop reasoning, aggregation, and QA. The gap widens on tasks that require combining clues scattered across the entire document. This loss cannot be compensated by quantization or batch scheduling — the attention mechanism simply never sees those tokens.
In vLLM, SWA is implemented in the v1 engine's KV cache manager via PR #14097. When a layer declares a sliding_window attribute, the manager creates a SlidingWindowSpec and automatically frees KV blocks that fall outside the window, reclaiming the memory.
Sparse Attention: The Gap Between Theoretical Complexity and GPU Reality
This is the approach taken by BigBird and Longformer. Instead of attending over all token pairs, they combine three patterns: a local window (neighboring tokens), global tokens (fixed positions like CLS that every token attends to), and random tokens (sampled per layer). Theoretical complexity is O(n × (w + g + r)), which approaches O(n) for sufficiently small w, g, and r.
Compared to SWA there is one structural advantage: global tokens provide an explicit long-range communication channel. Rather than relying on indirect propagation through a layer stack, global tokens communicate directly with the full sequence.
The problem is GPU implementation. GPUs are optimized for regular, dense matrix operations. Sparse attention patterns generate irregular memory access, and writing CUDA kernels that handle these patterns efficiently is structurally hard. Compared to dense matmul with cuBLAS-level optimization, sparse kernels often achieve equal or lower actual GPU throughput despite their lower theoretical complexity. This is why mainstream serving frameworks including vLLM do not ship sparse attention kernels out of the box.
Linear Attention: The Cost of O(1) Decode
This covers RWKV, RetNet, and the Mamba family. By approximating softmax attention with a kernel function and reformulating it as an RNN, decode memory complexity drops to O(1) — a fixed-size state vector. Decode cost does not increase as the sequence grows longer.
Empirically, this advantage is clear. According to Mamba benchmarks, beyond sequence lengths of 2K–4K, decode throughput reaches up to 5× that of a similarly sized Transformer. RWKV similarly demonstrates that decode latency remains constant regardless of sequence length.
There are two places where quality is lost.
First, prefill. Mamba's selective scan and RWKV's time-mixing are structurally sequential. Training uses parallelization tricks, but during inference prefill that sequential nature reduces GPU utilization. This contrasts with Full Attention + FlashAttention, which keeps GPUs densely utilized during prefill. Prefill TTFT can be worse at the same sequence length, and the crossover point typically falls in the 2K–4K token range — below that, Transformers have the advantage.
Second, recall accuracy. When a fixed-size state vector compresses the entire context, tasks that require precisely retrieving a value at a specific position take the hit, because exact positional information is diluted during state-space compression. Tracking a specific function name through a long code file, or precisely retrieving a specific item from conversation history, fall into this category.
Serving-Perspective Comparison
| Full Attention | Sliding Window | Sparse Attention | Linear Attention | |
|---|---|---|---|---|
| Prefill FLOPs | O(n²) | O(n × W) | O(n × (w+g+r)) | O(n) |
| KV cache | O(n) | O(W) fixed | O(n) (dense impl.) | O(1) fixed state |
| 128K KV memory (Llama-3 8B) | ~16 GB | ~512 MB | implementation-dependent | tens of MB or less |
| TTFT scaling | quadratic in n | stable at fixed W | below theoretical due to kernel inefficiency | GPU utilization degraded by sequential prefill |
| Decode throughput | degrades linearly with n | stable at fixed W | theoretically favorable, high variance in practice | n-independent — up to 5× advantage |
| Accuracy risk | none | long-range recall | pattern coverage gaps | broad recall-intensive tasks |
| Mainstream serving support | full | vLLM supported | limited | requires separate implementation |
Which One to Choose
The task and hardware budget determine the answer.
If you are running a RAG pipeline that assembles retrieved chunks into short contexts, or streaming document processing where local context drives the answer, SWA is cost-effective. You get predictable KV cache usage and it works out of the box in vLLM. This is why Mistral 7B is more cost-efficient than full-attention models for these workloads.
Long multi-hop reasoning or whole-codebase analysis is a different story. It is hard to know how much accuracy SWA or Linear Attention will sacrifice without benchmarking upfront, and once an accuracy regression is confirmed the only fix is changing the attention mechanism. For these cases, Full Attention + FlashAttention is the honest choice, with KV cache managed through vLLM's paging and offloading.
Recent models increasingly adopt hybrid architectures that keep full attention in some layers while replacing others with SWA or Linear Attention — a practical compromise that handles these trade-offs at the layer level. Which layers to keep as full attention remains empirical territory; no universal design principle has emerged yet.