aclnnMaxPool2dWithIndices
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:
- For an input channel of an input signal, a 2-dimensional (H and W dimensions) max pooling (max pooling) operation is provided, and a value out and an index indices after pooling are output.
- Description of input dims: N - batch, C - channel, D - depth, W - width, H - height.
Formula:
Formula for each element in the output tensor:
Formula for deriving the shape of the out tensor. The shape of the indices tensor is the same as that of the out tensor:
If is True and , . The same applies to the W dimension.
Function Prototype
Each operator uses a two-phase API. Call "aclnnMaxPool2dWithIndicesGetWorkspaceSize" first to obtain the workspace size required for computation and the executor containing the operator computation flow, and then call "aclnnMaxPool2dWithIndices" to perform the computation.
aclnnStatus aclnnMaxPool2dWithIndicesGetWorkspaceSize(
const aclTensor *self,
const aclIntArray *kernelSize,
const aclIntArray *stride,
const aclIntArray *padding,
const aclIntArray *dilation,
bool ceilMode,
aclTensor *out,
aclTensor *indices,
uint64_t *workspaceSize,
aclOpExecutor **executor)aclnnStatus aclnnMaxPool2dWithIndices(
void *workspace,
uint64_t workspaceSize,
aclOpExecutor *executor,
aclrtStream stream)aclnnMaxPool2dWithIndicesGetWorkspaceSize
Parameters:
Parameter Input/Output Description Instruction Data Type Data Format Dimension (shape) Non-contiguous Tensor self Input Tensor to be computed. - BFLOAT16, FLOAT16, FLOAT32 NCHW, NHWC, ND 3-4 √ kernelSize Input Window size for max pooling. The length is 1 or 2, and all array elements must be greater than 0. INT64 - - - stride Input Stride at which the window moves. The array length must be 0, 1, or 2, and all array elements must be greater than 0. If the array length is 0, the value of kernelSize is used as strides internally. INT64 - - - padding Input Number of layers padded on each side, with the padded positions filled with "negative infinity". Array length must be 1 or 2, and array elements must be greater than or equal to 0 and less than or equal to kernelSize/2. INT64 - - - dilation Input Controls the stride of elements in the window. Array length must be 1 or 2. INT64 - - - ceilMode Input The rounding method used when computing the output shape. If True, the output shape is rounded up when it is computed; if False, the output shape is rounded down. BOOL - - - out Output Output tensor, which stores the pooling result. shape is derived from the preceding formula. The data format and data type are the same as those of self. BFLOAT16, FLOAT16, FLOAT32 NCHW, NHWC, ND 3-4 √ indices Output Tensor composed of the index positions of the maximum values. The shape is the same as that of out. INT32, INT64 NCHW, NHWC, ND 3-4 √ workspaceSize Output Returns the size of the workspace to be applied for on the Device. - - - - - executor Output Returns the op executor, which contains the operator computation process. - - - - - Atlas A2 products andAtlas A3 products : NHWC is not supported, and only the element value 1 is supported in dilation.
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 self and out are null pointers. ACLNN_ERR_PARAM_INVALID 161002 The data type of self is not supported. The data format of self is not within the supported range. The shape of self is not 3D or 4D. An axis of the shape of out derived by the formula is 0. The length of kernelSize is not equal to 1 or 2. kernelSize contains a value less than or equal to 0. The length of stride is not equal to 0, 1, or 2. stride contains a value less than or equal to 0. The length of padding is not equal to 1 or 2. padding contains a value less than 0 or greater than kernelSize/2. The length of dilation is not equal to 1 or 2. The element values in dilation do not meet the requirements. An axis in the output shape derived by the formula is 0. The shape of out or indices is inconsistent with the derived output shape. The data type of out is inconsistent with that of self. The data format of out is inconsistent with that of self.
aclnnMaxPool2dWithIndices
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 aclnnMaxPool2dWithIndicesGetWorkspaceSize. executor Input Op executor that contains the operator computation flow. stream Input Specifies the stream that executes the task. Return value
aclnnStatus: return status code. For details, see aclnn Error Codes.
Constraints
- Deterministic computation:
- aclnnMaxPool2dWithIndices defaults to a deterministic implementation.
Atlas A2 products ,Atlas A3 products :- The input data does not support NaN or -Inf.
- When ceilMode is True, if the sliding window is entirely on the right padding region, this output result will be ignored.
Example
The following sample code is for reference only. For details about the compilation and execution procedures, see Compile and Run Samples.
#include <cstdio>
#include <iostream>
#include <vector>
#include "acl/acl.h"
#include "aclnnop/aclnn_max_pool2d_with_indices.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) {
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);
// Compute 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 aclCreateTensor to create an aclTensor.
*tensor = aclCreateTensor(shape.data(), shape.size(), dataType, strides.data(), 0, aclFormat::ACL_FORMAT_NCHW,
shape.data(), shape.size(), *deviceAddr);
return 0;
}
int main() {
// 1. (Boilerplate) device/stream initialization. Refer to the acl API documentation.
// 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 == ACL_SUCCESS, LOG_PRINT("Init acl failed. ERROR: %d\n", ret); return ret);
// 2. Construct inputs and outputs. Custom construction is required based on the API.
std::vector<int64_t> selfShape = {1, 1, 4, 3};
std::vector<int64_t> outShape = {1, 1, 2, 1};
std::vector<int64_t> indicesShape = {1, 1, 2, 1};
std::vector<int64_t> kernelSizeData = {2, 2};
std::vector<int64_t> strideData = {2, 2};
std::vector<int64_t> paddingData = {0, 0};
std::vector<int64_t> dilationData = {1, 1};
void* selfDeviceAddr = nullptr;
void* outDeviceAddr = nullptr;
void* indicesDeviceAddr = nullptr;
aclTensor* self = nullptr;
aclTensor* out = nullptr;
aclTensor* indices = nullptr;
std::vector<float> selfHostData = {0.0850, -0.5147, -0.0212, -0.5654, -0.3222, 0.5847, 1.7510, 0.9954, 0.1842, 0.8392, 0.4835, 0.9213};
std::vector<float> outHostData = {0, 0};
std::vector<int32_t> indicesHostData = {0, 0};
// Create the self aclTensor.
ret = CreateAclTensor(selfHostData, selfShape, &selfDeviceAddr, aclDataType::ACL_FLOAT, &self);
CHECK_RET(ret == ACL_SUCCESS, return ret);
// Create out aclTensor.
ret = CreateAclTensor(outHostData, outShape, &outDeviceAddr, aclDataType::ACL_FLOAT, &out);
CHECK_RET(ret == ACL_SUCCESS, return ret);
// Create indices aclTensor.
ret = CreateAclTensor(indicesHostData, indicesShape, &indicesDeviceAddr, aclDataType::ACL_INT32, &indices);
CHECK_RET(ret == ACL_SUCCESS, return ret);
// Create the input array.
aclIntArray* kernelSize = aclCreateIntArray(kernelSizeData.data(), 2);
aclIntArray* stride = aclCreateIntArray(strideData.data(), 2);
aclIntArray* padding = aclCreateIntArray(paddingData.data(), 2);
aclIntArray* dilation = aclCreateIntArray(dilationData.data(), 2);
const bool ceilMode = false;
uint64_t workspaceSize = 0;
aclOpExecutor* executor;
// Example of calling aclnnMaxPool2dWithIndices.
// 3. Call the CANN operator library API. Replace the API name with the actual one.
// Call the aclnnMaxPool2dWithIndices first-phase API.
ret = aclnnMaxPool2dWithIndicesGetWorkspaceSize(self, kernelSize, stride, padding, dilation, ceilMode, out, indices, &workspaceSize, &executor);
CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclnnMaxPool2dWithIndicesGetWorkspaceSize 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 aclnnMaxPool2dWithIndices second-phase API.
ret = aclnnMaxPool2dWithIndices(workspaceAddr, workspaceSize, executor, stream);
CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclnnMaxPool2dWithIndices 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 from the device side to the host side. Modify the code based on the definition of the specific API.
auto size = GetShapeSize(outShape);
std::vector<float> resultData(size, 0);
ret = aclrtMemcpy(resultData.data(), resultData.size() * sizeof(resultData[0]), outDeviceAddr,
size * sizeof(resultData[0]), ACL_MEMCPY_DEVICE_TO_HOST);
CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("copy out 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]);
}
size = GetShapeSize(indicesShape);
std::vector<int> indicesResultData(size, 0);
ret = aclrtMemcpy(indicesResultData.data(), indicesResultData.size() * sizeof(indicesResultData[0]), indicesDeviceAddr,
size * sizeof(indicesResultData[0]), ACL_MEMCPY_DEVICE_TO_HOST);
CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("copy indices 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: %d\n", i, indicesResultData[i]);
}
// 6. Release aclTensor and aclScalar. Modify them based on the actual API definition.
aclDestroyTensor(self);
aclDestroyTensor(out);
aclDestroyTensor(indices);
// 7. Release device resources. Modify them based on the actual API definition.
aclrtFree(selfDeviceAddr);
aclrtFree(outDeviceAddr);
aclrtFree(indicesDeviceAddr);
if (workspaceSize > 0) {
aclrtFree(workspaceAddr);
}
aclrtDestroyStream(stream);
aclrtResetDevice(deviceId);
aclFinalize();
return 0;
}