aclnnGroupedBiasAddGradV2
Applicable Products
| Product | Supported |
|---|---|
| √ | |
| √ | |
| √ | |
| × | |
| × | |
| × |
Function
Description: Performs backward computation of groupBiasAdd. This API extends the functionality of the aclnnGroupedBiasAddGrad API by adding the groupIdxType attribute, which specifies the type of groupIdx.
Formula:
(1) With Optional Input groupIdxOptional and groupIdxType set to 0:
(2) With Optional Input groupIdxOptional and groupIdxType set to 1:
Where, gradY has 2 dimensions, H Indicates the Dimension Size of the last dimension of gradY, G Indicates the Dimension Size of the 0th dimension of groupIdxOptional, i.e., groupIdxOptional has G numbers, groupIdxOptional(j) Indicates the size of the j-th number. After Computation, out has 2 dimensions with shape (G, H).
(3) Without Optional Input groupIdxOptional:
Here, gradY has 3 dimensions, with G, C, and H representing the sizes of the 0th to 2nd dimensions of gradY respectively. After computation, out is 2-dimensional with shape (G, H).
- Example:
(1) With optional input groupIdxOptional and groupIdxType set to 0:
gradY has shape (1000, 30), groupIdxOptional is (400, 600, 1000). gradY is divided into 3 groups, with the number of rows accumulated per group being 400, 200, and 400 respectively. After computation, out has shape (3, 30).
(2) With optional input groupIdxOptional and groupIdxType set to 1:
gradY has shape (1000, 30), groupIdxOptional is (400, 210, 390). gradY is divided into 3 groups, with the number of rows accumulated per group being 400, 210, and 390 respectively. After computation, out has shape (3, 30).
(3) Without optional input groupIdxOptional:
gradY has shape (10, 100, 30). gradY is divided into 10 groups, with the number of rows accumulated per group being 100 for all groups. After computation, out has shape (10, 30).
Function Prototype
Each operator is divided into a two-phase API. You must first call the "aclnnGroupedBiasAddGradV2GetWorkspaceSize" API to obtain the workspace size required for computation and the executor that includes the operator computation flow, and then call the "aclnnGroupedBiasAddGradV2" API to perform the computation.
aclnnStatus aclnnGroupedBiasAddGradV2GetWorkspaceSize(
const aclTensor *gradY,
const aclTensor *groupIdxOptional,
int64_t groupIdxType,
aclTensor *out,
uint64_t *workspaceSize,
aclOpExecutor **executor)aclnnStatus aclnnGroupedBiasAddGradV2(
void *workspace,
uint64_t workspaceSize,
aclOpExecutor *executor,
aclrtStream stream)aclnnGroupedBiasAddGradV2GetWorkspaceSize
Parameters
Parameter Input/Output Description Instruction Data Type Data Format Dimension (Shape) Non-Contiguous Tensor gradY Input Backward propagation gradient, i.e., gradY in the formula. With optional input groupIdxOptional, the shape supports only 2 dimensions. Without optional input groupIdxOptional, the shape supports only 3 dimensions. FLOAT, FLOAT16, BFLOAT16 ND 2-3 √ groupIdxOptional Input End position of each group, groupIdxOptional in the formula. - INT32, INT64 ND 1 √ groupIdxType groupIdxType Indicates the type of groupIdx. 0: Indicates that the value in groupIdxOptional is the end index of each group.
1: Indicates that the value in groupIdxOptional is the size of each group.- - - - out Output Gradient of bias, which is out in the formula. - Same as gradY. ND 2 × workspaceSize Output Returns the workspace size to be applied on the Device side. - - - - - executor Output Returns the operator executor, which includes the operator computation flow. - - - - - Return value
aclnnStatus: return code. For details, see aclnn Return Codes.
The first-phase API performs input parameter validation and returns an error in the following scenarios:
Return Value Error Code Description ACLNN_ERR_PARAM_NULLPTR 161001 The passed gradY or out is a null pointer. ACLNN_ERR_PARAM_INVALID 161002 The data type or dimension of gradY, groupIdxOptional, or out is not within the supported range. The dimension relationship among gradY, groupIdxOptional, and out does not match. The number of groups exceeds 2048. The value of groupIdxType passed in is not within the supported range.
aclnnGroupedBiasAddGradV2
Parameters:
Parameter Input/Output Description workspace Input Workspace memory address applied on the Device side. workspaceSize Input Workspace size applied on the Device side, obtained from the first-phase API aclnnGroupedBiasAddGradV2GetWorkspaceSize. executor Input Operator executor, which includes the operator computation flow. stream Input Specifies the stream for task execution. Return value
aclnnStatus: return code. For details, see aclnn Return Codes.
Constraints
Deterministic computation:
- aclnnGroupedBiasAddGradV2 defaults to a deterministic implementation.
groupIdxOptional supports a maximum of 2048 elements.
When the optional input groupIdxOptional is present, the tensor values must not exceed the INT32 maximum value and must be non-negative.
When the optional input groupIdxOptional is present and groupIdxType is 0, the tensor data must be in ascending order, and the last value must equal the size of the 0th dimension of gradY.
With optional input groupIdxOptional and groupIdxType set to 1, the sum of tensor values must equal the dimension size of the 0th dimension of gradY.
Example
The following provides example code for reference only. For details about compilation and running, see Compile and Run Samples.
#include <iostream>
#include <vector>
#include "acl/acl.h"
#include "aclnnop/aclnn_grouped_bias_add_grad.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 side.
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 data from the host side to the 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 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 API manual.
// Fill in the deviceId based on your actual device.
int32_t deviceId = 0;
aclrtStream stream;
auto ret = Init(deviceId, &stream);
// Handle the check based on your needs.
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> gradYShape = {40, 10};
std::vector<int64_t> groupIdxShape = {4};
std::vector<int64_t> outShape = {4, 10};
void* gradYDeviceAddr = nullptr;
void* groupIdxDeviceAddr = nullptr;
void* outDeviceAddr = nullptr;
aclTensor* gradY = nullptr;
aclTensor* groupIdx = nullptr;
aclTensor* out = nullptr;
std::vector<float> gradYHostData(400, 1.0);
std::vector<int32_t> groupIdxHostData = {5, 15, 10, 10};
std::vector<float> outHostData(40, 0.0);
int64_t groupIdxType = 1;
// Create the gradY aclTensor.
ret = CreateAclTensor(gradYHostData, gradYShape, &gradYDeviceAddr, aclDataType::ACL_FLOAT, &gradY);
CHECK_RET(ret == ACL_SUCCESS, return ret);
// Create the groupIdxOptional aclTensor.
ret = CreateAclTensor(groupIdxHostData, groupIdxShape, &groupIdxDeviceAddr, aclDataType::ACL_INT32, &groupIdx);
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);
// 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 aclnnGroupedBiasAddGradV2.
ret = aclnnGroupedBiasAddGradV2GetWorkspaceSize(gradY, groupIdx, groupIdxType, out, &workspaceSize, &executor);
CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclnnGroupedBiasAddGradV2GetWorkspaceSize failed. ERROR: %d\n", ret); return ret);
// Apply for 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 aclnnGroupedBiasAddGradV2.
ret = aclnnGroupedBiasAddGradV2(workspaceAddr, workspaceSize, executor, stream);
CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclnnGroupedBiasAddGradV2 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. Modify based on the specific API definition.
auto size = GetShapeSize(outShape);
std::vector<float> resultData(size, 0);
ret = aclrtMemcpy(resultData.data(), resultData.size() * sizeof(resultData[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("result[%ld] is: %f\n", i, resultData[i]);
}
// 6. Release aclTensor. Modify based on the specific API definition.
aclDestroyTensor(gradY);
aclDestroyTensor(groupIdx);
aclDestroyTensor(out);
// 7. Release device resources. Modify based on the specific API definition.
aclrtFree(groupIdxDeviceAddr);
aclrtFree(outDeviceAddr);
aclrtFree(gradYDeviceAddr);
if (workspaceSize > 0) {
aclrtFree(workspaceAddr);
}
aclrtDestroyStream(stream);
aclrtResetDevice(deviceId);
aclFinalize();
return 0;
}