| Files | Last commit | Last update |
|---|---|---|
| 14 days ago | ||
| 13 days ago | ||
| 15 days ago | ||
| 10 days ago | ||
| 7 days ago | ||
| 30 days ago | ||
| 1 month ago | ||
| 22 days ago | ||
| 7 days ago |
SparseFlashMla
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 | × |
Description
-
Operator function:
The
SparseFlashMlaoperator performs the Attention computation described by the following formula and supports three types of Attention computation scenarios: SWA (Sliding Window Attention), CSA (Compressed Sparse Attention), and HCA (Heavily Compressed Attention). The invocation requires the task listmetadatagenerated bySparseFlashMlaMetadata.The typical invocation flow is as follows:
- Prepare inputs such as
q,ori_kv,cmp_kv, sequence lengths,block table, andsinks. - Call
SparseFlashMlaMetadatato generatemetadata. - Call
SparseFlashMlaand pass themetadataobtained in the previous step to the main operator.
- Prepare inputs such as
-
Formula:
O=softmax(Q@K~T⋅softmax_scale)@V~O = \text{softmax}(Q@\tilde{K}^T \cdot \text{softmax\_scale})@\tilde{V}
Here, K~=V~\tilde{K}=\tilde{V} is the KVKV that actually participates in the computation, controlled by input parameters such as ori_kv, cmp_kv, and cmp_ratio.
Parameters
Description:
Parameter dimension meanings: B denotes Batch Size. Q_S, ORI_KV_S, and CMP_KV_S denote the Sequence Length of q, ori_kv, and cmp_kv, respectively. Q_N and KV_N denote the Head Num of q and ori_kv/cmp_kv, respectively. Q_T, ORI_KV_T, and CMP_KV_T denote the Total Tokens of q, ori_kv, and cmp_kv, respectively.
| Parameter Name | Input/Output/Attribute | Description | Data Type | Data Format |
|---|---|---|---|---|
| q | Input | Corresponds to Q in the formula. | BFLOAT16, FLOAT16 | ND |
| ori_kv | Optional input | Part of K and V in the formula, indicating the original KV without compression. Default value: None. | BFLOAT16, FLOAT16 | ND |
| cmp_kv | Optional input | Part of K and V in the formula, indicating the compressed KV. Default value: None. | BFLOAT16, FLOAT16 | ND |
| ori_sparse_indices | Optional input | In the SWA sparse ori_kv scenario, indicates the logical indices for sparsely fetching data from ori_kv. -1 indicates an invalid or padded slot. The shape is (T1, N2, K) or (B, S1, N2, K). Default value: None. | INT32 | ND |
| cmp_sparse_indices | Optional input | Indicates the indices for sparsely fetching data from cmp_kv. Default value: None. | INT32 | ND |
| ori_block_table | Optional input | Indicates the block mapping table used by ori_kv in PageAttention. Default value: None. | INT32 | ND |
| cmp_block_table | Optional input | Indicates the block mapping table used by cmp_kv in PageAttention. Default value: None. | INT32 | ND |
| cu_seqlens_q | Optional input | Indicates the cumulative sequence lengths of q in different batches under the TND layout. Default value: None. | INT32 | ND |
| cu_seqlens_ori_kv | Optional input | Indicates the cumulative sequence lengths of ori_kv in different batches under the TND layout. Default value: None. | INT32 | ND |
| cu_seqlens_cmp_kv | Optional input | Indicates the cumulative sequence lengths of cmp_kv in different batches under the TND layout. Default value: None. | INT32 | ND |
| seqused_q | Optional input | Indicates the number of tokens of q that actually participate in the computation in different batches. Default value: None. | INT32 | ND |
| seqused_ori_kv | Optional input | Indicates the number of tokens of ori_kv that actually participate in the computation in different batches. Default value: None. | INT32 | ND |
| seqused_cmp_kv | Optional input | Indicates the number of tokens of cmp_kv that actually participate in the computation in different batches. Default value: None. | INT32 | ND |
| cmp_residual_kv | Optional input | Indicates the compression KV remainder, used to restore the pre-compression KV length used by the cmp-side mask. Default value: None. | INT32 | ND |
| ori_topk_length | Optional input | In the SWA sparse ori_kv scenario, indicates the actual number of critical sparse ori_kv tokens corresponding to each q token. The shape is (T1, N2) or (B, S1, N2). Default value: None. | INT32 | ND |
| cmp_topk_length | Optional input | In the CSA scenario of the Ascend 950PR/950DT series, indicates the number of valid entries in cmp_sparse_indices corresponding to each q token and KV head. This parameter is mandatory when cmp_mask_mode=0 and cmp_sparse_indices is passed in. The shape is (T1, 1) under TND and (B, S1, 1) under BSND, consistent with cmp_sparse_indices after removing the last dimension K2. Atlas A2/A3 do not support passing in a non-empty Tensor. Default value: None. | INT32 | ND |
| sinks | Optional input | Indicates the attention sinks input. Default value: None. | FLOAT | ND |
| metadata | Optional input | Task splitting result generated by the accompanying metadata front-end API. Default value: None. | INT32 | ND |
| softmax_scale | Optional attribute | Corresponds to softmax_scale in the formula. Default value: 1.0. | FLOAT | - |
| cmp_ratio | Optional attribute | Indicates the compression ratio of cmp_kv relative to the pre-compression KV length, used to restore the pre-compression KV length used by the cmp-side mask. When only ori_kv is passed in, it does not participate in the compressed KV computation. Values from 1 to 128 are supported. Default value: 1. | INT | - |
| ori_mask_mode | Optional attribute | Indicates the mask mode for the computation of q and ori_kv. 0: No Mask. 3: RightDownCausal mode. 4: Band mode. Default value: 0. |
INT | - |
| cmp_mask_mode | Optional attribute | Indicates the mask mode for the computation of q and cmp_kv. 0: No Mask. 3: RightDownCausal mode. Default value: 0. |
INT | - |
| ori_win_left | Optional attribute | Indicates the number of past tokens with which q is computed in the computation of q and ori_kv. Both -1 and non-negative numbers are supported, where -1 indicates that the window is unlimited. Default value: -1. | INT | - |
| ori_win_right | Optional attribute | Indicates the number of future tokens with which q is computed in the computation of q and ori_kv. Both -1 and non-negative numbers are supported, where -1 indicates that the window is unlimited. Default value: -1. | INT | - |
| layout_q | Optional attribute | Indicates the data layout format of the input q. "BSND" and "TND" are supported. Default value: "BSND". | STRING | - |
| layout_kv | Optional attribute | Indicates the data layout format of the input ori_kv and cmp_kv. "BSND", "TND", and "PA_BBND" are supported. Default value: "BSND". | STRING | - |
| topk_value_mode | Optional attribute | Indicates the TopK index value mode. Default value: 1. When the value is 1, sparse indices represent logical token indices. When the value is 2, sparse indices in the PA_BBND layout represent physical token offsets, that is, physical_block_id * block_size + offset_in_block. | INT | - |
| return_softmax_lse | Optional attribute | Indicates whether to return softmax_lse. Default value: False. | BOOL | - |
| attn_out | Output | Corresponds to the output O in the formula. | BFLOAT16, FLOAT16 | ND |
| softmax_lse | Optional output | Returns the log-sum-exp result of softmax. By default, no valid result is returned (return_softmax_lse=False). When the value is True, valid values are output. | FLOAT | ND |
Constraints
-
This API can be used in training and inference scenarios.
-
This API supports aclgraph mode.
-
This API supports batch consistency.
-
This API currently supports four computation scenarios: the SWA (Sliding Window Attention) scenario passes in only
ori_kv; the SWA sparse ori_kv scenario passes inori_kv,ori_sparse_indices, andori_topk_length; the CSA (Compressed Sparse Attention) scenario passes inori_kv,cmp_kv, andcmp_sparse_indices; the HCA (Heavily Compressed Attention) scenario passes inori_kvandcmp_kv. -
The general specification constraints are as follows:
- Only KV_N of 1 is supported, and only D of 512 is supported. The D_kv of
ori_kvandcmp_kvis formed by concatenating nope(448) and rope(64). 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 the default value 1. Values from 1 to 128 are supported.- The values of
ori_mask_mode,cmp_mask_mode,ori_win_left, andori_win_rightvary by product model. For details, see the "Product Model Constraints" section. - The block_size of PageAttention supports 1 to 1024.
- The
layout_qandlayout_kvcombinations support only "BSND"/"BSND", "TND"/"TND", "BSND"/"PA_BBND", and "TND"/"PA_BBND". In non-PA_BBND scenarios,layout_qandlayout_kvmust be the same. - In the SWA sparse ori_kv scenario,
ori_topk_lengthmust be passed in. Theori_topkof the accompanying Metadata API is the last dimension K ofori_sparse_indices, and the element values ofori_topk_lengthmust be in the range [0, K]. In other scenarios, pass in nullptr or an empty Tensor forori_topk_length.
- Only KV_N of 1 is supported, and only D of 512 is supported. The D_kv of
-
The product model constraints are as follows:
- Atlas A2 products and Atlas A3 products: Q_N supports 1, 2, 4, 8, 16, 32, 64, and 128, and KV_N supports only 1. SWA does not pass in
cmp_kv, and cmp_ratio does not participate in the computation. CSA supports passing in 1, 2, or 4, and HCA supports passing in 128. block_size must be a multiple of 16, and the maximum supported value is 1024. The SWA sparse ori_kv scenario supportsori_sparse_indicesandori_topk_length;layout_qandlayout_kvsupport only the "TND"/"PA_BBND" combination;ori_mask_modeis 0;ori_win_leftandori_win_rightare non-negative numbers. In non-SWA sparse ori_kv scenarios,ori_mask_modeis 4,ori_win_leftis 127, andori_win_rightis 0. The last dimension K2 ofcmp_sparse_indicessupports any integer in [1, 8192];cmp_mask_modesupports only 3, and topk_value_mode supports only 1. - Ascend 950PR&950DT products: Q_N supports 1-128, and KV_N supports only 1. When only
ori_kvis passed in,cmp_ratiomust be 1. The CSA/HCA scenarios support 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 numbers, where -1 indicates that the corresponding direction is unlimited. Only whenori_mask_modeis 4,ori_win_leftandori_win_rightcan be >=0. topk_value_mode supports 1 and 2.
- Atlas A2 products and Atlas A3 products: Q_N supports 1, 2, 4, 8, 16, 32, 64, and 128, and KV_N supports only 1. SWA does not pass in
-
When
layout_qis TND, the function usage restrictions are as follows:- The shape of
qmust be [Q_T, Q_N, D]. - In the SWA sparse ori_kv scenario, the shape of
ori_sparse_indicesis [Q_T, KV_N, K], and the shape ofori_topk_lengthis [Q_T, KV_N]. - The shape of
cmp_sparse_indicesmust be [Q_T, KV_N, K2], where K2 is the number of tokens selected in a single sparse selection fromcmp_kv. cu_seqlens_qmust be passed in, with the shape [B+1,]. The first number is fixed to 0, that is, the prefix 0. Each of the following elements indicates the total number of tokens (prefix sum) of the current batch and all preceding batches. Therefore, the value of each element must be greater than or equal to that of the preceding element.
- The shape of
-
When
layout_qis BSND, the function usage restrictions are as follows:- The shape of
qmust be [B, Q_S, Q_N, D]. - In the SWA sparse ori_kv scenario, the shape of
ori_sparse_indicesis [B, Q_S, KV_N, K], and the shape ofori_topk_lengthis [B, Q_S, KV_N]. - The shape of
cmp_sparse_indicesmust be [B, Q_S, KV_N, K2], where K2 is the number of tokens selected in a single sparse selection fromcmp_kv.
- The shape of
-
In the PageAttention scenario, the function usage restrictions are as follows:
- The shapes of
ori_kvandcmp_kvare [ori_block_num, ori_block_size, KV_N, D] and [cmp_block_num, cmp_block_size, KV_N, D], respectively, where ori_block_num and cmp_block_num are the total number of blocks in PageAttention, and ori_block_size and cmp_block_size are the number of tokens in a block. ori_block_size and cmp_block_size support values from 1 to 1024, and only KV_N of 1 is supported. - The shapes of
ori_block_tableandcmp_block_tableare 2-dimensional. The length of the first dimension is B, and the length of the second dimension must not be less than the number of blocks corresponding to the maximum S2 and S3 among all batches, that is, the ceiling of ORI_KV_S_max / block_size and CMP_KV_S_max / block_size.
- The shapes of
-
metadatais the core splitting result that the operator actually needs to use. This parameter is currently mandatory, and the shape size is fixed to [2048]. -
layout_kvsupports the input "BSND", "TND", and "PA_BBND" values, which must satisfy the precedinglayout_qandlayout_kvcombination constraints.- 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, and the valid length must not exceed KV_S of key/value and must not be less than 0. When the input is TND, the total length ofori_kvis expressed bycu_seqlens_ori_kv. Ifseqused_ori_kvis passed in, the valid length ofori_kvis expressed byseqused_ori_kv; otherwise, the valid length is the same as the total length. - 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, D], and the shape of cmp_kv is [B, CMP_KV_S, KV_N, 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. -
The SWA sparse ori_kv scenario supports only the SWA template. Only
ori_kvis passed in, andori_sparse_indicesandori_topk_lengthmust be passed in together.ori_win_leftandori_win_righttake non-negative numbers only whenori_mask_modeis 4. Theori_topkof the accompanying Metadata API is K, andcmp_kvis not passed in this scenario. -
ori_topk_lengthindicates the actual number of valid index entries for each q token and KV head, and the values must be in the range [0, K]. Inori_sparse_indices, the range [0, ori_topk_length) contains left-aligned valid index entries, and the range [ori_topk_length, K) contains invalid or padded entries. It is recommended to fill in -1. -
For unused reserved inputs, they can be omitted or passed in as empty Tensors. Other passed-in Tensors do not support being 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 it is not passed in, the length is derived from thecmp_kvshape,cu_seqlens_cmp_kv, or the PA block table semantics. -
cmp_residual_kvis an optional input parameter of both the main API and the metadata front-end API. After it is passed in, it is used to restore the pre-compression KV length used by the cmp-side mask ascmp_len * cmp_ratio + residual, wherecmp_lenpreferentially comes from the explicitly passed-inseqused_cmp_kv. -
The data layout formats of
q,ori_kv, andcmp_kvcan be interpreted from multiple dimensions. B (Batch) indicates the batch size of the input samples. S (Seq-Length) indicates the sequence length of the input samples. H (Hidden-Size) indicates the size of the hidden layer. N (Head-Num) indicates the number of heads. D (Head-Dim) indicates the smallest unit size of the hidden layer, satisfying D=H/N. T indicates the cumulative sum of the input sample sequence lengths of all batches. -
Q_S indicates the S in the q shape. ORI_KV_S indicates the S in the ori_kv shape. CMP_KV_S indicates the S in the cmp_kv shape. Q_N indicates num_q_heads. KV_N indicates num_ori_kv_heads and num_cmp_kv_heads. Q_T indicates the cumulative sum of the input sample sequence lengths in the q shape.
Invocation Description
| Invocation Method | Sample Code | Description |
|---|---|---|
| aclnn API | test_aclnnSparseFlashMla | Calls the SparseFlashMla operator through aclnnSparseFlashMla |
| PyTorch API | sparse_flash_mla | Calls the SparseFlashMla operator through cann_ops_transformer.sparse_flash_mla |