Mojo function
mla_prefill_branch_fp8
mla_prefill_branch_fp8[dtype: DType, fp8_dtype: DType, fp8_scale_dtype: DType, collection_t: KVCollectionT, //, qk_nope_head_dim: Int, m_scale_granularity: Int, n_scale_granularity: Int, k_scale_granularity: Int, mask_str: StringSlice[StaticConstantOrigin], score_mod_str: StringSlice[StaticConstantOrigin], target: StringSlice[StaticConstantOrigin] = "cpu"](output: LayoutTensor[dtype, layout, origin, element_layout=element_layout, layout_int_type=layout_int_type, linear_idx_type=linear_idx_type, masked=masked, alignment=alignment], q: LayoutTensor[dtype, layout, origin, element_layout=element_layout, layout_int_type=layout_int_type, linear_idx_type=linear_idx_type, masked=masked, alignment=alignment], input_row_offsets: LayoutTensor[DType.uint32, layout, origin, element_layout=element_layout, layout_int_type=layout_int_type, linear_idx_type=linear_idx_type, masked=masked, alignment=alignment], kv_collection: collection_t, layer_idx: UInt32, scale: Float32, buffer_row_offsets: LayoutTensor[DType.uint32, layout, origin, element_layout=element_layout, layout_int_type=layout_int_type, linear_idx_type=linear_idx_type, masked=masked, alignment=alignment], cache_offsets: LayoutTensor[DType.uint32, layout, origin, element_layout=element_layout, layout_int_type=layout_int_type, linear_idx_type=linear_idx_type, masked=masked, alignment=alignment], buffer_length: Int, kv_b_proj: LayoutTensor[fp8_dtype, layout, origin, element_layout=element_layout, layout_int_type=layout_int_type, linear_idx_type=linear_idx_type, masked=masked, alignment=alignment], kv_b_proj_scale: LayoutTensor[fp8_scale_dtype, layout, origin, element_layout=element_layout, layout_int_type=layout_int_type, linear_idx_type=linear_idx_type, masked=masked, alignment=alignment], ctx: DeviceContext)
This is a manually fused kernel that performs the following operations: - Copy the KV latent values from PagedKVCache to a contiguous buffer. - Quantize the KV latent values to fp8. - Up-project the latent KV values to full K and V through a matmul. - Split the concatenated KV into K and V. - Perform MLA prefill.
Parameters:
- dtype (
DType): Data type of the input and output tensors. - fp8_dtype (
DType): Data type of the fp8 input and output tensors. - fp8_scale_dtype (
DType): Data type of the fp8 scale input and output tensors. - collection_t (
KVCollectionT): Type of the KV collection. - qk_nope_head_dim (
Int): Dimension of non-rope parts of the Q/K heads. - m_scale_granularity (
Int): Granularity of the scale for M dimension of the matrix multiplication. - n_scale_granularity (
Int): Granularity of the scale for N dimension of the matrix multiplication. - k_scale_granularity (
Int): Granularity of the scale for K dimension of the matrix multiplication. - mask_str (
StringSlice): Mask variant. - score_mod_str (
StringSlice): Positional encoding variant. - target (
StringSlice): Target device.
Args:
- output (
LayoutTensor): Output tensor of shape [tot_seq_len, num_heads, v_head_dim]. - q (
LayoutTensor): Query tensor of shape [tot_seq_len, num_heads, qk_nope_head_dim + qk_rope_head_dim]. - input_row_offsets (
LayoutTensor): Indicates where each request starts and ends inq. Shape: [num_batches + 1]. - kv_collection (
collection_t): Paged KV Cache object. - layer_idx (
UInt32): Layer index. - scale (
Float32): Scale for the attention calculation. - buffer_row_offsets (
LayoutTensor): Indicates where each request's KV latent values should be stored in the contiguous K buffer. This is a 1D tensor of shape [num_batches + 1]. - cache_offsets (
LayoutTensor): Indicates the starting token position in the KV cache from which to copy KV latent values for each request. This is a 1D tensor of shape [num_batches + 1]. - buffer_length (
Int): The total number of tokens in the KV cache. Scalar. - kv_b_proj (
LayoutTensor): Weight matrix for up-projecting the KV latent values to full K and V. Shape: [num_heads * (qk_nope_head_dim + v_head_dim), kv_latent_dim]. - kv_b_proj_scale (
LayoutTensor): The scale for the weight matrix. Shape varies depending on the float8_config. - ctx (
DeviceContext): Device context.
Was this page helpful?
Thank you! We'll create more content like this.
Thank you for helping us improve!