aclnnDynamicMxQuantWithDualAxis
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 , the dimensions before the -2 axis are merged into one dimension. This is equivalent to reshaping the input to 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 is quantized into
Simultaneously, group the input x along the -2 axis by 32 elements, where a group of 32 elements is quantized into
The quantized on the -1 axis is composed into output y1Out according to the corresponding positions of , and mxscale1 is composed into output mxscale1Out according to the grouping on the -1 axis dimension. The quantized on the -2 axis is composed into output y2Out according to the corresponding positions of , 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.
- Use case 1: When scaleAlg is 0, that is, the OCP Microscaling Formats (Mx) Specification implementation:
| 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 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 . 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:
- Map FP32 to the representable range of the target data type FP8, where is the maximum value that the target precision can represent.
- 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:
- Apply the final step of quantization. For each element in a group, . The final quantization result of the -1 axis output is , where represents the block scaling factor, that is, , and 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 .
- 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.
- -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 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 . 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.
Function Prototype
Each operator uses a two-phase API. Call "aclnnDynamicMxQuantWithDualAxisGetWorkspaceSize" first to obtain the workspace size required for computation and the executor that includes the calculation process of the operator, and then call "aclnnDynamicMxQuantWithDualAxis" to perform the computation.
aclnnStatus aclnnDynamicMxQuantWithDualAxisGetWorkspaceSize(
const aclTensor *x,
char *roundModeOptional,
int64_t dstType,
int64_t scaleAlg,
const aclTensor *y1Out,
const aclTensor *mxscale1Out,
const aclTensor *y2Out,
const aclTensor *mxscale2Out,
uint64_t *workspaceSize,
aclOpExecutor **executor)aclnnStatus aclnnDynamicMxQuantWithDualAxis(
void *workspace,
uint64_t workspaceSize,
aclOpExecutor *executor,
aclrtStream stream)aclnnDynamicMxQuantWithDualAxisGetWorkspaceSize
Parameters:
Parameter Input/Output Description Instruction Data Type Data Format Dimension (shape) Non-contiguous Tensor x (aclTensor*) Input Represents the input x, corresponding to Vi in the formula. - If the destination type is FLOAT4_E2M1 or FLOAT4_E1M2, the last dimension of x must be an even number.
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, corresponding to the data types FLOAT4_E2M1/FLOAT4_E1M2 of outputs y1Out and y2Out, {"rint", "floor", "round"} is supported.
- When dstType is 35/36, corresponding to the data types FLOAT8_E5M2/FLOAT8_E4M3FN of outputs y1Out and y2Out, only {"rint"} is supported.
- If a null pointer is passed in, the "rint" mode is used.
STRING - - - dstType (int64_t) Input Represents the type of y1Out and y2Out after the specified data conversion. - The value range is {35, 36, 40, 41}, corresponding to the data types {35:FLOAT8_E5M2, 36:FLOAT8_E4M3FN, 40:FLOAT4_E2M1, 41:FLOAT4_E1M2} of outputs y1Out and y2Out.
INT64 - - - scaleAlg (int64_t) Input Represents the calculation method of mxscale1Out and mxscale2Out. See scaleAlg in the corresponding formula. - Supported values are 0 and 1. Value 0 represents use case 1, and value 1 represents use case 2.
- When dstType is FLOAT4_E2M1/FLOAT4_E1M2, only value 0 is supported.
INT64 - - - y1Out (aclTensor*) Output Represents the result of quantizing the input x along the -1 axis, corresponding to Pi in the formula. - The shape is consistent with the input x.
FLOAT4_E2M1, FLOAT4_E1M2, FLOAT8_E4M3FN, FLOAT8_E5M2 ND 2-7 × mxscale1Out (aclTensor*) Output Represents the quantization scale corresponding to each group along the -1 axis, corresponding to mxscale1 in the formula. - The shape is the value of the -1 axis of x divided by 32, rounded up, and then even-padded with a pad fill value of 0.
FLOAT8_E8M0 ND 2-8 × y2Out (aclTensor*) Output Represents the corresponding result of input x after quantization along the -2 axis, corresponding to Pj in the formula. - Same shape as input x.
FLOAT4_E2M1, FLOAT4_E1M2, FLOAT8_E4M3FN, FLOAT8_E5M2 ND 2-7 × mxscale2Out (aclTensor*) Output Represents the quantization scale corresponding to each grouping along the -2 axis, corresponding to mxscale2 in the formula. - The shape is the value of the -2 axis of x divided by 32, rounded up, and padded to an even number, with a pad value of 0.
- The mxscale2Out output requires interleaving every two rows of data.
FLOAT8_E8M0 ND 2-8 × workspaceSize (uint64_t*) Output Returns the workspace size to be applied on the Device side. - - - - - executor (aclOpExecutor**) Output Returns the op executor, which includes the calculation process of the operator. - - - - - Return value
aclnnStatus: Returns a status code. For details, see aclnn Error 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, y1Out, mxscale1Out, y2Out, and mxscale2Out are not within the supported range. The shapes of x, y1Out, y2Out, mxscale1Out, or mxscale2Out do not meet the validation conditions. roundModeOptional, dstType, or scaleAlg is not a currently supported value. ACLNN_ERR_RUNTIME_ERROR 361001 The current platform is not within the range of supported platforms.
aclnnDynamicMxQuantWithDualAxis
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 aclnnDynamicMxQuantWithDualAxisGetWorkspaceSize. executor Input Op executor. It includes the calculation process of the operator. stream Input Specifies the stream on which tasks are executed. Return value
aclnnStatus: return status code. For details, see aclnn Error 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].
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.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 data from the host 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);
// 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 aclnnDynamicMxQuantWithDualAxisTest(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, customized based on 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;
// 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, and change it to the specific API name.
uint64_t workspaceSize = 0;
aclOpExecutor* executor;
// Call the first-phase API aclnnDynamicMxQuantWithDualAxis.
ret = aclnnDynamicMxQuantWithDualAxisGetWorkspaceSize(x, roundModeOptional, dstType, scaleAlg, y1Out, mxscale1Out, y2Out, mxscale2Out, &workspaceSize, &executor);
CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclnnDynamicMxQuantWithDualAxisGetWorkspaceSize 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 aclnnDynamicMxQuantWithDualAxis.
ret = aclnnDynamicMxQuantWithDualAxis(workspaceAddr, workspaceSize, executor, stream);
CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclnnDynamicMxQuantWithDualAxis 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, copy the results from device memory to the host, and modify them according to the interface definition of the specific API.
auto size1 = GetShapeSize(y1OutShape);
auto size2 = GetShapeSize(y2OutShape);
std::vector<uint8_t> y1OutData(
size1, 0); // In C, fp8 data cannot be printed directly. Read it as uint8 and manually convert it to fp8 through binary.
std::vector<uint8_t> y2OutData(
size2, 0); // In C, fp8 data cannot be printed directly. Read it as uint8 and manually convert it to fp8 through binary.
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); // In C, fp8 data cannot be printed directly. Read it as uint8 and manually convert it to fp8 through binary.
std::vector<uint8_t> mxscale2OutData(
size2, 0); // In C, fp8 data cannot be printed directly. Read it as uint8 and manually convert it to fp8 through binary.
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 according to the actual device.
int32_t deviceId = 0;
aclrtStream stream;
auto ret = aclnnDynamicMxQuantWithDualAxisTest(deviceId, stream);
CHECK_FREE_RET(ret == ACL_SUCCESS, LOG_PRINT("aclnnDynamicMxQuantWithDualAxisTest failed. ERROR: %d\n", ret); return ret);
Finalize(deviceId, stream);
return 0;
}