aclnnVarMean
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: Returns the mean and variance computed from the values of the input tensor along the specified dimension.
Formula: Assume dim is . The calculation is performed along this dimension. is the shape of this dimension. Take to calculate the mean along this dimension: .
The variance formula is as follows:
If
keepdim = true, this dimension is retained after reduction and is set to 1 in the output shape; ifkeepdim = false, this dimension is not retained. If dim is nullptr or [], all dimensions are calculated.
Function Prototype
Each operator uses a two-phase API. Call the "aclnnVarMeanGetWorkspaceSize" API first to obtain the workspace size required for the computation and the executor that contains the computation process of the operator, and then call the "aclnnVarMean" API to execute the computation.
aclnnStatus aclnnVarMeanGetWorkspaceSize(
const aclTensor* self,
const aclIntArray* dim,
int64_t correction,
bool keepdim,
aclTensor* varOut,
aclTensor* meanOut,
uint64_t* workspaceSize,
aclOpExecutor** executor)aclnnStatus aclnnVarMean(
void* workspace,
uint64_t workspaceSize,
aclOpExecutor* executor,
aclrtStream stream)aclnnVarMeanGetWorkspaceSize
Parameters:
Parameter Input/Output Description Instruction Data Type Data Format Dimension (shape) Non-contiguous Tensor self(aclTensor*) Input selfin the formula.shape supports 0 to 8 dimensions. FLOAT, FLOAT16, BFLOAT16 ND 1-8 √ dim (aclIntArray*) Input dimin the formula.Dimensions involved in the computation. The value range is [-self.dim(), self.dim()-1], and duplicate values are not allowed. The supported data type is INT64. If dim is nullptr or [], all dimensions are used for the computation. - - - - correction (int64_t) Input Input correctionin the formula, which is the correction value.- - - - - keepdim (bool) Input Specifies whether the dimension of the reduce axis is retained. - - - - - meanOut (aclTensor*) Output Calculation result of the mean. Supports 0 to 8 dimensions. FLOAT, FLOAT16, BFLOAT16 ND 0-8 √ varOut (aclTensor*) Output The output varOutin the formula; the variance calculation result.Supports 0 to 8 dimensions. FLOAT, FLOAT16, BFLOAT16 ND 0-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. - - - - - Atlas training products ,Atlas inference products : the data type BFLOAT16 is not supported.
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 self, meanOut, or varOut is a null pointer. ACLNN_ERR_PARAM_INVALID 161002 The data type of self, meanOut, or varOut is not within the supported range. The shape of self exceeds 8 dimensions. The value of dim is invalid (data in dim points to the same dimension, or dim is out of the dimension range of self). The shapes of self, meanOut, and varOut do not satisfy the deduction rules in the formula.
aclnnVarMean
Parameters:
Parameter Input/Output Description workspace Input Workspace memory address allocated on the Device. workspaceSize Input Size of the workspace allocated on the Device, obtained by the first-phase API aclnnVarMeanGetWorkspaceSize. executor Input Op executor that contains the operator computation process. stream Input Specifies the Stream for executing tasks. Return value aclnnStatus: return status code. For details, see aclnn Error Codes.
Constraints
- Deterministic computation:
- aclnnVarMean 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_var_mean.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 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 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 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 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 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,4};
std::vector<int64_t> outShape = {2,1};
void* selfDeviceAddr = nullptr;
void* meanDeviceAddr = nullptr;
void* varDeviceAddr = nullptr;
aclTensor* self = nullptr;
aclIntArray* dim = nullptr;
aclTensor* var = nullptr;
aclTensor* mean = nullptr;
std::vector<float> selfHostData = {0.0, 1.1, 2, 3, 4, 5, 6, 7};
std::vector<int64_t> dimData = {1};
int64_t correction = 1;
bool keepdim = true;
std::vector<float> varHostData = {0.0, 0};
std::vector<float> meanHostData = {0.0, 0};
// Create the self aclTensor.
ret = CreateAclTensor(selfHostData, selfShape, &selfDeviceAddr, aclDataType::ACL_FLOAT, &self);
CHECK_RET(ret == ACL_SUCCESS, return ret);
// Create the dim aclIntArray.
dim = aclCreateIntArray(dimData.data(), dimData.size());
CHECK_RET(dim != nullptr, return ret);
// Create the out aclTensor.
ret = CreateAclTensor(varHostData, outShape, &varDeviceAddr, aclDataType::ACL_FLOAT, &var);
ret = CreateAclTensor(meanHostData, outShape, &meanDeviceAddr, aclDataType::ACL_FLOAT, &mean);
CHECK_RET(ret == ACL_SUCCESS, return ret);
// 3. Call the CANN operator library API, and modify it to the specific API.
uint64_t workspaceSize = 0;
aclOpExecutor* executor;
// Call the first-phase API of aclnnVarMean.
ret = aclnnVarMeanGetWorkspaceSize(self, dim, correction, keepdim, var, mean, &workspaceSize, &executor);
CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclnnVarMeanGetWorkspaceSize 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 aclnnVarMean.
ret = aclnnVarMean(workspaceAddr, workspaceSize, executor, stream);
CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclnnVarMean 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 based on the API definition of the specific API.
auto size = GetShapeSize(outShape);
std::vector<float> meanData(size, 0);
ret = aclrtMemcpy(meanData.data(), meanData.size() * sizeof(meanData[0]), meanDeviceAddr, 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("meanResult[%ld] is: %f\n", i, meanData[i]);
}
std::vector<float> varData(size, 0);
ret = aclrtMemcpy(varData.data(), varData.size() * sizeof(varData[0]), varDeviceAddr, 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("varResult[%ld] is: %f\n", i, varData[i]);
}
// 6. Release the aclTensor and aclScalar. Modify it based on the API definition of the specific API.
aclDestroyTensor(self);
aclDestroyIntArray(dim);
aclDestroyTensor(mean);
aclDestroyTensor(var);
// 7. Release device resources. Modify it based on the API definition of the specific API.
aclrtFree(selfDeviceAddr);
aclrtFree(varDeviceAddr);
aclrtFree(meanDeviceAddr);
if (workspaceSize > 0) {
aclrtFree(workspaceAddr);
}
aclrtDestroyStream(stream);
aclrtResetDevice(deviceId);
aclFinalize();
return 0;
}