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_single_batch
def mla_prefill_single_batch[q_type: DType, k_t: MHAOperand, v_t: MHAOperand, k_rope_t: MHAOperand, output_type: DType, mask_t: MHAMask, *, config: MHAConfig[config.dtype], group: Int = Int(1), q_depth: Int = Int(192), cache_depth: Int = Int(576)](q_ptr: Pointer[Scalar[q_type], _safe=False], k: k_t, v: v_t, k_rope: k_rope_t, output_ptr: Pointer[Scalar[output_type], _safe=False], scale: Float32, seq_len: Int, max_seq_len: Int, start_pos: UInt32, cache_start_pos: UInt32, num_keys: Int, mask: mask_t, batch_idx: Int)
MLA for encoding where seqlen > 1.
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). - β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 1). - β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.
Args:
- βq_ptr (
Pointer[Scalar[q_type], _safe=False]): Pointer to this batch's query rows with shape[seq_len, num_heads, q_depth]. - βk (
k_t): Key operand providing the nope (latent) part of K. - β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], _safe=False]): Pointer to this batch's output rows with shape[seq_len, num_heads, depth]wheredepthisconfig.depth. - βscale (
Float32): Softmax scale factor applied to QK^T. - βseq_len (
Int): Valid sequence length (without padding) for this batch. - βmax_seq_len (
Int): Padded sequence length ceiling for this batch. - βstart_pos (
UInt32): Starting position of the query within the sequence (the cached length before this prefill). - βcache_start_pos (
UInt32): Starting offset into the paged KV cache for this batch. - βnum_keys (
Int): Total number of KV keys (cached plus new tokens) for this batch. - βmask (
mask_t): Mask functor instance applied to attention scores. - βbatch_idx (
Int): Index of the current batch in the request.
Was this page helpful?
Thank you! We'll create more content like this.
Thank you for helping us improve!