aclnnBatchNormStats
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 : Supported
Function
Description: Computes the mean and the reciprocal of the standard deviation of the single-device input data.
Formula:
Mean:
Reciprocal of standard deviation:
Function Prototype
Each operator uses a two-phase API. You must first call the "aclnnBatchNormStatsGetWorkspaceSize" API to obtain the input parameters and calculate the required workspace size according to the process, and then call the "aclnnBatchNormStats" API to perform the computation.
aclnnStatus aclnnBatchNormStatsGetWorkspaceSize(
const aclTensor* input,
double eps,
aclTensor* mean,
aclTensor* invstd,
uint64_t* workspaceSize,
aclOpExecutor** executor)aclnnStatus aclnnBatchNormStats(
void* workspace,
uint64_t workspaceSize,
aclOpExecutor* executor,
const aclrtStream stream)aclnnBatchNormStatsGetWorkspaceSize
Parameters:
Parameter Input/Output Description Instruction Data Type Data Format Dimension (shape) Non-contiguous Tensor input (aclTensor*) Input - The supported shapes and data formats are as follows:
2 dimensions (NC).
3 dimensions (NCL).
4 dimensions (NCHW).
5 dimensions (NCDHW).
6-8 dimensions (ND, where the second dimension is fixed as the channel axis).FLOAT, FLOAT16, BFLOAT16 ND 2-8 dimensions √ eps (double) Input Value added to the denominator for numerical stability. - double - - - mean (aclTensor*) Output Output mean. - FLOAT ND 1D √ invstd (aclTensor*) Output Outputs the inverse of standard deviation. - FLOAT ND 1D √ workspaceSize (uint64_t*) Output Returns the size of the workspace to be requested by the Device. - - - - - executor (aclOpExecutor**) Output Returns the op executor, which contains the operator computation process. - - - - - Atlas A2 products ,Atlas A3 products , andAtlas training products : The BFLOAT16 data type is not supported for the parameterinput.
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:
Return value Error Code Description ACLNN_ERR_PARAM_NULLPTR 161001 The input, mean, or invstd is a null pointer. ACLNN_ERR_PARAM_INVALID 161002 The data types and data formats of input, mean, and invstd are not in the supported range. The dimension of input is less than 2 or greater than 8, or the dimension of mean and invstd is not 1. The shape of mean or invstd is inconsistent with the channel axis of input. The value of the second dimension of input is 0.
aclnnBatchNormStats
Parameters:
Parameter Input/Output Description workspace Input Address of the workspace memory requested by the Device. workspaceSize Input Size of the workspace requested by the Device, obtained by calling the first-phase API aclnnBatchNormStatsGetWorkspaceSize. executor Input Op executor that contains the operator computation flow. stream Input Specifies the stream that executes tasks. Return value
aclnnStatus: return status code. For details, see aclnn Return Codes.
Constraints
- Deterministic computation:
- aclnnBatchNormStats defaults to a deterministic implementation.
Example
The sample code is as follows for reference only. For the compilation and execution process, see Compile and Run Samples.
#include <iostream>
#include <vector>
#include "acl/acl.h"
#include "aclnnop/aclnn_batch_norm_stats.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); 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 request 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 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 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 reference.
// Set 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. Custom construction is required based on the API.
std::vector<int64_t> inputShape = {2, 3};
std::vector<int64_t> outShape = {3,};
void* inputDeviceAddr = nullptr;
void* meanDeviceAddr = nullptr;
void* invstdDeviceAddr = nullptr;
aclTensor* input = nullptr;
aclTensor* mean = nullptr;
aclTensor* invstd = nullptr;
std::vector<float> inputHostData = {1, 2, 3, 4, 5, 6};
std::vector<float> meanHostData = {0, 0, 0};
std::vector<float> invstdHostData = {0, 0, 0};
double eps = 1e-5;
// Create the self aclTensor.
ret = CreateAclTensor(inputHostData, inputShape, &inputDeviceAddr, aclDataType::ACL_FLOAT, &input);
CHECK_RET(ret == ACL_SUCCESS, return ret);
// Create the mean aclTensor.
ret = CreateAclTensor(meanHostData, outShape, &meanDeviceAddr, aclDataType::ACL_FLOAT, &mean);
CHECK_RET(ret == ACL_SUCCESS, return ret);
// Create the invstd aclTensor.
ret = CreateAclTensor(invstdHostData, outShape, &invstdDeviceAddr, aclDataType::ACL_FLOAT, &invstd);
CHECK_RET(ret == ACL_SUCCESS, return ret);
uint64_t workspaceSize = 0;
aclOpExecutor* executor;
// Example of calling the aclnnBatchNormStats API
// 3. Call the CANN operator library API. Change it to the specific API name.
// Call the first-phase API of aclnnBatchNormStats.
ret = aclnnBatchNormStatsGetWorkspaceSize(input, eps, mean, invstd, &workspaceSize, &executor);
CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclnnBatchNormStatsGetWorkspaceSize 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 aclnnBatchNormStats.
ret = aclnnBatchNormStats(workspaceAddr, workspaceSize, executor, stream);
CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclnnBatchNormStats failed. ERROR: %d\n", ret); return ret);
// 4. (boilerplate) Wait synchronously for the task 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. Copy the result from Device memory to the Host side. Modify it based on the specific API definition.
auto size = GetShapeSize(outShape);
std::vector<float> meanData(size, 0);
ret = aclrtMemcpy(meanData.data(), meanData.size() * sizeof(meanData[0]), meanDeviceAddr,
size * sizeof(meanData[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("mean result[%ld] is: %f\n", i, meanData[i]);
}
std::vector<float> invstdData(size, 0);
ret = aclrtMemcpy(invstdData.data(), invstdData.size() * sizeof(invstdData[0]), invstdDeviceAddr,
size * sizeof(invstdData[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("invstd result[%ld] is: %f\n", i, invstdData[i]);
}
// 6. Release the aclTensor and aclScalar. Modify it based on the specific API definition.
aclDestroyTensor(input);
aclDestroyTensor(mean);
aclDestroyTensor(invstd);
// 7. Release the device resources. Modify it based on the specific API definition.
aclrtFree(inputDeviceAddr);
aclrtFree(meanDeviceAddr);
aclrtFree(invstdDeviceAddr);
if (workspaceSize > 0) {
aclrtFree(workspaceAddr);
}
aclrtDestroyStream(stream);
aclrtResetDevice(deviceId);
aclFinalize();
return 0;
}