| Files | Last commit | Last update |
|---|---|---|
| 6 days ago | ||
| 10 days ago | ||
| 6 days ago | ||
| 6 days ago | ||
| 6 days ago | ||
| 10 days ago | ||
| 1 month ago | ||
| 6 days ago | ||
| 6 days ago |
QuantLightningIndexerV2
English | 简体中文
Product Support Status
| Product | Support |
|---|---|
| Ascend 950PR&950DT products | √ |
| Atlas A3 products | √ |
| Atlas A2 products | √ |
| Atlas 200I/500 A2 inference products | × |
| Atlas inference products | × |
| Atlas training products | × |
Function Description
-
API function: In inference scenarios, QuantLightningIndexerV2 performs the preprocessing computation of sparse attention. It selects key sparse tokens and quantizes the inputs q and k to implement 8-bit storage and 8-bit computation, achieving maximum benefit.
-
Computation formula:
out=Top-k{[1]1×g@[(W@[1]1×Sk)⊙ReLU((ScaleQ@ScaleKT)⊙(QindexQuant@(KindexQuant)T))]}out = \text{Top-}k\left\{[1]_{1\times g}@\left[(W@[1]_{1\times S_{k}})\odot\text{ReLU}\left(\left(Scale_Q@Scale_K^T\right)\odot\left(Q_{index}^{Quant}@{\left(K_{index}^{Quant}\right)}^T\right)\right)\right]\right\}
The main computation process is:
- Multiply the input parameter
qof a token (QindexQuant∈Rg×dQ_{index}^{Quant}\in\R^{g\times d}) by the given contextk(KindexQuant∈RSk×dK_{index}^{Quant}\in\R^{S_{k}\times d}) to obtain the relevance. - Multiply the relevance result by the dequantization coefficients
q_descale(ScaleQScale_Q) andk_descale(ScaleKTScale_K^T) corresponding toqandk, filter invalid negative relevance signals through the activation function ReLUReLU, and obtain the relevance score vector between the current token and all preceding tokens. - After multiplying it by the weight coefficient
w(WW), select the first Top−kTop-k index values along the g direction to obtain the output outout, which serves as the input of Attention.
- Multiply the input parameter
Parameter Description
| Parameter Name | Input/Output/Attribute | Description | Data Type | Data Format |
|---|---|---|---|---|
| q | Input | QindexQuant∈Rg×dQ_{index}^{Quant}\in\R^{g\times d} in the formula, representing input Index Query; non-contiguous is not supported. | INT8, FLOAT8_e4m3fn, HIFLOAT8, FLOAT4_e2m1 | ND |
| k | Input | KindexQuant∈RSk×dK_{index}^{Quant}\in\R^{S_{k}\times d} in the formula, representing the compressed input Index Key; non-contiguity on axis 0 is supported. | INT8, FLOAT8_e4m3fn, HIFLOAT8, FLOAT4_e2m1 | ND |
| w | Input | WW in the formula, representing the weight coefficient; non-contiguous is not supported. | FLOAT16, FLOAT32 | ND |
| q_descale | Input | ScaleQScale_Q in the formula, representing the dequantization coefficient of Index Query; non-contiguous is not supported. When quant_mode is 3/5, the shape is obtained by replacing the D axis of q with (D/64, 2); when quant_mode is 4, the shape is (1,); in other scenarios, the shape is consistent with w |
FLOAT16, FLOAT32, FLOAT8_e8m0 | ND |
| k_descale | Input | ScaleKScale_K in the formula, representing the dequantization coefficient of Index Key; non-contiguity on axis 0 is supported. When quant_mode is 3/5, the shape is obtained by replacing the D axis of k with (D/64, 2); when quant_mode is 4, the shape is (1,); in other scenarios, the shape is obtained by removing the D axis of k |
FLOAT16, FLOAT32, FLOAT8_e8m0 | ND |
| cu_seqlens_q | Optional input | Must be passed in when layout_q is TND, representing the prefix sum of the number of valid tokens of q in each batch; it cannot be passed in when layout_q is BSND. The default value is None. |
INT32 | ND |
| cu_seqlens_k | Optional input | Must be passed in when layout_k is TND, representing the prefix sum of the number of valid tokens of k in each batch; it cannot be passed in when layout_k is PA_BBND or BSND. The default value is None. |
INT32 | ND |
| seqused_q | Optional input | Optionally passed in when layout_q is BSND, representing the number of valid tokens of q in each batch. The default value is None. |
INT32 | ND |
| seqused_k | Optional input | Used when layout_k is PA_BBND or BSND, representing the number of valid tokens of k in each batch. The default value is None. |
INT32 | ND |
| cmp_residual_k | Optional input | Residual length of Key in compression scenarios, which must satisfy 0 <= cmp_residual_k[i] < cmp_ratio. The default value is None. | INT32 | ND |
| block_table | Optional input | Represents the block mapping table used for KV storage in PageAttention. The default value is None. | INT32 | ND |
| output_idx_offset | Optional input | Offset of the output indices. The default value is None. | INT32 | ND |
| metadata | Optional input | Core partitioning information passed in by the QuantLightningIndexerV2Metadata operator, including the number of used cores, block size, and the start point of the data processed by each core. The default value is None. | INT32 | ND |
| quant_mode | Attribute | Identifies the quantization mode of inputs. | INT64 | - |
| max_seqlen_q | Optional attribute | Maximum sequence length of Query; the default value -1 means any possible length | INT64 | - |
| layout_q | Optional attribute | Identifies the data layout format of input q, with a default value of "BSND". |
STRING | - |
| layout_k | Optional attribute | Identifies the data layout format of input k, with a default value of "BSND". |
STRING | - |
| topk | Attribute | Represents the number of indices to retain in the topK stage, with a default value of 2048. | INT64 | - |
| mask_mode | Optional attribute | Represents the mask mode, with a default value of 0. | INT64 | - |
| cmp_ratio | Optional attribute | Used for sparse computation; represents the compression ratio of k, with a default value of 1. | INT64 | - |
| return_value | Optional attribute | Indicates whether to output sparse_values, with a default value of 0. |
INT64 | - |
| sparse_indices | Output | Output Out in the formula, representing the token index values that participate in the sparse attention computation. | INT32 | ND |
| sparse_values | Output | Value corresponding to the Indices output in the formula. When return_value is 1, the shape is consistent with sparse_indices; when return_value is 0, the shape is (0,) |
BFLOAT16 | ND |
Constraint Description
-
Ascend 950PR&950DT products:
qandksupport FLOAT8_e4m3fn whenquant_modeis 1/3, INT8 whenquant_modeis 2, HIFLOAT8 whenquant_modeis 4, and FLOAT4_e2m1 whenquant_modeis 5.q_descaleandk_descalesupport FLOAT32 whenquant_modeis 1/4, FLOAT16 whenquant_modeis 2, and FLOAT8_e8m0 whenquant_modeis 3/5.wsupports FLOAT16 whenquant_modeis 2 and FLOAT32 whenquant_modeis 1/3/4/5.- When
quant_modeis 3/5, the number of dimensions ofq_descaleandk_descaleis 1 more than that ofqandkrespectively, the prefix dimensions are consistent, and the last two dimensions are (D/64,2); D must be a multiple of 64, and each scale corresponds to 32 consecutive logical elements along the D axis. - N of
qsupports [1, 64], and N ofksupports only 1. topksupports [1, 8192].- When the passed-in parameter layout_q is TND, cu_seqlens_q must be passed in. If seqused_q is also passed in, ensure that the q length of each batch passed in by seqused_q does not exceed the q sequence length of each batch computed from cu_seqlens_q. When the q sequence length seqlen1 of a batch passed in by seqused_q is less than the q length seqlen2 computed from cu_seqlens_q, the TND Padding feature is enabled, and the sparse_indices and sparse_values of the q output for the difference between seqlen2 and seqlen1 of that batch are all set to invalid values. In some long-sequence scenarios, if too much invalid data needs to be padded, aicore execution may time out due to hardware limitations. You can compute the amount of data to pad through (seqlen2 - seqlen1) * topk, and it is recommended to keep this amount within 400 million.
-
Atlas A3 products and Atlas A2 products:
quant_modesupports only 2.qandksupport INT8 and do not support FLOAT8_e4m3fn, HIFLOAT8, or FLOAT4_e2m1.q_descaleandk_descalesupport FLOAT16 and do not support FLOAT32 or FLOAT8_e8m0.wsupports FLOAT16 and does not support FLOAT32.output_idx_offsetandreturn_valueare not supported.layout_ksupports PA_BBND and TND: when N ofqis 64, only PA_BBND is supported (seqused_kmust be passed in); when N is 24/32, PA_BBND (seqused_kmust be passed in) or TND (cu_seqlens_kmust be passed in, andseqused_kis not supported) is supported.- N of
qsupports 64, 32, and 24, and N ofksupports only 1; when N is 24/32,layout_qmust be TND,mask_modemust be 3, andcmp_ratiomust be 1; otherwise the API reports an error. - When N is 24/32, the TND Padding feature is not supported: passing in
seqused_qwhenlayout_qis TND causes the API to report an error. topksupports [1, 2048].
Invocation Example
| Invocation Method | Invocation Example | Description |
|---|---|---|
| PyTorch API | - | Call the QuantLightningIndexerV2 operator through the torch.ops.cann_ops_transformer.quant_lightning_indexer interface. |
| aclnn API | test_aclnn_quant_lightning_indexer_v2 | Call the QuantLightningIndexerV2 operator through the aclnnQuantLightningIndexerV2 two-phase interface. |