下载
中文
注册

aclnnConvertWeightToINT4Pack

支持的产品型号

CPU实现不涉及

接口原型

每个算子分为两段式接口,必须先调用“aclnnConvertWeightToINT4PackGetWorkspaceSize”接口获取计算所需workspace大小以及包含了算子计算流程的执行器,再调用“aclnnConvertWeightToINT4Pack”接口执行计算。

  • aclnnStatus aclnnConvertWeightToINT4PackGetWorkspaceSize(const aclTensor *weight, aclTensor *weightInt4Pack, uint64_t *workspaceSize, aclOpExecutor **executor)
  • aclnnStatus aclnnConvertWeightToINT4Pack(void *workspace, uint64_t workspaceSize, aclOpExecutor *executor, aclrtStream stream)

功能描述

算子功能:将int32的输入weight打包为int4,并进行交叠排放。当输出weightInt4Pack的数据格式为FRACTAL_NZ时,会将数据格式从ND转为FRACTAL_NZ之后输出。

  • weightInt4Pack的数据类型为INT32时:输入weight的shape为[dim0, dim1],按int4Pack打包后输出weightInt4Pack的shape为[dim0, dim1/8], 当weightInt4Pack的format为FRACTAL_NZ时,接口内部会将输出数据的数据格式转为FRACTAL_NZ,weightInt4Pack storage shape为[ceil_div(dim1/8, 8), ceil_div(k, 16), 16, 8]。
  • weightInt4Pack的数据类型为INT4时:输入weight的shape为[dim0, dim1],按int4Pack打包后输出weightInt4Pack的shape为[dim0, dim1], 当weightInt4Pack的format为FRACTAL_NZ时,接口内部会将输出数据的数据格式转为FRACTAL_NZ,weightInt4Pack storage shape为[ceil_div(dim1, 64), ceil_div(k, 16), 16, 64]。

aclnnConvertWeightToINT4PackGetWorkspaceSize

  • 参数说明

    • weight(aclTensor*, 计算输入):输入的weight,数据类型支持INT32,数据格式支持ND,维度支持2维,shape支持[k, n]、[n, k]。 当输出weightInt4Pack数据类型为INT4时,要求最后一维度为2对齐。当输出weightInt4Pack数据类型为INT32时,要求最后一维度为8对齐。 输入weight中元素的值需要在int4的表示范围内,即[-8, 7]。不支持非连续Tensor。

    • weightInt4Pack(aclTensor*, 计算输出):INT4打包后的输出,数据类型为INT4或INT32(用1个INT32数据承载8个INT4数据)。数据格式支持ND、FRACTAL_NZ。不支持非连续Tensor。

      对于weightInt4Pack不同的数据格式,weightInt4Pack的shape要求如下:

      • 数据格式为ND时:
        • weightInt4Pack数据类型为INT4时,shape需要和输入weight保持一致为(dim0, dim1)。
        • weightInt4Pack数据类型为INT32时,shape的最后一维度为weight最后一维度的1/8为(dim0, dim1/8);
      • 数据格式为FRACTAL_NZ时:
        • weightInt4Pack数据类型为INT4时,shape需要和输入weight保持一致为(dim0, dim1),storage shape为(ceil_div(dim1, 64), ceil_div(dim0, 16), 16, 64)。
        • weightInt4Pack数据类型为INT32时,view shape的最后一维度为weight最后一维度的1/8为(dim0, dim1/8),storage shape为(ceil_div(n, 64), ceil_div(k, 16), 16, 8)。
    • workspaceSize(uint64_t*, 出参):返回需要在Device侧申请的workspace大小。

    • executor(aclOpExecutor**, 出参):返回op执行器,包含了算子计算流程。

  • 返回值:

    aclnnStatus:返回状态码,具体参见aclnn返回码

    161001 (ACLNN_ERR_PARAM_NULLPTR):如果传入的必选输入、输出、属性是空指针。
    161002 (ACLNN_ERR_PARAM_INVALID):
      - 传入weight、weightInt4Pack的shape维度不符合要求。
      - 传入weight、weightInt4Pack的数据类型不在支持的范围之内。
      - 传入weight、weightInt4Pack的shape大小不符合约束要求。
      - 传入空tensor场景。
      - 输入tensor的Format不是ND。
    361001 (ACLNN_ERR_RUNTIME_ERROR):
      - 数据从host侧拷贝到device侧异常。
      - 数据从device侧拷贝到host侧异常。

aclnnConvertWeightToINT4Pack

  • 参数说明

    • workspace(void*, 入参):在Device侧申请的workspace内存地址。
    • workspaceSize(uint64_t, 入参):在Device侧申请的workspace大小,由第一段接口aclnnConvertWeightToINT4PackGetWorkspaceSize获取。
    • executor(aclOpExecutor*, 入参):op执行器,包含了算子计算流程。
    • stream(aclrtStream, 入参):指定执行任务的AscendCL Stream流。
  • 返回值:

    aclnnStatus:返回状态码,具体参见aclnn返回码

约束与限制

调用示例

示例代码如下,仅供参考,具体编译和执行过程请参考编译与运行样例。 伪量化有aclnnWeightQuantBatchMatmulV2和aclnnWeightQuantBatchMatmulV3接口, 这里以aclnnWeightQuantBatchMatmulV2为例

#include <iostream>
#include <vector>
#include "acl/acl.h"
#include "aclnnop/aclnn_cast.h"
#include "aclnnop/aclnn_weight_quant_batch_matmul_v2.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)

#define CEIL_DIV(x, y) ((((x) + (y)) - 1) / (y))
#define CEIL_ALIGN(x, y) ((((x) + (y)) - 1) / (y) * (y))

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

extern "C" aclnnStatus aclnnConvertWeightToINT4PackGetWorkspaceSize(const aclTensor *weight, const aclTensor *weightInt4Pack,
    uint64_t *workspaceSize, aclOpExecutor **executor);

extern "C" aclnnStatus aclnnConvertWeightToINT4Pack(void *workspace, uint64_t workspaceSize, aclOpExecutor *executor,
    aclrtStream stream);


int Init(int32_t deviceId, aclrtStream* stream) {
  // 固定写法,AscendCL初始化
  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);
  // 调用aclrtMalloc申请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);
  // 调用aclrtMemcpy将host侧数据拷贝到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);

  // 计算连续tensor的strides
  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];
  }

  // 调用aclCreateTensor接口创建aclTensor
  *tensor = aclCreateTensor(shape.data(), shape.size(), dataType, strides.data(), 0, aclFormat::ACL_FORMAT_ND,
                            shape.data(), shape.size(), *deviceAddr);
  return 0;
}

template <typename T>
int CreateAclTensorInt4(const std::vector<T>& hostData, const std::vector<int64_t>& shape, void** deviceAddr,
                    aclDataType dataType, aclTensor** tensor, aclFormat format) {
  auto size = hostData.size() * sizeof(T);
  // 调用aclrtMalloc申请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);
  // 调用aclrtMemcpy将host侧数据拷贝到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);

  // 计算连续tensor的strides
  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];
  }

  // 调用aclCreateTensor接口创建aclTensor
  if (format == aclFormat::ACL_FORMAT_ND) {
    *tensor = aclCreateTensor(shape.data(), shape.size(), dataType, strides.data(), 0, aclFormat::ACL_FORMAT_ND,
                              shape.data(), shape.size(), *deviceAddr);
  } else {
    std::vector<int64_t> nzShape;
    if (dataType == aclDataType::ACL_INT4) {
        nzShape = {CEIL_DIV(shape[1], 64), CEIL_DIV(shape[0], 16), 16, 64};
    } else {
        nzShape = {CEIL_DIV(shape[1], 8), CEIL_DIV(shape[0], 16), 16, 8};
    }
    *tensor = aclCreateTensor(shape.data(), shape.size(), dataType, strides.data(), 0, 
                              aclFormat::ACL_FORMAT_FRACTAL_NZ, nzShape.data(), nzShape.size(), *deviceAddr);
  }

  return 0;
}

int main() {
  // 1. (固定写法)device/stream初始化,参考AscendCL对外接口列表
  // 根据自己的实际device填写deviceId
  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);
  aclDataType weightInt4PackDtype = aclDataType::ACL_INT4;
  aclFormat weightFormat = aclFormat::ACL_FORMAT_FRACTAL_NZ;
  bool isWeightTransposed = true;


  // 2. 构造输入与输出,需要根据API的接口自定义构造
  int64_t m = 16;
  int64_t k = 72;
  int64_t n = 17;
  int64_t weightDim0 = k;
  int64_t weightDim1 = n;
  if (isWeightTransposed) {
    weightDim0 = n;
    weightDim1 = k;
  }
  std::vector<int64_t> xShape = {m, k};
  std::vector<int64_t> weightShape = {weightDim0, weightDim1};
  std::vector<int64_t> weightInt4PackShape;
  if (weightInt4PackDtype == aclDataType::ACL_INT4) {
    weightInt4PackShape = {weightDim0, weightDim1};
  } else {
    weightInt4PackShape = {weightDim0, weightDim1/8};
  }
  std::vector<int64_t> yShape = {m, n};
  void* xDeviceAddr = nullptr;
  void* weightDeviceAddr = nullptr;
  void* weightInt4PackDeviceAddr = nullptr;
  void* yDeviceAddr = nullptr;
  aclTensor* x = nullptr;
  aclTensor* weight = nullptr;
  aclTensor* weightInt4Pack = nullptr;
  aclTensor* y = nullptr;
  std::vector<float> xHostData(m * k, 1);
  std::vector<int32_t> weightHostData(k * n, 1);
  std::vector<float> yHostData(m * n, 0);

  std::vector<int64_t> antiquantScaleShape = {n};
  void* antiquantScaleDeviceAddr = nullptr;
  aclTensor* antiquantScale = nullptr;
  std::vector<float> antiquantScaleHostData(n, 1);

  // 创建x aclTensor
  ret = CreateAclTensor(xHostData, xShape, &xDeviceAddr, aclDataType::ACL_FLOAT, &x);
  CHECK_RET(ret == ACL_SUCCESS, return ret);
  // 创建weight aclTensor
  ret = CreateAclTensor(weightHostData, weightShape, &weightDeviceAddr, aclDataType::ACL_INT32, &weight);
  CHECK_RET(ret == ACL_SUCCESS, return ret);
  if (weightInt4PackDtype == aclDataType::ACL_INT4) {
    std::vector<int8_t> weightInt4PackHostData(n * k / 2, 0); //一个int8数据存放2个int4数据,所以这里除以2
    if (weightFormat == aclFormat::ACL_FORMAT_FRACTAL_NZ) {
      weightInt4PackHostData.resize(CEIL_ALIGN(weightDim1/2, 32) * CEIL_ALIGN(weightDim0, 16), 0);
    }
    // 创建weightInt4Pack aclTensor
    ret = CreateAclTensorInt4(weightInt4PackHostData, weightInt4PackShape, &weightInt4PackDeviceAddr, 
                              weightInt4PackDtype, &weightInt4Pack, weightFormat);
    CHECK_RET(ret == ACL_SUCCESS, return ret);
  } else {
    std::vector<int32_t> weightInt4PackHostData(n * k / 8, 1); //一个int32数据存放8个int4数据,所以这里除以8
    if (weightFormat == aclFormat::ACL_FORMAT_FRACTAL_NZ) {
      weightInt4PackHostData.resize(CEIL_ALIGN(weightDim1/8, 8) * CEIL_ALIGN(weightDim0, 16), 0);
      ret = CreateAclTensorInt4(weightInt4PackHostData, weightInt4PackShape, &weightInt4PackDeviceAddr, 
                                weightInt4PackDtype, &weightInt4Pack, weightFormat);
    } else {
        // 创建weightInt4Pack aclTensor
        ret = CreateAclTensor(weightInt4PackHostData, weightInt4PackShape, &weightInt4PackDeviceAddr, 
                              weightInt4PackDtype, &weightInt4Pack);
    }
    CHECK_RET(ret == ACL_SUCCESS, return ret);
  }
  // 创建y aclTensor
  ret = CreateAclTensor(yHostData, yShape, &yDeviceAddr, aclDataType::ACL_FLOAT, &y);
  CHECK_RET(ret == ACL_SUCCESS, return ret);
  // 创建antiquantScale aclTensor
  ret = CreateAclTensor(antiquantScaleHostData, antiquantScaleShape, &antiquantScaleDeviceAddr, aclDataType::ACL_FLOAT, &antiquantScale);
  CHECK_RET(ret == ACL_SUCCESS, return ret);

  // 创建xFp16 aclTensor
  void* xFp16DeviceAddr = nullptr;
  aclTensor* xFp16 = nullptr;
  ret = CreateAclTensor(xHostData, xShape, &xFp16DeviceAddr, aclDataType::ACL_FLOAT16, &xFp16);
  CHECK_RET(ret == ACL_SUCCESS, return ret);
  // 创建antiquantScale aclTensor
  void* antiquantScaleFp16DeviceAddr = nullptr;
  aclTensor* antiquantScaleFp16 = nullptr;
  ret = CreateAclTensor(antiquantScaleHostData, antiquantScaleShape, &antiquantScaleFp16DeviceAddr, aclDataType::ACL_FLOAT16, &antiquantScaleFp16);
  CHECK_RET(ret == ACL_SUCCESS, return ret);
  // 创建yFp16 aclTensor
  void* yFp16DeviceAddr = nullptr;
  aclTensor* yFp16 = nullptr;
  ret = CreateAclTensor(yHostData, yShape, &yFp16DeviceAddr, aclDataType::ACL_FLOAT16, &yFp16);
  CHECK_RET(ret == ACL_SUCCESS, return ret);

  // 3. 调用CANN算子库API,需要修改为具体的Api名称
  uint64_t workspaceSize = 0;
  aclOpExecutor* executor;
  void* workspaceAddr = nullptr;

  // 对weight做int32转int4pack
  ret = aclnnConvertWeightToINT4PackGetWorkspaceSize(weight, weightInt4Pack, &workspaceSize, &executor);
  CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclnnConvertWeightToINT4PackGetWorkspaceSize failed. ERROR: %d\n", ret); return ret);
  ret = aclnnConvertWeightToINT4Pack(workspaceAddr, workspaceSize, executor, stream);
  CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclnnConvertWeightToINT4Pack failed. ERROR: %d\n", ret); return ret);

  // weight为转置场景,且weightInt4Pack shape为NZ时,需要调用aclInitTensor转换为非连续tensor
  if (isWeightTransposed && weightFormat == aclFormat::ACL_FORMAT_FRACTAL_NZ) { 
    std::vector<int64_t> strides(weightInt4PackShape.size(), 1);
    for (int64_t i = weightInt4PackShape.size() - 2; i >= 0; i--) {
        strides[i] = weightInt4PackShape[i + 1] * strides[i + 1];
    }
    std::swap(strides[0], strides[1]);
    std::swap(weightInt4PackShape[0], weightInt4PackShape[1]);
    std::vector<int64_t> nzShape = {CEIL_DIV(k, 64), CEIL_DIV(n, 16), 16, 8};
    if (weightInt4PackDtype == aclDataType::ACL_INT4) {
        nzShape[3] = 64;
    }
    aclInitTensor(weightInt4Pack, weightInt4PackShape.data(), weightInt4PackShape.size(), weightInt4PackDtype, strides.data(), 0, 
                  weightFormat, nzShape.data(), nzShape.size(), weightInt4PackDeviceAddr);
  }

  // 调用cast生成FP16的输入
  ret = aclnnCastGetWorkspaceSize(x, aclDataType::ACL_FLOAT16, xFp16, &workspaceSize, &executor);
  CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclnnCastGetWorkspaceSize0 failed. ERROR: %d\n", ret); return ret);
  // 根据第一段接口计算出的workspaceSize申请device内存

  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);
  }
  ret = aclnnCast(workspaceAddr, workspaceSize, executor, stream);
  CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclnnCast0 failed. ERROR: %d\n", ret); return ret);

  ret = aclrtSynchronizeStream(stream);
  CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclrtSynchronizeStream failed. ERROR: %d\n", ret); return ret);

  ret = aclnnCastGetWorkspaceSize(antiquantScale, aclDataType::ACL_FLOAT16, antiquantScaleFp16, &workspaceSize, &executor);
  CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclnnCastGetWorkspaceSize1 failed. ERROR: %d\n", ret); return ret);
  // 根据第一段接口计算出的workspaceSize申请device内存

  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);
  }
  ret = aclnnCast(workspaceAddr, workspaceSize, executor, stream);
  CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclnnCast1 failed. ERROR: %d\n", ret); return ret);

  ret = aclrtSynchronizeStream(stream);
  CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclrtSynchronizeStream failed. ERROR: %d\n", ret); return ret);

  // 调用aclnnWeightQuantBatchMatmulV2第一段接口
  ret = aclnnWeightQuantBatchMatmulV2GetWorkspaceSize(xFp16, weightInt4Pack, antiquantScaleFp16, nullptr, nullptr, nullptr, nullptr, 0, yFp16, &workspaceSize, &executor);
  CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclnnWeightQuantBatchMatmulV2GetWorkspaceSize failed. ERROR: %d\n", ret); return ret);
  // 根据第一段接口计算出的workspaceSize申请device内存

  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);
  }
  // 调用aclnnWeightQuantBatchMatmulV2第二段接口
  ret = aclnnWeightQuantBatchMatmulV2(workspaceAddr, workspaceSize, executor, stream);
  CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclnnWeightQuantBatchMatmulV2 failed. ERROR: %d\n", ret); return ret);

  // 4. (固定写法)同步等待任务执行结束
  ret = aclrtSynchronizeStream(stream);
  CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclrtSynchronizeStream failed. ERROR: %d\n", ret); return ret);

 // 将输出转为FP32
  ret = aclnnCastGetWorkspaceSize(yFp16, aclDataType::ACL_FLOAT, y, &workspaceSize, &executor);
  CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclnnCastGetWorkspaceSize2 failed. ERROR: %d\n", ret); return ret);
  // 根据第一段接口计算出的workspaceSize申请device内存

  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);
  }
  ret = aclnnCast(workspaceAddr, workspaceSize, executor, stream);
  CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclnnCast2 failed. ERROR: %d\n", ret); return ret);

  ret = aclrtSynchronizeStream(stream);
  CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclrtSynchronizeStream failed. ERROR: %d\n", ret); return ret);

  // 5. 获取输出的值,将device侧内存上的结果拷贝至host侧,需要根据具体API的接口定义修改
  auto size = GetShapeSize(yShape);
  std::vector<float> resultData(size, 0);
  ret = aclrtMemcpy(resultData.data(), resultData.size() * sizeof(resultData[0]), yDeviceAddr,
                    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. 释放aclTensor和aclScalar,需要根据具体API的接口定义修改
  aclDestroyTensor(x);
  aclDestroyTensor(weight);
  aclDestroyTensor(weightInt4Pack);
  aclDestroyTensor(antiquantScale);
  aclDestroyTensor(y);
  aclDestroyTensor(xFp16);
  aclDestroyTensor(antiquantScaleFp16);
  aclDestroyTensor(yFp16);

  // 7. 释放device资源
  aclrtFree(xDeviceAddr);
  aclrtFree(weightDeviceAddr);
  aclrtFree(weightInt4PackDeviceAddr);
  aclrtFree(antiquantScaleDeviceAddr);
  aclrtFree(yDeviceAddr);
  aclrtFree(xFp16DeviceAddr);
  aclrtFree(antiquantScaleFp16DeviceAddr);
  aclrtFree(yFp16DeviceAddr);

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

  return 0;
}