aclnnDynamicBlockMxQuant
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: Converts the input variables to the destination data type by performing MX quantization, with a data block (32*32) as the basic block. In each basic block, a scale algorithm is selected based on the value of scaleAlg to compute the quantization parameter scale (1*1) of the current block. The parameter is broadcast to scale1 (32*1) and scale2 (1*32) as the output. Meanwhile, each value in the basic block is divided by scale and converted to the corresponding dst_type based on round_mode to obtain the quantized result y.
Formula:
- Scenario 1, when scaleAlg is 0:
Groups the input x into blocks, with a data block (32*32) as the basic block. The values in one data block are quantized to
At the same time, the scale (1*1) is broadcast into scale1 (32*1) and scale2 (1*32) as the outputs scale1 and scale2. The quantized values are arranged at the positions of the corresponding to form the output y.
emax: Exponent bits of the maximum normal number for the corresponding data type.
- Scenario 1, when scaleAlg is 0:
| DataType | Emax | | :-----------: | :--: | | FLOAT4_E2M1 | 2 | | FLOAT4_E1M2 | 0 | | FLOAT8_E4M3FN | 8 | | FLOAT8_E5M2 | 15 |
- Use case 2: When scaleAlg is 2, only the FLOAT4_E2M1 data type is involved:
- The long vector is divided into blocks, each of length k. A block scaling factor is computed for each block independently, and then all elements in the block are mapped to the target low-precision data type FLOAT4_E2M1 using the same . The storage data type of scale is FLOAT8_E8M0. If the last block has fewer than k elements, the missing values are treated as 0 and the block is processed as a complete block.
- Find the maximum absolute value in the block:
- A new attribute dstTypeMax of the double type is introduced. It defaults to 0 (indicating that dstType is the maximum value of the target data type). If another value is passed in, scale is calculated based on the passed value. Currently, the valid values are 0.0, 6.0, and 7.0. This value can be set only in the FLOAT4_E2M1 use case.
- Maps FP32 to the range representable by the target data type FLOAT4_E2M1.
- Convert the block scale factor to the representable scale value in FP8 format.
- Extract the unbiased exponent and mantissa from the block floating-point scale factor .
- To ensure no overflow during quantization, round up the exponent and keep it within the representable range of FP8:
- Calculate the block scale factor:
- Calculate the block conversion factor:
- The final step of quantization: for each element in the block, . The final quantized output is , where denotes the block scaling factor, that is, , and denotes the quantized data in the block.
Function Prototype
Each operator uses a two-phase API. First, call "aclnnDynamicBlockMxQuantGetWorkspaceSize" to obtain the workspace size required for computation and the executor that contains the operator computation process, and then call "aclnnDynamicBlockMxQuant" to perform the computation.
aclnnStatus aclnnDynamicBlockMxQuantGetWorkspaceSize(
const aclTensor *x,
char *roundModeOptional,
int64_t dstType,
int64_t scaleAlg,
double dstTypeMax,
const aclTensor *yOut,
const aclTensor *scale1Out,
const aclTensor *scale2Out,
uint64_t *workspaceSize,
aclOpExecutor **executor)aclnnStatus aclnnDynamicBlockMxQuant(
void *workspace,
uint64_t workspaceSize,
aclOpExecutor *executor,
aclrtStream stream)aclnnDynamicBlockMxQuantGetWorkspaceSize
Parameters:
Parameter Input/Output Description Instruction Data Type Data Format Dimension (shape) Non-contiguous Tensor x (aclTensor*) Input Indicates the input x, corresponding to Vi in the formula. If dstType is FLOAT4_E2M1 or FLOAT4_E1M2, the last dimension of x must be an even number. Empty tensors are not supported. FLOAT16, BFLOAT16 ND 2-3 √ roundModeOptional (char*) Input Indicates the data conversion mode, corresponding to round_mode in the formula. - When dstType is 40/41, corresponding to output yOut data type FLOAT4_E2M1/FLOAT4_E1M2, {"rint", "floor", "round"} are supported;
- When dstType is 35/36, corresponding to output yOut data type 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 Indicates the specified type of yOut after data conversion. The value range is {35, 36, 40, 41}, corresponding to the data types of output yOut {35:FLOAT8_E5M2, 36:FLOAT8_E4M3FN, 40:FLOAT4_E2M1, 41:FLOAT4_E1M2}. INT64 - - - scaleAlg (int64_t) Input Indicates the calculation method of scale1Out and scale2Out. Currently, the supported values are 0 and 2, representing the OCP Microscaling Formats (Mx) Specification implementation and the Dynamic Dtype Range implementation, respectively. INT64 - - - dstTypeMax (double) Input Indicates the maximum value of the target data type. - Takes effect when scaleAlg=2 and dstType is FLOAT4_E2M1. The scale is calculated based on the input value.
- Currently, only 0.0/6.0/7.0 is supported for dstTypeMax.
DOUBLE - - - yOut (aclTensor*) Output Indicates the quantized result of input x, corresponding to Pi in the formula. The shape is the same as that of input x. FLOAT4_E2M1, FLOAT4_E1M2, FLOAT8_E4M3FN, FLOAT8_E5M2 ND 2-3 × scale1Out (aclTensor*) Output Indicates the quantization scale for each group along axis -1, corresponding to scale1 in the formula. The shape is the value of the -1 axis of x divided by 32 and rounded up, and is padded to an even number, with 0 as the padding value. FLOAT8_E8M0 ND 3-4 × scale2Out (aclTensor*) Output Indicates the quantization scale corresponding to each group on the -2 axis, which is scale2 in the corresponding 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 0 as the padding value;
- The scale2Out output needs to be interleaved for every two rows of data.
FLOAT8_E8M0 ND 3-4 × workspaceSize Output Returns the workspace size that needs to be requested on the Device. - - - - - executor Output Returns the op executor, which contains the operator computation flow. - - - - - Return Value
aclnnStatus: returns the status code. For details, see aclnn return code.
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, yOut, scale1Out, and scale2Out are not in the supported range. The shapes of x, yOut, scale1Out, or scale2Out do not meet the validation conditions. roundModeOptional, dstType, scaleAlg, or dstTypeMax is not set to a currently supported value. ACLNN_ERR_RUNTIME_ERROR 361001 The current platform is not within the range of supported platforms.
aclnnDynamicBlockMxQuant
Parameters:
Parameter Input/Output Description workspace Input The workspace memory address requested on the Device. workspaceSize Input The workspace size requested on the Device, obtained by the first-phase API aclnnDynamicBlockMxQuantGetWorkspaceSize. executor Input Op executor that contains the computation flow of the operator. stream Input Specifies the stream for executing tasks. Return Value
aclnnStatus: return status code. For details, see aclnn Return Codes.
Constraints
- Shape constraints on x, scale1Out, and scale2Out are as follows:
- The dimension of x should be 2-3.
- rank(scale1) = rank(x) + 1.
- rank(scale2) = rank(x) + 1.
- scale1.shape[-2] = (ceil(x.shape[-1] / 32) + 2 - 1) / 2.
- scale2.shape[-3] = (ceil(x.shape[-2] / 32) + 2 - 1) / 2.
- scale1.shape[-1] = 2.
- scale2.shape[-1] = 2.
- Other dimensions are consistent with input x.
- Example: If input x has shape [B, M, N] and the destination data type is FP8, the corresponding y has shape [B, M, N], scale1 has shape [B, M, (ceil(N/32) + 2 - 1) / 2, 2], and scale2 has shape [B, (ceil(M / 32) + 2 - 1) / 2, N, 2].
- Deterministic computation: aclnnDynamicBlockMxQuant 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 <cstdio>
#include "aclnnop/aclnn_dynamic_block_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, 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 host data to the 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;
}
void Finalize(int32_t deviceId, aclrtStream stream)
{
aclrtDestroyStream(stream);
aclrtResetDevice(deviceId);
aclFinalize();
}
int aclnnDynamicBlockMxQuantTest(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. Custom construction is required based on the API.
std::vector<int64_t> xShape = {1, 4};
std::vector<int64_t> yOutShape = {1, 4};
std::vector<int64_t> scale1OutShape = {1, 1, 2};
std::vector<int64_t> scale2OutShape = {1, 4, 2};
void* xDeviceAddr = nullptr;
void* yOutDeviceAddr = nullptr;
void* scale1OutDeviceAddr = nullptr;
void* scale2OutDeviceAddr = nullptr;
aclTensor* x = nullptr;
aclTensor* yOut = nullptr;
aclTensor* scale1Out = nullptr;
aclTensor* scale2Out = nullptr;
std::vector<uint16_t> xHostData = {0, 16640, 17024, 17408};
std::vector<uint8_t> yOutHostData = {0, 72, 96, 120};
std::vector<uint8_t> scale1OutHostData = {128, 0};
std::vector<uint8_t> scale2OutHostData = {128, 0, 128, 0, 128, 0, 128, 0};
char* roundModeOptional = const_cast<char*>("rint");
int64_t dstType = 36;
int64_t scaleAlg = 0;
double dstTypeMax = 0;
// 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 yOut aclTensor.
ret = CreateAclTensor(yOutHostData, yOutShape, &yOutDeviceAddr, aclDataType::ACL_FLOAT8_E4M3FN, &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 scale1Out aclTensor.
ret = CreateAclTensor(scale1OutHostData, scale1OutShape, &scale1OutDeviceAddr, aclDataType::ACL_FLOAT8_E8M0, &scale1Out);
std::unique_ptr<aclTensor, aclnnStatus (*)(const aclTensor*)> scale1OutTensorPtr(scale1Out, aclDestroyTensor);
std::unique_ptr<void, aclError (*)(void*)> scale1OutDeviceAddrPtr(scale1OutDeviceAddr, aclrtFree);
CHECK_RET(ret == ACL_SUCCESS, return ret);
// Create scale2Out aclTensor.
ret = CreateAclTensor(scale2OutHostData, scale2OutShape, &scale2OutDeviceAddr, aclDataType::ACL_FLOAT8_E8M0, &scale2Out);
std::unique_ptr<aclTensor, aclnnStatus (*)(const aclTensor*)> scale2OutTensorPtr(scale2Out, aclDestroyTensor);
std::unique_ptr<void, aclError (*)(void*)> scale2OutDeviceAddrPtr(scale2OutDeviceAddr, aclrtFree);
CHECK_RET(ret == ACL_SUCCESS, return ret);
// Call the CANN operator library API, and change it to the specific API name.
uint64_t workspaceSize = 0;
aclOpExecutor* executor;
// Call the first-phase API of aclnnDynamicBlockMxQuant.
ret = aclnnDynamicBlockMxQuantGetWorkspaceSize(x, roundModeOptional, dstType, scaleAlg, dstTypeMax, yOut, scale1Out, scale2Out, &workspaceSize, &executor);
CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclnnDynamicBlockMxQuantGetWorkspaceSize failed. ERROR: %d\n", ret);
return ret);
// Allocate device memory based on the workspaceSize computed 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 aclnnDynamicBlockMxQuant.
ret = aclnnDynamicBlockMxQuant(workspaceAddr, workspaceSize, executor, stream);
CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclnnDynamicBlockMxQuant 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 results on the device side to the host side. Modify it based on the definition of the specific API.
auto size1 = GetShapeSize(yOutShape);
std::vector<uint8_t> yOutData(
size1, 0); // In C, fp4 data cannot be printed directly. Read it as uint8 and manually convert the binary data to fp4
ret = aclrtMemcpy(yOutData.data(), yOutData.size() * sizeof(yOutData[0]), yOutDeviceAddr,
size1 * 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 < size1; i++) {
LOG_PRINT("yOut[%ld] is: %d\n", i, yOutData[i]);
}
auto size2 = GetShapeSize(scale1OutShape);
auto size3 = GetShapeSize(scale2OutShape);
std::vector<uint8_t> scale1OutData(
size2, 0); // In C, fp8 data cannot be printed directly. Read it as uint8 and manually convert the binary data to fp8
std::vector<uint8_t> scale2OutData(
size3, 0); // In C, fp8 data cannot be printed directly. Read it as uint8 and manually convert the binary data to fp8
ret = aclrtMemcpy(scale1OutData.data(), scale1OutData.size() * sizeof(scale1OutData[0]), scale1OutDeviceAddr,
size2 * sizeof(scale1OutData[0]), ACL_MEMCPY_DEVICE_TO_HOST);
CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("copy scale1Out from device to host failed. ERROR: %d\n", ret);
return ret);
ret = aclrtMemcpy(scale2OutData.data(), scale2OutData.size() * sizeof(scale2OutData[0]), scale2OutDeviceAddr,
size3 * sizeof(scale2OutData[0]), ACL_MEMCPY_DEVICE_TO_HOST);
CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("copy scale2Out from device to host failed. ERROR: %d\n", ret);
return ret);
for (int64_t i = 0; i < size2; i++) {
LOG_PRINT("scale1Out[%ld] is: %d\n", i, scale1OutData[i]);
}
for (int64_t i = 0; i < size3; i++) {
LOG_PRINT("scale2Out[%ld] is: %d\n", i, scale2OutData[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 = aclnnDynamicBlockMxQuantTest(deviceId, stream);
CHECK_FREE_RET(ret == ACL_SUCCESS, LOG_PRINT("aclnnDynamicBlockMxQuantTest failed. ERROR: %d\n", ret); return ret);
Finalize(deviceId, stream);
return 0;
}