aclnnGroupedBiasAddGrad

📄 View source code

Supported Products

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

Function

  • Description: Implements the backpropagation of the grouped bias addition (GroupedBiasAdd). The extended API of this API is aclnnGroupedBiasAddGradV2.
  • Formula:
    (1) If groupIdxOptional is available:
out(G,H)={∑i=groupIdxOptional(j−1)groupIdxOptional(j)gradY(i,H),1≤j≤G−1∑i=0groupIdxOptional(j)gradY(i,H),j=0out(G,H) = \begin{cases} \sum_{i=groupIdxOptional(j-1)}^{groupIdxOptional(j)} gradY(i, H), & 1 \leq j \leq G-1 \\ \sum_{i=0}^{groupIdxOptional(j)} gradY(i, H), & j = 0 \end{cases}

  gradY has two dimensions. H indicates the size of the last dimension of gradY, while G indicates the size of dimension 0 of groupIdxOptional. That is, groupIdxOptional has G numbers, and groupIdxOptional(j) indicates the size of the jth dimension. After computation, out is 2-dimensional, with the shape (G, H).
  (2) If groupIdxOptional is unavailable:

out(G,H)=∑i=0CgradY(G,i,H)out(G, H) = \sum_{i=0}^{C} gradY(G, i, H)

  gradY has three dimensions. G, C, and H indicate the sizes of dimensions 0 to 2 of gradY. After computation, out is 2-dimensional, with the shape (G, H).

  • Example:
    (1) If groupIdxOptional is available:
    The shape of gradY is (1000, 30), and the shape of groupIdxOptional is (400, 600, 1000). gradY is divided into three groups, and the accumulated number of rows in each group is 400, 200, and 400 respectively. After computation, the shape of out is (3, 30).
    (2) If groupIdxOptional is unavailable:
    The shape of gradY is (10, 100, 30). gradY is divided into 10 groups. The accumulated number of rows in each group is 100. After computation, the shape of out is (10, 30).

Prototype

Each operator has two-phase API calls. First, aclnnGroupedBiasAddGradGetWorkspaceSize is called to obtain the workspace size required for computation and the executor that contains the operator computation process. Then, aclnnGroupedBiasAddGrad is called to perform computation.

  • aclnnStatus aclnnGroupedBiasAddGradGetWorkspaceSize(const aclTensor *gradY, const aclTensor *groupIdxOptional, aclTensor *out, uint64_t *workspaceSize, aclOpExecutor **executor)
  • aclnnStatus aclnnGroupedBiasAddGrad(void *workspace, uint64_t workspaceSize, aclOpExecutor *executor, aclrtStream stream)

aclnnGroupedBiasAddGradGetWorkspaceSize

  • Parameters:

    • gradY (aclTensor*, computation input): required parameter, backpropagation gradient, gradY in the formula, aclTensor on the device. The data type can be FLOAT, FLOAT16, or BFLOAT16. If groupIdxOptional is available, the shape can only be 2-dimensional. If groupIdxOptional is not available, the shape can only be 3-dimensional. Non-contiguous tensors are supported. The data format can be ND.
    • groupIdxOptional (aclTensor*, computation input): optional parameter, end position of each group, groupIdxOptional in the formula, aclTensor on the device. The data type can be INT32 or INT64. The shape supports only one dimension. Non-contiguous tensors are supported. The data format can be ND.
    • out (aclTensor*, output): gradient of bias, out in the formula, aclTensor on the device. The data type can be FLOAT, FLOAT16, or BFLOAT16. The data type must be the same as that of gradY. The shape supports only two dimensions. Non-contiguous tensors are supported. The data format supports ND.
    • workspaceSize (uint64_t*, output): size of the workspace to be allocated on the device.
    • executor (aclOpExecutor**, output): operator executor, containing the operator computation process.
  • Returns:

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

    The first-phase API performs input parameter validation. The following error codes may be returned:
    161001 (ACLNN_ERR_PARAM_NULLPTR): The passed gradY or out is a null pointer.
    161002 (ACLNN_ERR_PARAM_INVALID): 1. The data type or shape of gradY or out is not supported.
                                     2. The dimensions of gradY, groupIdxOptional, and out do not match.
                                     3. The number of groups exceeds 2048.

aclnnGroupedBiasAddGrad

  • Parameters:

    • workspace (void*, input): address of the workspace to be allocated on the device.
    • workspaceSize (uint64_t, input): size of the workspace to be allocated on the device, which is obtained by the first-phase API aclnnGroupedBiasAddGradGetWorkspaceSize.
    • executor (aclOpExecutor*, input): operator executor, containing the operator computation process.
    • stream (aclrtStream, input): stream for executing the task.
  • Returns:

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

Constraints

  • Deterministic computing:
    • aclnnGroupedBiasAddGrad defaults to a deterministic implementation.
    • groupIdxOptional supports a maximum of 2048 numbers.
    • If groupIdxOptional is available, ensure that the tensor data is sorted in ascending order and the last value is equal to the size of dimension 0 of gradY.
    • If groupIdxOptional is available, ensure that the tensor values are not greater than the maximum INT32 value and are not negative numbers.

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_grouped_bias_add_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 shape_size = 1;
  for (auto i : shape) {
    shape_size *= i;
  }
  return shape_size;
}

int Init(int32_t deviceId, aclrtStream* stream) {
  // (Boilerplate) 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. (Boilerplate) 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 == 0, LOG_PRINT("Init acl failed. ERROR: %d\n", ret); return ret);
  // 2. Construct inputs and outputs based on the API definition.
  std::vector<int64_t> gradYShape = {40, 10};
  std::vector<int64_t> groupIdxShape = {4};
  std::vector<int64_t> outShape = {4, 10};
  void* gradYDeviceAddr = nullptr;
  void* groupIdxDeviceAddr = nullptr;
  void* outDeviceAddr = nullptr;
  aclTensor* gradY = nullptr;
  aclTensor* groupIdx = nullptr;
  aclTensor* out = nullptr;

  std::vector<float> gradYHostData(400, 1.0);

  std::vector<int32_t> groupIdxHostData = {5, 15, 30, 40};
  std::vector<float> outHostData(40, 0.0);

  //Create a gradY aclTensor.
  ret = CreateAclTensor(gradYHostData, gradYShape, &gradYDeviceAddr, aclDataType::ACL_FLOAT, &gradY);
  CHECK_RET(ret == ACL_SUCCESS, return ret);
  // Create a groupIdxOptional aclTensor.
  ret = CreateAclTensor(groupIdxHostData, groupIdxShape, &groupIdxDeviceAddr, aclDataType::ACL_INT32, &groupIdx);
  CHECK_RET(ret == ACL_SUCCESS, return ret);
  // Create an out aclTensor.
  ret = CreateAclTensor(outHostData, outShape, &outDeviceAddr, aclDataType::ACL_FLOAT, &out);
  CHECK_RET(ret == ACL_SUCCESS, return ret);

  // 3. Call the CANN operator library API, which needs to be replaced with the actual API.
  uint64_t workspaceSize = 0;
  aclOpExecutor* executor;
  // Call the first-phase API of aclnnGroupedBiasAddGrad.
  ret = aclnnGroupedBiasAddGradGetWorkspaceSize(gradY, groupIdx, out, &workspaceSize, &executor);
  CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclnnGroupedBiasAddGradGetWorkspaceSize 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 aclnnGroupedBiasAddGrad.
  ret = aclnnGroupedBiasAddGrad(workspaceAddr, workspaceSize, executor, stream);
  CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclnnGroupedBiasAddGrad failed. ERROR: %d\n", ret); return ret);
  // 4. (Boilerplate) 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 code 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(float),
                    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. Modify the configuration based on the API definition.
  aclDestroyTensor(gradY);
  aclDestroyTensor(groupIdx);
  aclDestroyTensor(out);

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