API function: Computes the attention backpropagation output in training scenarios, which is the backpropagation of . Compared with the API, this API has a
[object Object]parameter.Formula:
The forward propagation formula for attention is as follows: (same as that of when
[object Object]is set to 1)For convenience, the formula can be represented using variables and :
Then the backward propagation formula for attention is as follows:
Each operator has calls. First, [object Object] is called to obtain the workspace size required for computation and the executor that contains the operator computation process. Then, [object Object] is called to perform computation.
Parameters
[object Object]Returns
aclnnStatus: status code. For details, see .
The first-phase API implements input parameter verification. The following errors may be thrown.
[object Object]
Deterministic computing:
[object Object]defaults to a non-deterministic implementation. You can call[object Object]to enable deterministic computing.
When this API is used together with PyTorch, ensure that the CANN package versions match the PyTorch package versions.
B (batch size) of the input
[object Object],[object Object],[object Object], and[object Object]must be the same.[object Object]must be consistent.D (Head-Dim) of the input
[object Object],[object Object], and[object Object]must satisfy (qD == kD && kD >= vD).The input data types of
[object Object],[object Object],[object Object], and[object Object]must be the same.The shapes of the input
[object Object]and[object Object]must be the same except for D. If the D values of[object Object],[object Object], and[object Object]are the same, the shapes of[object Object]and[object Object]must be the same.N of the input
[object Object]can be different from N of the[object Object]or[object Object], but they must be proportional. That is, Nq/Nkv must be a non-zero integer and the value of Nq ranges from 1 to 256.The following uses the
[object Object]TND as an example to describe the constraints on the shape:- T: The value ranges from 1 to 1M.
- N: The value ranges from 1 to 256.
- D: The value ranges from 1 to 768.
- KeepProb: The value range is (0, 1].
The data format of
[object Object],[object Object], and[object Object]can only be TND. T indicates the data closely arranged on the B and S axes (SeqLenQ and SeqLenKV of each batch). B (Batch) indicates the batch size of the input sample, and S (Seq-Length) indicates the length of the input sample sequence. H (Head-Size) indicates the size of the hidden layer, N (Head-Num) indicates the number of heads. D (Head-Dim) indicates the minimum unit size of the hidden layer (D = H/N).[object Object]: If Sq is greater than 1024, Sq and Skv of each batch are of equal length, and it is a lower triangular mask scenario with[object Object]being 0, 2, or 3, ALiBi positional encoding compression can be enabled. In this case, only the last 1024 rows of the original PSE need to be input for memory optimization, that is,[object Object]. Specifically:- If the parameters of each batch are different, the shape is BNHSkv (H=1024).
- When each batch is the same, the shape is 1NHSkv (H=1024).
- If
[object Object]is 2 or 3, the data type must be FLOAT32, and the supported shapes are [B,N] and [N]. - If this parameter is not enabled, pass a null pointer to
[object Object]and 1 to[object Object].
Meanings of
[object Object]values:[object Object]undefined
The constraints for
[object Object]are as follows:- If the shape values of all
[object Object]are the same and less than 2048, you are advised to use the default mode to reduce memory usage. - When the value is set to 1, 2, 3, or 5, the user-configured
[object Object]and[object Object]do not take effect. - When the value is set to 0 or 4, ensure that the ranges of
[object Object],[object Object], and[object Object]are consistent. - If no specific value is required, you are advised to set it to 0.
- For details about the sparse modes, see .
- When the value is set to 7,
[object Object]is not supported. - When the value is set to 8,
[object Object]is supported when the q and kv of each sequence have the same length. PSE generation is performed globally. Outer splitting in the q direction is supported. q and kv of each sequence must have the same length before outer splitting, and[object Object]is passed after outer splitting.
- If the shape values of all
In some scenarios, if the computation load is too large, the operator execution may time out (AI Core error, errorStr: timeout or trap error). In this case, you are advised to perform axis splitting. Note: The computation load is affected by parameters such as B, S, N, and D. Larger values indicate larger computation loads.
The
[object Object]sparse computing supports only compression scenarios ([object Object]). When Sq > Skv, the value range of N of[object Object]is [0, Skv]. When Sq ≤ Skv, the value range of N of[object Object]is [Skv – Sq, Skv].[object Object]-[object Object]+[object Object]-[object Object]== 0 (experimental feature)The
[object Object]input supports the S length of 0 in a batch. In this case, the[object Object]input is not supported. The length of[object Object]ranges from 1 to 2K. When[object Object]is present, its maximum length is 1K.Constraints on the
[object Object]and[object Object]parameters: The input format is fixed at [B, N, S, 8], except TND format, which is [T, N, 8]. Note: T = B x S.The value of
[object Object]must be the same as the value of N in[object Object].
The following example is for reference only. For details, see .