For the complete documentation index, see llms.txt. Markdown versions of all pages are available by appending .md to any URL (e.g. /max/get-started.md).
Mojo function
mha_decoding_single_batch_pipelined
def mha_decoding_single_batch_pipelined[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, _safe=False], k: k_t, v: v_t, output_ptr: Pointer[Scalar[output_type], MutAnyOrigin, _safe=False], exp_sum_ptr: Pointer[Scalar[get_accum_type[q_type]()], MutAnyOrigin, _safe=False], qk_max_ptr: Pointer[Scalar[get_accum_type[q_type]()], MutAnyOrigin, _safe=False], scale: Float32, num_keys: Int, num_partitions: Int, sink_weights: OptionalReg[LayoutTensor[q_type, Layout.row_major(Int(-1)), ImmutAnyOrigin]], mask: mask_t, batch_idx: Int)
Flash attention v2 decode kernel for a single batch element with pipelined multistage MMA.
Computes attention for the decoding (single-query) case using the FA2
online-softmax algorithm with multistage pipelining of K/V loads. When
num_partitions exceeds 1, each block processes a contiguous slice of
the key dimension and writes partial exp_sum and qk_max statistics
for a subsequent mha_splitk_reduce pass.
Parameters:
- βq_type (
DType): Element type of the query tensor (inferred). - βk_t (
MHAOperand): Key operand type backing the key tensor (inferred). - βv_t (
MHAOperand): Value operand type backing the value tensor (inferred). - βoutput_type (
DType): Element type of the output tensor (inferred). - βmask_t (
MHAMask): Attention mask type implementingMHAMask(inferred). - βBM (
Int): Number of query rows processed per thread block. - βBN (
Int): Number of key columns per thread block tile. - βBK (
Int): Tile size in the head-depth dimension. - β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 pipeline stages for the multistage MMA loads. - βgroup (
Int): GQA group size, query heads per key/value head (defaults to 1). - βdecoding_warp_split_k (
Bool): Enable warp-level split-K for decode (defaults toFalse). - βsink (
Bool): Enable attention-sink mode where the first tokens always attend (defaults toFalse).
Args:
- βq_ptr (
Pointer[Scalar[q_type], ImmutAnyOrigin, _safe=False]): Pointer to the query tensor for this batch element. - βk (
k_t): Key operand backed by a KV cache. - βv (
v_t): Value operand backed by a KV cache. - βoutput_ptr (
Pointer[Scalar[output_type], MutAnyOrigin, _safe=False]): Pointer to the output tensor for this batch element. - βexp_sum_ptr (
Pointer[Scalar[get_accum_type[q_type]()], MutAnyOrigin, _safe=False]): Pointer to the online-softmax exponent sum buffer for this batch. - βqk_max_ptr (
Pointer[Scalar[get_accum_type[q_type]()], MutAnyOrigin, _safe=False]): Pointer to the online-softmax running maximum buffer for this batch. - βscale (
Float32): Softmax temperature scale applied to QΒ·Kα΅. - βnum_keys (
Int): Total number of key/value entries (cache length) for this batch. - βnum_partitions (
Int): Number of split-K partitions dividing the key dimension. - βsink_weights (
OptionalReg[LayoutTensor[q_type, Layout.row_major(Int(-1)), ImmutAnyOrigin]]): Optional sink-token weight tensor for attention sinks. - βmask (
mask_t): Mask instance used to apply the attention mask. - βbatch_idx (
Int): Index of the batch element this block processes.
Was this page helpful?
Thank you! We'll create more content like this.
Thank you for helping us improve!