aclnnGroupNormSwish
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 : Not supported
Function
- Description: Computes the group normalization result out, mean meanOut, reciprocal of standard deviation rstdOut, and the Swish output of the input x.
- Formula:
GroupNorm:
Let represent the mean of , and represent the variance of , then
Swish:
When activateSwish is True, Swish is calculated. In this case, x in the Swish formula is the out obtained by the GroupNorm formula.
Function Prototype
Each operator uses a two-phase API. First, call "aclnnGroupNormSwishGetWorkspaceSize" to obtain the workspace size required for the computation and the executor containing the operator computation process. Then, call "aclnnGroupNormSwish" to execute the computation.
aclnnStatus aclnnGroupNormSwishGetWorkspaceSize(
const aclTensor *x,
const aclTensor *gamma,
const aclTensor *beta,
int64_t numGroups,
char *dataFormatOptional,
double eps,
bool activateSwish,
double swishScale,
const aclTensor *yOut,
const aclTensor *meanOut,
const aclTensor *rstdOut,
uint64_t *workspaceSize,
aclOpExecutor **executor)aclnnStatus aclnnGroupNormSwish(
void * workspace,
uint64_t workspaceSize,
aclOpExecutor *executor,
aclrtStream stream)aclnnGroupNormSwishGetWorkspaceSize
Parameters
Parameter Input/Output Description Instruction Data Type Data Format Dimension (shape) Non-contiguous Tensor x (aclTensor*) Input Target tensor for group normalization, which is x in the yOut formula. - Empty tensors are not supported.
- Dimensions range from 2D to 8D. Dimension 0 is N, and dimension 1 is C. Dimensions 0 and 1 of x must be greater than 0, and dimension 1 must be divisible by group.
FLOAT16, FLOAT, BFLOAT16 ND 2-8 √ gamma (aclTensor*) Input gamma parameter in group normalization, which is γ in the yOut formula. - Empty tensors are not supported.
- The number of elements must be the same as the 1st dimension of input x. gamma and beta must have the same data type, and the data type must be the same as that of x or be FLOAT.
FLOAT16, FLOAT, BFLOAT16 ND 1 √ beta (aclTensor*) Input Beta parameter in group normalization, corresponding to β in the formula for computing yOut. - Empty tensors are not supported.
- The number of elements must be the same as the 1st dimension of input x. gamma and beta must have the same data type, and the data type must be the same as that of x or be FLOAT.
FLOAT16, FLOAT, BFLOAT16 ND 2 √ numGroups (int64_t) Input The C dimension of input x is divided into group groups. group must be greater than 0. - - - - dataFormatOptional (char*) Input Data format. Recommended value: NCHW. - - - - eps (double) Input Value of eps in the formulas of yOut and rstdOut, used to prevent division by zero. Recommended value: 1e-5 - - - - activateSwish (bool) Input Whether swish computation is supported. If set to true, the swish computation is performed after the groupnorm computation. - - - - swishScale (double) Input Scale value used in the Swish computation. Recommended value: 1.0. - - - - yOut (aclTensor*) Output Group normalization result. Same data type and shape as x. FLOAT16, FLOAT, BFLOAT16 ND 2-8 x meanOut (aclTensor*) Output meanOut in the formula. Same data type as gamma, shape (N, numGroups), where N denotes the size of dimension 0 of x, and numGroups is a computation input, indicating that dimension 1 of input x is divided into numGroups groups. FLOAT16, FLOAT, BFLOAT16 ND 2 x rstdOut (aclTensor*) Output Reciprocal of the standard deviation of x after grouping. Same data type as gamma, shape (N, numGroups), where N is the size of the 0th dimension of x. numGroups is the computation input, indicating that the 1st dimension of input x is divided into group groups. FLOAT16, FLOAT, BFLOAT16 ND 2 x workspaceSize (uint64_t*) Output Returns the workspace size to be allocated on the device. - - - - - executor (aclOpExecutor**) Output Returns the op executor, which contains the computation flow of the operator. - - - - - 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 When x, gamma, beta, yOut, meanOut, or rstdOut is a null pointer. ACLNN_ERR_PARAM_INVALID 161002 The data types of x, gamma, beta, yOut, meanOut, or rstdOut are out of the supported range.
aclnnGroupNormSwish
Parameters
Parameter Input/Output Description workspace Input Workspace memory address allocated on the device. workspaceSize Input Workspace size allocated on the device. It is obtained by the first-phase API aclnnGroupNormSwishGetWorkspaceSize. executor Input Op executor that contains the operator computation process. stream Input Stream that specifies the task to execute. Return Value
aclnnStatus: return status code. For details, see aclnn Error Codes.
Constraints
- Deterministic computation
- aclnnGroupNormSwish 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_group_norm_swish.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. 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 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 host data to 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 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 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 inputs and outputs. Custom construction is required based on the API.
std::vector<int64_t> xShape = {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* xDeviceAddr = nullptr;
void* gammaDeviceAddr = nullptr;
void* betaDeviceAddr = nullptr;
void* outDeviceAddr = nullptr;
void* meanOutDeviceAddr = nullptr;
void* rstdOutDeviceAddr = nullptr;
aclTensor* x = nullptr;
aclTensor* gamma = nullptr;
aclTensor* beta = nullptr;
aclTensor* yOut = nullptr;
aclTensor* meanOut = nullptr;
aclTensor* rstdOut = nullptr;
std::vector<float> xHostData = {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 numGroups = 1;
double eps = 1e-5;
bool activateSwish = true;
double scale = 1.0;
char* dataFormatOptional = "NCHW";
// Create the x aclTensor.
ret = CreateAclTensor(xHostData, xShape, &xDeviceAddr, aclDataType::ACL_FLOAT, &x);
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, &yOut);
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 aclnnGroupNormSwish
ret = aclnnGroupNormSwishGetWorkspaceSize(x, gamma, beta, numGroups, dataFormatOptional, eps, activateSwish, scale, yOut, meanOut, rstdOut, &workspaceSize, &executor);
CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclnnGroupNormSwishGetWorkspaceSize 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 aclnnGroupNormSwish
ret = aclnnGroupNormSwish(workspaceAddr, workspaceSize, executor, stream);
CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclnnGroupNormSwish failed. ERROR: %d\n", ret); return ret);
// 4. (Boilerplate) Synchronize and 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 values and copy the results from device memory to host memory. Modify them based on 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[%ld] 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[%ld] 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[%ld] is: %f\n", i, rstdResultData[i]);
}
// 6. Release aclTensor and aclScalar. Modify it based on the specific API definition.
aclDestroyTensor(x);
aclDestroyTensor(gamma);
aclDestroyTensor(beta);
aclDestroyTensor(yOut);
aclDestroyTensor(meanOut);
aclDestroyTensor(rstdOut);
// 7. Release device resources. Modify it based on the specific API definition.
aclrtFree(xDeviceAddr);
aclrtFree(gammaDeviceAddr);
aclrtFree(betaDeviceAddr);
aclrtFree(outDeviceAddr);
aclrtFree(meanOutDeviceAddr);
aclrtFree(rstdOutDeviceAddr);
if (workspaceSize > 0) {
aclrtFree(workspaceAddr);
}
aclrtDestroyStream(stream);
aclrtResetDevice(deviceId);
aclFinalize();
return 0;
}