aclnnDequantSituQuant
Applicable Products
Ascend 950PR&950DT products : Not supported
Atlas A3 products : Supported
Atlas A2 products : Supported
Atlas 200I/500 A2 inference products : Not supported
Atlas inference products : Not supported
Atlas training products : Not supported
Function
Description: Adds dequant and quant operations before and after the Situ activation function to implement DequantSituQuant computation of x.
Formula:
The dequantization path varies depending on the data type of input x:
INT8 path
INT32 path
BF16/FLOAT16 path (pre-dequantization)
Situ Activation
When linear_beta > 0:
When activate_left is true, gate takes the first half of dequantOut and up takes the second half. When activate_left is false, gate takes the second half of dequantOut and up takes the first half.
Quantization
quant_type = static
quant_type = dynamic
Function Prototype
Each operator uses a two-phase API. You must first call the "aclnnDequantSituQuantGetWorkspaceSize" API to obtain the workspace size required for the calculation and the executor containing the operator calculation flow, and then call the "aclnnDequantSituQuant" API to perform the calculation.
aclnnStatus aclnnDequantSituQuantGetWorkspaceSize(
const aclTensor* x,
const aclTensor* weightScaleOptional,
const aclTensor* activationScaleOptional,
const aclTensor* biasOptional,
const aclTensor* quantScaleOptional,
const aclTensor* quantOffsetOptional,
const aclTensor* groupIndexOptional,
float beta,
float linearBeta,
bool activateLeft,
char* quantTypeOptional,
const aclTensor* yOut,
const aclTensor* yScaleOut,
uint64_t* workspaceSize,
aclOpExecutor** executor)aclnnStatus aclnnDequantSituQuant(
void* workspace,
uint64_t workspaceSize,
aclOpExecutor* executor,
aclrtStream stream)aclnnDequantSituQuantGetWorkspaceSize
Parameters:
Parameter Input/Output Description Instruction Data Type Data Format Dimension (shape) Non-contiguous Tensor x (aclTensor*) Input The input x in the formula. - Empty tensors are not supported.
- When the data type of x is INT8, the dimension of x must be ≥2; when the data type of x is INT32/BF16/FLOAT16, the dimension of x must be 2.
- The last dimension must be a multiple of 2.
INT8, INT32, BFLOAT16, FLOAT16 ND 2-8 x weightScaleOptional (aclTensor*) Input The weight scale for dequantization. - Empty tensors are not supported.
- Shape is (2H,) or (1,).
- Required for INT8 and INT32, not used for BF16/FLOAT16.
FLOAT ND 1 x activationScaleOptional (aclTensor*) Input Activation scale for dequantization. - Empty tensors are not supported.
- Shape is (1,).
- Required for INT32 only, not used for other data types.
FLOAT ND 1 x biasOptional (aclTensor*) Input Bias for dequantization. - Empty tensors are not supported.
- The shape is (2H,) or (1,).
- Optional for INT8 and INT32, not used for BF16/FLOAT16.
FLOAT ND 1 x quantScaleOptional (aclTensor*) Input Quantization scale. - Empty tensors are not supported.
- Required when `quantTypeOptional` is static, with shape (H,) or (1,).
- Optional when `quantTypeOptional` is dynamic, used as smoothScale.
- Used only for INT8.
FLOAT ND 1 x quantOffsetOptional (aclTensor*) Input Quantization offset. - Empty tensors are not supported.
- Required when `quantTypeOptional` is static. The shape is the same as that of `quantScaleOptional`.
- Optional when `quantTypeOptional` is dynamic.
FLOAT ND 1 x groupIndexOptional(aclTensor*) Input group_index of the MoE group. - Empty tensors are not supported.
- INT32/BF16/FLOAT16 are optional; INT8 is not used.
INT64 ND 1 x beta (float) Input The beta parameter of the Situ activation. Cannot be 0. Defaults to 4.0. - - - - linearBeta (float) Input The linear_beta parameter of the Situ activation. Not enabled when the value is ≤ 0. Defaults to 25.0. - - - - activateLeft(bool) Input Specifies whether to perform Situ activation on the left half of the input. When the value is false, activation is performed on the right half of the input. Defaults to true. - - - - quantTypeOptional(char*) Input Quantization mode. Supports only {"static", "dynamic"}. Defaults to "dynamic". - - - - yOut (aclTensor*) Output Quantized output. - Empty tensors are not supported.
- INT8: shape is x.shape[:-1]+[H]; others: shape is [M, H].
INT8 ND 2-8 x yScaleOut (aclTensor*) Output Scale for dynamic quantization. - Empty tensors are not supported.
- INT8: shape is x.shape[:-1]; others: shape is [M].
- When `quantTypeOptional` is static, the output is a meaningless value.
FLOAT ND 1-7 x workspaceSize (uint64_t*) Output Returns the workspace size to be requested on the device. - - - - - executor (aclOpExecutor**) Output Returns an op executor that 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 x, yOut, and yScaleOut are null pointers. |
| For INT8, weightScaleOptional is a null pointer. | ||
| For INT32, weightScaleOptional or activationScaleOptional is a null pointer. | ||
| When `quantTypeOptional` is static, quantScaleOptional is a null pointer. | ||
| ACLNN_ERR_PARAM_INVALID | 161002 | The passed x, yOut, and yScaleOut are empty tensors. |
| The data types of x, weightScaleOptional, and other inputs are out of the supported range. | ||
| quantTypeOptional is out of the supported range. | ||
| The last dimension of x is not a multiple of 2. | ||
| beta is 0. | ||
| The input constraints are not met. | ||
| ACLNN_ERR_RUNTIME_ERROR | 361001 | The current platform is out of the supported range. |
aclnnDequantSituQuant
Parameters:
Parameter Input/Output Description workspace Input Workspace memory address requested on the Device. workspaceSize Input Size of the workspace memory requested on the Device, obtained by the first-phase API aclnnDequantSituQuantGetWorkspaceSize. executor Input Op executor that contains the operator computing process. stream Input Stream for task execution. Return value
aclnnStatus: Return status code. For details, see Error Codes of aclnn APIs.
Constraints
Deterministic computation:
- aclnnDequantSituQuant defaults to a deterministic implementation.
The last dimension of x must be a multiple of 2.
When the data type of x is INT8, x must have at least 2 dimensions; when the data type of x is INT32/BF16/FLOAT16, x must have 2 dimensions.
beta cannot be 0.
INT8:
weightScaleOptionalis required, whileactivationScaleOptionalandgroupIndexOptionalare forbidden.INT32:
weightScaleOptionalandactivationScaleOptionalare required, whilequantScaleOptionalandquantOffsetOptionalare forbidden.BF16/FLOAT16: None of the optional inputs is used (pre-dequantization mode).
When
quantTypeOptionalis static,quantScaleOptionalis required.When
quantTypeOptionalis dynamic,quantScaleOptionalis optional (used as smoothScale).
Example
The sample code is as follows. 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_dequant_situ_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;
}
int Init(int32_t deviceId, aclrtStream* stream)
{
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);
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);
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);
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];
}
*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 the device/stream. Refer to the acl API manual
// 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 input and output. Customize the construction based on the API definition.
std::vector<int64_t> xShape = {16, 64};
std::vector<int64_t> wsShape = {64};
std::vector<int64_t> yShape = {16, 32};
std::vector<int64_t> yScaleShape = {16};
auto xSize = GetShapeSize(xShape);
std::vector<int8_t> xHostData(xSize);
for (int64_t i = 0; i < xSize; i++) {
xHostData[i] = static_cast<int8_t>((i * 7 + 3) % 100);
}
std::vector<float> wsHostData(64, 0.1f);
std::vector<int8_t> yHostData(GetShapeSize(yShape), 0);
std::vector<float> yScaleHostData(GetShapeSize(yScaleShape), 0.0f);
void* xDeviceAddr = nullptr;
void* wsDeviceAddr = nullptr;
void* yDeviceAddr = nullptr;
void* yScaleDeviceAddr = nullptr;
aclTensor* x = nullptr;
aclTensor* ws = nullptr;
aclTensor* y = nullptr;
aclTensor* yScale = nullptr;
ret = CreateAclTensor(xHostData, xShape, &xDeviceAddr, aclDataType::ACL_INT8, &x);
CHECK_RET(ret == ACL_SUCCESS, return ret);
ret = CreateAclTensor(wsHostData, wsShape, &wsDeviceAddr, aclDataType::ACL_FLOAT, &ws);
CHECK_RET(ret == ACL_SUCCESS, return ret);
ret = CreateAclTensor(yHostData, yShape, &yDeviceAddr, aclDataType::ACL_INT8, &y);
CHECK_RET(ret == ACL_SUCCESS, return ret);
ret = CreateAclTensor(yScaleHostData, yScaleShape, &yScaleDeviceAddr, aclDataType::ACL_FLOAT, &yScale);
CHECK_RET(ret == ACL_SUCCESS, return ret);
float beta = 4.0;
float linearBeta = 25.0;
bool activateLeft = true;
// 3. 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 aclnnDequantSituQuant.
ret = aclnnDequantSituQuantGetWorkspaceSize(x, ws, nullptr, nullptr, nullptr, nullptr, nullptr,
beta, linearBeta, activateLeft,
const_cast<char*>("dynamic"),
y, yScale, &workspaceSize, &executor);
CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclnnDequantSituQuantGetWorkspaceSize failed. ERROR: %d\n", ret); return ret);
// Allocate device memory based on the workspaceSize calculated 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 second-phase API of aclnnDequantSituQuant.
ret = aclnnDequantSituQuant(workspaceAddr, workspaceSize, executor, stream);
CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclnnDequantSituQuant failed. ERROR: %d\n", ret); return ret);
// 4. (Boilerplate) Synchronize and 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 value. Copy the result from the device memory to the host. Modify the code based on the specific API definition.
auto yResultSize = GetShapeSize(yShape);
std::vector<int8_t> yResult(yResultSize, 0);
ret = aclrtMemcpy(yResult.data(), yResult.size() * sizeof(int8_t), yDeviceAddr,
yResultSize * sizeof(int8_t), ACL_MEMCPY_DEVICE_TO_HOST);
CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("copy yResult from device to host failed. ERROR: %d\n", ret); return ret);
for (int64_t i = 0; i < yResultSize; i++) {
LOG_PRINT("y[%ld] is: %d\n", i, yResult[i]);
}
// 6. Destroy the aclTensor and aclScalar. Modify the code based on the specific API definition.
aclDestroyTensor(x);
aclDestroyTensor(ws);
aclDestroyTensor(y);
aclDestroyTensor(yScale);
// 7. Release device resources. Modify the code based on the specific API definition.
aclrtFree(xDeviceAddr);
aclrtFree(wsDeviceAddr);
aclrtFree(yDeviceAddr);
aclrtFree(yScaleDeviceAddr);
if (workspaceSize > 0) {
aclrtFree(workspaceAddr);
}
aclrtDestroyStream(stream);
aclrtResetDevice(deviceId);
aclFinalize();
return 0;
}