TokenSelect: Efficient Long-Context Inference and Length Extrapolation for LLMs via Dynamic Token-Level KV Cache Selection

Wei Wu, Zhuoshi Pan, Chao Wang, Liyi Chen, Yunchu Bai, Kun Fu, Zheng Wang, Hui Xiong · DOI

Introduction

As Large Language Models (LLMs) are increasingly applied to massive contexts like entire books or codebases, they face two primary bottlenecks: the Quadratic Tax, where attention complexity grows quadratically ($O(N^2)$) with sequence length, and Length Extrapolation Failure, where performance collapses on sequences longer than those seen during training.

TokenSelect is a training-free method designed to overcome these hurdles. By shifting from coarse "block-level" cache selection to a fine-grained "token-level" approach, it enables LLMs to handle contexts up to 2 million tokens with massive efficiency gains, achieving up to a 23.84× speedup in attention computation and 2.28× faster end-to-end latency while maintaining high accuracy.

The Challenge of Long-Context Inference

Existing solutions for long-context efficiency often rely on "sparse attention" or "KV cache eviction" operating at a block level (grouping tokens into chunks). However, critical information is often scattered non-contiguously across the context. As shown in the comparison below, block-level methods can accidentally discard "needle-in-a-haystack" tokens or include excessive noise, leading to poor recall of important information.

Comparison of attention selection strategies: Token-level selection achieves 100% recall compared to block-level and predefined patterns.

Methods

TokenSelect introduces three primary innovations to make fine-grained token selection both accurate and computationally viable:

1. Head Soft Vote Selection

LLM attention heads are "head-distinctive"—some heads produce much larger logit values than others. A simple global top-k selection would be dominated by a few "loud" heads. TokenSelect instead calculates criticality per-head, normalizes them via softmax, and then sums these scores across all heads. This ensures every head's "vote" on which tokens are important is fairly weighted.

2. Selection Cache

The authors observed that consecutive queries in a conversation or document processing task are often highly similar, with cosine similarity typically exceeding 0.9. TokenSelect leverages this by caching selection results; if the next query is similar enough to the previous one, it reuses the same KV cache tokens, skipping the expensive selection math.

Empirical evidence of high query similarity and overlap rate across various datasets, supporting the Selection Cache mechanism.

3. Paged Dot Product Kernel

Selecting individual tokens from non-contiguous memory is usually an I/O bottleneck for GPUs. The authors developed a custom Triton kernel that performs the Query-Key dot products directly on "paged" memory, avoiding the need to move or reformat the KV cache before selection.

Key Results

The authors evaluated TokenSelect across major long-context benchmarks including InfiniteBench, RULER, and LongBench using models like Qwen2-7B and Llama-3-8B.

  • Efficiency: TokenSelect achieved a 23.84× speedup in attention kernels compared to FlashInfer and a 2.28× speedup in end-to-end latency over state-of-the-art methods like InfLLM.
  • Accuracy: On InfiniteBench, TokenSelect (using a budget of only ~2.5k tokens) outperformed original models and other sparse methods. For example, on the R.KV (Key-Value retrieval) task, it achieved 86.60% accuracy where StreamingLLM and InfLLM struggled significantly.
  • Extrapolation: It successfully extended models to 2 million tokens while maintaining high accuracy on retrieval tasks, far exceeding the models' original training windows.

Limitations and Future Work

As a training-free method, TokenSelect's performance is inherently tied to the base model's quality. While it significantly reduces the computational load of attention, processing million-token contexts still requires substantial VRAM to store the full KV cache. Future work may involve combining TokenSelect with long-context post-training techniques to further reduce token budgets without accuracy loss.

Summary updated

BibTeX
@misc{wu2024tokenselect,
  author = {Wei Wu and Zhuoshi Pan and Chao Wang and Liyi Chen and Yunchu Bai and Kun Fu and Zheng Wang and Hui Xiong},
  title = {{TokenSelect: Efficient Long-Context Inference and Length Extrapolation for LLMs via Dynamic Token-Level KV Cache Selection}},
  year = {2024},
  eprint = {2411.02886},
  archivePrefix = {arXiv},
  url = {https://arxiv.org/abs/2411.02886},
}
Figure