flashinfer.sparse.qsa_sparse_paged_attention

flashinfer.sparse.qsa_sparse_paged_attention(q: Tensor, k_cache: Tensor, v_cache: Tensor, logical_indices: Tensor, block_table: Tensor, token_to_req: Tensor, use_prefill_config: bool, out: Tensor | None = None, k_scale: float | None = None, v_scale: float | None = None, *, output_gate: Tensor, k_sf: Tensor | None = None, v_sf: Tensor | None = None, backend: str = 'auto') → Tensor

Gated sparse paged GQA attention over an NVFP4, FP8 or BF16 KV cache.

This is vLLM’s qsa_sparse_paged_attention (Qwen QSA) contract: the same arguments in the same order, plus the block scales k_sf / v_sf of an NVFP4 cache. The default implementation runs every cache format on SM80 through SM90. NVFP4 on 10.0, 10.3, 10.7 and 12.0 is served by the experimental Cake backend (see backend).

Parameters:
  • q (torch.Tensor) – BF16 queries [rows, num_qo_heads, 256]; num_qo_heads / num_kv_heads is 1 to 16.

  • k_cache (torch.Tensor) – [num_blocks, page_size, num_kv_heads, D] with D = 128 packed E2M1 bytes (uint8, NVFP4) or D = 256 float8_e4m3fn or bfloat16 values. Any strides with a contiguous last dim and 16-byte alignment, so NHD and HND (transposed) views both work.

  • v_cache (torch.Tensor) – [num_blocks, page_size, num_kv_heads, D] with D = 128 packed E2M1 bytes (uint8, NVFP4) or D = 256 float8_e4m3fn or bfloat16 values. Any strides with a contiguous last dim and 16-byte alignment, so NHD and HND (transposed) views both work.

  • logical_indices (torch.Tensor) – int32 [rows, selection_width + 1]: logical token indices of each row’s selection; the last column holds the row’s valid-entry count. Entries that are negative, past the block table, unmapped or past the cache are skipped (never read), and a row with no valid entry is zero.

  • block_table (torch.Tensor) – int32 [num_requests, max_blocks] mapping logical pages to cache blocks.

  • token_to_req (torch.Tensor) – int32 [rows] request of each row.

  • use_prefill_config (bool) – Accepted for signature compatibility; it does not change the result.

  • out (Optional[torch.Tensor]) – Optional BF16 output with the shape of q.

  • k_scale (float) – Global (NVFP4) or per-tensor (FP8) dequantization scales of K and V; required for those caches and ignored for BF16.

  • v_scale (float) – Global (NVFP4) or per-tensor (FP8) dequantization scales of K and V; required for those caches and ignored for BF16.

  • output_gate (torch.Tensor) – Contiguous BF16 gate with the shape of q; the output is bf16(attention) * sigmoid(output_gate), with the attention rounded to BF16 before the FP32 gate.

  • k_sf (Optional[torch.Tensor]) – NVFP4 only: E4M3 block scales [num_blocks, page_size, num_kv_heads, 16], one per 16 dims. Data and scales use the layout that nvfp4_quantize_append_paged_kv_cache_with_slot_mapping writes.

  • v_sf (Optional[torch.Tensor]) – NVFP4 only: E4M3 block scales [num_blocks, page_size, num_kv_heads, 16], one per 16 dims. Data and scales use the layout that nvfp4_quantize_append_paged_kv_cache_with_slot_mapping writes.

  • backend (str) – "default" runs the default implementation (compute capability 8.x and 9.0). "cake" runs the experimental Cake kernels of flashinfer.experimental.cake_qsa_nvfp4 (NVFP4 only, on the capabilities that package exports a library for: 12.0 on the mma.sync programs; 10.0, 10.3 and 10.7 on the tcgen05 programs) and emits an ExperimentalWarning once. "auto" (the default) selects "default" for every cache on 8.x and 9.0 and for FP8 and BF16 caches anywhere. For an NVFP4 cache on a capability the Cake package exports, "auto" selects "cake" only when FLASHINFER_ALLOW_EXPERIMENTAL_AUTO_BACKENDS=1 is set and raises RuntimeError otherwise; anywhere else it selects "default", which reports the unsupported device. The Cake kernels’ results agree with the default implementation within the test tolerances but are not bit-identical to it. Any other value raises ValueError.

Returns:

out when given, otherwise a new tensor.

Return type:

torch.Tensor