aclnnGroupNormSilu
Applicable Products
| Product | 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 is divided into a two-phase API. The "aclnnGroupNormSiluGetWorkspaceSize" API must be called first to obtain the workspace size required for computation and the executor that encapsulates the operator computation flow. Then, the "aclnnGroupNormSilu" API is called 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 the first dimension is N and the second dimension is C. √ gamma (aclTensor*) Input γ in the formula. The data type must be the same as self or FLOAT, and the number of elements must be the same as the size of the second dimension of the input self. FLOAT16, FLOAT, BFLOAT16 ND 1 √ beta (aclTensor*) Input β in the formula. The data type must be consistent with self or be FLOAT, and the number of elements must be the same as the size of the second dimension of the input self. FLOAT16, FLOAT, BFLOAT16 ND 1 √ group (int64_t) Input Divides the second dimension of the input self into group groups. group must be divisible by the first dimension of self. - - - - eps (double) Input The eps in the formula. eps must be greater than 0. - - - - out (aclTensor*) Output out in the formula. The data type is the same as self. FLOAT16, FLOAT, BFLOAT16 ND Same as self. x meanOut (aclTensor*) Output meanOut in the formula. The data type is the same as self, and N in the shape is the size of the first dimension of self. FLOAT16, FLOAT, BFLOAT16 ND (N, group) x rstdOut (aclTensor*) Output rstdOut in the formula. The data type is the same as self, and N in the 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 allocated on the Device. - - - - - executor(aclOpExecutor**) output Returns the op executor, which contains the operator computation flow. - - - - - Atlas inference products : The data type does not support BFLOAT16.Ascend 950PR/Ascend 950DT : The data types of meanOut and rstdOut must be the same as gamma and beta.
Return Value
aclnnStatus: return code. For details, see aclnn Return Code.
The first-phase API performs input parameter validation and returns an error in the following scenarios:
Return Code Error Code Description ACLNN_ERR_PARAM_NULLPTR 161001 If a required input, output, or required attribute parameter is a null pointer, 161001 is returned. ACLNN_ERR_PARAM_INVALID 161002 The data types of the input and output are not within the supported range. The input and output parameters do not meet the constraints in Parameters.
aclnnGroupNormSilu
Parameters
Parameter Input/Output Description workspace Input Workspace memory address applied for on the Device side. workspaceSize Input Workspace size applied for on the Device side, obtained from the first-phase API aclnnGroupNormSiluGetWorkspaceSize. executor Input Operator executor, which contains the operator computation flow. stream Input Specifies the stream for executing the task. Return Value
aclnnStatus: return code. For details, see aclnn Return Code.
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 provides example code for reference only. For details about compilation and running, see Compile and Run Samples.
#include <iostream>
#include <memory>
#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 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 apply for 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 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 external ACL API list.
// Set deviceId based on the actual device.
int32_t deviceId = 0;
aclrtStream stream;
auto ret = Init(deviceId, &stream);
// Handle the check based on your requirements.
CHECK_RET(ret == 0, LOG_PRINT("Init acl failed. ERROR: %d\n", ret); return ret);
// 2. Construct the input and output. 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);
std::unique_ptr<aclTensor, aclnnStatus (*)(const aclTensor *)> selfTensorPtr(self, aclDestroyTensor);
std::unique_ptr<void, aclError (*)(void *)> selfDeviceAddrPtr(selfDeviceAddr, aclrtFree);
CHECK_RET(ret == ACL_SUCCESS, return ret);
// Create the gamma aclTensor.
ret = CreateAclTensor(gammaHostData, gammaShape, &gammaDeviceAddr, aclDataType::ACL_FLOAT, &gamma);
std::unique_ptr<aclTensor, aclnnStatus (*)(const aclTensor *)> gammaTensorPtr(gamma, aclDestroyTensor);
std::unique_ptr<void, aclError (*)(void *)> gammaDeviceAddrPtr(gammaDeviceAddr, aclrtFree);
CHECK_RET(ret == ACL_SUCCESS, return ret);
// Create the beta aclTensor.
ret = CreateAclTensor(betaHostData, betaShape, &betaDeviceAddr, aclDataType::ACL_FLOAT, &beta);
std::unique_ptr<aclTensor, aclnnStatus (*)(const aclTensor *)> betaTensorPtr(beta, aclDestroyTensor);
std::unique_ptr<void, aclError (*)(void *)> betaDeviceAddrPtr(betaDeviceAddr, aclrtFree);
CHECK_RET(ret == ACL_SUCCESS, return ret);
// Create the out aclTensor.
ret = CreateAclTensor(outHostData, outShape, &outDeviceAddr, aclDataType::ACL_FLOAT, &out);
std::unique_ptr<aclTensor, aclnnStatus (*)(const aclTensor *)> outTensorPtr(out, aclDestroyTensor);
std::unique_ptr<void, aclError (*)(void *)> outDeviceAddrPtr(outDeviceAddr, aclrtFree);
CHECK_RET(ret == ACL_SUCCESS, return ret);
// Create the meanOut aclTensor.
ret = CreateAclTensor(meanOutHostData, meanOutShape, &meanOutDeviceAddr, aclDataType::ACL_FLOAT, &meanOut);
std::unique_ptr<aclTensor, aclnnStatus (*)(const aclTensor *)> meanOutTensorPtr(meanOut, aclDestroyTensor);
std::unique_ptr<void, aclError (*)(void *)> meanOutDeviceAddrPtr(meanOutDeviceAddr, aclrtFree);
CHECK_RET(ret == ACL_SUCCESS, return ret);
// Create rstdOut aclTensor.
ret = CreateAclTensor(rstdOutHostData, rstdOutShape, &rstdOutDeviceAddr, aclDataType::ACL_FLOAT, &rstdOut);
std::unique_ptr<aclTensor, aclnnStatus (*)(const aclTensor *)> rstdOutTensorPtr(rstdOut, aclDestroyTensor);
std::unique_ptr<void, aclError (*)(void *)> rstdOutDeviceAddrPtr(rstdOutDeviceAddr, aclrtFree);
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);
// Apply for 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) Synchronously wait for 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 memory to the host memory. Modify this 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 this based on the specific API definition.
aclDestroyTensor(self);
aclDestroyTensor(gamma);
aclDestroyTensor(beta);
aclDestroyTensor(out);
aclDestroyTensor(meanOut);
aclDestroyTensor(rstdOut);
// 7. Release device resources. Modify this 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;
}