aclnnGroupedDynamicBlockQuant
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: Quantizes each group at the basic block granularity based on the starting values of the group indices (groupList), into (FP8/HiFP8), and outputs the quantization parameter scale (FP32).
Formula:
Function Prototype
Each operator uses a two-phase API. Call the "aclnnGroupedDynamicBlockQuantGetWorkspaceSize" API to obtain the workspace size required for computation and the executor that contains the operator computation flow, and then call the "aclnnGroupedDynamicBlockQuant" API to perform the computation.
aclnnStatus aclnnGroupedDynamicBlockQuantGetWorkspaceSize(
const aclTensor *x,
const aclTensor *groupList,
double minScale,
char *roundModeOptional,
int64_t dstType,
int64_t rowBlockSize,
int64_t colBlockSize,
int64_t groupListType,
const aclTensor *yOut,
const aclTensor *scaleOut,
uint64_t *workspaceSize,
aclOpExecutor **executor)aclnnStatus aclnnGroupedDynamicBlockQuant(
void *workspace,
uint64_t workspaceSize,
aclOpExecutor *executor,
aclrtStream stream)aclnnGroupedDynamicBlockQuantGetWorkspaceSize
Parameters:
Parameter Input/Output Description Instruction Data Type Data Format Shape Non-contiguous Tensor x (aclTensor*) Input Represents the input tensor of the operator. Corresponds to input in the formula. - FLOAT16, BFLOAT16 ND 2-3, in the shape of [M, N] and [B, M, N] √ groupList (aclTensor*) Input Represents the offset of each group on the M axis (cumsum mode). Start index of each quantization group. The value must be greater than or equal to 0 and non-decreasing, and the last number must be equal to the size of the -2 axis of x. INT32 ND 1 √ minScale (double) Input Minimum scale value used in scaleOut computation. It corresponds to min_scale in the formula. The value must be greater than or equal to 0. DOUBLE - - - roundModeOptional (char*) Input Indicates the approximate mode for the final cast from the high-bit data to the target data type. - When dstType is 35/36, corresponding to output yOut data types FLOAT8_E5M2/FLOAT8_E4M3FN, only {"rint"} is supported;
- When dstType is 34, corresponding to output yOut data type HIFLOAT8, {"round", "hybrid"} are supported;
- When a null pointer is passed in, the "rint" mode is used.
STRING - - - dstType (int64_t) Input Specifies the data type of yOut after data conversion. The input range is {34, 35, 36}, which corresponds to the data types of output y {34:HIFLOAT8, 35: FLOAT8_E5M2, 36: FLOAT8_E4M3FN}. INT64 - - - rowBlockSize (int64_t) Input Indicates the quantization granularity on the M axis. Currently supported values: 1/128/256/512. INT64 - - - colBlockSize (int64_t) Input Indicates the quantization granularity on the N axis. Currently supported values are 64/128/192/256. INT64 - - - groupListType (int64_t) Input Indicates the function type of group_list. Currently the supported value is 0, which corresponds to the cumsum mode. INT64 - - - yOut (aclTensor*) Output Quantized output tensor. Corresponds to y in the formula. The dimensions of shape are the same as those of x. HIFLOAT8, FLOAT8_E4M3FN, FLOAT8_E5M2 ND 2-3 × scaleOut (aclTensor*) Output Quantization scale corresponding to each group, which is the scale in the formula. - If the shape of input x is [M, N] and the shape of groupList is [g], the shape of output scaleOut is [(M//rowBlockSize+g), ceil(N/colBlockSize)].
- If the shape of input x is [B, M, N] and the shape of groupList is [g], the shape of output scaleOut is [B, (M//rowBlockSize+g), ceil(N/colBlockSize)].
FLOAT32 ND 2-3 × 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: Returned status code. For details, see 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 parameters x, groupList, yOut, or scaleOut are null pointers. ACLNN_ERR_PARAM_INVALID 161002 The input or output data format or data type is out of the supported range. The input or output data shape is out of the supported range.
aclnnGroupedDynamicBlockQuant
Parameters:
Parameter Input/Output Description workspace Input Workspace memory address allocated on the Device. workspaceSize Input Size of the workspace allocated on the Device, obtained by calling the first-phase API aclnnGroupedDynamicBlockQuantGetWorkspaceSize. 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
- Deterministic computation: aclnnGroupedDynamicBlockQuant defaults to a deterministic implementation.
- Supported use case note: Only the use case where both yOut and scaleOut are empty tensors is supported. Other empty tensor use cases are not supported.
Example
The following sample code is for reference only. For details about compilation and execution, see Compile and Run Samples.
#include <iostream>
#include <vector>
#include "acl/acl.h"
#include "aclnnop/aclnn_grouped_dynamic_block_quant.h"
#define CHECK_RET(cond, return_expr) \
do { \
if (!(cond)) { \
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;
}
void PrintOutResult(std::vector<int64_t> &shape, void** deviceAddr) {
auto size = GetShapeSize(shape);
std::vector<int8_t> resultData(size, 0);
auto ret = aclrtMemcpy(resultData.data(), resultData.size() * sizeof(resultData[0]), *deviceAddr, size * sizeof(resultData[0]), ACL_MEMCPY_DEVICE_TO_HOST);
CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("copy result from device to host failed. ERROR: %d\n", ret); return);
for (int64_t i = 0; i < size; i++) {
LOG_PRINT("result[%ld] is: %d\n", i, resultData[i]);
}
}
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 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 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 the aclCreateTensor API 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;
}
int main() {
// 1. (Boilerplate) Initialize device/stream. For details, see the acl API documentation.
// Set deviceId based on the actual device.
int32_t deviceId = 0;
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 inputs and outputs. Customize the construction according to the API.
std::vector<int64_t> xShape = {4, 2};
std::vector<int64_t> groupListShape = {1};
std::vector<int64_t> yShape = {4, 2};
std::vector<int64_t> scaleShape = {5, 1};
void* xDeviceAddr = nullptr;
void* groupListDeviceAddr = nullptr;
void* yDeviceAddr = nullptr;
void* scaleDeviceAddr = nullptr;
aclTensor* x = nullptr;
aclTensor* groupList = nullptr;
aclTensor* y = nullptr;
aclTensor* scale = nullptr;
std::vector<aclFloat16> xHostData = {1, 2, 3, 4, 5, 6, 7, 8};
std::vector<int32_t> groupListHostData = {1};
std::vector<uint8_t> yHostData(8, 0);
std::vector<float> scaleHostData = {0, 0, 0, 0, 0};
// Create x aclTensor.
ret = CreateAclTensor(xHostData, xShape, &xDeviceAddr, aclDataType::ACL_FLOAT16, &x);
CHECK_RET(ret == ACL_SUCCESS, return ret);
// Create groupList aclTensor.
ret = CreateAclTensor(groupListHostData, groupListShape, &groupListDeviceAddr, aclDataType::ACL_INT32, &groupList);
CHECK_RET(ret == ACL_SUCCESS, return ret);
// Create y aclTensor.
ret = CreateAclTensor(yHostData, yShape, &yDeviceAddr, aclDataType::ACL_FLOAT8_E5M2, &y);
CHECK_RET(ret == ACL_SUCCESS, return ret);
// Create scale aclTensor.
ret = CreateAclTensor(scaleHostData, scaleShape, &scaleDeviceAddr, aclDataType::ACL_FLOAT, &scale);
CHECK_RET(ret == ACL_SUCCESS, return ret);
// 3. Call the CANN operator library API. Change it to the specific API name.
uint64_t workspaceSize = 0;
aclOpExecutor* executor;
const char* roundMode = "rint";
float minScale = 0.0;
int64_t rowBlockSize = 1;
int64_t colBlockSize = 128;
int64_t groupListType = 0;
// Call the aclnnGroupedDynamicBlockQuant first-phase API.
ret = aclnnGroupedDynamicBlockQuantGetWorkspaceSize(x, groupList, minScale, (char *)roundMode, aclDataType::ACL_FLOAT8_E5M2, rowBlockSize, colBlockSize, groupListType, y, scale, &workspaceSize, &executor);
CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclnnGroupedDynamicBlockQuantGetWorkspaceSize failed. ERROR: %d\n", ret);
return ret);
// Allocate device memory based on the workspaceSize computed by the first-phase API.
void* workspaceAddr = nullptr;
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);
}
// Call the aclnnGroupedDynamicBlockQuant second-phase API.
ret = aclnnGroupedDynamicBlockQuant(workspaceAddr, workspaceSize, executor, stream);
CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclnnGroupedDynamicBlockQuant failed. ERROR: %d\n", ret);
return ret);
// 4. (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);
// 5. Obtain the output values. Copy the result on the device side to the host side. Change them according to the specific API definition.
LOG_PRINT("yOut is: \n");
PrintOutResult(yShape, &yDeviceAddr);
LOG_PRINT("scaleOut is: \n");
PrintOutResult(scaleShape, &scaleDeviceAddr);
// 6. Release aclTensor and aclScalar. Change them according to the specific API definition.
aclDestroyTensor(x);
aclDestroyTensor(groupList);
aclDestroyTensor(y);
aclDestroyTensor(scale);
// 7. Release device resources.
aclrtFree(xDeviceAddr);
aclrtFree(groupListDeviceAddr);
aclrtFree(yDeviceAddr);
aclrtFree(scaleDeviceAddr);
if (workspaceSize > 0) {
aclrtFree(workspaceAddr);
}
aclrtDestroyStream(stream);
aclrtResetDevice(deviceId);
aclFinalize();
return 0;
}