TL;DR
CompactAttention is a novel attention mechanism designed to accelerate "chunked prefill" for long-context Large Language Models (LLMs). By decoupling block-level Key-Value (KV) selection from sparse kernel execution, it transforms 2D block-sparse masks into Grouped-Query Attention (GQA)-aware block tables. This allows highly efficient, zero-copy, in-place paged dense attention, delivering up to a 2.72× attention speedup at a 128K context length while maintaining near-dense accuracy.
What Problem Does This Paper Address?
As LLMs tackle longer documents and multi-turn agentic workflows, their context windows have expanded to hundreds of thousands of tokens. Processing these long sequences in a single "one-shot" prefill pass is computationally expensive and stalls subsequent decoding requests. To maintain interactive serving speeds, modern frameworks use chunked prefill, breaking long inputs into fixed-size chunks processed sequentially.
However, existing sparse attention methods fail to deliver real-world speedups in this chunked regime. When processing a small chunk against a massive accumulated history, the query length ($Q$) is much smaller than the KV length ($KV$). In this $Q \ll KV$ regime:
- Block-sparse kernels lose efficiency because there are too few query blocks to expose hardware parallelism, causing irregular memory access overheads to dominate.
- Pattern search overhead accumulates because searching for sparse attention patterns must be repeated at every single chunk over an ever-growing KV cache.
Token-level alternatives (like QUOKA) avoid sparse kernels but rely on query subsampling, which misses query-specific tokens and introduces heavy memory-copy overhead to gather selected tokens into contiguous buffers.
Methods
CompactAttention introduces Block-Union KV Selection, which separates how KV blocks are selected from how they are executed. Instead of running custom sparse kernels, it filters the KV cache down to a compact subset and executes standard, highly optimized dense paged attention.
The pipeline operates in two stages:
- Selection Stage: It reuses lightweight block-sparse selectors (such as SeerAttention or FlashPrefill) to generate a standard 2D block mask. It then applies Q-block union (aggregating across all query blocks in the current chunk) followed by intra-group union (aggregating across query heads within the same GQA group). This compresses the 2D mask into a minimal, 1D per-group KV block table that guarantees coverage for all queries in the chunk.
- Execution Stage: To avoid the costly overhead of physically copying selected KV tokens into a new buffer, CompactAttention stores the accumulated KV cache in a KV-head-major layout. This layout allows the selected blocks to be treated as virtual pages. The per-group block tables are passed as metadata directly to a paged dense-attention backend (FlashInfer), executing the attention computation completely in place.
Datasets
The authors evaluated CompactAttention on long-context tasks using two prominent open-source LLMs: LLaMA-3.1-8B-Instruct and Qwen3-30B-A3B-Instruct-2507. They utilized two primary benchmarks:
- RULER: A benchmark designed to evaluate effective context window behaviors across various retrieval, aggregation, and multi-hop needle-in-a-haystack tasks up to 128K/256K context lengths.
- LongBench V2: A dataset focused on complex, long-context QA tasks that demand deep reasoning and understanding over extensive context lengths.
Key Insights and Ideas
- Decoupling Selection from Execution: The core "aha" moment is realizing that 2D sparse masks shouldn't dictate the execution plan for custom sparse kernels when $Q \ll KV$. Instead, they should be treated as coarse selection signals to prune the KV cache for optimized dense execution.
- The Coverage vs. Granularity Trade-off: Token-level query subsampling drops critical query-specific information. By evaluating all query blocks at a coarser block granularity, CompactAttention ensures that query-specific KV entries are never missed, preserving performance on tasks requiring distributed information access.
- Layout Enables Zero-Copy: Shifting from a traditional sequence-major cache layout to a KV-head-major layout turns the KV cache into metadata-addressable pages. This simple architectural shift eliminates explicit data compaction entirely.
Key Results
- Substantial Speedups: Tested on an NVIDIA H200 GPU with LLaMA-3.1-8B-Instruct at a 128K context length, CompactAttention achieved up to a 2.72× attention speedup and a 1.96× end-to-end speedup over the dense baseline. It consistently outperformed block-sparse baselines like SeerAttention and XAttention, which often ran slower than dense attention due to search overheads.
- Maintained Accuracy: On the RULER benchmark, CompactAttention maintained an average accuracy (~82.3%) nearly identical to dense attention (82.78%). Meanwhile, token-level subsampling (QUOKA) degraded significantly to 77.98%, failing completely on tasks like Multi-key NIAH and CWE.
Limitations and Future Work
- Sparsity Loss from Unions: Because the Q-block and intra-group union operations retain any KV block selected by any query block or head in the group, the final executed sparsity is lower than the initial 2D mask sparsity. The authors mitigate this by using more aggressive initial pattern filtering, but this introduces hyperparameter sensitivity.
- Upstream Dependency: CompactAttention is bounded by the quality of its underlying block-sparse selector; if the initial selector misses a critical KV block, CompactAttention cannot recover it.
- Amortization Bounds: The framework is most effective when the accumulated KV cache is large enough to fully amortize the pattern search and metadata construction overheads. At shorter context lengths (e.g., <16K), the metadata management can diminish attention-level gains.
Summary updated