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
fa_prefill_apple
def fa_prefill_apple[output_type: DType, k_t: MHAOperand, v_t: MHAOperand, mask_t: MHAMask, //, ragged: Bool = False, sink: Bool = False, _use_valid_length: Bool = False, _is_cache_length_accurate: Bool = False, num_simdgroups: Int = Int(4)](q: LayoutTensor[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, output: LayoutTensor[output_type, 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], valid_length: LayoutTensor[DType.uint32, element_layout=valid_length.element_layout, layout_int_type=valid_length.layout_int_type, linear_idx_type=valid_length.linear_idx_type, masked=valid_length.masked, alignment=valid_length.alignment], scale: Float32, batch_size: Int, max_prompt_len: Int, max_cache_size: Int, num_heads: Int, depth: Int, group: Int, ctx: DeviceContext, sink_weights: OptionalReg[LayoutTensor[q.dtype, Layout.row_major(Int(-1)), ImmutAnyOrigin]] = None)
Host launcher for the Apple M5 flash-attention prefill kernel.
Mirrors mha_gpu_naive's MHAOperand overload so flash_attention_dispatch
routes to it like the fallback, and specializes one kernel per supported
depth (a multiple of 16 up to FA_PREFILL_APPLE_MAX_HEAD_DIM). The external
LayoutTensor ABI is converted to TileTensor at the enqueue boundary so the
kernel is TileTensor-only.
Parameters:
- βoutput_type (
DType): The dtype of the output tensor (inferred). - βk_t (
MHAOperand): TheMHAOperandtype of the paged key cache (inferred). - βv_t (
MHAOperand): TheMHAOperandtype of the paged value cache (inferred). - βmask_t (
MHAMask): TheMHAMasktype of the attention mask functor (inferred). - βragged (
Bool): If True,valid_lengthis a cumulative offset buffer over variable-length sequences in the batch (defaults to False). - βsink (
Bool): If True, pre-seed the softmax state from per-head sink weights (defaults to False). - β_use_valid_length (
Bool): If True, read each batch's query length fromvalid_length[batch_id](defaults to False). - β_is_cache_length_accurate (
Bool): If True, the total attention length equals the query length (no cached prefix); if False, add the key operand's per-batch cache length (defaults to False). - βnum_simdgroups (
Int): Number of simdgroups co-resident per threadgroup, controlling per-launch occupancy (defaults to 4).
Args:
- βq (
LayoutTensor[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]): The query tensor in flattened BSHD layout (batch, seq, head, depth). - βk (
k_t): The paged key cache operand. - βv (
v_t): The paged value cache operand. - βmask_functor (
mask_t): The attention mask functor applied to each score tile. - βoutput (
LayoutTensor[output_type, 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]): The output tensor, written in the same BSHD layout asq. - βvalid_length (
LayoutTensor[DType.uint32, element_layout=valid_length.element_layout, layout_int_type=valid_length.layout_int_type, linear_idx_type=valid_length.linear_idx_type, masked=valid_length.masked, alignment=valid_length.alignment]): Per-batchuint32sequence lengths or cumulative offsets, depending onraggedand_use_valid_length. - βscale (
Float32): The softmax scale factor applied to QK score products. - βbatch_size (
Int): Number of sequences in the batch. - βmax_prompt_len (
Int): Maximum query length across the batch, in tokens. - βmax_cache_size (
Int): Maximum KV cache length across the batch, in tokens. - βnum_heads (
Int): Number of query attention heads. - βdepth (
Int): Head dimension; must be a multiple of 16 and at mostFA_PREFILL_APPLE_MAX_HEAD_DIM. - βgroup (
Int): Number of query heads per KV head (GQA group size). - βctx (
DeviceContext): The device context used to enqueue the kernel. - βsink_weights (
OptionalReg[LayoutTensor[q.dtype, Layout.row_major(Int(-1)), ImmutAnyOrigin]]): Optional per-head sink weights indexed by head id (defaults to None).
Was this page helpful?
Thank you! We'll create more content like this.
Thank you for helping us improve!