blob: 953a9ffb67fe2cb0b02943de51b64c187ba6d028 [file] [log] [blame]
Anthony Barbier6ff3b192017-09-04 18:44:23 +01001/*
Giorgio Arena5ae8d802021-11-18 18:02:13 +00002 * Copyright (c) 2017-2022 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
Giorgio Arena5ae8d802021-11-18 18:02:13 +000055static const std::vector<CpuPool2dKernel::PoolingKernel> available_kernels =
Anthony Barbier6ff3b192017-09-04 18:44:23 +010056{
Georgios Pinitasadaae7e2017-10-30 15:56:32 +000057 {
Georgios Pinitas5fdde992021-06-25 05:42:57 +010058 "neon_qu8_nhwc_poolMxN",
Giorgio Arena5ae8d802021-11-18 18:02:13 +000059 [](const PoolDataTypeISASelectorData & data) { return ((data.dl == DataLayout::NHWC) && (data.dt == DataType::QASYMM8)); },
Sheri Zhang79144a62021-02-08 17:43:04 +000060 REGISTER_QASYMM8_NEON(arm_compute::cpu::poolingMxN_qasymm8_neon_nhwc)
61 },
62 {
Georgios Pinitas5fdde992021-06-25 05:42:57 +010063 "neon_qs8_nhwc_poolMxN",
Giorgio Arena5ae8d802021-11-18 18:02:13 +000064 [](const PoolDataTypeISASelectorData & data) { return ((data.dl == DataLayout::NHWC) && (data.dt == DataType::QASYMM8_SIGNED)); },
Sheri Zhang79144a62021-02-08 17:43:04 +000065 REGISTER_QASYMM8_SIGNED_NEON(arm_compute::cpu::poolingMxN_qasymm8_signed_neon_nhwc)
66 },
67#if defined(__ARM_FEATURE_FP16_VECTOR_ARITHMETIC)
68 {
Georgios Pinitas5fdde992021-06-25 05:42:57 +010069 "neon_f16_nhwc_poolMxN",
Giorgio Arena5ae8d802021-11-18 18:02:13 +000070 [](const PoolDataTypeISASelectorData & data) { return ((data.dl == DataLayout::NHWC) && (data.dt == DataType::F16)); },
Sheri Zhang79144a62021-02-08 17:43:04 +000071 REGISTER_FP16_NEON(arm_compute::cpu::poolingMxN_fp16_neon_nhwc)
72 },
73#endif /* defined(__ARM_FEATURE_FP16_VECTOR_ARITHMETIC) */
74 {
Georgios Pinitas5fdde992021-06-25 05:42:57 +010075 "neon_fp32_nhwc_poolMxN",
Giorgio Arena5ae8d802021-11-18 18:02:13 +000076 [](const PoolDataTypeISASelectorData & data) { return ((data.dl == DataLayout::NHWC) && (data.dt == DataType::F32)); },
Sheri Zhang79144a62021-02-08 17:43:04 +000077 REGISTER_FP32_NEON(arm_compute::cpu::poolingMxN_fp32_neon_nhwc)
78 },
79#if defined(ENABLE_NCHW_KERNELS)
80 {
Georgios Pinitas5fdde992021-06-25 05:42:57 +010081 "neon_qu8_nchw_pool2",
Giorgio Arena5ae8d802021-11-18 18:02:13 +000082 [](const PoolDataTypeISASelectorData & 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)); },
Sheri Zhang79144a62021-02-08 17:43:04 +000083 REGISTER_QASYMM8_NEON(arm_compute::cpu::pooling2_quantized_neon_nchw<uint8_t>)
84 },
85 {
Georgios Pinitas5fdde992021-06-25 05:42:57 +010086 "neon_qu8_nchw_pool3",
Giorgio Arena5ae8d802021-11-18 18:02:13 +000087 [](const PoolDataTypeISASelectorData & 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)); },
Sheri Zhang79144a62021-02-08 17:43:04 +000088 REGISTER_QASYMM8_NEON(arm_compute::cpu::pooling3_quantized_neon_nchw<uint8_t>)
89 },
90 {
Georgios Pinitas5fdde992021-06-25 05:42:57 +010091 "neon_qu8_nchw_poolMxN",
Giorgio Arena5ae8d802021-11-18 18:02:13 +000092 [](const PoolDataTypeISASelectorData & data) { return ((data.dl == DataLayout::NCHW) && (data.dt == DataType::QASYMM8)); },
Sheri Zhang79144a62021-02-08 17:43:04 +000093 REGISTER_QASYMM8_NEON(arm_compute::cpu::poolingMxN_quantized_neon_nchw<uint8_t>)
94 },
95 {
Georgios Pinitas5fdde992021-06-25 05:42:57 +010096 "neon_qs8_nchw_pool2",
Giorgio Arena5ae8d802021-11-18 18:02:13 +000097 [](const PoolDataTypeISASelectorData & 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)); },
Sheri Zhang79144a62021-02-08 17:43:04 +000098 REGISTER_QASYMM8_SIGNED_NEON(arm_compute::cpu::pooling2_quantized_neon_nchw<int8_t>)
99 },
100 {
Georgios Pinitas5fdde992021-06-25 05:42:57 +0100101 "neon_qs8_nchw_pool3",
Giorgio Arena5ae8d802021-11-18 18:02:13 +0000102 [](const PoolDataTypeISASelectorData & 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)); },
Sheri Zhang79144a62021-02-08 17:43:04 +0000103 REGISTER_QASYMM8_SIGNED_NEON(arm_compute::cpu::pooling3_quantized_neon_nchw<int8_t>)
104 },
105 {
Georgios Pinitas5fdde992021-06-25 05:42:57 +0100106 "neon_qs8_nchw_poolMxN",
Giorgio Arena5ae8d802021-11-18 18:02:13 +0000107 [](const PoolDataTypeISASelectorData & data) { return ((data.dl == DataLayout::NCHW) && (data.dt == DataType::QASYMM8_SIGNED)); },
Sheri Zhang79144a62021-02-08 17:43:04 +0000108 REGISTER_QASYMM8_SIGNED_NEON(arm_compute::cpu::poolingMxN_quantized_neon_nchw<int8_t>)
109 },
110#if defined(__ARM_FEATURE_FP16_VECTOR_ARITHMETIC)
111 {
Georgios Pinitas5fdde992021-06-25 05:42:57 +0100112 "neon_fp16_nchw_pool2",
Giorgio Arena5ae8d802021-11-18 18:02:13 +0000113 [](const PoolDataTypeISASelectorData & data) { return ((data.dl == DataLayout::NCHW) && (data.dt == DataType::F16 && data.isa.fp16) && (data.pool_size.x() == data.pool_size.y()) && (data.pool_size.x() == 2)); },
Sheri Zhang79144a62021-02-08 17:43:04 +0000114 REGISTER_FP16_NEON(arm_compute::cpu::pooling2_fp16_neon_nchw)
115 },
116 {
Georgios Pinitas5fdde992021-06-25 05:42:57 +0100117 "neon_fp16_nchw_pool3",
Giorgio Arena5ae8d802021-11-18 18:02:13 +0000118 [](const PoolDataTypeISASelectorData & data) { return ((data.dl == DataLayout::NCHW) && (data.dt == DataType::F16 && data.isa.fp16) && (data.pool_size.x() == data.pool_size.y()) && (data.pool_size.x() == 3)); },
Sheri Zhang79144a62021-02-08 17:43:04 +0000119 REGISTER_FP16_NEON(arm_compute::cpu::pooling3_fp16_neon_nchw)
120 },
121 {
Georgios Pinitas5fdde992021-06-25 05:42:57 +0100122 "neon_fp16_nchw_poolMxN",
Giorgio Arena5ae8d802021-11-18 18:02:13 +0000123 [](const PoolDataTypeISASelectorData & data) { return ((data.dl == DataLayout::NCHW) && (data.dt == DataType::F16 && data.isa.fp16)); },
Sheri Zhang79144a62021-02-08 17:43:04 +0000124 REGISTER_FP16_NEON(arm_compute::cpu::poolingMxN_fp16_neon_nchw)
125 },
126#endif /* defined(__ARM_FEATURE_FP16_VECTOR_ARITHMETIC) */
127 {
Georgios Pinitas5fdde992021-06-25 05:42:57 +0100128 "neon_fp32_nchw_pool2",
Giorgio Arena5ae8d802021-11-18 18:02:13 +0000129 [](const PoolDataTypeISASelectorData & data) { return ((data.dl == DataLayout::NCHW) && (data.dt == DataType::F32) && (data.pool_size.x() == data.pool_size.y()) && (data.pool_size.x() == 2)); },
Sheri Zhang79144a62021-02-08 17:43:04 +0000130 REGISTER_FP32_NEON(arm_compute::cpu::pooling2_fp32_neon_nchw)
131 },
132 {
Georgios Pinitas5fdde992021-06-25 05:42:57 +0100133 "neon_fp32_nchw_pool3",
Giorgio Arena5ae8d802021-11-18 18:02:13 +0000134 [](const PoolDataTypeISASelectorData & data) { return ((data.dl == DataLayout::NCHW) && (data.dt == DataType::F32) && (data.pool_size.x() == data.pool_size.y()) && (data.pool_size.x() == 3)); },
Sheri Zhang79144a62021-02-08 17:43:04 +0000135 REGISTER_FP32_NEON(arm_compute::cpu::pooling3_fp32_neon_nchw)
136 },
137 {
Georgios Pinitas5fdde992021-06-25 05:42:57 +0100138 "neon_fp32_nchw_pool7",
Giorgio Arena5ae8d802021-11-18 18:02:13 +0000139 [](const PoolDataTypeISASelectorData & data) { return ((data.dl == DataLayout::NCHW) && (data.dt == DataType::F32) && (data.pool_size.x() == data.pool_size.y()) && (data.pool_size.x() == 7)); },
Sheri Zhang79144a62021-02-08 17:43:04 +0000140 REGISTER_FP32_NEON(arm_compute::cpu::pooling7_fp32_neon_nchw)
141 },
142 {
Georgios Pinitas5fdde992021-06-25 05:42:57 +0100143 "neon_fp32_nchw_poolMxN",
Giorgio Arena5ae8d802021-11-18 18:02:13 +0000144 [](const PoolDataTypeISASelectorData & data) { return ((data.dl == DataLayout::NCHW) && (data.dt == DataType::F32)); },
Sheri Zhang79144a62021-02-08 17:43:04 +0000145 REGISTER_FP32_NEON(arm_compute::cpu::poolingMxN_fp32_neon_nchw)
146 },
147#endif /* defined(ENABLE_NCHW_KERNELS) */
148};
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100149
Michele Di Giorgio19289042021-02-03 16:05:00 +0000150Status validate_arguments(const ITensorInfo *src, const ITensorInfo *dst, const PoolingLayerInfo &pool_info,
Freddie Liardetafcbb8f2021-05-04 12:41:16 +0100151 const ITensorInfo *indices, Size2D pool_size)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100152{
Michele Di Giorgio19289042021-02-03 16:05:00 +0000153 ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(src, dst);
Freddie Liardetafcbb8f2021-05-04 12:41:16 +0100154 ARM_COMPUTE_RETURN_ERROR_ON(pool_size.x() == 0);
155 ARM_COMPUTE_RETURN_ERROR_ON(pool_size.y() == 0);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100156
Isabella Gottardi7567f5f2018-01-30 15:26:00 +0000157 int pool_stride_x = 0;
158 int pool_stride_y = 0;
Freddie Liardetafcbb8f2021-05-04 12:41:16 +0100159 int output_width = 0;
160 int output_height = 0;
Sang-Hoon Park0cb3da62020-01-15 12:39:56 +0000161 PoolingType pool_type = pool_info.pool_type;
162 const PadStrideInfo pad_stride_info = pool_info.pad_stride_info;
Freddie Liardetafcbb8f2021-05-04 12:41:16 +0100163 const auto data_layout = pool_info.data_layout == DataLayout::UNKNOWN ? src->data_layout() : pool_info.data_layout;
164 const int idx_width = get_data_layout_dimension_index(data_layout, DataLayoutDimension::WIDTH);
165 const int idx_height = get_data_layout_dimension_index(data_layout, DataLayoutDimension::HEIGHT);
166
SiCongLic4270cf2021-12-22 11:22:40 +0000167 ARM_COMPUTE_RETURN_ERROR_ON_MSG((!is_data_type_float(src->data_type()))
168 && (is_pool_region_entirely_outside_input(pool_info)),
169 "Pooling region that is entirely outside input tensor is unsupported for non-float types");
170
Freddie Liardetafcbb8f2021-05-04 12:41:16 +0100171 std::tie(output_width, output_height) = scaled_dimensions_signed(src->tensor_shape()[idx_width], src->tensor_shape()[idx_height],
172 pool_size.x(), pool_size.y(), pool_info.pad_stride_info);
173 ARM_COMPUTE_RETURN_ERROR_ON_MSG((output_width < 1 || output_height < 1), "Calculated output dimension size is invalid");
174
175 TensorInfo out_info(TensorInfo(compute_pool_shape(*src, pool_info), 1, dst->data_type()));
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100176 std::tie(pool_stride_x, pool_stride_y) = pad_stride_info.stride();
Michele Di Giorgio8af2dd62017-06-19 15:19:29 +0100177
Michele Di Giorgio19289042021-02-03 16:05:00 +0000178 ARM_COMPUTE_RETURN_ERROR_ON_CPU_F16_UNSUPPORTED(src);
morgolockcc1f6c92020-03-24 09:26:48 +0000179 if(indices)
180 {
Michele Di Giorgio19289042021-02-03 16:05:00 +0000181 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(src, 1, DataType::F32, DataType::F16);
morgolockcc1f6c92020-03-24 09:26:48 +0000182 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(indices, 1, DataType::U32);
183 ARM_COMPUTE_RETURN_ERROR_ON_MSG(pool_type != PoolingType::MAX, "Pooling indices only supported for MAX pooling method");
184 }
Michele Di Giorgio19289042021-02-03 16:05:00 +0000185 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(src, 1, DataType::QASYMM8, DataType::QASYMM8_SIGNED, DataType::F16, DataType::F32);
186 ARM_COMPUTE_RETURN_ERROR_ON(pool_type == PoolingType::L2 && is_data_type_quantized(src->data_type()));
187 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()
188 && (src->data_layout() == DataLayout::NHWC),
Michele Di Giorgio2c877192020-02-18 19:06:27 +0000189 "exclude_padding equal false is not supported for AVG Pooling with padding on quantized types");
Isabella Gottardi7567f5f2018-01-30 15:26:00 +0000190
Michele Di Giorgio19289042021-02-03 16:05:00 +0000191 if(dst->total_size() != 0)
Georgios Pinitas1dad50e2017-07-03 17:51:34 +0100192 {
Michele Di Giorgio19289042021-02-03 16:05:00 +0000193 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(src, dst);
194 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_LAYOUT(src, dst);
Freddie Liardetafcbb8f2021-05-04 12:41:16 +0100195 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_SHAPES(dst, &out_info);
morgolockcc1f6c92020-03-24 09:26:48 +0000196 if(indices)
197 {
198 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 +0100199 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_SHAPES(indices, &out_info);
morgolockcc1f6c92020-03-24 09:26:48 +0000200 }
Georgios Pinitas1dad50e2017-07-03 17:51:34 +0100201 }
202
Giorgio Arena5ae8d802021-11-18 18:02:13 +0000203 const auto *uk = CpuPool2dKernel::get_implementation(PoolDataTypeISASelectorData{ src->data_type(), src->data_layout(), pool_stride_x, pool_size, CPUInfo::get().get_isa() });
Sheri Zhang79144a62021-02-08 17:43:04 +0000204 ARM_COMPUTE_RETURN_ERROR_ON(uk == nullptr || uk->ukernel == nullptr);
205
Michalis Spyrouafa5d812017-11-30 14:25:57 +0000206 return Status{};
207}
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100208
Michele Di Giorgio19289042021-02-03 16:05:00 +0000209std::pair<Status, Window> validate_and_configure_window(ITensorInfo *src, ITensorInfo *dst, ITensorInfo *indices, const PoolingLayerInfo &pool_info,
morgolockcc1f6c92020-03-24 09:26:48 +0000210 unsigned int &num_elems_processed_per_iteration,
Freddie Liardetafcbb8f2021-05-04 12:41:16 +0100211 int pool_size_x, int pool_size_y)
Michalis Spyrouafa5d812017-11-30 14:25:57 +0000212{
Michele Di Giorgio19289042021-02-03 16:05:00 +0000213 // dst auto inizialitation if not yet initialized
214 auto_init_if_empty(*dst, src->clone()->set_tensor_shape(compute_pool_shape(*src, pool_info)));
morgolockcc1f6c92020-03-24 09:26:48 +0000215 if(indices)
216 {
217 // Indices auto inizialitation if not yet initialized
Michele Di Giorgio19289042021-02-03 16:05:00 +0000218 auto_init_if_empty(*indices, (src->clone()->set_tensor_shape(compute_pool_shape(*src,
219 pool_info)))
morgolocke383c352020-04-03 16:57:46 +0100220 .set_data_type(DataType::U32) /* we store the offset to the element */);
morgolockcc1f6c92020-03-24 09:26:48 +0000221 }
Freddie Liardetded36632021-09-03 15:08:23 +0100222 const auto data_layout = pool_info.data_layout == DataLayout::UNKNOWN ? src->data_layout() : pool_info.data_layout;
Freddie Liardetded36632021-09-03 15:08:23 +0100223
224 int pool_stride_x = 0;
225 int pool_stride_y = 0;
226 const int idx_width = get_data_layout_dimension_index(data_layout, DataLayoutDimension::WIDTH);
227 const int idx_height = get_data_layout_dimension_index(data_layout, DataLayoutDimension::HEIGHT);
228 const PadStrideInfo pad_stride_info = pool_info.pad_stride_info;
229
Michalis Spyrouafa5d812017-11-30 14:25:57 +0000230 std::tie(pool_stride_x, pool_stride_y) = pad_stride_info.stride();
Freddie Liardetded36632021-09-03 15:08:23 +0100231 const bool is_square = pool_size_x == pool_size_y;
232 const unsigned int pooled_w = dst->dimension(idx_width);
233 const unsigned int pooled_h = dst->dimension(idx_height);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100234
Isabella Gottardi7567f5f2018-01-30 15:26:00 +0000235 //If it's not squared and optimized will be executed the MxN
Isabella Gottardi7567f5f2018-01-30 15:26:00 +0000236 num_elems_processed_per_iteration = 1;
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100237
Isabella Gottardi7567f5f2018-01-30 15:26:00 +0000238 if(is_square)
239 {
Michele Di Giorgio19289042021-02-03 16:05:00 +0000240 switch(src->data_type())
Isabella Gottardi7567f5f2018-01-30 15:26:00 +0000241 {
Isabella Gottardi7567f5f2018-01-30 15:26:00 +0000242 case DataType::QASYMM8:
Manuel Bottinib4bb8272019-12-18 18:01:27 +0000243 case DataType::QASYMM8_SIGNED:
Isabella Gottardi7567f5f2018-01-30 15:26:00 +0000244 switch(pool_size_x)
245 {
246 case 2:
Isabella Gottardi7567f5f2018-01-30 15:26:00 +0000247 num_elems_processed_per_iteration = (pool_stride_x == 2) ? 8 : 15;
Isabella Gottardi7567f5f2018-01-30 15:26:00 +0000248 break;
249 case 3:
Isabella Gottardi7567f5f2018-01-30 15:26:00 +0000250 num_elems_processed_per_iteration = (pool_stride_x == 2) ? 7 : 14;
Isabella Gottardi7567f5f2018-01-30 15:26:00 +0000251 break;
252 default:
253 break;
254 }
255 break;
Isabella Gottardi7567f5f2018-01-30 15:26:00 +0000256#ifdef __ARM_FEATURE_FP16_VECTOR_ARITHMETIC
257 case DataType::F16:
Freddie Liardetded36632021-09-03 15:08:23 +0100258 num_elems_processed_per_iteration = 1;
Isabella Gottardi7567f5f2018-01-30 15:26:00 +0000259 break;
260#endif /* __ARM_FEATURE_FP16_VECTOR_ARITHMETIC */
261 case DataType::F32:
Isabella Gottardi7567f5f2018-01-30 15:26:00 +0000262 num_elems_processed_per_iteration = 1;
Isabella Gottardi7567f5f2018-01-30 15:26:00 +0000263 break;
264 default:
265 ARM_COMPUTE_ERROR("Element size not supported");
266 break;
267 }
268 }
Michalis Spyrou57dac842018-03-01 16:03:50 +0000269
270 bool window_changed = false;
271 Window win{};
Freddie Liardetded36632021-09-03 15:08:23 +0100272 // Upper limit for the number of right/bottom border elements that are accessed
273 TensorShape dst_shape{ src->tensor_shape() };
274 dst_shape.set(0, pooled_w);
275 dst_shape.set(1, pooled_h);
276 TensorInfo dst_info(src->clone()->set_tensor_shape(dst_shape));
277 win = calculate_max_window(dst_info, Steps(num_elems_processed_per_iteration));
Michalis Spyrouafa5d812017-11-30 14:25:57 +0000278
279 Status err = (window_changed) ? ARM_COMPUTE_CREATE_ERROR(ErrorCode::RUNTIME_ERROR, "Insufficient Padding!") : Status{};
280 return std::make_pair(err, win);
281}
282} // namespace
283
Manuel Bottinib4bb6a02021-05-24 16:01:32 +0100284void CpuPool2dKernel::configure(ITensorInfo *src, ITensorInfo *dst, const PoolingLayerInfo &pool_info, ITensorInfo *indices)
Michalis Spyrouafa5d812017-11-30 14:25:57 +0000285{
Michele Di Giorgio19289042021-02-03 16:05:00 +0000286 ARM_COMPUTE_ERROR_ON_NULLPTR(src, dst);
Sang-Hoon Park0cb3da62020-01-15 12:39:56 +0000287 const PadStrideInfo pad_stride_info = pool_info.pad_stride_info;
288 const bool is_global_pooling = pool_info.is_global_pooling;
Michalis Spyrou57dac842018-03-01 16:03:50 +0000289
290 // Get data layout
Michele Di Giorgio19289042021-02-03 16:05:00 +0000291 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 +0000292 const int idx_width = get_data_layout_dimension_index(data_layout, DataLayoutDimension::WIDTH);
293 const int idx_height = get_data_layout_dimension_index(data_layout, DataLayoutDimension::HEIGHT);
Michalis Spyrouafa5d812017-11-30 14:25:57 +0000294
295 // Update pool size in case of global pooling
Pablo Tello77e6c552018-12-04 15:33:49 +0000296 const Size2D pool_size(
Michele Di Giorgio19289042021-02-03 16:05:00 +0000297 is_global_pooling ? src->dimension(idx_width) : pool_info.pool_size.width,
298 is_global_pooling ? src->dimension(idx_height) : pool_info.pool_size.height);
Michalis Spyrouafa5d812017-11-30 14:25:57 +0000299
Michalis Spyrouafa5d812017-11-30 14:25:57 +0000300 // Perform validation step
Freddie Liardetafcbb8f2021-05-04 12:41:16 +0100301 ARM_COMPUTE_ERROR_THROW_ON(validate_arguments(src, dst, pool_info, indices, pool_size));
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100302
Giorgio Arena5ae8d802021-11-18 18:02:13 +0000303 const auto *uk = CpuPool2dKernel::get_implementation(PoolDataTypeISASelectorData{ src->data_type(), src->data_layout(), (int)pad_stride_info.stride().first, pool_size, CPUInfo::get().get_isa() });
Georgios Pinitas5fdde992021-06-25 05:42:57 +0100304 ARM_COMPUTE_ERROR_ON(uk == nullptr);
305
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100306 // Set instance variables
Sheri Zhang79144a62021-02-08 17:43:04 +0000307 _pool_info = pool_info;
308 _data_layout = src->data_layout();
309 _pool_size = pool_size;
310 _pool_stride_x = pad_stride_info.stride().first;
Georgios Pinitas5fdde992021-06-25 05:42:57 +0100311 _run_method = uk->ukernel;
312 _name = std::string("CpuPool2dKernel").append("/").append(uk->name);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100313
Sheri Zhang79144a62021-02-08 17:43:04 +0000314 if(_data_layout == DataLayout::NHWC)
Michalis Spyroucffb2a32020-09-08 16:26:38 +0100315 {
316 // Configure kernel window
SiCongLib88272e2021-02-24 15:40:57 +0000317 Window win = calculate_max_window(*dst, Steps());
Giorgio Arena5ae8d802021-11-18 18:02:13 +0000318 NewICpuKernel::configure(win);
Michalis Spyroucffb2a32020-09-08 16:26:38 +0100319 }
320 else
321 {
322 // Configure kernel window
Michele Di Giorgio19289042021-02-03 16:05:00 +0000323 auto win_config = validate_and_configure_window(src, dst, indices, pool_info, _num_elems_processed_per_iteration,
Freddie Liardetded36632021-09-03 15:08:23 +0100324 pool_size.x(), pool_size.y());
Michalis Spyroucffb2a32020-09-08 16:26:38 +0100325 ARM_COMPUTE_ERROR_THROW_ON(win_config.first);
Giorgio Arena5ae8d802021-11-18 18:02:13 +0000326 NewICpuKernel::configure(win_config.second);
Michalis Spyroucffb2a32020-09-08 16:26:38 +0100327 }
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100328}
329
Manuel Bottinib4bb6a02021-05-24 16:01:32 +0100330Status CpuPool2dKernel::validate(const ITensorInfo *src, const ITensorInfo *dst, const PoolingLayerInfo &pool_info, const ITensorInfo *indices)
Michalis Spyrouafa5d812017-11-30 14:25:57 +0000331{
Michele Di Giorgio19289042021-02-03 16:05:00 +0000332 ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(src);
Michalis Spyrouafa5d812017-11-30 14:25:57 +0000333
Michalis Spyrouafa5d812017-11-30 14:25:57 +0000334 unsigned int num_elems_processed_per_iteration = 0;
Michalis Spyrouafa5d812017-11-30 14:25:57 +0000335
Freddie Liardetafcbb8f2021-05-04 12:41:16 +0100336 const bool is_global_pooling = pool_info.is_global_pooling;
Michalis Spyrou57dac842018-03-01 16:03:50 +0000337
338 // Get data layout
Michele Di Giorgio19289042021-02-03 16:05:00 +0000339 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 +0000340 const int idx_width = get_data_layout_dimension_index(data_layout, DataLayoutDimension::WIDTH);
341 const int idx_height = get_data_layout_dimension_index(data_layout, DataLayoutDimension::HEIGHT);
Michalis Spyrou57dac842018-03-01 16:03:50 +0000342
Freddie Liardetafcbb8f2021-05-04 12:41:16 +0100343 unsigned int pool_size_x = is_global_pooling ? src->dimension(idx_width) : pool_info.pool_size.width;
344 unsigned int pool_size_y = is_global_pooling ? src->dimension(idx_height) : pool_info.pool_size.height;
Michalis Spyrouafa5d812017-11-30 14:25:57 +0000345
Freddie Liardetafcbb8f2021-05-04 12:41:16 +0100346 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 +0000347 ARM_COMPUTE_RETURN_ON_ERROR(validate_and_configure_window(src->clone().get(), dst->clone().get(),
Freddie Liardetded36632021-09-03 15:08:23 +0100348 (indices) ? indices->clone().get() : nullptr, pool_info, num_elems_processed_per_iteration,
Isabella Gottardi7567f5f2018-01-30 15:26:00 +0000349 pool_size_x, pool_size_y)
350 .first);
Michalis Spyrouafa5d812017-11-30 14:25:57 +0000351
352 return Status{};
353}
354
Manuel Bottinib4bb6a02021-05-24 16:01:32 +0100355void CpuPool2dKernel::run_op(ITensorPack &tensors, const Window &window, const ThreadInfo &info)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100356{
Moritz Pflanzerc186b572017-09-07 09:48:04 +0100357 ARM_COMPUTE_UNUSED(info);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100358 ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this);
Giorgio Arena5ae8d802021-11-18 18:02:13 +0000359 ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(NewICpuKernel::window(), window);
Georgios Pinitas5fdde992021-06-25 05:42:57 +0100360 ARM_COMPUTE_ERROR_ON(_run_method == nullptr);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100361
Sheri Zhang79144a62021-02-08 17:43:04 +0000362 const ITensor *src = tensors.get_const_tensor(TensorType::ACL_SRC_0);
363 ITensor *dst = tensors.get_tensor(TensorType::ACL_DST_0);
364 ITensor *indices = tensors.get_tensor(TensorType::ACL_DST_1);
Michele Di Giorgio19289042021-02-03 16:05:00 +0000365
Sheri Zhang79144a62021-02-08 17:43:04 +0000366 const unsigned int pool_stride_x = _pool_info.pad_stride_info.stride().first;
367 const unsigned int pool_stride_y = _pool_info.pad_stride_info.stride().second;
368 const unsigned int pool_size = _pool_info.pool_size.width;
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100369
Michele Di Giorgio19289042021-02-03 16:05:00 +0000370 Window window_src(window);
Georgios Pinitas14d9d982019-12-13 12:33:09 +0000371 if(_data_layout == DataLayout::NCHW)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100372 {
Michele Di Giorgio19289042021-02-03 16:05:00 +0000373 // Set step for src in x and y direction for the src
Michalis Spyrou57dac842018-03-01 16:03:50 +0000374 unsigned int window_x_inc = 0;
Sheri Zhang79144a62021-02-08 17:43:04 +0000375 switch(src->info()->data_type())
Pablo Tello0c34fe22017-06-26 17:17:42 +0100376 {
Michalis Spyrou57dac842018-03-01 16:03:50 +0000377 case DataType::QASYMM8:
Manuel Bottinib4bb8272019-12-18 18:01:27 +0000378 case DataType::QASYMM8_SIGNED:
Michalis Spyrou57dac842018-03-01 16:03:50 +0000379 {
380 window_x_inc = pool_stride_x;
381 if((pool_size == 2 || pool_size == 3) && pool_stride_x < 3)
382 {
383 window_x_inc = (pool_stride_x == 2) ? _num_elems_processed_per_iteration * 2 : _num_elems_processed_per_iteration;
384 }
385 break;
386 }
Pablo Tello77e6c552018-12-04 15:33:49 +0000387
Georgios Pinitas13d96e02018-08-23 11:20:23 +0100388 case DataType::F16:
Michalis Spyrou57dac842018-03-01 16:03:50 +0000389 case DataType::F32:
390 {
391 window_x_inc = pool_stride_x;
392 break;
393 }
394 default:
395 {
396 ARM_COMPUTE_ERROR("Not supported");
397 }
Georgios Pinitas55186712018-01-08 17:37:12 +0000398 }
Michele Di Giorgio19289042021-02-03 16:05:00 +0000399 window_src.set(Window::DimX, Window::Dimension(window.x().start() * pool_stride_x, window.x().end() * pool_stride_x, window_x_inc));
400 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 +0100401 }
Michalis Spyrou57dac842018-03-01 16:03:50 +0000402 else
403 {
Michele Di Giorgio19289042021-02-03 16:05:00 +0000404 window_src.set(Window::DimX, Window::Dimension(0, 1, 1));
Sheri Zhang79144a62021-02-08 17:43:04 +0000405 window_src.set(Window::DimY, Window::Dimension(0, src->info()->dimension(1), pool_stride_x));
406 window_src.set(Window::DimZ, Window::Dimension(0, src->info()->dimension(2), pool_stride_y));
Michalis Spyrou57dac842018-03-01 16:03:50 +0000407 }
Georgios Pinitas5fdde992021-06-25 05:42:57 +0100408 _run_method(src, dst, indices, _pool_info, window_src, window);
Sheri Zhang79144a62021-02-08 17:43:04 +0000409}
410
Manuel Bottinib4bb6a02021-05-24 16:01:32 +0100411const char *CpuPool2dKernel::name() const
Sheri Zhang79144a62021-02-08 17:43:04 +0000412{
Georgios Pinitas5fdde992021-06-25 05:42:57 +0100413 return _name.c_str();
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100414}
Giorgio Arena5ae8d802021-11-18 18:02:13 +0000415
416const std::vector<CpuPool2dKernel::PoolingKernel> &CpuPool2dKernel::get_available_kernels()
417{
418 return available_kernels;
419}
420
Michele Di Giorgio19289042021-02-03 16:05:00 +0000421} // namespace kernels
422} // namespace cpu
morgolockcc1f6c92020-03-24 09:26:48 +0000423} // namespace arm_compute