[object Object][object Object][object Object]undefined
[object Object]
  • API function: Computes the attention backpropagation output in training scenarios, which is the backpropagation of .

  • Formula:

    The forward propagation formula for attention is as follows:

    Y=Dropout(Softmax(Mask(QKT+psed),atten_mask),keep_prob)VY=Dropout(Softmax(Mask(\frac{QK^T+pse}{\sqrt{d}}),atten\_mask),keep\_prob)V

    For convenience, the formula can be represented using variables SS and PP:

    S=Mask(QKT+psed,atten_mask)S=Mask(\frac{QK^T+pse}{\sqrt{d}},atten\_mask) P=Dropout(Softmax(S),keep_prob)P=Dropout(Softmax(S),keep\_prob) Y=PVY=PV

    Then the backward propagation formula for attention is as follows:

    dV=PTdYdV=P^TdY dQ=((dS)K)ddQ=\frac{((dS)*K)}{\sqrt{d}} dK=((dS)TQ)ddK=\frac{((dS)^T*Q)}{\sqrt{d}}
[object Object]

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.

[object Object]
[object Object]
[object Object]
  • Parameters

    [object Object]
  • Returns:

    [object Object]: status code. For details, see .

    The first-phase API implements input parameter verification. The following errors may be thrown.

    [object Object]
[object Object]
  • Parameters

    [object Object]
  • Returns:

    [object Object]: status code. For details, see .

[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.
  • D (Head-Dim) of the input [object Object], [object Object], and [object Object] must satisfy (qD == kD && kD >= vD).
  • The [object Object] of the input [object Object], [object Object], [object Object], and [object Object] must be the same.
  • 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. 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] or [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. The value of Nq ranges from 1 to 256.
  • You need to pay attention to constraints of the data shape. The following takes the [object Object] values BSND and BNSD as examples to describe the constraints (H = N*D in BSH and SBH):
    • B: The value ranges from 1 to 2M. When [object Object] is passed, B supports a maximum of 2K.
    • N: The value ranges from 1 to 256.
    • S: The value ranges from 1 to 1M.
    • D: The value ranges from 1 to 768.
  • The data format of [object Object], [object Object], and [object Object] can be interpreted from multiple dimensions. To be specific, B (Batch) indicates the size of an input sample batch, 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, and D (Head-Dim) indicates the minimum unit size of the hidden layer (D=H/N).
  • pseShiftOptional: If Sq is greater than 1024, the length of Sq in each batch is the same as that of Skv, and the sparseMode is 0, 2, or 3, the 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, alibi_compress = ori_pse[:, :, -1024:, :]. The details are as follows:
    • 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 this parameter is not used, a null pointer can be passed.
  • [object Object]: 0 and 1 are reserved, and 2 indicates that invalid row calculation is enabled. This function is used to prevent precision loss caused by the mask of the entire row during calculation. However, this configuration deteriorates the performance. If the operator can determine that invalid rows exist, the invalid row computation is automatically enabled, such as in scenarios where [object Object] is set to 3 and Sq is greater than Skv.
  • 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 .
  • 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.
  • 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].
  • In the band scenario, the values of [object Object] and [object Object] must overlap.
  • If Sq in [object Object] is greater than 1024 and the shape value is BNHS or 1NHS, Sq and Skv must have the same length.
  • The [object Object] sparse computing scenario is [object Object] or [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]

The following example is for reference only. For details, see .

[object Object]