aclnnFastBatchNormBackward
Applicable Products
Ascend 950PR&950DT products : Not supported
Atlas A3 products : Not 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: backward propagation of aclnnBatchNorm (high-performance version). Computes the gradients of the input tensor to update model parameters during backward propagation.
Formula:
When training is true:
$$ gradInput = \frac{weight}{ n{\sqrt{saveVar + eps}} }(n * gradOut - \sum^m_{i=0}{gradOut} - \frac{x-saveMean}{ \sqrt{saveVar + eps} }\sum^m_{i=0}({gradOut} *\frac{x-saveMean}{ \sqrt{saveVar + eps} } )) $$$$ gradWeight = \sum^m_{i=0}[{gradOut} * (x - saveMean)] * \frac{1}{ \sqrt{saveVar + eps} } $$$$ gradBias = \sum^m_{i=0}{gradOut} $$When training is false:
$$ gradInput = gradOut * \frac{1}{ \sqrt{runningVar + eps} } * weight $$$$ gradWeight = \sum^m_{i=0}[{gradOut} * (x - runningMean)] * \frac{1}{ \sqrt{runningVar + eps} } $$$$ gradBias = \sum^m_{i=0}{gradOut} $$
Function Prototype
Each operator uses a two-phase API. Call the "aclnnFastBatchNormBackwardGetWorkspaceSize" API first to obtain the input parameters and calculate the required workspace size based on the process. Then call the "aclnnFastBatchNormBackward" API to perform the computation.
aclnnStatus aclnnFastBatchNormBackwardGetWorkspaceSize(
const aclTensor *gradOut,
const aclTensor *input,
const aclTensor *weight,
const aclTensor *runningMean,
const aclTensor *runningVar,
const aclTensor *saveMean,
const aclTensor *saveInvstd,
bool training,
double eps,
const aclBoolArray *outputMask,
int version,
aclTensor *gradInput,
aclTensor *gradWeight,
aclTensor *gradBias,
uint64_t *workspaceSize,
aclOpExecutor **executor)aclnnStatus aclnnFastBatchNormBackward(
void *workspace,
uint64_t workspaceSize,
aclOpExecutor *executor,
const aclrtStream stream)aclnnFastBatchNormBackwardGetWorkspaceSize
Parameters
Parameter Input/Output Description Instruction Data Type Data Format Dimension (shape) Non-contiguous Tensor gradOut (aclTensor*) Input Indicates the gradient tensor, which is `gradOut` in the corresponding formula. - Supports empty tensors.
- Supported shapes and formats: 2D (the corresponding format is NC), 3D (the corresponding format is NCL), 4D (the corresponding format is NCHW), 5D (the corresponding format is NCDHW), and 6-8D (the corresponding format is ND, where the 2nd dimension is fixed to the channel axis).
FLOAT32, FLOAT16, BFLOAT16 NC, NCL, NCHW, NCDHW, ND 2-8 √ input (aclTensor*) Input Indicates the input tensor in forward propagation, which is `x` in the corresponding formula. - Supports empty tensors.
- The data type, shape, and data format must be the same as those of `gradOut`.
FLOAT32, FLOAT16, BFLOAT16 NC, NCL, NCHW, NCDHW, ND 2-8 √ weight (aclTensor*) Input Indicates the weight tensor, corresponding to `weight` in the formula. - Supports empty tensors.
- The shape length is equal to the channel axis length in the input `input`.
FLOAT32, FLOAT16, BFLOAT16 ND 1 √ runningMean (aclTensor*) Input Indicates the mean calculated during training, corresponding to `runningMean` in the formula. - Supports empty tensors.
- The shape length is equal to the channel axis length in the incoming `input`.
FLOAT32, FLOAT16, BFLOAT16 ND 1 √ runningVar (aclTensor*) Input Indicates the variance computed during training, corresponding to `runningVar` in the formula. - Supports empty tensor.
- Values are non-negative.
- The shape length is equal to the channel axis length in `input`.
FLOAT32, FLOAT16, BFLOAT16 ND 1 √ saveMean (aclTensor*) Input Saved mean, corresponding to `saveMean` in the formula. - Supports empty tensors.
- The shape length is equal to the channel axis length in `input`.
FLOAT32, FLOAT16, BFLOAT16 ND 1 √ saveInvstd (aclTensor*) Input Saved reciprocal of the standard deviation, corresponding to the reciprocal of the square root of (Var(x) + eps) in the formula. - Supports empty tensors.
- Values are non-negative.
- The shape length is equal to the channel axis length of the input parameter `input`.
FLOAT32, FLOAT16, BFLOAT16 ND 1 √ training (bool) Input Indicates whether the scenario is training, corresponding to `training` in the formula description. true indicates a training scenario, and false indicates an inference scenario. - - - - eps (double) Input Value added to the variance to avoid division by zero. It corresponds to `eps` in the corresponding formula. - - - - - outputMask (aclBoolArray*) Input Indicates the mask of outputs. The size is 3. Each value indicates whether to output `gradInput`, `gradWeight`, or `gradBias`. If true, the corresponding output is generated; otherwise, empty is returned at the corresponding position. BoolArray - - - version (int) Input Indicates the algorithm version number used inside the operator. Currently supported optional values: 0 and 1. Default value: 0. - - - - gradInput (aclTensor*) Output Indicates the gradient of the input tensor, corresponding to `gradInput` in the formula. - Supports empty tensors.
- Optional output. If outputMask[0] is True, this output is required; otherwise, no output.
- The data type, shape, and data format must be consistent with those of `gradOut`.
FLOAT32, FLOAT16, BFLOAT16 NC, NCL, NCHW, NCDHW, ND 2-8 √ gradWeight (aclTensor*) Output Gradient of the scale parameter, corresponding to `gradWeight` in the formula. - Supports empty tensors.
- Optional output. If outputMask[1] is True, this output is required; otherwise, no output.
- Length equal to the channel axis length of the incoming parameter `input`.
FLOAT32, FLOAT16, BFLOAT16 ND 1 √ gradBias (aclTensor*) Output Indicates the gradient of the bias parameter, corresponding to `gradBias` in the formula. - Supports empty tensors.
- Optional output. If outputMask[2] is True, then need to input, otherwise not input.
- The shape length is equal to the channel axis length of the input parameter `input`.
FLOAT32, FLOAT16, BFLOAT16 ND 1 √ workspaceSize (uint64_t*) Output Returns the size of the workspace allocated by the user on the Device. - - - - - executor (aclOpExecutor**) Output Returns the op executor containing the operator computation flow. - - - - - Atlas A2 products ,Atlas A3 products :- The data types of
weight,runningMean,runningVar,saveMean,saveInvstd,gradWeight, andgradBiasmust be consistent with that ofgradOut.
- The data types of
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 incoming gradOut and input pointers are null pointers. When outputMask[0] is true and the incoming gradInput is a null pointer. When outputMask[1] is true and the incoming gradWeight is a null pointer. When outputMask[2] is true and the incoming gradBias is a null pointer. ACLNN_ERR_PARAM_INVALID 161002 The data type, data format, or shape of input or gradOut is not within the supported range. When weight, runningMean, runningVar, saveMean, and saveInvstd are non-empty, the data type, data format, or shape is not within the supported range. The length of outputMask is not 3. When outputMask[0] is true and the data type, data format, or shape of gradInput is not within the supported range. When outputMask[1] is true and the data type, data format, or shape of gradWeight is not within the supported range. When outputMask[2] is true and the data type, data format, or shape of gradBias is not within the supported range. The shape length of weight, runningMean, runningVar, saveMean, saveInvstd, gradWeight (when non-empty), or gradBias (when non-empty) is not equal to the channel axis length in the input shape. The data formats of input, gradOut, and gradInput (when non-empty) are inconsistent. The data types of input, gradOut, and gradInput (when non-empty) are inconsistent. The shapes of input, gradOut, and gradInput (when non-empty) are inconsistent, or the shape dimension is greater than 8 or less than 2.
aclnnFastBatchNormBackward
Parameters
Parameter Input/Output Description workspace Input Device-allocated workspace memory address. workspaceSize Input Device-allocated workspace size, obtained by the first-phase API aclnnFastBatchNormBackwardGetWorkspaceSize. executor Input Op executor, containing the operator computation flow. stream Input Stream for executing tasks. Return Value
aclnnStatus: return status code. For details, see aclnn Error Codes.
Constraints
- Deterministic computation:
- aclnnFastBatchNormBackward 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_fast_batch_norm_backward.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); 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 request memory on the Device side.
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 the device/stream. For details, see the acl API manual.
// Fill in deviceId based on the actual device in use.
int32_t deviceId = 0;
aclrtStream stream;
auto ret = Init(deviceId, &stream);
// Handle the check as needed.
CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("Init acl failed. ERROR: %d\n", ret); return ret);
// 2. Construct inputs and outputs. Customize the construction based on the API.
std::vector<int64_t> gradOutShape = {1, 2, 4};
std::vector<int64_t> selfShape = {1, 2, 4};
std::vector<int64_t> weightShape = {2};
std::vector<int64_t> rMeanShape = {2};
std::vector<int64_t> rVarShape = {2};
std::vector<int64_t> sMeanShape = {2};
std::vector<int64_t> sVarShape = {2};
std::vector<int64_t> gradInShape = {1, 2, 4};
std::vector<int64_t> gradWeightShape = {2};
std::vector<int64_t> gradBiasShape = {2};
void* gradOutDeviceAddr = nullptr;
void* selfDeviceAddr = nullptr;
void* weightDeviceAddr = nullptr;
void* rMeanDeviceAddr = nullptr;
void* rVarDeviceAddr = nullptr;
void* sMeanDeviceAddr = nullptr;
void* sVarDeviceAddr = nullptr;
void* outMaskDeviceAddr = nullptr;
void* gradInDeviceAddr = nullptr;
void* gradWeightDeviceAddr = nullptr;
void* gradBiasDeviceAddr = nullptr;
aclTensor* gradOut = nullptr;
aclTensor* self = nullptr;
aclTensor* weight = nullptr;
aclTensor* rMean = nullptr;
aclTensor* rVar = nullptr;
aclTensor* sMean = nullptr;
aclTensor* sVar = nullptr;
aclBoolArray* outMask = nullptr;
aclTensor* gradIn = nullptr;
aclTensor* gradWeight = nullptr;
aclTensor* gradBias = nullptr;
std::vector<float> gradOutHostData = {0, 1, 2, 3, 4, 5, 6, 7};
std::vector<float> selfHostData = {0, 1, 2, 3, 4, 5, 6, 7};
std::vector<float> weightHostData = {1, 1};
std::vector<float> rMeanHostData = {0, 0};
std::vector<float> rVarHostData = {1, 1};
std::vector<float> sMeanHostData = {0, 0};
std::vector<float> sVarHostData = {1, 1};
std::vector<float> gradInHostData(8, 0);
std::vector<float> gradWeightHostData(2, 0);
std::vector<float> gradBiasHostData(2, 0);
bool training = true;
double eps = 1e-5;
// Create the gradOut aclTensor.
ret = CreateAclTensor(gradOutHostData, gradOutShape, &gradOutDeviceAddr, aclDataType::ACL_FLOAT, &gradOut);
CHECK_RET(ret == ACL_SUCCESS, return ret);
// Create the self aclTensor.
ret = CreateAclTensor(selfHostData, selfShape, &selfDeviceAddr, aclDataType::ACL_FLOAT, &self);
CHECK_RET(ret == ACL_SUCCESS, return ret);
// Create weight aclTensor.
ret = CreateAclTensor(weightHostData, weightShape, &weightDeviceAddr, aclDataType::ACL_FLOAT, &weight);
CHECK_RET(ret == ACL_SUCCESS, return ret);
// Create rMean aclTensor.
ret = CreateAclTensor(rMeanHostData, rMeanShape, &rMeanDeviceAddr, aclDataType::ACL_FLOAT, &rMean);
CHECK_RET(ret == ACL_SUCCESS, return ret);
// Create rVar aclTensor.
ret = CreateAclTensor(rVarHostData, rVarShape, &rVarDeviceAddr, aclDataType::ACL_FLOAT, &rVar);
CHECK_RET(ret == ACL_SUCCESS, return ret);
// Create sMean aclTensor.
ret = CreateAclTensor(sMeanHostData, sMeanShape, &sMeanDeviceAddr, aclDataType::ACL_FLOAT, &sMean);
CHECK_RET(ret == ACL_SUCCESS, return ret);
// Create sVar aclTensor.
ret = CreateAclTensor(sVarHostData, sVarShape, &sVarDeviceAddr, aclDataType::ACL_FLOAT, &sVar);
CHECK_RET(ret == ACL_SUCCESS, return ret);
// Create outMask aclBoolArray.
bool maskData[3] = {true, true, true};
outMask = aclCreateBoolArray(&(maskData[0]), 3);
// Create gradIn aclTensor.
ret = CreateAclTensor(gradInHostData, gradInShape, &gradInDeviceAddr, aclDataType::ACL_FLOAT, &gradIn);
CHECK_RET(ret == ACL_SUCCESS, return ret);
// Create gradWeight aclTensor.
ret = CreateAclTensor(gradWeightHostData, gradWeightShape, &gradWeightDeviceAddr, aclDataType::ACL_FLOAT, &gradWeight);
CHECK_RET(ret == ACL_SUCCESS, return ret);
// Create gradBias aclTensor.
ret = CreateAclTensor(gradBiasHostData, gradBiasShape, &gradBiasDeviceAddr, aclDataType::ACL_FLOAT, &gradBias);
CHECK_RET(ret == ACL_SUCCESS, return ret);
uint64_t workspaceSize = 0;
aclOpExecutor* executor;
// Example of calling the aclnnFastBatchNormBackward API.
// 3. Call the CANN operator library API. Replace it with the actual API name.
// Call the first-phase API of aclnnFastBatchNormBackward
ret = aclnnFastBatchNormBackwardGetWorkspaceSize(gradOut, self, weight, rMean, rVar, sMean, sVar, training, eps, outMask, 0, gradIn, gradWeight, gradBias, &workspaceSize, &executor);
CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclnnFastBatchNormBackwardGetWorkspaceSize 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 aclnnFastBatchNormBackward
ret = aclnnFastBatchNormBackward(workspaceAddr, workspaceSize, executor, stream);
CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclnnFastBatchNormBackward failed. ERROR: %d\n", ret); return ret);
// 4. (boilerplate) Synchronously 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 the Device side to the Host side. Modify it according to the definition of the specific API
auto size = GetShapeSize(gradInShape);
std::vector<float> resultData(size, 0);
ret = aclrtMemcpy(resultData.data(), resultData.size() * sizeof(resultData[0]), gradInDeviceAddr,
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: %f\n", i, resultData[i]);
}
// 6. Release the aclTensor and aclScalar. Modify them according to the definition of the specific API
aclDestroyTensor(gradOut);
aclDestroyTensor(self);
aclDestroyTensor(weight);
aclDestroyTensor(rMean);
aclDestroyTensor(rVar);
aclDestroyTensor(sMean);
aclDestroyTensor(sVar);
aclDestroyBoolArray(outMask);
aclDestroyTensor(gradIn);
aclDestroyTensor(gradWeight);
aclDestroyTensor(gradBias);
// 7. Release device resources. Modify them according to the definition of the specific API
aclrtFree(gradOutDeviceAddr);
aclrtFree(selfDeviceAddr);
aclrtFree(weightDeviceAddr);
aclrtFree(rMeanDeviceAddr);
aclrtFree(rVarDeviceAddr);
aclrtFree(sMeanDeviceAddr);
aclrtFree(sVarDeviceAddr);
aclrtFree(gradInDeviceAddr);
aclrtFree(gradWeightDeviceAddr);
aclrtFree(gradBiasDeviceAddr);
if (workspaceSize > 0) {
aclrtFree(workspaceAddr);
}
aclrtDestroyStream(stream);
aclrtResetDevice(deviceId);
aclFinalize();
return 0;
}