com.microsoft.SparsePagedAttention executes attention over externally selected
request-local indices. It does not perform selection or own indexer state. The
WebGPU implementation mirrors the numerics of the CUDA kernel documented in
cuda/sparse_paged_attention.md.
attention_mode |
selected_kv_source |
Reads |
|---|---|---|
selected_only |
main |
Selected logical positions resolved through the main block_table |
selected_only |
auxiliary |
Selected contiguous auxiliary rows |
local_plus_selected |
main |
Union of the main-cache local window and the selected main-cache positions, de-duplicated |
local_plus_selected |
auxiliary |
Main paged local window and selected contiguous auxiliary rows in one softmax |
Main-cache selection is causal when is_causal=1. Invalid, out-of-range,
non-causal, and unmapped selected positions are ignored rather than clamped, so a
caller may pad selected_indices with -1 freely.
selected_indices has shape [token_count, max_selected_entries];selected_counts has shape [token_count]. Indices are request-local and unused
entries are -1.
The contiguous auxiliary cache has shape[batch_size, auxiliary_capacity, kv_heads_or_one, head_dim], withauxiliary_lengths[batch_size] defining request boundaries. Whenauxiliary_kv_shared=1, auxiliary_key supplies both K and V andauxiliary_value must be absent. Auxiliary reads are never filtered byis_causal or by the main-cache length; only auxiliary_lengths and the
auxiliary capacity bound them.
The kernel is a bounded staged pipeline. Every stage is a separate compute pass,
and no stage truncates its candidate list.
PagedAttention kernel). Packed-QKVslot_mapping is present, a dedicated shaderPagedAttentionblock_table-derived scatter is reused. A negative slot_mapping entry, ori32 value isu32.token_meta[t] = (batch_id, query_position, main_length, auxiliary_length)cumulative_sequence_length, past_seqlens, andauxiliary_lengths. Values are sanitized here (negatives clamped, rangesmax_num_blocks_per_seq * block_size,u32i32(token, head) walks its candidate list.(accumulator[head_size], running_max, running_sum). Up to two partial states(max = sink, sum = 1, accumulator = 0). A token with no valid candidate andNaN.Selection data, block tables, and per-request lengths are read only by shaders.
The kernel never copies them to the host, so the op is usable under graph capture
and in continuous-batching runtimes. The only host-visible input is the optional
CPU attention_metadata tensor, which is declared OrtMemTypeCPUInput.
Cache outputs follow the WebGPU PagedAttention contract: key_cache_out andvalue_cache_out must be both present or both absent. When present but not
aliased onto the cache inputs by the allocation planner, the input caches are
copied into them first (with a warning) so untouched slots survive the run.
Configure IO-binding to alias the cache buffers in production.
| Feature | WebGPU support |
|---|---|
Activation (T) |
FP16 |
Main cache (T_CACHE) |
FP16 |
Auxiliary cache (T) |
Contiguous FP16 |
Index/length type (S) |
INT32 |
attention_mode |
selected_only, local_plus_selected |
selected_kv_source |
main, auxiliary |
auxiliary_cache_layout |
contiguous |
Auxiliary K=V (auxiliary_kv_shared=1) |
Supported |
slot_mapping, including -1 suppression |
Supported |
is_causal, local_window_size |
Supported |
softcap |
Supported |
RoPE (do_rotary, rotary_interleaved) |
Supported, rotary_offset must be 0 |
Attention sink (head_sink) |
Supported in the joint softmax |
| Device-resident selection (no readback) | Yes |
| Joint FP32 softmax across both sources | Yes |
Each of these is rejected with an explicit error; none silently changes numerics.
| Feature | Status |
|---|---|
| BFloat16 activations or cache | Not registered; fails kernel lookup |
INT8 main cache, k_quant_type/v_quant_type != NONE, k_scale/v_scale |
NOT_IMPLEMENTED |
| Session-level KV cache quantization | NOT_IMPLEMENTED |
QK-Norm (q_norm_weight/k_norm_weight) |
NOT_IMPLEMENTED |
rotary_offset != 0 |
NOT_IMPLEMENTED |
auxiliary_cache_layout other than contiguous |
Rejected when the kernel is created |
head_size > 512, or a head size whose workgroup storage exceeds the device limit |
NOT_IMPLEMENTED |
| Configurations needing more storage buffer bindings per stage than the device reports | NOT_IMPLEMENTED |
An optional (raw-indexed) binding larger than maxStorageBufferBindingSize |
NOT_IMPLEMENTED |
token_count * num_heads * (head_size + 2) floats and merge them inmaxStorageBufferBindingSize.maxStorageBufferBindingSize as several consecutive segments, so a largemaxStorageBuffersPerShaderStage, so any segmented buffer in that stage isselected_indices, selected_counts, auxiliary_value, auxiliary_lengths,head_sink) are read with raw indexing, which cannotcos_cache/sin_cache lengths are not validated against the largest KV(token, head) pair and iterates candidates