blob: e159bb40a9bf5fd6eeb85578891f54118f135309 [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 */
Michele Di Giorgio19289042021-02-03 16:05:00 +000024#include "src/core/cpu/kernels/CpuPoolingKernel.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"
37#include "src/core/cpu/kernels/pooling/neon/list.h"
Sang-Hoon Park68dd25f2020-10-19 16:00:11 +010038#include "src/core/helpers/AutoConfiguration.h"
39#include "src/core/helpers/WindowHelpers.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 {
Sheri Zhang79144a62021-02-08 17:43:04 +000075 "poolingMxN_qasymm8_neon_nhwc",
76 [](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 {
80 "poolingMxN_qasymm8_signed_neon_nhwc",
81 [](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 {
86 "poolingMxN_fp16_neon_nhwc",
87 [](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 {
92 "poolingMxN_fp32_neon_nhwc",
93 [](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 {
98 "pooling2_qasymm8_neon_nchw",
99 [](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 {
103 "pooling3_qasymm8_neon_nchw",
104 [](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 {
108 "poolingMxN_qasymm8_neon_nchw",
109 [](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 {
113 "pooling2_qasymm8_signed_neon_nchw",
114 [](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 {
118 "pooling3_qasymm8_signed_neon_nchw",
119 [](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 {
123 "poolingMxN_qasymm8_signed_neon_nchw",
124 [](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 {
129 "pooling2_fp16_neon_nchw",
130 [](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 {
134 "pooling3_fp16_neon_nchw",
135 [](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 {
139 "poolingMxN_fp16_neon_nchw",
140 [](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 {
145 "pooling2_fp32_neon_nchw",
146 [](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 {
150 "pooling3_fp32_neon_nchw",
151 [](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 {
155 "pooling7_fp32_neon_nchw",
156 [](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 {
160 "poolingMxN_fp32_neon_nchw",
161 [](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,
morgolockcc1f6c92020-03-24 09:26:48 +0000186 unsigned int &pooled_w, unsigned int pooled_h, 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);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100189
Isabella Gottardi7567f5f2018-01-30 15:26:00 +0000190 int pool_stride_x = 0;
191 int pool_stride_y = 0;
Sang-Hoon Park0cb3da62020-01-15 12:39:56 +0000192 PoolingType pool_type = pool_info.pool_type;
193 const PadStrideInfo pad_stride_info = pool_info.pad_stride_info;
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100194 std::tie(pool_stride_x, pool_stride_y) = pad_stride_info.stride();
Michele Di Giorgio8af2dd62017-06-19 15:19:29 +0100195
Michele Di Giorgio19289042021-02-03 16:05:00 +0000196 ARM_COMPUTE_RETURN_ERROR_ON_CPU_F16_UNSUPPORTED(src);
morgolockcc1f6c92020-03-24 09:26:48 +0000197 if(indices)
198 {
Michele Di Giorgio19289042021-02-03 16:05:00 +0000199 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(src, 1, DataType::F32, DataType::F16);
morgolockcc1f6c92020-03-24 09:26:48 +0000200 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(indices, 1, DataType::U32);
201 ARM_COMPUTE_RETURN_ERROR_ON_MSG(pool_type != PoolingType::MAX, "Pooling indices only supported for MAX pooling method");
202 }
Michele Di Giorgio19289042021-02-03 16:05:00 +0000203 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(src, 1, DataType::QASYMM8, DataType::QASYMM8_SIGNED, DataType::F16, DataType::F32);
204 ARM_COMPUTE_RETURN_ERROR_ON(pool_type == PoolingType::L2 && is_data_type_quantized(src->data_type()));
205 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()
206 && (src->data_layout() == DataLayout::NHWC),
Michele Di Giorgio2c877192020-02-18 19:06:27 +0000207 "exclude_padding equal false is not supported for AVG Pooling with padding on quantized types");
Isabella Gottardi7567f5f2018-01-30 15:26:00 +0000208
Michele Di Giorgio19289042021-02-03 16:05:00 +0000209 if(dst->total_size() != 0)
Georgios Pinitas1dad50e2017-07-03 17:51:34 +0100210 {
Michele Di Giorgio19289042021-02-03 16:05:00 +0000211 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(src, dst);
212 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_LAYOUT(src, dst);
213 ARM_COMPUTE_RETURN_ERROR_ON((dst->dimension(get_data_layout_dimension_index(src->data_layout(), DataLayoutDimension::WIDTH)) != pooled_w)
214 || (dst->dimension(get_data_layout_dimension_index(src->data_layout(), DataLayoutDimension::HEIGHT)) != pooled_h));
morgolockcc1f6c92020-03-24 09:26:48 +0000215
216 if(indices)
217 {
218 ARM_COMPUTE_RETURN_ERROR_ON_MSG((pool_size != Size2D(2, 2)), "Pooling indices only supported for pool size 2x2");
morgolockcc1f6c92020-03-24 09:26:48 +0000219 ARM_COMPUTE_RETURN_ERROR_ON((indices->dimension(get_data_layout_dimension_index(indices->data_layout(), DataLayoutDimension::WIDTH)) != pooled_w)
220 || (indices->dimension(get_data_layout_dimension_index(indices->data_layout(), DataLayoutDimension::HEIGHT)) != pooled_h));
221 }
Georgios Pinitas1dad50e2017-07-03 17:51:34 +0100222 }
223
Sheri Zhang79144a62021-02-08 17:43:04 +0000224 const auto *uk = get_implementation(src->data_type(), src->data_layout(), pool_stride_x, pool_size);
225 ARM_COMPUTE_RETURN_ERROR_ON(uk == nullptr || uk->ukernel == nullptr);
226
Michalis Spyrouafa5d812017-11-30 14:25:57 +0000227 return Status{};
228}
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100229
Isabella Gottardi7567f5f2018-01-30 15:26:00 +0000230Status validate_arguments_pool_info(const unsigned int pool_size_x, const unsigned int pool_size_y)
Michalis Spyrouafa5d812017-11-30 14:25:57 +0000231{
Isabella Gottardi7567f5f2018-01-30 15:26:00 +0000232 ARM_COMPUTE_RETURN_ERROR_ON(pool_size_x == 0);
233 ARM_COMPUTE_RETURN_ERROR_ON(pool_size_y == 0);
Michalis Spyrouafa5d812017-11-30 14:25:57 +0000234
235 return Status{};
236}
237
Michele Di Giorgio19289042021-02-03 16:05:00 +0000238std::pair<Status, Window> validate_and_configure_window(ITensorInfo *src, ITensorInfo *dst, ITensorInfo *indices, const PoolingLayerInfo &pool_info,
morgolockcc1f6c92020-03-24 09:26:48 +0000239 unsigned int &num_elems_processed_per_iteration,
240 BorderSize &border_size,
Isabella Gottardi7567f5f2018-01-30 15:26:00 +0000241 unsigned int pooled_w, unsigned int pooled_h, int pool_size_x, int pool_size_y)
Michalis Spyrouafa5d812017-11-30 14:25:57 +0000242{
Michele Di Giorgio19289042021-02-03 16:05:00 +0000243 // dst auto inizialitation if not yet initialized
244 auto_init_if_empty(*dst, src->clone()->set_tensor_shape(compute_pool_shape(*src, pool_info)));
morgolockcc1f6c92020-03-24 09:26:48 +0000245 if(indices)
246 {
247 // Indices auto inizialitation if not yet initialized
Michele Di Giorgio19289042021-02-03 16:05:00 +0000248 auto_init_if_empty(*indices, (src->clone()->set_tensor_shape(compute_pool_shape(*src,
249 pool_info)))
morgolocke383c352020-04-03 16:57:46 +0100250 .set_data_type(DataType::U32) /* we store the offset to the element */);
morgolockcc1f6c92020-03-24 09:26:48 +0000251 }
Michele Di Giorgio19289042021-02-03 16:05:00 +0000252 const auto data_layout = pool_info.data_layout == DataLayout::UNKNOWN ? src->data_layout() : pool_info.data_layout;
Michalis Spyrouafa5d812017-11-30 14:25:57 +0000253 unsigned int num_elems_read_per_iteration = 0;
254 unsigned int num_elems_horizontal_window = 0;
Michalis Spyrouafa5d812017-11-30 14:25:57 +0000255 int pool_stride_x = 0;
256 int pool_stride_y = 0;
Michalis Spyrou57dac842018-03-01 16:03:50 +0000257 const int idx_width = get_data_layout_dimension_index(data_layout, DataLayoutDimension::WIDTH);
258 const int idx_height = get_data_layout_dimension_index(data_layout, DataLayoutDimension::HEIGHT);
Michele Di Giorgio19289042021-02-03 16:05:00 +0000259 const int src_width = src->dimension(idx_width);
260 const int src_height = src->dimension(idx_height);
Sang-Hoon Park0cb3da62020-01-15 12:39:56 +0000261 const PadStrideInfo pad_stride_info = pool_info.pad_stride_info;
Michalis Spyrouafa5d812017-11-30 14:25:57 +0000262 std::tie(pool_stride_x, pool_stride_y) = pad_stride_info.stride();
Isabella Gottardi7567f5f2018-01-30 15:26:00 +0000263 const int pool_pad_right = pad_stride_info.pad_right();
264 const int pool_pad_top = pad_stride_info.pad_top();
265 const int pool_pad_left = pad_stride_info.pad_left();
266 const int pool_pad_bottom = pad_stride_info.pad_bottom();
267 const bool is_square = pool_size_x == pool_size_y;
Michalis Spyrou57dac842018-03-01 16:03:50 +0000268
Michele Di Giorgio19289042021-02-03 16:05:00 +0000269 // Check dst dimensions
270 std::tie(pooled_w, pooled_h) = scaled_dimensions(src->dimension(idx_width),
271 src->dimension(idx_height),
Isabella Gottardi7567f5f2018-01-30 15:26:00 +0000272 pool_size_x,
273 pool_size_y,
Michalis Spyrouafa5d812017-11-30 14:25:57 +0000274 pad_stride_info);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100275
Isabella Gottardi7567f5f2018-01-30 15:26:00 +0000276 //If it's not squared and optimized will be executed the MxN
277 num_elems_read_per_iteration = 1;
278 num_elems_processed_per_iteration = 1;
279 num_elems_horizontal_window = 1;
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100280
Isabella Gottardi7567f5f2018-01-30 15:26:00 +0000281 if(is_square)
282 {
Michele Di Giorgio19289042021-02-03 16:05:00 +0000283 switch(src->data_type())
Isabella Gottardi7567f5f2018-01-30 15:26:00 +0000284 {
Isabella Gottardi7567f5f2018-01-30 15:26:00 +0000285 case DataType::QASYMM8:
Manuel Bottinib4bb8272019-12-18 18:01:27 +0000286 case DataType::QASYMM8_SIGNED:
Isabella Gottardi7567f5f2018-01-30 15:26:00 +0000287 switch(pool_size_x)
288 {
289 case 2:
290 num_elems_read_per_iteration = 16;
291 num_elems_processed_per_iteration = (pool_stride_x == 2) ? 8 : 15;
292 num_elems_horizontal_window = (pool_stride_x == 2) ? 8 : 16;
293 break;
294 case 3:
295 num_elems_read_per_iteration = 16;
296 num_elems_processed_per_iteration = (pool_stride_x == 2) ? 7 : 14;
297 num_elems_horizontal_window = (pool_stride_x == 2) ? 8 : 16;
298 break;
299 default:
300 break;
301 }
302 break;
Isabella Gottardi7567f5f2018-01-30 15:26:00 +0000303#ifdef __ARM_FEATURE_FP16_VECTOR_ARITHMETIC
304 case DataType::F16:
305 switch(pool_size_x)
306 {
307 case 2:
Isabella Gottardi7567f5f2018-01-30 15:26:00 +0000308 case 3:
309 num_elems_read_per_iteration = 4;
310 num_elems_processed_per_iteration = 1;
311 num_elems_horizontal_window = 1;
312 break;
313 default:
314 break;
315 }
316 break;
317#endif /* __ARM_FEATURE_FP16_VECTOR_ARITHMETIC */
318 case DataType::F32:
319 switch(pool_size_x)
320 {
321 case 2:
322 num_elems_read_per_iteration = 2;
323 break;
324 case 3:
325 num_elems_read_per_iteration = 4; // We use vload4 for pooling3
326 break;
327 case 7:
328 num_elems_read_per_iteration = 8; // We use vload8 for pooling7
329 break;
330 default:
331 break;
332 }
333 num_elems_processed_per_iteration = 1;
334 num_elems_horizontal_window = 1;
335 break;
336 default:
337 ARM_COMPUTE_ERROR("Element size not supported");
338 break;
339 }
340 }
Michalis Spyrou57dac842018-03-01 16:03:50 +0000341
342 bool window_changed = false;
343 Window win{};
344 if(data_layout == DataLayout::NCHW)
345 {
346 // Number of iterations in X dimension
347 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 +0000348 // Upper limit for the number of right/bottom border elements that are accessed
Michele Di Giorgio19289042021-02-03 16:05:00 +0000349 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;
350 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 +0000351 border_size = BorderSize(pool_pad_top, pool_pad_right, pool_pad_bottom, pool_pad_left);
352 border_size.right = std::max(upper_bound_w, pool_pad_right);
353 border_size.bottom = std::max(upper_bound_h, pool_pad_bottom);
Michele Di Giorgio19289042021-02-03 16:05:00 +0000354 TensorShape dst_shape{ src->tensor_shape() };
355 dst_shape.set(0, pooled_w);
356 dst_shape.set(1, pooled_h);
357 TensorInfo dst_info(src->clone()->set_tensor_shape(dst_shape));
358 win = calculate_max_window(dst_info, Steps(num_elems_processed_per_iteration));
Michele Di Giorgiobae22372021-02-12 17:34:17 +0000359 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 +0000360 AccessWindowHorizontal dst_access(dst, 0, num_elems_horizontal_window);
morgolockcc1f6c92020-03-24 09:26:48 +0000361 if(indices)
362 {
363 AccessWindowHorizontal indices_access(indices, 0, num_elems_horizontal_window);
Michele Di Giorgio19289042021-02-03 16:05:00 +0000364 window_changed = update_window_and_padding(win, src_access, dst_access, indices_access);
morgolockcc1f6c92020-03-24 09:26:48 +0000365 }
366 else
367 {
Michele Di Giorgio19289042021-02-03 16:05:00 +0000368 window_changed = update_window_and_padding(win, src_access, dst_access);
morgolockcc1f6c92020-03-24 09:26:48 +0000369 }
Michele Di Giorgio19289042021-02-03 16:05:00 +0000370 dst_access.set_valid_region(win, ValidRegion(Coordinates(), dst->tensor_shape()));
Michele Di Giorgiobae22372021-02-12 17:34:17 +0000371
372 border_size = src->padding();
Michalis Spyrouafa5d812017-11-30 14:25:57 +0000373 }
Michalis Spyrouafa5d812017-11-30 14:25:57 +0000374
375 Status err = (window_changed) ? ARM_COMPUTE_CREATE_ERROR(ErrorCode::RUNTIME_ERROR, "Insufficient Padding!") : Status{};
376 return std::make_pair(err, win);
377}
378} // namespace
379
Michele Di Giorgio19289042021-02-03 16:05:00 +0000380BorderSize CpuPoolingKernel::border_size() const
Michalis Spyrouafa5d812017-11-30 14:25:57 +0000381{
382 return _border_size;
383}
384
Michele Di Giorgio19289042021-02-03 16:05:00 +0000385void CpuPoolingKernel::configure(ITensorInfo *src, ITensorInfo *dst, const PoolingLayerInfo &pool_info, ITensorInfo *indices)
Michalis Spyrouafa5d812017-11-30 14:25:57 +0000386{
Michele Di Giorgio19289042021-02-03 16:05:00 +0000387 ARM_COMPUTE_ERROR_ON_NULLPTR(src, dst);
Sang-Hoon Park0cb3da62020-01-15 12:39:56 +0000388 const PadStrideInfo pad_stride_info = pool_info.pad_stride_info;
389 const bool is_global_pooling = pool_info.is_global_pooling;
Michalis Spyrou57dac842018-03-01 16:03:50 +0000390
391 // Get data layout
Michele Di Giorgio19289042021-02-03 16:05:00 +0000392 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 +0000393 const int idx_width = get_data_layout_dimension_index(data_layout, DataLayoutDimension::WIDTH);
394 const int idx_height = get_data_layout_dimension_index(data_layout, DataLayoutDimension::HEIGHT);
Michalis Spyrouafa5d812017-11-30 14:25:57 +0000395
396 // Update pool size in case of global pooling
Pablo Tello77e6c552018-12-04 15:33:49 +0000397 const Size2D pool_size(
Michele Di Giorgio19289042021-02-03 16:05:00 +0000398 is_global_pooling ? src->dimension(idx_width) : pool_info.pool_size.width,
399 is_global_pooling ? src->dimension(idx_height) : pool_info.pool_size.height);
Michalis Spyrouafa5d812017-11-30 14:25:57 +0000400
401 // Validate pool info before calling scaled_dimensions
Pablo Tello77e6c552018-12-04 15:33:49 +0000402 ARM_COMPUTE_ERROR_THROW_ON(validate_arguments_pool_info(pool_size.x(), pool_size.y()));
Michalis Spyrouafa5d812017-11-30 14:25:57 +0000403
Michele Di Giorgio19289042021-02-03 16:05:00 +0000404 // Check dst dimensions
Michalis Spyroubcfd09a2019-05-01 13:03:59 +0100405 unsigned int pooled_w;
406 unsigned int pooled_h;
Michele Di Giorgio19289042021-02-03 16:05:00 +0000407 std::tie(pooled_w, pooled_h) = scaled_dimensions(src->dimension(idx_width),
408 src->dimension(idx_height),
Pablo Tello77e6c552018-12-04 15:33:49 +0000409 pool_size.x(),
410 pool_size.y(),
Diego Lopez Recas61ef5bf2017-12-11 12:36:55 +0000411 pad_stride_info);
Michalis Spyrouafa5d812017-11-30 14:25:57 +0000412
Michalis Spyrouafa5d812017-11-30 14:25:57 +0000413 // Perform validation step
Michele Di Giorgio19289042021-02-03 16:05:00 +0000414 ARM_COMPUTE_ERROR_THROW_ON(validate_arguments(src, dst, pool_info, pooled_w, pooled_h, indices, pool_size));
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100415
416 // Set instance variables
Sheri Zhang79144a62021-02-08 17:43:04 +0000417 _pool_info = pool_info;
418 _data_layout = src->data_layout();
419 _pool_size = pool_size;
420 _pool_stride_x = pad_stride_info.stride().first;
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100421
Sheri Zhang79144a62021-02-08 17:43:04 +0000422 if(_data_layout == DataLayout::NHWC)
Michalis Spyroucffb2a32020-09-08 16:26:38 +0100423 {
424 // Configure kernel window
Michele Di Giorgio19289042021-02-03 16:05:00 +0000425 Window win = calculate_max_window(*dst, Steps());
Michalis Spyroucffb2a32020-09-08 16:26:38 +0100426 Coordinates coord;
Michele Di Giorgio19289042021-02-03 16:05:00 +0000427 coord.set_num_dimensions(dst->num_dimensions());
428 dst->set_valid_region(ValidRegion(coord, dst->tensor_shape()));
429 ICpuKernel::configure(win);
Michalis Spyroucffb2a32020-09-08 16:26:38 +0100430 }
431 else
432 {
433 // Configure kernel window
Michele Di Giorgio19289042021-02-03 16:05:00 +0000434 auto win_config = validate_and_configure_window(src, dst, indices, pool_info, _num_elems_processed_per_iteration,
435 _border_size, pooled_w, pooled_h, pool_size.x(), pool_size.y());
Michalis Spyroucffb2a32020-09-08 16:26:38 +0100436 ARM_COMPUTE_ERROR_THROW_ON(win_config.first);
Michele Di Giorgio19289042021-02-03 16:05:00 +0000437 ICpuKernel::configure(win_config.second);
Michalis Spyroucffb2a32020-09-08 16:26:38 +0100438 }
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100439}
440
Michele Di Giorgio19289042021-02-03 16:05:00 +0000441Status CpuPoolingKernel::validate(const ITensorInfo *src, const ITensorInfo *dst, const PoolingLayerInfo &pool_info, const ITensorInfo *indices)
Michalis Spyrouafa5d812017-11-30 14:25:57 +0000442{
Michele Di Giorgio19289042021-02-03 16:05:00 +0000443 ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(src);
Michalis Spyrouafa5d812017-11-30 14:25:57 +0000444
445 unsigned int pooled_w = 0;
446 unsigned int pooled_h = 0;
447 unsigned int num_elems_processed_per_iteration = 0;
448 BorderSize border_size(0);
449
Sang-Hoon Park0cb3da62020-01-15 12:39:56 +0000450 const bool is_global_pooling = pool_info.is_global_pooling;
Michalis Spyrou57dac842018-03-01 16:03:50 +0000451 unsigned int pool_size_x = 0;
452 unsigned int pool_size_y = 0;
453
454 // Get data layout
Michele Di Giorgio19289042021-02-03 16:05:00 +0000455 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 +0000456 const int idx_width = get_data_layout_dimension_index(data_layout, DataLayoutDimension::WIDTH);
457 const int idx_height = get_data_layout_dimension_index(data_layout, DataLayoutDimension::HEIGHT);
Michalis Spyrou57dac842018-03-01 16:03:50 +0000458
Michele Di Giorgio19289042021-02-03 16:05:00 +0000459 pool_size_x = is_global_pooling ? src->dimension(idx_width) : pool_info.pool_size.width;
460 pool_size_y = is_global_pooling ? src->dimension(idx_height) : pool_info.pool_size.height;
Michalis Spyrouafa5d812017-11-30 14:25:57 +0000461
Isabella Gottardi7567f5f2018-01-30 15:26:00 +0000462 // Validate pool info before calling scaled_dimensions
463 ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments_pool_info(pool_size_x, pool_size_y));
Michalis Spyrouafa5d812017-11-30 14:25:57 +0000464
Michele Di Giorgio19289042021-02-03 16:05:00 +0000465 // Check dst dimensions
466 std::tie(pooled_w, pooled_h) = scaled_dimensions(src->dimension(idx_width),
467 src->dimension(idx_height),
Isabella Gottardi7567f5f2018-01-30 15:26:00 +0000468 pool_size_x,
469 pool_size_y,
Sang-Hoon Park0cb3da62020-01-15 12:39:56 +0000470 pool_info.pad_stride_info);
Michalis Spyrouafa5d812017-11-30 14:25:57 +0000471
Michele Di Giorgio19289042021-02-03 16:05:00 +0000472 ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments(src, dst, pool_info, pooled_w, pooled_h, indices, Size2D(pool_size_x, pool_size_y)));
473 ARM_COMPUTE_RETURN_ON_ERROR(validate_and_configure_window(src->clone().get(), dst->clone().get(),
morgolockcc1f6c92020-03-24 09:26:48 +0000474 (indices) ? indices->clone().get() : nullptr, pool_info, num_elems_processed_per_iteration, border_size, pooled_w, pooled_h,
Isabella Gottardi7567f5f2018-01-30 15:26:00 +0000475 pool_size_x, pool_size_y)
476 .first);
Michalis Spyrouafa5d812017-11-30 14:25:57 +0000477
478 return Status{};
479}
480
Michele Di Giorgio19289042021-02-03 16:05:00 +0000481void CpuPoolingKernel::run_op(ITensorPack &tensors, const Window &window, const ThreadInfo &info)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100482{
Moritz Pflanzerc186b572017-09-07 09:48:04 +0100483 ARM_COMPUTE_UNUSED(info);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100484 ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this);
Michele Di Giorgio19289042021-02-03 16:05:00 +0000485 ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(ICpuKernel::window(), window);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100486
Sheri Zhang79144a62021-02-08 17:43:04 +0000487 const ITensor *src = tensors.get_const_tensor(TensorType::ACL_SRC_0);
488 ITensor *dst = tensors.get_tensor(TensorType::ACL_DST_0);
489 ITensor *indices = tensors.get_tensor(TensorType::ACL_DST_1);
Michele Di Giorgio19289042021-02-03 16:05:00 +0000490
Sheri Zhang79144a62021-02-08 17:43:04 +0000491 const unsigned int pool_stride_x = _pool_info.pad_stride_info.stride().first;
492 const unsigned int pool_stride_y = _pool_info.pad_stride_info.stride().second;
493 const unsigned int pool_size = _pool_info.pool_size.width;
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100494
Michele Di Giorgio19289042021-02-03 16:05:00 +0000495 Window window_src(window);
Georgios Pinitas14d9d982019-12-13 12:33:09 +0000496 if(_data_layout == DataLayout::NCHW)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100497 {
Michele Di Giorgio19289042021-02-03 16:05:00 +0000498 // Set step for src in x and y direction for the src
Michalis Spyrou57dac842018-03-01 16:03:50 +0000499 unsigned int window_x_inc = 0;
Sheri Zhang79144a62021-02-08 17:43:04 +0000500 switch(src->info()->data_type())
Pablo Tello0c34fe22017-06-26 17:17:42 +0100501 {
Michalis Spyrou57dac842018-03-01 16:03:50 +0000502 case DataType::QASYMM8:
Manuel Bottinib4bb8272019-12-18 18:01:27 +0000503 case DataType::QASYMM8_SIGNED:
Michalis Spyrou57dac842018-03-01 16:03:50 +0000504 {
505 window_x_inc = pool_stride_x;
506 if((pool_size == 2 || pool_size == 3) && pool_stride_x < 3)
507 {
508 window_x_inc = (pool_stride_x == 2) ? _num_elems_processed_per_iteration * 2 : _num_elems_processed_per_iteration;
509 }
510 break;
511 }
Pablo Tello77e6c552018-12-04 15:33:49 +0000512
Georgios Pinitas13d96e02018-08-23 11:20:23 +0100513 case DataType::F16:
Michalis Spyrou57dac842018-03-01 16:03:50 +0000514 case DataType::F32:
515 {
516 window_x_inc = pool_stride_x;
517 break;
518 }
519 default:
520 {
521 ARM_COMPUTE_ERROR("Not supported");
522 }
Georgios Pinitas55186712018-01-08 17:37:12 +0000523 }
Michele Di Giorgio19289042021-02-03 16:05:00 +0000524 window_src.set(Window::DimX, Window::Dimension(window.x().start() * pool_stride_x, window.x().end() * pool_stride_x, window_x_inc));
525 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 +0100526 }
Michalis Spyrou57dac842018-03-01 16:03:50 +0000527 else
528 {
Michele Di Giorgio19289042021-02-03 16:05:00 +0000529 window_src.set(Window::DimX, Window::Dimension(0, 1, 1));
Sheri Zhang79144a62021-02-08 17:43:04 +0000530 window_src.set(Window::DimY, Window::Dimension(0, src->info()->dimension(1), pool_stride_x));
531 window_src.set(Window::DimZ, Window::Dimension(0, src->info()->dimension(2), pool_stride_y));
Michalis Spyrou57dac842018-03-01 16:03:50 +0000532 }
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100533
Michele Di Giorgiobae22372021-02-12 17:34:17 +0000534 const auto *uk = get_implementation(src->info()->data_type(), _data_layout, _pool_stride_x, _pool_size);
Sheri Zhang79144a62021-02-08 17:43:04 +0000535 ARM_COMPUTE_ERROR_ON(uk == nullptr || uk->ukernel == nullptr);
536
537 uk->ukernel(src, dst, indices, _pool_info, window_src, window);
538}
539
540const char *CpuPoolingKernel::name() const
541{
542 return "CpuPoolingKernel";
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100543}
Michele Di Giorgio19289042021-02-03 16:05:00 +0000544} // namespace kernels
545} // namespace cpu
morgolockcc1f6c92020-03-24 09:26:48 +0000546} // namespace arm_compute