aclnnDynamicDualLevelMxQuant

📄 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: MX quantization with the destination data type of the FLOAT4 class. Quantization is performed only on the trailing axis, and all preceding axes are merged. The input is divided into multiple data blocks by the given level0BlockSize, and level-0 quantization is performed on each data block to output the quantization scale level0ScaleOut. Then, the level-0 quantization result is used as the new input and divided into multiple data blocks by the given level1BlockSize, and level-1 quantization is performed on each data block to output the quantization scale level1ScaleOut. The data type is converted based on round_mode to obtain the quantization result yOut. See Figure.

    • Optional function: Fuses the smooth scale operation. Before the data input x is quantized, x=x*smooth_scale (broadcast element-wise multiplication) is performed.
  • Formula:

    • The input x is grouped on the trailing axis by k0k_0 = level0BlockSize values, and a group of k0k_0 values {{xi}i=1k0}\{\{x_i\}_{i=1}^{k_0}\} is dynamically quantized to {level0Scale,{tempi}i=1k0}\{level0Scale, \{temp_i\}_{i=1}^{k_0}\}, k0k_0 = level0BlockSize. Then temp is grouped on the trailing axis by k1k_1 = level1BlockSize values, and a group of k1k_1 values {{tempi}i=1k1}\{\{temp_i\}_{i=1}^{k_1}\} is dynamically quantized to {level1Scale,{yi}i=1k1}\{level1Scale, \{y_i\}_{i=1}^{k_1}\}, k1k_1 = level1BlockSize
    input_maxi=maxi(abs(xi))input\_max_i = max_i(abs(x_i)) level0Scale=input_maxi/(FP4_E2M1_MAX)level0Scale = input\_max_i / (FP4\_E2M1\_MAX) tempi=cast_to_x_type(xi/level0Scale), i from 1 to level0BlockSizetemp_i = cast\_to\_x\_type(x_i / level0Scale), \space i\space from\space 1\space to\space level0BlockSize shared_exp=floor(log2(maxi(∣tempi∣)))−emaxshared\_exp = floor(log_2(max_i(|temp_i|))) - emax level1Scale=2shared_explevel1Scale = 2^{shared\_exp} yi=cast_to_FP4_E2M1(tempi/level1Scale,round_mode), i from 1 to level1BlockSizey_i = cast\_to\_FP4\_E2M1(temp_i/level1Scale, round\_mode), \space i\space from\space 1\space to\space level1BlockSize
    • ​The quantized yiy_{i} values are placed at the positions of the corresponding xix_{i} values to form the output yOut. The level0Scale values of the groups on the trailing axis form the output level0ScaleOut, and the level1Scale values of the groups on the trailing axis form the output level1ScaleOut.

    • max_i represents the maximum value in the i-th group.

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

| DataType | Emax | | :-----------: | :--: | | FLOAT4_E2M1 | 2 |

Function Prototype

Each operator uses a two-phase API. Call "aclnnDynamicDualLevelMxQuantGetWorkspaceSize" to obtain the workspace size required for computation and the executor containing the operator computation process, and then call "aclnnDynamicDualLevelMxQuant" to perform the computation.

aclnnStatus aclnnDynamicDualLevelMxQuantGetWorkspaceSize(
  const aclTensor *x,
  const aclTensor *smoothScaleOptional,
  char            *roundModeOptional,
  int64_t          level0BlockSize,
  int64_t          level1BlockSize,
  const aclTensor *yOut,
  const aclTensor *level0ScaleOut,
  const aclTensor *level1ScaleOut,
  uint64_t        *workspaceSize,
  aclOpExecutor   **executor)
aclnnStatus aclnnDynamicDualLevelMxQuant(
  void          *workspace,
  uint64_t       workspaceSize,
  aclOpExecutor *executor,
  aclrtStream    stream)

aclnnDynamicDualLevelMxQuantGetWorkspaceSize

  • Parameters:

    Parameter Input/Output Description Instruction Data Type Data Format Dimension (shape) Non-contiguous Tensor
    x (aclTensor*) Input Represents the input x, corresponding to xi in the formula.
    • The last dimension of x must be an even number;
    • Does not support empty tensors.
    FLOAT16, BFLOAT16 ND 1-7 √
    smoothScaleOptional (aclTensor*) Input Represents the optional input smoothScaleOptional.
    • When the smooth scale operation does not need to be fused, set smooth_scale to nullptr;
    • When smooth_scale is not nullptr, the dtype of smooth_scale must be consistent with x, and its shape must be 1D with a length equal to the last dimension of x.
    FLOAT16, BFLOAT16 (consistent with input x) ND 1 √
    roundModeOptional (char*) Input Indicates the data conversion mode, corresponding to round_mode in the formula.
    • Supports {"rint", "round", "floor"};
    • Default value is "rint".
    STRING - - -
    level0BlockSize (int64_t) Input Represents the first-level-quantized block_size, corresponding to level0BlockSize in the corresponding formula. The input range is {512}. INT64 - - -
    level1BlockSize (int64_t) Input Indicates the block_size of second-level quantization, corresponding to level1BlockSize in the formula. The input range is {32}. INT64 - - -
    yOut (aclTensor*) Output Represents the quantized result of input x, corresponding to yi in the corresponding formula.
    • The shape is consistent with input x;
    • Does not support empty Tensor.
    FLOAT4_E2M1 ND 1-7 √
    level0ScaleOut (aclTensor*) Output Represents the scale of first-level quantization, corresponding to level0Scale in the corresponding formula.
    • The value of shape on the trailing axis is the value of the trailing axis of x divided by level0BlockSize, rounded up;
    • Does not support empty tensor.
    FLOAT32 ND 1-7 √
    level1ScaleOut (aclTensor*) Output Represents the second-level-quantized scale, which is level1Scale in the corresponding formula.
    • The size of shape is the dim of x + 1;
    • The values of shape on the last two axes are ((ceil(x.shape[-1] / level1Blocksize) + 2 - 1) / 2, 2), with even padding and a padding value of 0;
    • Does not support empty tensor.
    FLOAT8_E8M0 ND 1-8 √
    workspaceSize Output Returns the workspace size to be requested on the Device. - - - - -
    executor Output Returns the op executor, which contains the operator computation flow. - - - - -
  • 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 x contains a null pointer.
    ACLNN_ERR_PARAM_INVALID 161002 The data types and data formats of x, smoothScaleOptional, yOut, level0ScaleOut, and level1ScaleOut are not within the supported range.
    The shapes of x, smoothScaleOptional, yOut, level0ScaleOut, or level1ScaleOut do not meet the validation conditions.
    roundModeOptional, level0BlockSize, or level1BlockSize is not a currently supported value.
    ACLNN_ERR_RUNTIME_ERROR 361001 The current platform is not within the supported platform range.

aclnnDynamicDualLevelMxQuant

  • Parameters:

    Parameter Input/Output Description
    workspace Input Address of the workspace memory allocated on the Device.
    workspaceSize Input Size of the workspace memory allocated on the Device, obtained by calling the first-phase API aclnnDynamicDualLevelMxQuantGetWorkspaceSize.
    executor Input Op executor, containing the operator computation flow.
    stream Input Stream for executing tasks.
  • Return value

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

Constraints

  • The shape constraints for x, level0ScaleOut, and level1ScaleOut are described as follows:
    • rank(level1ScaleOut) = rank(x) + 1.
    • level0ScaleOut.shape[-1] = ceil(x.shape[-1] / level0Blocksize).
    • level1ScaleOut.shape[-2] = (ceil(x.shape[-1] / level1Blocksize) + 2 - 1) / 2.
    • level1ScaleOut.shape[-1] = 2.
    • Other dimensions are consistent with input x.
  • Deterministic computation description: aclnnDynamicDualLevelMxQuant 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_dual_level_mx_quant.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 code 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 memory on the 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);
    // Call aclrtMemcpy to copy data on the host to the memory on the 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);

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

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

int aclnnDynamicDualLevelMxQuantTest(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, which need to be customized based on the API
    std::vector<int64_t> xShape = {1, 512};
    std::vector<int64_t> smoothScaleOptionalShape = {512};
    std::vector<int64_t> yOutShape = {1, 512};
    std::vector<int64_t> level0ScaleOutShape = {1, 1};
    std::vector<int64_t> level1ScaleOutShape = {1, 8, 2};
    void* xDeviceAddr = nullptr;
    void* smoothScaleOptionalDeviceAddr = nullptr;
    void* yOutDeviceAddr = nullptr;
    void* level0ScaleOutDeviceAddr = nullptr;
    void* level1ScaleOutDeviceAddr = nullptr;
    aclTensor* x = nullptr;
    aclTensor* smoothScaleOptional = nullptr;
    aclTensor* yOut = nullptr;
    aclTensor* level0ScaleOut = nullptr;
    aclTensor* level1ScaleOut = nullptr;

    // Values corresponding to BF16 (0->0, 16640->8, 17024->64, 17408->512)
    std::vector<uint16_t> xHostData(512, 16640);
    std::vector<uint16_t> smoothScaleOptionalHostData(512, 0);
    // Values corresponding to float4_e2m1 (0->0, 72->4, 96->32, 120->256)
    std::vector<uint8_t> yOutHostData(512, 0);
    // Corresponding float32 value (0->0)
    std::vector<float> level0ScaleOutHostData = {{0}};
    //Corresponding float8_e8m0 value(128->2)
    std::vector<std::vector<std::vector<uint8_t>>> level1ScaleOutHostData(1, std::vector<std::vector<uint8_t>>(8, std::vector<uint8_t>(2, 0)));
    const char* roundModeOptional = "rint";
    int64_t level0Blocksize = 512;
    int64_t level1Blocksize = 32;

    // Create 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 smoothScaleOptional aclTensor.
    ret = CreateAclTensor(smoothScaleOptionalHostData, smoothScaleOptionalShape, &smoothScaleOptionalDeviceAddr, aclDataType::ACL_BF16, &smoothScaleOptional);
    std::unique_ptr<aclTensor, aclnnStatus (*)(const aclTensor*)> smoothScaleOptionalTensorPtr(smoothScaleOptional, aclDestroyTensor);
    std::unique_ptr<void, aclError (*)(void*)> smoothScaleOptionalDeviceAddrPtr(smoothScaleOptionalDeviceAddr, aclrtFree);
    CHECK_RET(ret == ACL_SUCCESS, return ret);
    // Create yOut aclTensor.
    ret = CreateAclTensor(yOutHostData, yOutShape, &yOutDeviceAddr, aclDataType::ACL_FLOAT4_E2M1, &yOut);
    std::unique_ptr<aclTensor, aclnnStatus (*)(const aclTensor*)> yOutTensorPtr(yOut, aclDestroyTensor);
    std::unique_ptr<void, aclError (*)(void*)> yOutDeviceAddrPtr(yOutDeviceAddr, aclrtFree);
    CHECK_RET(ret == ACL_SUCCESS, return ret);
    // Create level0ScaleOut aclTensor.
    ret = CreateAclTensor(level0ScaleOutHostData, level0ScaleOutShape, &level0ScaleOutDeviceAddr, aclDataType::ACL_FLOAT, &level0ScaleOut);
    std::unique_ptr<aclTensor, aclnnStatus (*)(const aclTensor*)> level0ScaleOutTensorPtr(level0ScaleOut, aclDestroyTensor);
    std::unique_ptr<void, aclError (*)(void*)> level0ScaleOutDeviceAddrPtr(level0ScaleOutDeviceAddr, aclrtFree);
    CHECK_RET(ret == ACL_SUCCESS, return ret);
    // Create level1ScaleOut aclTensor.
    ret = CreateAclTensor(level1ScaleOutHostData, level1ScaleOutShape, &level1ScaleOutDeviceAddr, aclDataType::ACL_FLOAT8_E8M0, &level1ScaleOut);
    std::unique_ptr<aclTensor, aclnnStatus (*)(const aclTensor*)> level1ScaleOutTensorPtr(level1ScaleOut, aclDestroyTensor);
    std::unique_ptr<void, aclError (*)(void*)> level1ScaleOutDeviceAddrPtr(level1ScaleOutDeviceAddr, aclrtFree);
    CHECK_RET(ret == ACL_SUCCESS, return ret);

    // Call the CANN operator library API. Replace it with the actual API name.
    uint64_t workspaceSize = 0;
    aclOpExecutor* executor;

    // Call the first-phase API of aclnnDynamicDualLevelMxQuant.
    ret = aclnnDynamicDualLevelMxQuantGetWorkspaceSize(x, smoothScaleOptional, (char*)roundModeOptional, level0Blocksize, level1Blocksize, yOut, level0ScaleOut, level1ScaleOut, &workspaceSize, &executor);
    CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclnnDynamicDualLevelMxQuantGetWorkspaceSize failed. ERROR: %d\n", ret);
              return ret);
    // Allocate device memory based on 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 aclnnDynamicDualLevelMxQuant
    ret = aclnnDynamicDualLevelMxQuant(workspaceAddr, workspaceSize, executor, stream);
    CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclnnDynamicDualLevelMxQuant failed. ERROR: %d\n", ret); return ret);

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

    // Obtain the output values and copy the result from device-side memory to host-side memory. Modify based on the specific API definition.
    auto size = GetShapeSize(yOutShape) / 2;
    std::vector<uint8_t> yOutData(
        size, 0);  // fp4 data cannot be printed directly in C. Read it as uint8 and manually convert the binary value to fp4.
    ret = aclrtMemcpy(yOutData.data(), yOutData.size() * sizeof(yOutData[0]), yOutDeviceAddr,
                      size * sizeof(yOutData[0]), ACL_MEMCPY_DEVICE_TO_HOST);
    CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("copy yOut from device to host failed. ERROR: %d\n", ret);
              return ret);
    for (int64_t i = 0; i < size; i++) {
        LOG_PRINT("yOut[%ld] is: %d\n", i, yOutData[i]);
    }
    size = GetShapeSize(level0ScaleOutShape);
    std::vector<float> level0ScaleOutData(
        size, 0);
    ret = aclrtMemcpy(level0ScaleOutData.data(), level0ScaleOutData.size() * sizeof(level0ScaleOutData[0]), level0ScaleOutDeviceAddr,
                      size * sizeof(level0ScaleOutData[0]), ACL_MEMCPY_DEVICE_TO_HOST);
    CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("copy level0ScaleOut from device to host failed. ERROR: %d\n", ret);
              return ret);
    for (int64_t i = 0; i < size; i++) {
        LOG_PRINT("level0ScaleOut[%ld] is: %f\n", i, level0ScaleOutData[i]);
    }
    size = GetShapeSize(level1ScaleOutShape);
    std::vector<uint8_t> level1ScaleOutData(
        size, 0);  // fp8 data cannot be printed directly in C. Read it as uint8 and manually convert the binary value to fp8.
    ret = aclrtMemcpy(level1ScaleOutData.data(), level1ScaleOutData.size() * sizeof(level1ScaleOutData[0]), level1ScaleOutDeviceAddr,
                      size * sizeof(level1ScaleOutData[0]), ACL_MEMCPY_DEVICE_TO_HOST);
    CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("copy level1ScaleOut from device to host failed. ERROR: %d\n", ret);
              return ret);
    for (int64_t i = 0; i < size; i++) {
        LOG_PRINT("level1ScaleOut[%ld] is: %d\n", i, level1ScaleOutData[i]);
    }
    return ACL_SUCCESS;
}

int main()
{
    // 1. (boilerplate) Initialize device/stream. See the acl API manual.
    // Fill in deviceId based on the actual device.
    int32_t deviceId = 0;
    aclrtStream stream;
    auto ret = aclnnDynamicDualLevelMxQuantTest(deviceId, stream);
    CHECK_FREE_RET(ret == ACL_SUCCESS, LOG_PRINT("aclnnDynamicDualLevelMxQuantTest failed. ERROR: %d\n", ret); return ret);

    Finalize(deviceId, stream);
    return 0;
}