flashinfer.cute_dsl.sparse.bsa_attn_sm100_blk64.bsa_attn_sm100_blk64_fwd¶
- flashinfer.cute_dsl.sparse.bsa_attn_sm100_blk64.bsa_attn_sm100_blk64_fwd(q: Tensor, k: Tensor, v: Tensor, q2k_block_index: Tensor, block_sparse_num: int, block_sizes: Tensor | None = None, q2k_block_nums: Tensor | None = None, softmax_scale: float | None = None, return_lse: bool = False, out: Tensor | None = None, lse: Tensor | None = None, kv_splits: int | str = 1, use_clc: bool | None = None, q_scale: Tensor | None = None, k_scale: Tensor | None = None, v_scale: Tensor | None = None, backend: str = 'auto') Tuple[Tensor, Tensor | None]¶
Forward pass for BSA block-sparse attention using the blk64 CuTe-DSL kernel.
Supports SM100 and SM103. The CuTe-DSL backend is MHA only (num_kv_heads must equal num_heads); head_dim must be 128.
- Parameters:
q – Query tensor (batch, seqlen_q, num_heads, head_dim).
k – Key tensor (batch, seqlen_k, num_heads, head_dim).
v – Value tensor (batch, seqlen_k, num_heads, head_dim).
q2k_block_index – Block index tensor (batch, num_heads, num_q_blocks, max_kv_blocks), int32.
block_sparse_num – Number of KV blocks each Q block attends to (>= 1). Ignored when q2k_block_nums is provided.
block_sizes – Actual token count per KV block (num_kv_blocks,), int32. Pass None to skip per-block padding masking (assumes all blocks are full).
q2k_block_nums – Per-(batch, head, q_block) number of KV blocks to attend to, (batch, num_heads, num_q_blocks) int32. When None, uses fixed block_sparse_num.
softmax_scale – Softmax scale (default: 1/sqrt(head_dim)).
return_lse – Whether to return log-sum-exp.
out – Pre-allocated output tensor (batch, seqlen_q, num_heads, head_dim).
lse – Pre-allocated LSE tensor (batch, num_heads, seqlen_q).
kv_splits – Number of KV splits (“auto”, or an explicit int in [1, 256]). kv_splits=1 (or an “auto” resolution of 1) reproduces the original single-kernel behavior exactly.
use_clc – Optional scheduler override (True forces the CLC persistent scheduler, False forces the static scheduler, None uses the shape-based heuristic).
q_scale – Sage FP8 quantization scales. All three must be provided together to enable the FP8 path (q/k/v must then be float8_e4m3fn); otherwise all three must be None and q/k/v must be bfloat16. With
backend="cute"the FP8 path additionally requires batch_size == 1, num_head in (4, 8), q2k_block_nums is None, and block_sizes is None (upstream kernel limits, not specific to this integration).k_scale – Sage FP8 quantization scales. All three must be provided together to enable the FP8 path (q/k/v must then be float8_e4m3fn); otherwise all three must be None and q/k/v must be bfloat16. With
backend="cute"the FP8 path additionally requires batch_size == 1, num_head in (4, 8), q2k_block_nums is None, and block_sizes is None (upstream kernel limits, not specific to this integration).v_scale – Sage FP8 quantization scales. All three must be provided together to enable the FP8 path (q/k/v must then be float8_e4m3fn); otherwise all three must be None and q/k/v must be bfloat16. With
backend="cute"the FP8 path additionally requires batch_size == 1, num_head in (4, 8), q2k_block_nums is None, and block_sizes is None (upstream kernel limits, not specific to this integration).backend –
"auto"(default; earlier releases defaulted to"cute", which a caller can pass to keep that behavior) runs the Cake backend for a Sage FP8 call on SM100/SM103 that leaveskv_splits=1anduse_clc=None, outside torch fake mode, when the Cake backend accepts the call (its full contract: head_dim 128, dtypes and shapes of every tensor, GQA ratio, contiguous 16-byte-aligned tensors of at most2**31 - 1elements, …). Every other call, including one the Cake backend refuses, runs the CuTe-DSL kernel exactly as"cute"does (BF16, SM107, split-KV, scheduler overrides, strided or misaligned views, larger tensors)."cute"runs the CuTe-DSL kernel, unchanged."cake"runs the Cake-generated Sage-FP8 kernel (flashinfer.cute_dsl.sparse.bsa_sage_sm100_cake.bsa_attn_sm100_sage_fwd_cake()) which requires the FP8 path and lifts its limits: any batch size and head count, GQA, unaligned seqlen_q/seqlen_k, q2k_block_nums (empty rows allowed) and block_sizes; it raises on a call outside its contract.kv_splitsanduse_clcare ignored by the Cake backend. Under torch fake mode it checks the call and returns the allocated outputs without a launch, as the CuTe-DSL path does.
- Returns:
(out, lse) where lse is None if return_lse is False.