| Files | Last commit | Last update |
|---|---|---|
| 1 day ago | ||
| 9 days ago | ||
| 27 days ago | ||
| 4 days ago | ||
| 7 days ago | ||
| 3 days ago | ||
| 26 days ago | ||
| 29 days ago | ||
| 24 days ago | ||
| 3 days ago |
MixedQuantSparseFlashMla
English | 简体中文
Product Support
| Product | Supported |
|---|---|
| Ascend 950PR&950DT products | √ |
| Atlas A3 products | √ |
| Atlas A2 products | √ |
| Atlas 200I/500 A2 inference products | × |
| Atlas inference products | × |
| Atlas training products | × |
Function Description
-
Operator function:
The
MixedQuantSparseFlashMlaoperator performs MLA (Multi-head Latent Attention) computation in quantization and sparsity scenarios. It supports three types of attention computation scenarios: SWA (Sliding Window Attention), CSA (Compressed Sparse Attention), and HCA (Heavily Compressed Attention). Different fromSparseFlashMla, this operator supports per-token-group quantized KV input. The three typical scenarios of this operator are as follows:- SWA (Sliding Window Attention): Only
ori_kvis passed in, and sliding window attention is performed on the original KV. - CSA (Compressed Sparse Attention):
ori_kv,cmp_kv, andcmp_sparse_indicesare passed in, and attention is performed on both the original KV window and the compressed KV selected by topK. - HCA (Heavily Compressed Attention):
ori_kvandcmp_kvare passed in, and attention is performed on both the original KV window and the continuous compressed KV segments.
The invocation requires the task list
metadatagenerated byMixedQuantSparseFlashMlaMetadata, which is generated before the main operator is executed. In the current version, thismetadatamust be passed to the main operator. The typical calling process is as follows:- Prepare the corresponding inputs such as
q,ori_kv, andcmp_kvbased on the calling scenario. - Call
MixedQuantSparseFlashMlaMetadatato generatemetadata, which is used as an input parameter ofMixedQuantSparseFlashMla. - Call
MixedQuantSparseFlashMlaand pass themetadataobtained in the previous step to the main operator to generate the computation result.
- SWA (Sliding Window Attention): Only
-
Computation formula:
MixedQuantSparseFlashMlauses MLA to compute sparse attention on inputs where Key and Value share the same data. The principle is to selectively compress and quantize the input KV, and then obtain the attention weights by applying Softmax to the computation result of the Query and the concatenated KV.The MLA computation formula is generally defined as follows, where K~=V~\tilde{K}=\tilde{V} is the KV that actually participates in the computation, controlled by the input parameters and composed of the sliding window part of
ori_kvand the compressed part ofcmp_kv. The range of the KV that actually participates in the computation is determined bycmp_ratio,ori_mask_mode,cmp_mask_mode,ori_win_left,ori_win_right, andcmp_sparse_indices.O=softmax(Q@K~T⋅softmax_scale)@V~O = \text{softmax}(Q@\tilde{K}^T \cdot \text{softmax\_scale})@\tilde{V}
Parameter Description
| Parameter Name | Input/Output/Attribute | Description | Data Type | Data Format |
|---|---|---|---|---|
| q | Input | Represents Q in the formula. | FLOAT16, BFLOAT16 | ND |
| ori_kv | Optional input | Represents part of K and V in the formula. It is the original uncompressed quantized KV, where Key and Value share the same data. It is concatenated from nope, rope, scale, and padding. For details, see quant_mode. The default value is None. | FLOAT8_E4M3FN, FLOAT16, BFLOAT16 | ND |
| cmp_kv | Optional input | Represents part of K and V in the formula. It is the compressed quantized KV, where Key and Value share the same data. It is concatenated from nope, rope, scale, and padding. For details, see quant_mode. The default value is None. | FLOAT8_E4M3FN, UINT8 | ND |
| ori_sparse_indices | Optional input | Represents the topK indexes of the original KV. Invalid positions are filled with -1. The default value is None. | INT32 | ND |
| cmp_sparse_indices | Optional input | Represents the topK indexes of the compressed KV. Invalid positions are filled with -1. The default value is None. | INT32 | ND |
| ori_block_table | Optional input | Represents the block mapping table used by ori_kv in the PageAttention scenario. The default value is None. | INT32 | ND |
| cmp_block_table | Optional input | Represents the block mapping table used by cmp_kv in the PageAttention scenario. The default value is None. | INT32 | ND |
| cu_seqlens_q | Optional input | Represents the cumulative sequence lengths of q in different batches under the TND layout. The default value is None. | INT32 | ND |
| cu_seqlens_ori_kv | Optional input | Represents the cumulative sequence lengths of ori_kv in different batches under the TND layout. The default value is None. | INT32 | ND |
| cu_seqlens_cmp_kv | Optional input | Represents the cumulative sequence lengths of cmp_kv in different batches under the TND layout. The default value is None. | INT32 | ND |
| seqused_q | Optional input | Represents the number of tokens of q that actually participate in the computation in each batch. The default value is None. | INT32 | ND |
| seqused_ori_kv | Optional input | Represents the number of tokens of ori_kv that actually participate in the computation in each batch. The default value is None. | INT32 | ND |
| seqused_cmp_kv | Optional input | Represents the number of tokens of cmp_kv that actually participate in the computation in each batch. The default value is None. | INT32 | ND |
| cmp_residual_kv | Optional input | Represents the remainder of the compressed KV, used to restore the pre-compression KV length used by the mask on the cmp side. The default value is None. | INT32 | ND |
| ori_topk_length | Optional input | Represents the length of ori_sparse_indices that actually participates in the computation. The default value is None. | INT32 | ND |
| cmp_topk_length | Optional input | Represents the length of cmp_sparse_indices that actually participates in the computation. The default value is None. | INT32 | ND |
| sinks | Optional input | Represents the independent learnable virtual offset items set for each attention head, used to maintain stability during long-text inference. The default value is None. | FLOAT | ND |
| metadata | Optional input | Represents the core partitioning information generated by MixedQuantSparseFlashMlaMetadata. The default value is None. | INT32 | ND |
| quant_mode | Attribute | Represents the quantization mode. Supports 1, 2, and 3. Q is not quantized.
|
INT | - |
| rope_head_dim | Optional attribute | Represents the dimension of the rope head. Only 64 is supported. | INT | - |
| softmax_scale | Optional attribute | Represents softmax_scale in the formula. | FLOAT | - |
| cmp_ratio | Optional attribute | Represents the compression ratio of cmp_kv relative to the pre-compression KV length, used to restore the pre-compression KV length used by the mask on the cmp side. When only ori_kv is passed in, this parameter does not participate in the compressed KV computation. Supports 1 to 128. | INT | - |
| ori_mask_mode | Optional attribute | Represents the mask mode for the computation between q and ori_kv. 0: No mask. 3: rightDownCausal mode. 4: sliding window mode. The default value is 0. |
INT | - |
| cmp_mask_mode | Optional attribute | Represents the mask mode for the computation between q and cmp_kv. 0: No mask. 3: rightDownCausal mode. The default value is 0. |
INT | - |
| ori_win_left | Optional attribute | Represents the number of historical tokens computed for each q in the computation between q and ori_kv. -1 indicates infinity, that is, all tokens participate in the computation. The default value is -1. | INT | - |
| ori_win_right | Optional attribute | Represents the number of future tokens computed for each q in the computation between q and ori_kv. -1 indicates infinity, that is, all tokens participate in the computation. The default value is -1. | INT | - |
| layout_q | Optional attribute | Represents the data layout format of the input q. Supports "BSND" and "TND". The default value is "BSND". | STRING | - |
| layout_kv | Optional attribute | Represents the data layout format of the input ori_kv and cmp_kv. Supports "BSND", "TND", and "PA_BBND". The default value is "BSND". | STRING | - |
| topk_value_mode | Optional attribute | Represents the value mode of the topK indexes. The default value is 1. When the value is 1, sparse indexes represent logical token indexes. When the value is 2, sparse indexes under the PA_BBND layout represent physical token offsets, that is, physical_block_id * block_size + offset_in_block, where physical_block_id is the actual physical block ID and offset_in_block is the offset within the actual physical block. | INT | - |
| return_softmax_lse | Optional attribute | Represents whether to return the lse result of softmax. The default value is False. | BOOL | - |
| attn_out | Output | Represents the output O in the formula. | FLOAT16, BFLOAT16 | ND |
| softmax_lse | Optional output | Represents the result obtained as follows: first take the max of the result of query multiplied by key to obtain softmax_max, then subtract softmax_max from the result of query multiplied by key, take exp and then sum to obtain softmax_sum, and finally take the log of softmax_sum and add softmax_max. By default, no valid result is returned (return_softmax_lse=False). When the value is True, valid values are output. | FLOAT | ND |
Constraints
- Ascend 950PR&950DT products: Only
quant_mode=1/2is supported. q and attn_out support only BFLOAT16, and ori_kv and cmp_kv support only FLOAT8_E4M3FN. When quant_mode is 1 or 2, kv_d is 608 or 584 respectively. When quant_mode is 2, layout_kv supports only PA_BBND, and topk_value_mode supports 1 and 2.
- Atlas A3 products: Only the TurboQuant CSA scenario of
quant_mode=3is supported. q, ori_kv, and attn_out support FLOAT16 and BFLOAT16, and their data types must be the same. The last dimension of ori_kv is 512. cmp_kv supports only UINT8 and its last dimension is 258. topk_value_mode supports only 1.
- Atlas A2 products: Only the TurboQuant CSA scenario of
quant_mode=3is supported. q, ori_kv, and attn_out support FLOAT16 and BFLOAT16, and their data types must be the same. The last dimension of ori_kv is 512. cmp_kv supports only UINT8 and its last dimension is 258. topk_value_mode supports only 1.
- This API is used in inference scenarios.
- This API supports the aclgraph mode.
- This API supports batch consistency.
- This API currently supports three computation scenarios: in the SWA (Sliding Window Attention) scenario, only
ori_kvis passed in; in the CSA (Compressed Sparse Attention) scenario,ori_kv,cmp_kv, andcmp_sparse_indicesare passed in; in the HCA (Heavily Compressed Attention) scenario,ori_kvandcmp_kvare passed in.
Common Field Definitions
| Name | Description |
|---|---|
| b | Batch size of input samples |
| q_s | Sequence length of the input q |
| ori_kv_s | Sequence length of the input ori_kv |
| cmp_kv_s | Sequence length of the input cmp_kv |
| q_n | Number of heads of the input q |
| kv_n | Number of heads of the input ori_kv/cmp_kv |
| q_d | Dimension of the attention head of the input q |
| kv_d | Dimension of the attention head of the input ori_kv/cmp_kv |
| q_t | Cumulative sum of the sequence lengths of all batches of the input q |
| ori_kv_t | Cumulative sum of the sequence lengths of all batches of the input ori_kv |
| cmp_kv_t | Cumulative sum of the sequence lengths of all batches of the input cmp_kv |
| ori_kv_k | Number of tokens selected by topK in the input ori_sparse_indices |
| cmp_kv_k | Number of tokens selected by topK in the input cmp_sparse_indices |
| ori_kv_s_max | Maximum sequence length of the input ori_kv |
| cmp_kv_s_max | Maximum sequence length of the input cmp_kv |
| ori_kv_block_size | Block size of the input ori_kv in the PagedAttention scenario |
| cmp_kv_block_size | Block size of the input cmp_kv in the PagedAttention scenario |
| ori_kv_block_nums | Number of blocks of the input ori_kv in the PagedAttention scenario |
| cmp_kv_block_nums | Number of blocks of the input cmp_kv in the PagedAttention scenario |
- The general specification constraints are as follows:
- kv_n supports only 1, and q_d supports only 512. The kv_d of
ori_kvandcmp_kvis concatenated from nope, rope, scale, and padding. For details, seequant_mode. cmp_ratioindicates the compression ratio ofcmp_kvrelative to the pre-compression KV length. When onlyori_kvis passed in,cmp_ratiodoes not participate in the compressed KV computation and must remain 1. Supports 1 to 128.ori_mask_modesupports 0, 3, and 4, andcmp_mask_modesupports 0 and 3.ori_win_leftandori_win_rightsupport -1 or non-negative values, where -1 indicates that the corresponding direction is unrestricted. Only whenori_mask_modeis 4,ori_win_leftandori_win_rightcan be >= 0.rope_head_dimsupports only 64.- The combinations of
layout_qandlayout_kvsupport only "BSND"/"BSND", "TND"/"TND", "BSND"/"PA_BBND", and "TND"/"PA_BBND". In non-PA_BBND scenarios,layout_qandlayout_kvmust be the same. In PA_BBND scenarios,block_sizesupports 1 to 1024.
- kv_n supports only 1, and q_d supports only 512. The kv_d of
- When
layout_qis TND, the function usage restrictions are as follows:- The shape of
qmust be [q_t, q_n, q_d]. - The shape of
ori_sparse_indicesmust be [q_t, kv_n, ori_kv_k]. - The shape of
cmp_sparse_indicesmust be [q_t, kv_n, cmp_kv_k]. cu_seqlens_qmust be passed in. The input dimension is b+1, and the value of each element indicates the total number of tokens in the current batch and all previous batches, that is, the prefix sum. Therefore, the value of each subsequent element must be >= the value of the previous element, and the first element must be 0.
- The shape of
- When
layout_qis BSND, the function usage restrictions are as follows:- The shape of
qmust be [b, q_s, q_n, q_d]. - The shape of
cmp_sparse_indicesmust be [b, q_s, kv_n, ori_kv_k]. - The shape of
cmp_sparse_indicesmust be [b, q_s, kv_n, cmp_kv_k].
- The shape of
- In the PageAttention scenario, the function usage restrictions are as follows:
- The shapes of
ori_kvandcmp_kvare [ori_kv_block_nums, ori_kv_block_size, kv_n, kv_d] and [cmp_kv_block_nums, cmp_kv_block_size, kv_n, kv_d] respectively, where ori_kv_block_nums and cmp_kv_block_nums are the numbers of blocks in the PagedAttention scenario, and ori_kv_block_size and cmp_kv_block_size are the numbers of tokens in a block, ranging from 1 to 1024. - The shapes of
ori_block_tableandcmp_block_tableare 2D, where the length of the first dimension is b, and the length of the second dimension is not smaller than the number of blocks corresponding to the maximum ori_kv_s and cmp_kv_s across all batches, that is, ori_kv_s_max / ori_kv_block_size and cmp_kv_s_max / cmp_kv_block_size rounded up.
- The shapes of
metadatais the core partitioning result actually used by the operator. Currently, this parameter is mandatory, and its shape size is fixed to [2048].layout_kvsupports "BSND", "TND", and "PA_BBND" as inputs, and must satisfy the preceding combination constraints oflayout_qandlayout_kv.- When the input is PA_BBND,
seqused_ori_kvandori_block_tablemust be passed in. When the input is BSND,seqused_ori_kvcan be used to express the valid length ofori_kvin each batch. When the input is TND, the valid length ofori_kvis expressed bycu_seqlens_ori_kv. - When the input is BSND, the layouts of both
ori_kvandcmp_kvmust be BSND. The shape of ori_kv is [b, ori_kv_s, kv_n, kv_d], and the shape of cmp_kv is [b, cmp_kv_s, kv_n, kv_d]. - When the input is TND,
cu_seqlens_ori_kvmust be passed in. Ifcmp_kvexists,cu_seqlens_cmp_kvmust also be passed in.
- When the input is PA_BBND,
- When
return_softmax_lseis False, a placeholder Tensor is returned. When it is True, the log-sum-exp result of softmax is returned. - Reserved inputs such as
ori_topk_lengthandcmp_topk_lengthmay not be passed in or may be passed in as empty Tensors. Other passed-in Tensors cannot be empty. seqused_cmp_kvis an optional input for alllayout_kvvalues. When explicitly passed in, it is used to overwrite the logical valid length on the cmp side. When not passed in, it is derived from thecmp_kvshape,cu_seqlens_cmp_kv, or the semantics related to the PA block table.cmp_residual_kvis an optional input parameter of the operator. After it is passed in, it is used to restore the pre-compression KV length used by the mask on the cmp side according tocmp_len * cmp_ratio + residual, wherecmp_lenpreferentially comes from the explicitly passed-inseqused_cmp_kv.
Calling Instructions
| Calling Method | Sample Code | Description |
|---|---|---|
| aclnn API | test_aclnn_mixed_quant_sparse_flash_mla | Calls the MixedQuantSparseFlashMla operator through aclnnMixedQuantSparseFlashMla |
| PyTorch API | mixed_quant_sparse_flash_mla | Calls the MixedQuantSparseFlashMla operator through cann_ops_transformer.mixed_quant_sparse_flash_mla |
TurboQuant Parameter Constraints
The following constraints apply to the quant_mode=3 scenario of Atlas A2 products and Atlas A3 products. The following fields are all existing parameters, and no new positional parameters are added. The newly added Tensor data types and layouts take effect only when quant_mode=3.
| Parameter | quant_mode=3 Constraint |
|---|---|
quant_mode |
3 |
q, attn_out |
FLOAT16 or BFLOAT16, with the same type and shape. TND layout, with shape (q_t, q_n, 512). q_n is a multiple of 4 from 4 to 128, and q_t can be 0. |
ori_kv |
Mandatory. For details about the data type, layout, and shape, see the description of quant_mode. |
cmp_kv |
Mandatory. For details about the data type, layout, and shape, see the description of quant_mode. |
layout_q, layout_kv |
TND and PA_BBND respectively. The block_size of both KV paths is a multiple of 16 from 16 to 1024, and the stride of axis 0 must cover a complete physical block. |
cmp_sparse_indices |
Mandatory INT32, with shape (q_t, 1, 512) or (q_t, 1, 1024). Invalid positions are filled with -1. |
ori_block_table, cmp_block_table, cu_seqlens_q, seqused_ori_kv |
All mandatory. The original INT32 type and parameter meanings are retained. |
ori_sparse_indices, ori_topk_length, cmp_topk_length, cu_seqlens_ori_kv, cu_seqlens_cmp_kv, seqused_q, seqused_cmp_kv, cmp_residual_kv |
Passing in is not supported. The original optional parameter positions are retained. |
ori_mask_mode, cmp_mask_mode, ori_win_left, ori_win_right |
4, 3, a non-negative value, and 0 respectively. |
cmp_ratio, rope_head_dim, topk_value_mode |
Support 4 or 128, 64, and 1 respectively. The supported range of the compression ratio of the operator does not mean that the framework quantizes all compressed KV. |
metadata |
Mandatory. It is generated by the preceding Metadata API with the same quant_mode=3 and the same input attributes. INT32, with shape (2048,). |
sinks and Softmax LSE continue to use the original parameters and return forms.
The parameter validation conditions of quant_mode=1 and quant_mode=2 of Ascend 950PR&950DT products remain unchanged.