aclnnDequantSwigluQuantV2

Applicable Products

  • Ascend 950PR&950DT products: 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: Adds dequant and quant operations before and after the Swish gated linear unit activation function to implement the DequantSwigluQuant computation on x. This API has two types of new parameters compared with aclnnDequantSwigluQuant:

    1. Adds three new input parameters used on Ascend 950PR&950DT products: dstType, roundModeOptional, and activateDim.
    1. Adds four parameters for the SwiGLU variant used by GPT-OSS on Atlas A3 products and Atlas A2 products: swigluMode, clampLimit, gluAlpha, and gluBias. When this API is used on Ascend 950PR&950DT products, default values must be set for these four parameters. Choose an appropriate API based on the actual situation.
  • Formula when swigluMode is 0:

    dequantOuti=Dequant(xi)dequantOut_i = Dequant(x_i) swigluOuti=Swiglu(dequantOuti)=Swish(Ai)∗BiswigluOut_i = Swiglu(dequantOut_i)=Swish(A_i)*B_i outi=Quant(swigluOuti)out_i = Quant(swigluOut_i)

    where Ai indicates the first half of dequantOuti, and Bi indicates the second half of dequantOuti.

  • Formula when swigluMode is 1:

    dequantOuti=Dequant(xi)dequantOut_i = Dequant(x_i) x_glu=x_glu.clamp(min=None,max=clampLimit)x\_glu = x\_glu.clamp(min=None, max=clampLimit) x_linear=x_linear.clamp(min=−clampLimit,max=clampLimit)x\_linear = x\_linear.clamp(min=-clampLimit, max=clampLimit) out_glu=x_glu∗sigmoid(gluAlpha∗x_glu)out\_glu = x\_glu * sigmoid(gluAlpha * x\_glu) swigluOuti=out_glu∗(x_linear+gluBias)swigluOut_i = out\_glu * (x\_linear + gluBias) outi=Quant(swigluOuti)out_i = Quant(swigluOut_i)

    where, x_glu indicates the even-index part of dequantOuti, and x_linear indicates the odd-index part of dequantOuti.

  • Formula when swigluMode is 2:

    The calculation logic is the same as that when swigluMode is 1 (both are the GPT-OSS variant SwiGLU, using clampLimit, gluAlpha, and gluBias):

    dequantOuti=Dequant(xi)dequantOut_i = Dequant(x_i) x_glu=x_glu.clamp(min=None,max=clampLimit)x\_glu = x\_glu.clamp(min=None, max=clampLimit) x_linear=x_linear.clamp(min=−clampLimit,max=clampLimit)x\_linear = x\_linear.clamp(min=-clampLimit, max=clampLimit) out_glu=x_glu∗sigmoid(gluAlpha∗x_glu)out\_glu = x\_glu * sigmoid(gluAlpha * x\_glu) swigluOuti=out_glu∗(x_linear+gluBias)swigluOut_i = out\_glu * (x\_linear + gluBias) outi=Quant(swigluOuti)out_i = Quant(swigluOut_i)

    The difference from swigluMode 1 lies in the splitting method of x_glu and x_linear: when swigluMode is 2, x_glu indicates the first half of dequantOuti, and x_linear indicates the second half of dequantOuti (consistent with the splitting method when swigluMode is 0); when swigluMode is 1, the splitting is interleaved by even and odd indexes.

  • The formula when swigluMode is 3 (SwiGLU variant, using clampLimit):

    dequantOuti=Dequant(xi)dequantOut_i = Dequant(x_i) gate=dequantOuti[:,:H/2]gate = dequantOut_i[:, :H/2] up=dequantOuti[:,H/2:]up = dequantOut_i[:, H/2:] gate=SiLU(gate)=gate∗sigmoid(gate)gate = SiLU(gate) = gate * sigmoid(gate) gate=gate.clamp(max=clampLimit)gate = gate.clamp(max=clampLimit) up=up.clamp(min=−clampLimit,max=clampLimit)up = up.clamp(min=-clampLimit, max=clampLimit) swigluOuti=gate∗upswigluOut_i = gate * up outi=Quant(swigluOuti)out_i = Quant(swigluOut_i)

    where gate indicates the first half of dequantOuti, and up indicates the second half of dequantOuti (the splitting method is consistent with that of swigluMode 0). swigluMode 3 combines the SiLU activation method of swigluMode 0 with the front-back splitting and clamping mechanism of swigluMode 2, but does not use the gluAlpha and gluBias parameters.

Function Prototype

Each operator uses a two-phase API. You must first call the "aclnnDequantSwigluQuantV2GetWorkspaceSize" API to obtain the workspace size required for the calculation and the executor that contains the calculation flow of the operator, and then call the "aclnnDequantSwigluQuantV2" API to perform the calculation.

aclnnStatus aclnnDequantSwigluQuantV2GetWorkspaceSize(
  const aclTensor *x,
  const aclTensor *weightScaleOptional,
  const aclTensor *activationScaleOptional,
  const aclTensor *biasOptional,
  const aclTensor *quantScaleOptional,
  const aclTensor *quantOffsetOptional,
  const aclTensor *groupIndexOptional,
  bool             activateLeft,
  char            *quantModeOptional,
  int64_t          dstType,
  char            *roundModeOptional,
  int64_t          activateDim,
  int64_t          swigluMode,
  double           clampLimit,
  double           gluAlpha,
  double           gluBias,
  const aclTensor *yOut,
  const aclTensor *scaleOut,
  uint64_t        *workspaceSize,
  aclOpExecutor  **executor)
aclnnStatus aclnnDequantSwigluQuantV2(
  void          *workspace,
  uint64_t       workspaceSize,
  aclOpExecutor *executor,
  aclrtStream    stream)

aclnnDequantSwigluQuantV2

  • Parameters:

    Parameter Input/Output Description Instruction Data Type Data Format Dimension (shape) Non-contiguous Tensor
    x(aclTensor*) Input Input data to be processed, that is, x in the formula.
    • The shape is [X1,X2,...Xn,2H], with 8 dimensions at most and 2 dimensions at least.
    • The dimension of input x corresponding to activateDim must be a multiple of 2.
    FLOAT16, BFLOAT16, INT32 ND 2-8 x
    weightScaleOptional (aclTensor*) Input Dequantization scale of weight.
    • The shape supports 1 dimension or 2 dimensions and is expressed as [2H] or [groupNum, 2H]. The value of 2H must be consistent with the last dimension of x.
    • Optional parameter. Supports passing a null pointer. When groupIndexOptional is a null pointer, the shape is [2H] or [1, 2H]. When groupIndexOptional is a non-null pointer, the shape is [groupNum, 2H].
    FLOAT ND 1 or 2 x
    activationScaleOptional (aclTensor*) Input Dequantization scale of the activation function.
    • Dequantization scale of the activation function.
    • The shape is [X1,X2,...Xn], with 1 to 7 dimensions, one fewer dimension than x, and consistent with the shape of x in the corresponding dimension.
    • Optional parameter. Supports passing a null pointer.
    FLOAT ND 1-7 x
    biasOptional (aclTensor*) Input Bias of Matmul, which is biasOptional in the formula.
    • The shape supports 1 dimension or 2 dimensions, expressed as [2H] or [groupNum, 2H], and the value of 2H is consistent with the last dimension of x. When groupIndexOptional is a null pointer, the shape is [2H]; when groupIndexOptional is a non-null pointer, the shape is [groupNum, 2H].
    • Optional parameter that supports passing null.
    FLOAT, FLOAT16, BFLOAT16, INT32 ND 1 or 2 x
    quantScaleOptional (aclTensor*) Input Quantization scale, which is quantScaleOptional in the formula.
    • When quantModeOptional is static, the shape is 1 dimension and the value is 1, expressed as shape[1].
    • When quantModeOptional is dynamic, the shape is 1 dimension or 2 dimensions, expressed as [H], [2H], or [groupNum, H].
    • When groupIndexOptional is a null pointer and activateDim is the last axis, the shape is [H].
    • When groupIndexOptional is a non-null pointer and activateDim is the last axis, the shape is [groupNum, H].
    FLOAT ND - x
    quantOffsetOptional (aclTensor*) Input Quantized offset.
    • When quant_mode is dynamic, the quantOffset input is not required. In static quantization, quantOffset is required, and its data type and shape must be the same as those of quantScale.
    FLOAT ND - x
    groupIndexOptional (aclTensor*) Input group_index required for MoE grouping.
    • The shape supports tensors of 1 dimension or 2 dimensions, that is, [groupNum] or [groupNum, 2], and groupNum must be greater than or equal to 1.
    • Optional parameter. Supports passing a null pointer.
    INT64 ND 1 or 2 x
    activateLeft (bool) Input Indicates whether to perform swiglu activation on the first half of the input. When the value is false, the activation is performed on the second half of the input. If swigluMode is 1, activateLeft must be true. - - - -
    quantModeOptional (char*) Input Indicates whether to use dynamic quantization or static quantization.
    • Supports "dynamic" and "static".
    • Supports passing null. If a null pointer is passed, "static" is used by default.
    - - - -
    dstType (int64_t) Input Indicates the specified data type of output y.
    • The value range of dstType is: [2, 34,35, 36, 40, 41], corresponding to INT8, HIFLOAT8, FLOAT8_E5M2, FLOAT8_E4M3FN, FLOAT4_E2M1, and FLOAT4_E1M2 respectively.
    - - - -
    roundModeOptional (char*) Input Indicates the rounding mode for output y.
    • The value range is ["rint", "round", "floor", "ceil", "trunc"].
    • When the data type of output y is INT8, FLOAT8_E5M2, or FLOAT8_E4M3FN, only the "rint" mode is supported.
    • When the data type of output y is HIFLOAT8, only the "round" mode is supported.
    • Supports passing a null pointer. If a null pointer is passed, "rint" is used by default.
    - - - -
    activateDim (int64_t) Input Indicates the specified splitting axis selected for the swish calculation.
    • The value range of activateDim is [-xDim, xDim - 1] (where xDim indicates the dimension of input x).
    • When activateDim does not correspond to the last axis of x, groupIndexOptional is not allowed as an input.
    • When activateDim does not correspond to the last axis of x, quantModeOptional only supports static.
    - - - -
    swigluMode (int64_t) Input Indicates the calculation mode of swiglu.
    • The value range is [0, 1, 2, 3], and the default value is 0.
    • 0: indicates the traditional swiglu calculation method.
    • 1: indicates the swiglu variant (GPT-OSS variant), which uses odd-even chunking and supports clampLimit, the gluAlpha activation coefficient, and the gluBias bias.
    • 2: the calculation method is the same as that of 1 (also a swiglu variant, supporting clampLimit, gluAlpha, and gluBias), but uses consecutive first-half/second-half chunking (the same splitting method as 0) instead of odd-even chunking.
    • 3: a swiglu variant whose splitting method is the same as 0 and 2 (consecutive first-half/second-half chunking). The calculation method differs from 2 in that clamp is performed after silu, and gluAlpha and gluBias are not supported.
    - - - -
    clampLimit (double) Input Indicates the threshold value used by the swiglu variant.
    • This parameter is an optional parameter.
    • Used to clip the input. Its value must be greater than 0 and less than infinity to prevent excessively large values from affecting the stability of the swiglu calculation. Default value: 7.0.
    - - - -
    gluAlpha (double) Input Indicates the parameter used by the swiglu variant.
    • This parameter is an optional parameter.
    • Adjusts the scaling of the linear part in the glu activation function. The default value is 1.702.
    - - - -
    gluBias (double) Input Indicates the bias parameter used by the swiglu variant.
    • This parameter is an optional parameter.
    • Used to add an offset in the linear calculation of swiglu. The default value is 1.0.
    - - - -
    yOut (aclTensor*) Output -
    • When activateDim corresponds to the last axis of x, the shape is [X1,X2,...Xn,H].
    • When activateDim does not correspond to the last axis of x, the shape is [X1,X2,...,XactivateDim / 2,...,2H].
    • When the data type of yOut is FLOAT4_E2M1 or FLOAT4_E1M2, the last dimension of yOut must be a multiple of 2.
    • When activateDim does not correspond to the last axis of x, the last axis of yOut must be less than 5120.
    INT8, HIFLOAT8, FLOAT8_E5M2, FLOAT8_E4M3FN, FLOAT4_E2M1, FLOAT4_E1M2 ND 2-8 x
    scaleOut (aclTensor*) Output -
    • When activateDim corresponds to the last axis of x, the shape is [X1,X2,...,Xn].
    • When activateDim does not correspond to the last axis of x, the shape is [X1,X2,...,XactivateDim / 2,...,Xn].
    • When quantModeOptional is static, scaleOut is not calculated.
    FLOAT ND 1-7 x
    workspaceSize (uint64_t*) Output Returns the workspace size to be side-applied on the Device. - - - - -
    executor (aclOpExecutor**) Output Returns the op executor, which contains the operator calculation flow. - - - - -
    • Atlas A2 products, Atlas A3 products:
      • In the x parameter, only 2-dimensional input with data type INT32 or BFLOAT16 is supported.
      • In the quantScaleOptional parameter, when quantModeOptional is static, the shape is expressed as [groupNum] or [groupNum, H]; when quantModeOptional is dynamic, the shape is expressed as [groupNum] or [groupNum, H].
      • In the groupIndexOptional parameter, only 1-dimensional input [groupNum] is supported, and null pointers are not supported.
      • In the dstType parameter, only input 2-INT8 is supported.
      • In the roundModeOptional parameter, only a null pointer or "rint" is supported as input.
      • In the activateDim parameter, only -1 is supported.
  • 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 Required parameters x, yOut, and scaleOut are null pointers.
    ACLNN_ERR_PARAM_INVALID 161002 The data type of the input or output is not within the supported range.
    ACLNN_ERR_INNER_TILING_ERROR 561002 When the data type of x is int32, weightScaleOptional is a null pointer.
    When quantModeOptional is static, quantScaleOptional is a null pointer.
    quantModeOptional is not within the specified value range.
    dstType is not within the specified value range.
    roundModeOptional is not within the specified value range.
    activateDim is not within the specified value range.
    The shapes of weightScaleOptional, activationScaleOptional, biasOptional, quantScaleOptional, and groupIndexOptional do not satisfy the constraints with x.

aclnnDequantSwigluQuantV2

  • Parameters:

    Parameter Input/Output Description
    workspace Input Workspace memory address side-applied on the Device.
    workspaceSize Input Workspace size side-applied on the Device, obtained by the first-phase API aclnnDequantSwigluQuantV2GetWorkspaceSize.
    executor Input Op executor, which contains the calculation process of the operator.
    stream Input Stream for executing tasks.
  • Return value

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

Constraints

  • Deterministic computation:

    • aclnnDequantSwigluQuantV2 defaults to a deterministic implementation.
  • The dimension of input x corresponding to activateDim must be a multiple of 2, and the number of dimensions of x must be greater than 1.

  • When the data type of input x is INT32, weightScaleOptional cannot be null. When the data type of input x is not INT32, weightScaleOptional is not allowed and a null pointer must be passed.

  • When the data type of input x is not INT32, activationScaleOptional is not allowed and a null pointer must be passed.

  • When the data type of input x is not INT32, biasOptional is not allowed and a null pointer must be passed.

  • When the data type of output yOut is FLOAT4_E2M1 or FLOAT4_E1M2, the last dimension of yOut must be a multiple of 2.

  • When the dimension corresponding to activateDim is not the last dimension of x, the last dimension of output yOut does not exceed 5120.

  • The sum of all elements in groupIndexOptional cannot exceed the product of the remaining axes of input x excluding the tail axis.

  • The portion of outputs yOut and scaleOut that exceeds the sum of all elements in groupIndexOptional is not cleared, and that portion of memory contains garbage data.

  • Atlas A2 products, Atlas A3 products:
    • When groupIndexOptional is input, the memory size of input tensors supported by the operator has an upper limit, and the tail axis of x must not exceed 7232.
    • The last dimension of x must be divisible by 64.
    • When quant_mode is dynamic, quant_offset must be empty.

Example

The sample code is as follows and is for reference only. For details about the compilation and execution process, see Compile and Run Samples.

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

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) {
  // Boilerplate, resource 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 host-side data to the device-side 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 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 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. For details, see the acl API manual.
  // 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 inputs and outputs. Customize the construction based on the API.
  std::vector<int64_t> xShape = {2, 64};
  std::vector<int64_t> weightScaleShape = {1, 64};
  std::vector<int64_t> activationScaleShape = {2};
  std::vector<int64_t> biasShape = {1, 64};
  std::vector<int64_t> scaleShape = {1, 32}; // quantscale
  std::vector<int64_t> offsetShape = {1};
  std::vector<int64_t> groupIndexShape = {1};
  std::vector<int64_t> outShape = {2, 32};
  std::vector<int64_t> scaleOutShape = {2};

  void* xDeviceAddr = nullptr;
  void* weightScaleDeviceAddr = nullptr;
  void* activationScaleDeviceAddr = nullptr;
  void* biasDeviceAddr = nullptr;

  void* scaleDeviceAddr = nullptr;
  void* offsetDeviceAddr = nullptr;
  void* groupIndexDeviceAddr = nullptr;
  void* outDeviceAddr = nullptr;
  void* scaleOutDeviceAddr = nullptr;

  aclTensor* x = nullptr;
  aclTensor* weightScale = nullptr;
  aclTensor* activationScale= nullptr;
  aclTensor* bias = nullptr;

  aclTensor* scale = nullptr;
  aclTensor* offset = nullptr;
  aclTensor* groupIndex = nullptr;
  aclTensor* out = nullptr;
  aclTensor* scaleOut = nullptr;

  std::vector<int32_t> xHostData = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22,
                                    23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42,
                                    43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63,
                                    0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22,
                                    23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42,
                                    43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63};
  std::vector<float> weightScaleData(64, 1.0);
  std::vector<float> activationScaleData(2, 1.0);
  std::vector<float> biasData(64, 1.0);
  std::vector<int64_t> groupIndexData = {1};
  std::vector<float> scaleHostData(32, 1);
  std::vector<float> offsetHostData = {1};
  std::vector<int8_t> outHostData(64, 0);
  std::vector<float> scaleOutHostData = {0, 0};

  bool activateLeft = true;
  int64_t dstType = 2;
  int64_t activateDim = -1;
  int64_t swigluMode = 1;
  float clampLimit = 7.0;
  float gluAlpha = 1.702;
  float gluBias = 1.0;

  // Create the x aclTensor.
  ret = CreateAclTensor(xHostData, xShape, &xDeviceAddr, aclDataType::ACL_INT32, &x);
  CHECK_RET(ret == ACL_SUCCESS, return ret);
  // Create the weightScale aclTensor.
  ret = CreateAclTensor(weightScaleData, weightScaleShape, &weightScaleDeviceAddr, aclDataType::ACL_FLOAT, &weightScale);
  CHECK_RET(ret == ACL_SUCCESS, return ret);
  // Create activationScale.
  ret = CreateAclTensor(activationScaleData, activationScaleShape, &activationScaleDeviceAddr, aclDataType::ACL_FLOAT, &activationScale);
  CHECK_RET(ret == ACL_SUCCESS, return ret);
  // Create bias.
  ret = CreateAclTensor(biasData, biasShape, &biasDeviceAddr, aclDataType::ACL_FLOAT, &bias);
  CHECK_RET(ret == ACL_SUCCESS, return ret);
  // Create the scale aclTensor.
  ret = CreateAclTensor(scaleHostData, scaleShape, &scaleDeviceAddr, aclDataType::ACL_FLOAT, &scale);
  CHECK_RET(ret == ACL_SUCCESS, return ret);
  // Create the offset aclTensor.
  ret = CreateAclTensor(offsetHostData, offsetShape, &offsetDeviceAddr, aclDataType::ACL_FLOAT, &offset);
  CHECK_RET(ret == ACL_SUCCESS, return ret);
  // Create the groupIndex aclTensor.
  ret = CreateAclTensor(groupIndexData, groupIndexShape, &groupIndexDeviceAddr, aclDataType::ACL_INT64, &groupIndex);
  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 scaleOut aclTensor.
  ret = CreateAclTensor(scaleOutHostData, scaleOutShape, &scaleOutDeviceAddr, aclDataType::ACL_FLOAT, &scaleOut);
  CHECK_RET(ret == ACL_SUCCESS, return ret);
  // 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 aclnnDequantSwigluQuantV2.
  ret = aclnnDequantSwigluQuantV2GetWorkspaceSize(x, weightScale, activationScale, bias, scale, nullptr, groupIndex, activateLeft, "dynamic", dstType, "rint", activateDim, swigluMode, clampLimit, gluAlpha, gluBias, out, scaleOut, &workspaceSize, &executor);
  CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclnnDequantSwigluQuantV2GetWorkspaceSize 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 aclnnDequantSwigluQuantV2.
  ret = aclnnDequantSwigluQuantV2(workspaceAddr, workspaceSize, executor, stream);
  CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclnnDequantSwigluQuantV2 failed. ERROR: %d\n", ret); return ret);

  // 4. (Boilerplate) Synchronously 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 device-side memory to host-side memory. Modify the code according to the specific API definition.
  auto size = GetShapeSize(outShape);
  std::vector<int8_t> 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: %d\n", i, resultData[i]);
  }
  // 6. Release aclTensor and aclScalar, which need to be modified based on the specific API definition.
  aclDestroyTensor(x);
  aclDestroyTensor(scale);
  aclDestroyTensor(offset);
  aclDestroyTensor(out);
  aclDestroyTensor(scaleOut);
  // 7. Release device resources, which need to be modified based on the specific API definition.
  aclrtFree(xDeviceAddr);
  aclrtFree(scaleDeviceAddr);
  aclrtFree(offsetDeviceAddr);
  aclrtFree(outDeviceAddr);
  aclrtFree(scaleOutDeviceAddr);
  if (workspaceSize > 0) {
    aclrtFree(workspaceAddr);
  }
  aclrtDestroyStream(stream);
  aclrtResetDevice(deviceId);
  aclFinalize();
  return 0;
}