aclnnMoeInitRoutingQuantV2

📄 View Source Code

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: This operator corresponds to Routing calculation in MoE (Mixture of Experts). It takes the calculation result of aclnnMoeGatingTopKSoftmax as input, and outputs the quantized routing matrix expandedXOut and other results for subsequent calculation. Compared with aclnnMoeInitRoutingQuant, this API includes the following function changes. Select an appropriate API based on the actual situation:

    • A Drop mode is newly added. In Drop mode, the count of tokens to be processed by each expert in the output is aligned to expertCapacity. Tokens exceeding expertCapacity are dropped, and the insufficient part is padded with 0.
    • Newly added optional output expertTokensCountOrCumsumOut in Dropless mode, which outputs the cumulative token count (Cumsum) or the token count (Count) that each expert needs to process.
    • Newly added optional output expertTokensBeforeCapacityOut in Drop mode, which outputs the count of tokens each expert should process before Drop.
    • Removed the rowIdx input.
    • Added the dynamic quant computation mode.
  • Formula:

    1.Flatten the expertIdx input with shape [NUM_ROWS, K] into one row for sorting, where NUM_ROWS is the count of input tokens and K is the count of experts selected by each token, to derive the sorted result sortedExpertIdx and the corresponding indices sortedRowIdx:

    $$ sortedExpertIdx, sortedRowIdx=keyValueSort(\text{flatten}(expertIdx)) $$

    2.Perform position mapping based on sortedRowIdx to obtain expandedRowIdxOut:

    $$ expandedRowIdxOut[sortedRowIdx[i]]=i $$

    3.In dropless mode, compute the histogram result for each expert in sortedExpertIdx, and then perform Cumsum to derive expertTokensCountOrCumsumOutOptional:

    $$ expertTokensCountOrCumsumOutOptional[i]=Cumsum(Histogram(sortedExpertIdx)) $$

    4.In Drop mode, compute the histogram result for each expert in sortedExpertIdx to derive expertTokensBeforeCapacityOutOptional:

    $$ expertTokensBeforeCapacityOutOptional[i]=Histogram(sortedExpertIdx) $$

    5.Compute the quant result:

    • static quant:

      $$ quantResult = round((x * scaleOptional) + offsetOptional) $$
    • Dynamic quant:

      • If scale is not passed in:

        $$ dynamicQuantScaleOutOptional = row\_max(abs(x)) / 127 $$
        $$ quantResult = round(x / dynamicQuantScaleOutOptional) $$
      • If scale is passed in:

        $$ dynamicQuantScaleOutOptional = row\_max(abs(x * scaleOptional)) / 127 $$
        $$ quantResult = round(x * scaleOptional / dynamicQuantScaleOutOptional) $$

    6.Derive expandedXOut from quantResult:

    $$ expandedXOut[expandedRowIdxOut[i]]=quantResult[i // K] $$

Function Prototype

Each operator uses a two-phase API. Call the "aclnnMoeInitRoutingQuantV2GetWorkspaceSize" API first to obtain the workspace size required for the calculation and the executor that includes calculation flow of the operator. Then call the "aclnnMoeInitRoutingQuantV2" API to perform the calculation.

aclnnStatus aclnnMoeInitRoutingQuantV2GetWorkspaceSize(
    const aclTensor  *x,
    const aclTensor  *expertIdx,
    const aclTensor  *scaleOptional,
    const aclTensor  *offsetOptional,
    int64_t           activeNum,
    int64_t           expertCapacity,
    int64_t           expertNum,
    int64_t           dropPadMode,
    int64_t           expertTokensCountOrCumsumFlag,
    bool              expertTokensBeforeCapacityFlag,
    int64_t           quantMode,
    const aclTensor  *expandedXOut,
    const aclTensor  *expandedRowIdxOut,
    const aclTensor  *expertTokensCountOrCumsumOutOptional,
    const aclTensor  *expertTokensBeforeCapacityOutOptional,
    const aclTensor  *dynamicQuantScaleOutOptional,
    uint64_t         *workspaceSize,
    aclOpExecutor   **executor)
aclnnStatus aclnnMoeInitRoutingQuantV2(
    void             *workspace,
    uint64_t          workspaceSize,
    aclOpExecutor    *executor,
    aclrtStream       stream)

aclnnMoeInitRoutingQuantV2GetWorkspaceSize

  • Parameters:

    Parameter Input/Output Description Instruction Data Type Data Format Dimension (shape) Non-contiguous Tensor
    x Input The input of MOE, i.e., the token feature input.
    • Supports empty tensor.
    • Must be a 2D tensor with shape [NUM_ROWS, H]. NUM_ROWS indicates the number of tokens. H indicates the length of each token.
    FLOAT16, BFLOAT16, FLOAT32 ND 2 √
    expertIdx Input Output of aclnnMoeGatingTopKSoftmaxV2, indicating the K experts for handling each row of features.
    • Supports empty tensors.
    • The requirement is a 2D shape [NUM_ROWS, K].
    • In Drop/Pad mode or in non-Drop/Pad mode where expertTokensCountOrCumsumOutOptional needs to be output, the value range must be [0, expertNum - 1]. In other cases, the value must be greater than or equal to 0.
    INT32 ND 2 √
    scaleOptional Input Parameter used to calculate the quant result.
    • Supports empty tensors.
    • Mandatory input in static quant mode. The requirement is a 1D shape [1].
    • In dynamic quant mode, if not input, this indicates that scale is not used during calculation. If input, the requirement is a 2D tensor with shape [expertNum, H] or [1, H].
    FLOAT32 ND 1 or 2 √
    offsetOptional Input Offset value used to calculate the quant result.
    • Supports empty tensor.
    • Required input in static quant use case. It is a 1D tensor with shape [1].
    FLOAT32 ND 1 √
    activeNum Input Indicates whether the task is an Active task. This parameter takes effect when dropPadMode is 0. The value range is greater than or equal to 0. 0 indicates the Dropless task, and a value greater than 0 indicates the Active task. The value constrains the total number of tokens jointly handled by all experts. - - - -
    expertCapacity Input Indicates the number of tokens each expert can handle. The value range is greater than or equal to 0; in the Drop/Pad use case, the value range is (0, NUM_ROWS], that is, the value must be greater than 0 and must not exceed NUM_ROWS; in other use cases, this attribute value is irrelevant. - - - -
    expertNum Input Indicates the number of experts. The value range is greater than or equal to 0. In Drop/Pad use cases, or when expertTokensCountOrCumsumFlag is greater than 0 and you need to output expertTokensCountOrCumsumOutOptional, expertNum must be greater than 0. - - - -
    dropPadMode Input Indicates whether the use case is Drop/Pad. Takes value 0 or 1.
    • 0: indicates a non-Drop/Pad use case. In this use case, expertCapacity is not checked.
    • 1: indicates a Drop/Pad use case. expertNum and expertCapacity need to be checked. Values that exceed or fall short of expertCapacity during expert handling are processed accordingly.
    - - - -
    expertTokensCountOrCumsumFlag Input Controls whether to output expertTokensCountOrCumsumOutOptional. Takes values 0, 1, and 2.
    • 0: Indicates no output of expertTokensCountOrCumsumOutOptional.
    • 1: Indicates output of the cumulative number of tokens handled by each expert.
    • 2: Indicates output of the number of tokens handled by each expert.
    - - - -
    expertTokensBeforeCapacityFlag Input Controls whether to output expertTokensBeforeCapacityOutOptional. Takes the value false or true.
    • false: indicates that expertTokensBeforeCapacityOutOptional is not output.
    • true: indicates that the output value is the number of tokens handled by each expert before drop.
    - - - -
    quantMode Input Indicates the quant calculation mode. Takes value 0 or 1.
    • 0: indicates the static quant use case.
    • 1: indicates the dynamic quant use case.
    - - - -
    expandedXOut Output Features expanded based on expertIdx.
    • Supports empty tensors.
    • In Dropless/Active scenarios, the requirement is a 2D tensor. The shape is [NUM_ROWS * K, H] in the Dropless scenario and [min(activeNum, NUM_ROWS * K), H] in the Active scenario.
    • In the Drop/Pad scenario, the requirement is a 3D tensor with the shape [expertNum, expertCapacity, H].
    • The data types support INT8 and INT4. If the data type of expandedXOut is INT4, the following restrictions apply: dropPadMode supports only 0; H must be divisible by 2; if scaleOptional is passed in, the shape of scaleOptional supports only [1, H].
    INT8, INT4 ND 2 or 3 ×
    expandedRowIdxOut Output Index mapping relationship between expandedXOut and x.
    • Supports empty tensors.
    • The requirement is a 1D tensor with the shape [NUM_ROWS*K].
    INT32 ND 1 ×
    expertTokensCountOrCumsumOutOptional Output Outputs the statistics and cumulative value of the number of tokens handled by each expert.
    • Supports empty tensors.
    • Whether to output is controlled by the expertTokensCountOrCumsumFlag parameter. This value is only output in the non-Drop/Pad scenario. The requirement is a 1D tensor with shape [expertNum].
    INT32 ND 1 ×
    expertTokensBeforeCapacityOutOptional Output Outputs statistics on the number of tokens processed by each expert before drop is applied.
    • Supports empty tensors.
    • Whether to output is controlled by the expertTokensBeforeCapacityFlag parameter. This value is only output in the Drop/Pad use case. The requirement is a 1D tensor with shape [expertNum].
    INT32 ND 1 ×
    dynamicQuantScaleOutOptional Output Outputs intermediate values in dynamic quant calculation.
    • Supports empty tensors.
    • This value is only output in dynamic quant scenarios. The requirement is a 1D tensor, whose shape is the product of all dimensions in the shape of expandedXOut after removing the last dimension.
    FLOAT32 ND 1 ×
    workspaceSize Output Returns the size of the workspace requested by the user on the Device side. - - - - -
    executor Output Returns the op executor. It includes calculation flow of the operator. - - - - -
    • Ascend 950PR&950DT products: The output expandedXOut data type only supports INT8.
  • Return value

    aclnnStatus: Returns a status code. For details, see aclnn return code.

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

    Return Value Error Code Description
    ACLNN_ERR_PARAM_NULLPTR 161001 A calculation input or a required calculation output is a null pointer.
    ACLNN_ERR_PARAM_INVALID 161002 The data type and format of calculation inputs and outputs are not within the supported range.
    ACLNN_ERR_INNER_TILING_ERROR 561002 The shape dimension of x and expertIdx is not equal to 2, and the first dimensions are not equal. When the data type of expandedXOut is INT4, dropPadMode is not 0, or the shape of input scaleOptional is not [1, H].
    The value of activeNum, expertNum, or expertCapacity is less than 0.
    dropPadMode, expertTokensCountOrCumsumFlag, expertTokensBeforeCapacityFlag, or quantMode is not within the value range.
    When dropPadMode is equal to 1, expertCapacity and expertNum are equal to 0.
    expertTokensCountOrCumsumOutOptional needs to be output, but expertNum equals 0.
    The data type of an optional input or output is not within the supported range.

aclnnMoeInitRoutingQuantV2

  • Parameters:

    Parameter Input/Output Description
    workspace Input Memory address of the workspace requested on the Device side.
    workspaceSize Input Size of the workspace requested on the Device side. Obtained by the first-phase API aclnnMoeInitRoutingQuantV2GetWorkspaceSize.
    executor Input Op executor, includes calculation flow of the operator.
    stream Input Specifies the stream for task execution.
  • Return value

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

Constraints

  • Deterministic computation:
    • aclnnMoeInitRoutingQuantV2 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 "acl/acl.h"
#include "aclnnop/aclnn_moe_init_routing_quant_v2.h"
#include <iostream>
#include <vector>
#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: 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 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);
    // 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 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 for device/stream initialization. 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 the check as needed.
    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> xShape = {3, 4};
    std::vector<int64_t> idxShape = {3, 2};
    std::vector<int64_t> scaleShape = {1};
    std::vector<int64_t> expandedXOutShape = {3, 2, 4};
    std::vector<int64_t> idxOutShape = {6};
    std::vector<int64_t> expertTokenOutShape = {3};
    std::vector<int64_t> dynamicQuantScaleOutOptionalShape = {6};
    void* xDeviceAddr = nullptr;
    void* expertIdxDeviceAddr = nullptr;
    void* scaleDeviceAddr = nullptr;
    void* offsetDeviceAddr = nullptr;
    void* expandedXOutDeviceAddr = nullptr;
    void* expandedRowIdxOutDeviceAddr = nullptr;
    void* expertTokenBeforeCapacityOutDeviceAddr = nullptr;
    void* dynamicQuantScaleOutOptionalDeviceAddr = nullptr;
    aclTensor* x = nullptr;
    aclTensor* expertIdx = nullptr;
    aclTensor* scale = nullptr;
    aclTensor* offset = nullptr;
    int64_t activeNum = 0;
    int64_t expertCapacity = 2;
    int64_t expertNum = 3;
    int64_t dropPadMode = 1;
    int64_t expertTokensCountOrCumsumFlag = 0;
    bool expertTokensBeforeCapacityFlag = true;
    int64_t quantMode = 0;
    aclTensor* expandedXOut = nullptr;
    aclTensor* expandedRowIdxOut = nullptr;
    aclTensor* expertTokensBeforeCapacityOutOptional = nullptr;
    aclTensor* dynamicQuantScaleOutOptional = nullptr;
    std::vector<float> xHostData = {0.1, 0.1, 0.1, 0.1, 0.2, 0.2, 0.2, 0.2, 0.3, 0.3, 0.3, 0.3};
    std::vector<int> expertIdxHostData = {1, 2, 0, 1, 0, 2};
    std::vector<float> scaleHostData = {0.3452};
    std::vector<float> offsetHostData = {1.8369};
    std::vector<int8_t> expandedXOutHostData = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
    std::vector<int> expandedRowIdxOutHostData = {0, 0, 0, 0, 0, 0};
    std::vector<int> expertTokensBeforeCapacityOutOptionalHostData = {0, 0, 0};
    std::vector<float> dynamicQuantScaleOutOptionalHostData = {0, 0, 0, 0, 0, 0};
    // Create the self aclTensor.
    ret = CreateAclTensor(xHostData, xShape, &xDeviceAddr, aclDataType::ACL_FLOAT, &x);
    CHECK_RET(ret == ACL_SUCCESS, return ret);
    ret = CreateAclTensor(expertIdxHostData, idxShape, &expertIdxDeviceAddr, aclDataType::ACL_INT32, &expertIdx);
    CHECK_RET(ret == ACL_SUCCESS, return ret);
    ret = CreateAclTensor(scaleHostData, scaleShape, &scaleDeviceAddr, aclDataType::ACL_FLOAT, &scale);
    CHECK_RET(ret == ACL_SUCCESS, return ret);
    ret = CreateAclTensor(offsetHostData, scaleShape, &offsetDeviceAddr, aclDataType::ACL_FLOAT, &offset);
    CHECK_RET(ret == ACL_SUCCESS, return ret);
    // Create the out aclTensor.
    ret = CreateAclTensor(expandedXOutHostData, expandedXOutShape, &expandedXOutDeviceAddr, aclDataType::ACL_INT8, &expandedXOut);
    CHECK_RET(ret == ACL_SUCCESS, return ret);
    ret = CreateAclTensor(expandedRowIdxOutHostData, idxOutShape, &expandedRowIdxOutDeviceAddr, aclDataType::ACL_INT32, &expandedRowIdxOut);
    CHECK_RET(ret == ACL_SUCCESS, return ret);
    ret = CreateAclTensor(expertTokensBeforeCapacityOutOptionalHostData, expertTokenOutShape, &expertTokenBeforeCapacityOutDeviceAddr, aclDataType::ACL_INT32, &expertTokensBeforeCapacityOutOptional);
    CHECK_RET(ret == ACL_SUCCESS, return ret);
    ret = CreateAclTensor(dynamicQuantScaleOutOptionalHostData, dynamicQuantScaleOutOptionalShape, &dynamicQuantScaleOutOptionalDeviceAddr, aclDataType::ACL_FLOAT, &dynamicQuantScaleOutOptional);
    CHECK_RET(ret == ACL_SUCCESS, return ret);
    // 3. Call the CANN operator library API. Replace it with the specific API.
    uint64_t workspaceSize = 0;
    aclOpExecutor* executor;
    // Call the first-phase API of aclnnMoeInitRoutingQuantV2.
    ret = aclnnMoeInitRoutingQuantV2GetWorkspaceSize(x, expertIdx, scale, offset, activeNum, expertCapacity, expertNum, dropPadMode, expertTokensCountOrCumsumFlag, expertTokensBeforeCapacityFlag, quantMode, expandedXOut, expandedRowIdxOut, nullptr, expertTokensBeforeCapacityOutOptional, dynamicQuantScaleOutOptional, &workspaceSize, &executor);
    CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclnnMoeInitRoutingQuantV2GetWorkspaceSize 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 aclnnMoeInitRoutingQuantV2.
    ret = aclnnMoeInitRoutingQuantV2(workspaceAddr, workspaceSize, executor, stream);
    CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclnnMoeInitRoutingQuantV2 failed. ERROR: %d\n", ret); return ret);
    // 4. Boilerplate. Synchronize and 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 values and copy the results from device memory to the host. Modify them based on the specific API definition.
    auto expandedXSize = GetShapeSize(expandedXOutShape);
    std::vector<int8_t> expandedXData(expandedXSize, 0);
    ret = aclrtMemcpy(expandedXData.data(), expandedXData.size() * sizeof(expandedXData[0]), expandedXOutDeviceAddr, expandedXSize * 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 < expandedXSize; i++) {
        LOG_PRINT("expandedXData[%ld] is: %d\n", i, expandedXData[i]);
    }
    auto expandedRowIdxSize = GetShapeSize(idxOutShape);
    std::vector<int> expandedRowIdxData(expandedRowIdxSize, 0);
    ret = aclrtMemcpy(expandedRowIdxData.data(), expandedRowIdxData.size() * sizeof(expandedRowIdxData[0]), expandedRowIdxOutDeviceAddr, expandedRowIdxSize * sizeof(int32_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 < expandedRowIdxSize; i++) {
        LOG_PRINT("expandedRowIdxData[%ld] is: %d\n", i, expandedRowIdxData[i]);
    }
    auto expertTokensBeforeCapacitySize = GetShapeSize(expertTokenOutShape);
    std::vector<int> expertTokenIdxData(expertTokensBeforeCapacitySize, 0);
    ret = aclrtMemcpy(expertTokenIdxData.data(), expertTokenIdxData.size() * sizeof(expertTokenIdxData[0]), expertTokenBeforeCapacityOutDeviceAddr, expertTokensBeforeCapacitySize * sizeof(int32_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 < expertTokensBeforeCapacitySize; i++) {
        LOG_PRINT("expertTokenIdxData[%ld] is: %d\n", i, expertTokenIdxData[i]);
    }

    auto dynamicQuantScaleSize = GetShapeSize(dynamicQuantScaleOutOptionalShape);
    std::vector<float> dynamicQuantScaleData(dynamicQuantScaleSize, 0);
    ret = aclrtMemcpy(dynamicQuantScaleData.data(), dynamicQuantScaleData.size() * sizeof(dynamicQuantScaleData[0]), dynamicQuantScaleOutOptionalDeviceAddr, dynamicQuantScaleSize * 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 < dynamicQuantScaleSize; i++) {
        LOG_PRINT("dynamicQuantScaleData[%ld] is: %f\n", i, dynamicQuantScaleData[i]);
    }
    // 6. Release aclTensor and aclScalar. Modify them based on the specific API definition.
    aclDestroyTensor(x);
    aclDestroyTensor(expertIdx);
    aclDestroyTensor(scale);
    aclDestroyTensor(offset);
    aclDestroyTensor(expandedXOut);
    aclDestroyTensor(expandedRowIdxOut);
    aclDestroyTensor(expertTokensBeforeCapacityOutOptional);
    aclDestroyTensor(dynamicQuantScaleOutOptional);

    // 7. Release device resources. Modify them based on the specific API definition.
    aclrtFree(xDeviceAddr);
    aclrtFree(expertIdxDeviceAddr);
    aclrtFree(scaleDeviceAddr);
    aclrtFree(offsetDeviceAddr);
    aclrtFree(expandedXOutDeviceAddr);
    aclrtFree(expandedRowIdxOutDeviceAddr);
    aclrtFree(expertTokenBeforeCapacityOutDeviceAddr);
    aclrtFree(dynamicQuantScaleOutOptionalDeviceAddr);
    if (workspaceSize > 0) {
      aclrtFree(workspaceAddr);
    }
    aclrtDestroyStream(stream);
    aclrtResetDevice(deviceId);
    aclFinalize();
    return 0;
}