blob: 9308d860d1fa14e6f1badf2ef891df0ff133b10b [file] [log] [blame]
Anthony Barbier6ff3b192017-09-04 18:44:23 +01001/*
Adnan AlSinanbbf2e742023-02-22 12:15:14 +00002 * Copyright (c) 2017-2023 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"
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010028#include "arm_compute/core/utils/misc/ShapeCalculator.h"
Anthony Barbier6ff3b192017-09-04 18:44:23 +010029#include "arm_compute/core/Validate.h"
30#include "arm_compute/core/Window.h"
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010031
Sheri Zhang79144a62021-02-08 17:43:04 +000032#include "src/core/common/Registrars.h"
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010033#include "src/core/CPP/Validate.h"
Sang-Hoon Park68dd25f2020-10-19 16:00:11 +010034#include "src/core/helpers/AutoConfiguration.h"
35#include "src/core/helpers/WindowHelpers.h"
Georgios Pinitasddb93bb2020-10-02 16:38:59 +010036#include "src/core/NEON/wrapper/wrapper.h"
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010037#include "src/cpu/kernels/pool2d/neon/list.h"
38
Anthony Barbier6ff3b192017-09-04 18:44:23 +010039#include <arm_neon.h>
Anthony Barbier6ff3b192017-09-04 18:44:23 +010040
Manuel Bottinib4bb8272019-12-18 18:01:27 +000041namespace arm_compute
42{
Michele Di Giorgio19289042021-02-03 16:05:00 +000043namespace cpu
44{
45namespace kernels
46{
Anthony Barbier6ff3b192017-09-04 18:44:23 +010047namespace
48{
Sheri Zhang79144a62021-02-08 17:43:04 +000049using namespace misc::shape_calculator;
50
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010051static const std::vector<CpuPool2dKernel::PoolingKernel> available_kernels = {
52 {"neon_qu8_nhwc_poolMxN",
53 [](const PoolDataTypeISASelectorData &data)
54 { return ((data.dl == DataLayout::NHWC) && (data.dt == DataType::QASYMM8)); },
55 REGISTER_QASYMM8_NEON(arm_compute::cpu::poolingMxN_qasymm8_neon_nhwc)},
56 {"neon_qs8_nhwc_poolMxN",
57 [](const PoolDataTypeISASelectorData &data)
58 { return ((data.dl == DataLayout::NHWC) && (data.dt == DataType::QASYMM8_SIGNED)); },
59 REGISTER_QASYMM8_SIGNED_NEON(arm_compute::cpu::poolingMxN_qasymm8_signed_neon_nhwc)},
60 {"neon_f16_nhwc_poolMxN",
61 [](const PoolDataTypeISASelectorData &data)
62 { return ((data.dl == DataLayout::NHWC) && (data.dt == DataType::F16)) && data.isa.fp16; },
63 REGISTER_FP16_NEON(arm_compute::cpu::poolingMxN_fp16_neon_nhwc)},
64 {"neon_fp32_nhwc_poolMxN",
65 [](const PoolDataTypeISASelectorData &data)
66 { return ((data.dl == DataLayout::NHWC) && (data.dt == DataType::F32)); },
67 REGISTER_FP32_NEON(arm_compute::cpu::poolingMxN_fp32_neon_nhwc)},
Sheri Zhang79144a62021-02-08 17:43:04 +000068#if defined(ENABLE_NCHW_KERNELS)
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010069 {"neon_qu8_nchw_pool2",
70 [](const PoolDataTypeISASelectorData &data)
71 {
72 return ((data.dl == DataLayout::NCHW) && (data.dt == DataType::QASYMM8) &&
73 (data.pool_size.x() == data.pool_size.y()) && (data.pool_size.x() == 2) && (data.pool_stride_x < 3));
74 },
75 REGISTER_QASYMM8_NEON(arm_compute::cpu::pooling2_quantized_neon_nchw<uint8_t>)},
76 {"neon_qu8_nchw_pool3",
77 [](const PoolDataTypeISASelectorData &data)
78 {
79 return ((data.dl == DataLayout::NCHW) && (data.dt == DataType::QASYMM8) &&
80 (data.pool_size.x() == data.pool_size.y()) && (data.pool_size.x() == 3) && (data.pool_stride_x < 3));
81 },
82 REGISTER_QASYMM8_NEON(arm_compute::cpu::pooling3_quantized_neon_nchw<uint8_t>)},
83 {"neon_qu8_nchw_poolMxN",
84 [](const PoolDataTypeISASelectorData &data)
85 { return ((data.dl == DataLayout::NCHW) && (data.dt == DataType::QASYMM8)); },
86 REGISTER_QASYMM8_NEON(arm_compute::cpu::poolingMxN_quantized_neon_nchw<uint8_t>)},
87 {"neon_qs8_nchw_pool2",
88 [](const PoolDataTypeISASelectorData &data)
89 {
90 return ((data.dl == DataLayout::NCHW) && (data.dt == DataType::QASYMM8_SIGNED) &&
91 (data.pool_size.x() == data.pool_size.y()) && (data.pool_size.x() == 2) && (data.pool_stride_x < 3));
92 },
93 REGISTER_QASYMM8_SIGNED_NEON(arm_compute::cpu::pooling2_quantized_neon_nchw<int8_t>)},
94 {"neon_qs8_nchw_pool3",
95 [](const PoolDataTypeISASelectorData &data)
96 {
97 return ((data.dl == DataLayout::NCHW) && (data.dt == DataType::QASYMM8_SIGNED) &&
98 (data.pool_size.x() == data.pool_size.y()) && (data.pool_size.x() == 3) && (data.pool_stride_x < 3));
99 },
100 REGISTER_QASYMM8_SIGNED_NEON(arm_compute::cpu::pooling3_quantized_neon_nchw<int8_t>)},
101 {"neon_qs8_nchw_poolMxN",
102 [](const PoolDataTypeISASelectorData &data)
103 { return ((data.dl == DataLayout::NCHW) && (data.dt == DataType::QASYMM8_SIGNED)); },
104 REGISTER_QASYMM8_SIGNED_NEON(arm_compute::cpu::poolingMxN_quantized_neon_nchw<int8_t>)},
105 {"neon_fp16_nchw_pool2",
106 [](const PoolDataTypeISASelectorData &data)
107 {
108 return ((data.dl == DataLayout::NCHW) && (data.dt == DataType::F16 && data.isa.fp16) &&
109 (data.pool_size.x() == data.pool_size.y()) && (data.pool_size.x() == 2));
110 },
111 REGISTER_FP16_NEON(arm_compute::cpu::pooling2_fp16_neon_nchw)},
112 {"neon_fp16_nchw_pool3",
113 [](const PoolDataTypeISASelectorData &data)
114 {
115 return ((data.dl == DataLayout::NCHW) && (data.dt == DataType::F16 && data.isa.fp16) &&
116 (data.pool_size.x() == data.pool_size.y()) && (data.pool_size.x() == 3));
117 },
118 REGISTER_FP16_NEON(arm_compute::cpu::pooling3_fp16_neon_nchw)},
119 {"neon_fp16_nchw_poolMxN",
120 [](const PoolDataTypeISASelectorData &data)
121 { return ((data.dl == DataLayout::NCHW) && (data.dt == DataType::F16 && data.isa.fp16)); },
122 REGISTER_FP16_NEON(arm_compute::cpu::poolingMxN_fp16_neon_nchw)},
123 {"neon_fp32_nchw_pool2",
124 [](const PoolDataTypeISASelectorData &data)
125 {
126 return ((data.dl == DataLayout::NCHW) && (data.dt == DataType::F32) &&
127 (data.pool_size.x() == data.pool_size.y()) && (data.pool_size.x() == 2));
128 },
129 REGISTER_FP32_NEON(arm_compute::cpu::pooling2_fp32_neon_nchw)},
130 {"neon_fp32_nchw_pool3",
131 [](const PoolDataTypeISASelectorData &data)
132 {
133 return ((data.dl == DataLayout::NCHW) && (data.dt == DataType::F32) &&
134 (data.pool_size.x() == data.pool_size.y()) && (data.pool_size.x() == 3));
135 },
136 REGISTER_FP32_NEON(arm_compute::cpu::pooling3_fp32_neon_nchw)},
137 {"neon_fp32_nchw_pool7",
138 [](const PoolDataTypeISASelectorData &data)
139 {
140 return ((data.dl == DataLayout::NCHW) && (data.dt == DataType::F32) &&
141 (data.pool_size.x() == data.pool_size.y()) && (data.pool_size.x() == 7));
142 },
143 REGISTER_FP32_NEON(arm_compute::cpu::pooling7_fp32_neon_nchw)},
144 {"neon_fp32_nchw_poolMxN",
145 [](const PoolDataTypeISASelectorData &data)
146 { return ((data.dl == DataLayout::NCHW) && (data.dt == DataType::F32)); },
147 REGISTER_FP32_NEON(arm_compute::cpu::poolingMxN_fp32_neon_nchw)},
Sheri Zhang79144a62021-02-08 17:43:04 +0000148#endif /* defined(ENABLE_NCHW_KERNELS) */
149};
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100150
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100151Status validate_arguments(const ITensorInfo *src,
152 const ITensorInfo *dst,
153 const PoolingLayerInfo &pool_info,
154 const ITensorInfo *indices,
155 Size2D pool_size)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100156{
Michele Di Giorgio19289042021-02-03 16:05:00 +0000157 ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(src, dst);
Freddie Liardetafcbb8f2021-05-04 12:41:16 +0100158 ARM_COMPUTE_RETURN_ERROR_ON(pool_size.x() == 0);
159 ARM_COMPUTE_RETURN_ERROR_ON(pool_size.y() == 0);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100160
Isabella Gottardi7567f5f2018-01-30 15:26:00 +0000161 int pool_stride_x = 0;
162 int pool_stride_y = 0;
Freddie Liardetafcbb8f2021-05-04 12:41:16 +0100163 int output_width = 0;
164 int output_height = 0;
Sang-Hoon Park0cb3da62020-01-15 12:39:56 +0000165 PoolingType pool_type = pool_info.pool_type;
166 const PadStrideInfo pad_stride_info = pool_info.pad_stride_info;
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100167 const auto data_layout = pool_info.data_layout == DataLayout::UNKNOWN ? src->data_layout() : pool_info.data_layout;
168 const int idx_width = get_data_layout_dimension_index(data_layout, DataLayoutDimension::WIDTH);
169 const int idx_height = get_data_layout_dimension_index(data_layout, DataLayoutDimension::HEIGHT);
Freddie Liardetafcbb8f2021-05-04 12:41:16 +0100170
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100171 ARM_COMPUTE_RETURN_ERROR_ON_MSG(
172 (!is_data_type_float(src->data_type())) && (is_pool_region_entirely_outside_input(pool_info)),
173 "Pooling region that is entirely outside input tensor is unsupported for non-float types");
SiCongLic4270cf2021-12-22 11:22:40 +0000174
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100175 std::tie(output_width, output_height) =
176 scaled_dimensions_signed(src->tensor_shape()[idx_width], src->tensor_shape()[idx_height], pool_size.x(),
177 pool_size.y(), pool_info.pad_stride_info);
178 ARM_COMPUTE_RETURN_ERROR_ON_MSG((output_width < 1 || output_height < 1),
179 "Calculated output dimension size is invalid");
Freddie Liardetafcbb8f2021-05-04 12:41:16 +0100180
181 TensorInfo out_info(TensorInfo(compute_pool_shape(*src, pool_info), 1, dst->data_type()));
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100182 std::tie(pool_stride_x, pool_stride_y) = pad_stride_info.stride();
Michele Di Giorgio8af2dd62017-06-19 15:19:29 +0100183
Michele Di Giorgio19289042021-02-03 16:05:00 +0000184 ARM_COMPUTE_RETURN_ERROR_ON_CPU_F16_UNSUPPORTED(src);
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100185 if (indices)
morgolockcc1f6c92020-03-24 09:26:48 +0000186 {
Michele Di Giorgio19289042021-02-03 16:05:00 +0000187 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(src, 1, DataType::F32, DataType::F16);
morgolockcc1f6c92020-03-24 09:26:48 +0000188 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(indices, 1, DataType::U32);
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100189 ARM_COMPUTE_RETURN_ERROR_ON_MSG(pool_type != PoolingType::MAX,
190 "Pooling indices only supported for MAX pooling method");
morgolockcc1f6c92020-03-24 09:26:48 +0000191 }
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100192 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(src, 1, DataType::QASYMM8, DataType::QASYMM8_SIGNED,
193 DataType::F16, DataType::F32);
Michele Di Giorgio19289042021-02-03 16:05:00 +0000194 ARM_COMPUTE_RETURN_ERROR_ON(pool_type == PoolingType::L2 && is_data_type_quantized(src->data_type()));
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100195 ARM_COMPUTE_RETURN_ERROR_ON_MSG(
196 is_data_type_quantized(src->data_type()) && !pool_info.exclude_padding &&
197 (pool_info.pool_type == PoolingType::AVG) && pool_info.pad_stride_info.has_padding() &&
198 (src->data_layout() == DataLayout::NHWC),
199 "exclude_padding equal false is not supported for AVG Pooling with padding on quantized types");
Isabella Gottardi7567f5f2018-01-30 15:26:00 +0000200
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100201 if (dst->total_size() != 0)
Georgios Pinitas1dad50e2017-07-03 17:51:34 +0100202 {
Michele Di Giorgio19289042021-02-03 16:05:00 +0000203 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(src, dst);
204 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_LAYOUT(src, dst);
Freddie Liardetafcbb8f2021-05-04 12:41:16 +0100205 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_SHAPES(dst, &out_info);
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100206 if (indices)
morgolockcc1f6c92020-03-24 09:26:48 +0000207 {
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100208 ARM_COMPUTE_RETURN_ERROR_ON_MSG(
209 ((pool_size != Size2D(2, 2)) && !pool_info.use_kernel_indices),
210 "Pooling indices returning source tensor coordinates is only supported for pool size 2x2");
211 ARM_COMPUTE_RETURN_ERROR_ON_MSG(pool_info.use_kernel_indices && (src->data_layout() != DataLayout::NHWC),
212 "Pooling kernel indices only supported for NHWC");
Freddie Liardetafcbb8f2021-05-04 12:41:16 +0100213 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_SHAPES(indices, &out_info);
morgolockcc1f6c92020-03-24 09:26:48 +0000214 }
Georgios Pinitas1dad50e2017-07-03 17:51:34 +0100215 }
216
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100217 const auto *uk = CpuPool2dKernel::get_implementation(PoolDataTypeISASelectorData{
218 src->data_type(), src->data_layout(), pool_stride_x, pool_size, CPUInfo::get().get_isa()});
Sheri Zhang79144a62021-02-08 17:43:04 +0000219 ARM_COMPUTE_RETURN_ERROR_ON(uk == nullptr || uk->ukernel == nullptr);
220
Michalis Spyrouafa5d812017-11-30 14:25:57 +0000221 return Status{};
222}
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100223
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100224std::pair<Status, Window> validate_and_configure_window(ITensorInfo *src,
225 ITensorInfo *dst,
226 ITensorInfo *indices,
227 const PoolingLayerInfo &pool_info,
228 unsigned int &num_elems_processed_per_iteration,
229 int pool_size_x,
230 int pool_size_y)
Michalis Spyrouafa5d812017-11-30 14:25:57 +0000231{
Michele Di Giorgio19289042021-02-03 16:05:00 +0000232 // dst auto inizialitation if not yet initialized
233 auto_init_if_empty(*dst, src->clone()->set_tensor_shape(compute_pool_shape(*src, pool_info)));
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100234 if (indices)
morgolockcc1f6c92020-03-24 09:26:48 +0000235 {
236 // Indices auto inizialitation if not yet initialized
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100237 auto_init_if_empty(*indices, (src->clone()->set_tensor_shape(compute_pool_shape(*src, pool_info)))
238 .set_data_type(DataType::U32) /* we store the offset to the element */);
morgolockcc1f6c92020-03-24 09:26:48 +0000239 }
Freddie Liardetded36632021-09-03 15:08:23 +0100240 const auto data_layout = pool_info.data_layout == DataLayout::UNKNOWN ? src->data_layout() : pool_info.data_layout;
Freddie Liardetded36632021-09-03 15:08:23 +0100241
242 int pool_stride_x = 0;
243 int pool_stride_y = 0;
244 const int idx_width = get_data_layout_dimension_index(data_layout, DataLayoutDimension::WIDTH);
245 const int idx_height = get_data_layout_dimension_index(data_layout, DataLayoutDimension::HEIGHT);
246 const PadStrideInfo pad_stride_info = pool_info.pad_stride_info;
247
Michalis Spyrouafa5d812017-11-30 14:25:57 +0000248 std::tie(pool_stride_x, pool_stride_y) = pad_stride_info.stride();
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100249 const bool is_square = pool_size_x == pool_size_y;
250 const unsigned int pooled_w = dst->dimension(idx_width);
251 const unsigned int pooled_h = dst->dimension(idx_height);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100252
Isabella Gottardi7567f5f2018-01-30 15:26:00 +0000253 //If it's not squared and optimized will be executed the MxN
Isabella Gottardi7567f5f2018-01-30 15:26:00 +0000254 num_elems_processed_per_iteration = 1;
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100255
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100256 if (is_square)
Isabella Gottardi7567f5f2018-01-30 15:26:00 +0000257 {
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100258 switch (src->data_type())
Isabella Gottardi7567f5f2018-01-30 15:26:00 +0000259 {
Isabella Gottardi7567f5f2018-01-30 15:26:00 +0000260 case DataType::QASYMM8:
Manuel Bottinib4bb8272019-12-18 18:01:27 +0000261 case DataType::QASYMM8_SIGNED:
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100262 switch (pool_size_x)
Isabella Gottardi7567f5f2018-01-30 15:26:00 +0000263 {
264 case 2:
Isabella Gottardi7567f5f2018-01-30 15:26:00 +0000265 num_elems_processed_per_iteration = (pool_stride_x == 2) ? 8 : 15;
Isabella Gottardi7567f5f2018-01-30 15:26:00 +0000266 break;
267 case 3:
Isabella Gottardi7567f5f2018-01-30 15:26:00 +0000268 num_elems_processed_per_iteration = (pool_stride_x == 2) ? 7 : 14;
Isabella Gottardi7567f5f2018-01-30 15:26:00 +0000269 break;
270 default:
271 break;
272 }
273 break;
Isabella Gottardi7567f5f2018-01-30 15:26:00 +0000274#ifdef __ARM_FEATURE_FP16_VECTOR_ARITHMETIC
275 case DataType::F16:
Freddie Liardetded36632021-09-03 15:08:23 +0100276 num_elems_processed_per_iteration = 1;
Isabella Gottardi7567f5f2018-01-30 15:26:00 +0000277 break;
278#endif /* __ARM_FEATURE_FP16_VECTOR_ARITHMETIC */
279 case DataType::F32:
Isabella Gottardi7567f5f2018-01-30 15:26:00 +0000280 num_elems_processed_per_iteration = 1;
Isabella Gottardi7567f5f2018-01-30 15:26:00 +0000281 break;
282 default:
283 ARM_COMPUTE_ERROR("Element size not supported");
284 break;
285 }
286 }
Michalis Spyrou57dac842018-03-01 16:03:50 +0000287
288 bool window_changed = false;
289 Window win{};
Freddie Liardetded36632021-09-03 15:08:23 +0100290 // Upper limit for the number of right/bottom border elements that are accessed
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100291 TensorShape dst_shape{src->tensor_shape()};
Freddie Liardetded36632021-09-03 15:08:23 +0100292 dst_shape.set(0, pooled_w);
293 dst_shape.set(1, pooled_h);
294 TensorInfo dst_info(src->clone()->set_tensor_shape(dst_shape));
295 win = calculate_max_window(dst_info, Steps(num_elems_processed_per_iteration));
Michalis Spyrouafa5d812017-11-30 14:25:57 +0000296
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100297 Status err =
298 (window_changed) ? ARM_COMPUTE_CREATE_ERROR(ErrorCode::RUNTIME_ERROR, "Insufficient Padding!") : Status{};
Michalis Spyrouafa5d812017-11-30 14:25:57 +0000299 return std::make_pair(err, win);
300}
301} // namespace
302
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100303void CpuPool2dKernel::configure(ITensorInfo *src,
304 ITensorInfo *dst,
305 const PoolingLayerInfo &pool_info,
306 ITensorInfo *indices)
Michalis Spyrouafa5d812017-11-30 14:25:57 +0000307{
Michele Di Giorgio19289042021-02-03 16:05:00 +0000308 ARM_COMPUTE_ERROR_ON_NULLPTR(src, dst);
Sang-Hoon Park0cb3da62020-01-15 12:39:56 +0000309 const PadStrideInfo pad_stride_info = pool_info.pad_stride_info;
310 const bool is_global_pooling = pool_info.is_global_pooling;
Michalis Spyrou57dac842018-03-01 16:03:50 +0000311
312 // Get data layout
Michele Di Giorgio19289042021-02-03 16:05:00 +0000313 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 +0000314 const int idx_width = get_data_layout_dimension_index(data_layout, DataLayoutDimension::WIDTH);
315 const int idx_height = get_data_layout_dimension_index(data_layout, DataLayoutDimension::HEIGHT);
Michalis Spyrouafa5d812017-11-30 14:25:57 +0000316
317 // Update pool size in case of global pooling
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100318 const Size2D pool_size(is_global_pooling ? src->dimension(idx_width) : pool_info.pool_size.width,
319 is_global_pooling ? src->dimension(idx_height) : pool_info.pool_size.height);
Michalis Spyrouafa5d812017-11-30 14:25:57 +0000320
Michalis Spyrouafa5d812017-11-30 14:25:57 +0000321 // Perform validation step
Freddie Liardetafcbb8f2021-05-04 12:41:16 +0100322 ARM_COMPUTE_ERROR_THROW_ON(validate_arguments(src, dst, pool_info, indices, pool_size));
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100323
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100324 const auto *uk = CpuPool2dKernel::get_implementation(
325 PoolDataTypeISASelectorData{src->data_type(), src->data_layout(), (int)pad_stride_info.stride().first,
326 pool_size, CPUInfo::get().get_isa()});
Georgios Pinitas5fdde992021-06-25 05:42:57 +0100327 ARM_COMPUTE_ERROR_ON(uk == nullptr);
328
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100329 // Set instance variables
Sheri Zhang79144a62021-02-08 17:43:04 +0000330 _pool_info = pool_info;
331 _data_layout = src->data_layout();
332 _pool_size = pool_size;
333 _pool_stride_x = pad_stride_info.stride().first;
Georgios Pinitas5fdde992021-06-25 05:42:57 +0100334 _run_method = uk->ukernel;
335 _name = std::string("CpuPool2dKernel").append("/").append(uk->name);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100336
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100337 if (_data_layout == DataLayout::NHWC)
Michalis Spyroucffb2a32020-09-08 16:26:38 +0100338 {
339 // Configure kernel window
SiCongLib88272e2021-02-24 15:40:57 +0000340 Window win = calculate_max_window(*dst, Steps());
Yair Schwarzbaum46d44d22022-01-12 16:38:58 +0200341 ICpuKernel::configure(win);
Michalis Spyroucffb2a32020-09-08 16:26:38 +0100342 }
343 else
344 {
345 // Configure kernel window
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100346 auto win_config = validate_and_configure_window(
347 src, dst, indices, pool_info, _num_elems_processed_per_iteration, pool_size.x(), pool_size.y());
Michalis Spyroucffb2a32020-09-08 16:26:38 +0100348 ARM_COMPUTE_ERROR_THROW_ON(win_config.first);
Yair Schwarzbaum46d44d22022-01-12 16:38:58 +0200349 ICpuKernel::configure(win_config.second);
Michalis Spyroucffb2a32020-09-08 16:26:38 +0100350 }
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100351}
352
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100353Status CpuPool2dKernel::validate(const ITensorInfo *src,
354 const ITensorInfo *dst,
355 const PoolingLayerInfo &pool_info,
356 const ITensorInfo *indices)
Michalis Spyrouafa5d812017-11-30 14:25:57 +0000357{
Michele Di Giorgio19289042021-02-03 16:05:00 +0000358 ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(src);
Michalis Spyrouafa5d812017-11-30 14:25:57 +0000359
Michalis Spyrouafa5d812017-11-30 14:25:57 +0000360 unsigned int num_elems_processed_per_iteration = 0;
Michalis Spyrouafa5d812017-11-30 14:25:57 +0000361
Freddie Liardetafcbb8f2021-05-04 12:41:16 +0100362 const bool is_global_pooling = pool_info.is_global_pooling;
Michalis Spyrou57dac842018-03-01 16:03:50 +0000363
364 // Get data layout
Michele Di Giorgio19289042021-02-03 16:05:00 +0000365 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 +0000366 const int idx_width = get_data_layout_dimension_index(data_layout, DataLayoutDimension::WIDTH);
367 const int idx_height = get_data_layout_dimension_index(data_layout, DataLayoutDimension::HEIGHT);
Michalis Spyrou57dac842018-03-01 16:03:50 +0000368
Freddie Liardetafcbb8f2021-05-04 12:41:16 +0100369 unsigned int pool_size_x = is_global_pooling ? src->dimension(idx_width) : pool_info.pool_size.width;
370 unsigned int pool_size_y = is_global_pooling ? src->dimension(idx_height) : pool_info.pool_size.height;
Michalis Spyrouafa5d812017-11-30 14:25:57 +0000371
Freddie Liardetafcbb8f2021-05-04 12:41:16 +0100372 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 +0000373 ARM_COMPUTE_RETURN_ON_ERROR(validate_and_configure_window(src->clone().get(), dst->clone().get(),
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100374 (indices) ? indices->clone().get() : nullptr, pool_info,
375 num_elems_processed_per_iteration, pool_size_x,
376 pool_size_y)
377 .first);
Michalis Spyrouafa5d812017-11-30 14:25:57 +0000378
379 return Status{};
380}
381
Manuel Bottinib4bb6a02021-05-24 16:01:32 +0100382void CpuPool2dKernel::run_op(ITensorPack &tensors, const Window &window, const ThreadInfo &info)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100383{
Moritz Pflanzerc186b572017-09-07 09:48:04 +0100384 ARM_COMPUTE_UNUSED(info);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100385 ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this);
Yair Schwarzbaum46d44d22022-01-12 16:38:58 +0200386 ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(ICpuKernel::window(), window);
Georgios Pinitas5fdde992021-06-25 05:42:57 +0100387 ARM_COMPUTE_ERROR_ON(_run_method == nullptr);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100388
Sheri Zhang79144a62021-02-08 17:43:04 +0000389 const ITensor *src = tensors.get_const_tensor(TensorType::ACL_SRC_0);
390 ITensor *dst = tensors.get_tensor(TensorType::ACL_DST_0);
391 ITensor *indices = tensors.get_tensor(TensorType::ACL_DST_1);
Michele Di Giorgio19289042021-02-03 16:05:00 +0000392
Sheri Zhang79144a62021-02-08 17:43:04 +0000393 const unsigned int pool_stride_x = _pool_info.pad_stride_info.stride().first;
394 const unsigned int pool_stride_y = _pool_info.pad_stride_info.stride().second;
395 const unsigned int pool_size = _pool_info.pool_size.width;
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100396
Michele Di Giorgio19289042021-02-03 16:05:00 +0000397 Window window_src(window);
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100398 if (_data_layout == DataLayout::NCHW)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100399 {
Michele Di Giorgio19289042021-02-03 16:05:00 +0000400 // Set step for src in x and y direction for the src
Michalis Spyrou57dac842018-03-01 16:03:50 +0000401 unsigned int window_x_inc = 0;
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100402 switch (src->info()->data_type())
Pablo Tello0c34fe22017-06-26 17:17:42 +0100403 {
Michalis Spyrou57dac842018-03-01 16:03:50 +0000404 case DataType::QASYMM8:
Manuel Bottinib4bb8272019-12-18 18:01:27 +0000405 case DataType::QASYMM8_SIGNED:
Michalis Spyrou57dac842018-03-01 16:03:50 +0000406 {
407 window_x_inc = pool_stride_x;
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100408 if ((pool_size == 2 || pool_size == 3) && pool_stride_x < 3)
Michalis Spyrou57dac842018-03-01 16:03:50 +0000409 {
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100410 window_x_inc = (pool_stride_x == 2) ? _num_elems_processed_per_iteration * 2
411 : _num_elems_processed_per_iteration;
Michalis Spyrou57dac842018-03-01 16:03:50 +0000412 }
413 break;
414 }
Pablo Tello77e6c552018-12-04 15:33:49 +0000415
Georgios Pinitas13d96e02018-08-23 11:20:23 +0100416 case DataType::F16:
Michalis Spyrou57dac842018-03-01 16:03:50 +0000417 case DataType::F32:
418 {
419 window_x_inc = pool_stride_x;
420 break;
421 }
422 default:
423 {
424 ARM_COMPUTE_ERROR("Not supported");
425 }
Georgios Pinitas55186712018-01-08 17:37:12 +0000426 }
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100427 window_src.set(Window::DimX, Window::Dimension(window.x().start() * pool_stride_x,
428 window.x().end() * pool_stride_x, window_x_inc));
429 window_src.set(Window::DimY, Window::Dimension(window.y().start() * pool_stride_y,
430 window.y().end() * pool_stride_y, pool_stride_y));
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100431 }
Michalis Spyrou57dac842018-03-01 16:03:50 +0000432 else
433 {
Michele Di Giorgio19289042021-02-03 16:05:00 +0000434 window_src.set(Window::DimX, Window::Dimension(0, 1, 1));
Sheri Zhang79144a62021-02-08 17:43:04 +0000435 window_src.set(Window::DimY, Window::Dimension(0, src->info()->dimension(1), pool_stride_x));
436 window_src.set(Window::DimZ, Window::Dimension(0, src->info()->dimension(2), pool_stride_y));
Michalis Spyrou57dac842018-03-01 16:03:50 +0000437 }
Georgios Pinitas5fdde992021-06-25 05:42:57 +0100438 _run_method(src, dst, indices, _pool_info, window_src, window);
Sheri Zhang79144a62021-02-08 17:43:04 +0000439}
440
Manuel Bottinib4bb6a02021-05-24 16:01:32 +0100441const char *CpuPool2dKernel::name() const
Sheri Zhang79144a62021-02-08 17:43:04 +0000442{
Georgios Pinitas5fdde992021-06-25 05:42:57 +0100443 return _name.c_str();
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100444}
Giorgio Arena5ae8d802021-11-18 18:02:13 +0000445
446const std::vector<CpuPool2dKernel::PoolingKernel> &CpuPool2dKernel::get_available_kernels()
447{
448 return available_kernels;
449}
450
Michele Di Giorgio19289042021-02-03 16:05:00 +0000451} // namespace kernels
452} // namespace cpu
morgolockcc1f6c92020-03-24 09:26:48 +0000453} // namespace arm_compute