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_decoding
def mla_decoding[q_type: DType, k_t: MHAOperand, output_type: DType, mask_t: MHAMask, ValidLT: TensorLayout, BM: Int, BN: Int, BK: Int, WM: Int, WN: Int, depth: Int, depth_v: Int, num_heads: Int, num_threads: Int, num_pipeline_stages: Int, group: Int = Int(1), ragged: Bool = False, _use_valid_length: Bool = False, _is_cache_length_accurate: Bool = False, decoding_warp_split_k: Bool = False, q_seq_len: Int = Int(1)](q_ptr: Pointer[Scalar[q_type], MutAnyOrigin, _safe=False], k: k_t, output_ptr: Pointer[Scalar[output_type], MutAnyOrigin, _safe=False], exp_sum_ptr: Pointer[Scalar[get_accum_type[q_type]()], MutAnyOrigin, _safe=False], qk_max_ptr: Pointer[Scalar[get_accum_type[q_type]()], MutAnyOrigin, _safe=False], scale: Float32, batch_size: Int, num_partitions: Int, max_cache_valid_length: Int, valid_length_tt: TileTensor[DType.uint32, ValidLT, ImmutAnyOrigin], mask: mask_t)
MLA decoding kernel that computes single-batch attention per CTA.
Sets up split-K offsets, batch indexing, and ragged or dense query strides,
then delegates to mla_decoding_single_batch on NVIDIA or
Attention.mla_decode on AMD to perform the actual QK and PV computation
for one decode step (with optional MTP query folding on AMD).
Parameters:
- βq_type (
DType): Element type of the query tensorq_ptr. Must be a half-float orfloat8_e4m3fn. - βk_t (
MHAOperand): KV cache operand type backingk. Carries the KV layout, dtype, and head geometry. - βoutput_type (
DType): Element type of the output tensoroutput_ptr.bfloat16whenq_typeisfloat8_e4m3fn, else matchesq_type. - βmask_t (
MHAMask): Mask functor type applied to attention scores. - βValidLT (
TensorLayout): Compile-time layout of thevalid_length_tttensor. - βBM (
Int): Number of query rows per block (the M-tile height). - βBN (
Int): Number of key rows per block (the N-tile width). - βBK (
Int): Tile size in the depth (head) dimension. - βWM (
Int): Warp tile height in the M dimension. - βWN (
Int): Warp tile width in the N dimension. - βdepth (
Int): Total head dimension of Q and K (576 for DeepSeek V2/3). - βdepth_v (
Int): V head dimension; V is derived asK[:, :, :depth_v](512 for DeepSeek V2/3). - βnum_heads (
Int): Number of query attention heads. - βnum_threads (
Int): Number of threads per CTA, derived from the warp tile geometry. - βnum_pipeline_stages (
Int): Number of software-pipelined MMA stages. - βgroup (
Int): GQA group size (num_heads // kv_num_heads); for MLAkv_num_heads == 1sogroup == num_heads(defaults to 1). - βragged (
Bool): Whetherq_ptris a ragged tensor with batch offsets invalid_length_ttinstead of a padded tensor (defaults toFalse). - β_use_valid_length (
Bool): Whether to readvalid_length_ttfor per-batch sequence lengths;Falsetreats all sequences asq_seq_lenlong (defaults toFalse). - β_is_cache_length_accurate (
Bool): Whethermax_cache_valid_lengthis the accurate latest length;Falseaddsseq_lenfor continuous execution (defaults toFalse). - βdecoding_warp_split_k (
Bool): Whether to use warp-level split-K reduction within the decode kernel (defaults toFalse). - βq_seq_len (
Int): Number of query tokens (S) folded into the MMA M dimension for MTP; 1 for single-token decode (defaults to 1).
Args:
- βq_ptr (
Pointer[Scalar[q_type], MutAnyOrigin, _safe=False]): Pointer to the query tensor with shape[batch, q_seq_len, num_heads, depth](or ragged equivalent). - βk (
k_t): KV cache operand. V is derived asK[:, :, :depth_v], so V is not loaded separately. - βoutput_ptr (
Pointer[Scalar[output_type], MutAnyOrigin, _safe=False]): Pointer to the output tensor with shape[batch, q_seq_len, num_heads, depth_v](or ragged equivalent). - βexp_sum_ptr (
Pointer[Scalar[get_accum_type[q_type]()], MutAnyOrigin, _safe=False]): Pointer to the per-row softmax exp-sum stats buffer, used for split-K reduction. - βqk_max_ptr (
Pointer[Scalar[get_accum_type[q_type]()], MutAnyOrigin, _safe=False]): Pointer to the per-row softmax max stats buffer, used for split-K reduction. - βscale (
Float32): Softmax scale factor applied to QK^T. - βbatch_size (
Int): Number of sequences in the batch. - βnum_partitions (
Int): Split-K partition count; 1 disables split-K reduction. - βmax_cache_valid_length (
Int): Total number of cached KV entries (the cache context length). - βvalid_length_tt (
TileTensor[DType.uint32, ValidLT, ImmutAnyOrigin]): Per-batchuint32tensor of valid (pre-padding) sequence lengths. For ragged inputs, carries cumulative row offsets. - β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!