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
flash_attention_dispatch
def flash_attention_dispatch[k_t: MHAOperand, v_t: MHAOperand, mask_t: MHAMask, dtype: DType, q_layout: Layout, //, kv_num_heads: Int, config: MHAConfig[dtype] = MHAConfig(SIMD[IntTuple](q_layout.shape[Int((add q_layout.rank(), -2))]), SIMD[IntTuple](q_layout.shape[Int((add q_layout.rank(), -1))]), Optional(None), Optional(None), Optional(None), Optional(None), Optional(None), Int(4), Int(1), FlashAttentionAlgorithm(Int(-1)), TensorMapSwizzle.SWIZZLE_128B), ragged: Bool = False, sink: Bool = False, _is_flash_attention_applicable: Bool = True, _is_cache_length_accurate: Bool = False, _use_valid_length: Bool = True, _padded_ndbuffer: Bool = False, decoding_warp_split_k: Bool = False](output: LayoutTensor[element_layout=output.element_layout, layout_int_type=output.layout_int_type, linear_idx_type=output.linear_idx_type, masked=output.masked, alignment=output.alignment], q: LayoutTensor[dtype, q_layout, element_layout=q.element_layout, layout_int_type=q.layout_int_type, linear_idx_type=q.linear_idx_type, masked=q.masked, alignment=q.alignment], k: k_t, v: v_t, mask_functor: mask_t, max_prompt_len: Int, max_cache_valid_length: Int, scale: Float32, is_token_generation: Bool, ctx: DeviceContext, valid_length: OptionalReg[LayoutTensor[DType.uint32, Layout.row_major(Int(-1)), ImmutAnyOrigin]] = None, kv_input_row_offsets: OptionalReg[LayoutTensor[DType.uint32, Layout.row_major(Int(-1)), ImmutAnyOrigin]] = None, num_partitions: Optional[Int] = None, sink_weights: OptionalReg[LayoutTensor[dtype, Layout.row_major(Int(-1)), ImmutAnyOrigin]] = None, decode_dispatch_metadata: OptionalReg[MHADecodeDispatchMetadata] = None)
Dispatch a flash-attention kernel for prefill or decode over dense or KV-cache operands.
Central dispatch point that inspects is_token_generation, dtype,
depth, and the target GPU to select among FA2, FA3, and naive
implementations. Handles both prefill (context encoding) and
incremental decode, routing ragged-batch and paged-KV-cache inputs
through appropriate kernel paths.
Parameters:
- βk_t (
MHAOperand): KV-cache or dense operand type for the key tensor. - βv_t (
MHAOperand): KV-cache or dense operand type for the value tensor. - βmask_t (
MHAMask): Attention mask type implementingMHAMask. - βdtype (
DType): Element type of Q (K/V type is inferred fromk_t). - βq_layout (
Layout): Compile-time layout of the query tensor. - βkv_num_heads (
Int): Number of key/value heads (for GQA). - βconfig (
MHAConfig[dtype]): Tile/pipeline configuration; defaults from query shape. - βragged (
Bool):Truefor ragged-batch (variable-length) inputs. - βsink (
Bool):Trueto enable attention-sink mode. - β_is_flash_attention_applicable (
Bool): Internal flag to suppress FA path. - β_is_cache_length_accurate (
Bool):Truewhen KV cache length already includes the newest tokens. - β_use_valid_length (
Bool):Trueto mask output with per-sequence lengths. - β_padded_ndbuffer (
Bool):Truewhen the NBuffer has padded dense inputs. - βdecoding_warp_split_k (
Bool): Enable warp-level split-K for decode.
Args:
- βoutput (
LayoutTensor[element_layout=output.element_layout, layout_int_type=output.layout_int_type, linear_idx_type=output.linear_idx_type, masked=output.masked, alignment=output.alignment]): Mutable output tensor. - βq (
LayoutTensor[dtype, q_layout, element_layout=q.element_layout, layout_int_type=q.layout_int_type, linear_idx_type=q.linear_idx_type, masked=q.masked, alignment=q.alignment]): Query tensor. - βk (
k_t): Key operand (KV cache or dense tensor). - βv (
v_t): Value operand (KV cache or dense tensor). - βmask_functor (
mask_t): Mask instance. - βmax_prompt_len (
Int): Maximum query sequence length in the batch. - βmax_cache_valid_length (
Int): Maximum key/value sequence length. - βscale (
Float32): Softmax temperature scale. - βis_token_generation (
Bool):Truefor decode mode;Falsefor prefill. - βctx (
DeviceContext): GPU device context. - βvalid_length (
OptionalReg[LayoutTensor[DType.uint32, Layout.row_major(Int(-1)), ImmutAnyOrigin]]): Per-sequence valid lengths for masked output. - βkv_input_row_offsets (
OptionalReg[LayoutTensor[DType.uint32, Layout.row_major(Int(-1)), ImmutAnyOrigin]]): Row offsets for ragged KV inputs. - βnum_partitions (
Optional[Int]): Override split-K partition count;Nonefor auto. - βsink_weights (
OptionalReg[LayoutTensor[dtype, Layout.row_major(Int(-1)), ImmutAnyOrigin]]): Optional sink-token weight tensor. - βdecode_dispatch_metadata (
OptionalReg[MHADecodeDispatchMetadata]): Pre-computed decode dispatch metadata.
Was this page helpful?
Thank you! We'll create more content like this.
Thank you for helping us improve!