aclnnAddLayerNormQuant
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: The LayerNorm operator is a commonly used normalization operation in large models. The AddLayerNormQuant operator fuses the Add operator before LayerNorm and the LayerNorm normalized output to one or two downstream quantization operators, reducing data movement. The downstream quantization operator of LayerNorm can be a Quantize, AscendQuantV2, or DynamicQuant operator. The specific quantization operator type is determined by the attr parameters divMode and quantMode. When there are two downstream quantization operators, the operator types, input/output dtype combinations, and optional input combinations of the two quantization operators must be completely identical.
- Formula:
When quantMode is set to "static", the outputs outScales1Out and outScales2Out are meaningless. Depending on the divMode input, the fused quantization operator may be Quantize or AscendQuantV2:
When divMode is set to true, the fused quantization operator is Quantize, with the following formula:
When divMode is set to false, the fused quantization operator is AscendQuantV2, and the formula is as follows:
When quantMode is set to "dynamic", the inputs zeroPoints1Optional and zeroPoints2Optional have no practical significance. The fused quantization operator is DynamicQuant, and divMode is ineffective in this case:
If neither scales1Optional nor scales2Optional is provided, the outputs y2Out and scale2Out have no practical significance and can be ignored. The formula is as follows:
If only scales1Optional is input, the outputs y2Out and scale2Out are meaningless and can be ignored. The corresponding formula is as follows:
If both scales1Optional and scales2Optional are present, y2Out and scale2Out are valid. The corresponding formula is as follows:
where row_max denotes finding the maximum value of each row.
Function Prototype
Each operator is split into a two-phase API. Call the aclnnAddLayerNormQuantGetWorkspaceSize API first to obtain the input parameters and the workspace size required for the computing process, and then call the aclnnAddLayerNormQuant API to perform the computation.
aclnnStatus aclnnAddLayerNormQuantGetWorkspaceSize(
const aclTensor *x1,
const aclTensor *x2,
const aclTensor *gamma,
const aclTensor *beta,
const aclTensor *biasOptional,
const aclTensor *scales1Optional,
const aclTensor *scales2Optional,
const aclTensor *zeroPoints1Optional,
const aclTensor *zeroPoints2Optional,
const char *quantMode,
double epsilon,
bool additionalOutput,
bool divMode,
aclTensor *y1Out,
aclTensor *y2Out,
aclTensor *xOut,
aclTensor *outScales1Out,
aclTensor *outScales2Out,
uint64_t *workspaceSize,
aclOpExecutor **executor)aclnnStatus aclnnAddLayerNormQuant(
void *workspace,
uint64_t workspaceSize,
aclOpExecutor *executor,
aclrtStream stream)aclnnAddLayerNormQuantGetWorkspaceSize
Parameters
Parameter Input/Output Description Instruction Data Type Data Format Dimension (shape) Non-contiguous Tensor x1 (aclTensor*) Input Input of the addition computation in AddLayerNorm. The operator computes x1 + x2 + biasOptional and performs layer normalization on the result. Corresponds to `x1` in the formula. - Empty tensors are supported.
- When quantMode = "static", 1-8 dimensions are supported, with data types of FLOAT32, FLOAT16, and BFLOAT16.
- When quantMode = "dynamic", 2-8 dimensions are supported, with data types of FLOAT16 and BFLOAT16.
FLOAT32, FLOAT16, BFLOAT16 ND 1-8 √ x2 (aclTensor*) Input Input of the addition computation in AddLayerNorm. The operator computes x1 + x2 + biasOptional and performs layer normalization on the result. Corresponds to `x2` in the formula. - Supports empty tensors.
- When quantMode = "static", shapes with 1-8 dimensions are supported, and the supported data types are FLOAT32, FLOAT16, and BFLOAT16.
- When quantMode = "dynamic", shapes with 2-8 dimensions are supported, and the supported data types are FLOAT16 and BFLOAT16.
- The shape must be the same as that of `x1`.
FLOAT32, FLOAT16, BFLOAT16 ND 1-8 √ gamma (aclTensor*) Input Indicates the gamma parameter in layer normalization. Corresponds to `gamma` in the Formula. - Supports empty tensors.
- When quantMode = "static", shapes with 1-8 dimensions are supported, and the supported data types are FLOAT32, FLOAT16, and BFLOAT16.
- When quantMode = "dynamic", shapes with 2-8 dimensions are supported, and the supported data types are FLOAT16 and BFLOAT16.
- The dimensions must be the same as the last few dimensions of `x1`/`x2`.
FLOAT32, FLOAT16, BFLOAT16 ND 1-8 √ beta(aclTensor*) Input Corresponds to beta in the LayerNorm formula, representing the beta parameter in layer normalization. Corresponds to `beta` in the formula. - Supports empty tensors.
- When quantMode = "static", the supported data types are FLOAT32, FLOAT16, and BFLOAT16.
- When quantMode = "dynamic", the shape must have 2-8 dimensions and the supported data types are FLOAT16 and BFLOAT16.
- The shape must be the same as that of `gamma`.
FLOAT32, FLOAT16, BFLOAT16 ND 1-8 √ biasOptional (aclTensor*) Input Optional input parameter. You can pass in an aclTensor that meets the following constraints, or use nullptr as a placeholder to indicate that this optional input does not exist. It represents the input of the addition computation in AddLayerNorm. The operator computes x1 + x2 + biasOptional and then performs layer normalization on the result. Corresponds to `biasOptional` in the corresponding formula. - Supports empty tensors.
- When quantMode = "static", the shape supports 1-8 dimensions, and the data types support FLOAT32, FLOAT16, and BFLOAT16.
- When quantMode = "dynamic", the shape supports 2-8 dimensions, and the data types support FLOAT16 and BFLOAT16.
- The shape can be the same as that of `gamma`/`beta` or `x1`/`x2`.
FLOAT32, FLOAT16, BFLOAT16 ND 1-8 √ scales1Optional (aclTensor*) Input Optional input, indicating the scale/smooth input of the first quantization operator to be fused. Corresponding formula: `scales1Optional`. - Empty tensors are supported.
- When quantMode = "static", supported data types are FLOAT32, FLOAT16, and BFLOAT16.
- When quantMode = "dynamic", supported data types are FLOAT16 and BFLOAT16.
- The shape is the same as that of `gamma`.
- If the optional input is passed in, see Constraints for the value constraint.
FLOAT32, FLOAT16, BFLOAT16 ND 1-8 √ scales2Optional(aclTensor*) Input Optional input, indicating the scale/smooth input of the second quantization operator to be fused. Corresponding formula: `scales2Optional`. - Supports empty tensors.
- When quantMode = "static", the supported data types are FLOAT32, FLOAT16, and BFLOAT16.
- When quantMode = "dynamic", the supported data types are FLOAT16 and BFLOAT16.
- The shape is the same as `gamma`.
- If the optional input parameter is passed in, see Constraints for value constraints.
FLOAT32, FLOAT16, BFLOAT16 ND 1-8 √ zeroPoints1Optional (aclTensor*) Input Optional input. Indicates the zeroPoints input of the first fused quantization operator. Valid only when quantMode = "static". Corresponds to `zeroPoints1Optional` in the formula. - Supports empty tensors.
- When quantMode = "static", the supported data types are FLOAT32, FLOAT16, and BFLOAT16.
- When quantMode = "dynamic", the supported data types are FLOAT16 and BFLOAT16.
- The shape is the same as `gamma`.
- If the optional input parameter is passed in, see Constraints for value constraints.
FLOAT32, FLOAT16, BFLOAT16 ND 1-8 √ zeroPoints2Optional (aclTensor*) Input Optional input, indicating the zeroPoints input of the second quantization operator to be fused. Valid only when quantMode = "static". Corresponds to `zeroPoints2Optional` in the formula. - Supports empty tensors.
- When quantMode = "static", data types FLOAT32, FLOAT16, and BFLOAT16 are supported.
- When quantMode = "dynamic", data types FLOAT16 and BFLOAT16 are supported.
- The shape is the same as that of `gamma`.
- When the optional input is passed, see Constraints for the value constraints.
FLOAT32, FLOAT16, BFLOAT16 ND 1-8 √ quantMode (char*) Input Quantization mode, used to determine whether the fused operator fuses a static or dynamic quantization operator. It corresponds to `quantMode` in the formula description. The value can be "static" or "dynamic". - - - - - epsilon (double) Input Represents the epsilon of the corresponding LayerNorm, that is, the value added to the denominator to ensure numerical stability. It corresponds to `epsilon` in the corresponding formula. - - - - - additionalOutput (bool) Input Specifies whether to enable the output of x=x1+x2+biasOptional. - - - - - divMode (bool) Input Valid when quantMode = "static". Indicates whether scale is processed by multiplication or division in static quantization. When true is passed, the operator performs division on scale in the quantization computation. Corresponds to `divMode` in the corresponding formula. - - - - - y1Out (aclTensor*) Output Represents the result of quantizing the LayerNorm output y by the first quantization operator. Corresponds to `y1Out` in the formula. The shape must be the same as that of input x1/x2. INT8 ND 1-8 √ y2Out (aclTensor*) Output Indicates the result of quantizing the LayerNorm output y by the second quantization operator. Corresponds to `y2Out` in the formula. The shape must be the same as that of inputs x1/x2. INT8 ND 1-8 √ xOut (aclTensor*) Output Represents x, the result output of Add. Corresponds to `x` in the formula. - Supports empty tensors.
- When quantMode = "static", the supported data types are FLOAT32, FLOAT16, and BFLOAT16.
- When quantMode = "dynamic", the supported data types are FLOAT16 and BFLOAT16.
- The shape must be the same as that of inputs `x1`/`x2`.
FLOAT32, FLOAT16, BFLOAT16 ND 1-8 √ outScales1Out (aclTensor*) Output Represents the outScale result output of the first dynamic quantization computation, valid only when quantMode="dynamic". Corresponds to `outScales1Out` in the formula. Shape of input `x1` with the last dimension removed. FLOAT32 ND 0-7 √ outScales2Out (aclTensor*) Output Indicates the outScale output of the second dynamic quantization computation, valid only when quantMode="dynamic". Corresponds to `outScales2Out` in the formula. Shape of input `x1` with the last dimension removed. FLOAT32 ND 0-7 √ workspaceSize (uint64_t*) Output Returns the workspace size to be allocated on the Device side. - - - - - executor (aclOpExecutor**) Output Returns the op executor containing the operator computation flow. - - - - - 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 If the input parameter is a required input, output, or required attribute and is a null pointer. ACLNN_ERR_PARAM_INVALID 161002 The hardware platform is not within the supported product range. The value of quantMode is not "static" or "dynamic". The input data type combination is invalid. For valid data type combinations, see the constraints below. The shapes of x1, gamma, and outScales1Out meet the following conditions: - The dimensions of gamma are inconsistent with the last several dimensions of x1.
- When the quantization mode is dynamic, that is, the value of input quantMode is "dynamic", the number of dimensions of x1 is less than 2, or the number of dimensions of gamma is not 1.
- When the quantization mode is dynamic, that is, the value of input quantMode is "dynamic", the shape of outScales1Out is not the shape of input x1 with the last dimension removed.
The shapes of all input tensors satisfy the following relations: - 1. The shapes of x1, x2, xOut, and y1 are not the same; when the optional input scales2Optional exists, this condition is strictly tightened such that the shapes of x1, x2, xOut, y1, and y2 are not the same.
- The shapes of gamma and beta are not the same; when the optional inputs scales1Optional, scales2Optional, zeroPoints1Optional, and zeroPoints2Optional exist, their shapes are different from that of gamma.
- When biasOptional exists, its shape is different from both gamma and x1.
- When the quantization mode is dynamic, that is, the value of input quantMode is "dynamic", and the optional input scales2Optional exists at the same time, the shape of outScales1Out is different from that of outScales2Out.
The presence of the optional inputs (scales1Optional, scales2Optional, zeroPoints1Optional, zeroPoints2Optional) does not meet the specified combination requirements.
aclnnAddLayerNormQuant
Parameters
Parameter Input/Output Description workspace Input Workspace memory address allocated on the Device side. workspaceSize Input Size of the workspace allocated on the Device side, obtained by calling the first-phase API aclnnAddLayerNormQuantGetWorkspaceSize. executor Input Executor of the op, containing the operator computation flow. stream Input Stream for executing tasks. Return Value
aclnnStatus: return status code. For details, see aclnn Return Codes.
Constraints
Functional dimensions:
The optional inputs (scales1Optional, scales2Optional, zeroPoints1Optional, zeroPoints2Optional) support the following combinations:
scales1Optional scales2Optional zeroPoints1Optional zeroPoints2Optional quantMode Whether Valid T T T T "static" T T T T F "static" F T T F T "static" F T T F F "static" T T F T T "static" F T F T F "static" T T F F T "static" F T F F F "static" T F X X X "static" F T T F F "dynamic" T T F F F "dynamic" T F T F F "dynamic" F F F F F "dynamic" T X X T X "dynamic" F X X X T "dynamic" F Where:
Tindicates that the optional input is present and/indicates valid.Findicates that the optional input is absent, and/is invalid.Xindicates that either case is valid.
Data type support description:
- When
quantModeis "static":
- When
| Data Type Of x1 | x2 Data Type | Gamma Data Type | Beta Data Type | Data Type Of Bias | Data Type Of scale1 | Data Type Of scale2 | Data Type Of zeroPoints1 | Data Type Of zeroPoints2 | Data Type Of y1 | Data Type Of y2 | Data Type Of X | Data Type Of outScale1 | Data Type Of outScale2 | | ---------- | --------- | ------------- | ----------- | ------------ | -------------- | -------------- | ------------------ | ------------------- | --------- | ---------- | --------- | ----------------- | :--------------- | | FLOAT16 | FLOAT16 | FLOAT16 | FLOAT16 | FLOAT16 | FLOAT16 | FLOAT16 | FLOAT16 | FLOAT16 | INT8 | INT8 | FLOAT16 | FLOAT32 | FLOAT32 | | BFLOAT16 | BFLOAT16 | BFLOAT16 | BFLOAT16 | BFLOAT16 | BFLOAT16 | BFLOAT16 | BFLOAT16 | BFLOAT16 | INT8 | INT8 | BFLOAT16 | FLOAT32 | FLOAT32 | | FLOAT32 | FLOAT32 | FLOAT32 | FLOAT32 | FLOAT32 | FLOAT32 | FLOAT32 | FLOAT32 | FLOAT32 | INT8 | INT8 | FLOAT32 | FLOAT32 | FLOAT32 | | FLOAT16 | FLOAT16 | FLOAT16 | FLOAT16 | FLOAT16 | FLOAT32 | FLOAT32 | FLOAT32 | FLOAT32 | INT8 | INT8 | FLOAT16 | FLOAT32 | FLOAT32 | | BFLOAT16 | BFLOAT16 | BFLOAT16 | BFLOAT16 | BFLOAT16 | FLOAT32 | FLOAT32 | FLOAT32 | FLOAT32 | INT8 | INT8 | BFLOAT16 | FLOAT32 | FLOAT32 |
- When
quantModeis "dynamic":
| Data Type Of x1 | x2 Data Type | Gamma Data Type | Beta Data Type | Data Type Of Bias | Data Type Of scale1 | Data Type Of scale2 | Data Type Of zeroPoints1 | Data Type Of zeroPoints2 | Data Type Of y1 | Data Type Of y2 | Data Type Of X | Data Type Of outScale1 | Data Type Of outScale2 | | ---------- | --------- | ------------- | ----------- | ------------ | -------------- | -------------- | ------------------ | ------------------- | --------- | ---------- | --------- | ----------------- | :--------------- | | FLOAT16 | FLOAT16 | FLOAT16 | FLOAT16 | FLOAT16 | FLOAT16 | FLOAT16 | FLOAT16 | FLOAT16 | INT8 | INT8 | FLOAT16 | FLOAT32 | FLOAT32 | | BFLOAT16 | BFLOAT16 | BFLOAT16 | BFLOAT16 | BFLOAT16 | BFLOAT16 | BFLOAT16 | BFLOAT16 | BFLOAT16 | INT8 | INT8 | BFLOAT16 | FLOAT32 | FLOAT32 |
- Deterministic computation:
- aclnnAddLayerNormQuant defaults to a deterministic implementation.
Example
The following sample code is for reference only. For details about the compilation and execution process, see Compile and Run Samples.
#include <iostream>
#include <vector>
#include "acl/acl.h"
#include "aclnnop/aclnn_add_layer_norm_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) {
// 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); aclFinalize(); return ret);
ret = aclrtCreateStream(stream);
CHECK_RET(ret == ACL_SUCCESS,
LOG_PRINT("aclrtCreateStream failed. ERROR: %d\n", ret);
aclrtResetDevice(deviceId); aclFinalize(); 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 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. 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 check as required.
CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("Init acl failed. ERROR: %d\n", ret); return ret);
// 2. Construct the inputs and outputs. They must be constructed as required by the API. This example calls the API once without the optional input bias and once with bias.
float eps = 1e-6;
bool additionalOut = true;
bool divMode = true;
const char* quantMode = "static";
std::vector<int64_t> xShape = {8, 64};
std::vector<int64_t> gammaShape = {64};
std::vector<int64_t> reduceShape = {8,};
void *x1DeviceAddr = nullptr;
void *x2DeviceAddr = nullptr;
void *betaDeviceAddr = nullptr;
void *gammaDeviceAddr = nullptr;
void *biasDeviceAddr = nullptr;
void *s1DeviceAddr = nullptr;
void *s2DeviceAddr = nullptr;
void *z1DeviceAddr = nullptr;
void *z2DeviceAddr = nullptr;
// Used for the Device address of the output without bias.
void *y1DeviceAddr = nullptr;
void *y2DeviceAddr = nullptr;
void *xDeviceAddr = nullptr;
void *outScales1DeviceAddr = nullptr;
void *outScales2DeviceAddr = nullptr;
aclTensor *x1 = nullptr;
aclTensor *x2 = nullptr;
aclTensor *beta = nullptr;
aclTensor *gamma = nullptr;
aclTensor *bias = nullptr;
aclTensor *s1 = nullptr;
aclTensor *s2 = nullptr;
aclTensor *z1 = nullptr;
aclTensor *z2 = nullptr;
// Used for the aclTensor without bias.
aclTensor *y1 = nullptr;
aclTensor *y2 = nullptr;
aclTensor *x = nullptr;
aclTensor *outScales1 = nullptr;
aclTensor *outScales2 = nullptr;
int64_t xShapeSize = GetShapeSize(xShape);
int64_t gammaShapeSize = GetShapeSize(gammaShape);
int64_t reduceShapeSize = GetShapeSize(reduceShape);
std::vector<float> x1HostData(xShapeSize, 0x3C00);
std::vector<float> x2HostData(xShapeSize, 0x3C00);
std::vector<float> gammaHostData(gammaShapeSize, 0x3C00);
std::vector<float> betaHostData(gammaShapeSize, 0x3C00);
std::vector<float> biasHostData(gammaShapeSize, 0x3C00);
std::vector<float> s1HostData(gammaShapeSize, 0x3C00);
std::vector<float> s2HostData(gammaShapeSize, 0x3C00);
std::vector<float> z1HostData(gammaShapeSize, 0x3C00);
std::vector<float> z2HostData(gammaShapeSize, 0x3C00);
// Used for the HostData without bias.
std::vector<int8_t> y1HostData(xShapeSize, 0);
std::vector<int8_t> y2HostData(xShapeSize, 0);
std::vector<float> xHostData(xShapeSize, 0);
std::vector<float> outScales1HostData(reduceShapeSize, 0);
std::vector<float> outScales2HostData(reduceShapeSize, 0);
// Create the self aclTensor.
ret = CreateAclTensor(x1HostData, xShape, &x1DeviceAddr, aclDataType::ACL_FLOAT, &x1);
CHECK_RET(ret == ACL_SUCCESS, return ret);
ret = CreateAclTensor(x2HostData, xShape, &x2DeviceAddr, aclDataType::ACL_FLOAT, &x2);
CHECK_RET(ret == ACL_SUCCESS, return ret);
ret = CreateAclTensor(gammaHostData, gammaShape, &gammaDeviceAddr, aclDataType::ACL_FLOAT, &gamma);
CHECK_RET(ret == ACL_SUCCESS, return ret);
ret = CreateAclTensor(betaHostData, gammaShape, &betaDeviceAddr, aclDataType::ACL_FLOAT, &beta);
CHECK_RET(ret == ACL_SUCCESS, return ret);
ret = CreateAclTensor(biasHostData, gammaShape, &biasDeviceAddr, aclDataType::ACL_FLOAT, &bias);
CHECK_RET(ret == ACL_SUCCESS, return ret);
ret = CreateAclTensor(s1HostData, gammaShape, &s1DeviceAddr, aclDataType::ACL_FLOAT, &s1);
CHECK_RET(ret == ACL_SUCCESS, return ret);
ret = CreateAclTensor(s2HostData, gammaShape, &s2DeviceAddr, aclDataType::ACL_FLOAT, &s2);
CHECK_RET(ret == ACL_SUCCESS, return ret);
ret = CreateAclTensor(z1HostData, gammaShape, &z1DeviceAddr, aclDataType::ACL_FLOAT, &z1);
CHECK_RET(ret == ACL_SUCCESS, return ret);
ret = CreateAclTensor(z2HostData, gammaShape, &z2DeviceAddr, aclDataType::ACL_FLOAT, &z2);
CHECK_RET(ret == ACL_SUCCESS, return ret);
// Create the aclTensor without bias.
ret = CreateAclTensor(y1HostData, xShape, &y1DeviceAddr, aclDataType::ACL_INT8, &y1);
CHECK_RET(ret == ACL_SUCCESS, return ret);
ret = CreateAclTensor(y2HostData, xShape, &y2DeviceAddr, aclDataType::ACL_INT8, &y2);
CHECK_RET(ret == ACL_SUCCESS, return ret);
ret = CreateAclTensor(xHostData, xShape, &xDeviceAddr, aclDataType::ACL_FLOAT, &x);
CHECK_RET(ret == ACL_SUCCESS, return ret);
ret = CreateAclTensor(outScales1HostData, reduceShape, &outScales1DeviceAddr, aclDataType::ACL_FLOAT, &outScales1);
CHECK_RET(ret == ACL_SUCCESS, return ret);
ret = CreateAclTensor(outScales2HostData, reduceShape, &outScales2DeviceAddr, aclDataType::ACL_FLOAT, &outScales2);
CHECK_RET(ret == ACL_SUCCESS, return ret);
// Example of calling the aclnnAddLayerNormQuant API, including one call with bias and one without bias.
// 3. Call the CANN operator library API. Replace it with the specific API name.
// 3.1 Example without the optional bias input.
// Call the first-phase API of aclnnAddLayerNormQuant.
uint64_t workspaceSize = 0;
aclOpExecutor *executor;
ret = aclnnAddLayerNormQuantGetWorkspaceSize(x1, x2, gamma, beta, bias, s1, s2, nullptr, nullptr, quantMode, eps, additionalOut, divMode, y1, y2, x, outScales1, outScales2, &workspaceSize, &executor);
CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclnnAddLayerNormQuantGetWorkspaceSize failed. ERROR: %d\n", ret); return ret);
// Allocate device memory according to 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 second-phase API aclnnAddLayerNormQuant.
ret = aclnnAddLayerNormQuant(workspaceAddr, workspaceSize, executor, stream);
CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclnnAddLayerNormQuant failed. ERROR: %d\n", ret); return ret);
// 4. (boilerplate) Wait for the task execution to finish 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 the Device side to the Host side. Modify it as required by the specific API definition.
auto y1Size = GetShapeSize(xShape);
std::vector<int8_t> resultDataY1(y1Size, 0);
ret = aclrtMemcpy(resultDataY1.data(), resultDataY1.size() * sizeof(resultDataY1[0]), y1DeviceAddr, y1Size * sizeof(resultDataY1[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);
LOG_PRINT("==== AddLayerNormQuant y1 output");
for (int64_t i = 0; i < y1Size; i++) {
LOG_PRINT("result[%ld] is: %d\n", i, resultDataY1[i]);
}
auto y2Size = GetShapeSize(xShape);
std::vector<int8_t> resultDataY2(y2Size, 0);
ret = aclrtMemcpy(resultDataY2.data(), resultDataY2.size() * sizeof(resultDataY2[0]), y2DeviceAddr, y2Size * sizeof(resultDataY2[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);
LOG_PRINT("==== AddLayerNormQuant y2 output");
for (int64_t i = 0; i < y2Size; i++) {
LOG_PRINT("result[%ld] is: %d\n", i, resultDataY2[i]);
}
auto xSize = GetShapeSize(xShape);
std::vector<float> resultDataX(xSize, 0);
ret = aclrtMemcpy(resultDataX.data(), resultDataX.size() * sizeof(resultDataX[0]), xDeviceAddr, xSize * sizeof(resultDataX[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);
LOG_PRINT("==== AddLayerNormQuant x output");
for (int64_t i = 0; i < xSize; i++) {
LOG_PRINT("result[%ld] is: %f\n", i, resultDataX[i]);
}
auto outScale1Size = GetShapeSize(reduceShape);
std::vector<float> resultDataOutScale1(outScale1Size, 0);
ret = aclrtMemcpy(resultDataOutScale1.data(), resultDataOutScale1.size() * sizeof(resultDataOutScale1[0]), outScales1DeviceAddr, outScale1Size * sizeof(resultDataOutScale1[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);
LOG_PRINT("==== AddLayerNormQuant outScale1 output");
for (int64_t i = 0; i < outScale1Size; i++) {
LOG_PRINT("result[%ld] is: %f\n", i, resultDataOutScale1[i]);
}
auto outScale2Size = GetShapeSize(reduceShape);
std::vector<float> resultDataOutScale2(outScale2Size, 0);
ret = aclrtMemcpy(resultDataOutScale2.data(), resultDataOutScale2.size() * sizeof(resultDataOutScale2[0]), outScales2DeviceAddr, outScale2Size * sizeof(resultDataOutScale2[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);
LOG_PRINT("==== AddLayerNormQuant outScale2 output");
for (int64_t i = 0; i < outScale2Size; i++) {
LOG_PRINT("result[%ld] is: %f\n", i, resultDataOutScale2[i]);
}
// 6. Release aclTensor and aclScalar. Modify it as required by the specific API definition.
aclDestroyTensor(x1);
aclDestroyTensor(x2);
aclDestroyTensor(beta);
aclDestroyTensor(gamma);
aclDestroyTensor(bias);
aclDestroyTensor(s1);
aclDestroyTensor(s2);
aclDestroyTensor(z1);
aclDestroyTensor(z2);
aclDestroyTensor(y1);
aclDestroyTensor(y2);
aclDestroyTensor(x);
aclDestroyTensor(outScales1);
aclDestroyTensor(outScales2);
// 7. Release Device resources. Modify it as required by the specific API definition.
aclrtFree(x1DeviceAddr);
aclrtFree(x2DeviceAddr);
aclrtFree(gammaDeviceAddr);
aclrtFree(betaDeviceAddr);
aclrtFree(biasDeviceAddr);
aclrtFree(s1DeviceAddr);
aclrtFree(s2DeviceAddr);
aclrtFree(z1DeviceAddr);
aclrtFree(z2DeviceAddr);
aclrtFree(y1DeviceAddr);
aclrtFree(y2DeviceAddr);
aclrtFree(xDeviceAddr);
aclrtFree(outScales1DeviceAddr);
aclrtFree(outScales2DeviceAddr);
if (workspaceSize > 0) {
aclrtFree(workspaceAddr);
}
aclrtDestroyStream(stream);
aclrtResetDevice(deviceId);
aclFinalize();
return 0;
}