blob: 2854cd82650a4dd0b234cf55f630d2fe9f7f24e7 [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"
29#include "arm_compute/core/CL/ICLTensor.h"
30#include "arm_compute/core/CL/OpenCL.h"
31#include "arm_compute/core/Helpers.h"
32#include "arm_compute/core/TensorInfo.h"
33#include "arm_compute/core/Utils.h"
34#include "arm_compute/core/Validate.h"
35#include "arm_compute/core/Window.h"
36
37#include <set>
38#include <string>
39#include <tuple>
40
41using namespace arm_compute;
42
43CLPoolingLayerKernel::CLPoolingLayerKernel()
Gian Marco Iodicecb292832017-08-02 13:19:48 +010044 : _input(nullptr), _output(nullptr), _pool_info(), _border_size(0), _num_elems_processed_per_iteration(1)
Anthony Barbier6ff3b192017-09-04 18:44:23 +010045{
46}
47
48BorderSize CLPoolingLayerKernel::border_size() const
49{
50 return _border_size;
51}
52
53void CLPoolingLayerKernel::configure(const ICLTensor *input, ICLTensor *output, const PoolingLayerInfo &pool_info)
54{
Gian Marco Iodice4e288692017-06-27 11:41:59 +010055 int pool_pad_x = 0;
56 int pool_pad_y = 0;
57 int pool_stride_x = 0;
58 int pool_stride_y = 0;
59 unsigned int pooled_w = 0;
60 unsigned int pooled_h = 0;
61 const PoolingType pool_type = pool_info.pool_type();
62 const int pool_size = pool_info.pool_size();
63 const PadStrideInfo pad_stride_info = pool_info.pad_stride_info();
Georgios Pinitasadaae7e2017-10-30 15:56:32 +000064 bool exclude_padding = pool_info.exclude_padding();
Anthony Barbier6ff3b192017-09-04 18:44:23 +010065 std::tie(pool_pad_x, pool_pad_y) = pad_stride_info.pad();
66 std::tie(pool_stride_x, pool_stride_y) = pad_stride_info.stride();
67
Georgios Pinitasf9d3a0a2017-11-03 19:01:44 +000068 ARM_COMPUTE_ERROR_ON_NULLPTR(input, output);
Anthony Barbier6ff3b192017-09-04 18:44:23 +010069
70 // Check output dimensions
71 std::tie(pooled_w, pooled_h) = scaled_dimensions(input->info()->dimension(0),
72 input->info()->dimension(1),
73 pool_size,
Gian Marco Iodice4e288692017-06-27 11:41:59 +010074 pool_size,
75 pool_info.pad_stride_info());
Georgios Pinitas1dad50e2017-07-03 17:51:34 +010076
77 // Output auto initialization if not yet initialized
78 {
79 TensorShape output_shape{ input->info()->tensor_shape() };
80 output_shape.set(0, pooled_w);
81 output_shape.set(1, pooled_h);
82
83 auto_init_if_empty(*output->info(), output_shape, 1, input->info()->data_type(), input->info()->fixed_point_position());
84 }
85
Georgios Pinitasf9d3a0a2017-11-03 19:01:44 +000086 ARM_COMPUTE_ERROR_THROW_ON(CLPoolingLayerKernel::validate(input->info(), output->info(), pool_info));
Anthony Barbier6ff3b192017-09-04 18:44:23 +010087
Gian Marco Iodicebf179552017-09-05 13:51:21 +010088 const int input_width = input->info()->dimension(0);
89 const int input_height = input->info()->dimension(1);
Anthony Barbier6ff3b192017-09-04 18:44:23 +010090
91 // Set instance variables
Gian Marco Iodicebf179552017-09-05 13:51:21 +010092 _input = input;
93 _output = output;
94 _pool_info = pool_info;
95 _border_size = BorderSize(pool_pad_y, pool_pad_x);
Anthony Barbier6ff3b192017-09-04 18:44:23 +010096
97 // Set build options
98 std::set<std::string> build_opts;
99 build_opts.emplace(("-DDATA_TYPE=" + get_cl_type_from_data_type(input->info()->data_type())));
Georgios Pinitascdf51452017-08-31 14:21:36 +0100100 build_opts.emplace(("-DPOOL_" + string_from_pooling_type(pool_type)));
steniu010c7614f2017-06-23 17:00:26 +0100101 if(is_data_type_fixed_point(input->info()->data_type()))
102 {
103 build_opts.emplace("-DFIXED_POINT_POSITION=" + support::cpp11::to_string(input->info()->fixed_point_position()));
104 }
105
Gian Marco Iodicecb292832017-08-02 13:19:48 +0100106 build_opts.emplace(("-DSTRIDE_X=" + support::cpp11::to_string(pool_stride_x)));
Georgios Pinitascdf51452017-08-31 14:21:36 +0100107 if(pool_type != PoolingType::MAX)
Gian Marco Iodicecb292832017-08-02 13:19:48 +0100108 {
Georgios Pinitasadaae7e2017-10-30 15:56:32 +0000109 if(exclude_padding)
110 {
111 build_opts.emplace("-DEXCLUDE_PADDING");
112 }
113 build_opts.emplace(("-DMAX_WIDTH=" + support::cpp11::to_string(input->info()->dimension(0) + (exclude_padding ? 0 : pool_pad_x))));
114 build_opts.emplace(("-DMAX_HEIGHT=" + support::cpp11::to_string(input->info()->dimension(1) + (exclude_padding ? 0 : pool_pad_y))));
Gian Marco Iodicecb292832017-08-02 13:19:48 +0100115 build_opts.emplace(("-DSTRIDE_Y=" + support::cpp11::to_string(pool_stride_y)));
116 build_opts.emplace(("-DPAD_X=" + support::cpp11::to_string(pool_pad_x)));
117 build_opts.emplace(("-DPAD_Y=" + support::cpp11::to_string(pool_pad_y)));
118 }
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100119
120 // Create kernel
Georgios Pinitas13fc22c2017-10-19 18:35:59 +0100121 if((pool_size == 2) || (pool_size == 3) || (pool_size == 7))
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100122 {
Gian Marco Iodicebf179552017-09-05 13:51:21 +0100123 // Check if we have pool3x3 with stride_x less equal than 3. In these cases, run an optimized OpenCL kernel where
124 // each thread computes 4 output elements
125 const bool is_pool3x3_stride_le3 = (pool_size == 3) && (pool_stride_x <= 3) && !is_data_type_fixed_point(input->info()->data_type());
126
127 int num_elements_read_per_iteration = (pool_size == 7) ? 8 : pool_size;
128 if(is_pool3x3_stride_le3)
129 {
130 // Change the number of elements processed and number of elements read per iteration for pooling 3x3 with stride less equal than 3
131 _num_elems_processed_per_iteration = 4;
132 num_elements_read_per_iteration = pool_size * (pool_stride_x + 1);
133 }
134
135 const int upper_bound_w = ((pooled_w - 1) * pool_stride_x - pool_pad_x + num_elements_read_per_iteration) - input_width;
136 const int upper_bound_h = ((pooled_h - 1) * pool_stride_y - pool_pad_y + pool_size) - input_height;
137
138 _border_size.right = std::max(upper_bound_w, pool_pad_x);
139 _border_size.bottom = std::max(upper_bound_h, pool_pad_y);
140
141 std::string kernel_name = "pooling_layer_" + support::cpp11::to_string(pool_size);
142 if(is_pool3x3_stride_le3)
143 {
144 _kernel = static_cast<cl::Kernel>(CLKernelLibrary::get().create_kernel(kernel_name + "_optimized", build_opts));
145 }
146 else
147 {
148 _kernel = static_cast<cl::Kernel>(CLKernelLibrary::get().create_kernel(kernel_name, build_opts));
149 }
Gian Marco Iodicecb292832017-08-02 13:19:48 +0100150 }
Gian Marco Iodicebf179552017-09-05 13:51:21 +0100151 else // Run general case
Gian Marco Iodicecb292832017-08-02 13:19:48 +0100152 {
Gian Marco Iodicebf179552017-09-05 13:51:21 +0100153 _num_elems_processed_per_iteration = 1;
154
155 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
161 build_opts.emplace(("-DPOOL_SIZE=" + support::cpp11::to_string(pool_size)));
162 if(input->info()->data_type() == DataType::F16)
163 {
164 build_opts.emplace("-DFP16");
165 }
166 _kernel = static_cast<cl::Kernel>(CLKernelLibrary::get().create_kernel("pooling_layer_N", build_opts));
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100167 }
168
169 // Configure kernel window
Gian Marco Iodicecb292832017-08-02 13:19:48 +0100170 Window win = calculate_max_window(*output->info(), Steps(_num_elems_processed_per_iteration));
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100171 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 +0100172 AccessWindowHorizontal output_access(output->info(), 0, _num_elems_processed_per_iteration);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100173 update_window_and_padding(win, input_access, output_access);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100174 output_access.set_valid_region(win, ValidRegion(Coordinates(), output->info()->tensor_shape()));
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100175 ICLKernel::configure(win);
176}
177
Georgios Pinitas3faea252017-10-30 14:13:50 +0000178Error CLPoolingLayerKernel::validate(const ITensorInfo *input, const ITensorInfo *output, const PoolingLayerInfo &pool_info)
179{
180 ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(input, output);
181 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::QS8, DataType::QS16, DataType::F16, DataType::F32);
182
183 int pool_pad_x = 0;
184 int pool_pad_y = 0;
185 int pool_size = pool_info.pool_size();
186 std::tie(pool_pad_x, pool_pad_y) = pool_info.pad_stride_info().pad();
187 ARM_COMPUTE_RETURN_ERROR_ON_MSG(((pool_pad_x >= pool_size) || (pool_pad_y >= pool_size)),
188 "Invalid pool size and pool pad combination");
189
190 // Checks performed when output is configured
191 if(output->total_size() != 0)
192 {
193 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input, output);
194 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_FIXED_POINT(input, output);
195
196 unsigned int pooled_w = 0;
197 unsigned int pooled_h = 0;
198 std::tie(pooled_w, pooled_h) = scaled_dimensions(input->dimension(0),
199 input->dimension(1),
200 pool_size,
201 pool_size,
202 pool_info.pad_stride_info());
203 ARM_COMPUTE_RETURN_ERROR_ON_MSG((output->dimension(0) != pooled_w) != (output->dimension(1) != pooled_h),
204 "Invalid output pooling dimensions!");
205 }
206
207 return Error{};
208}
209
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100210void CLPoolingLayerKernel::run(const Window &window, cl::CommandQueue &queue)
211{
212 ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this);
213 ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(IKernel::window(), window);
214
215 unsigned int pool_pad_x, pool_pad_y, pool_stride_x, pool_stride_y = 0;
216 std::tie(pool_pad_x, pool_pad_y) = _pool_info.pad_stride_info().pad();
217 std::tie(pool_stride_x, pool_stride_y) = _pool_info.pad_stride_info().stride();
218
steniu01f70256b2017-07-13 14:03:35 +0100219 Window window_collapsed = window.collapse_if_possible(ICLKernel::window(), Window::DimZ);
220 Window slice = window_collapsed.first_slice_window_3D();
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100221
222 do
223 {
224 // Upsample input by pool size
225 Window in_slice(slice);
Gian Marco Iodicecb292832017-08-02 13:19:48 +0100226 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 +0100227 in_slice.set(Window::DimY, Window::Dimension(in_slice.y().start() - pool_pad_y, in_slice.y().end() * pool_stride_y, pool_stride_y));
228
229 // Set inputs
230 unsigned int idx = 0;
231 add_3D_tensor_argument(idx, _input, in_slice);
232 add_3D_tensor_argument(idx, _output, slice);
233 enqueue(queue, *this, slice);
234 }
steniu01f70256b2017-07-13 14:03:35 +0100235 while(window_collapsed.slide_window_slice_3D(slice));
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100236}