aclnnAvgPool3dBackward
Applicable Products
| Product | Supported |
|---|---|
| √ | |
| √ | |
| √ | |
| × | |
| × | |
| × |
Function
Description: Backward propagation of 3D average pooling. Computes the input gradient of the forward 3D average pooling operation.
Formula:
The relationship between the output data input(), gradient gradOutput(), pooling stride(), and pooling kernel size kernelSize() during backward propagation is:
If ceilMode is true and the following condition is met:
then the shape of D_{out} needs to be reduced by 1. The same applies to H_{out} and W_{out}.
Function Prototype
Each operator is divided into a two-phase API. First call the "aclnnAvgPool3dBackwardGetWorkspaceSize" API to obtain the workspace size required for computation and the executor that contains the operator computation process, and then call the "aclnnAvgPool3dBackward" API to perform the computation.
aclnnStatus aclnnAvgPool3dBackwardGetWorkspaceSize(
const aclTensor *gradOutput,
const aclTensor *self,
const aclIntArray *kernelSize,
const aclIntArray *stride,
const aclIntArray *padding,
bool ceilMode,
bool countIncludePad,
int64_t divisorOverride,
aclTensor *output,
uint64_t *workspaceSize,
aclOpExecutor **executor)aclnnStatus aclnnAvgPool3dBackward(
void *workspace,
uint64_t workspaceSize,
aclOpExecutor *executor,
const aclrtStream stream)aclnnAvgPool3dBackwardGetWorkspaceSize
Parameters:
Parameter Input/Output Description Instruction Data Type Data Format Dimension (Shape) Non-Contiguous Tensor gradOutput Input Represents the input gradient. - FLOAT32, FLOAT16, BFLOAT16 ND 4-5 √ self Input. Input in the forward process, corresponding to input in the formula. - FLOAT32, FLOAT16, BFLOAT16 ND 4-5 √ kernelSize Input Pooling window size, denoted as k in the formula. Length of 1 (KD = KH = KW) or 3 (KD, KH, KW), with values greater than 0. INT64 - - - stride Input Stride of the pooling operation, i.e., strides in the formula. Length of 0 (the value is consistent with kernelSize), 1 (SD = SH = SW), or 3 (SD, SH, SW). The value must be greater than 0. INT64 - - - padding Input Specifies the number of zero-padding layers in the D, H, and W dimensions of the input, i.e., paddings in the formula. The length is 1 (PD = PH = PW) or 3 (PD, PH, PW), with values in the range [0, kernelSize/2]. INT64 - - - ceilMode Input Whether to round up the shape of the derived output out. - BOOL - - - countIncludePad Input Specifies whether to include zero-padding values when computing the average pooling. - BOOL - - - divisorOverride Input Divisor used for averaging. When the value is 0, this attribute does not take effect. INT64 - - - output output Output tensor. Shape is the same as the input self. FLOAT32, FLOAT16, BFLOAT16 ND 4-5 √ workspaceSize Output Returns the workspace size to be requested on the Device side. - - - - - executor Output Returns the operator executor, which contains the operator computation flow. - - - - - 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 The passed gradOutput, self, kernelSize, padding, or output is a null pointer. ACLNN_ERR_PARAM_INVALID 161002 The data type or dimension of the input gradOutput, self, and output is not within the supported range. The dimension of the input kernelSize, stride, or padding is not within the supported range. A dimension value of the input kernelSize or stride is less than or equal to 0, or a dimension value of padding is less than 0. The padding attribute exceeds half of the kernelSize at the corresponding position, for example, paddingH = 2, kernelSizeH = 2, and paddingH > kernelSizeH * 1/2. The shape of output is inconsistent with the shape of self. The shape of gradOutput computed based on average pooling semantics is inconsistent with the shape of gradOutput passed to the API.
aclnnAvgPool3dBackward
Parameters:
Parameter Input/Output Description workspace Input The workspace memory address applied for on the Device side. workspaceSize Input The workspace size applied for on the Device side, obtained from the first-phase API aclnnAvgPool3dBackwardGetWorkspaceSize. executor Input Operator executor, which contains the operator computation flow. stream Input Specifies the stream for task execution. Return value
aclnnStatus: return code. For details, see aclnn Return Code.
Constraints
Deterministic computation:
- aclnnAvgPool3dBackward defaults to a non-deterministic implementation. Deterministic computation can be enabled through aclrtCtxSetSysParamOpt.
Example
The following provides example code for reference only. For details about compilation and running, see Compile and Run Samples.
#include <cstdio>
#include <iostream>
#include <vector>
#include "acl/acl.h"
#include "aclnnop/aclnn_avgpool3d_backward.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 shapeSize = 1;
for (auto i : shape) {
shapeSize *= i;
}
return shapeSize;
}
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, aclFormat Format = aclFormat::ACL_FORMAT_ND) {
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);
// Copy data from the host to the device memory by calling aclrtMemcpy.
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];
}
// Create an aclTensor by calling the aclCreateTensor API.
*tensor = aclCreateTensor(shape.data(), shape.size(), dataType, strides.data(), 0, Format, 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);
CHECK_RET(ret == ACL_SUCCESS, 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> gradOutputShape = {1, 16, 1, 1, 1};
std::vector<int64_t> selfShape = {1, 16, 4, 4, 4};
std::vector<int64_t> kernelDims = {4, 4, 4};
std::vector<int64_t> strideDims = {1, 1, 1};
std::vector<int64_t> paddingDims = {0, 0, 0};
std::vector<int64_t> outputShape = {1, 16, 4, 4, 4};
bool ceilMode = false;
int64_t divisorOverride = 0;
bool countIncludePad = false;
void* gradOutputDeviceAddr = nullptr;
void* selfAddr = nullptr;
void* outputAddr = nullptr;
aclTensor* gradOutput = nullptr;
aclTensor* selfInput = nullptr;
aclTensor* output = nullptr;
std::vector<float> gradOutputHostData(GetShapeSize(gradOutputShape), 1);
std::vector<float> selfHostData(GetShapeSize(selfShape), 1);
std::vector<float> outputHostData(GetShapeSize(outputShape), 1);
// Create the gradOutput aclTensor.
ret = CreateAclTensor(gradOutputHostData, gradOutputShape, &gradOutputDeviceAddr, aclDataType::ACL_FLOAT, &gradOutput);
CHECK_RET(ret == ACL_SUCCESS, return ret);
// Create the inputshape aclTensor.
ret = CreateAclTensor(selfHostData, selfShape, &selfAddr, aclDataType::ACL_FLOAT,
&selfInput);
CHECK_RET(ret == ACL_SUCCESS, return ret);
// Create the output.
ret = CreateAclTensor(outputHostData, outputShape, &outputAddr, aclDataType::ACL_FLOAT, &output);
CHECK_RET(ret == ACL_SUCCESS, return ret);
// Create the kernel aclIntArray.
aclIntArray *kernelSize = aclCreateIntArray(kernelDims.data(), 3);
// Create the stride aclIntArray.
aclIntArray *stride = aclCreateIntArray(strideDims.data(), 3);
// Create the padding aclIntArray.
aclIntArray *padding = aclCreateIntArray(paddingDims.data(), 3);
// 3. Call the CANN operator library API. Modify it to the specific API name.
uint64_t workspaceSize = 0;
aclOpExecutor* executor;
// Call the first-phase API of aclnnAvgPool3dBackward.
ret = aclnnAvgPool3dBackwardGetWorkspaceSize(gradOutput,
selfInput,
kernelSize,
stride,
padding,
ceilMode,
countIncludePad,
divisorOverride,
output,
&workspaceSize,
&executor);
CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclnnAvgPool3dBackwardGetWorkspaceSize 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 aclnnAvgPool3dBackward.
ret = aclnnAvgPool3dBackward(workspaceAddr, workspaceSize, executor, stream);
CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclnnAvgPool3dBackward failed. ERROR: %d\n", ret); return ret);
// 4. (Boilerplate) Synchronize and 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(outputShape);
std::vector<float> outData(size, 0);
ret = aclrtMemcpy(outData.data(), outData.size() * sizeof(outData[0]), outputAddr,
size * sizeof(outData[0]), 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("out result[%ld] is: %f\n", i, outData[i]);
}
// 6. Release aclTensor and aclScalar. Modify based on the specific API definition.
aclDestroyTensor(gradOutput);
aclDestroyTensor(selfInput);
aclDestroyTensor(output);
aclDestroyIntArray(kernelSize);
aclDestroyIntArray(stride);
aclDestroyIntArray(padding);
// 7. Release device resources. Modify based on the specific API definition.
aclrtFree(gradOutputDeviceAddr);
aclrtFree(selfAddr);
aclrtFree(outputAddr);
if (workspaceSize > 0) {
aclrtFree(workspaceAddr);
}
aclrtDestroyStream(stream);
aclrtResetDevice(deviceId);
aclFinalize();
return 0;
}