ttnn.transformer.sparse_sdpa_msa
- ttnn.transformer.sparse_sdpa_msa(q: ttnn.Tensor, k: ttnn.Tensor, v: ttnn.Tensor, indices: ttnn.Tensor, *, scale: float | None, block_size: int, cache_batch_idx: int | None, chunk_start_idx: int | None, cluster_axis: int | None, block_cyclic_sp_axis: int | None, block_cyclic_chunk_local: int | None, enable_kv_block_cache: bool) ttnn.Tensor
-
MSA block-sparse prefill (MiniMax Sparse Attention), Blackhole single-chip. Attends the block_size-token K/V blocks named in indices; -1 (0xFFFFFFFF) sentinels mask a contiguous tail. Block selection bounds causality to block granularity; pass chunk_start_idx to additionally enforce a token-level causal mask on the diagonal block (required for correct causal prefill). RoPE and QK-norm are applied upstream.
- Parameters:
-
q (ttnn.Tensor)[1, H, S, d] bf16 | fp8_e4m3 ROW_MAJOR. H must be divisible by n_kv; H/n_kv may be 16 or a multiple of 32.
k (ttnn.Tensor)[B, n_kv, T, d] TILE bf16|bfloat8_b (B>1 only when indexed)
v (ttnn.Tensor)[B, n_kv, T, v_dim] TILE bf16|bfloat8_b
indices (ttnn.Tensor)[1, n_kv, S, TOPK] uint32 block-ids (-1 = sentinel, contiguous tail). Each row must contain a valid block, and valid block ids must be < T / block_size.
- Keyword Arguments:
-
scale (float, optional)defaults to d to the power of -0.5.
block_size (int)KV block size in tokens; defaults to 128. Must be a multiple of 32 and divide T.
compute_kernel_config (ttnn.DeviceComputeKernelConfig, optional) –
cache_batch_idx (int, optional)select the batch slot of a shared [B, n_kv, T, feature_dim] K/V cache. It is a dynamic runtime arg, so changing it (or T) does not recompile the kernels.
chunk_start_idx (int, optional)global position of query row 0. When set, enforces a token-level causal mask on the diagonal block (the query’s own block); toggling set/unset (None vs int) selects a different cached program. Requires bf16 q; fp8 q with this set is rejected.
cluster_axis (int, optional)SP mesh axis used to derive the per-device chunk_start under sequence parallelism. Host-side only. Without a block-cyclic cache: chunk_start_idx + rank*S. With one: the KV writer’s rotated per-device position, exact for a mid-slab (non-chunk-aligned) chunk_start_idx – which must then be a multiple of 32 – and the same geometry indexer_score_msa uses; a q also seq-sharded over the other mesh axis (block_cyclic_chunk_local == tp*S) takes its TP rank’s S-row slice. Must equal block_cyclic_sp_axis.
block_cyclic_sp_axis (int, optional)when set (with block_cyclic_chunk_local), the K/V cache is striped block-cyclic across SP on this mesh axis; the gather remaps each logical block id to its physical block in-kernel (invP), so no host reorder is needed. sp is read from the mesh.
block_cyclic_chunk_local (int, optional)per-shard chunk length (chunk_size_global / sp). Required iff block_cyclic_sp_axis is set; cross-checked against q (must equal q_isl or tp*q_isl).
enable_kv_block_cache (bool)per-core L1 cache for the gathered K/V blocks: a block selected again by a later query on the same core is read from L1 instead of DRAM; output is byte-identical either way. The slot count is sized per call from the free L1 (an estimate, not a reservation) and baked into the program, so a trace replays the count it captured unchecked. Default False.
- Returns:
-
ttnn.Tensor[1, H, S, v_dim] ROW-MAJOR, dtype = q.
Additional preconditions: d and v_dim must be multiples of 32; TOPK times 4 and output row bytes must meet device DRAM alignment.