aclnnDequantBias
Applicable Products
| Product | Supported |
|---|---|
| × | |
| √ | |
| √ | |
| × | |
| × | |
| × |
Function
Description: Performs dequantization on the input x, converting INT32 input data to FLOAT16/BFLOAT16 output.
Formula:
Function Prototype
Each operator is divided into a two-phase API. You must first call the "aclnnDequantBiasGetWorkspaceSize" API to obtain the input parameters and calculate the required workspace size based on the process, and then call the "aclnnDequantBias" API to perform the computation.
aclnnStatus aclnnDequantBiasGetWorkspaceSize(
const aclTensor *x,
const aclTensor *weightScale,
const aclTensor *activateScaleOptional,
const aclTensor *biasOptional,
int64_t outputDtype,
const aclTensor *out,
uint64_t *workspaceSize,
aclOpExecutor **executor)aclnnStatus aclnnDequantBias(
void *workspace,
uint64_t workspaceSize,
aclOpExecutor *executor,
aclrtStream stream)aclnnDequantBiasGetWorkspaceSize
Parameters:
Parameter Input/Output Description Instruction Data Type Data Format Dimension (Shape) Non-contiguous Tensor x (aclTensor*) Input Represents the input tensor of the dequantization operation, i.e., A in the formula. - Supports empty tensors.
- Shape: [M, N].
INT32 ND 2 × weightScale (aclTensor*) Input Represents the weight multiplied on the N dimension of the dequantization input, corresponding to weight_scale in the formula. - Supports empty tensors.
- The shape is [N], and the length is consistent with the N dimension length of x.
FLOAT, BFLOAT16 ND 1 × activateScaleOptional (aclTensor*) Input Represents the weight multiplied on the M dimension in the dequantization operation, corresponding to activate_scale in the formula. - Supports null tensor.
- Shape is [M], with the length consistent with the M dimension length of x.
FLOAT ND 1 × biasOptional (aclTensor*) Input Represents the weight added to the N dimension of the dequantization input, i.e., the input bias in the formula. - Supports empty tensors.
- The shape is [N], with the length consistent with the N dimension length of x.
FLOAT, BFLOAT16, FLOAT16, INT32 ND 1 × outputDtype (int64_t) Input Indicates the data type of the output out. - Supports empty tensors.
- The value is [1, 27]. A value of 1 indicates that the output type is FLOAT16, and a value of 27 indicates that the output type is BFLOAT16.
- When the weightScale data type is FLOAT, this parameter is set to 1.
- When the weightScale data type is BFLOAT16, this parameter is set to 27.
- - - - out (aclTensor*) Output Represents the output tensor of the dequantization operation, i.e., the output y in the formula. Shape: [M, N]. FLOAT16, BFLOAT16 ND 2 × workspaceSize (uint64_t*) Output Returns the workspace size to be allocated on the Device side. - - - - - executor (aclOpExecutor**) Output Returns the op executor, which contains the operator computation flow. - - - - - Return value
aclnnStatus: return code. For details, see aclnn Return Code.
The first-phase API performs input parameter validation and returns an error in the following scenarios:
| Return Code | Error Code | Description |
|---|---|---|
| ACLNN_ERR_PARAM_NULLPTR | 161001 | The tensor of input x, weightScale, or output out is a null pointer. |
| ACLNN_ERR_PARAM_INVALID | 161002 | The data type of input x, weightScale, or output out is not within the supported range. |
| ACLNN_ERR_INNER_TILING_ERROR | 561002 | The data type of input activateScaleOptional or biasOptional is not within the supported range. |
| The values of N and M in the input and output shapes do not meet the parameter requirements and constraints. |
aclnnDequantBias
Parameters:
Parameter Input/Output Description workspace Input Memory address of the workspace applied for on the Device side. workspaceSize Input Size of the workspace applied for on the Device side, obtained by the first-phase API aclnnDequantBiasGetWorkspaceSize. executor Input Operator executor, which contains the operator computation flow. stream Input Specifies the stream for task execution. Return value
aclnnStatus: return code. For details, see aclnn Return Code.
Constraints
Deterministic computation:
- aclnnDequantBias defaults to a deterministic implementation.
N and M in the shape of input and output parameters must be positive integers, and the value of M must be less than or equal to 25000.
Example
The following provides example code for reference only. For details about compilation and running, see Compile and Run Samples.
# include <iostream>
# include <vector>
# include "acl/acl.h"
# include "aclnnop/aclnn_dequant_bias.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("mean result[%ld] is: %d\n", i, resultData[i]);
}
}
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 apply for device 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 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 a 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;
}
int main() {
// 1. (Boilerplate) Initialize the device/stream. See the ACL API manual.
// Fill in the 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.
std::vector<int64_t> inputShape = {40, 256};
std::vector<int64_t> weightShape = {256};
std::vector<int64_t> activationShape = {40};
std::vector<int64_t> biasShape = {256};
std::vector<int16_t> inputHostData(40*256, 1);
std::vector<int32_t> weightHostData(256, 2);
std::vector<int32_t> activationHostData(40, 2);
std::vector<int32_t> biasHostData(256, 2);
void* inputDeviceAddr = nullptr;
void* weightDeviceAddr = nullptr;
void* activationDeviceAddr = nullptr;
void* biasDeviceAddr = nullptr;
aclTensor* input = nullptr;
aclTensor* weight = nullptr;
aclTensor* activation = nullptr;
aclTensor* bias = nullptr;
ret = CreateAclTensor(inputHostData, inputShape, &inputDeviceAddr, aclDataType::ACL_INT32, &input);
CHECK_RET(ret == ACL_SUCCESS, return ret);
ret = CreateAclTensor(weightHostData, weightShape, &weightDeviceAddr, aclDataType::ACL_FLOAT, &weight);
CHECK_RET(ret == ACL_SUCCESS, return ret);
ret = CreateAclTensor(activationHostData, activationShape, &activationDeviceAddr, aclDataType::ACL_FLOAT, &activation);
CHECK_RET(ret == ACL_SUCCESS, return ret);
ret = CreateAclTensor(biasHostData, biasShape, &biasDeviceAddr, aclDataType::ACL_FLOAT, &bias);
CHECK_RET(ret == ACL_SUCCESS, return ret);
std::vector<int64_t> yShape = {40,256};
std::vector<int16_t> yHostData(40*256, 9);
aclTensor* y = nullptr;
void* yDeviceAddr = nullptr;
ret = CreateAclTensor(yHostData, yShape, &yDeviceAddr, aclDataType::ACL_FLOAT16, &y);
CHECK_RET(ret == ACL_SUCCESS, return ret);
// 3. Call the CANN operator library API. Modify it to the specific API name.
uint64_t workspaceSize = 0;
aclOpExecutor* executor;
// Call the first-phase API of aclnnDequantBias.
ret = aclnnDequantBiasGetWorkspaceSize(input, weight, activation, bias,
true, y, &workspaceSize, &executor);
CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclnnDequantBiasGetWorkspaceSize failed. ERROR: %d\n", ret); return ret);
// Apply for 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 aclnnDequantBias.
ret = aclnnDequantBias(workspaceAddr, workspaceSize, executor, stream);
CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclnnDequantBias failed. ERROR: %d\n", ret); return ret);
// 4. (Boilerplate) Synchronously wait for 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 the device memory to the host memory. Modify based on the specific API definition.
PrintOutResult(yShape, &yDeviceAddr);
// 6. Release aclTensor and aclScalar. Modify based on the specific API definition.
aclDestroyTensor(input);
aclDestroyTensor(y);
// 7. Release device resources. Modify based on the specific API definition.
aclrtFree(inputDeviceAddr);
aclrtFree(yDeviceAddr);
if (workspaceSize > 0) {
aclrtFree(workspaceAddr);
}
aclrtDestroyStream(stream);
aclrtResetDevice(deviceId);
aclFinalize();
return 0;
}