blob: 542d5dcf9ff39a5d34c60e36a398c1ececed75b4 [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();
Anthony Barbier6ff3b192017-09-04 18:44:23 +010064 std::tie(pool_pad_x, pool_pad_y) = pad_stride_info.pad();
65 std::tie(pool_stride_x, pool_stride_y) = pad_stride_info.stride();
66
steniu010c7614f2017-06-23 17:00:26 +010067 ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::QS8, DataType::QS16, DataType::F16, DataType::F32);
Georgios Pinitas1dad50e2017-07-03 17:51:34 +010068 ARM_COMPUTE_ERROR_ON_NULLPTR(output);
Anthony Barbier6ff3b192017-09-04 18:44:23 +010069 ARM_COMPUTE_ERROR_ON(pool_pad_x >= pool_size || pool_pad_y >= pool_size);
70
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
84 auto_init_if_empty(*output->info(), output_shape, 1, input->info()->data_type(), input->info()->fixed_point_position());
85 }
86
87 ARM_COMPUTE_ERROR_ON_MISMATCHING_DATA_TYPES(input, output);
88 ARM_COMPUTE_ERROR_ON_MISMATCHING_FIXED_POINT(input, output);
Anthony Barbier6ff3b192017-09-04 18:44:23 +010089 ARM_COMPUTE_ERROR_ON((output->info()->dimension(0) != pooled_w) || (output->info()->dimension(1) != pooled_h));
90
Gian Marco Iodicebf179552017-09-05 13:51:21 +010091 const int input_width = input->info()->dimension(0);
92 const int input_height = input->info()->dimension(1);
Anthony Barbier6ff3b192017-09-04 18:44:23 +010093
94 // Set instance variables
Gian Marco Iodicebf179552017-09-05 13:51:21 +010095 _input = input;
96 _output = output;
97 _pool_info = pool_info;
98 _border_size = BorderSize(pool_pad_y, pool_pad_x);
Anthony Barbier6ff3b192017-09-04 18:44:23 +010099
100 // Set build options
101 std::set<std::string> build_opts;
102 build_opts.emplace(("-DDATA_TYPE=" + get_cl_type_from_data_type(input->info()->data_type())));
Georgios Pinitascdf51452017-08-31 14:21:36 +0100103 build_opts.emplace(("-DPOOL_" + string_from_pooling_type(pool_type)));
steniu010c7614f2017-06-23 17:00:26 +0100104 if(is_data_type_fixed_point(input->info()->data_type()))
105 {
106 build_opts.emplace("-DFIXED_POINT_POSITION=" + support::cpp11::to_string(input->info()->fixed_point_position()));
107 }
108
Gian Marco Iodicecb292832017-08-02 13:19:48 +0100109 build_opts.emplace(("-DSTRIDE_X=" + support::cpp11::to_string(pool_stride_x)));
Georgios Pinitascdf51452017-08-31 14:21:36 +0100110 if(pool_type != PoolingType::MAX)
Gian Marco Iodicecb292832017-08-02 13:19:48 +0100111 {
112 build_opts.emplace(("-DMAX_WIDTH=" + support::cpp11::to_string(input->info()->dimension(0) + pool_pad_x)));
113 build_opts.emplace(("-DMAX_HEIGHT=" + support::cpp11::to_string(input->info()->dimension(1) + pool_pad_y)));
114 build_opts.emplace(("-DSTRIDE_Y=" + support::cpp11::to_string(pool_stride_y)));
115 build_opts.emplace(("-DPAD_X=" + support::cpp11::to_string(pool_pad_x)));
116 build_opts.emplace(("-DPAD_Y=" + support::cpp11::to_string(pool_pad_y)));
117 }
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100118
119 // Create kernel
Georgios Pinitas13fc22c2017-10-19 18:35:59 +0100120 if((pool_size == 2) || (pool_size == 3) || (pool_size == 7))
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100121 {
Gian Marco Iodicebf179552017-09-05 13:51:21 +0100122 // Check if we have pool3x3 with stride_x less equal than 3. In these cases, run an optimized OpenCL kernel where
123 // each thread computes 4 output elements
124 const bool is_pool3x3_stride_le3 = (pool_size == 3) && (pool_stride_x <= 3) && !is_data_type_fixed_point(input->info()->data_type());
125
126 int num_elements_read_per_iteration = (pool_size == 7) ? 8 : pool_size;
127 if(is_pool3x3_stride_le3)
128 {
129 // Change the number of elements processed and number of elements read per iteration for pooling 3x3 with stride less equal than 3
130 _num_elems_processed_per_iteration = 4;
131 num_elements_read_per_iteration = pool_size * (pool_stride_x + 1);
132 }
133
134 const int upper_bound_w = ((pooled_w - 1) * pool_stride_x - pool_pad_x + num_elements_read_per_iteration) - input_width;
135 const int upper_bound_h = ((pooled_h - 1) * pool_stride_y - pool_pad_y + pool_size) - input_height;
136
137 _border_size.right = std::max(upper_bound_w, pool_pad_x);
138 _border_size.bottom = std::max(upper_bound_h, pool_pad_y);
139
140 std::string kernel_name = "pooling_layer_" + support::cpp11::to_string(pool_size);
141 if(is_pool3x3_stride_le3)
142 {
143 _kernel = static_cast<cl::Kernel>(CLKernelLibrary::get().create_kernel(kernel_name + "_optimized", build_opts));
144 }
145 else
146 {
147 _kernel = static_cast<cl::Kernel>(CLKernelLibrary::get().create_kernel(kernel_name, build_opts));
148 }
Gian Marco Iodicecb292832017-08-02 13:19:48 +0100149 }
Gian Marco Iodicebf179552017-09-05 13:51:21 +0100150 else // Run general case
Gian Marco Iodicecb292832017-08-02 13:19:48 +0100151 {
Gian Marco Iodicebf179552017-09-05 13:51:21 +0100152 _num_elems_processed_per_iteration = 1;
153
154 const int upper_bound_w = ((pooled_w - 1) * pool_stride_x - pool_pad_x + pool_size) - input_width;
155 const int upper_bound_h = ((pooled_h - 1) * pool_stride_y - pool_pad_y + pool_size) - input_height;
156
157 _border_size.right = std::max(upper_bound_w, pool_pad_x);
158 _border_size.bottom = std::max(upper_bound_h, pool_pad_y);
159
160 build_opts.emplace(("-DPOOL_SIZE=" + support::cpp11::to_string(pool_size)));
161 if(input->info()->data_type() == DataType::F16)
162 {
163 build_opts.emplace("-DFP16");
164 }
165 _kernel = static_cast<cl::Kernel>(CLKernelLibrary::get().create_kernel("pooling_layer_N", build_opts));
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100166 }
167
168 // Configure kernel window
Gian Marco Iodicecb292832017-08-02 13:19:48 +0100169 Window win = calculate_max_window(*output->info(), Steps(_num_elems_processed_per_iteration));
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100170 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 +0100171 AccessWindowHorizontal output_access(output->info(), 0, _num_elems_processed_per_iteration);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100172 update_window_and_padding(win, input_access, output_access);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100173 output_access.set_valid_region(win, ValidRegion(Coordinates(), output->info()->tensor_shape()));
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100174 ICLKernel::configure(win);
175}
176
177void CLPoolingLayerKernel::run(const Window &window, cl::CommandQueue &queue)
178{
179 ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this);
180 ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(IKernel::window(), window);
181
182 unsigned int pool_pad_x, pool_pad_y, pool_stride_x, pool_stride_y = 0;
183 std::tie(pool_pad_x, pool_pad_y) = _pool_info.pad_stride_info().pad();
184 std::tie(pool_stride_x, pool_stride_y) = _pool_info.pad_stride_info().stride();
185
steniu01f70256b2017-07-13 14:03:35 +0100186 Window window_collapsed = window.collapse_if_possible(ICLKernel::window(), Window::DimZ);
187 Window slice = window_collapsed.first_slice_window_3D();
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100188
189 do
190 {
191 // Upsample input by pool size
192 Window in_slice(slice);
Gian Marco Iodicecb292832017-08-02 13:19:48 +0100193 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 +0100194 in_slice.set(Window::DimY, Window::Dimension(in_slice.y().start() - pool_pad_y, in_slice.y().end() * pool_stride_y, pool_stride_y));
195
196 // Set inputs
197 unsigned int idx = 0;
198 add_3D_tensor_argument(idx, _input, in_slice);
199 add_3D_tensor_argument(idx, _output, slice);
200 enqueue(queue, *this, slice);
201 }
steniu01f70256b2017-07-13 14:03:35 +0100202 while(window_collapsed.slide_window_slice_3D(slice));
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100203}