aclnnFusedFloydAttentionGrad

Applicable Products

  • Ascend 950PR&950DT products: Not 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: Computes the backward output of Floyd attention for training. Compared with traditional FA, FloydAttn additionally uses seq as the batch axis when computing qk/pv attention, thereby converting the computation to batchMatmul.

  • Formula:

    The forward attention formula is:

    $$ S=Mask(scale*(Q*K_1^T + Q*K_2^T), atten\_mask) \\ P=Softmax(S) \\ Y=(P*V_1+P*V_2) $$

    The backward propagation formula for attention is:

    $$ dV_1=P^TdY $$
    $$ dV_2=P^TdY $$
    $$ dQ=\frac{((dS)*K_1)}{\sqrt{d}}+\frac{((dS)*K_2)}{\sqrt{d}} $$
    $$ dK_1=\frac{((dS)^T*Q)}{\sqrt{d}} $$
    $$ dK_2=\frac{((dS)^T*Q)}{\sqrt{d}} $$

Function Prototype

Each operator uses a two-phase API. You must first call the "aclnnFusedFloydAttentionGradGetWorkspaceSize" API to obtain the workspace size required for computation and the executor that contains the computation process of the operator, and then call the "aclnnFusedFloydAttentionGrad" API to perform the computation.

aclnnStatus aclnnFusedFloydAttentionGradGetWorkspaceSize(
  const aclTensor   *query,
  const aclTensor   *key1,
  const aclTensor   *value1,
  const aclTensor   *key2,
  const aclTensor   *value2,
  const aclTensor   *dy,
  const aclTensor   *attenMaskOptional,
  const aclTensor   *softmaxMax,
  const aclTensor   *softmaxSum,
  const aclTensor   *attentionIn,
  double             scaleValue,
  const aclTensor   *dqOut,
  const aclTensor   *dk1Out,
  const aclTensor   *dv1Out,
  const aclTensor   *dk2Out,
  const aclTensor   *dv2Out,
  uint64_t          *workspaceSize,
  aclOpExecutor    **executor)
aclnnStatus aclnnFusedFloydAttentionGrad(
  void          *workspace,
  uint64_t       workspaceSize,
  aclOpExecutor *executor,
  aclrtStream    stream)

aclnnFusedFloydAttentionGradGetWorkspaceSize

  • Parameters

    Parameter Input/Output Description Instruction Data Type Data Format Dimension (shape) Non-contiguous Tensor
    query (aclTensor) Input Q in the formula. The data type is the same as that of key1/value1/key2/value2. FLOAT16, BFLOAT16 ND [B,H,N,M,D] √
    key1 (aclTensor) Input K1 in the formula. Same data type as query/value1/key2/value2. FLOAT16, BFLOAT16 ND [B,H,N,K,D] √
    value1 (aclTensor) Input V1 in the formula. Same data type as query/key1/key2/value2. FLOAT16, BFLOAT16 ND [B,H,N,K,D] √
    key2 (aclTensor) Input K2 in the formula. The data type is the same as that of query/key1/value1/value2. FLOAT16, BFLOAT16 ND [B,H,K,M,D] √
    value2 (aclTensor) Input V2 in the formula. Data type must be the same as that of query/key1/value1/key2. FLOAT16, BFLOAT16 ND [B,H,K,M,D] √
    dy (aclTensor) Input dY input in the formula. - FLOAT16, BFLOAT16 ND [B,H,N,M,D] √
    attenMaskOptional (aclTensor) Input atten_mask in the formula. The value 1 indicates that the position does not participate in computation, and the value 0 indicates that the position participates in computation. BOOL, UINT8 ND [B,1,N,1,K] √
    softmaxMax (aclTensor) Input Intermediate output of the forward attention computation. The output shape is [B,H,N,M,8]. FLOAT ND [B,H,N,M,8] √
    softmaxSum (aclTensor) Input Intermediate output of the forward attention computation. The output shape is [B,H,N,M,8]. FLOAT ND [B,H,N,M,8] √
    attentionIn (aclTensor) Input Final output of the forward attention computation. The data type and shape type are the same as those of query. FLOAT16, BFLOAT16 ND [B,H,N,M,D] √
    dqOut (aclTensor) Output dQ in the formula, indicating the gradient of query. - FLOAT16, BFLOAT16 ND [B,H,N,M,D] √
    dk1Out (aclTensor) Output dK1 in the formula, indicating the gradient of key1. - FLOAT16, BFLOAT16 ND [B,H,N,K,D] √
    dv1Out (aclTensor) Output dV1 in the formula. Indicates the gradient of value1. - FLOAT16, BFLOAT16 ND [B,H,N,K,D] √
    dk2Out (aclTensor) Output dK2 in the formula. Indicates the gradient of key2. - FLOAT16, BFLOAT16 ND [B,H,K,M,D] √
    dv2Out (aclTensor) Output dV2 in the formula, indicating the gradient of value2. - FLOAT16, BFLOAT16 ND [B,H,K,M,D] √
    scaleValue (double) Input Scale in the formula, which indicates the scaling coefficient. - DOUBLE - - -
    workspaceSize (uint64_t*) Output Returns the workspace size to be allocated on the device. - - - - -
    executor(aclOpExecutor) Output Returns the op executor, which contains the operator computation flow. - - - - -
  • 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 input parameter is a required input, output, or required attribute, and is a null pointer.
    ACLNN_ERR_PARAM_INVALID 161002 The data type or data format of query, key1, value1, key2, value2, dy, attenMaskOptional, softmaxMax, softmaxSum, attentionIn, dqOut, dk1Out, dv1Out, dk2Out, or dv2Out is not within the supported range.
    ACLNN_ERR_INNER_TILING_ERROR 561002 An exception occurs during tiling. query, key1, value1, key2, value2, dy, attenMaskOptional, softmaxMax, softmaxSum, and attentionIn do not comply with the constraints.

aclnnFusedFloydAttentionGrad

  • Parameters:

    Parameter Input/Output Description
    workspace Input Workspace memory address allocated on the device.
    workspaceSize Input Workspace size allocated on the device, obtained by calling the first-phase API aclnnFusedFloydAttentionGradGetWorkspaceSize.
    executor Input Op executor, which contains the operator computation process.
    stream Input Specifies the Stream for executing tasks.
  • Return value

    Returns aclnnStatus status codes. For details, see aclnn return codes.

Constraints

  • This API does not support deterministic computation.

  • This API does not support non-contiguous output.

  • When using this API with PyTorch, ensure that the versions of CANN-related packages and PyTorch-related packages are compatible.

  • Constraints on data shape:

    • B: value range is 1 to 2K.
    • H: value range is 1 to 256.
    • N: value range is 16 to 1M, and N%16==0.
    • M: value range is 128 to 1M, and M%128==0.
    • K: value range is 128 to 1M, and K%128==0.
    • D: value range is 32/64/128.
  • Axis 0/2/4 of query and key1 must be the same.

  • key1 and value1 must have the same shape.

  • key2 and value2 must have the same shape.

  • The shapes of query and dy/attentionIn must be the same.

  • softmaxMax and softmaxSum must have the same shape.

  • D only supports 32, 64, or 128.

  • Due to underlying instruction limitations, when MD >= 65536 or KD >= 65536, significant performance degradation occurs. In this case, use small operator concatenation as a replacement.

Example

The following sample code is for reference only. For details about compilation and execution, see Compile and Run Samples.

#include <iostream>
#include <vector>
#include <cstdint>
#include <cmath>
#include "acl/acl.h"
#include "aclnnop/aclnn_fused_floyd_attention_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, aclrtStream* stream) {
  // Boilerplate code for AscendCL 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 = 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 device-side memory.
  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 host to 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 device/stream. Refer to the AscendCL external API list.
  // Set deviceId based on the actual device.
  int32_t deviceId = 0;
  aclrtStream stream;
  auto ret = Init(deviceId, &stream);
  CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("Init acl failed. ERROR: %d\n", ret); return ret);

  // 2. Construct custom inputs and outputs based on the API.
  int64_t B = 1;
  int64_t N = 1;
  int64_t S1 = 256;
  int64_t S2 = 256;
  int64_t S3 = 256;
  int64_t D = 128;

  int64_t q_size = B * N * S1 * S2 * D;
  int64_t k1_v1_size = B * N * S1 * S3 * D;
  int64_t k2_v2_size = B * N * S3 * S2 * D;
  int64_t atten_mask_size = B * S1 * S3;
  int64_t softmax_size = B * N * S1 * S2 * 8;

  std::vector<int64_t> qShape = {B, N, S1, S2, D};
  std::vector<int64_t> k1v1Shape = {B, N, S1, S3, D};
  std::vector<int64_t> k2v2Shape = {B, N, S3, S2, D};
  std::vector<int64_t> attenmaskShape = {B, 1, S1, 1, S3};
  std::vector<int64_t> softmaxMaxShape = {B, N, S1, S2, 8};
  std::vector<int64_t> softmaxSumShape = {B, N, S1, S2, 8};
  std::vector<int64_t> attentionInShape = {B, N, S1, S2, D};

  std::vector<int64_t> dqShape = {B, N, S1, S2, D};
  std::vector<int64_t> dk1dv1Shape = {B, N, S1, S3, D};
  std::vector<int64_t> dk2dv2Shape = {B, N, S3, S2, D};

  void* qDeviceAddr = nullptr;
  void* k1DeviceAddr = nullptr;
  void* v1DeviceAddr = nullptr;
  void* k2DeviceAddr = nullptr;
  void* v2DeviceAddr = nullptr;
  void* dxDeviceAddr = nullptr;
  void* attenmaskDeviceAddr = nullptr;
  void* softmaxMaxDeviceAddr = nullptr;
  void* softmaxSumDeviceAddr = nullptr;
  void* attentionInDeviceAddr = nullptr;
  void* dqDeviceAddr = nullptr;
  void* dk1DeviceAddr = nullptr;
  void* dv1DeviceAddr = nullptr;
  void* dk2DeviceAddr = nullptr;
  void* dv2DeviceAddr = nullptr;

  aclTensor* q = nullptr;
  aclTensor* k1 = nullptr;
  aclTensor* v1 = nullptr;
  aclTensor* k2 = nullptr;
  aclTensor* v2 = nullptr;
  aclTensor* dx = nullptr;
  aclTensor* attenmask = nullptr;
  aclTensor* softmaxMax = nullptr;
  aclTensor* softmaxSum = nullptr;
  aclTensor* attentionIn = nullptr;
  aclTensor* dq = nullptr;
  aclTensor* dk1 = nullptr;
  aclTensor* dv1 = nullptr;
  aclTensor* dk2 = nullptr;
  aclTensor* dv2 = nullptr;

  std::vector<float> qHostData(q_size, 1.0);
  std::vector<float> k1HostData(k1_v1_size, 1.0);
  std::vector<float> v1HostData(k1_v1_size, 1.0);
  std::vector<float> k2HostData(k2_v2_size, 1.0);
  std::vector<float> v2HostData(k2_v2_size, 1.0);
  std::vector<float> dxHostData(q_size, 1.0);
  std::vector<uint8_t> attenmaskHostData(atten_mask_size, 0);
  std::vector<float> softmaxMaxHostData(softmax_size, 3.0);
  std::vector<float> softmaxSumHostData(softmax_size, 3.0);
  std::vector<float> attentionInHostData(q_size, 1.0);
  std::vector<float> dqHostData(q_size, 0);
  std::vector<float> dk1HostData(k1_v1_size, 0);
  std::vector<float> dv1HostData(k1_v1_size, 0);
  std::vector<float> dk2HostData(k2_v2_size, 0);
  std::vector<float> dv2HostData(k2_v2_size, 0);

  ret = CreateAclTensor(qHostData, qShape, &qDeviceAddr, aclDataType::ACL_FLOAT16, &q);
  CHECK_RET(ret == ACL_SUCCESS, return ret);
  ret = CreateAclTensor(k1HostData, k1v1Shape, &k1DeviceAddr, aclDataType::ACL_FLOAT16, &k1);
  CHECK_RET(ret == ACL_SUCCESS, return ret);
  ret = CreateAclTensor(v1HostData, k1v1Shape, &v1DeviceAddr, aclDataType::ACL_FLOAT16, &v1);
  CHECK_RET(ret == ACL_SUCCESS, return ret);
  ret = CreateAclTensor(k2HostData, k2v2Shape, &k2DeviceAddr, aclDataType::ACL_FLOAT16, &k2);
  CHECK_RET(ret == ACL_SUCCESS, return ret);
  ret = CreateAclTensor(v2HostData, k2v2Shape, &v2DeviceAddr, aclDataType::ACL_FLOAT16, &v2);
  CHECK_RET(ret == ACL_SUCCESS, return ret);
  ret = CreateAclTensor(dxHostData, qShape, &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(dqHostData, dqShape, &dqDeviceAddr, aclDataType::ACL_FLOAT16, &dq);
  CHECK_RET(ret == ACL_SUCCESS, return ret);
  ret = CreateAclTensor(dk1HostData, dk1dv1Shape, &dk1DeviceAddr, aclDataType::ACL_FLOAT16, &dk1);
  CHECK_RET(ret == ACL_SUCCESS, return ret);
  ret = CreateAclTensor(dv1HostData, dk1dv1Shape, &dv1DeviceAddr, aclDataType::ACL_FLOAT16, &dv1);
  CHECK_RET(ret == ACL_SUCCESS, return ret);
  ret = CreateAclTensor(dk2HostData, dk2dv2Shape, &dk2DeviceAddr, aclDataType::ACL_FLOAT16, &dk2);
  CHECK_RET(ret == ACL_SUCCESS, return ret);
  ret = CreateAclTensor(dv2HostData, dk2dv2Shape, &dv2DeviceAddr, aclDataType::ACL_FLOAT16, &dv2);
  CHECK_RET(ret == ACL_SUCCESS, return ret);

  double scaleValue = 1.0/sqrt(128);

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

  // Call the first-phase API of aclnnFusedFloydAttentionGrad.
  ret = aclnnFusedFloydAttentionGradGetWorkspaceSize(q, k1, v1, k2, v2, dx, attenmask, softmaxMax, softmaxSum,
        attentionIn, scaleValue, dq, dk1, dv1, dk2, dv2, &workspaceSize, &executor);
  CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclnnFusedFloydAttentionGradGetWorkspaceSize failed. ERROR: %d\n", ret); return ret);

  // Allocate device memory based on the workspaceSize calculated 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 aclnnFusedFloydAttentionGrad.
  ret = aclnnFusedFloydAttentionGrad(workspaceAddr, workspaceSize, executor, stream);
  CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclnnFusedFloydAttentionGrad 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 the device memory to the host memory. Modify it based on the specific API definition.
  PrintOutResult(dqShape, &dqDeviceAddr);
  // PrintOutResult(dkShape, &dkDeviceAddr);
  // PrintOutResult(dvShape, &dvDeviceAddr);

  // 6. Release aclTensor and aclScalar. Modify it based on the specific API definition.
  aclDestroyTensor(q);
  aclDestroyTensor(k1);
  aclDestroyTensor(v1);
  aclDestroyTensor(k2);
  aclDestroyTensor(v2);
  aclDestroyTensor(dx);
  aclDestroyTensor(attenmask);
  aclDestroyTensor(softmaxMax);
  aclDestroyTensor(softmaxSum);
  aclDestroyTensor(attentionIn);
  aclDestroyTensor(dq);
  aclDestroyTensor(dk1);
  aclDestroyTensor(dv1);
  aclDestroyTensor(dk2);
  aclDestroyTensor(dv2);

  // 7. Release device resources
  aclrtFree(qDeviceAddr);
  aclrtFree(k1DeviceAddr);
  aclrtFree(v1DeviceAddr);
  aclrtFree(k2DeviceAddr);
  aclrtFree(v2DeviceAddr);
  aclrtFree(dxDeviceAddr);
  aclrtFree(attenmaskDeviceAddr);
  aclrtFree(softmaxMaxDeviceAddr);
  aclrtFree(softmaxSumDeviceAddr);
  aclrtFree(attentionInDeviceAddr);
  aclrtFree(dqDeviceAddr);
  aclrtFree(dk1DeviceAddr);
  aclrtFree(dv1DeviceAddr);
  aclrtFree(dk2DeviceAddr);
  aclrtFree(dv2DeviceAddr);
  if (workspaceSize > 0) {
    aclrtFree(workspaceAddr);
  }
  aclrtDestroyStream(stream);
  aclrtResetDevice(deviceId);
  aclFinalize();

  return 0;
}