Abstract
Rapid advances in Large Language Models (LLMs) have spurred demand for processing extended context sequences in contemporary applications. However, this progress faces two challenges: performance degradation due to sequence lengths out-of-distribution, and excessively long inference times caused by the quadratic computational complexity of attention. These issues limit LLMs in long-context scenarios. In this paper, we propose Dynamic Token-Level KV Cache Selection (TokenSelect), a training-free method for efficient and accurate long-context inference. TokenSelect builds upon the observation of non-contiguous attention sparsity, using QK dot products to measure per-head KV Cache criticality at token-level. By per-head soft voting mechanism, TokenSelect selectively involves a few critical KV cache tokens in attention calculation without sacrificing accuracy. To further accelerate TokenSelect, we design the Selection Cache based on observations of consecutive Query similarity and implemented the efficient Paged Dot Product Kernel, significantly reducing the selection overhead. A comprehensive evaluation of TokenSelect demonstrates up to 23.84 speedup in attention computation and up to 2.28 acceleration in end-to-end latency, while providing superior performance compared to state-of-the-art long-context inference methods.
Introduction: The Long-Context Challenge
As Large Language Models (LLMs) are increasingly tasked with processing entire books or massive codebases, they encounter two significant technical hurdles:
- The Quadratic Bottleneck: Standard attention mechanisms scale quadratically ($O(N^2)$) with sequence length. As $N$ grows, the computational and memory requirements for attention become prohibitive.
- Length Extrapolation Failure: Most LLMs are trained on fixed context windows (e.g., 8k tokens). Performance often collapses when processing 100k or 1M tokens because the model encounters positional distributions outside its training data.
Existing sparse attention methods often rely on predefined patterns (e.g., sliding windows) or coarse block-level selection. However, empirical observation reveals that attention is non-contiguous; the most critical tokens are often scattered sporadically throughout the context. Coarse selection misses these "needles in the haystack," leading to significant accuracy drops.
Methodology: TokenSelect
TokenSelect moves from coarse block-level selection to precise token-level selection. It identifies and retains only the most critical individual tokens for the attention calculation. The system consists of three core innovations:
Head Soft Vote
Attention heads vary significantly in their scaling and focus. A global top-K selection might be dominated by a few heads with large activation values. TokenSelect employs a "soft voting" mechanism: each head's preferences are normalized via softmax and then aggregated. This ensures a balanced representation of token importance across all heads, preserving the model's internal logic.
Selection Cache
LLM generation is auto-regressive, producing one token at a time. The authors observed that consecutive queries exhibit extremely high cosine similarity across various datasets.
By caching selection results and reusing them when a new query is sufficiently similar to the previous one, TokenSelect drastically reduces the overhead of the selection process.
Paged Dot Product Kernel
To ensure practical efficiency, the authors implemented a custom Triton kernel. This kernel operates directly on "paged" memory (where the KV cache is stored in non-contiguous chunks), avoiding expensive memory re-ordering operations during the selection phase.
Evaluation and Results
The researchers evaluated TokenSelect on major long-context benchmarks including InfiniteBench (avg. >200k tokens), RULER (up to 128k), and LongBench. Tests were conducted on popular models such as Qwen2-7B/72B, Llama-3-8B, and Yi-1.5-6B.
Performance Highlights
- Computational Speedup: On a 1-million-token context, TokenSelect achieved a 23.84× speedup in attention computation compared to FlashInfer and a 2.28× faster end-to-end latency.
- Accuracy and Recall: On InfiniteBench's Key-Value retrieval task (
R.KV), TokenSelect achieved 86.6% accuracy using a minimal token budget (~2.5k tokens), while other sparse methods like StreamingLLM or InfLLM fell below 6%. - Effective Extrapolation: TokenSelect enabled a Llama-3-8B model (trained for 8k context) to maintain high performance on tasks involving up to 2 million tokens without any additional fine-tuning.
Conclusion
TokenSelect demonstrates that fine-grained, token-level sparsity is key to efficient long-context inference. By exploiting the non-contiguous nature of attention and the temporal locality of queries, it provides a training-free path to scaling LLMs to million-token contexts with minimal accuracy loss and significant hardware acceleration.
Summary updated