1*c217d954SCole Faust /*
2*c217d954SCole Faust * Copyright (c) 2017-2022 Arm Limited.
3*c217d954SCole Faust *
4*c217d954SCole Faust * SPDX-License-Identifier: MIT
5*c217d954SCole Faust *
6*c217d954SCole Faust * Permission is hereby granted, free of charge, to any person obtaining a copy
7*c217d954SCole Faust * of this software and associated documentation files (the "Software"), to
8*c217d954SCole Faust * deal in the Software without restriction, including without limitation the
9*c217d954SCole Faust * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
10*c217d954SCole Faust * sell copies of the Software, and to permit persons to whom the Software is
11*c217d954SCole Faust * furnished to do so, subject to the following conditions:
12*c217d954SCole Faust *
13*c217d954SCole Faust * The above copyright notice and this permission notice shall be included in all
14*c217d954SCole Faust * copies or substantial portions of the Software.
15*c217d954SCole Faust *
16*c217d954SCole Faust * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17*c217d954SCole Faust * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18*c217d954SCole Faust * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19*c217d954SCole Faust * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20*c217d954SCole Faust * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21*c217d954SCole Faust * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22*c217d954SCole Faust * SOFTWARE.
23*c217d954SCole Faust */
24*c217d954SCole Faust #include "src/cpu/kernels/CpuPool2dKernel.h"
25*c217d954SCole Faust
26*c217d954SCole Faust #include "arm_compute/core/Helpers.h"
27*c217d954SCole Faust #include "arm_compute/core/TensorInfo.h"
28*c217d954SCole Faust #include "arm_compute/core/Validate.h"
29*c217d954SCole Faust #include "arm_compute/core/Window.h"
30*c217d954SCole Faust #include "arm_compute/core/utils/misc/ShapeCalculator.h"
31*c217d954SCole Faust #include "src/core/AccessWindowStatic.h"
32*c217d954SCole Faust #include "src/core/CPP/Validate.h"
33*c217d954SCole Faust #include "src/core/NEON/NEAsymm.h"
34*c217d954SCole Faust #include "src/core/NEON/NEFixedPoint.h"
35*c217d954SCole Faust #include "src/core/NEON/NEMath.h"
36*c217d954SCole Faust #include "src/core/common/Registrars.h"
37*c217d954SCole Faust #include "src/core/helpers/AutoConfiguration.h"
38*c217d954SCole Faust #include "src/core/helpers/WindowHelpers.h"
39*c217d954SCole Faust #include "src/cpu/kernels/pool2d/neon/list.h"
40*c217d954SCole Faust #include "support/ToolchainSupport.h"
41*c217d954SCole Faust
42*c217d954SCole Faust #include "src/core/NEON/wrapper/wrapper.h"
43*c217d954SCole Faust #include <arm_neon.h>
44*c217d954SCole Faust
45*c217d954SCole Faust namespace arm_compute
46*c217d954SCole Faust {
47*c217d954SCole Faust namespace cpu
48*c217d954SCole Faust {
49*c217d954SCole Faust namespace kernels
50*c217d954SCole Faust {
51*c217d954SCole Faust namespace
52*c217d954SCole Faust {
53*c217d954SCole Faust using namespace misc::shape_calculator;
54*c217d954SCole Faust
55*c217d954SCole Faust static const std::vector<CpuPool2dKernel::PoolingKernel> available_kernels =
56*c217d954SCole Faust {
57*c217d954SCole Faust {
58*c217d954SCole Faust "neon_qu8_nhwc_poolMxN",
__anonac3f3cc30202() 59*c217d954SCole Faust [](const PoolDataTypeISASelectorData & data) { return ((data.dl == DataLayout::NHWC) && (data.dt == DataType::QASYMM8)); },
60*c217d954SCole Faust REGISTER_QASYMM8_NEON(arm_compute::cpu::poolingMxN_qasymm8_neon_nhwc)
61*c217d954SCole Faust },
62*c217d954SCole Faust {
63*c217d954SCole Faust "neon_qs8_nhwc_poolMxN",
__anonac3f3cc30302() 64*c217d954SCole Faust [](const PoolDataTypeISASelectorData & data) { return ((data.dl == DataLayout::NHWC) && (data.dt == DataType::QASYMM8_SIGNED)); },
65*c217d954SCole Faust REGISTER_QASYMM8_SIGNED_NEON(arm_compute::cpu::poolingMxN_qasymm8_signed_neon_nhwc)
66*c217d954SCole Faust },
67*c217d954SCole Faust {
68*c217d954SCole Faust "neon_f16_nhwc_poolMxN",
__anonac3f3cc30402() 69*c217d954SCole Faust [](const PoolDataTypeISASelectorData & data) { return ((data.dl == DataLayout::NHWC) && (data.dt == DataType::F16)) && data.isa.fp16; },
70*c217d954SCole Faust REGISTER_FP16_NEON(arm_compute::cpu::poolingMxN_fp16_neon_nhwc)
71*c217d954SCole Faust },
72*c217d954SCole Faust {
73*c217d954SCole Faust "neon_fp32_nhwc_poolMxN",
__anonac3f3cc30502() 74*c217d954SCole Faust [](const PoolDataTypeISASelectorData & data) { return ((data.dl == DataLayout::NHWC) && (data.dt == DataType::F32)); },
75*c217d954SCole Faust REGISTER_FP32_NEON(arm_compute::cpu::poolingMxN_fp32_neon_nhwc)
76*c217d954SCole Faust },
77*c217d954SCole Faust #if defined(ENABLE_NCHW_KERNELS)
78*c217d954SCole Faust {
79*c217d954SCole Faust "neon_qu8_nchw_pool2",
__anonac3f3cc30602() 80*c217d954SCole Faust [](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)); },
81*c217d954SCole Faust REGISTER_QASYMM8_NEON(arm_compute::cpu::pooling2_quantized_neon_nchw<uint8_t>)
82*c217d954SCole Faust },
83*c217d954SCole Faust {
84*c217d954SCole Faust "neon_qu8_nchw_pool3",
__anonac3f3cc30702() 85*c217d954SCole Faust [](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)); },
86*c217d954SCole Faust REGISTER_QASYMM8_NEON(arm_compute::cpu::pooling3_quantized_neon_nchw<uint8_t>)
87*c217d954SCole Faust },
88*c217d954SCole Faust {
89*c217d954SCole Faust "neon_qu8_nchw_poolMxN",
__anonac3f3cc30802() 90*c217d954SCole Faust [](const PoolDataTypeISASelectorData & data) { return ((data.dl == DataLayout::NCHW) && (data.dt == DataType::QASYMM8)); },
91*c217d954SCole Faust REGISTER_QASYMM8_NEON(arm_compute::cpu::poolingMxN_quantized_neon_nchw<uint8_t>)
92*c217d954SCole Faust },
93*c217d954SCole Faust {
94*c217d954SCole Faust "neon_qs8_nchw_pool2",
__anonac3f3cc30902() 95*c217d954SCole Faust [](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)); },
96*c217d954SCole Faust REGISTER_QASYMM8_SIGNED_NEON(arm_compute::cpu::pooling2_quantized_neon_nchw<int8_t>)
97*c217d954SCole Faust },
98*c217d954SCole Faust {
99*c217d954SCole Faust "neon_qs8_nchw_pool3",
__anonac3f3cc30a02() 100*c217d954SCole Faust [](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)); },
101*c217d954SCole Faust REGISTER_QASYMM8_SIGNED_NEON(arm_compute::cpu::pooling3_quantized_neon_nchw<int8_t>)
102*c217d954SCole Faust },
103*c217d954SCole Faust {
104*c217d954SCole Faust "neon_qs8_nchw_poolMxN",
__anonac3f3cc30b02() 105*c217d954SCole Faust [](const PoolDataTypeISASelectorData & data) { return ((data.dl == DataLayout::NCHW) && (data.dt == DataType::QASYMM8_SIGNED)); },
106*c217d954SCole Faust REGISTER_QASYMM8_SIGNED_NEON(arm_compute::cpu::poolingMxN_quantized_neon_nchw<int8_t>)
107*c217d954SCole Faust },
108*c217d954SCole Faust {
109*c217d954SCole Faust "neon_fp16_nchw_pool2",
__anonac3f3cc30c02() 110*c217d954SCole Faust [](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)); },
111*c217d954SCole Faust REGISTER_FP16_NEON(arm_compute::cpu::pooling2_fp16_neon_nchw)
112*c217d954SCole Faust },
113*c217d954SCole Faust {
114*c217d954SCole Faust "neon_fp16_nchw_pool3",
__anonac3f3cc30d02() 115*c217d954SCole Faust [](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)); },
116*c217d954SCole Faust REGISTER_FP16_NEON(arm_compute::cpu::pooling3_fp16_neon_nchw)
117*c217d954SCole Faust },
118*c217d954SCole Faust {
119*c217d954SCole Faust "neon_fp16_nchw_poolMxN",
__anonac3f3cc30e02() 120*c217d954SCole Faust [](const PoolDataTypeISASelectorData & data) { return ((data.dl == DataLayout::NCHW) && (data.dt == DataType::F16 && data.isa.fp16)); },
121*c217d954SCole Faust REGISTER_FP16_NEON(arm_compute::cpu::poolingMxN_fp16_neon_nchw)
122*c217d954SCole Faust },
123*c217d954SCole Faust {
124*c217d954SCole Faust "neon_fp32_nchw_pool2",
__anonac3f3cc30f02() 125*c217d954SCole Faust [](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)); },
126*c217d954SCole Faust REGISTER_FP32_NEON(arm_compute::cpu::pooling2_fp32_neon_nchw)
127*c217d954SCole Faust },
128*c217d954SCole Faust {
129*c217d954SCole Faust "neon_fp32_nchw_pool3",
__anonac3f3cc31002() 130*c217d954SCole Faust [](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)); },
131*c217d954SCole Faust REGISTER_FP32_NEON(arm_compute::cpu::pooling3_fp32_neon_nchw)
132*c217d954SCole Faust },
133*c217d954SCole Faust {
134*c217d954SCole Faust "neon_fp32_nchw_pool7",
__anonac3f3cc31102() 135*c217d954SCole Faust [](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)); },
136*c217d954SCole Faust REGISTER_FP32_NEON(arm_compute::cpu::pooling7_fp32_neon_nchw)
137*c217d954SCole Faust },
138*c217d954SCole Faust {
139*c217d954SCole Faust "neon_fp32_nchw_poolMxN",
__anonac3f3cc31202() 140*c217d954SCole Faust [](const PoolDataTypeISASelectorData & data) { return ((data.dl == DataLayout::NCHW) && (data.dt == DataType::F32)); },
141*c217d954SCole Faust REGISTER_FP32_NEON(arm_compute::cpu::poolingMxN_fp32_neon_nchw)
142*c217d954SCole Faust },
143*c217d954SCole Faust #endif /* defined(ENABLE_NCHW_KERNELS) */
144*c217d954SCole Faust };
145*c217d954SCole Faust
validate_arguments(const ITensorInfo * src,const ITensorInfo * dst,const PoolingLayerInfo & pool_info,const ITensorInfo * indices,Size2D pool_size)146*c217d954SCole Faust Status validate_arguments(const ITensorInfo *src, const ITensorInfo *dst, const PoolingLayerInfo &pool_info,
147*c217d954SCole Faust const ITensorInfo *indices, Size2D pool_size)
148*c217d954SCole Faust {
149*c217d954SCole Faust ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(src, dst);
150*c217d954SCole Faust ARM_COMPUTE_RETURN_ERROR_ON(pool_size.x() == 0);
151*c217d954SCole Faust ARM_COMPUTE_RETURN_ERROR_ON(pool_size.y() == 0);
152*c217d954SCole Faust
153*c217d954SCole Faust int pool_stride_x = 0;
154*c217d954SCole Faust int pool_stride_y = 0;
155*c217d954SCole Faust int output_width = 0;
156*c217d954SCole Faust int output_height = 0;
157*c217d954SCole Faust PoolingType pool_type = pool_info.pool_type;
158*c217d954SCole Faust const PadStrideInfo pad_stride_info = pool_info.pad_stride_info;
159*c217d954SCole Faust const auto data_layout = pool_info.data_layout == DataLayout::UNKNOWN ? src->data_layout() : pool_info.data_layout;
160*c217d954SCole Faust const int idx_width = get_data_layout_dimension_index(data_layout, DataLayoutDimension::WIDTH);
161*c217d954SCole Faust const int idx_height = get_data_layout_dimension_index(data_layout, DataLayoutDimension::HEIGHT);
162*c217d954SCole Faust
163*c217d954SCole Faust ARM_COMPUTE_RETURN_ERROR_ON_MSG((!is_data_type_float(src->data_type()))
164*c217d954SCole Faust && (is_pool_region_entirely_outside_input(pool_info)),
165*c217d954SCole Faust "Pooling region that is entirely outside input tensor is unsupported for non-float types");
166*c217d954SCole Faust
167*c217d954SCole Faust std::tie(output_width, output_height) = scaled_dimensions_signed(src->tensor_shape()[idx_width], src->tensor_shape()[idx_height],
168*c217d954SCole Faust pool_size.x(), pool_size.y(), pool_info.pad_stride_info);
169*c217d954SCole Faust ARM_COMPUTE_RETURN_ERROR_ON_MSG((output_width < 1 || output_height < 1), "Calculated output dimension size is invalid");
170*c217d954SCole Faust
171*c217d954SCole Faust TensorInfo out_info(TensorInfo(compute_pool_shape(*src, pool_info), 1, dst->data_type()));
172*c217d954SCole Faust std::tie(pool_stride_x, pool_stride_y) = pad_stride_info.stride();
173*c217d954SCole Faust
174*c217d954SCole Faust ARM_COMPUTE_RETURN_ERROR_ON_CPU_F16_UNSUPPORTED(src);
175*c217d954SCole Faust if(indices)
176*c217d954SCole Faust {
177*c217d954SCole Faust ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(src, 1, DataType::F32, DataType::F16);
178*c217d954SCole Faust ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(indices, 1, DataType::U32);
179*c217d954SCole Faust ARM_COMPUTE_RETURN_ERROR_ON_MSG(pool_type != PoolingType::MAX, "Pooling indices only supported for MAX pooling method");
180*c217d954SCole Faust }
181*c217d954SCole Faust ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(src, 1, DataType::QASYMM8, DataType::QASYMM8_SIGNED, DataType::F16, DataType::F32);
182*c217d954SCole Faust ARM_COMPUTE_RETURN_ERROR_ON(pool_type == PoolingType::L2 && is_data_type_quantized(src->data_type()));
183*c217d954SCole Faust 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()
184*c217d954SCole Faust && (src->data_layout() == DataLayout::NHWC),
185*c217d954SCole Faust "exclude_padding equal false is not supported for AVG Pooling with padding on quantized types");
186*c217d954SCole Faust
187*c217d954SCole Faust if(dst->total_size() != 0)
188*c217d954SCole Faust {
189*c217d954SCole Faust ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(src, dst);
190*c217d954SCole Faust ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_LAYOUT(src, dst);
191*c217d954SCole Faust ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_SHAPES(dst, &out_info);
192*c217d954SCole Faust if(indices)
193*c217d954SCole Faust {
194*c217d954SCole Faust ARM_COMPUTE_RETURN_ERROR_ON_MSG((pool_size != Size2D(2, 2)), "Pooling indices only supported for pool size 2x2");
195*c217d954SCole Faust ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_SHAPES(indices, &out_info);
196*c217d954SCole Faust }
197*c217d954SCole Faust }
198*c217d954SCole Faust
199*c217d954SCole Faust const auto *uk = CpuPool2dKernel::get_implementation(PoolDataTypeISASelectorData{ src->data_type(), src->data_layout(), pool_stride_x, pool_size, CPUInfo::get().get_isa() });
200*c217d954SCole Faust ARM_COMPUTE_RETURN_ERROR_ON(uk == nullptr || uk->ukernel == nullptr);
201*c217d954SCole Faust
202*c217d954SCole Faust return Status{};
203*c217d954SCole Faust }
204*c217d954SCole Faust
validate_and_configure_window(ITensorInfo * src,ITensorInfo * dst,ITensorInfo * indices,const PoolingLayerInfo & pool_info,unsigned int & num_elems_processed_per_iteration,int pool_size_x,int pool_size_y)205*c217d954SCole Faust std::pair<Status, Window> validate_and_configure_window(ITensorInfo *src, ITensorInfo *dst, ITensorInfo *indices, const PoolingLayerInfo &pool_info,
206*c217d954SCole Faust unsigned int &num_elems_processed_per_iteration,
207*c217d954SCole Faust int pool_size_x, int pool_size_y)
208*c217d954SCole Faust {
209*c217d954SCole Faust // dst auto inizialitation if not yet initialized
210*c217d954SCole Faust auto_init_if_empty(*dst, src->clone()->set_tensor_shape(compute_pool_shape(*src, pool_info)));
211*c217d954SCole Faust if(indices)
212*c217d954SCole Faust {
213*c217d954SCole Faust // Indices auto inizialitation if not yet initialized
214*c217d954SCole Faust auto_init_if_empty(*indices, (src->clone()->set_tensor_shape(compute_pool_shape(*src,
215*c217d954SCole Faust pool_info)))
216*c217d954SCole Faust .set_data_type(DataType::U32) /* we store the offset to the element */);
217*c217d954SCole Faust }
218*c217d954SCole Faust const auto data_layout = pool_info.data_layout == DataLayout::UNKNOWN ? src->data_layout() : pool_info.data_layout;
219*c217d954SCole Faust
220*c217d954SCole Faust int pool_stride_x = 0;
221*c217d954SCole Faust int pool_stride_y = 0;
222*c217d954SCole Faust const int idx_width = get_data_layout_dimension_index(data_layout, DataLayoutDimension::WIDTH);
223*c217d954SCole Faust const int idx_height = get_data_layout_dimension_index(data_layout, DataLayoutDimension::HEIGHT);
224*c217d954SCole Faust const PadStrideInfo pad_stride_info = pool_info.pad_stride_info;
225*c217d954SCole Faust
226*c217d954SCole Faust std::tie(pool_stride_x, pool_stride_y) = pad_stride_info.stride();
227*c217d954SCole Faust const bool is_square = pool_size_x == pool_size_y;
228*c217d954SCole Faust const unsigned int pooled_w = dst->dimension(idx_width);
229*c217d954SCole Faust const unsigned int pooled_h = dst->dimension(idx_height);
230*c217d954SCole Faust
231*c217d954SCole Faust //If it's not squared and optimized will be executed the MxN
232*c217d954SCole Faust num_elems_processed_per_iteration = 1;
233*c217d954SCole Faust
234*c217d954SCole Faust if(is_square)
235*c217d954SCole Faust {
236*c217d954SCole Faust switch(src->data_type())
237*c217d954SCole Faust {
238*c217d954SCole Faust case DataType::QASYMM8:
239*c217d954SCole Faust case DataType::QASYMM8_SIGNED:
240*c217d954SCole Faust switch(pool_size_x)
241*c217d954SCole Faust {
242*c217d954SCole Faust case 2:
243*c217d954SCole Faust num_elems_processed_per_iteration = (pool_stride_x == 2) ? 8 : 15;
244*c217d954SCole Faust break;
245*c217d954SCole Faust case 3:
246*c217d954SCole Faust num_elems_processed_per_iteration = (pool_stride_x == 2) ? 7 : 14;
247*c217d954SCole Faust break;
248*c217d954SCole Faust default:
249*c217d954SCole Faust break;
250*c217d954SCole Faust }
251*c217d954SCole Faust break;
252*c217d954SCole Faust #ifdef __ARM_FEATURE_FP16_VECTOR_ARITHMETIC
253*c217d954SCole Faust case DataType::F16:
254*c217d954SCole Faust num_elems_processed_per_iteration = 1;
255*c217d954SCole Faust break;
256*c217d954SCole Faust #endif /* __ARM_FEATURE_FP16_VECTOR_ARITHMETIC */
257*c217d954SCole Faust case DataType::F32:
258*c217d954SCole Faust num_elems_processed_per_iteration = 1;
259*c217d954SCole Faust break;
260*c217d954SCole Faust default:
261*c217d954SCole Faust ARM_COMPUTE_ERROR("Element size not supported");
262*c217d954SCole Faust break;
263*c217d954SCole Faust }
264*c217d954SCole Faust }
265*c217d954SCole Faust
266*c217d954SCole Faust bool window_changed = false;
267*c217d954SCole Faust Window win{};
268*c217d954SCole Faust // Upper limit for the number of right/bottom border elements that are accessed
269*c217d954SCole Faust TensorShape dst_shape{ src->tensor_shape() };
270*c217d954SCole Faust dst_shape.set(0, pooled_w);
271*c217d954SCole Faust dst_shape.set(1, pooled_h);
272*c217d954SCole Faust TensorInfo dst_info(src->clone()->set_tensor_shape(dst_shape));
273*c217d954SCole Faust win = calculate_max_window(dst_info, Steps(num_elems_processed_per_iteration));
274*c217d954SCole Faust
275*c217d954SCole Faust Status err = (window_changed) ? ARM_COMPUTE_CREATE_ERROR(ErrorCode::RUNTIME_ERROR, "Insufficient Padding!") : Status{};
276*c217d954SCole Faust return std::make_pair(err, win);
277*c217d954SCole Faust }
278*c217d954SCole Faust } // namespace
279*c217d954SCole Faust
configure(ITensorInfo * src,ITensorInfo * dst,const PoolingLayerInfo & pool_info,ITensorInfo * indices)280*c217d954SCole Faust void CpuPool2dKernel::configure(ITensorInfo *src, ITensorInfo *dst, const PoolingLayerInfo &pool_info, ITensorInfo *indices)
281*c217d954SCole Faust {
282*c217d954SCole Faust ARM_COMPUTE_ERROR_ON_NULLPTR(src, dst);
283*c217d954SCole Faust const PadStrideInfo pad_stride_info = pool_info.pad_stride_info;
284*c217d954SCole Faust const bool is_global_pooling = pool_info.is_global_pooling;
285*c217d954SCole Faust
286*c217d954SCole Faust // Get data layout
287*c217d954SCole Faust const auto data_layout = pool_info.data_layout == DataLayout::UNKNOWN ? src->data_layout() : pool_info.data_layout;
288*c217d954SCole Faust const int idx_width = get_data_layout_dimension_index(data_layout, DataLayoutDimension::WIDTH);
289*c217d954SCole Faust const int idx_height = get_data_layout_dimension_index(data_layout, DataLayoutDimension::HEIGHT);
290*c217d954SCole Faust
291*c217d954SCole Faust // Update pool size in case of global pooling
292*c217d954SCole Faust const Size2D pool_size(
293*c217d954SCole Faust is_global_pooling ? src->dimension(idx_width) : pool_info.pool_size.width,
294*c217d954SCole Faust is_global_pooling ? src->dimension(idx_height) : pool_info.pool_size.height);
295*c217d954SCole Faust
296*c217d954SCole Faust // Perform validation step
297*c217d954SCole Faust ARM_COMPUTE_ERROR_THROW_ON(validate_arguments(src, dst, pool_info, indices, pool_size));
298*c217d954SCole Faust
299*c217d954SCole Faust 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() });
300*c217d954SCole Faust ARM_COMPUTE_ERROR_ON(uk == nullptr);
301*c217d954SCole Faust
302*c217d954SCole Faust // Set instance variables
303*c217d954SCole Faust _pool_info = pool_info;
304*c217d954SCole Faust _data_layout = src->data_layout();
305*c217d954SCole Faust _pool_size = pool_size;
306*c217d954SCole Faust _pool_stride_x = pad_stride_info.stride().first;
307*c217d954SCole Faust _run_method = uk->ukernel;
308*c217d954SCole Faust _name = std::string("CpuPool2dKernel").append("/").append(uk->name);
309*c217d954SCole Faust
310*c217d954SCole Faust if(_data_layout == DataLayout::NHWC)
311*c217d954SCole Faust {
312*c217d954SCole Faust // Configure kernel window
313*c217d954SCole Faust Window win = calculate_max_window(*dst, Steps());
314*c217d954SCole Faust ICpuKernel::configure(win);
315*c217d954SCole Faust }
316*c217d954SCole Faust else
317*c217d954SCole Faust {
318*c217d954SCole Faust // Configure kernel window
319*c217d954SCole Faust auto win_config = validate_and_configure_window(src, dst, indices, pool_info, _num_elems_processed_per_iteration,
320*c217d954SCole Faust pool_size.x(), pool_size.y());
321*c217d954SCole Faust ARM_COMPUTE_ERROR_THROW_ON(win_config.first);
322*c217d954SCole Faust ICpuKernel::configure(win_config.second);
323*c217d954SCole Faust }
324*c217d954SCole Faust }
325*c217d954SCole Faust
validate(const ITensorInfo * src,const ITensorInfo * dst,const PoolingLayerInfo & pool_info,const ITensorInfo * indices)326*c217d954SCole Faust Status CpuPool2dKernel::validate(const ITensorInfo *src, const ITensorInfo *dst, const PoolingLayerInfo &pool_info, const ITensorInfo *indices)
327*c217d954SCole Faust {
328*c217d954SCole Faust ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(src);
329*c217d954SCole Faust
330*c217d954SCole Faust unsigned int num_elems_processed_per_iteration = 0;
331*c217d954SCole Faust
332*c217d954SCole Faust const bool is_global_pooling = pool_info.is_global_pooling;
333*c217d954SCole Faust
334*c217d954SCole Faust // Get data layout
335*c217d954SCole Faust const auto data_layout = pool_info.data_layout == DataLayout::UNKNOWN ? src->data_layout() : pool_info.data_layout;
336*c217d954SCole Faust const int idx_width = get_data_layout_dimension_index(data_layout, DataLayoutDimension::WIDTH);
337*c217d954SCole Faust const int idx_height = get_data_layout_dimension_index(data_layout, DataLayoutDimension::HEIGHT);
338*c217d954SCole Faust
339*c217d954SCole Faust unsigned int pool_size_x = is_global_pooling ? src->dimension(idx_width) : pool_info.pool_size.width;
340*c217d954SCole Faust unsigned int pool_size_y = is_global_pooling ? src->dimension(idx_height) : pool_info.pool_size.height;
341*c217d954SCole Faust
342*c217d954SCole Faust ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments(src, dst, pool_info, indices, Size2D(pool_size_x, pool_size_y)));
343*c217d954SCole Faust ARM_COMPUTE_RETURN_ON_ERROR(validate_and_configure_window(src->clone().get(), dst->clone().get(),
344*c217d954SCole Faust (indices) ? indices->clone().get() : nullptr, pool_info, num_elems_processed_per_iteration,
345*c217d954SCole Faust pool_size_x, pool_size_y)
346*c217d954SCole Faust .first);
347*c217d954SCole Faust
348*c217d954SCole Faust return Status{};
349*c217d954SCole Faust }
350*c217d954SCole Faust
run_op(ITensorPack & tensors,const Window & window,const ThreadInfo & info)351*c217d954SCole Faust void CpuPool2dKernel::run_op(ITensorPack &tensors, const Window &window, const ThreadInfo &info)
352*c217d954SCole Faust {
353*c217d954SCole Faust ARM_COMPUTE_UNUSED(info);
354*c217d954SCole Faust ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this);
355*c217d954SCole Faust ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(ICpuKernel::window(), window);
356*c217d954SCole Faust ARM_COMPUTE_ERROR_ON(_run_method == nullptr);
357*c217d954SCole Faust
358*c217d954SCole Faust const ITensor *src = tensors.get_const_tensor(TensorType::ACL_SRC_0);
359*c217d954SCole Faust ITensor *dst = tensors.get_tensor(TensorType::ACL_DST_0);
360*c217d954SCole Faust ITensor *indices = tensors.get_tensor(TensorType::ACL_DST_1);
361*c217d954SCole Faust
362*c217d954SCole Faust const unsigned int pool_stride_x = _pool_info.pad_stride_info.stride().first;
363*c217d954SCole Faust const unsigned int pool_stride_y = _pool_info.pad_stride_info.stride().second;
364*c217d954SCole Faust const unsigned int pool_size = _pool_info.pool_size.width;
365*c217d954SCole Faust
366*c217d954SCole Faust Window window_src(window);
367*c217d954SCole Faust if(_data_layout == DataLayout::NCHW)
368*c217d954SCole Faust {
369*c217d954SCole Faust // Set step for src in x and y direction for the src
370*c217d954SCole Faust unsigned int window_x_inc = 0;
371*c217d954SCole Faust switch(src->info()->data_type())
372*c217d954SCole Faust {
373*c217d954SCole Faust case DataType::QASYMM8:
374*c217d954SCole Faust case DataType::QASYMM8_SIGNED:
375*c217d954SCole Faust {
376*c217d954SCole Faust window_x_inc = pool_stride_x;
377*c217d954SCole Faust if((pool_size == 2 || pool_size == 3) && pool_stride_x < 3)
378*c217d954SCole Faust {
379*c217d954SCole Faust window_x_inc = (pool_stride_x == 2) ? _num_elems_processed_per_iteration * 2 : _num_elems_processed_per_iteration;
380*c217d954SCole Faust }
381*c217d954SCole Faust break;
382*c217d954SCole Faust }
383*c217d954SCole Faust
384*c217d954SCole Faust case DataType::F16:
385*c217d954SCole Faust case DataType::F32:
386*c217d954SCole Faust {
387*c217d954SCole Faust window_x_inc = pool_stride_x;
388*c217d954SCole Faust break;
389*c217d954SCole Faust }
390*c217d954SCole Faust default:
391*c217d954SCole Faust {
392*c217d954SCole Faust ARM_COMPUTE_ERROR("Not supported");
393*c217d954SCole Faust }
394*c217d954SCole Faust }
395*c217d954SCole Faust window_src.set(Window::DimX, Window::Dimension(window.x().start() * pool_stride_x, window.x().end() * pool_stride_x, window_x_inc));
396*c217d954SCole Faust window_src.set(Window::DimY, Window::Dimension(window.y().start() * pool_stride_y, window.y().end() * pool_stride_y, pool_stride_y));
397*c217d954SCole Faust }
398*c217d954SCole Faust else
399*c217d954SCole Faust {
400*c217d954SCole Faust window_src.set(Window::DimX, Window::Dimension(0, 1, 1));
401*c217d954SCole Faust window_src.set(Window::DimY, Window::Dimension(0, src->info()->dimension(1), pool_stride_x));
402*c217d954SCole Faust window_src.set(Window::DimZ, Window::Dimension(0, src->info()->dimension(2), pool_stride_y));
403*c217d954SCole Faust }
404*c217d954SCole Faust _run_method(src, dst, indices, _pool_info, window_src, window);
405*c217d954SCole Faust }
406*c217d954SCole Faust
name() const407*c217d954SCole Faust const char *CpuPool2dKernel::name() const
408*c217d954SCole Faust {
409*c217d954SCole Faust return _name.c_str();
410*c217d954SCole Faust }
411*c217d954SCole Faust
get_available_kernels()412*c217d954SCole Faust const std::vector<CpuPool2dKernel::PoolingKernel> &CpuPool2dKernel::get_available_kernels()
413*c217d954SCole Faust {
414*c217d954SCole Faust return available_kernels;
415*c217d954SCole Faust }
416*c217d954SCole Faust
417*c217d954SCole Faust } // namespace kernels
418*c217d954SCole Faust } // namespace cpu
419*c217d954SCole Faust } // namespace arm_compute
420