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_gpu_naive
def mha_gpu_naive[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](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)
Launch the naive (two-pass BMM) GPU attention implementation.
Computes attention as two separate batched matrix multiplications using
temporary GMEM storage for the P = softmax(QΒ·Kα΅ / scale) intermediate.
This is slower than flash attention but supports any head depth and serves
as a correctness reference. Dispatches three sequential kernels:
_bmm0_bs (QΒ·Kα΅ + mask), softmax normalisation, and _bmm1_bs (PΒ·V).
Parameters:
- βoutput_type (
DType): Element type of the attention output. - βk_t (
MHAOperand): Key operand type (dense or KV-cache). - βv_t (
MHAOperand): Value operand type (dense or KV-cache). - βmask_t (
MHAMask): Attention mask type. - βragged (
Bool):Truefor ragged-batch inputs. - βsink (
Bool):Trueto enable attention-sink mode. - β_use_valid_length (
Bool):Trueto read per-sequence valid lengths. - β_is_cache_length_accurate (
Bool):Truewhen cache length is exact.
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]): Query tensor. - βk (
k_t): Key operand. - βv (
v_t): Value operand. - βmask_functor (
mask_t): Mask instance. - β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]): Mutable output tensor. - β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-sequence valid lengths. - βscale (
Float32): Softmax temperature scale. - βbatch_size (
Int): Number of sequences in the batch. - βmax_prompt_len (
Int): Maximum query sequence length. - βmax_cache_size (
Int): Maximum key/value sequence length. - βnum_heads (
Int): Number of query heads. - βdepth (
Int): Attention head depth. - βgroup (
Int): GQA group size. - βctx (
DeviceContext): GPU device context. - βsink_weights (
OptionalReg[LayoutTensor[q.dtype, Layout.row_major(Int(-1)), ImmutAnyOrigin]]): Sink-token weights for attention-sink mode.
def mha_gpu_naive[q_type: DType, k_type: DType, v_type: DType, output_type: DType, mask_type: DType, //, sink: Bool = False](q: LayoutTensor[q_type, 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: LayoutTensor[k_type, element_layout=k.element_layout, layout_int_type=k.layout_int_type, linear_idx_type=k.linear_idx_type, masked=k.masked, alignment=k.alignment], v: LayoutTensor[v_type, element_layout=v.element_layout, layout_int_type=v.layout_int_type, linear_idx_type=v.linear_idx_type, masked=v.masked, alignment=v.alignment], mask: LayoutTensor[mask_type, element_layout=mask.element_layout, layout_int_type=mask.layout_int_type, linear_idx_type=mask.linear_idx_type, masked=mask.masked, alignment=mask.alignment], 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], scale: Float32, batch_size: Int, seq_len: Int, num_keys: Int, num_heads: Int, depth: Int, group: Int, ctx: DeviceContext, sink_weights: OptionalReg[LayoutTensor[q_type, Layout.row_major(Int(-1)), ImmutAnyOrigin]] = None)
def mha_gpu_naive[q_type: DType, k_type: DType, v_type: DType, output_type: DType, MaskType: MHAMask, //, sink: Bool = False](q: LayoutTensor[q_type, 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: LayoutTensor[k_type, element_layout=k.element_layout, layout_int_type=k.layout_int_type, linear_idx_type=k.linear_idx_type, masked=k.masked, alignment=k.alignment], v: LayoutTensor[v_type, element_layout=v.element_layout, layout_int_type=v.layout_int_type, linear_idx_type=v.linear_idx_type, masked=v.masked, alignment=v.alignment], mask: MaskType, 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], scale: Float32, batch_size: Int, seq_len: Int, num_keys: Int, num_heads: Int, depth: Int, group: Int, ctx: DeviceContext, sink_weights: OptionalReg[LayoutTensor[q_type, Layout.row_major(Int(-1)), ImmutAnyOrigin]] = None)
def mha_gpu_naive[q_type: DType, k_type: DType, v_type: DType, output_type: DType, mask_type: DType, q_tt_layout: TensorLayout, k_tt_layout: TensorLayout, v_tt_layout: TensorLayout, mask_tt_layout: TensorLayout, output_tt_layout: TensorLayout, //, sink: Bool = False](q: TileTensor[q_type, q_tt_layout, Storage=q.Storage, linear_idx_type=q.linear_idx_type], k: TileTensor[k_type, k_tt_layout, Storage=k.Storage, linear_idx_type=k.linear_idx_type], v: TileTensor[v_type, v_tt_layout, Storage=v.Storage, linear_idx_type=v.linear_idx_type], mask: TileTensor[mask_type, mask_tt_layout, Storage=mask.Storage, linear_idx_type=mask.linear_idx_type], output: TileTensor[output_type, output_tt_layout, Storage=output.Storage, linear_idx_type=output.linear_idx_type], scale: Float32, batch_size: Int, seq_len: Int, num_keys: Int, num_heads: Int, depth: Int, group: Int, ctx: DeviceContext, sink_weights: OptionalReg[LayoutTensor[q_type, Layout.row_major(Int(-1)), ImmutAnyOrigin]] = None)
TileTensor overload of mha_gpu_naive (materialized mask). Bridges to LayoutTensor internally.
Parameters:
- βq_type (
DType): Element type of the query tensor. - βk_type (
DType): Element type of the key tensor. - βv_type (
DType): Element type of the value tensor. - βoutput_type (
DType): Element type of the output tensor. - βmask_type (
DType): Element type of the dense attention mask tensor. - βq_tt_layout (
TensorLayout): Compile-timeTensorLayoutof the query tensor. - βk_tt_layout (
TensorLayout): Compile-timeTensorLayoutof the key tensor. - βv_tt_layout (
TensorLayout): Compile-timeTensorLayoutof the value tensor. - βmask_tt_layout (
TensorLayout): Compile-timeTensorLayoutof the mask tensor. - βoutput_tt_layout (
TensorLayout): Compile-timeTensorLayoutof the output tensor. - βsink (
Bool):Trueto enable attention-sink mode (defaults toFalse).
Args:
- βq (
TileTensor[q_type, q_tt_layout, Storage=q.Storage, linear_idx_type=q.linear_idx_type]): QueryTileTensorwith BSHD layout. - βk (
TileTensor[k_type, k_tt_layout, Storage=k.Storage, linear_idx_type=k.linear_idx_type]): KeyTileTensor. - βv (
TileTensor[v_type, v_tt_layout, Storage=v.Storage, linear_idx_type=v.linear_idx_type]): ValueTileTensor. - βmask (
TileTensor[mask_type, mask_tt_layout, Storage=mask.Storage, linear_idx_type=mask.linear_idx_type]): Dense attention maskTileTensor. - βoutput (
TileTensor[output_type, output_tt_layout, Storage=output.Storage, linear_idx_type=output.linear_idx_type]): Mutable outputTileTensor. - βscale (
Float32): Softmax temperature scale applied to QΒ·Kα΅. - βbatch_size (
Int): Number of sequences in the batch. - βseq_len (
Int): Maximum query sequence length in the batch. - βnum_keys (
Int): Maximum key/value sequence length. - βnum_heads (
Int): Number of query heads. - βdepth (
Int): Attention head depth (key/value dimension per head). - βgroup (
Int): GQA group size (query heads per key/value head). - βctx (
DeviceContext): GPU device context for kernel dispatch. - βsink_weights (
OptionalReg[LayoutTensor[q_type, Layout.row_major(Int(-1)), ImmutAnyOrigin]]): Optional sink-token weight tensor for attention sinks.
def mha_gpu_naive[q_type: DType, k_type: DType, v_type: DType, output_type: DType, MaskType: MHAMask, q_tt_layout: TensorLayout, k_tt_layout: TensorLayout, v_tt_layout: TensorLayout, output_tt_layout: TensorLayout, //, sink: Bool = False](q: TileTensor[q_type, q_tt_layout, Storage=q.Storage, linear_idx_type=q.linear_idx_type], k: TileTensor[k_type, k_tt_layout, Storage=k.Storage, linear_idx_type=k.linear_idx_type], v: TileTensor[v_type, v_tt_layout, Storage=v.Storage, linear_idx_type=v.linear_idx_type], mask: MaskType, output: TileTensor[output_type, output_tt_layout, Storage=output.Storage, linear_idx_type=output.linear_idx_type], scale: Float32, batch_size: Int, seq_len: Int, num_keys: Int, num_heads: Int, depth: Int, group: Int, ctx: DeviceContext, sink_weights: OptionalReg[LayoutTensor[q_type, Layout.row_major(Int(-1)), ImmutAnyOrigin]] = None)
TileTensor overload of mha_gpu_naive (MHAMask functor). Bridges to LayoutTensor internally.
Parameters:
- βq_type (
DType): Element type of the query tensor. - βk_type (
DType): Element type of the key tensor. - βv_type (
DType): Element type of the value tensor. - βoutput_type (
DType): Element type of the output tensor. - βMaskType (
MHAMask): Attention mask type implementingMHAMask. - βq_tt_layout (
TensorLayout): Compile-timeTensorLayoutof the query tensor. - βk_tt_layout (
TensorLayout): Compile-timeTensorLayoutof the key tensor. - βv_tt_layout (
TensorLayout): Compile-timeTensorLayoutof the value tensor. - βoutput_tt_layout (
TensorLayout): Compile-timeTensorLayoutof the output tensor. - βsink (
Bool):Trueto enable attention-sink mode (defaults toFalse).
Args:
- βq (
TileTensor[q_type, q_tt_layout, Storage=q.Storage, linear_idx_type=q.linear_idx_type]): QueryTileTensorwith BSHD layout. - βk (
TileTensor[k_type, k_tt_layout, Storage=k.Storage, linear_idx_type=k.linear_idx_type]): KeyTileTensor. - βv (
TileTensor[v_type, v_tt_layout, Storage=v.Storage, linear_idx_type=v.linear_idx_type]): ValueTileTensor. - βmask (
MaskType): Mask instance used to apply the attention mask. - βoutput (
TileTensor[output_type, output_tt_layout, Storage=output.Storage, linear_idx_type=output.linear_idx_type]): Mutable outputTileTensor. - βscale (
Float32): Softmax temperature scale applied to QΒ·Kα΅. - βbatch_size (
Int): Number of sequences in the batch. - βseq_len (
Int): Maximum query sequence length in the batch. - βnum_keys (
Int): Maximum key/value sequence length. - βnum_heads (
Int): Number of query heads. - βdepth (
Int): Attention head depth (key/value dimension per head). - βgroup (
Int): GQA group size (query heads per key/value head). - βctx (
DeviceContext): GPU device context for kernel dispatch. - βsink_weights (
OptionalReg[LayoutTensor[q_type, Layout.row_major(Int(-1)), ImmutAnyOrigin]]): Optional sink-token weight tensor for attention sinks.
def mha_gpu_naive[q_type: DType, output_type: DType, cache_t: KVCacheT, mask_t: MHAMask, //, ragged: Bool = False, sink: Bool = False](q: LayoutTensor[q_type, 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: cache_t, v: cache_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_type, Layout.row_major(Int(-1)), ImmutAnyOrigin]] = None)
Was this page helpful?
Thank you! We'll create more content like this.
Thank you for helping us improve!