blob: a08c5d4be71783e026ddfd92458353b807f9409e [file] [log] [blame]
ramelg0137515692022-02-26 22:06:20 +00001/*
Matthew Bentham314d3e22023-06-23 10:53:52 +00002 * Copyright (c) 2022-2023 Arm Limited.
ramelg0137515692022-02-26 22:06:20 +00003 *
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 */
24#include "src/gpu/cl/kernels/ClPool3dKernel.h"
25
26#include "arm_compute/core/CL/ICLTensor.h"
27#include "arm_compute/core/TensorInfo.h"
Matthew Bentham314d3e22023-06-23 10:53:52 +000028#include "arm_compute/core/utils/helpers/AdjustVecSize.h"
ramelg0137515692022-02-26 22:06:20 +000029#include "arm_compute/core/utils/misc/ShapeCalculator.h"
Matthew Bentham314d3e22023-06-23 10:53:52 +000030#include "arm_compute/core/utils/StringUtils.h"
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010031
ramelg0137515692022-02-26 22:06:20 +000032#include "src/core/CL/CLValidate.h"
33#include "src/core/helpers/AutoConfiguration.h"
34#include "src/core/helpers/WindowHelpers.h"
35#include "support/Cast.h"
36#include "utils/TypePrinter.h"
37
38namespace arm_compute
39{
40namespace opencl
41{
42namespace kernels
43{
44using namespace arm_compute::misc::shape_calculator;
45
46namespace
47{
48Status validate_arguments(const ITensorInfo *src, const ITensorInfo *dst, const Pooling3dLayerInfo &pool_info)
49{
50 ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(src, dst);
51 ARM_COMPUTE_RETURN_ERROR_ON_MSG(src->data_layout() != DataLayout::NDHWC, "Only NDHWC layout supported");
52
53 ARM_COMPUTE_RETURN_ERROR_ON_F16_UNSUPPORTED(src);
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010054 ARM_COMPUTE_RETURN_ERROR_ON_MSG(
55 (pool_info.stride.x() == 0 || pool_info.stride.y() == 0 || pool_info.stride.z() == 0),
56 "Strides cannot be zero.");
57 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(src, 1, DataType::F16, DataType::F32, DataType::QASYMM8_SIGNED,
58 DataType::QASYMM8);
59 ARM_COMPUTE_RETURN_ERROR_ON_MSG((!is_data_type_float(src->data_type())) &&
60 (!pool_info.exclude_padding && (pool_info.pool_type == PoolingType::AVG)),
Mohammed Suhail Munshi5e549fa2022-03-16 11:14:06 +000061 "Exclude padding is unsupported for non-float types for Avg op");
ramelg0137515692022-02-26 22:06:20 +000062
Mohammed Suhail Munshi5e549fa2022-03-16 11:14:06 +000063 const auto data_layout = src->data_layout();
64 const int idx_width = get_data_layout_dimension_index(data_layout, DataLayoutDimension::WIDTH);
65 const int idx_height = get_data_layout_dimension_index(data_layout, DataLayoutDimension::HEIGHT);
66 const int idx_depth = get_data_layout_dimension_index(data_layout, DataLayoutDimension::DEPTH);
67 const bool is_global_pooling = pool_info.is_global_pooling;
ramelg0137515692022-02-26 22:06:20 +000068 const unsigned int pool_size_x = is_global_pooling ? src->dimension(idx_width) : pool_info.pool_size.width;
69 const unsigned int pool_size_y = is_global_pooling ? src->dimension(idx_height) : pool_info.pool_size.height;
70 const unsigned int pool_size_z = is_global_pooling ? src->dimension(idx_depth) : pool_info.pool_size.depth;
Mohammed Suhail Munshi5e549fa2022-03-16 11:14:06 +000071 int output_width = 0;
72 int output_height = 0;
73 int output_depth = 0;
ramelg0137515692022-02-26 22:06:20 +000074
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010075 bool round_type_ceil_with_asymm_padding =
76 (pool_info.round_type == DimensionRoundingType::CEIL) && (!is_symmetric(pool_info.padding));
77 ARM_COMPUTE_RETURN_ERROR_ON_MSG(round_type_ceil_with_asymm_padding,
78 "Cannot use dimension round type CEIL when padding is asymmetric.");
ramelg0137515692022-02-26 22:06:20 +000079
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010080 ARM_COMPUTE_RETURN_ERROR_ON_MSG(is_pool_3d_region_entirely_outside_input(pool_info),
81 "Pooling region that is entirely outside input tensor is unsupported");
82 std::tie(output_width, output_height, output_depth) =
83 scaled_3d_dimensions_signed(src->tensor_shape()[idx_width], src->tensor_shape()[idx_height],
84 src->tensor_shape()[idx_depth], pool_size_x, pool_size_y, pool_size_z, pool_info);
ramelg0137515692022-02-26 22:06:20 +000085
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010086 ARM_COMPUTE_RETURN_ERROR_ON_MSG((output_width < 1 || output_height < 1 || output_depth < 1),
87 "Calculated output dimension size is invalid");
ramelg0137515692022-02-26 22:06:20 +000088 // Checks performed when dst is configured
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010089 if (dst->total_size() != 0)
ramelg0137515692022-02-26 22:06:20 +000090 {
91 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(src, dst);
92 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_LAYOUT(src, dst);
93 TensorInfo out_info(TensorInfo(compute_pool3d_shape(src->tensor_shape(), pool_info), 1, dst->data_type()));
94 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_SHAPES(dst, &out_info);
95 }
96
97 return Status{};
98}
99} // namespace
100
101ClPool3dKernel::ClPool3dKernel()
102{
103 _type = CLKernelType::POOL;
104}
105
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100106void ClPool3dKernel::configure(const ClCompileContext &compile_context,
107 const ITensorInfo *src,
108 ITensorInfo *dst,
109 const Pooling3dLayerInfo &pool_info)
ramelg0137515692022-02-26 22:06:20 +0000110{
111 ARM_COMPUTE_ERROR_ON_NULLPTR(src, dst);
112 ARM_COMPUTE_ERROR_THROW_ON(validate_arguments(src, dst, pool_info));
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100113 auto padding_info = get_padding_info({src, dst});
ramelg0137515692022-02-26 22:06:20 +0000114
115 // Auto init if empty
116 TensorShape out_shape = compute_pool3d_shape(src->tensor_shape(), pool_info);
117 auto_init_if_empty(*dst, src->clone()->set_tensor_shape(out_shape));
118
119 // Set instance variables
120 _pool_info = pool_info;
121 _data_layout = src->data_layout();
122
123 _num_elems_processed_per_iteration = (dst->data_type() == DataType::F32) ? 2 : 4;
124 _num_elems_processed_per_iteration = adjust_vec_size(_num_elems_processed_per_iteration, dst->dimension(0));
125
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100126 const PoolingType pool_type = pool_info.pool_type;
127 const int idx_width = get_data_layout_dimension_index(_data_layout, DataLayoutDimension::WIDTH);
128 const int idx_height = get_data_layout_dimension_index(_data_layout, DataLayoutDimension::HEIGHT);
129 const int idx_depth = get_data_layout_dimension_index(_data_layout, DataLayoutDimension::DEPTH);
130 const int idx_channel = get_data_layout_dimension_index(_data_layout, DataLayoutDimension::CHANNEL);
131 const int idx_batch_size = get_data_layout_dimension_index(_data_layout, DataLayoutDimension::BATCHES);
132 const int pool_size_x = pool_info.is_global_pooling ? src->dimension(idx_width) : pool_info.pool_size.width;
133 const int pool_size_y = pool_info.is_global_pooling ? src->dimension(idx_height) : pool_info.pool_size.height;
134 const int pool_size_z = pool_info.is_global_pooling ? src->dimension(idx_depth) : pool_info.pool_size.depth;
135 const bool exclude_padding = pool_info.exclude_padding;
136 const int pool_stride_x = pool_info.stride.x();
137 const int pool_stride_y = pool_info.stride.y();
138 const int pool_stride_z = pool_info.stride.z();
139 const int pool_pad_top = pool_info.padding.top;
140 const int pool_pad_left = pool_info.padding.left;
141 const int pool_pad_front = pool_info.padding.front;
142 const DataType data_type = src->data_type();
ramelg0137515692022-02-26 22:06:20 +0000143
144 // Set build options
145 CLBuildOptions build_opts;
146 build_opts.add_option("-DVEC_SIZE=" + support::cpp11::to_string(_num_elems_processed_per_iteration));
147 build_opts.add_option("-DDATA_TYPE=" + get_cl_type_from_data_type(data_type));
148 build_opts.add_option("-DPOOL_" + string_from_pooling_type(pool_type));
149 build_opts.add_option("-DSTRIDE_X=" + support::cpp11::to_string(pool_stride_x));
150 build_opts.add_option("-DSTRIDE_Y=" + support::cpp11::to_string(pool_stride_y));
151 build_opts.add_option("-DSTRIDE_Z=" + support::cpp11::to_string(pool_stride_z));
152 build_opts.add_option("-DPAD_X=" + support::cpp11::to_string(pool_pad_left));
153 build_opts.add_option("-DPAD_Y=" + support::cpp11::to_string(pool_pad_top));
154 build_opts.add_option("-DPAD_Z=" + support::cpp11::to_string(pool_pad_front));
155 build_opts.add_option("-DPOOL_SIZE_X=" + support::cpp11::to_string(pool_size_x));
156 build_opts.add_option("-DPOOL_SIZE_Y=" + support::cpp11::to_string(pool_size_y));
157 build_opts.add_option("-DPOOL_SIZE_Z=" + support::cpp11::to_string(pool_size_z));
158 build_opts.add_option("-DSRC_WIDTH=" + support::cpp11::to_string(src->dimension(idx_width)));
159 build_opts.add_option("-DSRC_HEIGHT=" + support::cpp11::to_string(src->dimension(idx_height)));
160 build_opts.add_option("-DSRC_DEPTH=" + support::cpp11::to_string(src->dimension(idx_depth)));
161
Mohammed Suhail Munshi5e549fa2022-03-16 11:14:06 +0000162 // If datatype is quantized add relevant parameters
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100163 if (is_data_type_quantized_asymmetric(data_type) && src->quantization_info() != dst->quantization_info())
Mohammed Suhail Munshi5e549fa2022-03-16 11:14:06 +0000164 {
165 const UniformQuantizationInfo iq_info = src->quantization_info().uniform();
166 const UniformQuantizationInfo oq_info = dst->quantization_info().uniform();
167
168 build_opts.add_option("-DOFFSET_IN1=" + float_to_string_with_full_precision(iq_info.offset));
169 build_opts.add_option("-DOFFSET_OUT=" + float_to_string_with_full_precision(oq_info.offset));
170 build_opts.add_option("-DSCALE_IN1=" + float_to_string_with_full_precision(iq_info.scale));
171 build_opts.add_option("-DSCALE_OUT=" + float_to_string_with_full_precision(oq_info.scale));
172 }
173
ramelg0137515692022-02-26 22:06:20 +0000174 // Set the initial value for the pooling operation accordingly with the data type
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100175 if (pool_type == PoolingType::MAX)
ramelg0137515692022-02-26 22:06:20 +0000176 {
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100177 if (is_data_type_quantized(data_type))
Mohammed Suhail Munshi5e549fa2022-03-16 11:14:06 +0000178 {
179 PixelValue type_min{};
180 std::tie(type_min, std::ignore) = get_min_max(data_type);
181 build_opts.add_option("-DINITIAL_VALUE=" + support::cpp11::to_string(type_min.get<int32_t>()));
182 }
183 else
184 {
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100185 build_opts.add_option("-DINITIAL_VALUE=" +
186 float_to_string_with_full_precision(std::numeric_limits<float>::lowest()));
Mohammed Suhail Munshi5e549fa2022-03-16 11:14:06 +0000187 }
ramelg0137515692022-02-26 22:06:20 +0000188 }
189 else
190 {
191 // Pool AVG and Pool L2 initial value
192 build_opts.add_option("-DINITIAL_VALUE=0");
193 }
194 // Create kernel
195 // Floating point mixed precision is support on F16 only
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100196 const auto use_fp_mixed_precision =
197 (data_type == DataType::F16) && pool_info.fp_mixed_precision && pool_type != PoolingType::MAX;
ramelg0137515692022-02-26 22:06:20 +0000198
199 // Wider accumulation is required to avoid accuracy loss
200 // Case 1: Floating point mixed precision (fp16 src data and fp32 accumulation)
201 DataType acc_data_type = data_type;
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100202 if (use_fp_mixed_precision)
ramelg0137515692022-02-26 22:06:20 +0000203 {
204 acc_data_type = DataType::F32;
205 }
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100206 else if (is_data_type_quantized(data_type) &&
207 pool_type != PoolingType::MAX) // Use S32 for avg pooling to allow for integer division
Mohammed Suhail Munshi5e549fa2022-03-16 11:14:06 +0000208 {
209 acc_data_type = DataType::S32;
210 }
211
ramelg0137515692022-02-26 22:06:20 +0000212 build_opts.add_option("-DACC_DATA_TYPE=" + get_cl_type_from_data_type(acc_data_type));
213 build_opts.add_option_if(use_fp_mixed_precision, "-DFP_MIXED_PRECISION");
214 build_opts.add_option_if(exclude_padding, "-DEXCLUDE_PADDING");
215 build_opts.add_option("-DDST_HEIGHT=" + support::cpp11::to_string(dst->dimension(idx_height)));
216 build_opts.add_option("-DDST_DEPTH=" + support::cpp11::to_string(dst->dimension(idx_depth)));
217 build_opts.add_option("-DDST_CHANNELS=" + support::cpp11::to_string(dst->dimension(idx_channel)));
218 build_opts.add_option("-DDST_BATCH_SIZE=" + support::cpp11::to_string(dst->dimension(idx_batch_size)));
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100219 build_opts.add_option("-DVEC_SIZE_LEFTOVER=" +
220 support::cpp11::to_string(src->dimension(0) % _num_elems_processed_per_iteration));
Mohammed Suhail Munshi5e549fa2022-03-16 11:14:06 +0000221
222 // if datatype is quantized use quantized kernel function
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100223 std::string kernel_name = (is_data_type_quantized_asymmetric(data_type) ? "pooling_3d_layer_MxN_ndhwc_quantized"
224 : "pooling_3d_layer_MxN_ndhwc");
225 _kernel = create_kernel(compile_context, kernel_name, build_opts.options());
ramelg0137515692022-02-26 22:06:20 +0000226
227 // Configure kernel window
228 Window win = calculate_max_window(*dst, Steps(_num_elems_processed_per_iteration));
229 ICLKernel::configure_internal(win);
230
231 // Set config_id for enabling LWS tuning
232 _config_id = "pooling_layer_3d";
233 _config_id += lower_string(string_from_data_type(data_type));
234 _config_id += "_";
235 _config_id += lower_string(string_from_data_layout(_data_layout));
236 _config_id += "_";
237 _config_id += support::cpp11::to_string(dst->dimension(idx_width));
238 _config_id += "_";
239 _config_id += support::cpp11::to_string(dst->dimension(idx_height));
240 _config_id += "_";
241 _config_id += support::cpp11::to_string(dst->dimension(idx_channel));
242 _config_id += "_";
243 _config_id += lower_string(string_from_data_layout(src->data_layout()));
244
245 ARM_COMPUTE_ERROR_ON(has_padding_changed(padding_info));
246}
247
248Status ClPool3dKernel::validate(const ITensorInfo *src, const ITensorInfo *dst, const Pooling3dLayerInfo &pool_info)
249{
250 ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments(src, dst, pool_info));
251 return Status{};
252}
253
254void ClPool3dKernel::run_op(ITensorPack &tensors, const Window &window, cl::CommandQueue &queue)
255{
256 ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this);
257 ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(ICLKernel::window(), window);
258
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100259 const auto src =
260 utils::cast::polymorphic_downcast<const ICLTensor *>(tensors.get_const_tensor(TensorType::ACL_SRC));
261 auto dst = utils::cast::polymorphic_downcast<ICLTensor *>(tensors.get_tensor(TensorType::ACL_DST_0));
ramelg0137515692022-02-26 22:06:20 +0000262
263 // Collapse 3D window
264 Window window_collapsed = window.collapse_if_possible(ICLKernel::window(), Window::DimZ);
265
266 // Set CL kernel arguments
267 unsigned int idx = 0;
268 // Passing of the window not needed, as the steps are not used for the pool3d kernel
269 add_5D_tensor_argument(idx, src, window);
270 add_5D_tensor_argument(idx, dst, window);
271 enqueue(queue, *this, window_collapsed, lws_hint());
272}
273} // namespace kernels
274} // namespace opencl
275} // namespace arm_compute