blob: e436c46d2980755ba58da9f333ef64bdbb2b9686 [file] [log] [blame]
Anthony Barbier6ff3b192017-09-04 18:44:23 +01001/*
2 * Copyright (c) 2017 ARM Limited.
3 *
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 "arm_compute/core/CL/kernels/CLPoolingLayerKernel.h"
25
26#include "arm_compute/core/AccessWindowStatic.h"
27#include "arm_compute/core/CL/CLHelpers.h"
28#include "arm_compute/core/CL/CLKernelLibrary.h"
Anton Lokhmotovaf6204c2017-11-08 09:34:19 +000029#include "arm_compute/core/CL/ICLKernel.h"
Anthony Barbier6ff3b192017-09-04 18:44:23 +010030#include "arm_compute/core/CL/ICLTensor.h"
31#include "arm_compute/core/CL/OpenCL.h"
32#include "arm_compute/core/Helpers.h"
33#include "arm_compute/core/TensorInfo.h"
34#include "arm_compute/core/Utils.h"
35#include "arm_compute/core/Validate.h"
36#include "arm_compute/core/Window.h"
37
38#include <set>
39#include <string>
40#include <tuple>
41
42using namespace arm_compute;
43
44CLPoolingLayerKernel::CLPoolingLayerKernel()
Gian Marco Iodicecb292832017-08-02 13:19:48 +010045 : _input(nullptr), _output(nullptr), _pool_info(), _border_size(0), _num_elems_processed_per_iteration(1)
Anthony Barbier6ff3b192017-09-04 18:44:23 +010046{
47}
48
49BorderSize CLPoolingLayerKernel::border_size() const
50{
51 return _border_size;
52}
53
54void CLPoolingLayerKernel::configure(const ICLTensor *input, ICLTensor *output, const PoolingLayerInfo &pool_info)
55{
Georgios Pinitas4c2dd542017-11-13 12:58:41 +000056 int pool_pad_x = 0;
57 int pool_pad_y = 0;
58 int pool_stride_x = 0;
59 int pool_stride_y = 0;
60 unsigned int pooled_w = 0;
61 unsigned int pooled_h = 0;
62 const PoolingType pool_type = pool_info.pool_type();
63 int pool_size = pool_info.pool_size();
64 const PadStrideInfo pad_stride_info = pool_info.pad_stride_info();
65 const bool exclude_padding = pool_info.exclude_padding();
66 const bool is_global_pooling = pool_info.is_global_pooling();
Anthony Barbier6ff3b192017-09-04 18:44:23 +010067 std::tie(pool_pad_x, pool_pad_y) = pad_stride_info.pad();
68 std::tie(pool_stride_x, pool_stride_y) = pad_stride_info.stride();
69
Georgios Pinitasf9d3a0a2017-11-03 19:01:44 +000070 ARM_COMPUTE_ERROR_ON_NULLPTR(input, output);
Anthony Barbier6ff3b192017-09-04 18:44:23 +010071
Georgios Pinitas4c2dd542017-11-13 12:58:41 +000072 // Update pool size in case of global pooling
73 pool_size = is_global_pooling ? input->info()->dimension(0) : pool_size;
74
Anthony Barbier6ff3b192017-09-04 18:44:23 +010075 // Check output dimensions
76 std::tie(pooled_w, pooled_h) = scaled_dimensions(input->info()->dimension(0),
77 input->info()->dimension(1),
78 pool_size,
Gian Marco Iodice4e288692017-06-27 11:41:59 +010079 pool_size,
80 pool_info.pad_stride_info());
Georgios Pinitas1dad50e2017-07-03 17:51:34 +010081
82 // Output auto initialization if not yet initialized
83 {
84 TensorShape output_shape{ input->info()->tensor_shape() };
85 output_shape.set(0, pooled_w);
86 output_shape.set(1, pooled_h);
87
Anton Lokhmotovaf6204c2017-11-08 09:34:19 +000088 auto_init_if_empty(*output->info(),
89 output_shape,
90 1,
91 input->info()->data_type(),
92 input->info()->fixed_point_position(),
93 input->info()->quantization_info());
Georgios Pinitas1dad50e2017-07-03 17:51:34 +010094 }
95
Georgios Pinitasf9d3a0a2017-11-03 19:01:44 +000096 ARM_COMPUTE_ERROR_THROW_ON(CLPoolingLayerKernel::validate(input->info(), output->info(), pool_info));
Anthony Barbier6ff3b192017-09-04 18:44:23 +010097
Gian Marco Iodicebf179552017-09-05 13:51:21 +010098 const int input_width = input->info()->dimension(0);
99 const int input_height = input->info()->dimension(1);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100100
101 // Set instance variables
Gian Marco Iodicebf179552017-09-05 13:51:21 +0100102 _input = input;
103 _output = output;
104 _pool_info = pool_info;
105 _border_size = BorderSize(pool_pad_y, pool_pad_x);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100106
Anton Lokhmotovaf6204c2017-11-08 09:34:19 +0000107 const GPUTarget gpu_target = get_arch_from_target(get_target());
108 const DataType data_type = input->info()->data_type();
steniu010c7614f2017-06-23 17:00:26 +0100109
Anton Lokhmotovaf6204c2017-11-08 09:34:19 +0000110 // Set build options
111 CLBuildOptions build_opts;
112 build_opts.add_option("-DDATA_TYPE=" + get_cl_type_from_data_type(data_type));
113 build_opts.add_option("-DPOOL_" + string_from_pooling_type(pool_type));
114 build_opts.add_option_if(is_data_type_fixed_point(data_type),
115 "-DFIXED_POINT_POSITION=" + support::cpp11::to_string(input->info()->fixed_point_position()));
116 build_opts.add_option("-DSTRIDE_X=" + support::cpp11::to_string(pool_stride_x));
Georgios Pinitascdf51452017-08-31 14:21:36 +0100117 if(pool_type != PoolingType::MAX)
Gian Marco Iodicecb292832017-08-02 13:19:48 +0100118 {
Anton Lokhmotovaf6204c2017-11-08 09:34:19 +0000119 build_opts.add_option_if(exclude_padding, "-DEXCLUDE_PADDING");
120 build_opts.add_option("-DMAX_WIDTH=" + support::cpp11::to_string(input->info()->dimension(0) + (exclude_padding ? 0 : pool_pad_x)));
121 build_opts.add_option("-DMAX_HEIGHT=" + support::cpp11::to_string(input->info()->dimension(1) + (exclude_padding ? 0 : pool_pad_y)));
122 build_opts.add_option("-DSTRIDE_Y=" + support::cpp11::to_string(pool_stride_y));
123 build_opts.add_option("-DPAD_X=" + support::cpp11::to_string(pool_pad_x));
124 build_opts.add_option("-DPAD_Y=" + support::cpp11::to_string(pool_pad_y));
Gian Marco Iodicecb292832017-08-02 13:19:48 +0100125 }
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100126
127 // Create kernel
Anton Lokhmotovaf6204c2017-11-08 09:34:19 +0000128 if((pool_size == 3) && !is_data_type_quantized_asymmetric(data_type))
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100129 {
Gian Marco Iodicebf179552017-09-05 13:51:21 +0100130 // Check if we have pool3x3 with stride_x less equal than 3. In these cases, run an optimized OpenCL kernel where
131 // each thread computes 4 output elements
Anton Lokhmotovaf6204c2017-11-08 09:34:19 +0000132 const bool is_pool3x3_stride_le3 = (pool_size == 3) && (pool_stride_x <= 3) && !is_data_type_fixed_point(data_type);
Gian Marco Iodicebf179552017-09-05 13:51:21 +0100133
Anton Lokhmotovaf6204c2017-11-08 09:34:19 +0000134 int num_elems_read_per_iteration = pool_size;
Gian Marco Iodicebf179552017-09-05 13:51:21 +0100135 if(is_pool3x3_stride_le3)
136 {
Anton Lokhmotovaf6204c2017-11-08 09:34:19 +0000137 // Change the number of elements processed and the number of elements read per iteration
138 // for pooling 3x3 with stride less equal than 3
Gian Marco Iodicebf179552017-09-05 13:51:21 +0100139 _num_elems_processed_per_iteration = 4;
Anton Lokhmotovaf6204c2017-11-08 09:34:19 +0000140 num_elems_read_per_iteration = pool_size * (pool_stride_x + 1);
Gian Marco Iodicebf179552017-09-05 13:51:21 +0100141 }
142
Anton Lokhmotovaf6204c2017-11-08 09:34:19 +0000143 const int upper_bound_w = ((pooled_w - 1) * pool_stride_x - pool_pad_x + num_elems_read_per_iteration) - input_width;
Gian Marco Iodicebf179552017-09-05 13:51:21 +0100144 const int upper_bound_h = ((pooled_h - 1) * pool_stride_y - pool_pad_y + pool_size) - input_height;
145
146 _border_size.right = std::max(upper_bound_w, pool_pad_x);
147 _border_size.bottom = std::max(upper_bound_h, pool_pad_y);
148
Anton Lokhmotovaf6204c2017-11-08 09:34:19 +0000149 std::string kernel_name = ((is_pool3x3_stride_le3) ? "pooling_layer_optimized_" : "pooling_layer_")
150 + support::cpp11::to_string(pool_size);
151 _kernel = static_cast<cl::Kernel>(CLKernelLibrary::get().create_kernel(kernel_name, build_opts.options()));
Gian Marco Iodicecb292832017-08-02 13:19:48 +0100152 }
Gian Marco Iodicebf179552017-09-05 13:51:21 +0100153 else // Run general case
Gian Marco Iodicecb292832017-08-02 13:19:48 +0100154 {
Gian Marco Iodicebf179552017-09-05 13:51:21 +0100155 const int upper_bound_w = ((pooled_w - 1) * pool_stride_x - pool_pad_x + pool_size) - input_width;
156 const int upper_bound_h = ((pooled_h - 1) * pool_stride_y - pool_pad_y + pool_size) - input_height;
157
158 _border_size.right = std::max(upper_bound_w, pool_pad_x);
159 _border_size.bottom = std::max(upper_bound_h, pool_pad_y);
160
Anton Lokhmotovaf6204c2017-11-08 09:34:19 +0000161 build_opts.add_option("-DPOOL_SIZE=" + support::cpp11::to_string(pool_size));
162 build_opts.add_option_if(data_type == DataType::F16, "-DFP16");
163
164 std::string kernel_name = is_data_type_quantized_asymmetric(data_type) ? "pooling_layer_N_quantized" : "pooling_layer_N";
165 _kernel = static_cast<cl::Kernel>(CLKernelLibrary::get().create_kernel(kernel_name, build_opts.options()));
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100166 }
167
168 // Configure kernel window
Anton Lokhmotovaf6204c2017-11-08 09:34:19 +0000169 Window win = calculate_max_window(*output->info(), Steps(_num_elems_processed_per_iteration));
170
171 // Configure the local work size (hint) from the first two dimensions of the global work size.
172 // On Bifrost, this works for up to 35x35xC filters, for which the pooling_layer_3_optimized
173 // kernel is launched with gws=(9, 33, C). In any case, the hint will be ignored if it is
174 // invalid (e.g. exceeds the maximum workgroup size that the kernel can be launched with).
175 if(gpu_target == GPUTarget::BIFROST)
176 {
177 cl::NDRange gws = ICLKernel::gws_from_window(win);
178 _lws_hint = cl::NDRange(gws[0], gws[1], 1);
179 }
180
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100181 AccessWindowStatic input_access(input->info(), -pool_pad_x, -pool_pad_y, input_width + _border_size.right, input_height + _border_size.bottom);
Gian Marco Iodicecb292832017-08-02 13:19:48 +0100182 AccessWindowHorizontal output_access(output->info(), 0, _num_elems_processed_per_iteration);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100183 update_window_and_padding(win, input_access, output_access);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100184 output_access.set_valid_region(win, ValidRegion(Coordinates(), output->info()->tensor_shape()));
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100185 ICLKernel::configure(win);
186}
187
Georgios Pinitas3faea252017-10-30 14:13:50 +0000188Error CLPoolingLayerKernel::validate(const ITensorInfo *input, const ITensorInfo *output, const PoolingLayerInfo &pool_info)
189{
190 ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(input, output);
Anton Lokhmotovaf6204c2017-11-08 09:34:19 +0000191 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::QASYMM8, DataType::QS8, DataType::QS16, DataType::F16, DataType::F32);
192 ARM_COMPUTE_RETURN_ERROR_ON_MSG((is_data_type_quantized_asymmetric(input->data_type()) && pool_info.pool_type() == PoolingType::L2),
193 "Unsupported combination of parameters!");
Georgios Pinitas3faea252017-10-30 14:13:50 +0000194
Georgios Pinitas4c2dd542017-11-13 12:58:41 +0000195 const bool is_global_pooling = pool_info.is_global_pooling();
196 const unsigned int pool_size = is_global_pooling ? input->tensor_shape().x() : pool_info.pool_size();
197
198 ARM_COMPUTE_RETURN_ERROR_ON_MSG(is_global_pooling && (input->tensor_shape().x() != input->tensor_shape().y()),
199 "Global pooling is supported only with rectangular inputs!");
200 ARM_COMPUTE_RETURN_ERROR_ON_MSG(!is_global_pooling && ((pool_info.pad_stride_info().pad().first >= pool_size) || (pool_info.pad_stride_info().pad().second >= pool_size)),
Anton Lokhmotovaf6204c2017-11-08 09:34:19 +0000201 "Invalid pool size and pool pad combination!");
Georgios Pinitas3faea252017-10-30 14:13:50 +0000202
203 // Checks performed when output is configured
204 if(output->total_size() != 0)
205 {
206 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input, output);
207 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_FIXED_POINT(input, output);
208
209 unsigned int pooled_w = 0;
210 unsigned int pooled_h = 0;
211 std::tie(pooled_w, pooled_h) = scaled_dimensions(input->dimension(0),
212 input->dimension(1),
213 pool_size,
214 pool_size,
215 pool_info.pad_stride_info());
Georgios Pinitas4c2dd542017-11-13 12:58:41 +0000216 ARM_COMPUTE_RETURN_ERROR_ON_MSG((output->dimension(0) != pooled_w) || (output->dimension(1) != pooled_h),
Georgios Pinitas3faea252017-10-30 14:13:50 +0000217 "Invalid output pooling dimensions!");
218 }
219
220 return Error{};
221}
222
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100223void CLPoolingLayerKernel::run(const Window &window, cl::CommandQueue &queue)
224{
225 ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this);
226 ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(IKernel::window(), window);
227
228 unsigned int pool_pad_x, pool_pad_y, pool_stride_x, pool_stride_y = 0;
229 std::tie(pool_pad_x, pool_pad_y) = _pool_info.pad_stride_info().pad();
230 std::tie(pool_stride_x, pool_stride_y) = _pool_info.pad_stride_info().stride();
231
steniu01f70256b2017-07-13 14:03:35 +0100232 Window window_collapsed = window.collapse_if_possible(ICLKernel::window(), Window::DimZ);
233 Window slice = window_collapsed.first_slice_window_3D();
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100234
235 do
236 {
237 // Upsample input by pool size
238 Window in_slice(slice);
Gian Marco Iodicecb292832017-08-02 13:19:48 +0100239 in_slice.set(Window::DimX, Window::Dimension(in_slice.x().start() - pool_pad_x, in_slice.x().end() * pool_stride_x, pool_stride_x * _num_elems_processed_per_iteration));
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100240 in_slice.set(Window::DimY, Window::Dimension(in_slice.y().start() - pool_pad_y, in_slice.y().end() * pool_stride_y, pool_stride_y));
241
242 // Set inputs
243 unsigned int idx = 0;
244 add_3D_tensor_argument(idx, _input, in_slice);
245 add_3D_tensor_argument(idx, _output, slice);
Anton Lokhmotovaf6204c2017-11-08 09:34:19 +0000246 enqueue(queue, *this, slice, _lws_hint);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100247 }
steniu01f70256b2017-07-13 14:03:35 +0100248 while(window_collapsed.slide_window_slice_3D(slice));
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100249}