aclnnDeepNorm
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 : Supported
Atlas training products : Not supported
Function
- Description: Performs deep normalization on the elements of the input tensor x by computing its mean and standard deviation, normalizing each element to an output tensor with zero mean and unit variance.
- Formula:
Function Prototype
Each operator uses a two-phase API. Call the "aclnnDeepNormGetWorkspaceSize" API first to obtain the workspace size required for computation and the executor containing the computation process of the operator, and then call the "aclnnDeepNorm" API to perform the computation.
aclnnStatus aclnnDeepNormGetWorkspaceSize(
const aclTensor *x,
const aclTensor *gx,
const aclTensor *beta,
const aclTensor *gamma,
double alpha,
double epsilon,
const aclTensor *meanOut,
const aclTensor *rstdOut,
const aclTensor *yOut,
uint64_t *workspaceSize,
aclOpExecutor **executor)aclnnStatus aclnnDeepNorm(
void *workspace,
uint64_t workspaceSize,
aclOpExecutor *executor,
aclrtStream stream)aclnnDeepNormGetWorkspaceSize
Parameters
Parameter Input/Output Description Instruction Data Type Data Format Dimension (shape) Non-contiguous Tensor x (aclTensor*) Input Input data, usually the output of an intermediate layer of a neural network. It corresponds to `x` in the formula. Supports empty tensors. FLOAT32, FLOAT16, BFLOAT16 ND 2-8 √ gx (aclTensor*) Input Gradient of the input data, used for backward propagation. It corresponds to `gx` in the formula. - Support empty tensor.
- The data type is the same as that of `x`.
- The shape dimensions are the same as those of input `x`.
FLOAT32, FLOAT16, BFLOAT16 ND 2-8 √ beta (aclTensor*) Input Bias parameter, used to adjust the normalized output, corresponding to `beta` in the formula. - Support empty tensor.
- The data type is the same as that of `x`.
- The shape dimensions are the same as the trailing dimensions of input `x`, which indicate the dimensions to be normalized.
FLOAT32, FLOAT16, BFLOAT16 ND 1-7 √ gamma (aclTensor*) Input Scaling parameter used to adjust the normalized output. Corresponds to `gamma` in the formula. - Supports empty tensor.
- Data type is the same as that of `x`.
- Shape dimensions are the same as the trailing dimensions of input `x`, where the trailing dimensions indicate the dimensions to be normalized.
FLOAT32, FLOAT16, BFLOAT16 ND 1-7 √ alpha (double) Input Represents the weight parameter, used to adjust the weight of input data, corresponding to `alpha` in the formula. - - - - - epsilon (double) Input Value added to the variance to avoid division by zero. Corresponds to `epsilon` in the corresponding formula. - - - - - meanOut (aclTensor*) Output Mean of the computation output, used for normalization. Corresponds to `mean` in the corresponding formula. FLOAT32 ND 2-8 × rstdOut (aclTensor*) Output Reciprocal of the standard deviation of the computed output, used for normalization. Corresponds to `rstd` in the formula. FLOAT32 ND 2-8 × yOut (aclTensor*) Output Represents the normalized output data, corresponding to `y` in the formula. - Supports empty tensor.
- The data type is the same as that of `x`.
- The shape dimension is the same as that of input `x`.
FLOAT32, FLOAT16, BFLOAT16 ND 2-8 × workspaceSize (uint64_t*) Output Returns the workspace size to be requested on the Device. - - - - - executor (aclOpExecutor**) Output Returns the op executor containing 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 passed-in parameter is a mandatory input, output, or attribute and is a null pointer. ACLNN_ERR_PARAM_INVALID 161002 The data type of the input or output is not within the supported range. The shapes of the input and output do not match or are not within the supported dimension range. The data types of the input and output do not meet the constraints in the parameter description.
aclnnDeepNorm
Parameters
Parameter Input/Output Description workspace Input Workspace memory address allocated on the Device. workspaceSize Input Workspace size allocated on the Device, obtained through the first-phase API aclnnDeepNormGetWorkspaceSize. executor Input Executor of the op, containing the operator computation flow. stream Input Specifies the stream for executing tasks. Return Value
aclnnStatus: return status code. For details, see aclnn Error Codes.
Constraints
Functional dimensions:
Supported data types:
Atlas inference products : x, gx, beta, gamma, and yOut support FLOAT32 and FLOAT16.
Ascend 950PR&950DT products ,Atlas A2 products , andAtlas A3 products : x, gx, beta, gamma, and yOut support FLOAT32, FLOAT16, and BFLOAT16.
- rstdOut and meanOut support: FLOAT32.
Data format support: ND.
Unsupported type description:
DOUBLE: The instruction does not support DOUBLE.
Boundary value scenario description:
- When the input is Inf, the output is Inf.
- When the input is NaN, the output is NaN.
Deterministic computation:
- aclnnDeepNorm 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_deep_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: 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 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 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) Device/stream initialization. For details, see the acl API manual.
// Fill in 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. Customize the construction based on the API definition.
float alpha = 0.3;
float eps = 1e-6;
std::vector<int64_t> xShape = {3, 1, 4};
std::vector<int64_t> gxShape = {3, 1, 4};
std::vector<int64_t> betaShape = {4};
std::vector<int64_t> gammaShape = {4};
std::vector<int64_t> outputMeanShape = {3, 1, 1};
std::vector<int64_t> outputRstdShape = {3, 1, 1};
std::vector<int64_t> outputYShape = {3, 1, 4};
void* xDeviceAddr = nullptr;
void* gxDeviceAddr = nullptr;
void* betaDeviceAddr = nullptr;
void* gammaDeviceAddr = nullptr;
void* outputMeanDeviceAddr = nullptr;
void* outputRstdDeviceAddr = nullptr;
void* outputYDeviceAddr = nullptr;
aclTensor* x = nullptr;
aclTensor* gx = nullptr;
aclTensor* beta = nullptr;
aclTensor* gamma = nullptr;
aclTensor* outputMean = nullptr;
aclTensor* outputRstd = nullptr;
aclTensor* outputY = nullptr;
std::vector<float> xHostData = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12};
std::vector<float> gxHostData = {2, 2, 2, 4, 4, 4, 6, 6, 6, 8, 8, 8};
std::vector<float> betaHostData = {0, 1, 2, 3};
std::vector<float> gammaHostData = {0, 1, 2, 3};
std::vector<float> outputMeanHostData = {0, 1, 2};
std::vector<float> outputRstdHostData = {0, 1, 2};
std::vector<float> outputYHostData = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12};
// Create the self aclTensor.
ret = CreateAclTensor(xHostData, xShape, &xDeviceAddr, aclDataType::ACL_FLOAT, &x);
CHECK_RET(ret == ACL_SUCCESS, return ret);
ret = CreateAclTensor(gxHostData, gxShape, &gxDeviceAddr, aclDataType::ACL_FLOAT, &gx);
CHECK_RET(ret == ACL_SUCCESS, return ret);
ret = CreateAclTensor(betaHostData, betaShape, &betaDeviceAddr, aclDataType::ACL_FLOAT, &beta);
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(
outputMeanHostData, outputMeanShape, &outputMeanDeviceAddr, aclDataType::ACL_FLOAT, &outputMean);
CHECK_RET(ret == ACL_SUCCESS, return ret);
ret = CreateAclTensor(
outputRstdHostData, outputRstdShape, &outputRstdDeviceAddr, aclDataType::ACL_FLOAT, &outputRstd);
CHECK_RET(ret == ACL_SUCCESS, return ret);
ret = CreateAclTensor(outputYHostData, outputYShape, &outputYDeviceAddr, aclDataType::ACL_FLOAT, &outputY);
CHECK_RET(ret == ACL_SUCCESS, return ret);
uint64_t workspaceSize = 0;
aclOpExecutor* executor;
// Example of calling the aclnnDeepNorm API.
// 3. Call the CANN operator library API. Change it to the specific API name.
// Call the first-phase API of aclnnDeepNorm.
LOG_PRINT("\nUse aclnnDeepNorm Port.");
ret = aclnnDeepNormGetWorkspaceSize(
x, gx, beta, gamma, alpha, eps, outputMean, outputRstd, outputY, &workspaceSize, &executor);
CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclnnDeepNormGetWorkspaceSize 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 aclnnDeepNorm.
ret = aclnnDeepNorm(workspaceAddr, workspaceSize, executor, stream);
CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclnnDeepNorm 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 values and copy the result from the device to the host. Modify it based on the specific API definition.
auto outputMeanSize = GetShapeSize(outputMeanShape);
std::vector<float> resultDataMean(outputMeanSize, 0);
ret = aclrtMemcpy(
resultDataMean.data(), resultDataMean.size() * sizeof(resultDataMean[0]), outputMeanDeviceAddr,
outputMeanSize * sizeof(resultDataMean[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("== pdx output");
for (int64_t i = 0; i < outputMeanSize; i++) {
LOG_PRINT("result[%ld] is: %f\n", i, resultDataMean[i]);
}
auto outputRstdSize = GetShapeSize(outputRstdShape);
std::vector<float> resultDataRstd(outputRstdSize, 0);
ret = aclrtMemcpy(
resultDataRstd.data(), resultDataRstd.size() * sizeof(resultDataRstd[0]), outputRstdDeviceAddr,
outputRstdSize * sizeof(resultDataRstd[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("== pdx output");
for (int64_t i = 0; i < outputRstdSize; i++) {
LOG_PRINT("result[%ld] is: %f\n", i, resultDataRstd[i]);
}
auto outputYSize = GetShapeSize(outputYShape);
std::vector<float> resultDataY(outputYSize, 0);
ret = aclrtMemcpy(
resultDataY.data(), resultDataY.size() * sizeof(resultDataY[0]), outputYDeviceAddr,
outputYSize * sizeof(resultDataY[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("== pdx output");
for (int64_t i = 0; i < outputYSize; i++) {
LOG_PRINT("result[%ld] is: %f\n", i, resultDataY[i]);
}
// 6. Release the aclTensor and aclScalar. Modify it based on the specific API definition.
aclDestroyTensor(x);
aclDestroyTensor(gx);
aclDestroyTensor(beta);
aclDestroyTensor(gamma);
aclDestroyTensor(outputMean);
aclDestroyTensor(outputRstd);
aclDestroyTensor(outputY);
// 7. Release device resources. Modify it based on the specific API definition.
aclrtFree(xDeviceAddr);
aclrtFree(gxDeviceAddr);
aclrtFree(gammaDeviceAddr);
aclrtFree(betaDeviceAddr);
aclrtFree(outputMeanDeviceAddr);
aclrtFree(outputRstdDeviceAddr);
aclrtFree(outputYDeviceAddr);
if (workspaceSize > 0) {
aclrtFree(workspaceAddr);
}
aclrtDestroyStream(stream);
aclrtResetDevice(deviceId);
aclFinalize();
return 0;
}