aclnnStdMeanCorrection
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 sample standard deviation and mean.
- Formula:
Assume dim is , then calculation is performed on this dimension. is the shape of this dimension. Take and calculate the mean on this dimension .
When
keepdim = true, the dimension is retained after reduction, and its value in the output shape is 1. Whenkeepdim = false, the dimension is not retained.
Function Prototype
Each operator uses a two-phase API. Call the "aclnnStdMeanCorrectionGetWorkspaceSize" API first to obtain the input parameters and calculate the required workspace size based on the computation process, and then call the "aclnnStdMeanCorrection" API to perform the computation.
aclnnStatus aclnnStdMeanCorrectionGetWorkspaceSize(
const aclTensor* self,
const aclIntArray* dim,
int64_t correction,
bool keepdim,
aclTensor* stdOut,
aclTensor* meanOut,
uint64_t* workspaceSize,
aclOpExecutor** executor)aclnnStatus aclnnStdMeanCorrection(
void* workspace,
uint64_t workspaceSize,
aclOpExecutor* executor,
const aclrtStream stream)aclnnStdMeanCorrectionGetWorkspaceSize
Parameters
Parameter Input/Output Description Instruction Data Type Data Format Dimension (Shape) Non-Contiguous Tensor self (aclTensor*) Input self in the formula. - FLOAT, BFLOAT16, FLOAT16 ND - √ dim (aclIntArray*) Input dim in the formula, indicating the dimension that participates in the calculation. The value range is [-self.dim(), self.dim()-1], and the values in it cannot be the same.
When dim is nullptr or [], all dimensions are calculated.INT64 - - - correction (int64_t) Input δN value in the formula, which is the correction value. - - - - - keepdim (bool) Input In the formula, keepdim specifies whether to retain the dimensions of the input tensor in the output tensor. - - - - - stdOut (aclTensor*) Output stdOut in the formula. - FLOAT, BFLOAT16, FLOAT16 ND - √ meanOut (aclTensor*) Output meanOut in the Formula. - FLOAT, BFLOAT16, FLOAT16 ND - √ workspaceSize (uint64_t*) Output Returns the workspace size to be requested by the Device side. - - - - - executor (aclOpExecutor**) Output Returns the op executor that contains 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:
Return Value Error Code Description ACLNN_ERR_PARAM_NULLPTR 161001 When self, stdOut, and meanOut are null pointers. ACLNN_ERR_PARAM_INVALID 161002 The data types of self, stdOut, and meanOut are not within the supported range. A dimension in the dim array exceeds the dimension range of self. Elements in the dim array are duplicated. An error occurs when the shape of stdOut is as follows:
If keepdim is true, stdOut.shape != self.shape (shape with the dimension specified by dim set to 1);
If keepdim is false, stdOut.shape != self.shape (shape with the dimension specified by dim removed).An error occurs when the shape of meanOut is as follows:
If keepdim is true, meanOut.shape != self.shape (shape with the dimension specified by dim set to 1);
If keepdim is false, meanOut.shape != self.shape (shape with the dimension specified by dim removed).
aclnnStdMeanCorrection
Parameters
Parameter Input/Output Description workspace Input Workspace memory address requested by the Device side. workspaceSize Input Workspace size requested by the Device side, obtained by calling the first-phase API aclnnStdMeanCorrectionGetWorkspaceSize. executor Input Op executor that contains the operator computation process. stream Input Stream on which the task is executed. Return value
aclnnStatus: return status code. For details, see aclnn Return Codes.
Constraints
- Deterministic computation:
- aclnnStdMeanCorrection 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_std_mean_correction.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 shape_size = 1;
for (auto i : shape) {
shape_size *= i;
}
return shape_size;
}
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 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);
// Compute the strides of a 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.
// Set deviceId based on the actual device.
int32_t deviceId = 0;
aclrtStream stream;
auto ret = Init(deviceId, &stream);
// Handle the check as needed.
CHECK_RET(ret == 0, 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> selfShape = {2, 3, 4};
std::vector<int64_t> stdOutShape = {2, 4};
std::vector<int64_t> meanOutShape = {2, 4};
void* selfDeviceAddr = nullptr;
void* stdOutDeviceAddr = nullptr;
void* meanOutDeviceAddr = nullptr;
aclTensor* self = nullptr;
aclTensor* stdOut = nullptr;
aclTensor* meanOut = nullptr;
std::vector<float> selfHostData = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18,
19, 20, 21, 22, 23, 24};
std::vector<float> stdOutHostData = {1, 2, 3, 4, 5, 6, 7, 8.0};
std::vector<float> meanOutHostData = {1, 2, 3, 4, 5, 6, 7, 8.0};
std::vector<int64_t> dimData = {1};
int64_t correction = 1;
bool keepdim = false;
// Create the self aclTensor.
ret = CreateAclTensor(selfHostData, selfShape, &selfDeviceAddr, aclDataType::ACL_FLOAT, &self);
CHECK_RET(ret == ACL_SUCCESS, return ret);
// Create the stdOut aclTensor.
ret = CreateAclTensor(stdOutHostData, stdOutShape, &stdOutDeviceAddr, aclDataType::ACL_FLOAT, &stdOut);
CHECK_RET(ret == ACL_SUCCESS, return ret);
// Create the meanOut aclTensor.
ret = CreateAclTensor(meanOutHostData, meanOutShape, &meanOutDeviceAddr, aclDataType::ACL_FLOAT, &meanOut);
CHECK_RET(ret == ACL_SUCCESS, return ret);
const aclIntArray *dim = aclCreateIntArray(dimData.data(), dimData.size());
CHECK_RET(dim != nullptr, return ACL_ERROR_INTERNAL_ERROR);
// 3. Call the CANN operator library API, and change it to the specific API.
uint64_t workspaceSize = 0;
aclOpExecutor* executor;
// Call the first-phase API of aclnnStdMeanCorrection.
ret = aclnnStdMeanCorrectionGetWorkspaceSize(self, dim, correction, keepdim, stdOut, meanOut, &workspaceSize, &executor);
CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclnnStdMeanCorrectionGetWorkspaceSize 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 aclnnStdMeanCorrection.
ret = aclnnStdMeanCorrection(workspaceAddr, workspaceSize, executor, stream);
CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclnnStdMeanCorrection failed. ERROR: %d\n", ret); return ret);
// 4. (Boilerplate) Wait synchronously 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 memory to the host side. Modify it based on the definition of the specific API.
auto size = GetShapeSize(stdOutShape);
std::vector<float> stdResultData(size, 0);
ret = aclrtMemcpy(stdResultData.data(), stdResultData.size() * sizeof(stdResultData[0]), stdOutDeviceAddr, size * sizeof(float),
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("stdResultData[%ld] is: %f\n", i, stdResultData[i]);
}
std::vector<float> meanResultData(size, 0);
ret = aclrtMemcpy(meanResultData.data(), meanResultData.size() * sizeof(meanResultData[0]), meanOutDeviceAddr, size * sizeof(float),
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("meanResultData[%ld] is: %f\n", i, meanResultData[i]);
}
// 6. Destroy the aclTensor and aclScalar. Modify it based on the definition of the specific API.
aclDestroyTensor(self);
aclDestroyTensor(stdOut);
aclDestroyTensor(meanOut);
// 7. Release device resources. Modify it based on the definition of the specific API.
aclrtFree(selfDeviceAddr);
aclrtFree(stdOutDeviceAddr);
aclrtFree(meanOutDeviceAddr);
if (workspaceSize > 0) {
aclrtFree(workspaceAddr);
}
aclrtDestroyStream(stream);
aclrtResetDevice(deviceId);
aclFinalize();
return 0;
}