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_prefill_v2_ragged
def mha_prefill_v2_ragged[k_t: MHAOperand, v_t: MHAOperand, mask_t: MHAMask, qkv_dtype: DType, output_dtype: DType, //, config: MhaConfigV2, cross_attention: Bool = False, sink: Bool = False, compile_options: StringSlice[ImmStaticOrigin] = CompilationTarget.default_compile_options()](q_ptr: Pointer[Scalar[qkv_dtype], ImmutAnyOrigin, _safe=False], k: k_t, v: v_t, output_ptr: Pointer[Scalar[output_dtype], MutAnyOrigin, _safe=False], mask_functor: mask_t, scale: Float32, input_row_offsets_ptr: Pointer[UInt32, ImmutAnyOrigin, _safe=False], kv_input_row_offsets_ptr: Pointer[UInt32, ImmutAnyOrigin, _safe=False], max_prompt_len: Int, batch_size: Int, ctx: DeviceContext, sink_weights_ptr: Pointer[Scalar[qkv_dtype], ImmutAnyOrigin, _safe=False] = Pointer.unsafe_dangling())
Host launcher for ragged MhaPrefillV2 prefill.
Mirrors mha_prefill_v2 for non-ragged, but each block does the
per-sequence ragged setup (start_of_seq / q_batch_offset / seq_len /
num_keys / start_pos) and constructs its rank-4 BSHD Q/O view
internally, so the caller doesn't have to pre-slice per sequence.
cross_attention=False (default): self-attention. K/V length per
batch derives from start_pos + (end_of_seq - start_of_seq).
kv_input_row_offsets_ptr is unused inside the kernel and the
caller may pass any well-typed stub (the dispatcher passes
input_row_offsets_ptr itself).
cross_attention=True: encoder-decoder. Pass the kv-side
input_row_offsets (uint32 cumulative sum, length batch_size + 1).
Grid: (NUM_HEADS, ceildiv(max_prompt_len, BM), batch_size). Blocks
where block_idx.y * BM >= seq_len for this sequence early-return.
Partial-Q-tile (seq_len % BM != 0) is handled internally via
lane-gated _store_o_to_gmem.
Parameters:
- βk_t (
MHAOperand): K operand type (inferred); anyMHAOperandwhoseblock_paged_tilereturns(KV_BLOCK, DEPTH)tiles. - βv_t (
MHAOperand): V operand type (inferred); same tile contract ask_t. - βmask_t (
MHAMask): Mask functor type (inferred); selects the per-tile masking strategy (causal, sliding-window, null, etc.). - βqkv_dtype (
DType): Element dtype of Q, K, and V (inferred); must equalconfig.dtype. - βoutput_dtype (
DType): Element dtype of the output buffer (inferred); must equalconfig.output_dtype. - βconfig (
MhaConfigV2): Shape configuration (MhaConfigV2). - βcross_attention (
Bool): Whether K/V length is independent of Q (encoder-decoder style) (defaults to False). - βsink (
Bool): Whether to seed the online softmax with attention-sink weights (defaults to False). - βcompile_options (
StringSlice[ImmStaticOrigin]): LLVM compile options string forwarded toctx.compile_function(defaults to the device'sdefault_compile_options).
Args:
- βq_ptr (
Pointer[Scalar[qkv_dtype], ImmutAnyOrigin, _safe=False]): Pointer to the packed Q buffer, pre-offset per sequence byinput_row_offsets_ptr. - βk (
k_t): K operand (MHAOperand). - βv (
v_t): V operand (MHAOperand). - βoutput_ptr (
Pointer[Scalar[output_dtype], MutAnyOrigin, _safe=False]): Pointer to the output buffer, same packing asq_ptr. - βmask_functor (
mask_t): Per-tile mask predicate (causal, sliding-window, etc.). - βscale (
Float32): Softmax scale (1/sqrt(DEPTH)). - βinput_row_offsets_ptr (
Pointer[UInt32, ImmutAnyOrigin, _safe=False]): Cumulative uint32 row offsets for Q sequences, lengthbatch_size + 1. - βkv_input_row_offsets_ptr (
Pointer[UInt32, ImmutAnyOrigin, _safe=False]): Cumulative uint32 row offsets for K/V sequences; read only whencross_attentionis True. - βmax_prompt_len (
Int): Maximum sequence length across the batch; sizes the grid'sblock_idx.ydimension. - βbatch_size (
Int): Number of sequences in the packed batch; sizes the grid'sblock_idx.zdimension. - βctx (
DeviceContext): Device context used to compile and enqueue the kernel. - βsink_weights_ptr (
Pointer[Scalar[qkv_dtype], ImmutAnyOrigin, _safe=False]): Per-q-head attention-sink scalar weights; read only whensinkis True. Callers may passunsafe_dangling()whensink=False.
Was this page helpful?
Thank you! We'll create more content like this.
Thank you for helping us improve!