flashinfer.msa_ops.msa_sparse_attention¶
- flashinfer.msa_ops.msa_sparse_attention(q: Tensor, k: Tensor, v: Tensor, q2k_indices: Tensor, cu_seqlens_q: Tensor, cu_seqlens_k: Tensor | None = None, causal: bool = False, softmax_scale: float | None = None, page_table: Tensor | None = None, seqused_k: Tensor | None = None, return_softmax_lse: bool = False, k_scale: Tensor | None = None, v_scale: Tensor | None = None, k_global_scale: float | None = None, v_global_scale: float | None = None, q_offset=None, return_temperature_lse: bool = False, lse_temperature_scale: float = 1.0)¶
Minimax Sparse Attention forward (prefill) for SM120/SM121.
Each query attends only the top-K KV blocks selected in
q2k_indices. Query tokens are processed in tiles: each tile runs one online softmax over the union of the blocks its tokens selected, writing the final output directly. The union metadata is built internally fromq2k_indices.qis bf16/fp16;k/vare bf16/fp16, fp8 (E4M3), or packed NVFP4 (uint8 withk_scale/v_scale), in GQA or MHA layouts, flat or paged. Packed NVFP4 stores two values per byte, so its K/V last dimension ishead_dim // 2instead ofhead_dim.q2k_indicesis(num_kv_heads, total_q, topk)int32 (ascending,-1padded).- Parameters:
q (torch.Tensor) – Query tensor of shape
(total_q, num_qo_heads, head_dim)with dtype bf16/fp16.k (torch.Tensor) – Key tensor. For bf16/fp16/fp8, the flat shape is
(total_k, num_kv_heads, head_dim)and the paged shape is(num_pages, num_kv_heads, 128, head_dim). Packed NVFP4 useshead_dim // 2for the last dimension in both layouts.v (torch.Tensor) – Value tensor with the same layout conventions as
k.q2k_indices (torch.Tensor) – Int32 tensor of shape
(num_kv_heads, total_q, topk)containing the selected KV block indices for each query, sorted ascending and padded with-1.cu_seqlens_q (torch.Tensor) – Int32 cumulative query sequence lengths of shape
(batch_size + 1,).cu_seqlens_k (Optional[torch.Tensor], default=None) – Int32 cumulative KV sequence lengths of shape
(batch_size + 1,)for the dense path. May be omitted whenseqused_kis provided.causal (bool, default=False) – Whether to apply causal masking.
softmax_scale (Optional[float], default=None) – Softmax scale. Defaults to
head_dim**-0.5.page_table (Optional[torch.Tensor], default=None) – Enables the paged-KV path.
page_tablehas shape(batch_size, max_pages)and maps batch-local KV block indices to pages. Requiresseqused_k.k/vmay also be views split from a cache that packs K and V in one2 * head_dimcontent dim per token (seeSUPPORTS_PACKED_KV).seqused_k (Optional[torch.Tensor], default=None) – Int32 tensor of shape
(batch_size,)giving the valid KV length per sequence in the paged path.return_softmax_lse (bool, default=False) – Whether to also return the natural-log LSE with shape
(total_q, num_qo_heads).k_scale (Optional[torch.Tensor], default=None) – NVFP4 only: the
torch.uint8byte view (sf.view(torch.uint8)) of the E4M3 block-scale tensor produced byflashinfer.nvfp4_quantize()withsf_vec_size=16and the swizzled 128x4 layout (one scale per 16 elements, with rows padded to a multiple of 128). Scale rows follow(token, head)order for flat K and(page, head, token)order for paged K.v_scale (Optional[torch.Tensor], default=None) – NVFP4 block scales for V, with the same dtype, layout, and row-order contract as
k_scale.k_global_scale (Optional[float], default=None) – Global dequant scale for K; folds into the softmax scale (NVFP4 K only).
v_global_scale (Optional[float], default=None) – Global dequant scale applied to the output, for any KV dtype (e.g. an fp8 per-tensor V descale).
q_offset (optional) – Optional per-query offset tensor used by specific MSA workflows.
return_temperature_lse (bool, default=False) – Whether to also return the MSA temperature LSE. When enabled, the return value is
(out, lse, lse_t).lse_temperature_scale (float, default=1.0) – Scale applied to the exponent when computing temperature LSE.
- Returns:
outhas shape(total_q, num_qo_heads, head_dim). Ifreturn_temperature_lse=True, returns(out, lse, lse_t)regardless ofreturn_softmax_lse. Otherwise, settingreturn_softmax_lse=Truereturns(out, lse); when both flags are false, returnsout. Each returned LSE tensor has shape(total_q, num_qo_heads)and dtype float32.- Return type:
torch.Tensor or tuple of torch.Tensor