aclnnAvgPool3d
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 : Supported
Atlas training products : Not supported
Function
- Description: Performs 3D average pooling on the input tensor with a window of and a stride of , where is kernelSize, representing the size of the pooling window, and is stride, representing the stride of the pooling operation.
- Formula: The relationship between the input(), output out(), pooling stride(), and pooling window size kernelSize() 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. First, call the "aclnnAvgPool3dGetWorkspaceSize" API to obtain the workspace size required for the computation and the executor containing the operator computation process. Then, call the "aclnnAvgPool3d" API to perform the computation.
aclnnStatus aclnnAvgPool3dGetWorkspaceSize(
const aclTensor *self,
const aclIntArray *kernelSize,
const aclIntArray *stride,
const aclIntArray *padding,
bool ceilMode,
bool countIncludePad,
int64_t divisorOverride,
aclTensor *out,
uint64_t *workspaceSize,
aclOpExecutor **executor)aclnnStatus aclnnAvgPool3d(
void *workspace,
uint64_t workspaceSize,
aclOpExecutor *executor,
aclrtStream stream)aclnnAvgPool3dGetWorkspaceSize
Parameters:
Parameter Input/Output Description Instruction Data Type Data Format Dimension (shape) Non-contiguous Tensor self Input Input in the forward process, corresponding to input in the formula. - FLOAT32, FLOAT16, BFLOAT16 ND 4-5 √ kernelSize Input Pooling window size, corresponding to k in the formula. Length of 1 (KD = KH = KW) or 3 (KD, KH, KW). The value must be greater than 0 and less than or equal to the size of self in the corresponding DHW dimension. INT64 - - - stride Input Stride of the pooling operation, corresponding to stride in the formula. Length of 0 (the value is the same as that of kernelSize), 1 (SD = SH = SW), or 3 (SD, SH, SW). When the length is 1 or 3, the value must be greater than 0. INT64 - - - padding Input Number of zero-padding layers on the D, H, and W dimensions of the input, corresponding to 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 deduced output out is rounded up. - BOOL - - - countIncludePad Input Whether the 0s padded by padding are included when computing average pooling. - BOOL - - - divisorOverride Input Divisor used to compute the average. If the value is 0, this attribute does not take effect. INT64 - - - out Output Output tensor. - 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 flow. - - - - - Atlas inference products : The data types of self and out do not support BFLOAT16.
Return value:
aclnnStatus: return status code. For details, see aclnn Error 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 Any of self, kernelSize, stride, padding, or out is a null pointer. ACLNN_ERR_PARAM_INVALID 161002 The data type or data format of self or out is out of the supported range. The data types or data formats of the input self and out are inconsistent. A dimension of the input stride is less than or equal to 0, or the value of padding is not within the range [0, kernelSize/2]. A dimension of the input kernelSize is less than or equal to 0 or greater than the value of self in the corresponding dimension. The length of the input kernelSize or padding is neither 1 nor 3, and the length of stride is neither 0, 1, nor 3. The output shape calculated based on the average pooling semantics is inconsistent with the output shape passed to the API.
aclnnAvgPool3d
Parameters:
Parameter Input/Output Description workspace Input Memory address of the workspace allocated on the device. workspaceSize Input Size of the workspace allocated on the device, obtained by calling the first-phase API aclnnAvgPool3dGetWorkspaceSize. executor Input Executor of the op, containing the operator computation flow. stream Input Stream for task execution. Return value
aclnnStatus: return status code. For details, see aclnn Error Codes.
Constraints
- Deterministic computation:
- aclnnAvgPool3d 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 <memory>
#include <vector>
#include "acl/acl.h"
#include "aclnnop/aclnn_avgpool3d.h"
#define CHECK_RET(cond, return_expr) \
do { \
if (!(cond)) { \
return_expr; \
} \
} while (0)
#define CHECK_FREE_RET(cond, return_expr) \
do { \
if (!(cond)) { \
Finalize(deviceId, stream); \
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) {
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);
// Compute the stride of the contiguous tensor.
std::vector<int64_t> stride(shape.size(), 1);
for (int64_t i = shape.size() - 2; i >= 0; i--) {
stride[i] = shape[i + 1] * stride[i + 1];
}
// Call the aclCreateTensor API to create an aclTensor.
*tensor = aclCreateTensor(shape.data(), shape.size(), dataType, stride.data(), 0, aclFormat::ACL_FORMAT_ND,
shape.data(), shape.size(), *deviceAddr);
return 0;
}
void Finalize(int32_t deviceId, aclrtStream stream)
{
aclrtDestroyStream(stream);
aclrtResetDevice(deviceId);
aclFinalize();
}
int aclnnAvgPool3dTest(int32_t deviceId, aclrtStream& stream) {
auto ret = Init(deviceId, &stream);
CHECK_FREE_RET(ret == ACL_SUCCESS, LOG_PRINT("Init acl failed. ERROR: %d\n", ret); return ret);
// 2. Construct inputs and outputs, which need to be custom-constructed based on the API.
int64_t divisorOverride = 0;
bool countIncludePad = true;
bool ceilMode = false;
std::vector<int64_t> selfShape = {1, 16, 4, 4, 4};
std::vector<int64_t> outShape = {1, 16, 1, 1, 1};
std::vector<int64_t> kernelDims = {4, 4, 4};
std::vector<int64_t> strideDims = {1, 1, 1};
std::vector<int64_t> paddingDims = {0, 0, 0};
void* selfDeviceAddr = nullptr;
void* outDeviceAddr = nullptr;
aclTensor* self = nullptr;
aclTensor* out = nullptr;
std::vector<float> selfHostData(1024, 2);
std::vector<float> outHostData(16, 0);
// Create the self aclTensor.
ret = CreateAclTensor(selfHostData, selfShape, &selfDeviceAddr, aclDataType::ACL_FLOAT, &self);
std::unique_ptr<aclTensor, aclnnStatus (*)(const aclTensor *)> selfTensorPtr(self, aclDestroyTensor);
std::unique_ptr<void, aclError (*)(void *)> selfDeviceAddrPtr(selfDeviceAddr, aclrtFree);
CHECK_FREE_RET(ret == ACL_SUCCESS, return ret);
// Create the kernel aclIntArray.
aclIntArray *kernelSize = aclCreateIntArray(kernelDims.data(), 3);
CHECK_FREE_RET(kernelSize != nullptr, return ACL_ERROR_INTERNAL_ERROR);
// Create the stride aclIntArray.
aclIntArray *stride = aclCreateIntArray(strideDims.data(), 3);
CHECK_FREE_RET(stride != nullptr, return ACL_ERROR_INTERNAL_ERROR);
// Create the padding aclIntArray.
aclIntArray *padding = aclCreateIntArray(paddingDims.data(), 3);
CHECK_FREE_RET(padding != nullptr, return ACL_ERROR_INTERNAL_ERROR);
// Create the out aclTensor.
ret = CreateAclTensor(outHostData, outShape, &outDeviceAddr, aclDataType::ACL_FLOAT, &out);
std::unique_ptr<aclTensor, aclnnStatus (*)(const aclTensor *)> outTensorPtr(out, aclDestroyTensor);
std::unique_ptr<void, aclError (*)(void *)> outDeviceAddrPtr(outDeviceAddr, aclrtFree);
CHECK_FREE_RET(ret == ACL_SUCCESS, return ret);
// 3. Call the CANN operator library API. Replace it with the actual API name.
uint64_t workspaceSize = 0;
aclOpExecutor* executor;
// Call the first-phase API of aclnnAvgPool3d.
ret = aclnnAvgPool3dGetWorkspaceSize(self,
kernelSize,
stride,
padding,
ceilMode,
countIncludePad,
divisorOverride,
out,
&workspaceSize,
&executor);
CHECK_FREE_RET(ret == ACL_SUCCESS, LOG_PRINT("aclnnAvgPool3dGetWorkspaceSize failed. ERROR: %d\n", ret); return ret);
// Allocate device memory based on the workspaceSize calculated by the first-phase API.
void* workspaceAddr = nullptr;
std::unique_ptr<void, aclError (*)(void *)> workspaceAddrPtr(nullptr, aclrtFree);
if (workspaceSize > 0) {
ret = aclrtMalloc(&workspaceAddr, workspaceSize, ACL_MEM_MALLOC_HUGE_FIRST);
CHECK_FREE_RET(ret == ACL_SUCCESS, LOG_PRINT("allocate workspace failed. ERROR: %d\n", ret); return ret);
workspaceAddrPtr.reset(workspaceAddr);
}
// Call the second-phase API of aclnnAvgPool3d.
ret = aclnnAvgPool3d(workspaceAddr, workspaceSize, executor, stream);
CHECK_FREE_RET(ret == ACL_SUCCESS, LOG_PRINT("aclnnAvgPool3d failed. ERROR: %d\n", ret); return ret);
// 4. (Boilerplate) Synchronize and wait for the task execution to complete.
ret = aclrtSynchronizeStream(stream);
CHECK_FREE_RET(ret == ACL_SUCCESS, LOG_PRINT("aclrtSynchronizeStream failed. ERROR: %d\n", ret); return ret);
// 5. Obtain the output value and copy the result from device memory to host memory. Modify the code according to the definition of the specific API.
auto size = GetShapeSize(outShape);
std::vector<float> outData(size, 0);
ret = aclrtMemcpy(outData.data(), outData.size() * sizeof(outData[0]), outDeviceAddr,
size * sizeof(outData[0]), ACL_MEMCPY_DEVICE_TO_HOST);
CHECK_FREE_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]);
}
return ACL_SUCCESS;
}
int main() {
// 1. (Boilerplate) Initialize device/stream. Refer to the acl API manual.
// Set deviceId based on the actual device.
int32_t deviceId = 0;
aclrtStream stream;
auto ret = aclnnAvgPool3dTest(deviceId, stream);
CHECK_FREE_RET(ret == ACL_SUCCESS, LOG_PRINT("aclnnAvgPool3dTest failed. ERROR: %d\n", ret); return ret);
Finalize(deviceId, stream);
return 0;
}