aclnnQuantizedBatchNorm
Applicable Products
Ascend 950PR&950DT products : 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:
Performs dequantization on the input tensor, performs normalization based on the input weight, bias, and epsilon, and performs quantization based on the input outputScale and outputZeroPoint.
Formula:
Dequantization computation:
Normalization calculation:
Quantization calculation:
Function Prototype
Each operator uses a two-phase API. You must first call the "aclnnQuantizedBatchNormGetWorkspaceSize" API to obtain the input parameters and calculate the required workspace size based on the computation flow, and then call the "aclnnQuantizedBatchNorm" API to perform the computation.
aclnnStatus aclnnQuantizedBatchNormGetWorkspaceSize(
const aclTensor* input,
const aclTensor* mean,
const aclTensor* var,
const aclScalar* inputScale,
const aclScalar* inputZeroPoint,
const aclScalar* outputScale,
const aclScalar* outputZeroPoint,
aclTensor* weight,
aclTensor* bias,
float epsilon,
aclTensor* output,
uint64_t *workspaceSize,
aclOpExecutor **executor)aclnnStatus aclnnQuantizedBatchNorm(
void *workspace,
uint64_t workspaceSize,
aclOpExecutor *executor,
aclrtStream stream)aclnnQuantizedBatchNormGetWorkspaceSize
Parameters
Parameter Input/Output Description Instruction Data Type Data Format Dimension (shape) Non-contiguous Tensor input (aclTensor*) Input Quantized data of the model input, corresponding to `x` in the formula. Does not support null tensor. INT8, UINT8, INT32 NCHW 4 √ mean (aclTensor*) Input Mean of the model input data, corresponding to `mean` in the formula. - Empty tensors are not supported.
- The shape length is equal to the length of the channel axis in input parameter `input`.
FLOAT32 ND 1 √ var (aclTensor*) Input Variance of the model input data, corresponding to `var` in the formula. - Empty tensors are not supported.
- The shape length is equal to the length of the channel axis in input parameter `input`.
FLOAT32 ND 1 √ inputScale (aclScalar*) Input Represents the scale factor of the model input data, corresponding to `inputScale` in the formula. - FLOAT32 ND - - inputZeroPoint (aclScalar*) Input Represents the bias of the model input data, corresponding to `inputZeroPoint` in the formula. The value passed in cannot exceed the upper and lower bounds of the data type of `input`, for example, the bounds of INT8 are [-128,127]. INT32 ND - - outputScale (aclScalar*) Input Denotes the scaling factor of the model output data, corresponding to `outputScale` in the formula. - FLOAT32 ND - - outputZeroPoint (aclScalar*) Input Denotes the offset of the model output data, corresponding to `outputZeroPoint` in the formula. The passed-in value cannot exceed the upper and lower bounds of the data type of `input`. For example, the upper and lower bounds of INT8 are [-128,127]. INT32 ND - - weight (aclTensor*) Input Optional parameter, indicating the normalization weight, corresponding to `weight` in the formula. - Does not support null tensors.
- Default value: 1.
- The shape length is equal to the channel axis length in input parameter `input`.
FLOAT32 ND 1 √ bias (aclTensor*) Input Optional parameter, indicating the normalization bias, corresponding to `bias` in the formula. - Does not support null tensors.
- Default value: 0.
- The shape length and input parameter `input`'s channel axis length are equal.
FLOAT32 ND 1 √ epsilon (float) Input Value added to the variance to avoid division by zero. This is the `epsilon` in the corresponding formula. - - - - - output (aclTensor*) Output Represents the quantized data of the model output, corresponding to `output` in the formula. - Does not support null tensors.
- The shape, data format, and data type are the same as those of `input`.
INT8, UINT8, INT32 NCHW 4 √ workspaceSize (uint64_t*) Output Returns the size of the workspace to be side-applied by the user on the Device. - - - - - executor (aclOpExecutor**) Output Returns the op executor, which contains the operator computation flow. - - - - - 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 input or output is a null pointer. ACLNN_ERR_PARAM_INVALID 161002 The data type is not within the supported range, or the parameters do not meet the preceding constraints.
aclnnQuantizedBatchNorm
Parameters
Parameter Input/Output Description workspace Input Side-applied workspace memory address on the Device. workspaceSize Input Side-applied workspace size on the Device, obtained by calling the first-phase API aclnnQuantizedBatchNormGetWorkspaceSize. executor Input Op executor, which contains the calculation process of the operator. stream Input Stream for task execution. Return Value
aclnnStatus: return status code. For details, see aclnn Error Codes.
Constraints
- Deterministic computation:
- aclnnQuantizedBatchNorm 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 <vector>
#include "acl/acl.h"
#include "aclnnop/aclnn_quantized_batch_norm.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)
{
// 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 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
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;
}
template <typename T>
int CreateAclTensorNCHW(
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
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_NCHW, shape.data(), shape.size(),
*deviceAddr);
return 0;
}
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 = Init(deviceId, &stream);
// Handle the check as required.
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> selfShape = {1, 2, 1, 4};
std::vector<int64_t> meanShape = {2};
std::vector<int64_t> varShape = {2};
std::vector<int64_t> weightShape = {2};
std::vector<int64_t> biasShape = {2};
std::vector<int64_t> outShape = {1, 2, 1, 4};
void* selfDeviceAddr = nullptr;
void* weightDeviceAddr = nullptr;
void* biasDeviceAddr = nullptr;
void* rMeanDeviceAddr = nullptr;
void* rVarDeviceAddr = nullptr;
void* outDeviceAddr = nullptr;
void* meanDeviceAddr = nullptr;
void* varDeviceAddr = nullptr;
aclTensor* self = nullptr;
aclTensor* weight = nullptr;
aclTensor* bias = nullptr;
aclTensor* out = nullptr;
aclTensor* mean = nullptr;
aclTensor* var = nullptr;
std::vector<int32_t> selfHostData = {0, 1, 2, 3, 4, 5, 6, 7};
std::vector<float> weightHostData = {1, 1};
std::vector<float> biasHostData = {0, 0};
std::vector<float> meanHostData = {0, 0};
std::vector<float> varHostData = {1, 1};
std::vector<int32_t> outHostData(8, 0);
float inputScaleValue = 1.0f;
int32_t inputZeroPointValue = 1;
float outputScaleValue = 2.0f;
int32_t outputZeroPointValue = 1;
double eps = 1e-5;
// Create the self aclTensor.
ret = CreateAclTensorNCHW(selfHostData, selfShape, &selfDeviceAddr, aclDataType::ACL_INT32, &self);
CHECK_RET(ret == ACL_SUCCESS, return ret);
// Create the mean aclTensor.
ret = CreateAclTensor(meanHostData, meanShape, &meanDeviceAddr, aclDataType::ACL_FLOAT, &mean);
CHECK_RET(ret == ACL_SUCCESS, return ret);
// Create the var aclTensor.
ret = CreateAclTensor(varHostData, varShape, &varDeviceAddr, aclDataType::ACL_FLOAT, &var);
CHECK_RET(ret == ACL_SUCCESS, return ret);
// Create the weight aclTensor.
ret = CreateAclTensor(weightHostData, weightShape, &weightDeviceAddr, aclDataType::ACL_FLOAT, &weight);
CHECK_RET(ret == ACL_SUCCESS, return ret);
// Create the bias aclTensor.
ret = CreateAclTensor(biasHostData, biasShape, &biasDeviceAddr, aclDataType::ACL_FLOAT, &bias);
CHECK_RET(ret == ACL_SUCCESS, return ret);
// Create the out aclTensor.
ret = CreateAclTensorNCHW(outHostData, outShape, &outDeviceAddr, aclDataType::ACL_INT32, &out);
CHECK_RET(ret == ACL_SUCCESS, return ret);
// Create the inputScale aclScalar.
aclScalar* inputScale = aclCreateScalar(&inputScaleValue, aclDataType::ACL_FLOAT);
// Create the inputZeroPoint aclScalar.
aclScalar* inputZeroPoint = aclCreateScalar(&inputZeroPointValue, aclDataType::ACL_INT32);
// Create outputScale aclScalar.
aclScalar* outputScale = aclCreateScalar(&outputScaleValue, aclDataType::ACL_FLOAT);
// Create outputZeroPoint aclScalar.
aclScalar* outputZeroPoint = aclCreateScalar(&outputZeroPointValue, aclDataType::ACL_INT32);
uint64_t workspaceSize = 0;
aclOpExecutor* executor;
// Example of calling the aclnnQuantizedBatchNorm API.
// 3. Call the CANN operator library API. Replace the API name with the actual one.
// Call the first-phase API of aclnnQuantizedBatchNorm.
ret = aclnnQuantizedBatchNormGetWorkspaceSize(
self, mean, var, inputScale, inputZeroPoint, outputScale, outputZeroPoint, weight, bias, eps, out,
&workspaceSize, &executor);
CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclnnQuantizedBatchNormGetWorkspaceSize failed. ERROR: %d\n", ret);
return ret);
// Allocate device memory based on the workspace size 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 aclnnQuantizedBatchNorm.
ret = aclnnQuantizedBatchNorm(workspaceAddr, workspaceSize, executor, stream);
CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclnnQuantizedBatchNorm 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 and copy the result from device memory to the host. Modify the code based on the actual API definition.
auto size = GetShapeSize(outShape);
printf("size is %ld", size);
std::vector<int32_t> resultData(size, 0);
ret = aclrtMemcpy(
resultData.data(), resultData.size() * sizeof(resultData[0]), outDeviceAddr, 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 ret);
for (int64_t i = 0; i < size; i++) {
LOG_PRINT("result[%ld] is: %d\n", i, resultData[i]);
}
// 6. Release aclTensor and aclScalar. Modify the code based on the actual API definition.
aclDestroyTensor(self);
aclDestroyTensor(weight);
aclDestroyTensor(bias);
aclDestroyTensor(mean);
aclDestroyTensor(var);
aclDestroyTensor(out);
aclDestroyScalar(inputScale);
aclDestroyScalar(inputZeroPoint);
aclDestroyScalar(outputScale);
aclDestroyScalar(outputZeroPoint);
// 7. Release the device resources. Modify it according to the specific API definition.
aclrtFree(selfDeviceAddr);
aclrtFree(meanDeviceAddr);
aclrtFree(varDeviceAddr);
aclrtFree(weightDeviceAddr);
aclrtFree(biasDeviceAddr);
aclrtFree(outDeviceAddr);
if (workspaceSize > 0) {
aclrtFree(workspaceAddr);
}
aclrtDestroyStream(stream);
aclrtResetDevice(deviceId);
aclFinalize();
return 0;
}