Co-Designing Sparse Attention for Efficient Long-Context Inference

NVIDIA's developer blog published a co-design guide for sparse attention in long-context inference, aimed at model developers building attention that runs well on NVIDIA GPUs. The post focuses on model-level sparse attention, where sparsity is trained into the model, as distinct from drop-in post-training methods such as BLASST, MInference or Quest. Its measurements come from a production FP8 indexer kernel used to sweep design knobs: query heads, key heads, head dimension, compression ratio and speculative-decoding depth.
The motivation is context growth from tens of thousands of tokens to hundreds of thousands for agentic workloads, long documents and large codebases. Dense attention scores every query against every key, so cost per query grows with context length N; at 128K tokens it can take most of the prefill time in a layer. Sparse attention limits each query to a small subset of keys, typically a few thousand, which stops core attention scaling with context. But the indexer that finds those keys still scores all N of them, so the cost moves rather than disappears. In the post's own figure, the indexer and Top-k account for 47% of the DeepSeek V3.2 prefill layer at a 128K input sequence length, compared with dense MLA in DeepSeek-R1.
Sparse attention splits into three stages: the indexer scores every key, Top-k keeps the k highest, and core attention runs softmax attention over the selected keys and values. The last two scale with k; the indexer scales with N. Treating the indexer as a batched matmul with a fused epilogue, the post derives arithmetic intensity for prefill and decode. Two results drive much of the guidance: sequence length and compression ratio scale total work but not the FLOP-to-byte ratio, and both intensities are proportional to the group size G = QH/KH. Designs without a cross-head reduction, such as MSA and MoBA, lose that factor.
On query heads, prefill is compute-bound so more heads cost proportionally more time, while decode is memory-bound because reading the key cache dominates. Decode runtime moved only 2% while G grew 16x. Prefill grew more slowly than G at small sizes (doubling G from 8 to 16 cost 1.66x, not 2x) because the FP32 logit write dominates there; above about G = 16 the matmul takes over. Speculative decoding adds a second lever: D+1 query tokens scored per step against the same key cache, so drafts are nearly free while they fit in one pass. One Tensor Core tile holds up to 256 query rows, but FP32 epilogue register pressure limits it to about 200, meaning G x (D+1) up to roughly 200: six query tokens at G = 32, 26 at G = 8. Within that, runtime rose 1.37x and 1.87x while work grew 6x and 26x; beyond it, extra CTAs cause a step up. An FP16 epilogue widens the tile to 256 rows and gave 1.31x at G = 64, D+1 = 4; FP16 accumulation reached 1.45x if QK scaling keeps outputs in range.
Increasing key heads at fixed QH shrinks G. In decode each extra key head means another full read of the key cache and another set of logits, and runtime grew 7.6x from KH = 1 to 8. Prefill was less sensitive at 1.3x to 1.6x. On head dimension, prefill is partly epilogue-bound on CUDA cores at small Hsz, so going from 64 to 128 cost only 1.1x to 1.2x; beyond 128 runtime follows FLOPs. Decode grows with Hsz throughout since every extra key byte is read from HBM. On compression ratio, where one indexer key stands for four tokens at CR = 4, prefill doubled with every halving of CR, and decode doubled too except at the smallest 8K-key cache (1.8x).
Top-k runs on CUDA cores. Each doubling of keys scored cost about 1.7x in prefill and 1.5x in decode, while 64x more k raised Top-k time under 2x in prefill and 1.7x in decode. NVIDIA's Guess-Verify-Refine Top-k in TensorRT LLM reuses the previous decode step's selection as a guess, reported 2.1x to 2.4x faster than standard radix select on DeepSeek V3.2 and DeepSeek V4 decode. Fusing Top-k into the indexer avoids writing and reading back logits; FlashInfer's selective Top-k path is cited as an open-source example. On selection granularity, token-level selection scatters fetches across the KV cache, while block-level selection fetches contiguous blocks in bulk and shares them across queries that pick the same block. At a 2,048-key budget, halving block size nearly doubled core attention runtime in prefill and decode because each block adds a query load, a partial result and a merge.
The eight guidelines: keep G x (D+1) within one Tensor Core tile of about 200 rows (256 with an FP16 epilogue); run the epilogue in FP16 and accumulate in FP16 when scaling allows; use KH = 1 with cross-head reduction so one logit per key is written; use Hsz of 128 or 256, with most designs settling at 128; cut the keys scored through compression, hierarchical indexing or index sharing; treat Top-k as part of the indexer budget, fusing it and reusing the previous step's result in decode; prefer block-level selection with large blocks; and use a large core attention group size so many query heads share each KV head.
This suggests the practical message is that sparse attention shifts the long-context bottleneck rather than removing it, so teams training sparse models should budget indexer and Top-k cost together. A likely trade-off is that several guidelines compete for the same hardware resources: speculation depth and query-head group size both consume Tensor Core tile rows, and narrower heads save decode bytes but starve the prefill matmul. The suggested tools are TensorRT LLM for the fused Top-k implementation, FlashInfer's selective Top-k path, and the Guess-Verify-Refine Top-k paper.
Why it matters: teams serving models at 128K tokens and beyond should expect indexing and selection, not core attention, to set latency and cost, and design choices made at training time constrain how efficiently kernels can run later. The eight guidelines give concrete defaults for head counts, head dimension and selection granularity that map directly onto GPU tile limits. Inference engineers evaluating DeepSeek-style sparse models, or training their own, may find the hardest constraint is keeping speculation depth and group size inside one tile, which in practice caps how much drafting can be folded into a single decode step (an inference drawn from the reported measurements, not a stated conclusion).
Based on reporting from the original publisher. Visit the source for full context and later updates.
Publisher excerpt
Agentic workloads, long documents, and large codebases are pushing context lengths from tens of thousands of tokens to hundreds of thousands. At those lengths,...