aclnnGroupNormSiluQuant

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 group normalization of the input self, outputs the mean meanOut, the reciprocal of the standard deviation rstdOut, and the quantized result out of the SiLU output.
  • Formula:
    • GroupNorm:

      Let E[x]=xˉE[x] = \bar{x} denote the mean of xx, and Var[x]=1n∗∑i=1n(xi−E[x])2Var[x] = \frac{1}{n} * \sum_{i=1}^n(x_i - E[x])^2 denote the variance of xx. Then

      {groupNormOut=x−E[x]Var[x]+eps∗γ+βmeanOut=E[x]rstdOut=1Var[x]+eps\left\{ \begin{array} {rcl} groupNormOut& &= \frac{x - E[x]}{\sqrt{Var[x] + eps}} * \gamma + \beta \\ meanOut& &= E[x]\\ rstdOut& &= \frac{1}{\sqrt{Var[x] + eps}}\\ \end{array} \right.
    • Silu:

      siluOut=groupNormOut1+e−groupNormOutsiluOut = \frac{groupNormOut}{1+e^{-groupNormOut}}
    • Quant:

      out=round(siluOut/quantScale)out = round(siluOut / quantScale)

Function Prototype

Each operator uses a two-phase API. First, call "aclnnGroupNormSiluQuantGetWorkspaceSize" to obtain the workspace size required for computation and the executor containing the operator computation process. Then, call "aclnnGroupNormSiluQuant" to perform the computation.

aclnnStatus aclnnGroupNormSiluQuantGetWorkspaceSize(
    const aclTensor* self,
    const aclTensor* gammaOptional,
    const aclTensor* betaOptional,
    const aclTensor* quantScale,
    int64_t          group,
    double           eps,
    bool             activateSilu,
    aclTensor*       out,
    aclTensor*       meanOut,
    aclTensor*       rstdOut,
    uint64_t*        workspaceSize,
    aclOpExecutor**  executor);
aclnnStatus aclnnGroupNormSiluQuant(
    void *         workspace,
    uint64_t       workspaceSize,
    aclOpExecutor *executor,
    aclrtStream    stream)

aclnnGroupNormSiluQuantGetWorkspaceSize

  • Parameters

    Parameter Input/Output Description Instruction Data Type Data Format Dimension (shape) Non-contiguous Tensor
    self Input x in the formula. Empty tensors are supported: N (dimension 0) and C (dimension 1) must be greater than 0, and other dimensions can be 0. In this case, out is empty, meanOut is filled with 0, and rstdOut is filled with NAN. FLOAT16, BFLOAT16 ND 2-8, where dimension 0 is N and dimension 1 is C √
    gammaOptional Optional input γ in the formula. When empty, the default value of elements is 1. The data type must remain consistent with self, and the number of elements must be the same as that of dimension 1 of the input self. FLOAT16, BFLOAT16 ND 1 √
    betaOptional Optional input β in the formula. When empty, the default value of elements is 1. The data type must remain consistent with self, and the number of elements must be the same as that of dimension 1 of the input self. FLOAT16, BFLOAT16 ND 1 √
    quantScale Input quantScale in the formula. The number of elements must be 1 or the same as the first dimension of input self. FLOAT32 ND 1 √
    group Input Indicates that the first dimension of input self is divided into group groups. The first dimension of self must be divisible by group. INT64 - - -
    eps Input eps in the formula. eps must be greater than 0. DOUBLE - - -
    activateSilu Input Whether to enable silu computation. Currently, only enabled is supported. BOOL - - -
    out Output Quantized result, which is out in the formula. - INT8 ND consistent with self √
    meanOut Output meanOut in the formula. The data type remains consistent with self, and N in the shape remains consistent with the 0th dimension of self. FLOAT16, BFLOAT16 ND (N, group) √
    rstdOut Output rstdOut in the formula. Data type remains consistent with self, and N in shape remains consistent with the 0th dimension of self. FLOAT16, BFLOAT16 ND (N, group) √
    workspaceSize Output Returns the size of the workspace to be allocated on the Device. - - - - -
    executor Output Returns the op executor containing the operator computation process. - - - - -
  • Return Value

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

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

    Error Code Error Code Description
    ACLNN_ERR_PARAM_NULLPTR 161001 If a required input, output, or attribute is a null pointer, 161001 is returned.
    ACLNN_ERR_PARAM_INVALID 161002 The data types of the input and output are not supported.
    The input and output parameters do not meet the constraints described in Parameters.

aclnnGroupNormSiluQuant

  • Parameters

    ParameterInput/OutputDescription
    workspaceInputAddress of the workspace memory requested on the device.
    workspaceSizeInputSize of the workspace memory requested on the device, obtained by calling the first-phase API aclnnGroupNormSiluQuantGetWorkspaceSize.
    executorInput op executor that contains the operator computation process.
    streamInput Specifies the stream for task execution.
  • Return Value

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

Constraints

  • Deterministic computation:
    • aclnnGroupNormSiluQuant defaults to a deterministic implementation.

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 "acl/acl.h"
#include "aclnnop/aclnn_group_norm_silu_quant.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: 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 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 host data 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);

  // 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 device/stream. For details, see the list of external acl APIs.
  // Set deviceId based on the actual device.
  int32_t deviceId = 0;
  aclrtStream stream;
  auto ret = Init(deviceId, &stream);
  // Handle check as needed.
  CHECK_RET(ret == 0, LOG_PRINT("Init acl failed. ERROR: %d\n", ret); return ret);
  // 2. Construct the input and output as required by the API.
  std::vector<int64_t> selfShape = {1, 2, 4, 4};    // Main input
  std::vector<int64_t> gammaShape = {2};      // gamma parameter
  std::vector<int64_t> betaShape = {2};       // beta parameter
  std::vector<int64_t> quantScaleShape = {1}; // quantization scale factor
  std::vector<int64_t> outShape = {1, 2, 4, 4};     // quantized output
  std::vector<int64_t> meanOutShape = {1, 2};  // mean output [N, G]
  std::vector<int64_t> rstdOutShape = {1, 2};   // reciprocal standard deviation output [N, G]

  void* selfDeviceAddr = nullptr;
  void* gammaDeviceAddr = nullptr;
  void* betaDeviceAddr = nullptr;
  void* quantScaleDeviceAddr = nullptr;
  void* outDeviceAddr = nullptr;
  void* meanOutDeviceAddr = nullptr;
  void* rstdOutDeviceAddr = nullptr;
  aclTensor* self = nullptr;
  aclTensor* gamma = nullptr;
  aclTensor* beta = nullptr;
  aclTensor* quantScale = nullptr;
  aclTensor* out = nullptr;
  aclTensor* meanOut = nullptr;
  aclTensor* rstdOut = nullptr;
  // Use uint16_t to store BF16/FP16 bit patterns.
  std::vector<uint16_t> selfHostData = {
  0x3C00, 0x4000, 0x4200, 0x4400, // FP16 bit patterns of 1.0, 2.0, 3.0, 4.0
  0x4500, 0x4600, 0x4700, 0x4800, // FP16 bit patterns of 5.0, 6.0, 7.0, 8.0
  0x3C00, 0x4000, 0x4200, 0x4400, // FP16 bit patterns of 1.0, 2.0, 3.0, 4.0
  0x4500, 0x4600, 0x4700, 0x4800, // FP16 bit patterns of 5.0, 6.0, 7.0, 8.0
  0x3C00, 0x4000, 0x4200, 0x4400, // FP16 bit pattern of 1.0, 2.0, 3.0, 4.0
  0x4500, 0x4600, 0x4700, 0x4800, // FP16 bit pattern of 5.0, 6.0, 7.0, 8.0
  0x3C00, 0x4000, 0x4200, 0x4400, // FP16 bit pattern of 1.0, 2.0, 3.0, 4.0
  0x4500, 0x4600, 0x4700, 0x4800 // FP16 bit pattern of 5.0, 6.0, 7.0, 8.0
  };
  std::vector<uint16_t> gammaHostData = {0x3C00, 0x3C00}; // FP16 bit pattern of 1.0, 1.0
  std::vector<uint16_t> betaHostData = {0x0000, 0x0000}; // FP16 bit pattern of 0.0, 0.0
  std::vector<float> quantScaleHostData = {1.0};
  std::vector<int8_t> outHostData = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
                                    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}; // Initialized to 0; actually filled by the operator
  // The statistics outputs also use the half-precision format
  std::vector<uint16_t> meanOutHostData = {0x0000, 0x0000}; // Initialized to 0
  std::vector<uint16_t> rstdOutHostData = {0x0000, 0x0000}; // Initialized to 0

  int64_t group = 2;
  double eps = 0.00001;
  bool activateSilu = true;
  // Create the self aclTensor.
  ret = CreateAclTensor(selfHostData, selfShape, &selfDeviceAddr, aclDataType::ACL_FLOAT16, &self);
  CHECK_RET(ret == ACL_SUCCESS, return ret);
  // Create the gamma aclTensor.
  ret = CreateAclTensor(gammaHostData, gammaShape, &gammaDeviceAddr, aclDataType::ACL_FLOAT16, &gamma);
  CHECK_RET(ret == ACL_SUCCESS, return ret);
  // Create the beta aclTensor.
  ret = CreateAclTensor(betaHostData, betaShape, &betaDeviceAddr, aclDataType::ACL_FLOAT16, &beta);
  CHECK_RET(ret == ACL_SUCCESS, return ret);
  // Create the quantScale aclTensor.
  ret = CreateAclTensor(quantScaleHostData, quantScaleShape, &quantScaleDeviceAddr, aclDataType::ACL_FLOAT, &quantScale);
  CHECK_RET(ret == ACL_SUCCESS, return ret);  // Create the out aclTensor.
  ret = CreateAclTensor(outHostData, outShape, &outDeviceAddr, aclDataType::ACL_INT8, &out);
  CHECK_RET(ret == ACL_SUCCESS, return ret);
  // Create the meanOut aclTensor.
  ret = CreateAclTensor(meanOutHostData, meanOutShape, &meanOutDeviceAddr, aclDataType::ACL_FLOAT16, &meanOut);
  CHECK_RET(ret == ACL_SUCCESS, return ret);
  // Create the rstdOut aclTensor.
  ret = CreateAclTensor(rstdOutHostData, rstdOutShape, &rstdOutDeviceAddr, aclDataType::ACL_FLOAT16, &rstdOut);
  CHECK_RET(ret == ACL_SUCCESS, return ret);

  // 3. Call the CANN operator library API. Modify it to the specific API.
  uint64_t workspaceSize = 0;
  aclOpExecutor* executor;
  // Call the first-phase API of aclnnGroupNormSiluQuant
  ret = aclnnGroupNormSiluQuantGetWorkspaceSize(self, gamma, beta, quantScale, group, eps, activateSilu, out, meanOut, rstdOut, &workspaceSize, &executor);
  CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclnnGroupNormSiluQuantGetWorkspaceSize 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 aclnnGroupNormSiluQuant
  ret = aclnnGroupNormSiluQuant(workspaceAddr, workspaceSize, executor, stream);
  CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclnnGroupNormSiluQuant failed. ERROR: %d\n", ret); return ret);
  // 4. (boilerplate) Wait for the task execution to end synchronously
  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 it according to the specific API definition
  auto size = GetShapeSize(outShape);
  std::vector<int8_t> outResultData(size, 0);
  ret = aclrtMemcpy(outResultData.data(), outResultData.size() * sizeof(outResultData[0]), outDeviceAddr, size * sizeof(int8_t),
                    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("outResultData[%ld] is: %d\n", i, outResultData[i]);
  }

  // Receive the meanOut result
  size = GetShapeSize(meanOutShape);
  std::vector<uint16_t> meanResultData(size, 0);
  ret = aclrtMemcpy(meanResultData.data(),
                    meanResultData.size() * sizeof(uint16_t), // 2 bytes
                    meanOutDeviceAddr,
                    size * sizeof(uint16_t), // 2 bytes
                    ACL_MEMCPY_DEVICE_TO_HOST);
  CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("copy meanOut from device to host failed. ERROR: %d\n", ret); return ret);

  // Receive the rstdOut result
  size = GetShapeSize(rstdOutShape);
  std::vector<uint16_t> rstdResultData(size, 0);
  ret = aclrtMemcpy(rstdResultData.data(),
                    rstdResultData.size() * sizeof(uint16_t), // 2 bytes
                    rstdOutDeviceAddr,
                    size * sizeof(uint16_t), // 2 bytes
                    ACL_MEMCPY_DEVICE_TO_HOST);
  CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("copy rstdOut from device to host failed. ERROR: %d\n", ret); return ret);

  // Print the result (convert to float for readability)
  for (int64_t i = 0; i < meanResultData.size(); i++) {
    // Simple conversion: convert the FP16 bit pattern to float.
    __fp16 fp16_val = *reinterpret_cast<__fp16*>(&meanResultData[i]);
    float fp32_val = static_cast<float>(fp16_val);
    LOG_PRINT("meanResultData[%ld] is: %f\n", i, fp32_val);
  }

  for (int64_t i = 0; i < rstdResultData.size(); i++) {
    __fp16 fp16_val = *reinterpret_cast<__fp16*>(&rstdResultData[i]);
    float fp32_val = static_cast<float>(fp16_val);
    LOG_PRINT("rstdResultData[%ld] is: %f\n", i, fp32_val);
  }

  // 6. Release aclTensor and aclScalar. Modify it according to the specific API definition.
  aclDestroyTensor(self);
  aclDestroyTensor(gamma);
  aclDestroyTensor(beta);
  aclDestroyTensor(quantScale);
  aclDestroyTensor(out);
  aclDestroyTensor(meanOut);
  aclDestroyTensor(rstdOut);

  // 7. Release device resources. Modify it according to the specific API definition.
  aclrtFree(selfDeviceAddr);
  aclrtFree(gammaDeviceAddr);
  aclrtFree(betaDeviceAddr);
  aclrtFree(quantScaleDeviceAddr);
  aclrtFree(outDeviceAddr);
  aclrtFree(meanOutDeviceAddr);
  aclrtFree(rstdOutDeviceAddr);

  if (workspaceSize > 0) {
    aclrtFree(workspaceAddr);
  }
  aclrtDestroyStream(stream);
  aclrtResetDevice(deviceId);
  aclFinalize();
  return 0;
}