Make sparse attention pick just enough tokens at runtime with top-p pruning to speed long-context LLMs

February 4, 20258 min

Overview

Decision SnapshotNeeds Validation

Results show clear runtime wins on GPU serving and offload scenarios; integration complexity exists but code and kernels are provided, so engineering adoption is practical with moderate effort.

Citations0

Evidence Strength0.80

Confidence0.85

Risk Signals11

Trust Signals

Findings with numeric evidence: 4/4

Findings with evidence refs: 4/4

Results with explicit delta: 1/6

Reproducibility

Status: Partial assets available

Open source: Yes

At A Glance

Cost impact: 70%

Production readiness: 70%

Novelty: 60%

Authors

Chaofan Lin, Jiaming Tang, Shuo Yang, Hanshuo Wang, Tian Tang, Boyu Tian, Ion Stoica, Song Han, Mingyu Gao

Links

Abstract / PDF / Code

Why It Matters For Business

Twilight reduces memory reads and latency for long-context serving, cutting compute cost and enabling larger context use-cases without retraining models.

Who Should Care

Summary TLDR

Twilight converts fixed-budget top-k sparse attention into an adaptive top-p (nucleus) pruning wrapper. It sits on top of existing token-selection methods, keeps a conservative candidate set, then prunes to the smallest set whose estimated attention mass exceeds p. On long- and medium-context benchmarks it prunes up to 98% redundant tokens while keeping accuracy nearly unchanged, gives up to 15.8× speedup on the self-attention operator versus dense FlashAttention2, and yields ~1.4× speedup versus the same sparse base method. It uses an INT4 paged K cache and GPU-friendly topp kernels and targets serving scenarios with large batch sizes or CPU offload.

Problem Statement

Most sparse attention systems pick a fixed number of tokens (top-k) to compute attention. Fixed budgets either over-select (waste memory/bandwidth) or under-select (hurt accuracy) because attention weight shapes vary across heads, layers, and queries. Picking a single k requires offline tuning per algorithm and model and fails at runtime dynamism.

Main Contribution

Propose using top-p (topp) pruning for attention: pick the minimum tokens whose estimated attention sum ≥ p.

Design Twilight, a select-then-prune wrapper that can be applied to existing sparse attention algorithms to make budget decisions adaptive.

Key Findings

Twilight can prune most redundant tokens after a conservative selection step.

NumbersPruned up to 98% of over-selected tokens

Practical UseIn serving, keep a conservative candidate set and apply topp to cut memory loads by up to ~25× for redundant tokens, reducing bandwidth and latency.

Evidence RefTable 2, Longbench results (Longchat)

Large operator speedups versus dense attention and gains over base sparse methods.

NumbersSelf-attention speedups: up to 15.8× vs FlashAttention2; 1.4× vs base sparse method

Practical UseReplace or wrap existing sparse attention kernels with Twilight to lower per-attention cost; expect biggest wins for long contexts and batch/offload scenarios.

Evidence RefFigure 7 and Section 5.2

Results

MetricValueBaselineDeltaSplit / DatasetEvidenceEvidence Ref
Max self-attention operator speedup15.8×FlashAttention2various sequence lengths and batch sizes (Figure 7)FlashInfer-Twi and Quest-Twi reach up to 6.5× and 15.8× vs FlashAttention2 (Section 5.2)Figure 7
Speedup over same base sparse method1.4×Quest or FlashInfer without Twilightself-attention operatorTwilight accelerates respective base algorithms by 2.4× (FlashInfer) and 1.4× (Quest) in operator-level tests (Section 5.2)Figure 7, Section 5.2

What To Try In 7 Days

Wrap an existing sparse attention implementation (Quest or FlashInfer) with the select-then-prune step and run end-to-end tests.

Calibrate topp threshold p on a small validation set (start at p≈0.85) and measure TPOT and perplexity.

Add an INT4 paged K cache and run SpGEMV kernels to measure bandwidth savings and latency.

Optimization Features

Token Efficiency
Head/group-wise varlen budgets via toppPrune to cumulative attention mass p
Infra Optimization
Avoids loading full KV for most tokensBetter gains when KV offload from CPU dominates
System Optimization
Paged INT4 cache layout to align with KV pagesLoad-balancing by flattening head dimensionKernel fusion for elementwise ops in topp
Inference Optimization
Adaptive top-p (topp) token pruningHierarchical select-then-prune pipelineINT4 paged K cache to reduce memory bandwidthGPU-friendly binary-search topp kernel and SpGEMV

Reproducibility

Code AvailableYes
Data AvailableNo
Open Source StatusYes
LicenseUnknown

Risks & Boundaries

Limitations

Twilight adds an estimation/pruner overhead that can offset gains for single-request or tiny-batch cases.

Head-wise dynamism conflicts with Group Query Attention (GQA), requiring group-level compromises.

When Not To Use

Low-latency single-request workloads where pruner overhead dominates.

Models or kernels that cannot provide per-head attention weight estimates.

Failure Modes

Estimation overhead outweighs sparse-attention savings on small batches.

Too-low quantization (≤2-bit) underestimates attention mass and breaks pruning.

Core Entities

Models

Longchat-7B-v1.5-32kLLaMA2-7B-ChatLLaMA-3.1-8B-Instruct

Metrics

self-attention speedupend-to-end decoding speedupperplexityaverage benchmark scoreTPOT (time per output token)

Datasets

LongbenchRULERGSM8KCOQAPG-19

Benchmarks

LongbenchRULERPG-19GSM8KCOQA