aclnnGroupNormSilu
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 : Supported
Atlas training products : Not supported
Function
- Description: Computes the Group Normalization result groupnormOut, mean meanOut, and reciprocal of standard deviation rstdOut of the input self, and applies the SiLU operation to groupnormOut to obtain the final output out.
- Formula:
GroupNorm:
Let denote the mean of , and denote the variance of . Then
Silu:
Function Prototype
Each operator uses a two-phase API. First, call the "aclnnGroupNormSiluGetWorkspaceSize" API to obtain the workspace size required for computation and the executor containing the operator computation process. Then, call the "aclnnGroupNormSilu" API to perform the computation.
aclnnStatus aclnnGroupNormSiluGetWorkspaceSize(
const aclTensor* self,
const aclTensor* gamma,
const aclTensor* beta,
int64_t group,
double eps,
aclTensor* out,
aclTensor* meanOut,
aclTensor* rstdOut,
uint64_t* workspaceSize,
aclOpExecutor** executor);aclnnStatus aclnnGroupNormSilu(
void * workspace,
uint64_t workspaceSize,
aclOpExecutor *executor,
aclrtStream stream)aclnnGroupNormSiluGetWorkspaceSize
Parameters
Parameter Input/Output Description Instruction Data Type Data Format Dimension (shape) Non-contiguous Tensor self (aclTensor*) Input x in the formula. - FLOAT16, FLOAT, BFLOAT16 ND 2-8, where dim 1 is N and dim 2 is C √ gamma (aclTensor*) Input γ in the formula. Data type must be the same as that of self or be FLOAT, and the number of elements must be the same as the size of the second dimension of input self. FLOAT16, FLOAT, BFLOAT16 ND 1 √ beta (aclTensor*) Input β in the formula. Data type must be the same as that of self or be FLOAT, and the number of elements must be the same as the size of the second dimension of input self. FLOAT16, FLOAT, BFLOAT16 ND 1 √ group (int64_t) Input Specifies the number of groups to divide the second dimension of input self into. The first dimension of self must be divisible by group. INT64 - - - eps (double) Input eps in the formula. eps must be greater than 0. DOUBLE - - - out (aclTensor*) Output The out in the formula. The data type is the same as self. FLOAT16, FLOAT, BFLOAT16 ND Same as self √ meanOut(aclTensor*) Output The meanOut in the formula. Data type is the same as that of self. N in the shape is the size of the first dimension of self. FLOAT16, FLOAT, BFLOAT16 ND (N, group) √ rstdOut (aclTensor*) Output rstdOut in the formula. Data type is the same as that of self. N in the shape is the size of the first dimension of self. FLOAT16, FLOAT, BFLOAT16 ND (N, group) √ workspaceSize (uint64_t*) Output Returns the workspace size to be requested on the Device. - - - - - executor (aclOpExecutor**) Output Returns the op executor, which contains the operator computation process. - - - - - Atlas inference products : The BFLOAT16 data type is not supported.
Ascend 950PR&950DT products : gamma and beta can be empty. When gamma or beta is not empty, the data types of meanOut and rstdOut must be the same as those of gamma or beta. When both gamma and beta are empty, the data types of meanOut and rstdOut must be the same as that of self.
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 If a mandatory input, output, or attribute is a null pointer, 161001 is returned. ACLNN_ERR_PARAM_INVALID 161002 The input and output data types are not within the supported range. The input and output parameters do not meet the constraints described in Parameters.
aclnnGroupNormSilu
Parameters
Parameter Input/Output Description workspace Input Address of the workspace memory allocated on the Device. workspaceSize Input Size of the workspace memory allocated on the Device, obtained by the first-phase API aclnnGroupNormSiluGetWorkspaceSize. executor Input Op executor that contains the computation process of the operator. stream Input Specifies the stream that executes the task. Return Value
aclnnStatus: return status code. For details, see aclnn Error Codes.
Constraints
Deterministic computation: aclnnGroupNormSilu defaults to a deterministic implementation.
Input shape constraints:
- The size of the second dimension of self must be divisible by group.
- The shape of meanOut and rstdOut must be (N, group), where N is the size of the first dimension of self.
Input attribute restriction: eps > 0.
Example
The following sample code is for reference only. For details about compilation and execution, see Compile and Run Samples.
#include <cinttypes>
#include <iostream>
#include <vector>
#include "acl/acl.h"
#include "aclnnop/aclnn_group_norm_silu.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, AscendCL 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);
// 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 device/stream. For details, see the list of ACL external APIs.
// 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 inputs and outputs. Customize the construction based on the API.
std::vector<int64_t> selfShape = {2, 3, 4};
std::vector<int64_t> gammaShape = {3};
std::vector<int64_t> betaShape = {3};
std::vector<int64_t> outShape = {2, 3, 4};
std::vector<int64_t> meanOutShape = {2, 1};
std::vector<int64_t> rstdOutShape = {2, 1};
void* selfDeviceAddr = nullptr;
void* gammaDeviceAddr = nullptr;
void* betaDeviceAddr = nullptr;
void* outDeviceAddr = nullptr;
void* meanOutDeviceAddr = nullptr;
void* rstdOutDeviceAddr = nullptr;
aclTensor* self = nullptr;
aclTensor* gamma = nullptr;
aclTensor* beta = nullptr;
aclTensor* out = nullptr;
aclTensor* meanOut = nullptr;
aclTensor* rstdOut = nullptr;
std::vector<float> selfHostData = {1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0, 11.0, 12.0,
13.0, 14.0, 15.0, 16.0, 17.0, 18.0, 19.0, 20.0, 21.0, 22.0, 23.0, 24.0};
std::vector<float> gammaHostData = {2.0, 2, 2};
std::vector<float> betaHostData = {2.0, 2, 2};
std::vector<float> outHostData = {1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0, 11.0, 12.0,
13.0, 14.0, 15.0, 16.0, 17.0, 18.0, 19.0, 20.0, 21.0, 22.0, 23.0, 24.0};
std::vector<float> meanOutHostData = {2.0, 2};
std::vector<float> rstdOutHostData = {2.0, 2};
int64_t group = 1;
double eps = 0.00001;
// Create the self aclTensor.
ret = CreateAclTensor(selfHostData, selfShape, &selfDeviceAddr, aclDataType::ACL_FLOAT, &self);
CHECK_RET(ret == ACL_SUCCESS, return ret);
// Create the gamma aclTensor.
ret = CreateAclTensor(gammaHostData, gammaShape, &gammaDeviceAddr, aclDataType::ACL_FLOAT, &gamma);
CHECK_RET(ret == ACL_SUCCESS, return ret);
// Create the beta aclTensor.
ret = CreateAclTensor(betaHostData, betaShape, &betaDeviceAddr, aclDataType::ACL_FLOAT, &beta);
CHECK_RET(ret == ACL_SUCCESS, return ret);
// Create the out aclTensor.
ret = CreateAclTensor(outHostData, outShape, &outDeviceAddr, aclDataType::ACL_FLOAT, &out);
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);
// Create the rstdOut aclTensor.
ret = CreateAclTensor(rstdOutHostData, rstdOutShape, &rstdOutDeviceAddr, aclDataType::ACL_FLOAT, &rstdOut);
CHECK_RET(ret == ACL_SUCCESS, return ret);
// 3. Call the CANN operator library API. Replace it with the specific API.
uint64_t workspaceSize = 0;
aclOpExecutor* executor;
// Call the first-phase API of aclnnGroupNormSilu.
ret = aclnnGroupNormSiluGetWorkspaceSize(self, gamma, beta, group, eps, out, meanOut, rstdOut, &workspaceSize, &executor);
CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclnnGroupNormSiluGetWorkspaceSize 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 aclnnGroupNormSilu.
ret = aclnnGroupNormSilu(workspaceAddr, workspaceSize, executor, stream);
CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclnnGroupNormSilu 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 device-side memory to host-side memory. Modify it according to the specific API definition.
auto size = GetShapeSize(outShape);
std::vector<float> outResultData(size, 0);
ret = aclrtMemcpy(outResultData.data(), outResultData.size() * sizeof(outResultData[0]), outDeviceAddr, 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("outResultData[%" PRId64 "] is: %f\n", i, outResultData[i]);
}
size = GetShapeSize(meanOutShape);
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[%" PRId64 "] is: %f\n", i, meanResultData[i]);
}
size = GetShapeSize(rstdOutShape);
std::vector<float> rstdResultData(size, 0);
ret = aclrtMemcpy(rstdResultData.data(), rstdResultData.size() * sizeof(rstdResultData[0]), rstdOutDeviceAddr, 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("rstdResultData[%" PRId64 "] is: %f\n", i, rstdResultData[i]);
}
// 6. Release aclTensor and aclScalar. Modify it according to the specific API definition.
aclDestroyTensor(self);
aclDestroyTensor(gamma);
aclDestroyTensor(beta);
aclDestroyTensor(out);
aclDestroyTensor(meanOut);
aclDestroyTensor(rstdOut);
// 7. Release device resources. Modify it according to the specific API definition.
aclrtFree(selfDeviceAddr);
aclrtFree(gammaDeviceAddr);
aclrtFree(betaDeviceAddr);
aclrtFree(outDeviceAddr);
aclrtFree(meanOutDeviceAddr);
aclrtFree(rstdOutDeviceAddr);
if (workspaceSize > 0) {
aclrtFree(workspaceAddr);
}
aclrtDestroyStream(stream);
aclrtResetDevice(deviceId);
aclFinalize();
return 0;
}