aclnnAvgPool3dBackward
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 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 uses a two-phase API. Call the "aclnnAvgPool3dBackwardGetWorkspaceSize" API first to obtain the workspace size required for computation and the executor containing the operator computation process. 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 of the forward process, corresponding to input in the formula. - FLOAT32, FLOAT16, BFLOAT16 ND 4-5 √ kernelSize Input Pooling window size, which is k in the formula. The length is 1 (KD = KH = KW) or 3 (KD, KH, KW), and each value must be greater than 0. INT64 - - - stride Input Stride of the pooling operation, corresponding to strides in the formula. The length is 0 (consistent with kernelSize), 1 (SD = SH = SW), or 3 (SD, SH, SW). The value must be greater than 0. INT64 - - - padding Input Number of layers padded with zeros along the D, H, and W dimensions of the input, that is, paddings in the formula. Length of 1 (PD = PH = PW) or 3 (PD, PH, PW), with values in the range [0, kernelSize/2]. INT64 - - - ceilMode Input Whether the shape of the derived output out is rounded up. - BOOL - - - countIncludePad Input Whether the zeros filled by padding are included when computing average pooling. - BOOL - - - divisorOverride Input Divisor used to compute the average. When the value is 0, this attribute does not take effect. INT64 - - - Output Output The output tensor. Same as the shape of self FLOAT32, FLOAT16, BFLOAT16 ND 4-5 √ workspaceSize Output Returns the size of the workspace to be allocated on the Device. - - - - - executor Output Returns the op executor, which contains the operator computation process. - - - - - Return value:
aclnnStatus: return status. 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 The gradOutput, self, kernelSize, padding, or output is a null pointer. ACLNN_ERR_PARAM_INVALID 161002 The data types or dimensions of gradOutput, self, and output are not within the supported ranges. The dimensions of kernelSize, stride, or padding are not within the supported ranges. A dimension value of 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 corresponding kernelSize value, for example, paddingH=2 and kernelSizeH=2, where paddingH > kernelSizeH*1/2. The shape of output is inconsistent with the shape of self. The shape of gradOutput calculated based on average pooling semantics is inconsistent with the shape of gradOutput passed to the API.
aclnnAvgPool3dBackward
Parameters:
Parameter Input/Output Description workspace Input Workspace memory address allocated on the device. workspaceSize Input Workspace size allocated on the device, obtained by the first-phase API aclnnAvgPool3dBackwardGetWorkspaceSize. executor Input Op executor that contains the operator computation flow. stream Input Stream for executing tasks. Return value
aclnnStatus: return status code. For details, see aclnn Error Codes.
Constraints
- Deterministic computation:
Atlas A3 products ,Atlas A2 products : aclnnAvgPool3dBackward defaults to a non-deterministic implementation, and determinism can be enabled through aclrtCtxSetSysParamOpt.
Ascend 950PR&950DT products : aclnnAvgPool3dBackward defaults to a deterministic implementation.
Example
The following sample code is for reference only. For details about compilation and execution, 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. 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 allocate device 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 data to 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 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 an aclTensor.
*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 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 as required by the API.
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 selfshape 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 paddings aclIntArray
aclIntArray *padding = aclCreateIntArray(paddingDims.data(), 3);
// 3. Call the CANN operator library API, and change 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);
// 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 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 the 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 on the device memory to the host memory. Modify the code 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 the code based on the specific API definition.
aclDestroyTensor(gradOutput);
aclDestroyTensor(selfInput);
aclDestroyTensor(output);
aclDestroyIntArray(kernelSize);
aclDestroyIntArray(stride);
aclDestroyIntArray(padding);
// 7. Release device resources. Modify the code based on the specific API definition.
aclrtFree(gradOutputDeviceAddr);
aclrtFree(selfAddr);
aclrtFree(outputAddr);
if (workspaceSize > 0) {
aclrtFree(workspaceAddr);
}
aclrtDestroyStream(stream);
aclrtResetDevice(deviceId);
aclFinalize();
return 0;
}