# SparseAttentionIndexer — Operator Documentation This document describes the `com.microsoft::SparseAttentionIndexer` contrib operator: the two indexer policies it implements, the schema and state contract, the CUDA kernel pipeline, and the known limitations and performance follow-ups. The operator answers a single question for every query token: *which* keys is the following attention operator allowed to read. It does not compute attention itself. Two policies are supported, selected by the `policy_mode` attribute: | `policy_mode` | Reference implementation | Selection granularity | |---|---|---| | `qsa` | `Qwen4ExpTextQSAIndexer` | token indices, grouped in blocks of `compress_ratio` | | `csa` | `DeepseekV4Indexer` / `DeepseekV4IndexerScorer` | compressed-entry indices | Source: [bert_defs.cc](../../../onnxruntime/core/graph/contrib_ops/bert_defs.cc) (schema), [sparse_attention_indexer_common.h](../../../onnxruntime/contrib_ops/cpu/sparse/sparse_attention_indexer_common.h) (slot map, capacity and window plan shared by the schema, the kernel and the tests), [sparse_attention_indexer.cc](../../../onnxruntime/contrib_ops/cuda/sparse/sparse_attention_indexer.cc), [sparse_attention_indexer_impl.cu](../../../onnxruntime/contrib_ops/cuda/sparse/sparse_attention_indexer_impl.cu). --- ## Table of Contents 1. [Overview](#1-overview) 2. [Operator Schema](#2-operator-schema) 3. [State Contract](#3-state-contract) 4. [Policy `qsa`](#4-policy-qsa) 5. [Policy `csa`](#5-policy-csa) 6. [Rotary Embeddings](#6-rotary-embeddings) 7. [CUDA Kernel Pipeline](#7-cuda-kernel-pipeline) 8. [Numerics and Determinism](#8-numerics-and-determinism) 9. [Validation Rules](#9-validation-rules) 10. [Testing](#10-testing) 11. [Known Limitations and Performance Follow-ups](#11-known-limitations-and-performance-follow-ups) --- ## 1. Overview Sparse-attention decoders run a small, cheap "indexer" attention next to the real attention. The indexer has its own low-rank query/key projections, its own RMSNorm and its own rotary embedding, and its only product is a set of indices. The real attention then reads just those keys. `SparseAttentionIndexer` implements that stage as one operator with three properties that matter for a runtime: - **Fixed output capacity.** `selected_indices` always has shape `(batch_size, sequence_length, capacity)` where `capacity` is a pure function of the attributes (`token_budget + compress_ratio - 1` for `qsa`, `index_topk` for `csa`). Entries that are not used are `-1`. No output size depends on tensor *data*, so the kernel never copies a count back to the host and never allocates from a device-computed size. - **Explicit, graph-visible state.** Everything that survives between calls — the concatenated indexer key cache, the compressed-key cache and the partial-window buffers — is an input/output pair. The operator caches nothing internally. - **A single versioned schema.** Both policies share one schema; the policy-specific inputs and outputs are optional slots at fixed indices and are validated strictly (see [§9](#9-validation-rules)). ## 2. Operator Schema ### Attributes | Attribute | Type | Required | Description | |---|---|---|---| | `policy_mode` | string | yes | Exactly `qsa` or `csa`. Any other value is rejected. | | `compress_ratio` | int | yes | Tokens folded into one block/entry. Must be `> 0`. | | `token_budget` | int | `qsa` only | Maximum number of tokens taken from complete blocks. Must be `> 0` and divisible by `compress_ratio`. Must be absent for `csa`. | | `index_topk` | int | `csa` only | Number of compressed entries selected per query. Must be `> 0`. Must be absent for `qsa`. | | `epsilon` | float | no | RMSNorm epsilon of queries and compressed keys. Default `1e-6`. | | `scale` | float | no | Scale of the per-head ReLU score. Default `1/sqrt(head_size)`. | | `head_weight_scale` | float | `csa` only | Scale applied to `head_weights`. Default `1/sqrt(num_heads)`. Must be absent for `qsa`. | ### Inputs `B` = batch size, `S` = sequence length, `N` = `num_heads`, `D` = `head_size`, `r` = `compress_ratio`, `P` = past sequence length, `T = P + S`, `R` = rotary width. | # | Name | Policy | Type | Shape | |---|---|---|---|---| | 0 | `query` | both | `T` | `(B, S, N*D)`; logically reshaped and normalized inside the operator | | 1 | `key` | both | `T` | `(B, S, D)` for `qsa`, `(B, S, 2D)` for `csa` | | 2 | `query_norm_weight` | both | `T` | `(D)` | | 3 | `key_norm_weight` | both | `T` | `(D)` | | 4 | `cos_cache` | both | `T` | `(max_rotary_sequence_length, R)` when shared across the batch, or `(B, max_rotary_sequence_length, R)` | | 5 | `sin_cache` | both | `T` | same as `cos_cache` | | 6 | `mask` | `qsa` | `TB` | INT64 padding mask `(B, T)` | | 7 | `past_key` | both | `T` | `(B, P, D)`; raw keys for `qsa`, compressed keys for `csa` | | 8 | `gate` | `csa` | `T` | `(B, S, 2D)` | | 9 | `position_bias` | `csa` | `T` | `(r, 2D)` | | 10 | `head_weights` | `csa` | `T` | `(B, S, N)` | | 11 | `position_ids` | `csa` | `I` | `(B, S)` | | 12 | `past_sequence_length` | both | `M` | `(1)`; optional valid length for a max-capacity `past_key` | | 13 | `past_proj_buffer` | `csa` | `T` | `(2, B, Lb, 2D)`, `Lb` in `[0, 2r)`; keys then gates | ### Outputs | # | Name | Policy | Type | Shape | |---|---|---|---|---| | 0 | `selected_indices` | both | `M` | `(B, S, capacity)` | | 1 | `present_key` | both | `T` | updated raw (`qsa`) or compressed (`csa`) key cache | | 2 | `present_proj_buffer` | `csa` | `T` | `(2, B, Lb', 2D)` | `W` is the number of complete windows closed by this call and `Lb'` the new buffer length; both follow from `Lb`, `S` and `r` alone (see [§5](#5-policy-csa)). ### Type constraints | Name | Allowed types | |---|---| | `T` | `tensor(float)`, `tensor(float16)`, `tensor(bfloat16)` | | `TB` | `tensor(int64)` | | `I` | `tensor(int64)` | | `M` | `tensor(int32)` | CUDA registers all three `T` types. WebGPU registers `float` and `float16`; see the [WebGPU implementation notes](../webgpu/sparse_attention_indexer.md). There is no CPU kernel; the header under `contrib_ops/cpu/sparse/` only holds the provider-neutral constants that the schema, kernels and tests must agree on. ### Output slot discipline A `qsa` node declares exactly 2 outputs (`selected_indices`, `present_key`). A `csa` node declares exactly 3, adding `present_proj_buffer`. Shape inference verifies the declared output count before touching any output, so `getOutputType` is never called past the declared range. ## 3. State Contract `SparseAttentionIndexer` is a pure function of its inputs. Every value it needs on the next call is returned as an output, so the caller (or the ORT session binding) owns the buffers: | Policy | State pair | |---|---| | `qsa` | `past_key` → `present_key` | | `csa` | `past_key` → `present_key` | | `csa` | `past_proj_buffer` → `present_proj_buffer` | By default, `present_key` grows by a number of positions that is known from the input shapes and attributes. For autoregressive decoding, the caller can instead provide a max-capacity cache and `past_sequence_length`, which gives the valid raw-key count for `qsa` or compressed-key count for `csa`. In this mode, the past and present tensors have the same shape and the CUDA allocation planner may alias them. The kernel appends new rows at the valid length without copying existing rows. The capacity must accommodate all rows emitted by the call. The packed `csa` projection buffer retains its ordinary bounded state contract and stays below `2r` positions. ## 4. Policy `qsa` For every `(b, s)`: 1. **Visible set.** `visible = [t for t in range(T) if mask[b, t] != 0 and t <= P + s]`, in ascending `t`. 2. **Complete blocks.** `nblocks = len(visible) // r`. Block `j` covers `visible[j*r : (j+1)*r]`. 3. **Pooled key.** `k_j = mean` of the `r` raw `present_key` rows of block `j`, then `RMSNorm(k_j) * key_norm_weight`, then rotary at absolute position `visible[j*r]`. 4. **Score.** Logically reshape each raw query row from `(N*D)` to `(N,D)`, apply `RMSNorm(q_h) * query_norm_weight`, and rotate it at absolute position `P + s`. Then `score_j = scale * sum_h ReLU(q_h · k_j)`. 5. **Selection.** `topk = min(token_budget // r, nblocks)` blocks, ordered by decreasing score. Their tokens are emitted in that block order, `r` token indices per block. 6. **Tail.** The `len(visible) % r` tokens of the trailing incomplete block, `visible[nblocks*r:]`, are appended unconditionally. 7. Remaining entries up to `capacity = token_budget + r - 1` are `-1`. The capacity is exactly `token_budget` selected tokens plus at most `r - 1` tail tokens. ## 5. Policy `csa` ### Window bookkeeping Let `ext = concat(past_proj_buffer[0], key)` along the sequence axis (and likewise use `past_proj_buffer[1]` for the gates). The buffer is split as ``` overlap = (Lb >= r) ? r : 0 # previous complete window, the "Ca" source leftover = Lb - overlap # tokens of the still-open window, always < r pending = leftover + S W = pending / r # complete windows closed by this call ``` Window `w` (`0 <= w < W`) covers `ext[overlap + w*r : overlap + (w+1)*r]`. Its `Ca` partner is the preceding `r` tokens, which exist iff `w >= 1 || overlap == r`. Because `leftover < r` always holds, a buffer length `>= r` unambiguously means "the previous complete window is present" — no extra state tensor is needed to disambiguate. The new buffer starts at `overlap + (W-1)*r` when `W > 0` (the last complete window followed by the new leftover) and at `0` otherwise, giving ``` Lb' = (W > 0) ? r + pending % r : Lb + S ``` `Lb'` is again in `[0, 2r)`. ### Compression For window `w`, a `2r`-slot pooling window is built from `[B, 2r, D]` values and `[B, 2r, D]` gates: - slots `[r, 2r)` take the **`Cb`** half (channels `[D, 2D)`) of the window's own `r` tokens; - slots `[0, r)` take the **`Ca`** half (channels `[0, D)`) of the preceding `r` tokens, or are masked out (value `0`, gate `-inf`) when that window does not exist. `position_bias[slot % r]` is added to the raw gate (the buffers store the **raw** projection, so the bias is re-applied at use time and never accumulates). A softmax over the `2r` slots is taken **per channel `d`**, the values are weighted and summed, the result is RMS-normalized with `key_norm_weight`, and finally rotated at absolute position `(Pc + w) * r`. If the softmax denominator is non-positive or non-finite (for example, all logits are `-inf` or any logit is `+inf`), the pooled value for that channel is zero. ### Scoring and selection ``` q_h = RoPE(RMSNorm(query[b, s, h, :]) * query_norm_weight) scores[b, s, e] = head_weight_scale * sum_h head_weights[b, s, h] * scale * ReLU(q_h · k_e) threshold[b, s] = (position_ids[b, s] + 1) / r # integer division scores[b, s, e] = -inf for e >= threshold[b, s] ``` The top `min(index_topk, Pc + W)` entries are emitted in decreasing score order; any emitted entry whose index is `>= threshold` (which only happens when fewer than `index_topk` entries are visible) is written as `-1`, and the remaining capacity is `-1`. ## 6. Rotary Embeddings Both policies reuse the model's precomputed `cos_cache` / `sin_cache`, indexed by absolute position. This keeps the exact rotary variant of each reference model without re-deriving frequencies inside the kernel: - **`qsa` (leading, split-half).** `R = cos_cache.shape[2]` channels are rotated, and the rotation is the split-half form `out[i] = x[i]*cos[i] - x[i + R/2]*sin[i]`, `out[i + R/2] = x[i + R/2]*cos[i + R/2] + x[i]*sin[i + R/2]`, applied to channels `[0, R)`. Channels `[R, D)` pass through. Feeding an MRoPE-expanded `cos_cache`/`sin_cache` therefore reproduces the model's MRoPE exactly, because the operator never recomputes the position→angle mapping. - **`csa` (trailing, interleaved).** `2R` channels are rotated and they are the **last** `2R` channels of the head. `cos`/`sin` are half-width, so entry `j >> 1` covers the channel pair `(j, j+1)`, and the rotation is the interleaved form `out[2i] = x[2i]*cos[i] - x[2i+1]*sin[i]`, `out[2i+1] = x[2i+1]*cos[i] + x[2i]*sin[i]`. Positions are clamped into `[0, max_rotary_sequence_length - 1]` inside the kernel, so an out-of-range `position_ids` value cannot read out of bounds. ### Norm weights and zero-centered gamma `query_norm_weight` and `key_norm_weight` are the **effective** multipliers: the kernel computes `normalized * norm_weight`. Qwen's `Qwen4ExpTextRMSNorm` multiplies by `1 + gamma`. Exporters targeting that model must fold the addition into both initializers (`norm_weight = 1 + gamma`). DeepSeek's `DeepseekV4RMSNorm` uses a plain `weight *`, so its tensor is passed through unchanged. ## 7. CUDA Kernel Pipeline All kernels use a fixed block of 128 threads (a power of two, required by the shared-memory block reductions). Elementwise kernels clamp the grid to 65535 blocks and use grid-stride loops; kernels that assign one block to each work item clamp the grid to the CUDA `gridDim.x` limit. ### `qsa` | Stage | Kernel | Parallelism | |---|---|---| | 1 | `CopyQsaPastKeyKernel` / `AppendQsaKeyKernel` | element; the copy is skipped for aliased fixed-capacity caches | | 2 | `RotateQueryKernel` | one block per `(b, s, h)` | | 3 | `InitializePrefixVisibleCountKernel` or mask analysis/compaction | omitted masks initialize prefix counts directly; irregular masks use one block per `(b, s)` | | 4 | `QsaBlockScoreQwenKernel` / `QsaBlockScoreKernel` | one block per `(b, s, block)`; the Qwen `D=128, r=4, N=4` path assigns one warp per query head | | 5 | `QsaPartialTopKKernel` / `QsaSelectKernel` | one block per `(b, s)`; score-threshold radix selection and bounded winner sorting | ### `csa` | Stage | Kernel | Parallelism | |---|---|---| | 1 | `CsaCopyPastCompressedKernel` | element | | 2 | `CsaCompressKernel` | one block per `(b, window)`; per-channel softmax over `2r` slots | | 3 | `CsaCopyBufferKernel` | element | | 4 | `RotateQueryKernel` | one block per `(b, s, h)` | | 5 | `CsaScoreKernel` | one thread per `(b, s, entry)` | | 6 | `CsaSelectKernel` | one block per `(b, s)`; iterated block arg-max | ### Workspaces | Policy | Buffer | Elements | |---|---|---| | `qsa` | float | `B*S*N*D` (rotated query) + `B*S*max_block_count` (block scores) | | `qsa` | int32 | optional `B*S*T` (arbitrary-mask visible indices) + `B*S` (visible counts) + up to `B*S*block_topk` (TopK indices) | | `csa` | float | `B*S*N*D` (rotated query) + `B*S*present_compressed_length` (scores) + `B*W*D` (new compressed keys before persistent-cache rounding) | Every size is derived from shapes and attributes only. ### Selection For QSA `block_topk` values from 33 through 512, `QsaPartialTopKKernel` finds the score threshold with four byte-wise radix-histogram passes, gathers exactly the winning candidates, resolves threshold ties by ascending index, and sorts only that bounded set. Selection storage and output traffic are therefore `O(block_topk)`, rather than `O(entries)`, while preserving deterministic "score descending, index ascending" ordering. Smaller QSA rows use the in-kernel block TopK; larger configured TopK values retain the repeated-scan fallback. ## 8. Numerics and Determinism - Pooling, softmax, RMS normalization, rotary and scoring are all done in `float32`; only the final store is rounded to the tensor element type. This is a deliberate deviation from the reference implementations, which for `qsa` run in the model dtype — the operator is strictly more accurate, never less. - The `2r`-slot softmax is a two-pass (max-subtracted) formulation. A non-positive or non-finite denominator falls back to a zero pooled value, so non-finite gate columns do not produce `NaN`. - Selection order is a total order, so the output is bitwise reproducible for a given input. - `float16` and `bfloat16` are supported for all `T` tensors. `bfloat16` uses the conversion helpers in `cu_inc/cuda_type_helper.cuh`, which are emulated on pre-`sm_80` devices, so no architecture guard is required. ## 9. Validation Rules Shape inference rejects policy, attribute, required-input/output, rank, and statically known state-layout errors. The CUDA kernel additionally validates concrete runtime dimensions and cross-input shape equality. Together they reject: - a `policy_mode` other than `qsa` / `csa`; - `compress_ratio <= 0`; - a `qsa` node that provides any `csa`-only input (slots 8–11 or 13) or attribute, and vice versa; - a missing required input for the active policy; - `token_budget` absent, `<= 0`, or not divisible by `compress_ratio` for `qsa`; - `index_topk` absent or `<= 0` for `csa`; - an output count other than 2 (`qsa`) or 3 (`csa`); - a `past_proj_buffer` length outside `[0, 2 * compress_ratio)`; - rank or dimension mismatches between `query`, `key`, the caches and the buffers. Rotary caches may be shared rank-2 tables or request-specific rank-3 tables. Schema and statically knowable errors fail at `Graph::Resolve()`; checks that require concrete runtime dimensions fail before any CUDA kernel is launched. ## 10. Testing [`onnxruntime/test/contrib_ops/sparse_attention_indexer_op_test.cc`](../../../onnxruntime/test/contrib_ops/sparse_attention_indexer_op_test.cc) contains: - **Shape-inference tests** that build a real `Model`, run `Graph::Resolve()` and assert the inferred element type and shape of every output for both policies, including the buffer-only `csa` case (`W == 0`). - **Negative tests** that assert the exact validation message for each rule in [§9](#9-validation-rules), including a `csa` node that declares too few outputs — the case that would otherwise write past the declared output range. - **Numeric tests** (`float`, `float16`, `bfloat16`) that run the CUDA kernel against a float reference implementation of both policies written directly from the reference semantics. Inputs are round-tripped through the tested element type before the reference runs, so the reference sees exactly the values the kernel reads. Shared rank-2 rotary caches are covered for both CUDA and WebGPU. These tests skip when the requested EP is unavailable. - **Long-context parity tests** at 8K, 32K, 64K, 128K and 256K for `compress_ratio=4, token_budget=2048`. - **Fixed-address CUDA graph capture and replay** in `onnxruntime/test/contrib_ops/cuda_kernels/sparse_attention_indexer_kernel_test.cc`. Run them with: ```bash ./build/Linux/Release/onnxruntime_provider_test --gtest_filter='SparseAttentionIndexer*' ``` Run the CUDA decode sweep against a built Python package with: ```bash python onnxruntime/python/tools/microbench/sparse_attention_indexer.py ``` ## 11. Known Limitations and Performance Follow-ups The implementation is correctness-first. The following are known and deliberate: 1. **Exact query-specific scoring remains `O(visible_tokens / compress_ratio)`.** Every decode query can change, so an exact implementation must inspect every eligible block; persistent candidates from an earlier query cannot preserve QSA semantics. The bounded radix path removes full-context sorting and candidate buffers, but not this scoring lower bound. 2. **Generic scoring is not tensor-core accelerated.** The common Qwen `D=128, r=4, N=4` path uses vectorized loads and one warp per query head. Other QSA shapes and `CsaScoreKernel` retain the generic shared-memory reductions. 3. **Rotated queries use an FP32 workspace.** Query normalization and rotation are computed once per `(b, s, h)` before block scoring instead of being recomputed for every candidate block. 4. **Irregular-mask analysis remains `O(T)` per query row.** Omitting the mask declares prefix-causal visibility and skips analysis and compaction. Padding masks with holes still require exact compaction because block membership is defined by visible rank, not position. 5. **Only the common Qwen dimensions are specialized.** Other `compress_ratio`, `head_size`, and head-count combinations retain dynamic loop bounds in the generic scoring kernel. 6. **No CPU kernel.** The operator is CUDA-only today.