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
mla_prefill
def mla_prefill[q_type: DType, k_t: MHAOperand, v_t: MHAOperand, k_rope_t: MHAOperand, output_type: DType, mask_t: MHAMask, valid_layout: TensorLayout, config: MHAConfig[config.dtype], group: Int = Int(128), q_depth: Int = Int(192), cache_depth: Int = Int(576), _ndbuffer_mha_operand: Bool = False](q_ptr: Pointer[Scalar[q_type], MutAnyOrigin, _safe=False], k: k_t, v: v_t, k_rope: k_rope_t, output_ptr: Pointer[Scalar[output_type], MutAnyOrigin, _safe=False], scale: Float32, batch_size: Int, seq_len_arg: Int, valid_length_tt: TileTensor[DType.uint32, valid_layout, ImmutAnyOrigin], cache_offsets: OptionalReg[LayoutTensor[DType.uint32, Layout.row_major(Int(-1)), MutAnyOrigin]], mask: mask_t)
MLA prefill kernel that computes attention for sequences longer than one token.
Resolves per-batch sequence lengths from the ragged row-offsets, then
delegates to mla_prefill_single_batch on NVIDIA or
Attention.mla_prefill on AMD to compute the QK and PV matmuls with
online softmax over the full prompt plus cached KV.
Parameters:
- βq_type (
DType): Element type of the query tensorq_ptr(inferred). Must be a half-float orfloat8_e4m3fn. - βk_t (
MHAOperand): Key operand type backingk(inferred). Provides the nope part of K. - βv_t (
MHAOperand): Value operand type backingv(inferred). - βk_rope_t (
MHAOperand): KV cache operand type backingk_rope(inferred). Provides the rope part of K and the per-batch cache length. - βoutput_type (
DType): Element type ofoutput_ptr(inferred).bfloat16whenq_typeisfloat8_e4m3fn, else matchesq_type. - βmask_t (
MHAMask): Mask functor type applied to attention scores (inferred). - βvalid_layout (
TensorLayout): Compile-time layout ofvalid_length_tt(inferred). - βconfig (
MHAConfig[config.dtype]): MLA attention config carryingnum_heads,depth, and tile geometry (block_m,block_n,block_k). - βgroup (
Int): GQA group size,num_heads // num_kv_heads(defaults to 128). - βq_depth (
Int): Q head dimension in elements (defaults to 192). For DeepSeek V2/3, 192 = 128 nope + 64 rope. - βcache_depth (
Int): KV cache head dimension in elements (defaults to 576). The absorbed-latent width of the paged MLA cache. - β_ndbuffer_mha_operand (
Bool): Whether the K/V/k_ropeoperands use ND-buffer layout instead of the default TileTensor layout (defaults toFalse).
Args:
- βq_ptr (
Pointer[Scalar[q_type], MutAnyOrigin, _safe=False]): Pointer to the query tensor with shape[total_q_tokens, num_heads, q_depth]. - βk (
k_t): Key operand (nope part). - βv (
v_t): Value operand. - βk_rope (
k_rope_t): KV cache operand providing the rope part of K;k_rope.cache_length(batch_idx)gives the cached length. - βoutput_ptr (
Pointer[Scalar[output_type], MutAnyOrigin, _safe=False]): Pointer to the output tensor with shape[total_q_tokens, num_heads, depth]. - βscale (
Float32): Softmax scale factor applied to QK^T. - βbatch_size (
Int): Number of batches in the request; drives the grid's z dimension. - βseq_len_arg (
Int): Maximum query sequence length (padding ceiling across all batches). - βvalid_length_tt (
TileTensor[DType.uint32, valid_layout, ImmutAnyOrigin]): Per-batchuint32tensor of cumulative row offsets withbatch_size + 1entries;offsets[b]tooffsets[b+1]gives batchb's token range. - βcache_offsets (
OptionalReg[LayoutTensor[DType.uint32, Layout.row_major(Int(-1)), MutAnyOrigin]]): Optional per-batchuint32tensor of starting offsets into the paged KV cache;Nonewhen the cache is contiguous. - βmask (
mask_t): Mask functor instance applied to attention scores.
Was this page helpful?
Thank you! We'll create more content like this.
Thank you for helping us improve!