aclnnGroupedBiasAddGrad
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 : Not supported
Atlas training products : Not supported
Function
Description: Backward computation of GroupedBiasAdd. The extended API of this API is aclnnGroupedBiasAddGradV2.
Formula:
(1) When the optional input groupIdxOptional is provided:Where gradY is a 2D tensor, H indicates the dimension size of the last dimension of gradY, and G indicates the dimension size of dimension 0 of groupIdxOptional, that is, groupIdxOptional has G numbers. groupIdxOptional(j) denotes the size of the j-th number. After computation, out is a 2D tensor with shape (G, H).
(2) When the optional input groupIdxOptional is not provided:Where gradY is a 3D tensor, G, C, and H indicate the dimension sizes of dimensions 0-2 of gradY, respectively. After computation, out is a 2D tensor with shape (G, H).
Example:
(1) When the optional input groupIdxOptional is provided:
The shape of gradY is (1000, 30), and groupIdxOptional is (400, 600, 1000). gradY is divided into 3 groups, and the number of rows accumulated in each group is 400, 200, and 400 in sequence. After computation, the shape of out is (3, 30).
(2) When the optional input groupIdxOptional is not provided:
The shape of gradY is (10, 100, 30). gradY is divided into 10 groups, and the number of rows accumulated in each group is 100. After computation, the shape of out is (10, 30).
Function Prototype
Each operator uses a two-phase API. Call the "aclnnGroupedBiasAddGradGetWorkspaceSize" API first to obtain the workspace size required for computation and the executor that contains the operator computation process, and then call the "aclnnGroupedBiasAddGrad" API to perform 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, gradY in the formula. If the operator has optional input groupIdxOptional, shape only supports 2 dimensions.
If the operator has no optional input groupIdxOptional, shape only supports 3 dimensions.FLOAT, FLOAT16, BFLOAT16 ND 2-3 √ groupIdxOptional (aclTensor*) Input End position of each group, groupIdxOptional in the formula. Only 1D supported INT32, INT64 ND 1 √ out (aclTensor*) Output Gradient of bias, corresponding to out in the formula. The data type must be the same as that of gradY, and only 2D shapes are supported.
If the input is 3D (G, C, H), the output shape is (G, H);
if the input is 2D (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 to be side-applied on the Device. - - - - - executor (aclOpExecutor**) Output Returns the op executor, which contains the operator computation process. - - - - - Return value
aclnnStatus: return status code. For details, see aclnn Return 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 Returned when gradY or out is a null pointer. ACLNN_ERR_PARAM_INVALID 161002 The data type/dimension of gradY or out is out of the supported range. The dimension relationships of gradY, groupIdxOptional, and out do not match. The number of groups exceeds 2048.
aclnnGroupedBiasAddGrad
Parameters:
Parameter Input/Output Description workspace Input Workspace memory address side-applied on the Device. workspaceSize Input Size of the workspace side-applied on the Device, obtained by the first-phase API aclnnGroupedBiasAddGradGetWorkspaceSize. executor Input op executor, which contains the operator computation flow. stream Input Stream for task execution. Return value
aclnnStatus: return status 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 provided, ensure that the Tensor data is in ascending order and the last value equals the dimension size of dim 0 in gradY.
- When the optional input groupIdxOptional is present, the tensor values must not exceed the INT32 maximum value and must be non-negative.
Example
The following sample code is for reference only. For details about compilation and execution, 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 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 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 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 device/stream. For details, see the acl API manual.
// Set deviceId based on the actual device.
int32_t deviceId = 0;
aclrtStream stream;
auto ret = Init(deviceId, &stream);
// Handle check as needed.
CHECK_RET(ret == 0, LOG_PRINT("Init acl failed. ERROR: %d\n", ret); return ret);
// 2. Construct the input and output. Custom construction is required 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, 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. Change it to 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);
// 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 aclnnGroupedBiasAddGrad.
ret = aclnnGroupedBiasAddGrad(workspaceAddr, workspaceSize, executor, stream);
CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclnnGroupedBiasAddGrad failed. ERROR: %d\n", ret); return ret);
// 4. (boilerplate) Synchronously wait 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 and copy the result from the device side to the host side. Change it 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 the aclTensor. Change it based on the specific API definition.
aclDestroyTensor(gradY);
aclDestroyTensor(groupIdx);
aclDestroyTensor(out);
// 7. Release device resources. Change them based on the specific API definition.
aclrtFree(groupIdxDeviceAddr);
aclrtFree(outDeviceAddr);
aclrtFree(gradYDeviceAddr);
if (workspaceSize > 0) {
aclrtFree(workspaceAddr);
}
aclrtDestroyStream(stream);
aclrtResetDevice(deviceId);
aclFinalize();
return 0;
}