1*c217d954SCole Faust /*
2*c217d954SCole Faust * Copyright (c) 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/gpu/cl/kernels/ClPool3dKernel.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 #include "utils/TypePrinter.h"
34*c217d954SCole Faust
35*c217d954SCole Faust namespace arm_compute
36*c217d954SCole Faust {
37*c217d954SCole Faust namespace opencl
38*c217d954SCole Faust {
39*c217d954SCole Faust namespace kernels
40*c217d954SCole Faust {
41*c217d954SCole Faust using namespace arm_compute::misc::shape_calculator;
42*c217d954SCole Faust
43*c217d954SCole Faust namespace
44*c217d954SCole Faust {
validate_arguments(const ITensorInfo * src,const ITensorInfo * dst,const Pooling3dLayerInfo & pool_info)45*c217d954SCole Faust Status validate_arguments(const ITensorInfo *src, const ITensorInfo *dst, const Pooling3dLayerInfo &pool_info)
46*c217d954SCole Faust {
47*c217d954SCole Faust ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(src, dst);
48*c217d954SCole Faust ARM_COMPUTE_RETURN_ERROR_ON_MSG(src->data_layout() != DataLayout::NDHWC, "Only NDHWC layout supported");
49*c217d954SCole Faust
50*c217d954SCole Faust ARM_COMPUTE_RETURN_ERROR_ON_F16_UNSUPPORTED(src);
51*c217d954SCole Faust ARM_COMPUTE_RETURN_ERROR_ON_MSG((pool_info.stride.x() == 0 || pool_info.stride.y() == 0 || pool_info.stride.z() == 0), "Strides cannot be zero.");
52*c217d954SCole Faust ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(src, 1, DataType::F16, DataType::F32, DataType::QASYMM8_SIGNED, DataType::QASYMM8);
53*c217d954SCole Faust ARM_COMPUTE_RETURN_ERROR_ON_MSG((!is_data_type_float(src->data_type())) && (!pool_info.exclude_padding
54*c217d954SCole Faust && (pool_info.pool_type == PoolingType::AVG)),
55*c217d954SCole Faust "Exclude padding is unsupported for non-float types for Avg op");
56*c217d954SCole Faust
57*c217d954SCole Faust const auto data_layout = src->data_layout();
58*c217d954SCole Faust const int idx_width = get_data_layout_dimension_index(data_layout, DataLayoutDimension::WIDTH);
59*c217d954SCole Faust const int idx_height = get_data_layout_dimension_index(data_layout, DataLayoutDimension::HEIGHT);
60*c217d954SCole Faust const int idx_depth = get_data_layout_dimension_index(data_layout, DataLayoutDimension::DEPTH);
61*c217d954SCole Faust const bool is_global_pooling = pool_info.is_global_pooling;
62*c217d954SCole Faust const unsigned int pool_size_x = is_global_pooling ? src->dimension(idx_width) : pool_info.pool_size.width;
63*c217d954SCole Faust const unsigned int pool_size_y = is_global_pooling ? src->dimension(idx_height) : pool_info.pool_size.height;
64*c217d954SCole Faust const unsigned int pool_size_z = is_global_pooling ? src->dimension(idx_depth) : pool_info.pool_size.depth;
65*c217d954SCole Faust int output_width = 0;
66*c217d954SCole Faust int output_height = 0;
67*c217d954SCole Faust int output_depth = 0;
68*c217d954SCole Faust
69*c217d954SCole Faust bool round_type_ceil_with_asymm_padding = (pool_info.round_type == DimensionRoundingType::CEIL) && (!is_symmetric(pool_info.padding));
70*c217d954SCole Faust ARM_COMPUTE_RETURN_ERROR_ON_MSG(round_type_ceil_with_asymm_padding, "Cannot use dimension round type CEIL when padding is asymmetric.");
71*c217d954SCole Faust
72*c217d954SCole Faust ARM_COMPUTE_RETURN_ERROR_ON_MSG(is_pool_3d_region_entirely_outside_input(pool_info), "Pooling region that is entirely outside input tensor is unsupported");
73*c217d954SCole Faust std::tie(output_width, output_height, output_depth) = scaled_3d_dimensions_signed(src->tensor_shape()[idx_width], src->tensor_shape()[idx_height],
74*c217d954SCole Faust src->tensor_shape()[idx_depth], pool_size_x, pool_size_y,
75*c217d954SCole Faust pool_size_z, pool_info);
76*c217d954SCole Faust
77*c217d954SCole Faust ARM_COMPUTE_RETURN_ERROR_ON_MSG((output_width < 1 || output_height < 1 || output_depth < 1), "Calculated output dimension size is invalid");
78*c217d954SCole Faust // Checks performed when dst is configured
79*c217d954SCole Faust if(dst->total_size() != 0)
80*c217d954SCole Faust {
81*c217d954SCole Faust ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(src, dst);
82*c217d954SCole Faust ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_LAYOUT(src, dst);
83*c217d954SCole Faust TensorInfo out_info(TensorInfo(compute_pool3d_shape(src->tensor_shape(), pool_info), 1, dst->data_type()));
84*c217d954SCole Faust ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_SHAPES(dst, &out_info);
85*c217d954SCole Faust }
86*c217d954SCole Faust
87*c217d954SCole Faust return Status{};
88*c217d954SCole Faust }
89*c217d954SCole Faust } // namespace
90*c217d954SCole Faust
ClPool3dKernel()91*c217d954SCole Faust ClPool3dKernel::ClPool3dKernel()
92*c217d954SCole Faust {
93*c217d954SCole Faust _type = CLKernelType::POOL;
94*c217d954SCole Faust }
95*c217d954SCole Faust
configure(const ClCompileContext & compile_context,const ITensorInfo * src,ITensorInfo * dst,const Pooling3dLayerInfo & pool_info)96*c217d954SCole Faust void ClPool3dKernel::configure(const ClCompileContext &compile_context, const ITensorInfo *src, ITensorInfo *dst, const Pooling3dLayerInfo &pool_info)
97*c217d954SCole Faust {
98*c217d954SCole Faust ARM_COMPUTE_ERROR_ON_NULLPTR(src, dst);
99*c217d954SCole Faust ARM_COMPUTE_ERROR_THROW_ON(validate_arguments(src, dst, pool_info));
100*c217d954SCole Faust auto padding_info = get_padding_info({ src, dst });
101*c217d954SCole Faust
102*c217d954SCole Faust // Auto init if empty
103*c217d954SCole Faust TensorShape out_shape = compute_pool3d_shape(src->tensor_shape(), pool_info);
104*c217d954SCole Faust auto_init_if_empty(*dst, src->clone()->set_tensor_shape(out_shape));
105*c217d954SCole Faust
106*c217d954SCole Faust // Set instance variables
107*c217d954SCole Faust _pool_info = pool_info;
108*c217d954SCole Faust _data_layout = src->data_layout();
109*c217d954SCole Faust
110*c217d954SCole Faust _num_elems_processed_per_iteration = (dst->data_type() == DataType::F32) ? 2 : 4;
111*c217d954SCole Faust _num_elems_processed_per_iteration = adjust_vec_size(_num_elems_processed_per_iteration, dst->dimension(0));
112*c217d954SCole Faust
113*c217d954SCole Faust const PoolingType pool_type = pool_info.pool_type;
114*c217d954SCole Faust const int idx_width = get_data_layout_dimension_index(_data_layout, DataLayoutDimension::WIDTH);
115*c217d954SCole Faust const int idx_height = get_data_layout_dimension_index(_data_layout, DataLayoutDimension::HEIGHT);
116*c217d954SCole Faust const int idx_depth = get_data_layout_dimension_index(_data_layout, DataLayoutDimension::DEPTH);
117*c217d954SCole Faust const int idx_channel = get_data_layout_dimension_index(_data_layout, DataLayoutDimension::CHANNEL);
118*c217d954SCole Faust const int idx_batch_size = get_data_layout_dimension_index(_data_layout, DataLayoutDimension::BATCHES);
119*c217d954SCole Faust const int pool_size_x = pool_info.is_global_pooling ? src->dimension(idx_width) : pool_info.pool_size.width;
120*c217d954SCole Faust const int pool_size_y = pool_info.is_global_pooling ? src->dimension(idx_height) : pool_info.pool_size.height;
121*c217d954SCole Faust const int pool_size_z = pool_info.is_global_pooling ? src->dimension(idx_depth) : pool_info.pool_size.depth;
122*c217d954SCole Faust const bool exclude_padding = pool_info.exclude_padding;
123*c217d954SCole Faust const int pool_stride_x = pool_info.stride.x();
124*c217d954SCole Faust const int pool_stride_y = pool_info.stride.y();
125*c217d954SCole Faust const int pool_stride_z = pool_info.stride.z();
126*c217d954SCole Faust const int pool_pad_top = pool_info.padding.top;
127*c217d954SCole Faust const int pool_pad_left = pool_info.padding.left;
128*c217d954SCole Faust const int pool_pad_front = pool_info.padding.front;
129*c217d954SCole Faust const DataType data_type = src->data_type();
130*c217d954SCole Faust
131*c217d954SCole Faust // Set build options
132*c217d954SCole Faust CLBuildOptions build_opts;
133*c217d954SCole Faust build_opts.add_option("-DVEC_SIZE=" + support::cpp11::to_string(_num_elems_processed_per_iteration));
134*c217d954SCole Faust build_opts.add_option("-DDATA_TYPE=" + get_cl_type_from_data_type(data_type));
135*c217d954SCole Faust build_opts.add_option("-DPOOL_" + string_from_pooling_type(pool_type));
136*c217d954SCole Faust build_opts.add_option("-DSTRIDE_X=" + support::cpp11::to_string(pool_stride_x));
137*c217d954SCole Faust build_opts.add_option("-DSTRIDE_Y=" + support::cpp11::to_string(pool_stride_y));
138*c217d954SCole Faust build_opts.add_option("-DSTRIDE_Z=" + support::cpp11::to_string(pool_stride_z));
139*c217d954SCole Faust build_opts.add_option("-DPAD_X=" + support::cpp11::to_string(pool_pad_left));
140*c217d954SCole Faust build_opts.add_option("-DPAD_Y=" + support::cpp11::to_string(pool_pad_top));
141*c217d954SCole Faust build_opts.add_option("-DPAD_Z=" + support::cpp11::to_string(pool_pad_front));
142*c217d954SCole Faust build_opts.add_option("-DPOOL_SIZE_X=" + support::cpp11::to_string(pool_size_x));
143*c217d954SCole Faust build_opts.add_option("-DPOOL_SIZE_Y=" + support::cpp11::to_string(pool_size_y));
144*c217d954SCole Faust build_opts.add_option("-DPOOL_SIZE_Z=" + support::cpp11::to_string(pool_size_z));
145*c217d954SCole Faust build_opts.add_option("-DSRC_WIDTH=" + support::cpp11::to_string(src->dimension(idx_width)));
146*c217d954SCole Faust build_opts.add_option("-DSRC_HEIGHT=" + support::cpp11::to_string(src->dimension(idx_height)));
147*c217d954SCole Faust build_opts.add_option("-DSRC_DEPTH=" + support::cpp11::to_string(src->dimension(idx_depth)));
148*c217d954SCole Faust
149*c217d954SCole Faust // If datatype is quantized add relevant parameters
150*c217d954SCole Faust if(is_data_type_quantized_asymmetric(data_type) && src->quantization_info() != dst->quantization_info())
151*c217d954SCole Faust {
152*c217d954SCole Faust const UniformQuantizationInfo iq_info = src->quantization_info().uniform();
153*c217d954SCole Faust const UniformQuantizationInfo oq_info = dst->quantization_info().uniform();
154*c217d954SCole Faust
155*c217d954SCole Faust build_opts.add_option("-DOFFSET_IN1=" + float_to_string_with_full_precision(iq_info.offset));
156*c217d954SCole Faust build_opts.add_option("-DOFFSET_OUT=" + float_to_string_with_full_precision(oq_info.offset));
157*c217d954SCole Faust build_opts.add_option("-DSCALE_IN1=" + float_to_string_with_full_precision(iq_info.scale));
158*c217d954SCole Faust build_opts.add_option("-DSCALE_OUT=" + float_to_string_with_full_precision(oq_info.scale));
159*c217d954SCole Faust }
160*c217d954SCole Faust
161*c217d954SCole Faust // Set the initial value for the pooling operation accordingly with the data type
162*c217d954SCole Faust if(pool_type == PoolingType::MAX)
163*c217d954SCole Faust {
164*c217d954SCole Faust if(is_data_type_quantized(data_type))
165*c217d954SCole Faust {
166*c217d954SCole Faust PixelValue type_min{};
167*c217d954SCole Faust std::tie(type_min, std::ignore) = get_min_max(data_type);
168*c217d954SCole Faust build_opts.add_option("-DINITIAL_VALUE=" + support::cpp11::to_string(type_min.get<int32_t>()));
169*c217d954SCole Faust }
170*c217d954SCole Faust else
171*c217d954SCole Faust {
172*c217d954SCole Faust build_opts.add_option("-DINITIAL_VALUE=" + float_to_string_with_full_precision(std::numeric_limits<float>::lowest()));
173*c217d954SCole Faust }
174*c217d954SCole Faust }
175*c217d954SCole Faust else
176*c217d954SCole Faust {
177*c217d954SCole Faust // Pool AVG and Pool L2 initial value
178*c217d954SCole Faust build_opts.add_option("-DINITIAL_VALUE=0");
179*c217d954SCole Faust }
180*c217d954SCole Faust // Create kernel
181*c217d954SCole Faust // Floating point mixed precision is support on F16 only
182*c217d954SCole Faust const auto use_fp_mixed_precision = (data_type == DataType::F16) && pool_info.fp_mixed_precision && pool_type != PoolingType::MAX;
183*c217d954SCole Faust
184*c217d954SCole Faust // Wider accumulation is required to avoid accuracy loss
185*c217d954SCole Faust // Case 1: Floating point mixed precision (fp16 src data and fp32 accumulation)
186*c217d954SCole Faust DataType acc_data_type = data_type;
187*c217d954SCole Faust if(use_fp_mixed_precision)
188*c217d954SCole Faust {
189*c217d954SCole Faust acc_data_type = DataType::F32;
190*c217d954SCole Faust }
191*c217d954SCole Faust else if(is_data_type_quantized(data_type) && pool_type != PoolingType::MAX) // Use S32 for avg pooling to allow for integer division
192*c217d954SCole Faust {
193*c217d954SCole Faust acc_data_type = DataType::S32;
194*c217d954SCole Faust }
195*c217d954SCole Faust
196*c217d954SCole Faust build_opts.add_option("-DACC_DATA_TYPE=" + get_cl_type_from_data_type(acc_data_type));
197*c217d954SCole Faust build_opts.add_option_if(use_fp_mixed_precision, "-DFP_MIXED_PRECISION");
198*c217d954SCole Faust build_opts.add_option_if(exclude_padding, "-DEXCLUDE_PADDING");
199*c217d954SCole Faust build_opts.add_option("-DDST_HEIGHT=" + support::cpp11::to_string(dst->dimension(idx_height)));
200*c217d954SCole Faust build_opts.add_option("-DDST_DEPTH=" + support::cpp11::to_string(dst->dimension(idx_depth)));
201*c217d954SCole Faust build_opts.add_option("-DDST_CHANNELS=" + support::cpp11::to_string(dst->dimension(idx_channel)));
202*c217d954SCole Faust build_opts.add_option("-DDST_BATCH_SIZE=" + support::cpp11::to_string(dst->dimension(idx_batch_size)));
203*c217d954SCole Faust build_opts.add_option("-DVEC_SIZE_LEFTOVER=" + support::cpp11::to_string(src->dimension(0) % _num_elems_processed_per_iteration));
204*c217d954SCole Faust
205*c217d954SCole Faust // if datatype is quantized use quantized kernel function
206*c217d954SCole Faust std::string kernel_name = (is_data_type_quantized_asymmetric(data_type) ? "pooling_3d_layer_MxN_ndhwc_quantized" : "pooling_3d_layer_MxN_ndhwc");
207*c217d954SCole Faust _kernel = create_kernel(compile_context, kernel_name, build_opts.options());
208*c217d954SCole Faust
209*c217d954SCole Faust // Configure kernel window
210*c217d954SCole Faust Window win = calculate_max_window(*dst, Steps(_num_elems_processed_per_iteration));
211*c217d954SCole Faust ICLKernel::configure_internal(win);
212*c217d954SCole Faust
213*c217d954SCole Faust // Set config_id for enabling LWS tuning
214*c217d954SCole Faust _config_id = "pooling_layer_3d";
215*c217d954SCole Faust _config_id += lower_string(string_from_data_type(data_type));
216*c217d954SCole Faust _config_id += "_";
217*c217d954SCole Faust _config_id += lower_string(string_from_data_layout(_data_layout));
218*c217d954SCole Faust _config_id += "_";
219*c217d954SCole Faust _config_id += support::cpp11::to_string(dst->dimension(idx_width));
220*c217d954SCole Faust _config_id += "_";
221*c217d954SCole Faust _config_id += support::cpp11::to_string(dst->dimension(idx_height));
222*c217d954SCole Faust _config_id += "_";
223*c217d954SCole Faust _config_id += support::cpp11::to_string(dst->dimension(idx_channel));
224*c217d954SCole Faust _config_id += "_";
225*c217d954SCole Faust _config_id += lower_string(string_from_data_layout(src->data_layout()));
226*c217d954SCole Faust
227*c217d954SCole Faust ARM_COMPUTE_ERROR_ON(has_padding_changed(padding_info));
228*c217d954SCole Faust }
229*c217d954SCole Faust
validate(const ITensorInfo * src,const ITensorInfo * dst,const Pooling3dLayerInfo & pool_info)230*c217d954SCole Faust Status ClPool3dKernel::validate(const ITensorInfo *src, const ITensorInfo *dst, const Pooling3dLayerInfo &pool_info)
231*c217d954SCole Faust {
232*c217d954SCole Faust ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments(src, dst, pool_info));
233*c217d954SCole Faust return Status{};
234*c217d954SCole Faust }
235*c217d954SCole Faust
run_op(ITensorPack & tensors,const Window & window,cl::CommandQueue & queue)236*c217d954SCole Faust void ClPool3dKernel::run_op(ITensorPack &tensors, const Window &window, cl::CommandQueue &queue)
237*c217d954SCole Faust {
238*c217d954SCole Faust ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this);
239*c217d954SCole Faust ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(ICLKernel::window(), window);
240*c217d954SCole Faust
241*c217d954SCole Faust const auto src = utils::cast::polymorphic_downcast<const ICLTensor *>(tensors.get_const_tensor(TensorType::ACL_SRC));
242*c217d954SCole Faust auto dst = utils::cast::polymorphic_downcast<ICLTensor *>(tensors.get_tensor(TensorType::ACL_DST_0));
243*c217d954SCole Faust
244*c217d954SCole Faust // Collapse 3D window
245*c217d954SCole Faust Window window_collapsed = window.collapse_if_possible(ICLKernel::window(), Window::DimZ);
246*c217d954SCole Faust
247*c217d954SCole Faust // Set CL kernel arguments
248*c217d954SCole Faust unsigned int idx = 0;
249*c217d954SCole Faust // Passing of the window not needed, as the steps are not used for the pool3d kernel
250*c217d954SCole Faust add_5D_tensor_argument(idx, src, window);
251*c217d954SCole Faust add_5D_tensor_argument(idx, dst, window);
252*c217d954SCole Faust enqueue(queue, *this, window_collapsed, lws_hint());
253*c217d954SCole Faust }
254*c217d954SCole Faust } // namespace kernels
255*c217d954SCole Faust } // namespace opencl
256*c217d954SCole Faust } // namespace arm_compute
257