AttnEx: Sparse-Attention Indexing with Per-Layer Head Allocation and Streaming Top-k

Dongfang ZhaoarXiv 2026

TL;DR

Sparse attention speeds up long-context LLMs by having each query attend to only the top-$k$ most relevant keys—but finding those keys still requires a multi-head "indexer" that scores every preceding token, eating up 74% of attention latency at 128K tokens. AttnEx cuts this bottleneck down to size by giving each layer only as many indexer heads as its attention coverage actually needs (shrinking ~64 or 32 heads down to ~15 on average) and fusing scoring with a single-pass streaming top-$k$ kernel. The result: up to 1.52× faster time-to-first-token (TTFT) within native context windows and 1.76× at 1M tokens in vLLM, with no measurable drop in quality.

What Problem Does This Paper Address?

To handle prompts beyond 100K tokens, modern models like DeepSeek-V3.2 and GLM-5.1 use trainable sparse attention. Instead of computing full quadratic attention over all past tokens, a lightweight multi-head indexer in every layer scores all preceding keys and picks the top $k=2048$ for the main attention mechanism.

Once attention itself is sparse, however, the indexer becomes the new compute and memory bottleneck during prefill:

  1. Uniform head counts waste compute: Production models use the exact same number of indexer heads in every layer (64 in DeepSeek-V3.2, 32 in GLM-5.1), because changing them would mean repeating massive pretraining runs over nearly a trillion tokens.
  2. The score matrix hogs GPU memory: Standard kernels write the full FP32 score matrix to GPU memory before running a two-pass top-$k$ selection. To keep this matrix from crowding out the KV cache, serving engines like vLLM cap it at 512 MB—which forces long prompts to split into many small chunks that repeatedly re-read keys from memory.
Figure 1: Sparse-attention indexer latency and quality.

Methods

AttnEx tackles both bottlenecks without touching the frozen model weights or the FP8 key cache:

  • Coverage-Guided Per-Layer Head Allocation: AttnEx measures attention coverage—the share of attention probability mass captured by the selected top-$k$ keys. Offline, it retrains only the indexer's query projections and scalar head weights at five candidate head counts ($M = \{2, 4, 8, 16, 32\}$) using just 23 documents (~8 H100 hours, five orders of magnitude cheaper than original indexer training). A greedy max-heap allocator then assigns each layer the fewest heads needed to stay within a total coverage-loss budget ($B=0$ matches the original model's total coverage).
  • Single-Pass Streaming Top-$k$: Conventional top-$k$ kernels read every score row from GPU memory twice. AttnEx's streaming kernel maintains a running lower bound using a 2048-bin histogram in shared memory, dropping any score below the threshold bin on the fly and reading each row only once.
  • Modular vs. Fused Execution: For shorter prompts (<128K), AttnEx runs in modular mode, pairing DeepGEMM scoring with streaming top-$k$. For longer prompts, fused mode (sm90_fp8_mqa_topk) filters scores directly in the scoring kernel's epilogue, writing only surviving (key index, score) candidates to GPU memory and keeping the full score matrix out of memory entirely.
Figure 5: Streaming top-k on one streaming multiprocessor.

Datasets

  • Calibration (23 documents at 128K tokens): 7 PG-19 books and 16 packed LongAlign chats (20 for query retraining, 1 book for coverage curves, and 2 chats to test domain transfer).
  • Evaluation (8 benchmarks at 128K tokens, plus PG-19 up to 1M): LongBench v2, InfiniteBench MC, RULER (VT, CWE, FWE), HELMET ICL, InfiniteBench En.Sum, and PG-19 language modeling. No new benchmarks were introduced.

Key Insights and Ideas

  • Layers vary by over 10× in indexer capacity needs: Early layers in DeepSeek-V3.2 saturate with just 2–4 retrained heads, while deep layers keep gaining coverage up to 32 heads. A uniform head count simultaneously over-provisions early layers and starves deep ones.
  • Retraining is non-negotiable: Simply pruning original indexer heads by importance collapses coverage (losing 56.5% per layer at 8 heads on DeepSeek-V3.2), because the original heads rank keys jointly through a weighted sum. Retraining query projections keeps loss within 0.07%.
  • Book calibration transfers cleanly: Allocations calibrated on 128K-token PG-19 books transfer to multi-turn chat prompts and generalize to 160K–196K native context windows without recalibration.
Figure 3: Coverage loss and heads needed by layer.

Key Results

  • Quality Parity: At $B=0$, AttnEx reduces average indexer heads from 64 to 15.6 (DeepSeek-V3.2) and from 32 to 15.2 (GLM-5.1), with no statistically significant difference across all eight 128K-token benchmarks.
  • Kernel Efficiency: Fused mode cuts peak indexer GPU memory by up to 16× and runs up to 1.99× faster than modular mode at 8 heads. Streaming top-$k$ is ~2× faster than vLLM and SGLang top-$k$ kernels at 1M tokens.
  • End-to-End Serving (vLLM on 8× H200 GPUs):
  • Native Windows: TTFT improves by 1.52× on DeepSeek-V3.2 (160K) and 1.25× on GLM-5.1 (196K).
  • 1M-Token Prompts (512 MB budget): Fused mode boosts TTFT by 1.76× (DeepSeek-V3.2) and 1.28× (GLM-5.1), while preserving enough KV-cache memory for three concurrent 1M-token requests—finishing three DeepSeek-V3.2 requests 1.49× faster than the original indexer even when the original is given an 8192 MB budget.
  • Decoding: Time-per-output-token (TPOT) stays within 5% of the original models.

Limitations and Future Work

  • Hardware specificity: The custom fused CUDA kernels target NVIDIA Hopper GPUs (SM90 wgmma and TMA); a portable Triton path is provided for all head counts, though without the fused Hopper optimizations.
  • Decode padding: During decoding, allocated layers are still zero-padded to 32 heads to match DeepGEMM's paged decode kernel.
  • Buffer overflow fallback: If extreme score ties fill a row's fixed 12,288-entry candidate buffer, the kernel triggers an exact re-scan of that row.

Summary updated

BibTeX
@misc{zhao2026attnex,
  author = {Dongfang Zhao},
  title = {{AttnEx: Sparse-Attention Indexing with Per-Layer Head Allocation and Streaming Top-k}},
  year = {2026},
  eprint = {2612.01233},
  archivePrefix = {arXiv},
  url = {https://arxiv.org/abs/2612.01233},
}
Figure