> ## Documentation Index
> Fetch the complete documentation index at: https://bench.flashinfer.ai/docs/llms.txt
> Use this file to discover all available pages before exploring further.

# DSA Paged

DeepSeek Sparse Attention (DSA) with paged memory layout. DSA is a two-stage sparse attention mechanism: first an indexer selects top-K relevant KV cache entries using ReLU scoring, then MLA-style attention is performed only on selected entries. This reduces attention computation from O(n) to O(k) where `k << n`.

Variants:

* indexer
* sparse\_attention

## indexer

Computes sparse attention scores using ReLU activation and learned weights, then selects top-K KV cache indices. Uses FP8 quantization with deep\_gemm format.

Axes (9 dimensions):

* `batch_size`, `max_num_pages`, `num_pages`: variable
* `num_index_heads`, `index_head_dim`, `page_size`, `topk`, `kv_cache_num_heads`, `head_dim_with_scale`: constant

Inputs (5 tensors):

* `q_index_fp8`: FP8 query for indexing \[batch\_size, num\_index\_heads, index\_head\_dim]
* `k_index_cache_fp8`: FP8 key index cache with scales \[num\_pages, page\_size, kv\_cache\_num\_heads, head\_dim\_with\_scale]
* `weights`: learned head weights \[batch\_size, num\_index\_heads]
* `seq_lens`: sequence lengths \[batch\_size]
* `block_table`: page mapping \[batch\_size, max\_num\_pages]

Outputs (1 tensor):

* `topk_indices`: selected token indices \[batch\_size, topk], -1 indicates padding

Constraints:

* `topk <= max_num_pages * page_size`
* `num_index_heads == 64`, `index_head_dim == 128` (deep\_gemm requirement)
* `head_dim_with_scale == 132` (128 + 4 scale bytes)

## sparse\_attention

Performs MLA-style attention on top-K selected KV entries. Works for both prefill (multiple tokens) and decode (one token per sequence) - the computation is identical, only the first dimension differs.

Axes (7 dimensions):

* `num_tokens`, `num_pages`: variable
* `num_qo_heads`, `head_dim_ckv`, `head_dim_kpe`, `page_size`, `topk`: constant

Inputs (5 tensors + 1 scalar):

* `q_nope`: query without positional encoding \[num\_tokens, num\_qo\_heads, head\_dim\_ckv]
* `q_pe`: query positional encoding \[num\_tokens, num\_qo\_heads, head\_dim\_kpe]
* `ckv_cache`: compressed KV cache \[num\_pages, page\_size, head\_dim\_ckv]
* `kpe_cache`: key positional encoding cache \[num\_pages, page\_size, head\_dim\_kpe]
* `sparse_indices`: top-K indices per token \[num\_tokens, topk], -1 indicates padding
* `sm_scale`: softmax scale (scalar)

Outputs (2 tensors):

* `output`: attention output \[num\_tokens, num\_qo\_heads, head\_dim\_ckv]
* `lse`: 2-based log-sum-exp \[num\_tokens, num\_qo\_heads]

Constraints:

* `sparse_indices.shape[0] == num_tokens`
* `sparse_indices.shape[-1] == topk`
* `ckv_cache.shape[1] == page_size`
