blob: 1317278fb56c87e3c59597046b029c2923f77ae1 [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{
Gian Marco Iodice4e288692017-06-27 11:41:59 +010056 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 const int pool_size = pool_info.pool_size();
64 const PadStrideInfo pad_stride_info = pool_info.pad_stride_info();
Georgios Pinitasadaae7e2017-10-30 15:56:32 +000065 bool exclude_padding = pool_info.exclude_padding();
Anthony Barbier6ff3b192017-09-04 18:44:23 +010066 std::tie(pool_pad_x, pool_pad_y) = pad_stride_info.pad();
67 std::tie(pool_stride_x, pool_stride_y) = pad_stride_info.stride();
68
Georgios Pinitasf9d3a0a2017-11-03 19:01:44 +000069 ARM_COMPUTE_ERROR_ON_NULLPTR(input, output);
Anthony Barbier6ff3b192017-09-04 18:44:23 +010070
71 // Check output dimensions
72 std::tie(pooled_w, pooled_h) = scaled_dimensions(input->info()->dimension(0),
73 input->info()->dimension(1),
74 pool_size,
Gian Marco Iodice4e288692017-06-27 11:41:59 +010075 pool_size,
76 pool_info.pad_stride_info());
Georgios Pinitas1dad50e2017-07-03 17:51:34 +010077
78 // Output auto initialization if not yet initialized
79 {
80 TensorShape output_shape{ input->info()->tensor_shape() };
81 output_shape.set(0, pooled_w);
82 output_shape.set(1, pooled_h);
83
Anton Lokhmotovaf6204c2017-11-08 09:34:19 +000084 auto_init_if_empty(*output->info(),
85 output_shape,
86 1,
87 input->info()->data_type(),
88 input->info()->fixed_point_position(),
89 input->info()->quantization_info());
Georgios Pinitas1dad50e2017-07-03 17:51:34 +010090 }
91
Georgios Pinitasf9d3a0a2017-11-03 19:01:44 +000092 ARM_COMPUTE_ERROR_THROW_ON(CLPoolingLayerKernel::validate(input->info(), output->info(), pool_info));
Anthony Barbier6ff3b192017-09-04 18:44:23 +010093
Gian Marco Iodicebf179552017-09-05 13:51:21 +010094 const int input_width = input->info()->dimension(0);
95 const int input_height = input->info()->dimension(1);
Anthony Barbier6ff3b192017-09-04 18:44:23 +010096
97 // Set instance variables
Gian Marco Iodicebf179552017-09-05 13:51:21 +010098 _input = input;
99 _output = output;
100 _pool_info = pool_info;
101 _border_size = BorderSize(pool_pad_y, pool_pad_x);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100102
Anton Lokhmotovaf6204c2017-11-08 09:34:19 +0000103 const GPUTarget gpu_target = get_arch_from_target(get_target());
104 const DataType data_type = input->info()->data_type();
steniu010c7614f2017-06-23 17:00:26 +0100105
Anton Lokhmotovaf6204c2017-11-08 09:34:19 +0000106 // Set build options
107 CLBuildOptions build_opts;
108 build_opts.add_option("-DDATA_TYPE=" + get_cl_type_from_data_type(data_type));
109 build_opts.add_option("-DPOOL_" + string_from_pooling_type(pool_type));
110 build_opts.add_option_if(is_data_type_fixed_point(data_type),
111 "-DFIXED_POINT_POSITION=" + support::cpp11::to_string(input->info()->fixed_point_position()));
112 build_opts.add_option("-DSTRIDE_X=" + support::cpp11::to_string(pool_stride_x));
Georgios Pinitascdf51452017-08-31 14:21:36 +0100113 if(pool_type != PoolingType::MAX)
Gian Marco Iodicecb292832017-08-02 13:19:48 +0100114 {
Anton Lokhmotovaf6204c2017-11-08 09:34:19 +0000115 build_opts.add_option_if(exclude_padding, "-DEXCLUDE_PADDING");
116 build_opts.add_option("-DMAX_WIDTH=" + support::cpp11::to_string(input->info()->dimension(0) + (exclude_padding ? 0 : pool_pad_x)));
117 build_opts.add_option("-DMAX_HEIGHT=" + support::cpp11::to_string(input->info()->dimension(1) + (exclude_padding ? 0 : pool_pad_y)));
118 build_opts.add_option("-DSTRIDE_Y=" + support::cpp11::to_string(pool_stride_y));
119 build_opts.add_option("-DPAD_X=" + support::cpp11::to_string(pool_pad_x));
120 build_opts.add_option("-DPAD_Y=" + support::cpp11::to_string(pool_pad_y));
Gian Marco Iodicecb292832017-08-02 13:19:48 +0100121 }
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100122
123 // Create kernel
Anton Lokhmotovaf6204c2017-11-08 09:34:19 +0000124 if((pool_size == 3) && !is_data_type_quantized_asymmetric(data_type))
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100125 {
Gian Marco Iodicebf179552017-09-05 13:51:21 +0100126 // Check if we have pool3x3 with stride_x less equal than 3. In these cases, run an optimized OpenCL kernel where
127 // each thread computes 4 output elements
Anton Lokhmotovaf6204c2017-11-08 09:34:19 +0000128 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 +0100129
Anton Lokhmotovaf6204c2017-11-08 09:34:19 +0000130 int num_elems_read_per_iteration = pool_size;
Gian Marco Iodicebf179552017-09-05 13:51:21 +0100131 if(is_pool3x3_stride_le3)
132 {
Anton Lokhmotovaf6204c2017-11-08 09:34:19 +0000133 // Change the number of elements processed and the number of elements read per iteration
134 // for pooling 3x3 with stride less equal than 3
Gian Marco Iodicebf179552017-09-05 13:51:21 +0100135 _num_elems_processed_per_iteration = 4;
Anton Lokhmotovaf6204c2017-11-08 09:34:19 +0000136 num_elems_read_per_iteration = pool_size * (pool_stride_x + 1);
Gian Marco Iodicebf179552017-09-05 13:51:21 +0100137 }
138
Anton Lokhmotovaf6204c2017-11-08 09:34:19 +0000139 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 +0100140 const int upper_bound_h = ((pooled_h - 1) * pool_stride_y - pool_pad_y + pool_size) - input_height;
141
142 _border_size.right = std::max(upper_bound_w, pool_pad_x);
143 _border_size.bottom = std::max(upper_bound_h, pool_pad_y);
144
Anton Lokhmotovaf6204c2017-11-08 09:34:19 +0000145 std::string kernel_name = ((is_pool3x3_stride_le3) ? "pooling_layer_optimized_" : "pooling_layer_")
146 + support::cpp11::to_string(pool_size);
147 _kernel = static_cast<cl::Kernel>(CLKernelLibrary::get().create_kernel(kernel_name, build_opts.options()));
Gian Marco Iodicecb292832017-08-02 13:19:48 +0100148 }
Gian Marco Iodicebf179552017-09-05 13:51:21 +0100149 else // Run general case
Gian Marco Iodicecb292832017-08-02 13:19:48 +0100150 {
Gian Marco Iodicebf179552017-09-05 13:51:21 +0100151 const int upper_bound_w = ((pooled_w - 1) * pool_stride_x - pool_pad_x + pool_size) - input_width;
152 const int upper_bound_h = ((pooled_h - 1) * pool_stride_y - pool_pad_y + pool_size) - input_height;
153
154 _border_size.right = std::max(upper_bound_w, pool_pad_x);
155 _border_size.bottom = std::max(upper_bound_h, pool_pad_y);
156
Anton Lokhmotovaf6204c2017-11-08 09:34:19 +0000157 build_opts.add_option("-DPOOL_SIZE=" + support::cpp11::to_string(pool_size));
158 build_opts.add_option_if(data_type == DataType::F16, "-DFP16");
159
160 std::string kernel_name = is_data_type_quantized_asymmetric(data_type) ? "pooling_layer_N_quantized" : "pooling_layer_N";
161 _kernel = static_cast<cl::Kernel>(CLKernelLibrary::get().create_kernel(kernel_name, build_opts.options()));
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100162 }
163
164 // Configure kernel window
Anton Lokhmotovaf6204c2017-11-08 09:34:19 +0000165 Window win = calculate_max_window(*output->info(), Steps(_num_elems_processed_per_iteration));
166
167 // Configure the local work size (hint) from the first two dimensions of the global work size.
168 // On Bifrost, this works for up to 35x35xC filters, for which the pooling_layer_3_optimized
169 // kernel is launched with gws=(9, 33, C). In any case, the hint will be ignored if it is
170 // invalid (e.g. exceeds the maximum workgroup size that the kernel can be launched with).
171 if(gpu_target == GPUTarget::BIFROST)
172 {
173 cl::NDRange gws = ICLKernel::gws_from_window(win);
174 _lws_hint = cl::NDRange(gws[0], gws[1], 1);
175 }
176
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100177 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 +0100178 AccessWindowHorizontal output_access(output->info(), 0, _num_elems_processed_per_iteration);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100179 update_window_and_padding(win, input_access, output_access);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100180 output_access.set_valid_region(win, ValidRegion(Coordinates(), output->info()->tensor_shape()));
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100181 ICLKernel::configure(win);
182}
183
Georgios Pinitas3faea252017-10-30 14:13:50 +0000184Error CLPoolingLayerKernel::validate(const ITensorInfo *input, const ITensorInfo *output, const PoolingLayerInfo &pool_info)
185{
186 ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(input, output);
Anton Lokhmotovaf6204c2017-11-08 09:34:19 +0000187 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::QASYMM8, DataType::QS8, DataType::QS16, DataType::F16, DataType::F32);
188 ARM_COMPUTE_RETURN_ERROR_ON_MSG((is_data_type_quantized_asymmetric(input->data_type()) && pool_info.pool_type() == PoolingType::L2),
189 "Unsupported combination of parameters!");
Georgios Pinitas3faea252017-10-30 14:13:50 +0000190
191 int pool_pad_x = 0;
192 int pool_pad_y = 0;
193 int pool_size = pool_info.pool_size();
194 std::tie(pool_pad_x, pool_pad_y) = pool_info.pad_stride_info().pad();
195 ARM_COMPUTE_RETURN_ERROR_ON_MSG(((pool_pad_x >= pool_size) || (pool_pad_y >= pool_size)),
Anton Lokhmotovaf6204c2017-11-08 09:34:19 +0000196 "Invalid pool size and pool pad combination!");
Georgios Pinitas3faea252017-10-30 14:13:50 +0000197
198 // Checks performed when output is configured
199 if(output->total_size() != 0)
200 {
201 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input, output);
202 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_FIXED_POINT(input, output);
203
204 unsigned int pooled_w = 0;
205 unsigned int pooled_h = 0;
206 std::tie(pooled_w, pooled_h) = scaled_dimensions(input->dimension(0),
207 input->dimension(1),
208 pool_size,
209 pool_size,
210 pool_info.pad_stride_info());
211 ARM_COMPUTE_RETURN_ERROR_ON_MSG((output->dimension(0) != pooled_w) != (output->dimension(1) != pooled_h),
212 "Invalid output pooling dimensions!");
213 }
214
215 return Error{};
216}
217
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100218void CLPoolingLayerKernel::run(const Window &window, cl::CommandQueue &queue)
219{
220 ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this);
221 ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(IKernel::window(), window);
222
223 unsigned int pool_pad_x, pool_pad_y, pool_stride_x, pool_stride_y = 0;
224 std::tie(pool_pad_x, pool_pad_y) = _pool_info.pad_stride_info().pad();
225 std::tie(pool_stride_x, pool_stride_y) = _pool_info.pad_stride_info().stride();
226
steniu01f70256b2017-07-13 14:03:35 +0100227 Window window_collapsed = window.collapse_if_possible(ICLKernel::window(), Window::DimZ);
228 Window slice = window_collapsed.first_slice_window_3D();
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100229
230 do
231 {
232 // Upsample input by pool size
233 Window in_slice(slice);
Gian Marco Iodicecb292832017-08-02 13:19:48 +0100234 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 +0100235 in_slice.set(Window::DimY, Window::Dimension(in_slice.y().start() - pool_pad_y, in_slice.y().end() * pool_stride_y, pool_stride_y));
236
237 // Set inputs
238 unsigned int idx = 0;
239 add_3D_tensor_argument(idx, _input, in_slice);
240 add_3D_tensor_argument(idx, _output, slice);
Anton Lokhmotovaf6204c2017-11-08 09:34:19 +0000241 enqueue(queue, *this, slice, _lws_hint);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100242 }
steniu01f70256b2017-07-13 14:03:35 +0100243 while(window_collapsed.slide_window_slice_3D(slice));
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100244}