1*c217d954SCole Faust /*
2*c217d954SCole Faust * Copyright (c) 2017-2021 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/gpu/cl/kernels/ClPool2dKernel.h"
25*c217d954SCole Faust
26*c217d954SCole Faust #include "arm_compute/core/CL/ICLTensor.h"
27*c217d954SCole Faust #include "arm_compute/core/TensorInfo.h"
28*c217d954SCole Faust #include "arm_compute/core/utils/misc/ShapeCalculator.h"
29*c217d954SCole Faust #include "src/core/CL/CLValidate.h"
30*c217d954SCole Faust #include "src/core/helpers/AutoConfiguration.h"
31*c217d954SCole Faust #include "src/core/helpers/WindowHelpers.h"
32*c217d954SCole Faust #include "support/Cast.h"
33*c217d954SCole Faust
34*c217d954SCole Faust namespace arm_compute
35*c217d954SCole Faust {
36*c217d954SCole Faust namespace opencl
37*c217d954SCole Faust {
38*c217d954SCole Faust namespace kernels
39*c217d954SCole Faust {
40*c217d954SCole Faust using namespace arm_compute::misc::shape_calculator;
41*c217d954SCole Faust
42*c217d954SCole Faust namespace
43*c217d954SCole Faust {
validate_arguments(const ITensorInfo * src,const ITensorInfo * dst,const PoolingLayerInfo & pool_info,const ITensorInfo * indices)44*c217d954SCole Faust Status validate_arguments(const ITensorInfo *src, const ITensorInfo *dst, const PoolingLayerInfo &pool_info, const ITensorInfo *indices)
45*c217d954SCole Faust {
46*c217d954SCole Faust ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(src, dst);
47*c217d954SCole Faust ARM_COMPUTE_RETURN_ERROR_ON_F16_UNSUPPORTED(src);
48*c217d954SCole Faust ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(src, 1, DataType::QASYMM8, DataType::QASYMM8_SIGNED, DataType::F16, DataType::F32);
49*c217d954SCole Faust ARM_COMPUTE_RETURN_ERROR_ON_MSG((is_data_type_quantized_asymmetric(src->data_type()) && pool_info.pool_type == PoolingType::L2),
50*c217d954SCole Faust "Unsupported combination of parameters!");
51*c217d954SCole Faust
52*c217d954SCole Faust const auto data_layout = pool_info.data_layout == DataLayout::UNKNOWN ? src->data_layout() : pool_info.data_layout;
53*c217d954SCole Faust const int idx_width = get_data_layout_dimension_index(data_layout, DataLayoutDimension::WIDTH);
54*c217d954SCole Faust const int idx_height = get_data_layout_dimension_index(data_layout, DataLayoutDimension::HEIGHT);
55*c217d954SCole Faust const bool is_global_pooling = pool_info.is_global_pooling;
56*c217d954SCole Faust unsigned int pool_size_x = is_global_pooling ? src->dimension(idx_width) : pool_info.pool_size.width;
57*c217d954SCole Faust unsigned int pool_size_y = is_global_pooling ? src->dimension(idx_height) : pool_info.pool_size.height;
58*c217d954SCole Faust int output_width = 0;
59*c217d954SCole Faust int output_height = 0;
60*c217d954SCole Faust
61*c217d954SCole Faust ARM_COMPUTE_RETURN_ERROR_ON_MSG(is_pool_region_entirely_outside_input(pool_info), "Pooling region that is entirely outside input tensor is unsupported");
62*c217d954SCole Faust
63*c217d954SCole Faust std::tie(output_width, output_height) = scaled_dimensions_signed(src->tensor_shape()[idx_width], src->tensor_shape()[idx_height],
64*c217d954SCole Faust pool_size_x, pool_size_y, pool_info.pad_stride_info);
65*c217d954SCole Faust ARM_COMPUTE_RETURN_ERROR_ON_MSG((output_width < 1 || output_height < 1), "Calculated output dimension size is invalid");
66*c217d954SCole Faust
67*c217d954SCole Faust // Check indices
68*c217d954SCole Faust if(indices)
69*c217d954SCole Faust {
70*c217d954SCole Faust ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(src, 1, DataType::F16, DataType::F32);
71*c217d954SCole Faust ARM_COMPUTE_RETURN_ERROR_ON_MSG(pool_info.pool_type != PoolingType::MAX, "Pooling indices only supported for MAX pooling method");
72*c217d954SCole Faust ARM_COMPUTE_RETURN_ERROR_ON_MSG((pool_info.pool_size != Size2D(2, 2)), "Pooling indices only supported for pool size 2x2");
73*c217d954SCole Faust
74*c217d954SCole Faust if(indices->total_size() != 0)
75*c217d954SCole Faust {
76*c217d954SCole Faust TensorInfo idx_info(TensorInfo(compute_pool_shape(*src, pool_info), 1, DataType::U32));
77*c217d954SCole Faust ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_SHAPES(indices, &idx_info);
78*c217d954SCole Faust }
79*c217d954SCole Faust }
80*c217d954SCole Faust
81*c217d954SCole Faust // Checks performed when dst is configured
82*c217d954SCole Faust if(dst->total_size() != 0)
83*c217d954SCole Faust {
84*c217d954SCole Faust ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(src, dst);
85*c217d954SCole Faust ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_LAYOUT(src, dst);
86*c217d954SCole Faust TensorInfo out_info(TensorInfo(compute_pool_shape(*src, pool_info), 1, dst->data_type()));
87*c217d954SCole Faust ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_SHAPES(dst, &out_info);
88*c217d954SCole Faust }
89*c217d954SCole Faust
90*c217d954SCole Faust return Status{};
91*c217d954SCole Faust }
92*c217d954SCole Faust } // namespace
93*c217d954SCole Faust
ClPool2dKernel()94*c217d954SCole Faust ClPool2dKernel::ClPool2dKernel()
95*c217d954SCole Faust {
96*c217d954SCole Faust _type = CLKernelType::POOL;
97*c217d954SCole Faust }
98*c217d954SCole Faust
configure(const ClCompileContext & compile_context,ITensorInfo * src,ITensorInfo * dst,const PoolingLayerInfo & pool_info,ITensorInfo * indices)99*c217d954SCole Faust void ClPool2dKernel::configure(const ClCompileContext &compile_context, ITensorInfo *src, ITensorInfo *dst, const PoolingLayerInfo &pool_info, ITensorInfo *indices)
100*c217d954SCole Faust {
101*c217d954SCole Faust ARM_COMPUTE_ERROR_ON_NULLPTR(src, dst);
102*c217d954SCole Faust ARM_COMPUTE_ERROR_THROW_ON(validate_arguments(src, dst, pool_info, indices));
103*c217d954SCole Faust
104*c217d954SCole Faust auto padding_info = get_padding_info({ src, dst, indices });
105*c217d954SCole Faust
106*c217d954SCole Faust // Auto init if empty
107*c217d954SCole Faust TensorShape out_shape = compute_pool_shape(*src, pool_info);
108*c217d954SCole Faust auto_init_if_empty(*dst, src->clone()->set_tensor_shape(out_shape));
109*c217d954SCole Faust if(indices)
110*c217d954SCole Faust {
111*c217d954SCole Faust auto_init_if_empty(*indices, src->clone()->set_tensor_shape(out_shape).set_data_type(DataType::U32));
112*c217d954SCole Faust }
113*c217d954SCole Faust
114*c217d954SCole Faust // Set instance variables
115*c217d954SCole Faust _pool_info = pool_info;
116*c217d954SCole Faust _data_layout = pool_info.data_layout == DataLayout::UNKNOWN ? src->data_layout() : pool_info.data_layout;
117*c217d954SCole Faust _num_elems_processed_per_iteration = (_data_layout == DataLayout::NCHW) ? 1 : ((dst->data_type() == DataType::F32) ? 2 : 4);
118*c217d954SCole Faust _num_elems_processed_per_iteration = adjust_vec_size(_num_elems_processed_per_iteration, dst->dimension(0));
119*c217d954SCole Faust
120*c217d954SCole Faust int pool_stride_x = 0;
121*c217d954SCole Faust int pool_stride_y = 0;
122*c217d954SCole Faust const PoolingType pool_type = pool_info.pool_type;
123*c217d954SCole Faust const int idx_width = get_data_layout_dimension_index(_data_layout, DataLayoutDimension::WIDTH);
124*c217d954SCole Faust const int idx_height = get_data_layout_dimension_index(_data_layout, DataLayoutDimension::HEIGHT);
125*c217d954SCole Faust const int idx_channel = get_data_layout_dimension_index(_data_layout, DataLayoutDimension::CHANNEL);
126*c217d954SCole Faust const int idx_batch_size = get_data_layout_dimension_index(_data_layout, DataLayoutDimension::BATCHES);
127*c217d954SCole Faust const int pool_size_x = pool_info.is_global_pooling ? src->dimension(idx_width) : pool_info.pool_size.width;
128*c217d954SCole Faust const int pool_size_y = pool_info.is_global_pooling ? src->dimension(idx_height) : pool_info.pool_size.height;
129*c217d954SCole Faust const PadStrideInfo pad_stride_info = pool_info.pad_stride_info;
130*c217d954SCole Faust const bool exclude_padding = pool_info.exclude_padding;
131*c217d954SCole Faust std::tie(pool_stride_x, pool_stride_y) = pad_stride_info.stride();
132*c217d954SCole Faust const int pool_pad_top = pad_stride_info.pad_top();
133*c217d954SCole Faust const int pool_pad_left = pad_stride_info.pad_left();
134*c217d954SCole Faust const DataType data_type = src->data_type();
135*c217d954SCole Faust
136*c217d954SCole Faust // Set build options
137*c217d954SCole Faust CLBuildOptions build_opts;
138*c217d954SCole Faust build_opts.add_option("-DVEC_SIZE=" + support::cpp11::to_string(_num_elems_processed_per_iteration));
139*c217d954SCole Faust build_opts.add_option("-DDATA_TYPE=" + get_cl_type_from_data_type(data_type));
140*c217d954SCole Faust build_opts.add_option("-DPOOL_" + string_from_pooling_type(pool_type));
141*c217d954SCole Faust build_opts.add_option("-DSTRIDE_X=" + support::cpp11::to_string(pool_stride_x));
142*c217d954SCole Faust build_opts.add_option("-DSTRIDE_Y=" + support::cpp11::to_string(pool_stride_y));
143*c217d954SCole Faust build_opts.add_option("-DPAD_X=" + support::cpp11::to_string(pool_pad_left));
144*c217d954SCole Faust build_opts.add_option("-DPAD_Y=" + support::cpp11::to_string(pool_pad_top));
145*c217d954SCole Faust build_opts.add_option("-DPOOL_SIZE_X=" + support::cpp11::to_string(pool_size_x));
146*c217d954SCole Faust build_opts.add_option("-DPOOL_SIZE_Y=" + support::cpp11::to_string(pool_size_y));
147*c217d954SCole Faust build_opts.add_option("-DSRC_WIDTH=" + support::cpp11::to_string(src->dimension(idx_width)));
148*c217d954SCole Faust build_opts.add_option("-DSRC_HEIGHT=" + support::cpp11::to_string(src->dimension(idx_height)));
149*c217d954SCole Faust build_opts.add_option("-DMAX_WIDTH=" + support::cpp11::to_string(src->dimension(idx_width) + (exclude_padding ? 0 : pool_pad_left)));
150*c217d954SCole Faust build_opts.add_option("-DMAX_HEIGHT=" + support::cpp11::to_string(src->dimension(idx_height) + (exclude_padding ? 0 : pool_pad_top)));
151*c217d954SCole Faust
152*c217d954SCole Faust // Tensor paddings are used to calculate the indicies for MAX pooling
153*c217d954SCole Faust if(pool_info.pool_size == Size2D(2, 2) && pool_type == PoolingType::MAX && indices && is_data_type_float(data_type))
154*c217d954SCole Faust {
155*c217d954SCole Faust build_opts.add_option("-DSRC_BATCH=" + support::cpp11::to_string(src->tensor_shape().total_size_lower(3)));
156*c217d954SCole Faust }
157*c217d954SCole Faust
158*c217d954SCole Faust if(is_data_type_quantized_asymmetric(data_type))
159*c217d954SCole Faust {
160*c217d954SCole Faust build_opts.add_option("-DQUANTIZED");
161*c217d954SCole Faust
162*c217d954SCole Faust if(src->quantization_info() != dst->quantization_info())
163*c217d954SCole Faust {
164*c217d954SCole Faust const UniformQuantizationInfo iq_info = src->quantization_info().uniform();
165*c217d954SCole Faust const UniformQuantizationInfo oq_info = dst->quantization_info().uniform();
166*c217d954SCole Faust
167*c217d954SCole Faust build_opts.add_option("-DOFFSET_IN1=" + float_to_string_with_full_precision(iq_info.offset));
168*c217d954SCole Faust build_opts.add_option("-DOFFSET_OUT=" + float_to_string_with_full_precision(oq_info.offset));
169*c217d954SCole Faust build_opts.add_option("-DSCALE_IN1=" + float_to_string_with_full_precision(iq_info.scale));
170*c217d954SCole Faust build_opts.add_option("-DSCALE_OUT=" + float_to_string_with_full_precision(oq_info.scale));
171*c217d954SCole Faust }
172*c217d954SCole Faust }
173*c217d954SCole Faust
174*c217d954SCole Faust // Set the initial value for the pooling operation accordingly with the data type
175*c217d954SCole Faust if(pool_type == PoolingType::MAX)
176*c217d954SCole Faust {
177*c217d954SCole Faust if(is_data_type_quantized(data_type))
178*c217d954SCole Faust {
179*c217d954SCole Faust PixelValue type_min{};
180*c217d954SCole Faust std::tie(type_min, std::ignore) = get_min_max(data_type);
181*c217d954SCole Faust build_opts.add_option("-DINITIAL_VALUE=" + support::cpp11::to_string(type_min.get<int32_t>()));
182*c217d954SCole Faust }
183*c217d954SCole Faust else
184*c217d954SCole Faust {
185*c217d954SCole Faust build_opts.add_option("-DINITIAL_VALUE=" + float_to_string_with_full_precision(std::numeric_limits<float>::lowest()));
186*c217d954SCole Faust }
187*c217d954SCole Faust }
188*c217d954SCole Faust else
189*c217d954SCole Faust {
190*c217d954SCole Faust // Pool AVG and Pool L2 initial value
191*c217d954SCole Faust build_opts.add_option("-DINITIAL_VALUE=0");
192*c217d954SCole Faust }
193*c217d954SCole Faust
194*c217d954SCole Faust // Create kernel
195*c217d954SCole Faust switch(_data_layout)
196*c217d954SCole Faust {
197*c217d954SCole Faust case DataLayout::NCHW:
198*c217d954SCole Faust {
199*c217d954SCole Faust const auto use_fp_mixed_precision = (data_type == DataType::F16) && pool_info.fp_mixed_precision;
200*c217d954SCole Faust const auto use_wider_accumulator = use_fp_mixed_precision && (pool_type != PoolingType::MAX);
201*c217d954SCole Faust const auto acc_data_type = get_cl_type_from_data_type(use_wider_accumulator ? DataType::F32 : (is_data_type_quantized(data_type) ? DataType::S32 : data_type));
202*c217d954SCole Faust build_opts.add_option("-DACC_DATA_TYPE=" + acc_data_type);
203*c217d954SCole Faust build_opts.add_option_if(use_wider_accumulator, "-DFP_MIXED_PRECISION");
204*c217d954SCole Faust
205*c217d954SCole Faust if(pool_type != PoolingType::MAX)
206*c217d954SCole Faust {
207*c217d954SCole Faust build_opts.add_option_if(exclude_padding, "-DEXCLUDE_PADDING");
208*c217d954SCole Faust }
209*c217d954SCole Faust
210*c217d954SCole Faust if(pool_info.pool_size == Size2D(2, 2) && pool_type == PoolingType::MAX && indices && is_data_type_float(data_type))
211*c217d954SCole Faust {
212*c217d954SCole Faust // For max pooling with pool2x2, store indicies which will be used in max unpooling
213*c217d954SCole Faust std::string kernel_name = "pooling_layer_2_nchw_indices";
214*c217d954SCole Faust _kernel = create_kernel(compile_context, kernel_name, build_opts.options());
215*c217d954SCole Faust }
216*c217d954SCole Faust else // Run general case
217*c217d954SCole Faust {
218*c217d954SCole Faust std::string kernel_name = "pooling_layer_MxN_nchw";
219*c217d954SCole Faust _kernel = create_kernel(compile_context, kernel_name, build_opts.options());
220*c217d954SCole Faust }
221*c217d954SCole Faust break;
222*c217d954SCole Faust }
223*c217d954SCole Faust case DataLayout::NHWC:
224*c217d954SCole Faust {
225*c217d954SCole Faust // Floating point mixed precision is support on F16 only
226*c217d954SCole Faust const auto use_fp_mixed_precision = (data_type == DataType::F16) && pool_info.fp_mixed_precision && pool_type != PoolingType::MAX;
227*c217d954SCole Faust
228*c217d954SCole Faust // Wider accumulation is required to avoid accuracy loss
229*c217d954SCole Faust // Case 1: Floating point mixed precision (fp16 src data and fp32 accumulation)
230*c217d954SCole Faust // Cast 2: Quantized (int8/uint8 src data and int32 accumulation )
231*c217d954SCole Faust DataType acc_data_type = data_type;
232*c217d954SCole Faust
233*c217d954SCole Faust if(use_fp_mixed_precision)
234*c217d954SCole Faust {
235*c217d954SCole Faust acc_data_type = DataType::F32;
236*c217d954SCole Faust }
237*c217d954SCole Faust else if(is_data_type_quantized(data_type) && pool_type != PoolingType::MAX)
238*c217d954SCole Faust {
239*c217d954SCole Faust acc_data_type = DataType::S32;
240*c217d954SCole Faust }
241*c217d954SCole Faust
242*c217d954SCole Faust build_opts.add_option("-DACC_DATA_TYPE=" + get_cl_type_from_data_type(acc_data_type));
243*c217d954SCole Faust build_opts.add_option_if(use_fp_mixed_precision, "-DFP_MIXED_PRECISION");
244*c217d954SCole Faust build_opts.add_option_if(exclude_padding, "-DEXCLUDE_PADDING");
245*c217d954SCole Faust build_opts.add_option("-DSRC_WIDTH=" + support::cpp11::to_string(src->dimension(idx_width)));
246*c217d954SCole Faust build_opts.add_option("-DSRC_HEIGHT=" + support::cpp11::to_string(src->dimension(idx_height)));
247*c217d954SCole Faust build_opts.add_option("-DDST_HEIGHT=" + support::cpp11::to_string(dst->dimension(idx_height)));
248*c217d954SCole Faust build_opts.add_option("-DDST_CHANNELS=" + support::cpp11::to_string(dst->dimension(idx_channel)));
249*c217d954SCole Faust build_opts.add_option("-DDST_BATCH_SIZE=" + support::cpp11::to_string(dst->dimension(idx_batch_size)));
250*c217d954SCole Faust build_opts.add_option("-DVEC_SIZE_LEFTOVER=" + support::cpp11::to_string(src->dimension(0) % _num_elems_processed_per_iteration));
251*c217d954SCole Faust if(pool_info.pool_size == Size2D(2, 2) && is_data_type_float(data_type))
252*c217d954SCole Faust {
253*c217d954SCole Faust build_opts.add_option_if(indices != nullptr && pool_type == PoolingType::MAX, "-DEXTRACT_MAX_INDEX");
254*c217d954SCole Faust
255*c217d954SCole Faust std::string kernel_name = "pooling_layer_2x2_nhwc";
256*c217d954SCole Faust _kernel = create_kernel(compile_context, kernel_name, build_opts.options());
257*c217d954SCole Faust }
258*c217d954SCole Faust else
259*c217d954SCole Faust {
260*c217d954SCole Faust std::string kernel_name = is_data_type_quantized_asymmetric(data_type) ? "pooling_layer_MxN_quantized_nhwc" : "pooling_layer_MxN_nhwc";
261*c217d954SCole Faust _kernel = create_kernel(compile_context, kernel_name, build_opts.options());
262*c217d954SCole Faust }
263*c217d954SCole Faust break;
264*c217d954SCole Faust }
265*c217d954SCole Faust default:
266*c217d954SCole Faust ARM_COMPUTE_ERROR("Not implemented");
267*c217d954SCole Faust }
268*c217d954SCole Faust
269*c217d954SCole Faust // Configure kernel window
270*c217d954SCole Faust Window win = calculate_max_window(*dst, Steps(_num_elems_processed_per_iteration));
271*c217d954SCole Faust ICLKernel::configure_internal(win);
272*c217d954SCole Faust
273*c217d954SCole Faust // Set config_id for enabling LWS tuning
274*c217d954SCole Faust _config_id = "pooling_layer_";
275*c217d954SCole Faust _config_id += lower_string(string_from_data_type(data_type));
276*c217d954SCole Faust _config_id += "_";
277*c217d954SCole Faust _config_id += lower_string(string_from_data_layout(_data_layout));
278*c217d954SCole Faust _config_id += "_";
279*c217d954SCole Faust _config_id += support::cpp11::to_string(dst->dimension(idx_width));
280*c217d954SCole Faust _config_id += "_";
281*c217d954SCole Faust _config_id += support::cpp11::to_string(dst->dimension(idx_height));
282*c217d954SCole Faust _config_id += "_";
283*c217d954SCole Faust _config_id += support::cpp11::to_string(dst->dimension(idx_channel));
284*c217d954SCole Faust _config_id += "_";
285*c217d954SCole Faust _config_id += lower_string(string_from_data_layout(src->data_layout()));
286*c217d954SCole Faust
287*c217d954SCole Faust ARM_COMPUTE_ERROR_ON(has_padding_changed(padding_info));
288*c217d954SCole Faust }
289*c217d954SCole Faust
validate(const ITensorInfo * src,const ITensorInfo * dst,const PoolingLayerInfo & pool_info,const ITensorInfo * indices)290*c217d954SCole Faust Status ClPool2dKernel::validate(const ITensorInfo *src, const ITensorInfo *dst, const PoolingLayerInfo &pool_info, const ITensorInfo *indices)
291*c217d954SCole Faust {
292*c217d954SCole Faust ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments(src, dst, pool_info, indices));
293*c217d954SCole Faust return Status{};
294*c217d954SCole Faust }
295*c217d954SCole Faust
run_op(ITensorPack & tensors,const Window & window,cl::CommandQueue & queue)296*c217d954SCole Faust void ClPool2dKernel::run_op(ITensorPack &tensors, const Window &window, cl::CommandQueue &queue)
297*c217d954SCole Faust {
298*c217d954SCole Faust ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this);
299*c217d954SCole Faust ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(ICLKernel::window(), window);
300*c217d954SCole Faust
301*c217d954SCole Faust unsigned int pool_stride_x = 0;
302*c217d954SCole Faust unsigned int pool_stride_y = 0;
303*c217d954SCole Faust std::tie(pool_stride_x, pool_stride_y) = _pool_info.pad_stride_info.stride();
304*c217d954SCole Faust
305*c217d954SCole Faust const auto src = utils::cast::polymorphic_downcast<const ICLTensor *>(tensors.get_const_tensor(TensorType::ACL_SRC));
306*c217d954SCole Faust auto dst = utils::cast::polymorphic_downcast<ICLTensor *>(tensors.get_tensor(TensorType::ACL_DST_0));
307*c217d954SCole Faust auto indices = utils::cast::polymorphic_downcast<ICLTensor *>(tensors.get_tensor(TensorType::ACL_DST_1));
308*c217d954SCole Faust
309*c217d954SCole Faust // Collapse window
310*c217d954SCole Faust Window window_collapsed = window.collapse_if_possible(ICLKernel::window(), Window::DimZ);
311*c217d954SCole Faust
312*c217d954SCole Faust switch(_data_layout)
313*c217d954SCole Faust {
314*c217d954SCole Faust case DataLayout::NCHW:
315*c217d954SCole Faust {
316*c217d954SCole Faust Window slice = window_collapsed.first_slice_window_3D();
317*c217d954SCole Faust do
318*c217d954SCole Faust {
319*c217d954SCole Faust // Set srcs
320*c217d954SCole Faust unsigned int idx = 0;
321*c217d954SCole Faust add_3D_tensor_argument(idx, src, slice);
322*c217d954SCole Faust add_3D_tensor_argument(idx, dst, slice);
323*c217d954SCole Faust if(indices && is_data_type_float(src->info()->data_type()) && (_pool_info.pool_size == Size2D(2, 2)))
324*c217d954SCole Faust {
325*c217d954SCole Faust add_3D_tensor_argument(idx, indices, slice);
326*c217d954SCole Faust }
327*c217d954SCole Faust enqueue(queue, *this, slice, lws_hint());
328*c217d954SCole Faust }
329*c217d954SCole Faust while(window_collapsed.slide_window_slice_3D(slice));
330*c217d954SCole Faust break;
331*c217d954SCole Faust }
332*c217d954SCole Faust case DataLayout::NHWC:
333*c217d954SCole Faust {
334*c217d954SCole Faust const size_t batch_size = dst->info()->tensor_shape().total_size_upper(3);
335*c217d954SCole Faust
336*c217d954SCole Faust Window slice = window_collapsed.first_slice_window_4D();
337*c217d954SCole Faust Window in_slice = window_collapsed.first_slice_window_4D();
338*c217d954SCole Faust in_slice.set(Window::DimX, Window::Dimension(0, src->info()->dimension(0), _num_elems_processed_per_iteration));
339*c217d954SCole Faust in_slice.set(Window::DimY, Window::Dimension(0, src->info()->dimension(1), pool_stride_x));
340*c217d954SCole Faust in_slice.set(Window::DimZ, Window::Dimension(0, src->info()->dimension(2), pool_stride_y));
341*c217d954SCole Faust in_slice.set(3, Window::Dimension(0, batch_size, 1));
342*c217d954SCole Faust do
343*c217d954SCole Faust {
344*c217d954SCole Faust // Set srcs
345*c217d954SCole Faust unsigned int idx = 0;
346*c217d954SCole Faust add_4D_tensor_argument(idx, src, in_slice);
347*c217d954SCole Faust add_4D_tensor_argument(idx, dst, slice);
348*c217d954SCole Faust if(indices && is_data_type_float(src->info()->data_type()) && (_pool_info.pool_type == PoolingType::MAX) && (_pool_info.pool_size == Size2D(2, 2)))
349*c217d954SCole Faust {
350*c217d954SCole Faust add_4D_tensor_argument(idx, indices, slice);
351*c217d954SCole Faust }
352*c217d954SCole Faust enqueue(queue, *this, slice, lws_hint());
353*c217d954SCole Faust }
354*c217d954SCole Faust while(window.slide_window_slice_4D(slice) && window.slide_window_slice_4D(in_slice));
355*c217d954SCole Faust break;
356*c217d954SCole Faust }
357*c217d954SCole Faust default:
358*c217d954SCole Faust ARM_COMPUTE_ERROR("Not implemented");
359*c217d954SCole Faust }
360*c217d954SCole Faust }
361*c217d954SCole Faust } // namespace kernels
362*c217d954SCole Faust } // namespace opencl
363*c217d954SCole Faust } // namespace arm_compute
364