aclnnAntiMxQuant
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: Dequantizes the FLOAT4/FLOAT8 tensor quantized by calling
aclnnDynamicMxQuant/aclnnDynamicMxQuantV2to the FLOAT16/BFLOAT16/FLOAT32 format.Formula:
- where is the scaling factor provided by the input mxscale; is the exponent bias. For the FLOAT8_E8M0 format, ; is the quantized FLOAT4/FLOAT8 tensor; is the dequantized FLOAT16/BFLOAT16/FLOAT32 tensor.
Function Prototype
Each operator uses a two-phase API. First call the aclnnAntiMxQuantGetWorkspaceSize API to obtain the workspace size required for computation and the executor that contains the operator computation process. Then call the aclnnAntiMxQuant API to execute the computation.
aclnnStatus aclnnAntiMxQuantGetWorkspaceSize(
const aclTensor *x,
const aclTensor *mxscale,
int64_t axis,
int64_t dstType,
const aclTensor *y,
uint64_t *workspaceSize,
aclOpExecutor **executor)aclnnStatus aclnnAntiMxQuant(
void *workspace,
uint64_t workspaceSize,
aclOpExecutor *executor,
aclrtStream stream)aclnnAntiMxQuantGetWorkspaceSize
Parameters:
Parameter Input/Output Description Instruction Data Type Data Format Dimension (shape) Non-contiguous Tensor x (aclTensor*) Input Represents the quantized tensor input of the operator. Corresponds to Xq in the corresponding formula. Supports empty tensors. FLOAT4_E2M1, FLOAT4_E1M2, FLOAT8_E4M3FN, FLOAT8_E5M2 ND 1-7. Must be consistent with the output of DynamicMxQuant. Non-contiguous tensors are supported by FLOAT8, but not supported by FLOAT4. mxscale (aclTensor*) Input Quantization scale calculated by calling DynamicMxQuant. Corresponds to sf in the corresponding formula. - Supports empty tensor.
- The shape must satisfy the formula in Constraints.
FLOAT8_E8M0 ND 2-8, must be consistent with the output of DynamicMxQuant √ axis (int64_t) Input Specifies the dequantization axis. Value range: [-D, D-1], where D is the number of dimensions of input x. INT64 - - - dstType (int64_t) Input Specifies the data type of output y. Input range: {0, 1, 27}, corresponding to the data type of output y: {0: FLOAT32, 1: FLOAT16, 27: BFLOAT16}. INT64 - - - y (aclTensor*) Output Dequantized output tensor. Xdq in the corresponding formula. - Supports empty tensor.
- The shape dimensions are the same as x.
- The data type must match dstType.
FLOAT16, BFLOAT16, FLOAT32 ND 1-7 × workspaceSize Output Returns the workspace size to be allocated on the Device. - - - - - executor Output Returns the op executor, which contains the operator computation process. - - - - - 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 The passed-in parameter is a required input or output and is a null pointer. ACLNN_ERR_PARAM_INVALID 161002 The data types of x, mxscale, and y are not within the supported range. The shapes of x, mxscale, and y do not meet the check conditions. The dimensions of x, mxscale, and y are not within the supported range. The values of axis and dstType are not within the currently supported values. The data type of y does not match the corresponding relationship with dstType. ACLNN_ERR_RUNTIME_ERROR 361001 The current platform is not in the range of supported platforms.
aclnnAntiMxQuant
Parameters:
Parameter Input/Output Description workspace Input Workspace memory address requested on the device. workspaceSize Input Workspace size requested on the device, obtained by the first-phase API aclnnAntiMxQuantGetWorkspaceSize. executor Input Op executor, which contains the operator computation flow. stream Input Specifies the stream that executes the task. Return value
aclnnStatus: return status code. For details, see aclnn Error Codes.
Constraints
- Deterministic computation:
aclnnAntiMxQuantdefaults to a deterministic implementation.
- Constraints on the shapes of x and mxscale are as follows:
- If the data type of input x is float4_e2m1 or float4_e1m2, x.shape[-1] must be an even number.
- axis_change = axis if axis >= 0 else axis + rank(x).
- mxscale.shape[axis_change] = (ceil(x.shape[axis] / 32) + 2 - 1) / 2.
- mxscale.shape[-1] = 2.
- rank(mxscale) = rank(x) + 1.
- Other dimensions are consistent with input x.
Example
The following sample code is for reference only. For details about the compilation and execution process, see Compile and Run Samples.
#include <iostream>
#include <memory>
#include <vector>
#include "acl/acl.h"
#include "aclnnop/aclnn_anti_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 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);
// 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 aclnnAntiMxQuantTest(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 as required by the API.
// x: FP8_E4M3FN, shape {1, 4}
std::vector<int64_t> xShape = {1, 4};
// mxscale: FP8_E8M0, shape {1, 1, 2}
// ceil(4/32) = 1, (1+2-1)/2 = 1, append 2 -> {1, 1, 2}
std::vector<int64_t> mxscaleShape = {1, 1, 2};
// y: BF16, shape {1, 4}
std::vector<int64_t> yOutShape = {1, 4};
void* xDeviceAddr = nullptr;
void* mxscaleDeviceAddr = nullptr;
void* yOutDeviceAddr = nullptr;
aclTensor* x = nullptr;
aclTensor* mxscale = nullptr;
aclTensor* yOut = nullptr;
std::vector<uint8_t> xHostData = {0, 72, 96, 120};
std::vector<uint8_t> mxscaleHostData = {128, 0};
std::vector<uint16_t> yOutHostData = {0, 16640, 17024, 17408};
int64_t axis = -1;
int64_t dstType = 27; // BFLOAT16
// Create the x aclTensor(FP8_E4M3FN).
ret = CreateAclTensor(xHostData, xShape, &xDeviceAddr, aclDataType::ACL_FLOAT8_E4M3FN, &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 mxscale aclTensor(FP8_E8M0).
ret = CreateAclTensor(mxscaleHostData, mxscaleShape, &mxscaleDeviceAddr, aclDataType::ACL_FLOAT8_E8M0, &mxscale);
std::unique_ptr<aclTensor, aclnnStatus (*)(const aclTensor*)> mxscaleTensorPtr(mxscale, aclDestroyTensor);
std::unique_ptr<void, aclError (*)(void*)> mxscaleDeviceAddrPtr(mxscaleDeviceAddr, aclrtFree);
CHECK_RET(ret == ACL_SUCCESS, return ret);
// Create the yOut aclTensor(BF16).
ret = CreateAclTensor(yOutHostData, yOutShape, &yOutDeviceAddr, aclDataType::ACL_BF16, &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);
// Call the CANN operator library API.
uint64_t workspaceSize = 0;
aclOpExecutor* executor;
// Call the first-phase API of aclnnAntiMxQuant.
ret = aclnnAntiMxQuantGetWorkspaceSize(x, mxscale, axis, dstType, yOut, &workspaceSize, &executor);
CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclnnAntiMxQuantGetWorkspaceSize 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 aclnnAntiMxQuant.
ret = aclnnAntiMxQuant(workspaceAddr, workspaceSize, executor, stream);
CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclnnAntiMxQuant failed. ERROR: %d\n", ret); return ret);
// 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 value of the output y and copy the result from device memory to host memory.
auto size = GetShapeSize(yOutShape);
std::vector<uint16_t> yOutData(size, 0);
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("y[%ld] is: %u\n", i, yOutData[i]);
}
return ACL_SUCCESS;
}
int main()
{
// 1. (Boilerplate) Initialize device/stream. For details, see the acl API guide.
// Set deviceId based on the actual device.
int32_t deviceId = 0;
aclrtStream stream;
auto ret = aclnnAntiMxQuantTest(deviceId, stream);
CHECK_FREE_RET(ret == ACL_SUCCESS, LOG_PRINT("aclnnAntiMxQuantTest failed. ERROR: %d\n", ret); return ret);
Finalize(deviceId, stream);
return 0;
}