aclnnBaddbmm&aclnnInplaceBaddbmm

📄 View source code

Supported Products

ProductSupported
Atlas A3 training series products/Atlas A3 inference series products√
Atlas A2 training products/Atlas A2 inference products√
Atlas 200I/500 A2 inference products×
Atlas inference series products√
Atlas training series products√

Function

  • Description: Computes the product of α and the matrix multiplication result of batch1 and batch2, and then sums the product with the product of β and self. Note: batch1 and batch2 must be 3D tensors. The two shapes can be broadcast only in aclnnBaddbmm. If the two shapes are broadcast in aclnnInplaceBaddbmm, the operation will be blocked. self must support broadcast with the result of batch1@batch2. (The broadcast mechanism refers to the process of expanding a smaller shape to a larger shape so that the two shapes are compatible. Currently, only broadcast of (1, n) is supported. That is, the dimensions of the two tensors must be the same or one of them must be 1.)

  • Formula:

    out=βself+α(batch1@batch2)out = βself+α(batch1@batch2)

    Note: If β is 0, self is ignored and does not participate in the computation.

  • Example:

    The shape of self is [1, M, K], the shape of batch1@batch2 is [A, M, 1], and the shape of the output out is [A, M, K]. The numbers in each dimension must be the same or one of them must be 1. If the shape of self is [2, M, K], the broadcast condition is not met and an error is reported.

Prototype

  • aclnnBaddbmm and aclnnInplaceBaddbmm implement the same function in different ways. Select a proper operator based on your requirements.
    • aclnnBaddbmm: An output tensor object needs to be created to store the computation result.
    • aclnnInplaceBaddbmm: No output tensor object needs to be created, and the computation result is stored in the memory of the input tensor.
  • Each operator has two-phase API calls. First, aclnnBaddbmmGetWorkspaceSize is called to obtain the input parameters and compute the required workspace size based on the process. Then, aclnnBaddbmm is called to perform computation.
aclnnStatus aclnnBaddbmmGetWorkspaceSize(
  const aclTensor*    self, 
  const aclTensor*    batch1, 
  const aclTensor*    batch2, 
  const aclScalar*    beta, 
  const aclScalar*    alpha, 
  aclTensor*          out, 
  int8_t              cubeMathType, 
  uint64_t*           workspaceSize, 
  aclOpExecutor**     executor)
aclnnStatus aclnnBaddbmm(
  void*           workspace, 
  uint64_t        workspaceSize, 
  aclOpExecutor*  executor, 
  aclrtStream     stream)
aclnnStatus aclnnInplaceBaddbmmGetWorkspaceSize(
  const aclTensor*    selfRef, 
  const aclTensor*    batch1, 
  const aclTensor*    batch2, 
  const aclScalar*    beta, 
  const aclScalar*    alpha, 
  int8_t              cubeMathType, 
  uint64_t*           workspaceSize, 
  aclOpExecutor**     executor)
aclnnStatus aclnnInplaceBaddbmm(
  void*             workspace, 
  uint64_t          workspaceSize, 
  aclOpExecutor*    executor, 
  aclrtStream       stream)

aclnnBaddbmmGetWorkspaceSize

  • Parameters:

    Name Input/Output Description Precaution Data Type Data Format Dimension (Shape) Non-contiguous Tensor
    self Input Input self in the formula.
    • The data type must be the same as that of out.
    • The shape of the last two dimensions of batch1@batch2 must meet the broadcast relationship. (Note: Only self can be broadcast to the same shape as the last two dimensions of batch1@batch2. The last two dimensions of batch1@batch2 cannot be broadcast to the same shape as self. For example, self: [2, 3, 5], batch1@batch2: [1, 1, 1]. An error will be reported.)
    • Empty tensors are supported.
    BFLOAT16, FLOAT16, FLOAT32 ND 3 √
    batch1 Input Input batch1 in the formula.
    • Its data type and the data types of self and batch2 must meet the type deduction rules (see Deduction Relationship and Constraints).
    • The shape must meet the BMM input constraints with batch2.
    • Empty tensors are supported.
    BFLOAT16, FLOAT16, FLOAT32 ND 3 √
    batch2 Input Input batch2 in the formula.
    • Its data type and the data types of self and batch1 must meet the type deduction rules (see Deduction Relationship and Constraints).
    • The shape must meet the BMM input constraints with batch1.
    • .
    • Empty tensors are supported.
    BFLOAT16, FLOAT16, FLOAT32 ND 3 √
    beta Input Input β in the formula. The data type can be converted to the data type deduced from self and batch1@batch2 (see Deduction Relationship and Constraints). FLOAT, FLOAT16, DOUBLE, INT32, INT64, INT16, INT8, UINT8, BOOL - - -
    alpha Input Input α in the formula. The data type can be converted to the data type deduced from self and batch1@batch2 (see Deduction Relationship and Constraints). FLOAT, FLOAT16, DOUBLE, INT32, INT64, INT16, INT8, UINT8, BOOL - - -
    out Output output in the formula.
    • The data type must be the same as that of self.
    • The shape must be the same as the last two dimensions of batch1@batch2.
    • The format must be the same as that of self and batch1@batch2.
    BFLOAT16, FLOAT16, FLOAT32 ND 2 √
    cubeMathType Input Computation logic of the Cube unit. If the input data types can be deduced from each other, this parameter processes the deduced data type by default. The supported enumerated values are as follows:
    • 0: KEEP_DTYPE. The input data type is retained for computation.
    • 1: ALLOW_FP32_DOWN_PRECISION. The input data can be computed with reduced precision.
    • 2: USE_FP16. The input data can be downgraded to FLOAT16 for computation.
    • 3: USE_HF32. The input data can be downgraded to HFLOAT32 for computation.
    INT8 - - -
    workspaceSize Output Size of the workspace required to be allocated on the device. - - - - -
    executor Output Operator executor, containing the operator computation process. - - - - -
    • Atlas training series products and Atlas inference series products:
      • The data type BFLOAT16 is not supported.
      • cubeMathType=0 is not supported when the input data type is FLOAT32.
      • cubeMathType=1: If the input data type is FLOAT32, it is converted to FLOAT16 for computation. If the input data type is not FLOAT32, no processing is performed.
      • cubeMathType=3 is not supported.
    • Atlas A2 training products/Atlas A2 inference products and Atlas A3 training series products/Atlas A3 inference series products:
      • cubeMathType=0: If the input data type is FLOAT16 or BFLOAT16, matrix multiplication uses FLOAT16/BFLOAT16 input and FLOAT32 output. If the input data type is not FLOAT16/BFLOAT16, no processing is performed.
      • cubeMathType=1: If the input data type is FLOAT32, it is converted to HFLOAT32 for computation. If the input data type is not FLOAT32, no processing is performed.
      • cubeMathType=2: If the input data type is FLOAT32, it is converted to FLOAT16 for computation. If the input data type is not FLOAT32, no processing is performed.
      • cubeMathType=3: If the input data type is FLOAT32, it is converted to HFLOAT32 for computation. If the input data type is not FLOAT32, no processing is performed.
  • Returns:

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

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

    Return Error Code Description
    ACLNN_ERR_PARAM_NULLPTR 161001 The passed self, batch1, or batch2 is a null pointer.
    ACLNN_ERR_PARAM_INVALID 161002 The data type of self, batch1, batch2, or out is not supported.
    The data format of self, batch1, batch2, or out is not supported.
    The data type of self, batch1, batch2, or out is inconsistent.
    The data format of self, batch1, batch2, or out is inconsistent.
    The broadcast operation cannot be performed between self and batch1@batch2.
    self and batch1@batch2 must be both empty tensors or non-empty tensors.
    The data type of self, batch1, batch2, or out does not meet the deduction rule.

aclnnBaddbmm

  • Parameters:

    Name Input/Output Description
    workspace Input Address of the workspace to be allocated on the device.
    workspaceSize Input Size of the workspace to be allocated on the device, obtained by calling the first-phase API aclnnBaddbmmGetWorkspaceSize.
    executor Input Operator executor, containing the operator computation process.
    stream Input Stream for executing the task.
  • Returns:

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

aclnnInplaceBaddbmmGetWorkspaceSize

  • Parameters:

    Name Input/Output Description Precaution Data Type Data Format Dimension (Shape) Non-contiguous Tensor
    selfRef Input/Output Input and output tensor, that is, input self and out in the formula.
    • Its data type and the data type of batch1@batch2 must meet the type deduction rules (see Deduction Relationship and Constraints).
    • The shape must be consistent with the shape of the last two dimensions of batch1@batch2.
    • Empty tensors are supported.
    BFLOAT16, FLOAT16, FLOAT32 ND 2 √
    batch1 Input Input batch1 in the formula.
    • Its data type and the data types of self and batch2 must meet the type deduction rules (see Deduction Relationship and Constraints).
    • The shape must meet the BMM input constraints with batch2.
    • Empty tensors are supported.
    BFLOAT16, FLOAT16, FLOAT32 ND 3 √
    batch2 Input Input batch2 in the formula.
    • Its data type and the data types of self and batch1 must meet the type deduction rules (see Deduction Relationship and Constraints).
    • The shape must meet the BMM input constraints with batch1.
    • .
    • Empty tensors are supported.
    BFLOAT16, FLOAT16, FLOAT32 ND 3 √
    beta Input Input β in the formula. The data type can be converted to the data type deduced from self and batch1@batch2 (see Deduction Relationship and Constraints). FLOAT, FLOAT16, DOUBLE, INT32, INT64, INT16, INT8, UINT8, BOOL - - -
    alpha Input Input α in the formula. The data type can be converted to the data type deduced from self and batch1@batch2 (see Deduction Relationship and Constraints). FLOAT, FLOAT16, DOUBLE, INT32, INT64, INT16, INT8, UINT8, BOOL - - -
    cubeMathType Input Computation logic of the Cube unit. If the input data types can be deduced from each other, this parameter processes the deduced data type by default. The supported enumerated values are as follows:
    • 0: KEEP_DTYPE. The input data type is retained for computation.
    • 1: ALLOW_FP32_DOWN_PRECISION. The input data can be computed with reduced precision.
    • 2: USE_FP16. The input data can be downgraded to FLOAT16 for computation.
    • 3: USE_HF32. The input data can be downgraded to HFLOAT32 for computation.
    INT8 - - -
    workspaceSize Output Size of the workspace required to be allocated on the device. - - - - -
    executor Output Operator executor, containing the operator computation process. - - - - -
    • Atlas training series products and Atlas inference series products:
      • The data type BFLOAT16 is not supported.
      • cubeMathType=0 is not supported when the input data type is FLOAT32.
      • cubeMathType=1: If the input data type is FLOAT32, it is converted to FLOAT16 for computation. If the input data type is not FLOAT32, no processing is performed.
      • cubeMathType=3 is not supported.
    • Atlas A2 training products/Atlas A2 inference products and Atlas A3 training series products/Atlas A3 inference series products:
      • cubeMathType=1: If the input data type is FLOAT32, it is converted to HFLOAT32 for computation. If the input data type is not FLOAT32, no processing is performed.
      • cubeMathType=2: If the input data type is BFLOAT16, this option is not supported.
      • cubeMathType=3: If the input data type is FLOAT32, it is converted to HFLOAT32 for computation. If the input data type is not FLOAT32, this option is not supported.
  • Returns:

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

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

    Return Error Code Description
    ACLNN_ERR_PARAM_NULLPTR 161001 The passed selfRef, batch1, or batch2 is a null pointer.
    ACLNN_ERR_PARAM_INVALID 161002 The data type of selfRef, batch1, or batch2 is not supported.
    The data format of selfRef, batch1, or batch2 is not supported.
    The data format of selfRef, batch1, or batch2 is inconsistent.
    The broadcast operation cannot be performed between selfRef and batch1@batch2.
    The first dimensions of batch1 and batch2 are not equal.
    The shapes of batch1 and batch2 are not 3D.
    The last dimension of batch1 is not equal to the penultimate dimension of batch2.

aclnnInplaceBaddbmm

  • Parameters:

    Name Input/Output Description
    workspace Input Address of the workspace to be allocated on the device.
    workspaceSize Input Size of the workspace to be allocated on the device, obtained by calling the first-phase API aclnnInplaceBaddbmmGetWorkspaceSize.
    executor Input Operator executor, containing the operator computation process.
    stream Input Stream for executing the task.
  • Returns:

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

Constraints

  • Determinism:
    • Atlas training series products and Atlas inference series products: aclnnBaddbmm&aclnnInplaceBaddbmm defaults to a deterministic implementation.
  • Atlas training series products and Atlas inference series products: The Cube unit does not support FLOAT32 computation. The input data type FLOAT32 can be converted to FLOAT16 in the API for computation by setting cubeMathType to 1 (ALLOW_FP32_DOWN_PRECISION).
  • Atlas A2 training products/Atlas A2 inference products and Atlas A3 training series products/Atlas A3 inference series products: If one of the inputs of batch1 and batch2 is BFLOAT16 and the other is FLOAT or FLOAT16, the data type cannot be deduced.

Example

The following example is for reference only. For details, see Compilation and Running Sample.

#include <iostream>
#include <vector>
#include "acl/acl.h"
#include "aclnnop/aclnn_baddbmm.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;
}

int Init(int32_t deviceId, aclrtStream* stream) {
  // (Fixed writing) Initialize resources.
  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 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 the data on the host to the memory on the device.
  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);

  // Compute the 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 aclCreateTensor 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. (Fixed writing) Initialize the device and stream. For details, see the ACL API manual.
  // Set the device ID in use.
  int32_t deviceId = 0;
  aclrtStream stream;
  auto ret = Init(deviceId, &stream);
  // Handle the check as required.
  CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("Init acl failed. ERROR: %d\n", ret); return ret);

  // 2. Construct the input and output based on the API.
  std::vector<int64_t> selfShape = {1, 2, 4};
  std::vector<int64_t> batch1Shape = {1, 2, 3};
  std::vector<int64_t> batch2Shape = {1, 3, 4};
  std::vector<int64_t> outShape = {1, 2, 4};
  void* selfDeviceAddr = nullptr;
  void* batch1DeviceAddr = nullptr;
  void* batch2DeviceAddr = nullptr;
  void* outDeviceAddr = nullptr;
  aclTensor* self = nullptr;
  aclTensor* batch1 = nullptr;
  aclTensor* batch2 = nullptr;
  aclScalar* alpha = nullptr;
  aclScalar* beta = nullptr;
  aclTensor* out = nullptr;
  std::vector<float> selfHostData = {0, 1, 2, 3, 4, 5, 6, 7};
  std::vector<float> batch1HostData = {1, 1, 1, 2, 2, 2};
  std::vector<float> batch2HostData = {1, 1, 1, 2, 2, 2, 3, 3, 3, 4, 4, 4};
  std::vector<float> outHostData(8, 0);
  int8_t cubeMathType = 1;
  float alphaValue = 1.2f;
  float betaValue = 1.0f;
  // Create a self aclTensor.
  ret = CreateAclTensor(selfHostData, selfShape, &selfDeviceAddr, aclDataType::ACL_FLOAT, &self);
  CHECK_RET(ret == ACL_SUCCESS, return ret);
  // Create a batch1 aclTensor.
  ret = CreateAclTensor(batch1HostData, batch1Shape, &batch1DeviceAddr, aclDataType::ACL_FLOAT, &batch1);
  CHECK_RET(ret == ACL_SUCCESS, return ret);
  // Create a batch2 aclTensor.
  ret = CreateAclTensor(batch2HostData, batch2Shape, &batch2DeviceAddr, aclDataType::ACL_FLOAT, &batch2);
  CHECK_RET(ret == ACL_SUCCESS, return ret);
  // Create an alpha aclScalar.
  alpha = aclCreateScalar(&alphaValue,aclDataType::ACL_FLOAT);
  CHECK_RET(alpha != nullptr, return ret);
  // Create a beta aclScalar.
  beta = aclCreateScalar(&betaValue,aclDataType::ACL_FLOAT);
  CHECK_RET(beta != nullptr, return ret);
  // Create an out aclTensor.
  ret = CreateAclTensor(outHostData, outShape, &outDeviceAddr, aclDataType::ACL_FLOAT, &out);
  CHECK_RET(ret == ACL_SUCCESS, return ret);

  uint64_t workspaceSize = 0;
  aclOpExecutor* executor = nullptr;

  // aclnnBaddbmm call example
  // 3. Call the CANN operator library API.
  // Call the first-phase API of aclnnBaddbmm.
  ret = aclnnBaddbmmGetWorkspaceSize(self, batch1, batch2, alpha, beta, out, cubeMathType, &workspaceSize, &executor);
  CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclnnBaddbmmGetWorkspaceSize failed. ERROR: %d\n", ret); return ret);
  // Allocate device memory based on 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 aclnnBaddbmm.
  ret = aclnnBaddbmm(workspaceAddr, workspaceSize, executor, stream);
  CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclnnBaddbmm failed. ERROR: %d\n", ret); return ret);

  // 4. (Fixed writing) Wait until the task execution is 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. Modify the configuration based on the API definition.
  auto size = GetShapeSize(outShape);
  std::vector<float> resultData(size, 0);
  ret = aclrtMemcpy(resultData.data(), resultData.size() * sizeof(resultData[0]), outDeviceAddr,
                    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 ret);
  for (int64_t i = 0; i < size; i++) {
    LOG_PRINT("result[%ld] is: %f\n", i, resultData[i]);
  }

  // aclnnInplaceBaddbmm call example
  // 3. Call the CANN operator library API.
  LOG_PRINT("\ntest aclnnInplaceBaddbmm\n");
  // Call the first-phase API of aclnnInplaceBaddbmm.
  ret = aclnnInplaceBaddbmmGetWorkspaceSize(self, batch1, batch2, alpha, beta, cubeMathType, &workspaceSize, &executor);
  CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclnnInplaceBaddbmmGetWorkspaceSize failed. ERROR: %d\n", ret); return ret);
  // Allocate device memory based on workspaceSize computed by the first-phase API.
  void* inplaceWorkspaceAddr = nullptr;
  if (workspaceSize > 0) {
    ret = aclrtMalloc(&inplaceWorkspaceAddr, 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 aclnnInplaceBaddbmm.
  ret = aclnnInplaceBaddbmm(inplaceWorkspaceAddr, workspaceSize, executor, stream);
  CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclnnInplaceBaddbmm failed. ERROR: %d\n", ret); return ret);

  // 4. (Fixed writing) Wait until the task execution is 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. Modify the configuration based on the API definition.
  ret = aclrtMemcpy(resultData.data(), resultData.size() * sizeof(resultData[0]), selfDeviceAddr,
                    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 ret);
  for (int64_t i = 0; i < size; i++) {
    LOG_PRINT("result[%ld] is: %f\n", i, resultData[i]);
  }

  // 6. Release aclTensor and aclScalar. Modify the configuration based on the API definition.
  aclDestroyTensor(self);
  aclDestroyTensor(batch1);
  aclDestroyTensor(batch2);
  aclDestroyScalar(alpha);
  aclDestroyScalar(beta);
  aclDestroyTensor(out);

  // 7. Release device resources. Modify the configuration based on the API definition.
  aclrtFree(selfDeviceAddr);
  aclrtFree(batch1DeviceAddr);
  aclrtFree(batch2DeviceAddr);
  aclrtFree(outDeviceAddr);
  if (workspaceSize > 0) {
    aclrtFree(workspaceAddr);
  }
  aclrtFree(inplaceWorkspaceAddr);
  aclrtDestroyStream(stream);
  aclrtResetDevice(deviceId);
  aclFinalize();
  return 0;
}