aclnnFlashAttentionUnpaddingScoreGradV5

Applicable Products

  • Ascend 950PR&950DT products: Supported
  • Atlas A3 products: Supported
  • Atlas A2 products: Supported
  • Atlas 200I/500 A2 inference products: Not supported
  • Atlas inference products: Not supported
  • Atlas training products: Not supported

Function

  • Description: Implements self-attention computation using the FlashAttention algorithm in training scenarios. Adds the sinkInOptional optional input.

    • Ascend 950PR&950DT products: The sinkInOptional parameter is not supported yet.
  • Formula:

    The forward attention formula is:

    $$ =Dropout(Softmax(Mask(\frac{QK^T+pse}{\sqrt{d}}),atten\_mask),keep\_prob)V $$

    For ease of expression, variables $S$ and $P$ are used to represent the formulas:

    $$ =Mask(\frac{QK^T+pse}{\sqrt{d}}),atten\_mask $$
    $$ =Dropout(Softmax(S),keep\_prob) $$
    $$ =PV $$

    The backward propagation formula for attention is:

    $$ V=P^TdY $$
    $$ Q=\frac{((dS)*K)}{\sqrt{d}} $$
    $$ K=\frac{((dS)^T*Q)}{\sqrt{d}} $$

The computation logic after adding sink is as follows, mainly modifying the relevant softmax_max and softmax_sum logic computation parts.

$$ S = Q @ K^{T} $$
$$ m = max(sink, max(S)) $$
$$ Attention = \frac{e^{S - m} @ V}{\sum e^{S-m} + e^{sink - m}} $$
$$ dSink = reduce(-P * dP * SimpleSoftmax(sink, x\_max, x\_sum)) $$

Function Prototype

Each operator uses a two-phase API. Call the "aclnnFlashAttentionUnpaddingScoreGradV5GetWorkspaceSize" API or the "aclnnFlashAttentionUnpaddingScoreGradV5GetMaxWorkspaceSize" API to obtain the workspace size required for computation and the executor that contains the operator computation process, and then call the "aclnnFlashAttentionUnpaddingScoreGradV5" API to perform the computation.

aclnnStatus aclnnFlashAttentionUnpaddingScoreGradV5GetWorkspaceSize(
 const aclTensor   *query,
 const aclTensor   *queryRope,
 const aclTensor   *keyIn,
 const aclTensor   *keyInRope,
 const aclTensor   *value,
 const aclTensor   *dy,
 const aclTensor   *pseShiftOptional,
 const aclTensor   *dropMaskOptional,
 const aclTensor   *paddingMaskOptional,
 const aclTensor   *attenMaskOptional,
 const aclTensor   *softmaxMaxOptional,
 const aclTensor   *softmaxSumOptional,
 const aclTensor   *softmaxInOptional,
 const aclTensor   *attentionInOptional,
 const aclTensor   *sinkInOptional,
 const aclIntArray *prefixOptional,
 const aclIntArray *actualSeqQLenOptional,
 const aclIntArray *actualSeqKvLenOptional,
 const aclIntArray *qStartIdxOptional,
 const aclIntArray *kvStartIdxOptional,
 double             scaleValue,
 double             keepProb,
 int64_t            preTokens,
 int64_t            nextTokens,
 int64_t            headNum,
 char              *inputLayout,
 int64_t            innerPrecise,
 int64_t            sparseMode,
 int64_t            pseType,
 char              *softmaxInLayout,
 const aclTensor   *dqOut,
 const aclTensor   *dqRopeOut,
 const aclTensor   *dkOut,
 const aclTensor   *dkRopeOut,
 const aclTensor   *dvOut,
 const aclTensor   *dpseOut,
 const aclTensor   *dsinkOut,
 uint64_t          *workspaceSize,
 aclOpExecutor    **executor);
aclnnStatus aclnnFlashAttentionUnpaddingScoreGradV5GetMaxWorkspaceSize(
    const aclTensor *query,
    const aclTensor *queryRope,
    const aclTensor *keyIn,
    const aclTensor *keyInRope,
    const aclTensor *value,
    const aclTensor *dy,
    const aclTensor *pseShiftOptional,
    const aclTensor *dropMaskOptional,
    const aclTensor *paddingMaskOptional,
    const aclTensor *attenMaskOptional,
    const aclTensor *softmaxMaxOptional,
    const aclTensor *softmaxSumOptional,
    const aclTensor *softmaxInOptional,
    const aclTensor *attentionInOptional,
    const aclTensor *sinkInOptional,
    const aclIntArray *prefixOptional,
    const aclIntArray *actualSeqQLenOptional,
    const aclIntArray *actualSeqKvLenOptional,
    const aclIntArray *qStartIdxOptional,
    const aclIntArray *kvStartIdxOptional,
    double scaleValue,
    double keepProb,
    int64_t preTokens,
    int64_t nextTokens,
    int64_t headNum,
    char *inputLayout,
    int64_t innerPrecise,
    int64_t sparseMode,
    int64_t pseType,
    char *softmaxInLayout,
    const aclTensor *dqOut,
    const aclTensor *dqRopeOut,
    const aclTensor *dkOut,
    const aclTensor *dkRopeOut,
    const aclTensor *dvOut,
    const aclTensor *dpseOut,
    const aclTensor *dsinkOut,
    uint64_t *workspaceSize,
    aclOpExecutor **executor);
aclnnStatus aclnnFlashAttentionUnpaddingScoreGradV5(
  void             *workspace,
  uint64_t          workspaceSize,
  aclOpExecutor    *executor,
  const aclrtStream stream)

aclnnFlashAttentionUnpaddingScoreGradV5GetWorkspaceSize

  • Parameters

    Parameter Input/Output Description Instruction Data Type Data Format Dimension (shape) Non-contiguous Tensor
    query Input Q in the formula. The data type is consistent with keyIn/value. FLOAT16, BFLOAT16 ND [TND] √
    queryRope Optional input The rope part of Q (rotary position embedding). The data type is consistent with query. BFLOAT16 ND [TND] √
    keyIn Input K in the formula. The data type is consistent with query/value. FLOAT16, BFLOAT16 ND [TND] √
    keyInRope Optional input Rope part of K. Data type is consistent with keyIn. BFLOAT16 ND [TND] √
    value Input V in the formula. The data type is consistent with query/keyIn. FLOAT16, BFLOAT16 ND [TND] √
    dy Input dY in the formula. - FLOAT16, BFLOAT16 ND [TND] √
    pseShiftOptional Optional input pse in the formula. Data type is consistent with the data type of query. This parameter must be used together with pseType. FLOAT16, BFLOAT16 ND [B,N,1024,Skv], [1,N,1024,Skv], [B,N], [N] √
    dropMaskOptional Optional input Dropout mask. Incompatible with rope. If not used, pass nullptr. UINT8 ND 0, 1 √
    paddingMaskOptional Optional input Reserved parameter, not used yet. Pass an empty value when calling this API. - - - -
    qStartIdxOptional Optional input Represents the external tiling use case. It is the global start index of the query sequence in the current block. - INT64 ND 0, 1 -
    kvStartIdxOptional Optional input Represents the start index of the key and value sequences of the current chunk in the global sequence in the external splitting use case. - INT64 ND 0, 1 -
    attenMaskOptional Optional input atten_mask in the formula. A value of 1 indicates that the position does not participate in computation, and a value of 0 indicates that it does. BOOL, UINT8 ND [B,N,Sq,Skv], [B,1,Sq,Skv], [1,1,Sq,Skv], [Sq,Skv] √
    softmaxMaxOptional Optional input Intermediate output of forward softmax. - FLOAT ND [TN8], [NT8] √
    softmaxSumOptional Optional input Intermediate output of the forward softmax. - FLOAT ND [TN8], [NT8] √
    softmaxInOptional Optional input Intermediate output of forward softmax. Reserved parameter, not used yet. - - - -
    attentionInOptional Optional input Forward attention output. The data type and shape are consistent with those of query. FLOAT16, BFLOAT16, FLOAT32 ND [TND] √
    sinkInOptional Optional input sink in the formula. The length is headNum. FLOAT32 ND [headNum] √
    prefixOptional Optional input N of each batch in the prefix sparse use case. Pass nullptr if not used. INT64 ND 0, 1 -
    actualSeqQLenOptional Optional input Actual Query sequence length. - INT64 ND 1 -
    actualSeqKvLenOptional Optional input Actual Key/Value sequence length. - INT64 ND 1 -
    scaleValue Optional input Scaling factor of scale. Generally set to D^-0.5. DOUBLE - - -
    keepProb Optional input Proportion of 1s in dropMaskOptional. Generally set to 1.0. DOUBLE - - -
    preTokens Optional input Left boundary of the sliding window in sparse computation. If not specifically specified, 2147483647 is recommended. INT64 - - -
    nextTokens Optional input Right boundary of the sliding window in sparse computation. If not specified, 2147483647 is recommended. INT64 - - -
    headNum Input Number of heads per device, that is, the length of the N axis of Query. For overall constraints, see Constraints. INT64 - - -
    inputLayout Input Input Q/K/V data layout. Supports TND. String - - -
    innerPrecise Optional input Controls the precision of internal computation. Reserved parameter, not used yet. INT64 - - -
    sparseMode Optional input Sparse mode. Supports values 0-8, excluding 5. Value 6 is not supported when rope is passed in. INT64 - - -
    pseType Optional input Pse type. Supported values are 0-3. INT64 - - -
    softmaxInLayout Optional input Controls the actual data layout of softmaxMax and softmaxSum. Pass "same_as_input" when the shape is TND but the actual data layout needs to be NTD; pass "" when it is TND. String - - -
    dqOut Output dQ in the formula, the Query gradient. - FLOAT16, BFLOAT16 ND [TND] √
    dkOut Output dK in the formula, the Key gradient. - FLOAT16, BFLOAT16 ND [TND] √
    dvOut Output Value gradient, corresponding to dV in the formula. - FLOAT16, BFLOAT16 ND [TND] √
    dpseOut Output d(pse) gradient. Reserved parameter, not used yet. - - - -
    dsinkOut Output Gradient of dSink in the formula, d(sinkInOptional). - FLOAT32 ND [headNum] √
    workspaceSize Output Returns the size of the workspace to be allocated on the Device side. - - - - -
    executor Output Returns the op executor, which contains the operator computation process. - - - - -
  • Return Value

    aclnnStatus: return status code. For details, see aclnn Return Codes.

    The first-phase API performs input parameter validation and returns an error in the following scenarios:

    Return Value Error Code Description
    ACLNN_ERR_PARAM_NULLPTR 161001 The passed-in parameter is a required input, output, or required attribute and is a null pointer.
    ACLNN_ERR_PARAM_INVALID 161002 The data types of query, keyIn, value, dy, pseShiftOptional, dropMaskOptional, paddingMaskOptional, attenMaskOptional, softmaxMaxOptional, softmaxSumOptional, softmaxInOptional, attentionInOptional, sinkInOptional, dqOut, dkOut, dvOut, dsinkOut, and softmaxInLayout are not within the supported range.
    The data formats of query, keyIn, value, dy, pseShiftOptional, dropMaskOptional, paddingMaskOptional, attenMaskOptional, softmaxMaxOptional, softmaxSumOptional, softmaxInOptional, attentionInOptional, sinkInOptional, dqOut, dkOut, dvOut, dsinkOut, and softmaxInLayout are not within the supported range.

aclnnFlashAttentionUnpaddingScoreGradV5

  • Parameters

    Parameter Input/Output Description
    workspace Input Address of the workspace memory applied for on the Device.
    workspaceSize Input Size of the workspace memory applied for on the Device, obtained by calling the first-phase API aclnnFlashAttentionUnpaddingScoreGradV5GetWorkspaceSize.
    executor Input Op executor that contains the operator computation flow.
    stream Input Specifies the Stream for task execution.
  • Return Value

    aclnnStatus: return status code. For details, see aclnn Return Codes.

Constraints

  • Deterministic computation:

    • aclnnFlashAttentionUnpaddingScoreGradV5 defaults to a non-deterministic implementation. Deterministic computation can be enabled by calling aclrtCtxSetSysParamOpt.
  • When using this API with PyTorch, ensure that the versions of CANN-related packages and PyTorch-related packages are compatible.

  • The B (batchsize) dimension of the inputs query, queryRope, key, keyRope, value, and dy must be equal.

  • The inputLayout of query, key, value, and dy must be consistent.

  • The inputLayout of query, queryRope, key, keyRope, value, and dy must be TND.

  • The data type of query, key, value, and pseShiftOptional must be consistent.

  • The shape of key and value must be consistent. When the d dimensions of query, key, and value are the same, the shape of query and dy must be consistent. Note: The current version only supports the case where the d dimensions of query, key, and value are the same.

  • The N of query and the N of key/value may differ, but they must be proportional, that is, Nq/Nkv must be a non-zero integer, with Nq in the value range of 1 to 256.

  • Constraints on data shape, taking TND of inputLayout as an example, where:

    • T(B*S): ranges from 1 to 1M.
    • B: ranges from 1 to 20000. When prefixOptional is used, B supports a maximum of 1K.
    • N: ranges from 1 to 256.
    • S: ranges from 1 to 1M.
    • D: ranges from 1 to 768.
    • KeepProb: value range: (0, 1].
  • The data layout format of query, key, and value only supports TND, where T is the tightly packed data of the B and S axes (SeqLenQ and SeqLenKV of each batch). B (Batch) indicates the input sample batch size, S (Seq-Length) indicates the input sample sequence length, H (Head-Size) indicates the hidden size, N (Head-Num) indicates the number of heads, and D (Head-Dim) indicates the smallest unit dimension of the hidden layer, satisfying D = H/N.

  • In TND format, some trailing batches are supported to be not involved in calculation. In this case, pass the corresponding number of 0s at the end of actual_seq_qlen and actual_seq_kv_len. For example, if the actual S length is [2, 3, 4, 5, 6] and the last two batches are not involved in calculation, the passed actual_seq_qlen is [2, 5, 9, 0, 0].

  • pseShiftOptional: If Sq is greater than 1024, the Sq and Skv of each batch are of the same length, and sparseMode is 0, 2, or 3 in the lower triangular mask use case, alibi position encoding compression can be enabled. In this case, only the last 1024 rows of the original PSE need to be input, that is, alibi_compress = ori_pse[:, :, -1024:, :], to achieve memory optimization. Details are as follows:

    • When the parameter differs for each batch, the shape is BNHSkv (H = 1024).
    • When the parameter is the same for each batch, the shape is 1NHSkv (H = 1024).
    • In the TND use case, each batch segment is still generated as [N, Sq_i, Skv_i], but is uniformly flattened for storage and parameter passing. If the actual query length of the i-th batch segment is Sq_i and the actual key/value length is Skv_i, the number of PSE elements in this segment is N Sq_i Skv_i, and the total length of the entire PSE, pseTotalLen, is sum_i(N Sq_i Skv_i).
    • If pseType is 2 or 3, the data type must be FLOAT32, and the supported shape range is [B,N] or [N].
    • If this parameter is not enabled, pass nullptr for pseShiftOptional and 1 for pseType.
  • Meaning of each pseType value

    pseTypeMeaningNote
    0Externally input pse: mul first, then add-
    1Externally input pse: add first, then mulConsistent with the implementation of FlashAttentionScoreGrad.
    2Internally generated pse: mul first, then add-
    3Internally generated pse: mul first, then add, then sqrt-
  • Constraints on sparseMode are as follows:

    • When the shapes of all attenMaskOptional are smaller than 2048 and identical, it is recommended to use the default mode to reduce memory usage.
    • When configured as 1, 2, or 3, the user-configured preTokens and nextTokens do not take effect.
    • When sparseMode is configured as 0 or 4, ensure that attenMaskOptional is consistent with the ranges of preTokens and nextTokens.
    • If not explicitly specified, it is recommended to pass 0.
    • For details about different sparse modes, see sparse mode description.
    • When configured as 7, the optional input pseShiftOptional is not supported.
    • When configured as 8, the optional input pseShiftOptional is supported when q and kv of each sequence have equal lengths, and pse is generated globally. Outward slicing in the q direction is supported, which requires that q and kv of each sequence have equal lengths before slicing, and after slicing, actualSeqQLenOptional[0] - actualSeqKvLenOptional[0] + qStartIdxOptional - kvStartIdxOptional == 0 (this is an experimental feature).
  • For details about different data formats, see data format.

  • In some scenarios, if the computation workload is too large, the operator execution may time out (aicore error with errorStr: timeout or trap error). In this case, axis splitting is recommended. Note: The computation workload here is affected by parameters such as B, S, N, and D. Larger values result in a heavier workload.

  • The prefixOptional sparse computation only supports the compressed scenario, sparseMode=6. When Sq > Skv, the N value range of prefix is [0, Skv]; when Sq <= Skv, the N value range of prefix is [Skv-Sq, Skv].

  • The actualSeqQLenOptional input supports a case where the S length of a batch is 0, but the optional input pseShiftOptional is not supported in this case.

  • Constraints on the softmaxMax and softmaxSum parameters: The input format is fixed as [B, N, S, 8], except for the TND input format, which is [T, N, 8]. Note: T=B*S.

  • The value of headNum must be consistent with the N value in the input query.

  • When the data layout of softmaxSum and softmaxMax is NTD, softmaxInLayout must be "same_as_input".

  • The dimension of sinkInOptional is 1, and its length must be the same as the headNum of query.

Example

The sample code is as follows, for reference only. For details about the compilation and execution procedure, see Compile and Run Samples.

#include <iostream>
#include <vector>
#include "acl/acl.h"
#include "aclnnop/aclnn_flash_attention_score_grad.h"

#define CHECK_RET(cond, return_expr) \
  do {                               \
    if (!(cond)) {                   \
      return_expr;                   \
    }                                \
  } while (0)

#define LOG_PRINT(message, ...)     \
  do {                              \
    printf(message, ##__VA_ARGS__); \
  } while (0)

int64_t GetShapeSize(const std::vector<int64_t>& shape) {
  int64_t shapeSize = 1;
  for (auto i : shape) {
    shapeSize *= i;
  }
  return shapeSize;
}

void PrintOutResult(std::vector<int64_t> &shape, void** deviceAddr) {
  auto size = GetShapeSize(shape);
  std::vector<float> resultData(size, 0);
  auto ret = aclrtMemcpy(resultData.data(), resultData.size() * sizeof(resultData[0]),
                           *deviceAddr, size * sizeof(resultData[0]), ACL_MEMCPY_DEVICE_TO_HOST);
  CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("copy result from device to host failed. ERROR: %d\n", ret); return);
  for (int64_t i = 0; i < size; i++) {
    LOG_PRINT("mean result[%ld] is: %f\n", i, resultData[i]);
  }
}

int Init(int32_t deviceId, aclrtContext* context, aclrtStream* stream) {
  // Boilerplate for resource initialization
  auto ret = aclInit(nullptr);
  CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclInit failed. ERROR: %d\n", ret); return ret);
  ret = aclrtSetDevice(deviceId);
  CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclrtSetDevice failed. ERROR: %d\n", ret); return ret);
  ret = aclrtCreateContext(context, deviceId);
  CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclrtCreateContext failed. ERROR: %d\n", ret); return ret);
  ret = aclrtSetCurrentContext(*context);
  CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclrtSetCurrentContext failed. ERROR: %d\n", ret); return ret);
  ret = aclrtCreateStream(stream);
  CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclrtCreateStream failed. ERROR: %d\n", ret); return ret);
  return 0;
}

template <typename T>
int CreateAclTensor(const std::vector<T>& hostData, const std::vector<int64_t>& shape, void** deviceAddr,
                      aclDataType dataType, aclTensor** tensor) {
  auto size = GetShapeSize(shape) * sizeof(T);
  // Call aclrtMalloc to allocate memory on the device.
  auto ret = aclrtMalloc(deviceAddr, size, ACL_MEM_MALLOC_HUGE_FIRST);
  CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclrtMalloc failed. ERROR: %d\n", ret); return ret);
  // Call aclrtMemcpy to copy data from the host to the device memory.
  ret = aclrtMemcpy(*deviceAddr, size, hostData.data(), size, ACL_MEMCPY_HOST_TO_DEVICE);
  CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclrtMemcpy failed. ERROR: %d\n", ret); return ret);

  // Calculate strides of the contiguous tensor.
  std::vector<int64_t> strides(shape.size(), 1);
  for (int64_t i = shape.size() - 2; i >= 0; i--) {
    strides[i] = shape[i + 1] * strides[i + 1];
  }

  // Call the aclCreateTensor API to create an aclTensor.
  *tensor = aclCreateTensor(shape.data(), shape.size(), dataType, strides.data(), 0, aclFormat::ACL_FORMAT_ND,
                              shape.data(), shape.size(), *deviceAddr);
  return 0;
}

int main() {
  // 1. (Boilerplate) Initialize the device/stream. For details, see the acl API manual.
  // Set deviceId based on the actual device.
  int32_t deviceId = 0;
  aclrtContext context;
  aclrtStream stream;
  auto ret = Init(deviceId, &context, &stream);
  CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("Init acl failed. ERROR: %d\n", ret); return ret);

  // 2. Construct inputs and outputs as required by the API.
  std::vector<int64_t> qShape = {256, 1, 128};
  std::vector<int64_t> kShape = {256, 1, 128};
  std::vector<int64_t> vShape = {256, 1, 128};
  std::vector<int64_t> dxShape = {256, 1, 128};
  std::vector<int64_t> attenmaskShape = {256, 256};
  std::vector<int64_t> softmaxMaxShape = {256, 1, 8};
  std::vector<int64_t> softmaxSumShape = {256, 1, 8};
  std::vector<int64_t> attentionInShape = {256, 1, 128};
  std::vector<int64_t> sinkInOptionalShape = {1};
  std::vector<int64_t> dqShape = {256, 1, 128};
  std::vector<int64_t> dkShape = {256, 1, 128};
  std::vector<int64_t> dvShape = {256, 1, 128};
  std::vector<int64_t> dsinkShape = {1};

  void* qDeviceAddr = nullptr;
  void* kDeviceAddr = nullptr;
  void* vDeviceAddr = nullptr;
  void* dxDeviceAddr = nullptr;
  void* attenmaskDeviceAddr = nullptr;
  void* softmaxMaxDeviceAddr = nullptr;
  void* softmaxSumDeviceAddr = nullptr;
  void* attentionInDeviceAddr = nullptr;
  void* sinkInOptionalDeviceAddr = nullptr;

  void* dqDeviceAddr = nullptr;
  void* dkDeviceAddr = nullptr;
  void* dvDeviceAddr = nullptr;
  void* dsinkDeviceAddr = nullptr;

  aclTensor* q = nullptr;
  aclTensor* k = nullptr;
  aclTensor* v = nullptr;
  aclTensor* dx = nullptr;
  aclTensor* pse = nullptr;
  aclTensor* dropMask = nullptr;
  aclTensor* padding = nullptr;
  aclTensor* attenmask = nullptr;
  aclTensor* softmaxMax = nullptr;
  aclTensor* softmaxSum = nullptr;
  aclTensor* softmaxIn = nullptr;
  aclTensor* attentionIn = nullptr;
  aclTensor* sinkInOptional = nullptr;
  aclTensor* dq = nullptr;
  aclTensor* dk = nullptr;
  aclTensor* dv = nullptr;
  aclTensor* dpse = nullptr;
  aclTensor* dsink = nullptr;

  std::vector<uint16_t> qHostData(32768, 0);
  std::vector<uint16_t> kHostData(32768, 0);
  std::vector<uint16_t> vHostData(32768, 0);
  std::vector<uint16_t> dxHostData(32768, 0);
  std::vector<uint8_t> attenmaskHostData(65536, 0);
  std::vector<float> softmaxMaxHostData(2048, 3.0);
  std::vector<float> softmaxSumHostData(2048, 3.0);
  std::vector<uint16_t> attentionInHostData(32768, 0);
  std::vector<float> sinkInOptionalHostData(1, 0);
  std::vector<uint16_t> dqHostData(32768, 0);
  std::vector<uint16_t> dkHostData(32768, 0);
  std::vector<uint16_t> dvHostData(32768, 0);
  std::vector<float> dsinkHostData(1, 0);
  ret = CreateAclTensor(qHostData, qShape, &qDeviceAddr, aclDataType::ACL_FLOAT16, &q);
  CHECK_RET(ret == ACL_SUCCESS, return ret);
  ret = CreateAclTensor(kHostData, kShape, &kDeviceAddr, aclDataType::ACL_FLOAT16, &k);
  CHECK_RET(ret == ACL_SUCCESS, return ret);
  ret = CreateAclTensor(vHostData, vShape, &vDeviceAddr, aclDataType::ACL_FLOAT16, &v);
  CHECK_RET(ret == ACL_SUCCESS, return ret);
  ret = CreateAclTensor(dxHostData, dxShape, &dxDeviceAddr, aclDataType::ACL_FLOAT16, &dx);
  CHECK_RET(ret == ACL_SUCCESS, return ret);
  ret = CreateAclTensor(attenmaskHostData, attenmaskShape, &attenmaskDeviceAddr, aclDataType::ACL_UINT8, &attenmask);
  CHECK_RET(ret == ACL_SUCCESS, return ret);
  ret = CreateAclTensor(softmaxMaxHostData, softmaxMaxShape, &softmaxMaxDeviceAddr, aclDataType::ACL_FLOAT, &softmaxMax);
  CHECK_RET(ret == ACL_SUCCESS, return ret);
  ret = CreateAclTensor(softmaxSumHostData, softmaxSumShape, &softmaxSumDeviceAddr, aclDataType::ACL_FLOAT, &softmaxSum);
  CHECK_RET(ret == ACL_SUCCESS, return ret);
  ret = CreateAclTensor(attentionInHostData, attentionInShape, &attentionInDeviceAddr, aclDataType::ACL_FLOAT16, &attentionIn);
  CHECK_RET(ret == ACL_SUCCESS, return ret);
  ret = CreateAclTensor(sinkInOptionalHostData, sinkInOptionalShape, &sinkInOptionalDeviceAddr, aclDataType::ACL_FLOAT, &sinkInOptional);
  CHECK_RET(ret == ACL_SUCCESS, return ret);
  ret = CreateAclTensor(dqHostData, dqShape, &dqDeviceAddr, aclDataType::ACL_FLOAT16, &dq);
  CHECK_RET(ret == ACL_SUCCESS, return ret);
  ret = CreateAclTensor(dkHostData, dkShape, &dkDeviceAddr, aclDataType::ACL_FLOAT16, &dk);
  CHECK_RET(ret == ACL_SUCCESS, return ret);
  ret = CreateAclTensor(dvHostData, dvShape, &dvDeviceAddr, aclDataType::ACL_FLOAT16, &dv);
  CHECK_RET(ret == ACL_SUCCESS, return ret);
  ret = CreateAclTensor(dsinkHostData, dsinkShape, &dsinkDeviceAddr, aclDataType::ACL_FLOAT, &dsink);
  CHECK_RET(ret == ACL_SUCCESS, return ret);
  std::vector<int64_t> prefixOp = {0};
  aclIntArray* prefix = aclCreateIntArray(prefixOp.data(), 1);
  std::vector<int64_t>  acSeqQLenOp = {256};
  std::vector<int64_t>  acSeqKvLenOp = {256};
  aclIntArray* acSeqQLen = aclCreateIntArray(acSeqQLenOp.data(), acSeqQLenOp.size());
  aclIntArray* acSeqKvLen = aclCreateIntArray(acSeqKvLenOp.data(), acSeqKvLenOp.size());
  std::vector<int64_t> qStartIdxOp = {0};
  std::vector<int64_t> kvStartIdxOp = {0};
  aclIntArray *qStartIdx = aclCreateIntArray(qStartIdxOp.data(), 1);
  aclIntArray *kvStartIdx = aclCreateIntArray(kvStartIdxOp.data(), 1);
  double scaleValue = 0.088388;
  double keepProb = 1;
  int64_t preTokens = 65536;
  int64_t nextTokens = 65536;
  int64_t headNum = 1;
  int64_t innerPrecise = 0;
  int64_t sparseMode = 0;
  int64_t pseType = 1;
  char softmaxInLayoutArr[] = "same_as_input";
  char layOut[5] = {'T', 'N', 'D', 0};

  // 3. Call the CANN operator library API. Replace Api with the actual API name.
  uint64_t workspaceSize = 0;
  aclOpExecutor* executor;

  // Call the first-phase API of aclnnFlashAttentionUnpaddingScoreGradV5.
  ret = aclnnFlashAttentionUnpaddingScoreGradV5GetWorkspaceSize(q, nullptr, k, nullptr, v, dx, pse, dropMask, padding,
              attenmask, softmaxMax, softmaxSum, softmaxIn, attentionIn, sinkInOptional, prefix, acSeqQLen, acSeqKvLen,
              qStartIdx, kvStartIdx, scaleValue, keepProb, preTokens, nextTokens, headNum, layOut, innerPrecise, sparseMode,
              pseType, softmaxInLayoutArr, dq, nullptr, dk, nullptr, dv, dpse, dsink, &workspaceSize, &executor);
  CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclnnFlashAttentionUnpaddingScoreGradV5GetWorkspaceSize failed. ERROR: %d\n", ret); return ret);

  // Allocate device memory based on the workspaceSize computed by the first-phase API.
  void* workspaceAddr = nullptr;
  if (workspaceSize > 0) {
    ret = aclrtMalloc(&workspaceAddr, workspaceSize, ACL_MEM_MALLOC_HUGE_FIRST);
    CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("allocate workspace failed. ERROR: %d\n", ret); return ret);
  }

  // Call the second-phase API of aclnnFlashAttentionUnpaddingScoreGradV5.
  ret = aclnnFlashAttentionUnpaddingScoreGradV5(workspaceAddr, workspaceSize, executor, stream);
  CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclnnFlashAttentionUnpaddingScoreGradV5 failed. ERROR: %d\n", ret); return ret);

  // 4. (boilerplate) Synchronize and wait for the task execution to complete.
  ret = aclrtSynchronizeStream(stream);
  CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclrtSynchronizeStream failed. ERROR: %d\n", ret); return ret);

  // 5. Obtain the output value and copy the result from device memory to host memory. Modify the code based on the specific API definition.
  PrintOutResult(dqShape, &dqDeviceAddr);
  PrintOutResult(dkShape, &dkDeviceAddr);
  PrintOutResult(dvShape, &dvDeviceAddr);
  PrintOutResult(dsinkShape, &dsinkDeviceAddr);

  // 6. Release aclTensor and aclScalar. Modify the code based on the specific API definition.
  aclDestroyTensor(q);
  aclDestroyTensor(k);
  aclDestroyTensor(v);
  aclDestroyTensor(dx);
  aclDestroyTensor(attenmask);
  aclDestroyTensor(softmaxMax);
  aclDestroyTensor(softmaxSum);
  aclDestroyTensor(attentionIn);
  aclDestroyTensor(sinkInOptional);
  aclDestroyTensor(dq);
  aclDestroyTensor(dk);
  aclDestroyTensor(dv);
  aclDestroyTensor(dsink);
  // 7. Release device resources.
  aclrtFree(qDeviceAddr);
  aclrtFree(kDeviceAddr);
  aclrtFree(vDeviceAddr);
  aclrtFree(dxDeviceAddr);
  aclrtFree(attenmaskDeviceAddr);
  aclrtFree(softmaxMaxDeviceAddr);
  aclrtFree(softmaxSumDeviceAddr);
  aclrtFree(attentionInDeviceAddr);
  aclrtFree(sinkInOptionalDeviceAddr);
  aclrtFree(dqDeviceAddr);
  aclrtFree(dkDeviceAddr);
  aclrtFree(dvDeviceAddr);
  aclrtFree(dsinkDeviceAddr);
  if (workspaceSize > 0) {
    aclrtFree(workspaceAddr);
  }
  aclrtDestroyStream(stream);
  aclrtDestroyContext(context);
  aclrtResetDevice(deviceId);
  aclFinalize();

  return 0;
}