aclnnGroupedBiasAddGrad
Applicable Products
| Product | Supported |
|---|---|
| √ | |
| √ | |
| √ | |
| × | |
| × | |
| × |
Function
Description: Backward computation of GroupedBiasAdd. The extended API of this API is aclnnGroupedBiasAddGradV2.
Formula:
(1) With optional input groupIdxOptional:
$$ out(G,H) = \begin{cases} \sum_{i=groupIdxOptional(j-1)}^{groupIdxOptional(j)} gradY(i, H), & 1 \leq j \leq G-1 \\ \sum_{i=0}^{groupIdxOptional(j)} gradY(i, H), & j = 0 \end{cases} $$Where gradY has 2 dimensions, H represents the size of the last dimension of gradY, G represents the size of the 0th dimension of groupIdxOptional, i.e., groupIdxOptional has G numbers, and groupIdxOptional(j) represents the value of the j-th number. After computation, out is 2-dimensional with shape (G, H).
(2) Without optional input groupIdxOptional:$$ out(G, H) = \sum_{i=0}^{C} gradY(G, i, H) $$Where gradY has 3 dimensions, G, C, and H represent 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:
The shape of gradY is (1000, 30), and 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, the shape of out is (3, 30).
(2) Without optional input groupIdxOptional:
The shape of gradY is (10, 100, 30). gradY is divided into 10 groups, with the number of rows accumulated per group being 100. After computation, the shape of out is (10, 30).
Function Prototype
Each operator is divided into a two-phase API. The "aclnnGroupedBiasAddGradGetWorkspaceSize" API must be called first to obtain the workspace size required for computation and the executor that includes the operator computation flow. Then, the "aclnnGroupedBiasAddGrad" API is called to perform the computation.
aclnnStatus aclnnGroupedBiasAddGradGetWorkspaceSize(
const aclTensor* gradY,
const aclTensor* groupIdxOptional,
aclTensor* out,
uint64_t* workspaceSize,
aclOpExecutor** executor)aclnnStatus aclnnGroupedBiasAddGrad(
void* workspace,
uint64_t workspaceSize,
aclOpExecutor* executor,
aclrtStream stream)aclnnGroupedBiasAddGradGetWorkspaceSize
Parameters:
Parameter Input/Output Description Instruction Data Type Data Format Dimension (Shape) Non-contiguous Tensor gradY (aclTensor*) Input Backward propagation gradient, i.e., gradY in the formula. With the optional input groupIdxOptional, only supports a 2D shape.
Without the optional input groupIdxOptional, only supports a 3D shape.FLOAT, FLOAT16, BFLOAT16 ND 2-3 √ groupIdxOptional (aclTensor*) Input End position of each group, i.e., groupIdxOptional in the formula. Only supports one dimension. INT32, INT64 ND 1 √ out (aclTensor*) Output Gradient of bias, corresponding to out in the formula. The data type must be consistent with that of gradY, and the shape only supports 2D.
If the input is three-dimensional (G, C, H), the output shape is (G, H);
if the input is two-dimensional (GB, H) and group_idx is (G), the output shape is (G, H).FLOAT, FLOAT16, BFLOAT16 ND 2 √ workspaceSize (uint64_t*) Output Returns the workspace size applied on the Device side. - - - - - executor (aclOpExecutor**) Output Returns the op 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 Code Error Code Description ACLNN_ERR_PARAM_NULLPTR 161001 The input gradY or out is a null pointer. ACLNN_ERR_PARAM_INVALID 161002 The data type or dimension of gradY 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.
aclnnGroupedBiasAddGrad
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 by the first-phase API aclnnGroupedBiasAddGradGetWorkspaceSize. executor Input Operator executor, which includes the computation process of the operator. stream Input Specifies the stream for task execution. Return value
aclnnStatus: return code. For details, see aclnn Return Codes.
Constraints
Deterministic computation:
- aclnnGroupedBiasAddGrad defaults to a deterministic implementation. groupIdxOptional supports a maximum of 2048 elements. When the optional input groupIdxOptional is present, the tensor data must be in ascending order, and the last value must equal the dimension size of the 0th dimension of gradY. When the optional input groupIdxOptional is present, the tensor values must not exceed the maximum INT32 value and must be non-negative.
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 apply for 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);
// Compute 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 aclCreateTensor 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. Refer to 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 as needed.
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 interface.
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, 30, 40};
std::vector<float> outHostData(40, 0.0);
// 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 with the specific API.
uint64_t workspaceSize = 0;
aclOpExecutor* executor;
// Call the first-phase API of aclnnGroupedBiasAddGrad.
ret = aclnnGroupedBiasAddGradGetWorkspaceSize(gradY, groupIdx, out, &workspaceSize, &executor);
CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclnnGroupedBiasAddGradGetWorkspaceSize 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 aclnnGroupedBiasAddGrad.
ret = aclnnGroupedBiasAddGrad(workspaceAddr, workspaceSize, executor, stream);
CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclnnGroupedBiasAddGrad failed. ERROR: %d\n", ret); return ret);
// 4. (Boilerplate) Synchronize and wait for task execution to finish.
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;
}