RingMLAOperation
Applicable Products
Hardware Model |
Supported or Not |
|---|---|
Atlas 350 accelerator card |
x |
√ |
|
√ |
|
x |
|
x |
|
x |
Description
Based on the traditional MultiLatentAttention, the intermediate results lse and attention out output by the ring MLA operator are updated to global results, supporting a longer sequence length.
Definition
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | struct RingMLAParam { enum CalcType : int { CALC_TYPE_DEFAULT = 0, CALC_TYPE_FISRT_RING, CALC_TYPE_MAX }; enum KernelType : int { KERNELTYPE_DEFAULT = 0, KERNELTYPE_HIGH_PRECISION }; enum MaskType : int { NO_MASK = 0, MASK_TYPE_TRIU, }; CalcType calcType = CalcType::CALC_TYPE_DEFAULT; int32_t headNum = 0; int32_t kvHeadNum = 0; float qkScale = 1; KernelType kernelType = KERNELTYPE_HIGH_PRECISION; MaskType maskType = MASK_TYPE_TRIU; InputLayout inputLayout = TYPE_BSND; uint8_t rsv[64] = {0}; }; |
Parameters
Member |
Type |
Default Value |
Value Range |
Mandatory or Not |
Description |
|---|---|---|---|---|---|
calcType |
CalcType |
CALC_TYPE_DEFAULT |
CALC_TYPE_DEFAULT CALC_TYPE_FISRT_RING |
Yes |
Computation type
|
headNum |
int32_t |
0 |
Greater than 0 |
Yes |
Query head size, which must be greater than 0. |
kvHeadNum |
int32_t |
0 |
Greater than or equal to 0 |
Yes |
Number of KV heads. The value is determined based on the actual model.
|
qkScale |
float |
1 |
- |
Yes |
Tor value of the operator, which is post-multiplied by Q*K^T. |
kernelType |
KernelType |
KERNELTYPE_HIGH_PRECISION |
KERNELTYPE_HIGH_PRECISION |
Yes |
Kernel precision type KERNELTYPE_HIGH_PRECISION: The input/output tensor uses the float16/bf16 type, and softmax uses the float type. |
maskType |
MaskType |
MASK_TYPE_TRIU |
NO_MASK MASK_TYPE_TRIU |
Yes |
Mask type NO_MASK: No mask is used. MASK_TYPE_TRIU: upper triangular mask. This is the default value. |
inputLayout |
InputLayout |
TYPE_BSND |
TYPE_BSND |
Yes |
Data layout format. The default data layout format is BSND. |
rsv[64] |
uint8_t |
{0} |
[0] |
No |
Reserved |
Input
Parameter |
Dimension |
Data Type |
Format |
cpu/npu |
Description |
Application Scenarios |
|---|---|---|---|---|---|---|
query |
[qNTokens, headNum, 128] |
float16/bf16 |
ND |
npu |
Query matrix without positional encoding. |
Basic scenario |
queryRope |
[qNTokens, headNum, 64] |
float16/bf16 |
ND |
npu |
Query component with rotational positional encoding. |
Basic scenario |
key |
[kvNTokens, kvHeadNum, 128] |
float16/bf16 |
ND |
npu |
Key matrix without positional encoding. |
Basic scenario |
keyRope |
[kvNTokens, kvHeadNum, 64] |
float16/bf16 |
ND |
npu |
Key with rotational positional encoding. |
Basic scenario |
value |
[kvNTokens, kvHeadNum, 128] |
float16/bf16 |
ND |
npu |
Value matrix |
Basic scenario |
mask |
|
float16/bf16 |
ND |
npu |
|
Basic scenario |
seqLen |
[batch]/[2, batch] |
int32/uint32 |
ND |
cpu |
Sequence length.
|
Basic scenario |
prevOut |
[qNTokens, headNum, 128] |
float16/bf16 |
ND |
npu |
Previous output. |
Non-first-card scenario |
prevLse |
[headNum, qNTokens] |
float |
ND |
npu |
Result of the previous QK^T × tor. softmax, exp, sum, and log are calculated in sequence. |
Non-first-card scenario |
Output
Parameter |
Dimension |
Data Type |
Format |
cpu/npu |
Description |
Application Scenarios |
|---|---|---|---|---|---|---|
output |
[qNTokens, headNum, headSizeV] |
float16/bf16 |
ND |
npu |
It has the same address as prevOut. |
Basic scenario |
softmaxLse |
[headNum, qNTokens] |
float |
ND |
npu |
It has the same address as prevLse. |
Basic scenario |
Functions
- First-card scenario
- How to enable: calcType = CALC_TYPE_FISRT_RING
- Difference: No prevLse or prevOut is passed, and the softmaxLse output is generated.
- Non-first-card scenario
- How to enable: calcType = CALC_TYPE_DEFAULT
- Difference: prevLse and prevOut are passed, and the softmaxLse output is generated.
Constraints
- mask is used only when maskType is MASK_TYPE_TRIU.
- inputLayout supports only TYPE_BSND.
- Restrictions on 2D seqLen:
- qSeqLen is seqLen[0].
- kvSeqLen is seqLen[1].
- For each subscript i, qSeqLen[i] cannot be 0.
- If mask is used, for each subscript i, kvSeqLen[i] can be 0. The output result is the value of prevOut corresponding to seqlen in each batch. Note that kvSeqLen[0] and kvSeqLen[batch - 1] cannot be 0.
- If calcType is set to CALC_TYPE_FISRT_RING, for each subscript i, kvSeqLen[i] must be greater than or equal to qSeqLen[i].