aclnnDynamicMxQuantWithDualAxisV2

📄 View Source Code

Applicable Products

  • Ascend 950PR&950DT products: Supported
  • Atlas A3 products: Not supported
  • Atlas A2 products: Not supported
  • Atlas 200I/500 A2 inference products: Not supported
  • Atlas inference products: Not supported
  • Atlas training products: Not supported

Function

  • Description: Simultaneously performs MX quantization to the FLOAT4 and FLOAT8 destination data types on the -1 axis and -2 axis. On the given -1 axis and -2 axis, for every 32 values, computes the quantization scaling factors mxscale1 and mxscale2 of the two groups of values as the corresponding parts of outputs mxscale1Out and mxscale2Out. Then divides all elements of the two groups of values by the corresponding mxscale1 or mxscale2, converts them to the corresponding dstType according to round_mode, and obtains the quantization results y1 and y2 as the corresponding parts of outputs y1Out and y2Out, respectively. When dstType is FLOAT8_E4M3FN or FLOAT8_E5M2, the value of scaleAlg specifies the algorithm for computing mxscale.

  • Axis merging: In the operator implementation, all axes before the -2 axis (exclusive) are merged. That is, for a tensor with the input shape (d0,d1,...,dn−3,dn−2,dn−1)(d_0, d_1, ..., d_{n-3}, d_{n-2}, d_{n-1}), the dimensions (d0,d1,...,dn−3)(d_0, d_1, ..., d_{n-3}) before the -2 axis are merged into one dimension. This is equivalent to reshaping the input to (d0×d1×...×dn−3,dn−2,dn−1)(d_0 \times d_1 \times ... \times d_{n-3}, d_{n-2}, d_{n-1}) before performing the quantization.

  • Formula:

    • Use case 1: When scaleAlg is 0, that is, the OCP Microscaling Formats (Mx) Specification implementation:
      • Group the input x along the -1 axis by 32 elements, where a group of 32 elements {{Vi}i=132}\{\{V_i\}_{i=1}^{32}\} is quantized into {mxscale1,{Pi}i=132}\{mxscale1, \{P_i\}_{i=1}^{32}\}

        shared_exp=floor(log2(maxi(∣Vi∣)))−emaxshared\_exp = floor(log_2(max_i(|V_i|))) - emax mxscale1=2shared_expmxscale1 = 2^{shared\_exp} Pi=cast_to_dst_type(Vi/mxscale1,round_mode), i from 1 to 32P_i = cast\_to\_dst\_type(V_i/mxscale1, round\_mode), \space i\space from\space 1\space to\space 32
      • Simultaneously, group the input x along the -2 axis by 32 elements, where a group of 32 elements {{Vj}j=132}\{\{V_j\}_{j=1}^{32}\} is quantized into {mxscale2,{Pj}j=132}\{mxscale2, \{P_j\}_{j=1}^{32}\}

        shared_exp=floor(log2(maxj(∣Vj∣)))−emaxshared\_exp = floor(log_2(max_j(|V_j|))) - emax mxscale2=2shared_expmxscale2 = 2^{shared\_exp} Pj=cast_to_dst_type(Vj/mxscale2,round_mode), j from 1 to 32P_j = cast\_to\_dst\_type(V_j/mxscale2, round\_mode), \space j\space from\space 1\space to\space 32
      • The quantized PiP_{i} on the -1 axis is composed into output y1Out according to the corresponding positions of ViV_{i}, and mxscale1 is composed into output mxscale1Out according to the grouping on the -1 axis dimension. The quantized PjP_{j} on the -2 axis is composed into output y2Out according to the corresponding positions of VjV_{j}, and mxscale2 is composed into output mxscale2Out according to the grouping on the -2 axis dimension.

      • emax: The exponent of the maximum normal number of the corresponding data type.

| DataType | Emax | | :-----------: | :--: | | FLOAT4_E2M1 | 2 | | FLOAT4_E1M2 | 0 | | FLOAT8_E4M3FN | 8 | | FLOAT8_E5M2 | 15 |

  • Use case 2: When scaleAlg is 1, only FP8 types are involved (round-up algorithm):

    • -1 axis quantization: Input x is grouped by count 32 along the -1 axis, and each group has a length of 32. A block scaling factor Sfp32bS_{fp32}^b is calculated for each group separately, and all elements in the group are then mapped to the target low-precision type FP8 using the same Sfp32bS_{fp32}^b. If the last group has fewer than 32 elements, the missing values are treated as 0 and the group is processed as a complete group.
      • Find the maximum absolute value of the values in the group:Amax(Dfp32b)=max({∣di∣}i=132)Amax(D_{fp32}^b)=max(\{|d_{i}|\}_{i=1}^{32})
      • Map FP32 to the representable range of the target data type FP8, where Amax(DType)Amax(DType) is the maximum value that the target precision can represent.Sfp32b=Amax(Dfp32b)Amax(DType)S_{fp32}^b = \frac{Amax(D_{fp32}^b)}{Amax(DType)}
      • Convert the block scale factor Sfp32bS_{fp32}^b to the representable scale value Sue8m0bS_{ue8m0}^b in FP8 format.
      • Extract the unbiased exponent EintbE_{int}^b and mantissa MfixpbM_{fixp}^b from the block floating-point scale factor Sfp32bS_{fp32}^b.
      • To ensure no overflow during quantization, round up the exponent and keep it within the representable range of FP8:Eintb={Eintb+1, ifSfp32b is a normal number, andEintb<254 andMfixpb>0Eintb+1, ifSfp32b is a subnormal number, andMfixpb>0.5Eintb, otherwiseE_{int}^b = \begin{cases} E_{int}^b + 1, & \text{ if} S_{fp32}^b \text{ is a normal number, and} E_{int}^b < 254 \text{ and} M_{fixp}^b > 0 \\ E_{int}^b + 1, & \text{ if} S_{fp32}^b \text{ is a subnormal number, and} M_{fixp}^b > 0.5 \\ E_{int}^b, & \text{ otherwise} \end{cases}
      • Calculate the block scale factor: Sue8m0b=2EintbS_{ue8m0}^b=2^{E_{int}^b}
      • Calculate the block conversion factor: Rfp32b=1fp32(Sue8m0b)R_{fp32}^b=\frac{1}{fp32(S_{ue8m0}^b)}
      • Apply the final step of quantization. For each element in a group, di=DType(dfp32i⋅Rfp32b)d^i = DType(d_{fp32}^i \cdot R_{fp32}^b). The final quantization result of the -1 axis output is (Sb,[di]i=132)\left(S^b, [d^i]_{i=1}^{32}\right), where SbS^b represents the block scaling factor, that is, Sue8m0bS_{ue8m0}^b, and [di]i=132[d^i]_{i=1}^{32} represents the quantized data in the group.
    • -2 axis quantization: Simultaneously, group the input x along the -2 axis by 32, adopt the same round-up algorithm as the -1 axis, and independently compute the block scaling factor and quantize each group. The quantization result of the -2 axis output is (Sb,[dj]j=132)\left(S^b, [d^j]_{j=1}^{32}\right).
    • The -1 axis quantization results compose output y1Out, and the corresponding block scaling factors compose output mxscale1Out. The -2 axis quantization results compose output y2Out, and the corresponding block scaling factors compose output mxscale2Out.
  • Use case 3: when scaleAlg is 2, only the FP4_E2M1 type is involved:

    • When dstTypeMax = 0.0/6.0/7.0:
      • -1 axis quantization: Group the input x along the -1 axis by 32. A group of 32 numbers {{Vi}i=132}\{\{V_i\}_{i=1}^{32}\} is quantized to {mxscale1,{Pi}i=132}\{mxscale1, \{P_i\}_{i=1}^{32}\}:shared_exp={ceil(log2(maxi(∣Vi∣)))−emax, iftheone/twobitsbeforethehighbitofthemantissa are 1, and the mantissa is not all 0floor(log2(maxi(∣Vi∣)))−emax, otherwiseshared\_exp = \begin{cases} ceil(log_2(max_i(|V_i|))) - emax, & \text{ if} the one/two bits before the high bit of the mantissa \text{ are 1, and the mantissa is not all 0} \\ floor(log_2(max_i(|V_i|))) - emax, & \text{ otherwise} \end{cases} \\ mxscale1=2shared_expmxscale1 = 2^{shared\_exp} Pi=cast_to_dst_type(Vi/mxscale1,round_mode), i from 1 to 32P_i = cast\_to\_dst\_type(V_i/mxscale1, round\_mode), \space i\space from\space 1\space to\space 32\\
      • -2-axis quantization: Simultaneously, input x is grouped along the -2 axis by count of 32. A group of 32 numbers {{Vj}j=132}\{\{V_j\}_{j=1}^{32}\} is quantized to {mxscale2,{Pj}j=132}\{mxscale2, \{P_j\}_{j=1}^{32}\} using the same carry computation method as the -1 axis.
      • ​The quantized PiP_{i} on the -1 axis compose the output y1Out according to the positions of the corresponding ViV_{i}, and mxscale1 composes the output mxscale1Out according to the grouping on the -1 axis dimension. The quantized PjP_{j} on the -2 axis compose the output y2Out according to the positions of the corresponding VjV_{j}, and mxscale2 composes the output mxscale2Out according to the grouping on the -2 axis dimension.
    • When dstTypeMax != 0.0/6.0/7.0:
      • -1-axis quantization: Input x is grouped along the -1 axis by count of 32, and each group has a length of 32. A block scaling factor Sfp32bS_{fp32}^b is computed separately for each group, and then all elements in the group are mapped to the target low-precision type FP4_E2M1 with the same Sfp32bS_{fp32}^b. If the last group has fewer than 32 elements, the missing values are regarded as 0, and the group is processed as a complete group.
        • Find the maximum absolute value of the values in the group:Amax(Dfp32b)=max({∣di∣}i=132)Amax(D_{fp32}^b)=max(\{|d_{i}|\}_{i=1}^{32})
        • Map FP32 to within the range representable by the target data type, where Amax(DType)Amax(DType) is the maximum value representable by the target precision when dstTypeMax=0; Amax(DType)Amax(DType) is the value passed in for dstTypeMax when dstTypeMax!=0.Sfp32b=Amax(Dfp32b)Amax(DType)S_{fp32}^b = \frac{Amax(D_{fp32}^b)}{Amax(DType)}
        • Convert the block quantization scale factor Sfp32bS_{fp32}^b to a scale value Sue8m0bS_{ue8m0}^b representable in the FP8 format.
        • Extract the unbiased exponent EintbE_{int}^b and the mantissa MfixpbM_{fixp}^b from the floating-point quantization scale factor Sfp32bS_{fp32}^b of the block.
        • To ensure no overflow during quantization, round up the exponent and keep it within the representable range of FP8:Eintb={Eintb+1, ifSfp32b is a normal number, andEintb<254 andMfixpb>0Eintb, otherwiseE_{int}^b = \begin{cases} E_{int}^b + 1, & \text{ if} S_{fp32}^b \text{ is a normal number, and} E_{int}^b < 254 \text{ and} M_{fixp}^b > 0 \\ E_{int}^b, & \text{ otherwise} \end{cases}
        • Calculate the block scale factor: Sue8m0b=2EintbS_{ue8m0}^b=2^{E_{int}^b}
        • Calculate the block conversion factor: Rfp32b=1fp32(Sue8m0b)R_{fp32}^b=\frac{1}{fp32(S_{ue8m0}^b)}
        • Apply the final step of quantization. For each element in a group, di=DType(dfp32i⋅Rfp32b)d^i = DType(d_{fp32}^i \cdot R_{fp32}^b). The final quantization result of the -1 axis output is (Sb,[di]i=132)\left(S^b, [d^i]_{i=1}^{32}\right), where SbS^b represents the block scaling factor, that is, Sue8m0bS_{ue8m0}^b, and [di]i=132[d^i]_{i=1}^{32} represents the quantized data in the group.
      • -2 axis quantization: Simultaneously, group the input x along the -2 axis by a count of 32, adopt the same algorithm as the -1 axis, and independently compute the block scaling factor and perform quantization for each group. The quantization result of the -2 axis output is (Sb,[dj]j=132)\left(S^b, [d^j]_{j=1}^{32}\right).
      • The -1 axis quantization results compose output y1Out, and the corresponding block scaling factors compose output mxscale1Out. The -2 axis quantization results compose output y2Out, and the corresponding block scaling factors compose output mxscale2Out.

Function Prototype

Each operator uses a two-phase API. Call the "aclnnDynamicMxQuantWithDualAxisV2GetWorkspaceSize" API first to obtain the workspace size required for computation and the executor that contains the operator computation process. Then call the "aclnnDynamicMxQuantWithDualAxisV2" API to perform computation.

aclnnStatus aclnnDynamicMxQuantWithDualAxisV2GetWorkspaceSize(
  const aclTensor *x,
  char            *roundModeOptional,
  int64_t          dstType,
  int64_t          scaleAlg,
  double           dstTypeMax,
  const aclTensor *y1Out,
  const aclTensor *mxscale1Out,
  const aclTensor *y2Out,
  const aclTensor *mxscale2Out,
  uint64_t        *workspaceSize,
  aclOpExecutor   **executor)
aclnnStatus aclnnDynamicMxQuantWithDualAxisV2(
  void          *workspace,
  uint64_t       workspaceSize,
  aclOpExecutor *executor,
  aclrtStream    stream)

aclnnDynamicMxQuantWithDualAxisV2GetWorkspaceSize

  • Parameters:

    Parameter Input/Output Description Instruction Data Type Data Format Dimension (shape) Non-contiguous Tensor
    x (aclTensor*) Input Represents input x, corresponding to Vi and di in the formula. When the target type is FLOAT4_E2M1 or FLOAT4_E1M2, the last dimension of x must be even. FLOAT16, BFLOAT16 ND 2-7 √
    roundModeOptional (char*) Input Represents the data conversion mode, corresponding to round_mode in the formula.
    • When dstType is 40/41 and the data types of outputs y1Out and y2Out are FLOAT4_E2M1/FLOAT4_E1M2, {"rint", "floor", "round"} are supported.
    • When dstType is 35/36 and the data types of outputs y1Out and y2Out are FLOAT8_E5M2/FLOAT8_E4M3FN, only {"rint"} is supported.
    • If a null pointer is passed in, the "rint" mode is used.
    STRING - - -
    dstType (int64_t) Input Represents the types of y1Out and y2Out after the specified data conversion, corresponding to DType in the formula. The input range is {35, 36, 40, 41}, corresponding to the data types of outputs y1Out and y2Out: {35:FLOAT8_E5M2, 36:FLOAT8_E4M3FN, 40:FLOAT4_E2M1, 41:FLOAT4_E1M2}. INT64 - - -
    scaleAlg (int64_t) Input Represents the calculation method of mxscale1Out and mxscale2Out, corresponding to scaleAlg in the formula.
    • Supports the values 0, 1, and 2. The value 0 represents scenario 1, 1 represents scenario 2, and 2 represents scenario 3.
    • Only the value 0 is supported when dstType is FLOAT4_E1M2.
    • Only the values 0 and 2 are supported when dstType is FLOAT4_E2M1.
    • Only the values 0 and 1 are supported when dstType is FLOAT8.
    INT64 - - -
    dstTypeMax (double) Input Represents the value of maxType, corresponding to Amax(DType) in the formula.
    • Supported values are 0.0 and 6.0-12.0. A value of 0.0 indicates that Amax(DType) is the maximum value of the quantization result data type; a value of 6.0-12.0 indicates that Amax(DType) is the passed-in value.
    • This value can be set only when the data type is FP4_E2M1 and scaleAlg is 2.
    DOUBLE - - -
    y1Out (aclTensor*) Output Represents the result of input x after quantization on the -1 axis, corresponding to Pi and di in the formula. The shape is the same as input x. FLOAT4_E2M1, FLOAT4_E1M2, FLOAT8_E4M3FN, FLOAT8_E5M2 ND 2-7 ×
    mxscale1Out (aclTensor*) Output Represents the quantization scale corresponding to each group on the -1 axis, corresponding to mxscale1 and Sb in the formula.
    • The shape is the value of the -1 axis of x divided by 32 and rounded up, and is evenly padded with a padding value of 0.
    FLOAT8_E8M0 ND 2-8 ×
    y2Out (aclTensor*) Output Represents the corresponding result after quantizing input x on the -2 axis, corresponding to Pj and dj in the formula. The shape is the same as that of input x. FLOAT4_E2M1, FLOAT4_E1M2, FLOAT8_E4M3FN, FLOAT8_E5M2 ND 2-7 ×
    mxscale2Out (aclTensor*) Output Represents the quantization scale corresponding to each grouping on the -2 axis, corresponding to mxscale2 and Sb in the formula.
    • The shape is the value of the -2 axis of x divided by 32 and rounded up, and is padded to an even number, with a pad value of 0.
    • The mxscale2Out output requires interleaving for every two rows of data.
    FLOAT8_E8M0 ND 2-8 ×
    workspaceSize (uint64_t*) Output Returns the size of the workspace to be applied on the Device side. - - - - -
    executor (aclOpExecutor**) Output Returns the op executor, which contains the operator computation flow. - - - - -
  • Return value

    aclnnStatus: return status code. For details, see aclnn Return 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 x is a null pointer.
    ACLNN_ERR_PARAM_INVALID 161002 The data types and data formats of x, roundModeOptional, dstType, scaleAlg, dstTypeMax, y1Out, mxscale1Out, y2Out, and mxscale2Out are not within the supported range.
    The shape of x, y1Out, y2Out, mxscale1Out, or mxscale2Out does not meet the validation conditions.
    roundModeOptional, dstType, scaleAlg, or dstTypeMax does not match the currently supported values.
    ACLNN_ERR_RUNTIME_ERROR 361001 The current platform is not within the range of supported platforms.

aclnnDynamicMxQuantWithDualAxisV2

  • Parameters:

    Parameter Input/Output Description
    workspace Input Workspace memory address applied on the Device side.
    workspaceSize Input Size of the workspace applied on the Device side, obtained by the first-phase API aclnnDynamicMxQuantWithDualAxisV2GetWorkspaceSize.
    executor Input Executor of the op. It contains the algorithm sub-computation flow.
    stream Input Specifies the stream for task execution.
  • Return value

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

Constraints

  • The shape constraints for x, mxscale1Out, and mxscale2Out are as follows:
    • rank(mxscale1Out) = rank(x) + 1.
    • rank(mxscale2Out) = rank(x) + 1.
    • mxscale1Out.shape[-2] = (ceil(x.shape[-1] / 32) + 2 - 1) / 2.
    • mxscale2Out.shape[-3] = (ceil(x.shape[-2] / 32) + 2 - 1) / 2.
    • mxscale1Out.shape[-1] = 2.
    • mxscale2Out.shape[-1] = 2.
    • Other dimensions are consistent with input x.
    • Example: When the shape of input x is [B, M, N] and the target data type is FP8 type, the corresponding shapes of y1 and y2 are [B, M, N], the shape of mxscale1 is [B, M, (ceil(N/32)+2-1)/2, 2], and the shape of mxscale2 is [B, (ceil(M/32)+2-1)/2, N, 2].
  • aclnnDynamicMxQuantWithDualAxisV2 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 <memory>
#include <vector>
#include "acl/acl.h"
#include "aclnnop/aclnn_dynamic_mx_quant_with_dual_axis_v2.h"

#define CHECK_RET(cond, return_expr) \
    do {                             \
        if (!(cond)) {               \
            return_expr;             \
        }                            \
    } while (0)

#define CHECK_FREE_RET(cond, return_expr) \
    do {                                  \
        if (!(cond)) {                    \
            Finalize(deviceId, stream);   \
            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 for 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 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);

    // 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;
}

void Finalize(int32_t deviceId, aclrtStream stream)
{
    aclrtDestroyStream(stream);
    aclrtResetDevice(deviceId);
    aclFinalize();
}

int aclnnDynamicMxQuantWithDualAxisV2Test(int32_t deviceId, 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 the input and output. Customize the construction according to the API.
    std::vector<int64_t> xShape = {1, 4};
    std::vector<int64_t> y1OutShape = {1, 4};
    std::vector<int64_t> y2OutShape = {1, 4};
    std::vector<int64_t> mxscale1OutShape = {1, 1, 2};
    std::vector<int64_t> mxscale2OutShape = {1, 4, 2};
    void* xDeviceAddr = nullptr;
    void* y1OutDeviceAddr = nullptr;
    void* mxscale1OutDeviceAddr = nullptr;
    void* y2OutDeviceAddr = nullptr;
    void* mxscale2OutDeviceAddr = nullptr;
    aclTensor* x = nullptr;
    aclTensor* y1Out = nullptr;
    aclTensor* mxscale1Out = nullptr;
    aclTensor* y2Out = nullptr;
    aclTensor* mxscale2Out = nullptr;
    std::vector<uint16_t> xHostData = {0, 16640, 17024, 17408};
    std::vector<uint8_t> y1OutHostData = {0, 72, 96, 120};
    std::vector<uint8_t> y2OutHostData = {0, 120, 120, 120};
    std::vector<uint8_t> mxscale1OutHostData = {128, 0};
    std::vector<uint8_t> mxscale2OutHostData = {0, 0, 122, 0, 125, 0, 128, 0};
    char* roundModeOptional = const_cast<char*>("rint");
    int64_t dstType = 36;
    int64_t scaleAlg = 0;
    double dstTypeMax = 0.0;
    // Create the x aclTensor.
    ret = CreateAclTensor(xHostData, xShape, &xDeviceAddr, aclDataType::ACL_BF16, &x);
    std::unique_ptr<aclTensor, aclnnStatus (*)(const aclTensor*)> xTensorPtr(x, aclDestroyTensor);
    std::unique_ptr<void, aclError (*)(void*)> xDeviceAddrPtr(xDeviceAddr, aclrtFree);
    CHECK_RET(ret == ACL_SUCCESS, return ret);
    // Create the y1Out aclTensor.
    ret = CreateAclTensor(y1OutHostData, y1OutShape, &y1OutDeviceAddr, aclDataType::ACL_FLOAT8_E4M3FN, &y1Out);
    std::unique_ptr<aclTensor, aclnnStatus (*)(const aclTensor*)> y1OutTensorPtr(y1Out, aclDestroyTensor);
    std::unique_ptr<void, aclError (*)(void*)> y1OutDeviceAddrPtr(y1OutDeviceAddr, aclrtFree);
    CHECK_RET(ret == ACL_SUCCESS, return ret);
    // Create the mxscale1Out aclTensor.
    ret = CreateAclTensor(mxscale1OutHostData, mxscale1OutShape, &mxscale1OutDeviceAddr, aclDataType::ACL_FLOAT8_E8M0, &mxscale1Out);
    std::unique_ptr<aclTensor, aclnnStatus (*)(const aclTensor*)> mxscale1OutTensorPtr(mxscale1Out, aclDestroyTensor);
    std::unique_ptr<void, aclError (*)(void*)> mxscale1OutDeviceAddrPtr(mxscale1OutDeviceAddr, aclrtFree);
    CHECK_RET(ret == ACL_SUCCESS, return ret);
    // Create the y2Out aclTensor.
    ret = CreateAclTensor(y2OutHostData, y2OutShape, &y2OutDeviceAddr, aclDataType::ACL_FLOAT8_E4M3FN, &y2Out);
    std::unique_ptr<aclTensor, aclnnStatus (*)(const aclTensor*)> y2OutTensorPtr(y2Out, aclDestroyTensor);
    std::unique_ptr<void, aclError (*)(void*)> y2OutDeviceAddrPtr(y2OutDeviceAddr, aclrtFree);
    CHECK_RET(ret == ACL_SUCCESS, return ret);
    // Create the mxscale2Out aclTensor.
    ret = CreateAclTensor(mxscale2OutHostData, mxscale2OutShape, &mxscale2OutDeviceAddr, aclDataType::ACL_FLOAT8_E8M0, &mxscale2Out);
    std::unique_ptr<aclTensor, aclnnStatus (*)(const aclTensor*)> mxscale2OutTensorPtr(mxscale2Out, aclDestroyTensor);
    std::unique_ptr<void, aclError (*)(void*)> mxscale2OutDeviceAddrPtr(mxscale2OutDeviceAddr, aclrtFree);
    CHECK_RET(ret == ACL_SUCCESS, return ret);

    // Call the CANN operator library API. Change it to the specific API name.
    uint64_t workspaceSize = 0;
    aclOpExecutor* executor;

    // Call the first-phase API of aclnnDynamicMxQuantWithDualAxisV2.
    ret = aclnnDynamicMxQuantWithDualAxisV2GetWorkspaceSize(x, roundModeOptional, dstType, scaleAlg, dstTypeMax, y1Out, mxscale1Out, y2Out, mxscale2Out, &workspaceSize, &executor);
    CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclnnDynamicMxQuantWithDualAxisV2GetWorkspaceSize failed. ERROR: %d\n", ret);
              return ret);
    // Allocate device memory according to the workspaceSize calculated by the first-phase API.
    void* workspaceAddr = nullptr;
    std::unique_ptr<void, aclError (*)(void*)> workspaceAddrPtr(nullptr, aclrtFree);
    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);
        workspaceAddrPtr.reset(workspaceAddr);
    }
    // Call the second-phase API of aclnnDynamicMxQuantWithDualAxisV2.
    ret = aclnnDynamicMxQuantWithDualAxisV2(workspaceAddr, workspaceSize, executor, stream);
    CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclnnDynamicMxQuantWithDualAxisV2 failed. ERROR: %d\n", ret); return ret);

    // (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);

    // Obtain the output values and copy the results from device memory to host memory. Modify based on the definition of the specific API.
    auto size1 = GetShapeSize(y1OutShape);
    auto size2 = GetShapeSize(y2OutShape);
    std::vector<uint8_t> y1OutData(
        size1, 0);  // fp8 data cannot be printed directly in C. Read it as uint8 and manually convert the binary data to fp8.
    std::vector<uint8_t> y2OutData(
        size2, 0);  // fp8 data cannot be printed directly in C. Read it as uint8 and manually convert the binary data to fp8.
    ret = aclrtMemcpy(y1OutData.data(), y1OutData.size() * sizeof(y1OutData[0]), y1OutDeviceAddr,
                      size1 * sizeof(y1OutData[0]), ACL_MEMCPY_DEVICE_TO_HOST);
    CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("copy y1Out from device to host failed. ERROR: %d\n", ret);
              return ret);
    ret = aclrtMemcpy(y2OutData.data(), y2OutData.size() * sizeof(y2OutData[0]), y2OutDeviceAddr,
                      size2 * sizeof(y2OutData[0]), ACL_MEMCPY_DEVICE_TO_HOST);
    CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("copy y2Out from device to host failed. ERROR: %d\n", ret);
              return ret);
    for (int64_t i = 0; i < size1; i++) {
        LOG_PRINT("y1Out[%ld] is: %d\n", i, y1OutData[i]);
    }
    for (int64_t i = 0; i < size2; i++) {
        LOG_PRINT("y2Out[%ld] is: %d\n", i, y2OutData[i]);
    }
    size1 = GetShapeSize(mxscale1OutShape);
    size2 = GetShapeSize(mxscale2OutShape);
    std::vector<uint8_t> mxscale1OutData(
        size1, 0);  // fp8 data cannot be printed directly in C. Read it as uint8 and manually convert the binary data to fp8.
    std::vector<uint8_t> mxscale2OutData(
        size2, 0);  // fp8 data cannot be printed directly in C. Read it as uint8 and manually convert the binary data to fp8.
    ret = aclrtMemcpy(mxscale1OutData.data(), mxscale1OutData.size() * sizeof(mxscale1OutData[0]), mxscale1OutDeviceAddr,
                      size1 * sizeof(mxscale1OutData[0]), ACL_MEMCPY_DEVICE_TO_HOST);
    CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("copy mxscale1Out from device to host failed. ERROR: %d\n", ret);
              return ret);
    ret = aclrtMemcpy(mxscale2OutData.data(), mxscale2OutData.size() * sizeof(mxscale2OutData[0]), mxscale2OutDeviceAddr,
                      size2 * sizeof(mxscale2OutData[0]), ACL_MEMCPY_DEVICE_TO_HOST);
    CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("copy mxscale2Out from device to host failed. ERROR: %d\n", ret);
              return ret);
    for (int64_t i = 0; i < size1; i++) {
        LOG_PRINT("mxscale1Out[%ld] is: %d\n", i, mxscale1OutData[i]);
    }
    for (int64_t i = 0; i < size2; i++) {
        LOG_PRINT("mxscale2Out[%ld] is: %d\n", i, mxscale2OutData[i]);
    }
    return ACL_SUCCESS;
}

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 = aclnnDynamicMxQuantWithDualAxisV2Test(deviceId, stream);
    CHECK_FREE_RET(ret == ACL_SUCCESS, LOG_PRINT("aclnnDynamicMxQuantWithDualAxisV2Test failed. ERROR: %d\n", ret); return ret);

    Finalize(deviceId, stream);
    return 0;
}