aclnnGroupNormSiluV2
Applicable Products
| Product | 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: $$ \left{ \begin{array} {rcl} out& &= \frac{x - E[x]}{\sqrt{Var[x] + eps}} * \gamma + \beta \ meanOut& &= E[x]\ rstdOut& &= \frac{1}{\sqrt{Var[x] + eps}}\ \end{array} \right. $$
- Silu:
When activateSilu is True, SiLU is computed, where x in the SiLU formula is the out obtained from the GroupNorm formula.Function Prototype
Each operator is divided into a two-phase API. The "aclnnGroupNormSiluV2GetWorkspaceSize" API must be called first to obtain the workspace size required for computation and the executor that encapsulates the operator computation flow. Then, the "aclnnGroupNormSiluV2" API is called to perform 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. The data type is the same as that of self or is 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 the same as self or FLOAT, and the number of elements must be the same as the size of the second dimension of 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 be divisible by the first dimension of self. - - - - eps (double) Input The eps in the formula. eps must be greater than 0. - - - - activateSilu (bool) Output Whether to enable SiLU computation. - - - - - 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 side. - - - - - executor (aclOpExecutor**) Output Returns the operator 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 those of 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 Returns 161001 if the passed parameter is a mandatory input, output, or mandatory attribute and is a null pointer. ACLNN_ERR_PARAM_INVALID 161002 The data type of the input or output is not within the supported range. The input or output parameters do not meet the constraints specified in Parameters.
aclnnGroupNormSiluV2
Parameters
Parameter Input/Output Description workspace Input Address of the workspace memory applied for on the Device side. workspaceSize Input Size of the workspace applied for on the Device side, obtained through the first-phase API aclnnGroupNormSiluV2GetWorkspaceSize. executor Input Operator executor, which contains the operator computation flow. stream Input Specifies the stream on which the task is executed. Return Value
aclnnStatus: return code. For details, see aclnn Return Code.
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 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 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-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. See the acl external API list.
// Enter the deviceId based on your 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 inputs and outputs. Custom construction is required 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 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 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 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 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 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 the 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 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 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 aclnnGroupNormSiluV2.
ret = aclnnGroupNormSiluV2(workspaceAddr, workspaceSize, executor, stream);
CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclnnGroupNormSiluV2 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 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 based on the specific API definition.
aclDestroyTensor(self);
aclDestroyTensor(gamma);
aclDestroyTensor(beta);
aclDestroyTensor(out);
aclDestroyTensor(meanOut);
aclDestroyTensor(rstdOut);
// 7. Release device resources. Modify 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;
}