blob: d7fb75ee60e49c21704777450a96815ddfd446e9 [file] [log] [blame]
Anthony Barbier6ff3b192017-09-04 18:44:23 +01001/*
Michele Di Giorgio19289042021-02-03 16:05:00 +00002 * Copyright (c) 2017-2021 Arm Limited.
Anthony Barbier6ff3b192017-09-04 18:44:23 +01003 *
4 * SPDX-License-Identifier: MIT
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to
8 * deal in the Software without restriction, including without limitation the
9 * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
10 * sell copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in all
14 * copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22 * SOFTWARE.
23 */
Georgios Pinitas7891a732021-08-20 21:39:25 +010024#include "src/cpu/kernels/CpuPool2dKernel.h"
Anthony Barbier6ff3b192017-09-04 18:44:23 +010025
Anthony Barbier6ff3b192017-09-04 18:44:23 +010026#include "arm_compute/core/Helpers.h"
Anthony Barbier6ff3b192017-09-04 18:44:23 +010027#include "arm_compute/core/TensorInfo.h"
Anthony Barbier6ff3b192017-09-04 18:44:23 +010028#include "arm_compute/core/Validate.h"
29#include "arm_compute/core/Window.h"
Giorgio Arena9fb6c7e2018-08-22 12:15:25 +010030#include "arm_compute/core/utils/misc/ShapeCalculator.h"
Sang-Hoon Park68dd25f2020-10-19 16:00:11 +010031#include "src/core/AccessWindowStatic.h"
32#include "src/core/CPP/Validate.h"
Georgios Pinitasddb93bb2020-10-02 16:38:59 +010033#include "src/core/NEON/NEAsymm.h"
34#include "src/core/NEON/NEFixedPoint.h"
35#include "src/core/NEON/NEMath.h"
Sheri Zhang79144a62021-02-08 17:43:04 +000036#include "src/core/common/Registrars.h"
Sang-Hoon Park68dd25f2020-10-19 16:00:11 +010037#include "src/core/helpers/AutoConfiguration.h"
38#include "src/core/helpers/WindowHelpers.h"
Georgios Pinitas7891a732021-08-20 21:39:25 +010039#include "src/cpu/kernels/pool2d/neon/list.h"
Georgios Pinitas55186712018-01-08 17:37:12 +000040#include "support/ToolchainSupport.h"
41
Georgios Pinitasddb93bb2020-10-02 16:38:59 +010042#include "src/core/NEON/wrapper/wrapper.h"
Anthony Barbier6ff3b192017-09-04 18:44:23 +010043#include <arm_neon.h>
Anthony Barbier6ff3b192017-09-04 18:44:23 +010044
Manuel Bottinib4bb8272019-12-18 18:01:27 +000045namespace arm_compute
46{
Michele Di Giorgio19289042021-02-03 16:05:00 +000047namespace cpu
48{
49namespace kernels
50{
Anthony Barbier6ff3b192017-09-04 18:44:23 +010051namespace
52{
Sheri Zhang79144a62021-02-08 17:43:04 +000053using namespace misc::shape_calculator;
54
55struct PoolingSelectorData
Michalis Spyroucffb2a32020-09-08 16:26:38 +010056{
Sheri Zhang79144a62021-02-08 17:43:04 +000057 DataType dt;
58 DataLayout dl;
59 int pool_stride_x;
60 Size2D pool_size;
61};
Michalis Spyroucffb2a32020-09-08 16:26:38 +010062
Sheri Zhang79144a62021-02-08 17:43:04 +000063using PoolingSelectorPtr = std::add_pointer<bool(const PoolingSelectorData &data)>::type;
64using PoolingKernelPtr = std::add_pointer<void(const ITensor *, ITensor *, ITensor *, PoolingLayerInfo &, const Window &, const Window &)>::type;
65struct PoolingKernel
Michalis Spyroucffb2a32020-09-08 16:26:38 +010066{
Sheri Zhang79144a62021-02-08 17:43:04 +000067 const char *name;
68 const PoolingSelectorPtr is_selected;
69 PoolingKernelPtr ukernel;
70};
Michalis Spyroucffb2a32020-09-08 16:26:38 +010071
Sheri Zhang79144a62021-02-08 17:43:04 +000072static const PoolingKernel available_kernels[] =
Anthony Barbier6ff3b192017-09-04 18:44:23 +010073{
Georgios Pinitasadaae7e2017-10-30 15:56:32 +000074 {
Georgios Pinitas5fdde992021-06-25 05:42:57 +010075 "neon_qu8_nhwc_poolMxN",
Sheri Zhang79144a62021-02-08 17:43:04 +000076 [](const PoolingSelectorData & data) { return ((data.dl == DataLayout::NHWC) && (data.dt == DataType::QASYMM8)); },
77 REGISTER_QASYMM8_NEON(arm_compute::cpu::poolingMxN_qasymm8_neon_nhwc)
78 },
79 {
Georgios Pinitas5fdde992021-06-25 05:42:57 +010080 "neon_qs8_nhwc_poolMxN",
Sheri Zhang79144a62021-02-08 17:43:04 +000081 [](const PoolingSelectorData & data) { return ((data.dl == DataLayout::NHWC) && (data.dt == DataType::QASYMM8_SIGNED)); },
82 REGISTER_QASYMM8_SIGNED_NEON(arm_compute::cpu::poolingMxN_qasymm8_signed_neon_nhwc)
83 },
84#if defined(__ARM_FEATURE_FP16_VECTOR_ARITHMETIC)
85 {
Georgios Pinitas5fdde992021-06-25 05:42:57 +010086 "neon_f16_nhwc_poolMxN",
Sheri Zhang79144a62021-02-08 17:43:04 +000087 [](const PoolingSelectorData & data) { return ((data.dl == DataLayout::NHWC) && (data.dt == DataType::F16)); },
88 REGISTER_FP16_NEON(arm_compute::cpu::poolingMxN_fp16_neon_nhwc)
89 },
90#endif /* defined(__ARM_FEATURE_FP16_VECTOR_ARITHMETIC) */
91 {
Georgios Pinitas5fdde992021-06-25 05:42:57 +010092 "neon_fp32_nhwc_poolMxN",
Sheri Zhang79144a62021-02-08 17:43:04 +000093 [](const PoolingSelectorData & data) { return ((data.dl == DataLayout::NHWC) && (data.dt == DataType::F32)); },
94 REGISTER_FP32_NEON(arm_compute::cpu::poolingMxN_fp32_neon_nhwc)
95 },
96#if defined(ENABLE_NCHW_KERNELS)
97 {
Georgios Pinitas5fdde992021-06-25 05:42:57 +010098 "neon_qu8_nchw_pool2",
Sheri Zhang79144a62021-02-08 17:43:04 +000099 [](const PoolingSelectorData & data) { return ((data.dl == DataLayout::NCHW) && (data.dt == DataType::QASYMM8) && (data.pool_size.x() == data.pool_size.y()) && (data.pool_size.x() == 2) && (data.pool_stride_x < 3)); },
100 REGISTER_QASYMM8_NEON(arm_compute::cpu::pooling2_quantized_neon_nchw<uint8_t>)
101 },
102 {
Georgios Pinitas5fdde992021-06-25 05:42:57 +0100103 "neon_qu8_nchw_pool3",
Sheri Zhang79144a62021-02-08 17:43:04 +0000104 [](const PoolingSelectorData & data) { return ((data.dl == DataLayout::NCHW) && (data.dt == DataType::QASYMM8) && (data.pool_size.x() == data.pool_size.y()) && (data.pool_size.x() == 3) && (data.pool_stride_x < 3)); },
105 REGISTER_QASYMM8_NEON(arm_compute::cpu::pooling3_quantized_neon_nchw<uint8_t>)
106 },
107 {
Georgios Pinitas5fdde992021-06-25 05:42:57 +0100108 "neon_qu8_nchw_poolMxN",
Sheri Zhang79144a62021-02-08 17:43:04 +0000109 [](const PoolingSelectorData & data) { return ((data.dl == DataLayout::NCHW) && (data.dt == DataType::QASYMM8)); },
110 REGISTER_QASYMM8_NEON(arm_compute::cpu::poolingMxN_quantized_neon_nchw<uint8_t>)
111 },
112 {
Georgios Pinitas5fdde992021-06-25 05:42:57 +0100113 "neon_qs8_nchw_pool2",
Sheri Zhang79144a62021-02-08 17:43:04 +0000114 [](const PoolingSelectorData & data) { return ((data.dl == DataLayout::NCHW) && (data.dt == DataType::QASYMM8_SIGNED) && (data.pool_size.x() == data.pool_size.y()) && (data.pool_size.x() == 2) && (data.pool_stride_x < 3)); },
115 REGISTER_QASYMM8_SIGNED_NEON(arm_compute::cpu::pooling2_quantized_neon_nchw<int8_t>)
116 },
117 {
Georgios Pinitas5fdde992021-06-25 05:42:57 +0100118 "neon_qs8_nchw_pool3",
Sheri Zhang79144a62021-02-08 17:43:04 +0000119 [](const PoolingSelectorData & data) { return ((data.dl == DataLayout::NCHW) && (data.dt == DataType::QASYMM8_SIGNED) && (data.pool_size.x() == data.pool_size.y()) && (data.pool_size.x() == 3) && (data.pool_stride_x < 3)); },
120 REGISTER_QASYMM8_SIGNED_NEON(arm_compute::cpu::pooling3_quantized_neon_nchw<int8_t>)
121 },
122 {
Georgios Pinitas5fdde992021-06-25 05:42:57 +0100123 "neon_qs8_nchw_poolMxN",
Sheri Zhang79144a62021-02-08 17:43:04 +0000124 [](const PoolingSelectorData & data) { return ((data.dl == DataLayout::NCHW) && (data.dt == DataType::QASYMM8_SIGNED)); },
125 REGISTER_QASYMM8_SIGNED_NEON(arm_compute::cpu::poolingMxN_quantized_neon_nchw<int8_t>)
126 },
127#if defined(__ARM_FEATURE_FP16_VECTOR_ARITHMETIC)
128 {
Georgios Pinitas5fdde992021-06-25 05:42:57 +0100129 "neon_fp16_nchw_pool2",
Sheri Zhang79144a62021-02-08 17:43:04 +0000130 [](const PoolingSelectorData & data) { return ((data.dl == DataLayout::NCHW) && (data.dt == DataType::F16) && (data.pool_size.x() == data.pool_size.y()) && (data.pool_size.x() == 2)); },
131 REGISTER_FP16_NEON(arm_compute::cpu::pooling2_fp16_neon_nchw)
132 },
133 {
Georgios Pinitas5fdde992021-06-25 05:42:57 +0100134 "neon_fp16_nchw_pool3",
Sheri Zhang79144a62021-02-08 17:43:04 +0000135 [](const PoolingSelectorData & data) { return ((data.dl == DataLayout::NCHW) && (data.dt == DataType::F16) && (data.pool_size.x() == data.pool_size.y()) && (data.pool_size.x() == 3)); },
136 REGISTER_FP16_NEON(arm_compute::cpu::pooling3_fp16_neon_nchw)
137 },
138 {
Georgios Pinitas5fdde992021-06-25 05:42:57 +0100139 "neon_fp16_nchw_poolMxN",
Sheri Zhang79144a62021-02-08 17:43:04 +0000140 [](const PoolingSelectorData & data) { return ((data.dl == DataLayout::NCHW) && (data.dt == DataType::F16)); },
141 REGISTER_FP16_NEON(arm_compute::cpu::poolingMxN_fp16_neon_nchw)
142 },
143#endif /* defined(__ARM_FEATURE_FP16_VECTOR_ARITHMETIC) */
144 {
Georgios Pinitas5fdde992021-06-25 05:42:57 +0100145 "neon_fp32_nchw_pool2",
Sheri Zhang79144a62021-02-08 17:43:04 +0000146 [](const PoolingSelectorData & data) { return ((data.dl == DataLayout::NCHW) && (data.dt == DataType::F32) && (data.pool_size.x() == data.pool_size.y()) && (data.pool_size.x() == 2)); },
147 REGISTER_FP32_NEON(arm_compute::cpu::pooling2_fp32_neon_nchw)
148 },
149 {
Georgios Pinitas5fdde992021-06-25 05:42:57 +0100150 "neon_fp32_nchw_pool3",
Sheri Zhang79144a62021-02-08 17:43:04 +0000151 [](const PoolingSelectorData & data) { return ((data.dl == DataLayout::NCHW) && (data.dt == DataType::F32) && (data.pool_size.x() == data.pool_size.y()) && (data.pool_size.x() == 3)); },
152 REGISTER_FP32_NEON(arm_compute::cpu::pooling3_fp32_neon_nchw)
153 },
154 {
Georgios Pinitas5fdde992021-06-25 05:42:57 +0100155 "neon_fp32_nchw_pool7",
Sheri Zhang79144a62021-02-08 17:43:04 +0000156 [](const PoolingSelectorData & data) { return ((data.dl == DataLayout::NCHW) && (data.dt == DataType::F32) && (data.pool_size.x() == data.pool_size.y()) && (data.pool_size.x() == 7)); },
157 REGISTER_FP32_NEON(arm_compute::cpu::pooling7_fp32_neon_nchw)
158 },
159 {
Georgios Pinitas5fdde992021-06-25 05:42:57 +0100160 "neon_fp32_nchw_poolMxN",
Sheri Zhang79144a62021-02-08 17:43:04 +0000161 [](const PoolingSelectorData & data) { return ((data.dl == DataLayout::NCHW) && (data.dt == DataType::F32)); },
162 REGISTER_FP32_NEON(arm_compute::cpu::poolingMxN_fp32_neon_nchw)
163 },
164#endif /* defined(ENABLE_NCHW_KERNELS) */
165};
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100166
Sheri Zhang79144a62021-02-08 17:43:04 +0000167/** Micro-kernel selector
168 *
169 * @param[in] data Selection data passed to help pick the appropriate micro-kernel
170 *
171 * @return A matching micro-kernel else nullptr
172 */
173const PoolingKernel *get_implementation(DataType dt, DataLayout dl, int pool_stride_x, Size2D pool_size)
Georgios Pinitas55186712018-01-08 17:37:12 +0000174{
Sheri Zhang79144a62021-02-08 17:43:04 +0000175 for(const auto &uk : available_kernels)
Georgios Pinitas55186712018-01-08 17:37:12 +0000176 {
Sheri Zhang79144a62021-02-08 17:43:04 +0000177 if(uk.is_selected({ dt, dl, pool_stride_x, pool_size }))
Georgios Pinitas55186712018-01-08 17:37:12 +0000178 {
Sheri Zhang79144a62021-02-08 17:43:04 +0000179 return &uk;
Georgios Pinitas55186712018-01-08 17:37:12 +0000180 }
Georgios Pinitas55186712018-01-08 17:37:12 +0000181 }
Sheri Zhang79144a62021-02-08 17:43:04 +0000182 return nullptr;
Georgios Pinitas55186712018-01-08 17:37:12 +0000183}
184
Michele Di Giorgio19289042021-02-03 16:05:00 +0000185Status validate_arguments(const ITensorInfo *src, const ITensorInfo *dst, const PoolingLayerInfo &pool_info,
Freddie Liardetafcbb8f2021-05-04 12:41:16 +0100186 const ITensorInfo *indices, Size2D pool_size)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100187{
Michele Di Giorgio19289042021-02-03 16:05:00 +0000188 ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(src, dst);
Freddie Liardetafcbb8f2021-05-04 12:41:16 +0100189 ARM_COMPUTE_RETURN_ERROR_ON(pool_size.x() == 0);
190 ARM_COMPUTE_RETURN_ERROR_ON(pool_size.y() == 0);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100191
Isabella Gottardi7567f5f2018-01-30 15:26:00 +0000192 int pool_stride_x = 0;
193 int pool_stride_y = 0;
Freddie Liardetafcbb8f2021-05-04 12:41:16 +0100194 int output_width = 0;
195 int output_height = 0;
Sang-Hoon Park0cb3da62020-01-15 12:39:56 +0000196 PoolingType pool_type = pool_info.pool_type;
197 const PadStrideInfo pad_stride_info = pool_info.pad_stride_info;
Freddie Liardetafcbb8f2021-05-04 12:41:16 +0100198 const auto data_layout = pool_info.data_layout == DataLayout::UNKNOWN ? src->data_layout() : pool_info.data_layout;
199 const int idx_width = get_data_layout_dimension_index(data_layout, DataLayoutDimension::WIDTH);
200 const int idx_height = get_data_layout_dimension_index(data_layout, DataLayoutDimension::HEIGHT);
201
202 std::tie(output_width, output_height) = scaled_dimensions_signed(src->tensor_shape()[idx_width], src->tensor_shape()[idx_height],
203 pool_size.x(), pool_size.y(), pool_info.pad_stride_info);
204 ARM_COMPUTE_RETURN_ERROR_ON_MSG((output_width < 1 || output_height < 1), "Calculated output dimension size is invalid");
205
206 TensorInfo out_info(TensorInfo(compute_pool_shape(*src, pool_info), 1, dst->data_type()));
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100207 std::tie(pool_stride_x, pool_stride_y) = pad_stride_info.stride();
Michele Di Giorgio8af2dd62017-06-19 15:19:29 +0100208
Michele Di Giorgio19289042021-02-03 16:05:00 +0000209 ARM_COMPUTE_RETURN_ERROR_ON_CPU_F16_UNSUPPORTED(src);
morgolockcc1f6c92020-03-24 09:26:48 +0000210 if(indices)
211 {
Michele Di Giorgio19289042021-02-03 16:05:00 +0000212 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(src, 1, DataType::F32, DataType::F16);
morgolockcc1f6c92020-03-24 09:26:48 +0000213 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(indices, 1, DataType::U32);
214 ARM_COMPUTE_RETURN_ERROR_ON_MSG(pool_type != PoolingType::MAX, "Pooling indices only supported for MAX pooling method");
215 }
Michele Di Giorgio19289042021-02-03 16:05:00 +0000216 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(src, 1, DataType::QASYMM8, DataType::QASYMM8_SIGNED, DataType::F16, DataType::F32);
217 ARM_COMPUTE_RETURN_ERROR_ON(pool_type == PoolingType::L2 && is_data_type_quantized(src->data_type()));
218 ARM_COMPUTE_RETURN_ERROR_ON_MSG(is_data_type_quantized(src->data_type()) && !pool_info.exclude_padding && (pool_info.pool_type == PoolingType::AVG) && pool_info.pad_stride_info.has_padding()
219 && (src->data_layout() == DataLayout::NHWC),
Michele Di Giorgio2c877192020-02-18 19:06:27 +0000220 "exclude_padding equal false is not supported for AVG Pooling with padding on quantized types");
Isabella Gottardi7567f5f2018-01-30 15:26:00 +0000221
Michele Di Giorgio19289042021-02-03 16:05:00 +0000222 if(dst->total_size() != 0)
Georgios Pinitas1dad50e2017-07-03 17:51:34 +0100223 {
Michele Di Giorgio19289042021-02-03 16:05:00 +0000224 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(src, dst);
225 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_LAYOUT(src, dst);
Freddie Liardetafcbb8f2021-05-04 12:41:16 +0100226 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_SHAPES(dst, &out_info);
morgolockcc1f6c92020-03-24 09:26:48 +0000227 if(indices)
228 {
229 ARM_COMPUTE_RETURN_ERROR_ON_MSG((pool_size != Size2D(2, 2)), "Pooling indices only supported for pool size 2x2");
Freddie Liardetafcbb8f2021-05-04 12:41:16 +0100230 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_SHAPES(indices, &out_info);
morgolockcc1f6c92020-03-24 09:26:48 +0000231 }
Georgios Pinitas1dad50e2017-07-03 17:51:34 +0100232 }
233
Sheri Zhang79144a62021-02-08 17:43:04 +0000234 const auto *uk = get_implementation(src->data_type(), src->data_layout(), pool_stride_x, pool_size);
235 ARM_COMPUTE_RETURN_ERROR_ON(uk == nullptr || uk->ukernel == nullptr);
236
Michalis Spyrouafa5d812017-11-30 14:25:57 +0000237 return Status{};
238}
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100239
Michele Di Giorgio19289042021-02-03 16:05:00 +0000240std::pair<Status, Window> validate_and_configure_window(ITensorInfo *src, ITensorInfo *dst, ITensorInfo *indices, const PoolingLayerInfo &pool_info,
morgolockcc1f6c92020-03-24 09:26:48 +0000241 unsigned int &num_elems_processed_per_iteration,
242 BorderSize &border_size,
Freddie Liardetafcbb8f2021-05-04 12:41:16 +0100243 int pool_size_x, int pool_size_y)
Michalis Spyrouafa5d812017-11-30 14:25:57 +0000244{
Michele Di Giorgio19289042021-02-03 16:05:00 +0000245 // dst auto inizialitation if not yet initialized
246 auto_init_if_empty(*dst, src->clone()->set_tensor_shape(compute_pool_shape(*src, pool_info)));
morgolockcc1f6c92020-03-24 09:26:48 +0000247 if(indices)
248 {
249 // Indices auto inizialitation if not yet initialized
Michele Di Giorgio19289042021-02-03 16:05:00 +0000250 auto_init_if_empty(*indices, (src->clone()->set_tensor_shape(compute_pool_shape(*src,
251 pool_info)))
morgolocke383c352020-04-03 16:57:46 +0100252 .set_data_type(DataType::U32) /* we store the offset to the element */);
morgolockcc1f6c92020-03-24 09:26:48 +0000253 }
Michele Di Giorgio19289042021-02-03 16:05:00 +0000254 const auto data_layout = pool_info.data_layout == DataLayout::UNKNOWN ? src->data_layout() : pool_info.data_layout;
Michalis Spyrouafa5d812017-11-30 14:25:57 +0000255 unsigned int num_elems_read_per_iteration = 0;
256 unsigned int num_elems_horizontal_window = 0;
Michalis Spyrouafa5d812017-11-30 14:25:57 +0000257 int pool_stride_x = 0;
258 int pool_stride_y = 0;
Michalis Spyrou57dac842018-03-01 16:03:50 +0000259 const int idx_width = get_data_layout_dimension_index(data_layout, DataLayoutDimension::WIDTH);
260 const int idx_height = get_data_layout_dimension_index(data_layout, DataLayoutDimension::HEIGHT);
Michele Di Giorgio19289042021-02-03 16:05:00 +0000261 const int src_width = src->dimension(idx_width);
262 const int src_height = src->dimension(idx_height);
Sang-Hoon Park0cb3da62020-01-15 12:39:56 +0000263 const PadStrideInfo pad_stride_info = pool_info.pad_stride_info;
Michalis Spyrouafa5d812017-11-30 14:25:57 +0000264 std::tie(pool_stride_x, pool_stride_y) = pad_stride_info.stride();
Freddie Liardetafcbb8f2021-05-04 12:41:16 +0100265 const int pool_pad_right = pad_stride_info.pad_right();
266 const int pool_pad_top = pad_stride_info.pad_top();
267 const int pool_pad_left = pad_stride_info.pad_left();
268 const int pool_pad_bottom = pad_stride_info.pad_bottom();
269 const bool is_square = pool_size_x == pool_size_y;
270 const unsigned int pooled_w = dst->dimension(idx_width);
271 const unsigned int pooled_h = dst->dimension(idx_height);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100272
Isabella Gottardi7567f5f2018-01-30 15:26:00 +0000273 //If it's not squared and optimized will be executed the MxN
274 num_elems_read_per_iteration = 1;
275 num_elems_processed_per_iteration = 1;
276 num_elems_horizontal_window = 1;
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100277
Isabella Gottardi7567f5f2018-01-30 15:26:00 +0000278 if(is_square)
279 {
Michele Di Giorgio19289042021-02-03 16:05:00 +0000280 switch(src->data_type())
Isabella Gottardi7567f5f2018-01-30 15:26:00 +0000281 {
Isabella Gottardi7567f5f2018-01-30 15:26:00 +0000282 case DataType::QASYMM8:
Manuel Bottinib4bb8272019-12-18 18:01:27 +0000283 case DataType::QASYMM8_SIGNED:
Isabella Gottardi7567f5f2018-01-30 15:26:00 +0000284 switch(pool_size_x)
285 {
286 case 2:
287 num_elems_read_per_iteration = 16;
288 num_elems_processed_per_iteration = (pool_stride_x == 2) ? 8 : 15;
289 num_elems_horizontal_window = (pool_stride_x == 2) ? 8 : 16;
290 break;
291 case 3:
292 num_elems_read_per_iteration = 16;
293 num_elems_processed_per_iteration = (pool_stride_x == 2) ? 7 : 14;
294 num_elems_horizontal_window = (pool_stride_x == 2) ? 8 : 16;
295 break;
296 default:
297 break;
298 }
299 break;
Isabella Gottardi7567f5f2018-01-30 15:26:00 +0000300#ifdef __ARM_FEATURE_FP16_VECTOR_ARITHMETIC
301 case DataType::F16:
302 switch(pool_size_x)
303 {
304 case 2:
Isabella Gottardi7567f5f2018-01-30 15:26:00 +0000305 case 3:
306 num_elems_read_per_iteration = 4;
307 num_elems_processed_per_iteration = 1;
308 num_elems_horizontal_window = 1;
309 break;
310 default:
311 break;
312 }
313 break;
314#endif /* __ARM_FEATURE_FP16_VECTOR_ARITHMETIC */
315 case DataType::F32:
316 switch(pool_size_x)
317 {
318 case 2:
319 num_elems_read_per_iteration = 2;
320 break;
321 case 3:
322 num_elems_read_per_iteration = 4; // We use vload4 for pooling3
323 break;
324 case 7:
325 num_elems_read_per_iteration = 8; // We use vload8 for pooling7
326 break;
327 default:
328 break;
329 }
330 num_elems_processed_per_iteration = 1;
331 num_elems_horizontal_window = 1;
332 break;
333 default:
334 ARM_COMPUTE_ERROR("Element size not supported");
335 break;
336 }
337 }
Michalis Spyrou57dac842018-03-01 16:03:50 +0000338
339 bool window_changed = false;
340 Window win{};
341 if(data_layout == DataLayout::NCHW)
342 {
343 // Number of iterations in X dimension
344 const int num_iterations_x = (pooled_w + num_elems_processed_per_iteration - 1) / num_elems_processed_per_iteration;
Michalis Spyrou57dac842018-03-01 16:03:50 +0000345 // Upper limit for the number of right/bottom border elements that are accessed
Michele Di Giorgio19289042021-02-03 16:05:00 +0000346 const int upper_bound_w = ((num_iterations_x - 1) * num_elems_processed_per_iteration * pool_stride_x - pool_pad_left + num_elems_read_per_iteration) - src_width;
347 const int upper_bound_h = ((pooled_h - 1) * pool_stride_y - pool_pad_top + pool_size_y) - src_height;
morgolockcc1f6c92020-03-24 09:26:48 +0000348 border_size = BorderSize(pool_pad_top, pool_pad_right, pool_pad_bottom, pool_pad_left);
349 border_size.right = std::max(upper_bound_w, pool_pad_right);
350 border_size.bottom = std::max(upper_bound_h, pool_pad_bottom);
Michele Di Giorgio19289042021-02-03 16:05:00 +0000351 TensorShape dst_shape{ src->tensor_shape() };
352 dst_shape.set(0, pooled_w);
353 dst_shape.set(1, pooled_h);
354 TensorInfo dst_info(src->clone()->set_tensor_shape(dst_shape));
355 win = calculate_max_window(dst_info, Steps(num_elems_processed_per_iteration));
Michele Di Giorgiobae22372021-02-12 17:34:17 +0000356 AccessWindowStatic src_access(src, -pool_pad_left, -pool_pad_top, ceil_to_multiple(src_width + border_size.right, pool_size_x), src_height + border_size.bottom);
Michele Di Giorgio19289042021-02-03 16:05:00 +0000357 AccessWindowHorizontal dst_access(dst, 0, num_elems_horizontal_window);
morgolockcc1f6c92020-03-24 09:26:48 +0000358 if(indices)
359 {
360 AccessWindowHorizontal indices_access(indices, 0, num_elems_horizontal_window);
Michele Di Giorgio19289042021-02-03 16:05:00 +0000361 window_changed = update_window_and_padding(win, src_access, dst_access, indices_access);
morgolockcc1f6c92020-03-24 09:26:48 +0000362 }
363 else
364 {
Michele Di Giorgio19289042021-02-03 16:05:00 +0000365 window_changed = update_window_and_padding(win, src_access, dst_access);
morgolockcc1f6c92020-03-24 09:26:48 +0000366 }
Michele Di Giorgio19289042021-02-03 16:05:00 +0000367 dst_access.set_valid_region(win, ValidRegion(Coordinates(), dst->tensor_shape()));
Michele Di Giorgiobae22372021-02-12 17:34:17 +0000368
369 border_size = src->padding();
Michalis Spyrouafa5d812017-11-30 14:25:57 +0000370 }
Michalis Spyrouafa5d812017-11-30 14:25:57 +0000371
372 Status err = (window_changed) ? ARM_COMPUTE_CREATE_ERROR(ErrorCode::RUNTIME_ERROR, "Insufficient Padding!") : Status{};
373 return std::make_pair(err, win);
374}
375} // namespace
376
Manuel Bottinib4bb6a02021-05-24 16:01:32 +0100377BorderSize CpuPool2dKernel::border_size() const
Michalis Spyrouafa5d812017-11-30 14:25:57 +0000378{
379 return _border_size;
380}
381
Manuel Bottinib4bb6a02021-05-24 16:01:32 +0100382void CpuPool2dKernel::configure(ITensorInfo *src, ITensorInfo *dst, const PoolingLayerInfo &pool_info, ITensorInfo *indices)
Michalis Spyrouafa5d812017-11-30 14:25:57 +0000383{
Michele Di Giorgio19289042021-02-03 16:05:00 +0000384 ARM_COMPUTE_ERROR_ON_NULLPTR(src, dst);
Sang-Hoon Park0cb3da62020-01-15 12:39:56 +0000385 const PadStrideInfo pad_stride_info = pool_info.pad_stride_info;
386 const bool is_global_pooling = pool_info.is_global_pooling;
Michalis Spyrou57dac842018-03-01 16:03:50 +0000387
388 // Get data layout
Michele Di Giorgio19289042021-02-03 16:05:00 +0000389 const auto data_layout = pool_info.data_layout == DataLayout::UNKNOWN ? src->data_layout() : pool_info.data_layout;
Sang-Hoon Park11fedda2020-01-15 14:44:04 +0000390 const int idx_width = get_data_layout_dimension_index(data_layout, DataLayoutDimension::WIDTH);
391 const int idx_height = get_data_layout_dimension_index(data_layout, DataLayoutDimension::HEIGHT);
Michalis Spyrouafa5d812017-11-30 14:25:57 +0000392
393 // Update pool size in case of global pooling
Pablo Tello77e6c552018-12-04 15:33:49 +0000394 const Size2D pool_size(
Michele Di Giorgio19289042021-02-03 16:05:00 +0000395 is_global_pooling ? src->dimension(idx_width) : pool_info.pool_size.width,
396 is_global_pooling ? src->dimension(idx_height) : pool_info.pool_size.height);
Michalis Spyrouafa5d812017-11-30 14:25:57 +0000397
Michalis Spyrouafa5d812017-11-30 14:25:57 +0000398 // Perform validation step
Freddie Liardetafcbb8f2021-05-04 12:41:16 +0100399 ARM_COMPUTE_ERROR_THROW_ON(validate_arguments(src, dst, pool_info, indices, pool_size));
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100400
Georgios Pinitas5fdde992021-06-25 05:42:57 +0100401 const auto *uk = get_implementation(src->data_type(), src->data_layout(), pad_stride_info.stride().first, pool_size);
402 ARM_COMPUTE_ERROR_ON(uk == nullptr);
403
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100404 // Set instance variables
Sheri Zhang79144a62021-02-08 17:43:04 +0000405 _pool_info = pool_info;
406 _data_layout = src->data_layout();
407 _pool_size = pool_size;
408 _pool_stride_x = pad_stride_info.stride().first;
Georgios Pinitas5fdde992021-06-25 05:42:57 +0100409 _run_method = uk->ukernel;
410 _name = std::string("CpuPool2dKernel").append("/").append(uk->name);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100411
Sheri Zhang79144a62021-02-08 17:43:04 +0000412 if(_data_layout == DataLayout::NHWC)
Michalis Spyroucffb2a32020-09-08 16:26:38 +0100413 {
414 // Configure kernel window
SiCongLib88272e2021-02-24 15:40:57 +0000415 Window win = calculate_max_window(*dst, Steps());
Michele Di Giorgio19289042021-02-03 16:05:00 +0000416 ICpuKernel::configure(win);
Michalis Spyroucffb2a32020-09-08 16:26:38 +0100417 }
418 else
419 {
420 // Configure kernel window
Michele Di Giorgio19289042021-02-03 16:05:00 +0000421 auto win_config = validate_and_configure_window(src, dst, indices, pool_info, _num_elems_processed_per_iteration,
Freddie Liardetafcbb8f2021-05-04 12:41:16 +0100422 _border_size, pool_size.x(), pool_size.y());
Michalis Spyroucffb2a32020-09-08 16:26:38 +0100423 ARM_COMPUTE_ERROR_THROW_ON(win_config.first);
Michele Di Giorgio19289042021-02-03 16:05:00 +0000424 ICpuKernel::configure(win_config.second);
Michalis Spyroucffb2a32020-09-08 16:26:38 +0100425 }
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100426}
427
Manuel Bottinib4bb6a02021-05-24 16:01:32 +0100428Status CpuPool2dKernel::validate(const ITensorInfo *src, const ITensorInfo *dst, const PoolingLayerInfo &pool_info, const ITensorInfo *indices)
Michalis Spyrouafa5d812017-11-30 14:25:57 +0000429{
Michele Di Giorgio19289042021-02-03 16:05:00 +0000430 ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(src);
Michalis Spyrouafa5d812017-11-30 14:25:57 +0000431
Michalis Spyrouafa5d812017-11-30 14:25:57 +0000432 unsigned int num_elems_processed_per_iteration = 0;
433 BorderSize border_size(0);
434
Freddie Liardetafcbb8f2021-05-04 12:41:16 +0100435 const bool is_global_pooling = pool_info.is_global_pooling;
Michalis Spyrou57dac842018-03-01 16:03:50 +0000436
437 // Get data layout
Michele Di Giorgio19289042021-02-03 16:05:00 +0000438 const auto data_layout = pool_info.data_layout == DataLayout::UNKNOWN ? src->data_layout() : pool_info.data_layout;
Sang-Hoon Park11fedda2020-01-15 14:44:04 +0000439 const int idx_width = get_data_layout_dimension_index(data_layout, DataLayoutDimension::WIDTH);
440 const int idx_height = get_data_layout_dimension_index(data_layout, DataLayoutDimension::HEIGHT);
Michalis Spyrou57dac842018-03-01 16:03:50 +0000441
Freddie Liardetafcbb8f2021-05-04 12:41:16 +0100442 unsigned int pool_size_x = is_global_pooling ? src->dimension(idx_width) : pool_info.pool_size.width;
443 unsigned int pool_size_y = is_global_pooling ? src->dimension(idx_height) : pool_info.pool_size.height;
Michalis Spyrouafa5d812017-11-30 14:25:57 +0000444
Freddie Liardetafcbb8f2021-05-04 12:41:16 +0100445 ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments(src, dst, pool_info, indices, Size2D(pool_size_x, pool_size_y)));
Michele Di Giorgio19289042021-02-03 16:05:00 +0000446 ARM_COMPUTE_RETURN_ON_ERROR(validate_and_configure_window(src->clone().get(), dst->clone().get(),
Freddie Liardetafcbb8f2021-05-04 12:41:16 +0100447 (indices) ? indices->clone().get() : nullptr, pool_info, num_elems_processed_per_iteration, border_size,
Isabella Gottardi7567f5f2018-01-30 15:26:00 +0000448 pool_size_x, pool_size_y)
449 .first);
Michalis Spyrouafa5d812017-11-30 14:25:57 +0000450
451 return Status{};
452}
453
Manuel Bottinib4bb6a02021-05-24 16:01:32 +0100454void CpuPool2dKernel::run_op(ITensorPack &tensors, const Window &window, const ThreadInfo &info)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100455{
Moritz Pflanzerc186b572017-09-07 09:48:04 +0100456 ARM_COMPUTE_UNUSED(info);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100457 ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this);
Michele Di Giorgio19289042021-02-03 16:05:00 +0000458 ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(ICpuKernel::window(), window);
Georgios Pinitas5fdde992021-06-25 05:42:57 +0100459 ARM_COMPUTE_ERROR_ON(_run_method == nullptr);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100460
Sheri Zhang79144a62021-02-08 17:43:04 +0000461 const ITensor *src = tensors.get_const_tensor(TensorType::ACL_SRC_0);
462 ITensor *dst = tensors.get_tensor(TensorType::ACL_DST_0);
463 ITensor *indices = tensors.get_tensor(TensorType::ACL_DST_1);
Michele Di Giorgio19289042021-02-03 16:05:00 +0000464
Sheri Zhang79144a62021-02-08 17:43:04 +0000465 const unsigned int pool_stride_x = _pool_info.pad_stride_info.stride().first;
466 const unsigned int pool_stride_y = _pool_info.pad_stride_info.stride().second;
467 const unsigned int pool_size = _pool_info.pool_size.width;
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100468
Michele Di Giorgio19289042021-02-03 16:05:00 +0000469 Window window_src(window);
Georgios Pinitas14d9d982019-12-13 12:33:09 +0000470 if(_data_layout == DataLayout::NCHW)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100471 {
Michele Di Giorgio19289042021-02-03 16:05:00 +0000472 // Set step for src in x and y direction for the src
Michalis Spyrou57dac842018-03-01 16:03:50 +0000473 unsigned int window_x_inc = 0;
Sheri Zhang79144a62021-02-08 17:43:04 +0000474 switch(src->info()->data_type())
Pablo Tello0c34fe22017-06-26 17:17:42 +0100475 {
Michalis Spyrou57dac842018-03-01 16:03:50 +0000476 case DataType::QASYMM8:
Manuel Bottinib4bb8272019-12-18 18:01:27 +0000477 case DataType::QASYMM8_SIGNED:
Michalis Spyrou57dac842018-03-01 16:03:50 +0000478 {
479 window_x_inc = pool_stride_x;
480 if((pool_size == 2 || pool_size == 3) && pool_stride_x < 3)
481 {
482 window_x_inc = (pool_stride_x == 2) ? _num_elems_processed_per_iteration * 2 : _num_elems_processed_per_iteration;
483 }
484 break;
485 }
Pablo Tello77e6c552018-12-04 15:33:49 +0000486
Georgios Pinitas13d96e02018-08-23 11:20:23 +0100487 case DataType::F16:
Michalis Spyrou57dac842018-03-01 16:03:50 +0000488 case DataType::F32:
489 {
490 window_x_inc = pool_stride_x;
491 break;
492 }
493 default:
494 {
495 ARM_COMPUTE_ERROR("Not supported");
496 }
Georgios Pinitas55186712018-01-08 17:37:12 +0000497 }
Michele Di Giorgio19289042021-02-03 16:05:00 +0000498 window_src.set(Window::DimX, Window::Dimension(window.x().start() * pool_stride_x, window.x().end() * pool_stride_x, window_x_inc));
499 window_src.set(Window::DimY, Window::Dimension(window.y().start() * pool_stride_y, window.y().end() * pool_stride_y, pool_stride_y));
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100500 }
Michalis Spyrou57dac842018-03-01 16:03:50 +0000501 else
502 {
Michele Di Giorgio19289042021-02-03 16:05:00 +0000503 window_src.set(Window::DimX, Window::Dimension(0, 1, 1));
Sheri Zhang79144a62021-02-08 17:43:04 +0000504 window_src.set(Window::DimY, Window::Dimension(0, src->info()->dimension(1), pool_stride_x));
505 window_src.set(Window::DimZ, Window::Dimension(0, src->info()->dimension(2), pool_stride_y));
Michalis Spyrou57dac842018-03-01 16:03:50 +0000506 }
Georgios Pinitas5fdde992021-06-25 05:42:57 +0100507 _run_method(src, dst, indices, _pool_info, window_src, window);
Sheri Zhang79144a62021-02-08 17:43:04 +0000508}
509
Manuel Bottinib4bb6a02021-05-24 16:01:32 +0100510const char *CpuPool2dKernel::name() const
Sheri Zhang79144a62021-02-08 17:43:04 +0000511{
Georgios Pinitas5fdde992021-06-25 05:42:57 +0100512 return _name.c_str();
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100513}
Michele Di Giorgio19289042021-02-03 16:05:00 +0000514} // namespace kernels
515} // namespace cpu
morgolockcc1f6c92020-03-24 09:26:48 +0000516} // namespace arm_compute