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
naive_fa_decode_apple_stitch
def naive_fa_decode_apple_stitch[output_type: DType, p_type: DType, k_t: MHAOperand, v_t: MHAOperand, mask_t: MHAMask, output_layout: TensorLayout, p_layout: TensorLayout, valid_length_layout: TensorLayout, ragged: Bool = False, sink: Bool = False, _use_valid_length: Bool = False, _is_cache_length_accurate: Bool = False, *, SplitSize: Int](output: TileTensor[output_type, output_layout, MutAnyOrigin], o_partial: TileTensor[p_type, p_layout, ImmutAnyOrigin], m_partial: TileTensor[p_type, p_layout, ImmutAnyOrigin], l_partial: TileTensor[p_type, p_layout, ImmutAnyOrigin], k: k_t, valid_length: TileTensor[DType.uint32, valid_length_layout, ImmutAnyOrigin], max_prompt_len: Int, max_cache_size: Int, num_heads: Int, depth: Int, num_partitions: Int)
Combines per-partition partials into the final decode attention output.
Grid (num_heads, batch_size), block depth: one thread per head-dim
element. Reads the contiguous per-split (o_partial, m_partial, l_partial)
buffers written by naive_fa_decode_apple_core and merges them with a
log-sum-exp (LSE) reduction in FP32, then writes the normalized
acc / l row back into output. The active split count mirrors the
producer's cur_cache_len so only the partials actually written are
combined.
Constraints:
o_partial, m_partial, and output must be flat 1D TileTensors.
Parameters:
- βoutput_type (
DType): Element type of theoutputtensor (inferred). - βp_type (
DType): Accumulation and partials element type (inferred). - βk_t (
MHAOperand):MHAOperandtype of the key cache operand (inferred). - βv_t (
MHAOperand): Unused; mirrorsmha_gpu_naivefor dispatch uniformity (inferred). - βmask_t (
MHAMask): Unused; mirrorsmha_gpu_naivefor dispatch uniformity (inferred). - βoutput_layout (
TensorLayout):TensorLayoutof theoutputtensor (inferred). - βp_layout (
TensorLayout):TensorLayoutof the partials buffers (inferred). - βvalid_length_layout (
TensorLayout):TensorLayoutof thevalid_lengthtensor (inferred). - βragged (
Bool): Whether sequences are ragged with variable lengths and row offsets invalid_length(defaults toFalse). - βsink (
Bool): Whether attention sink is enabled; the producer already bakes the sink contribution into split 0 partials, so the stitch does not act on this flag (defaults toFalse). - β_use_valid_length (
Bool): Whether to usevalid_lengthfor KVCache decode as per-sequence query lengths (defaults toFalse). - β_is_cache_length_accurate (
Bool): Whether the cache length equals the query length, so no new-token KV is added (defaults toFalse). - βSplitSize (
Int): Per-partition KV span in keys; must match the producer'sSplitSize.
Args:
- βoutput (
TileTensor[output_type, output_layout, MutAnyOrigin]): Flat 1D output tensor; written with the normalized attention output per(batch, head, depth). - βo_partial (
TileTensor[p_type, p_layout, ImmutAnyOrigin]): Flat 1D partial output buffer from the producer; one accumulator per(batch, head, depth, split). - βm_partial (
TileTensor[p_type, p_layout, ImmutAnyOrigin]): Flat 1D partial row-max buffer from the producer; one running max per(batch, head, split). - βl_partial (
TileTensor[p_type, p_layout, ImmutAnyOrigin]): Flat 1D partial row-sum buffer from the producer; one running denominator per(batch, head, split). - βk (
k_t): Key cache operand implementing theMHAOperandcontract; used forcache_lengthin the ragged and KVCache paths. - βvalid_length (
TileTensor[DType.uint32, valid_length_layout, ImmutAnyOrigin]): Per-sequence row offsets or query lengths (uint32); meaning depends onraggedand_use_valid_length. - βmax_prompt_len (
Int): Maximum prompt length; the dense decode path's query length. - βmax_cache_size (
Int): Full key count for the dense decode path (the K tensor's seq dim). - βnum_heads (
Int): Number of query attention heads. - βdepth (
Int): Runtime head dimension. - βnum_partitions (
Int): Number of KV splits; must match the producer's partition count.
Was this page helpful?
Thank you! We'll create more content like this.
Thank you for helping us improve!