aclnnDequantBias
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 x and converts the input INT32 data into FLOAT16/BFLOAT16 output.
- Formula:
Function Prototype
Each operator uses a two-phase API. Call the "aclnnDequantBiasGetWorkspaceSize" API first 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 Input tensor of the dequantization operation, which is A in the formula. - Supports empty tensors.
- The shape is [M, N].
INT32 ND 2 × weightScale (aclTensor*) Input Weight of the multiplication on the N dimension of the dequantization operation input, which is the input weight_scale in the formula. - Supports empty Tensor.
- Shape is [N], and the length is the same as the N dimension length of x.
FLOAT, BFLOAT16 ND 1 × activateScaleOptional (aclTensor*) Input Represents the weight of the multiplication on the M dimension of the dequantization operation input, that is, the input activate_scale in the formula. - Supports empty Tensor.
- Shape is [M], and the length is the same as the M dimension length of x.
FLOAT ND 1 × biasOptional (aclTensor*) Input Represents the weight of the addition on the N dimension of the dequantization operation input, that is, the input bias in the formula. - Supports empty tensors.
- The shape is [N], and the length is the same as the N dimension length of x.
FLOAT, BFLOAT16, FLOAT16, INT32 ND 1 × outputDtype (int64_t) Input Represents the data type of output out. - The value is [1, 27]. The value 1 indicates that the output type is FLOAT16, and the value 27 indicates that the output type is BFLOAT16.
- Set this parameter to 1 when the data type of weightScale is FLOAT.
- Set this parameter to 27 when the data type of weightScale is BFLOAT16.
- - - - out (aclTensor*) Output Represents the output Tensor of the dequantization operation, i.e., output y in the formula. The shape is [M, N]. FLOAT16, BFLOAT16 ND 2 × workspaceSize (uint64_t*) Output Returns the workspace size to be applied on the Device side. - - - - - executor (aclOpExecutor**) Output Returns the op executor containing the operator computation process. - - - - - Return Value
aclnnStatus: return status code. For details, see aclnn Return 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 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 shapes of inputs and outputs do not meet the parameter requirements and constraints. |
aclnnDequantBias
Parameters:
Parameter Input/Output Description workspace Input Workspace memory address applied on the Device side. workspaceSize Input Workspace size applied on the Device side, obtained by the first-phase API aclnnDequantBiasGetWorkspaceSize. executor Input Executor of the op, containing the operator computation flow. stream Input Stream for executing the task. Return Value
aclnnStatus: return status code. For details, see aclnn Return Codes.
Constraints
Deterministic computation:
- aclnnDequantBias defaults to a deterministic implementation.
N and M in the shapes 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 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_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<int16_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: 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 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 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 inputs and outputs. Custom construction is required 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<int32_t> inputHostData(40 * 256, 1);
std::vector<float> weightHostData(256, 2.0f);
std::vector<float> activationHostData(40, 2.0f);
std::vector<float> biasHostData(256, 2.0f);
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. Replace it with the specific API name.
uint64_t workspaceSize = 0;
aclOpExecutor* executor;
// Call the first-phase API of aclnnDequantBias.
ret = aclnnDequantBiasGetWorkspaceSize(input, weight, activation, bias,
1, y, &workspaceSize, &executor);
CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclnnDequantBiasGetWorkspaceSize 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 aclnnDequantBias.
ret = aclnnDequantBias(workspaceAddr, workspaceSize, executor, stream);
CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclnnDequantBias failed. ERROR: %d\n", ret); return ret);
// 4. (boilerplate) Wait for the task execution to complete synchronously.
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 host memory. Modify the code based on the specific API definition.
PrintOutResult(yShape, &yDeviceAddr);
// 6. Release aclTensor and aclScalar. Modify the code based on the specific API definition.
aclDestroyTensor(input);
aclDestroyTensor(y);
// 7. Release device resources. Modify the code based on the specific API definition.
aclrtFree(inputDeviceAddr);
aclrtFree(yDeviceAddr);
if (workspaceSize > 0) {
aclrtFree(workspaceAddr);
}
aclrtDestroyStream(stream);
aclrtResetDevice(deviceId);
aclFinalize();
return 0;
}