aclnnGroupNormSiluV2
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 out, mean meanOut, reciprocal of standard deviation rstdOut, and the SiLU output for the input self.
- Formula:
GroupNorm:
Let denote the mean of , and denote the variance of . Then
Silu:
When activateSilu is True, Silu is computed. In this case, x in the Silu Formula is the out obtained from the GroupNorm Formula.
Function Prototype
Each operator uses a two-phase API. Call "aclnnGroupNormSiluV2GetWorkspaceSize" first to obtain the workspace size required for computation and the executor that contains the operator computation process, and then call "aclnnGroupNormSiluV2" to execute the computation.
aclnnStatus aclnnGroupNormSiluV2GetWorkspaceSize(
const aclTensor* self,
const aclTensor* gamma,
const aclTensor* beta,
int64_t group,
double eps,
bool activateSilu,
aclTensor* out,
aclTensor* meanOut,
aclTensor* rstdOut,
uint64_t* workspaceSize,
aclOpExecutor** executor);aclnnStatus aclnnGroupNormSiluV2(
void * workspace,
uint64_t workspaceSize,
aclOpExecutor *executor,
aclrtStream stream)aclnnGroupNormSiluV2GetWorkspaceSize
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 the first dimension is N and the second dimension is C √ gamma (aclTensor*) Input γ in the Formula. Data type must remain consistent with that of self or be FLOAT. 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 remain consistent with that of self or be FLOAT. 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 Indicates that the second dimension of the input self is divided into group groups. group must evenly divide the first dimension of self. - - - - eps (double) Input eps in the formula. eps must be greater than 0. - - - - activateSilu (bool) Input Whether to enable silu computation. - - - - - out (aclTensor*) Output The out in the formula. The data type remains consistent with self. FLOAT16, FLOAT, BFLOAT16 ND Same as self x meanOut (aclTensor*) Output meanOut in the Formula. The data type remains consistent with self. N in shape is the size of the first dimension of self. FLOAT16, FLOAT, BFLOAT16 ND (N, group) x rstdOut (aclTensor*) Output rstdOut in the Formula. Data types remain consistent with self, and N in shape is the size of the first dimension of self. FLOAT16, FLOAT, BFLOAT16 ND (N, group) x workspaceSize (uint64_t*) Output Returns the workspace size to be requested on the device. - - - - - executor (aclOpExecutor**) Output Returns the op executor that contains the calculation flow of the operator. - - - - - Atlas inference products : The BFLOAT16 data type is not supported.
Ascend 950PR&950DT products : gamma or beta can be empty. If gamma or beta is not empty, the data types of meanOut and rstdOut must be the same as those of gamma or beta; if 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 required input, output, or attribute is a null pointer, 161001 is returned. ACLNN_ERR_PARAM_INVALID 161002 The data type of an input or output is not within the supported range. The input and output parameters do not meet the constraints in Parameters.
aclnnGroupNormSiluV2
Parameters
Parameter Input/Output Description workspace Input Workspace memory address allocated on the device. workspaceSize Input Workspace size allocated on the device, obtained by calling the first-phase API aclnnGroupNormSiluV2GetWorkspaceSize. executor Input Op executor that contains the operator computation flow. stream Input Stream used to execute tasks. Return Value
aclnnStatus: return status code. For details, see aclnn Error Codes.
Constraints
Deterministic computation: aclnnGroupNormSiluV2 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 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 host-side 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);
// Compute 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 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 required.
CHECK_RET(ret == 0, LOG_PRINT("Init acl failed. ERROR: %d\n", ret); return ret);
// 2. Construct inputs and outputs. The construction must be customized 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;
bool activateSilu = true;
// 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 out aclTensor.
ret = CreateAclTensor(outHostData, outShape, &outDeviceAddr, aclDataType::ACL_FLOAT, &out);
CHECK_RET(ret == ACL_SUCCESS, return ret);
// Create meanOut aclTensor.
ret = CreateAclTensor(meanOutHostData, meanOutShape, &meanOutDeviceAddr, aclDataType::ACL_FLOAT, &meanOut);
CHECK_RET(ret == ACL_SUCCESS, return ret);
// Create 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 aclnnGroupNormSiluV2.
ret = aclnnGroupNormSiluV2GetWorkspaceSize(self, gamma, beta, group, eps, activateSilu, out, meanOut, rstdOut, &workspaceSize, &executor);
CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclnnGroupNormSiluV2GetWorkspaceSize failed. ERROR: %d\n", ret); return ret);
// Allocate device memory based on the workspaceSize computed 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 aclnnGroupNormSiluV2.
ret = aclnnGroupNormSiluV2(workspaceAddr, workspaceSize, executor, stream);
CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclnnGroupNormSiluV2 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 host memory. Modify it based on the interface definition of the specific API.
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 based on the interface definition of the specific API.
aclDestroyTensor(self);
aclDestroyTensor(gamma);
aclDestroyTensor(beta);
aclDestroyTensor(out);
aclDestroyTensor(meanOut);
aclDestroyTensor(rstdOut);
// 7. Release device resources, which need to be modified based on 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;
}