For the complete documentation index, see llms.txt. Markdown versions of all pages are available by appending .md to any URL (e.g. /get-started.md).
Mojo function
mha_decoding_single_batch
def mha_decoding_single_batch[q_type: DType, k_t: MHAOperand, v_t: MHAOperand, output_type: DType, mask_t: MHAMask, *, BM: Int, BN: Int, BK: Int, WM: Int, WN: Int, depth: Int, num_heads: Int, num_threads: Int, num_pipeline_stages: Int, group: Int = Int(1), decoding_warp_split_k: Bool = False, sink: Bool = False](q_ptr: Pointer[Scalar[q_type], ImmutAnyOrigin], k: k_t, v: v_t, output_ptr: Pointer[Scalar[output_type], MutAnyOrigin], exp_sum_ptr: Pointer[Scalar[get_accum_type[q_type]()], MutAnyOrigin], qk_max_ptr: Pointer[Scalar[get_accum_type[q_type]()], MutAnyOrigin], scale: Float32, num_keys: Int, num_partitions: Int, mask: mask_t, batch_idx: Int, sink_weights: OptionalReg[LayoutTensor[q_type, Layout.row_major(Int(-1)), ImmutAnyOrigin]])
Flash attention v2 algorithm.
Parameters:
- q_type (
DType): Element type of the query tensor. - k_t (
MHAOperand): Key operand type (KV cache or dense tensor). - v_t (
MHAOperand): Value operand type (KV cache or dense tensor). - output_type (
DType): Element type of the output tensor. - mask_t (
MHAMask): Attention mask type implementingMHAMask. - BM (
Int): Number of query rows per thread block. - BN (
Int): Number of key columns per thread block. - BK (
Int): Tile size in the depth dimension for shared-memory tiles. - WM (
Int): Warp tile height in the query (M) dimension. - WN (
Int): Warp tile width in the key (N) dimension. - depth (
Int): Attention head depth (key/value dimension per head). - num_heads (
Int): Total number of query heads. - num_threads (
Int): Number of threads per thread block. - num_pipeline_stages (
Int): Number of software-pipeline stages for async copies. - group (
Int): GQA group size, query heads per key/value head (defaults to 1). - decoding_warp_split_k (
Bool): Enable warp-level split-K reduction (defaults toFalse). - sink (
Bool): Enable attention-sink mode where the first tokens always attend (defaults toFalse).
Args:
- q_ptr (
Pointer[Scalar[q_type], ImmutAnyOrigin]): Pointer to the query tensor in global memory. - k (
k_t): Key operand backed by a KV cache or dense tensor. - v (
v_t): Value operand backed by a KV cache or dense tensor. - output_ptr (
Pointer[Scalar[output_type], MutAnyOrigin]): Pointer to the output tensor in global memory. - exp_sum_ptr (
Pointer[Scalar[get_accum_type[q_type]()], MutAnyOrigin]): Pointer to the per-head online-softmax denominator (sum of exponentials) for cross-partition reduction. - qk_max_ptr (
Pointer[Scalar[get_accum_type[q_type]()], MutAnyOrigin]): Pointer to the per-head online-softmax running maximum for cross-partition reduction. - scale (
Float32): Softmax temperature scale applied to Q·Kᵀ. - num_keys (
Int): Number of valid key/value entries (cache length). - num_partitions (
Int): Number of split-K partitions along the key dimension. - mask (
mask_t): Mask instance used to apply the attention mask. - batch_idx (
Int): Index of the sequence within the batch. - sink_weights (
OptionalReg[LayoutTensor[q_type, Layout.row_major(Int(-1)), ImmutAnyOrigin]]): Optional sink-token weight tensor for attention sinks.