blob: 08f0d4a4e58bceaca8e7cfc43c9e74e85bc00ae4 [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()
44 : _input(nullptr), _output(nullptr), _pool_info(), _border_size(0)
45{
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
Georgios Pinitasce093142017-06-19 16:11:53 +010067 static const std::set<int> supported_pool_sizes = { 2, 3, 7 };
68 ARM_COMPUTE_UNUSED(supported_pool_sizes);
69
Anthony Barbier6ff3b192017-09-04 18:44:23 +010070 ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::F16, DataType::F32);
Georgios Pinitas1dad50e2017-07-03 17:51:34 +010071 ARM_COMPUTE_ERROR_ON_NULLPTR(output);
Georgios Pinitasce093142017-06-19 16:11:53 +010072 ARM_COMPUTE_ERROR_ON(supported_pool_sizes.find(pool_size) == supported_pool_sizes.end());
Anthony Barbier6ff3b192017-09-04 18:44:23 +010073 ARM_COMPUTE_ERROR_ON(pool_pad_x >= pool_size || pool_pad_y >= pool_size);
74
75 // 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
88 auto_init_if_empty(*output->info(), output_shape, 1, input->info()->data_type(), input->info()->fixed_point_position());
89 }
90
91 ARM_COMPUTE_ERROR_ON_MISMATCHING_DATA_TYPES(input, output);
92 ARM_COMPUTE_ERROR_ON_MISMATCHING_FIXED_POINT(input, output);
Anthony Barbier6ff3b192017-09-04 18:44:23 +010093 ARM_COMPUTE_ERROR_ON((output->info()->dimension(0) != pooled_w) || (output->info()->dimension(1) != pooled_h));
94
Georgios Pinitasce093142017-06-19 16:11:53 +010095 const int num_elements_read_per_iteration = (pool_size == 7) ? 8 : pool_size;
96 const int input_width = input->info()->dimension(0);
97 const int input_height = input->info()->dimension(1);
98 const int upper_bound_w = ((pooled_w - 1) * pool_stride_x - pool_pad_x + num_elements_read_per_iteration) - input_width;
99 const int upper_bound_h = ((pooled_h - 1) * pool_stride_y - pool_pad_y + pool_size) - input_height;
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100100
101 // Set instance variables
102 _input = input;
103 _output = output;
104 _pool_info = pool_info;
105 _border_size = BorderSize(pool_pad_y, pool_pad_x);
106 _border_size.right = std::max(upper_bound_w, pool_pad_x);
107 _border_size.bottom = std::max(upper_bound_h, pool_pad_y);
108
109 // Set build options
110 std::set<std::string> build_opts;
111 build_opts.emplace(("-DDATA_TYPE=" + get_cl_type_from_data_type(input->info()->data_type())));
112 build_opts.emplace(("-DPOOL_" + ((PoolingType::MAX == pool_type) ? std::string("MAX") : std::string("AVG"))));
113
114 // Create kernel
115 std::string kernel_name = "pooling_layer_" + val_to_string(pool_size);
116 _kernel = static_cast<cl::Kernel>(CLKernelLibrary::get().create_kernel(kernel_name, build_opts));
117
118 // Set static kernel arguments
119 if(pool_type == PoolingType::AVG)
120 {
121 // Create static kernel arguments
122 const cl_int2 max_dims =
123 {
124 {
125 static_cast<cl_int>(input->info()->dimension(0)) + pool_pad_x,
126 static_cast<cl_int>(input->info()->dimension(1)) + pool_pad_y,
127 }
128 };
129 const cl_int2 strides =
130 {
131 {
132 pool_stride_x,
133 pool_stride_y,
134 }
135 };
136 const cl_int2 paddings =
137 {
138 {
139 pool_pad_x,
140 pool_pad_y,
141 }
142 };
143
144 // Set static kernel arguments
145 unsigned int idx = 2 * num_arguments_per_3D_tensor();
146 _kernel.setArg<cl_int2>(idx++, max_dims);
147 _kernel.setArg<cl_int2>(idx++, strides);
148 _kernel.setArg<cl_int2>(idx++, paddings);
149 }
150
151 // Configure kernel window
Georgios Pinitasce093142017-06-19 16:11:53 +0100152 const unsigned int num_elems_processed_per_iteration = 1;
153 Window win = calculate_max_window(*output->info(), Steps(num_elems_processed_per_iteration));
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100154 AccessWindowStatic input_access(input->info(), -pool_pad_x, -pool_pad_y, input_width + _border_size.right, input_height + _border_size.bottom);
155 AccessWindowHorizontal output_access(output->info(), 0, num_elems_processed_per_iteration);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100156 update_window_and_padding(win, input_access, output_access);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100157 output_access.set_valid_region(win, ValidRegion(Coordinates(), output->info()->tensor_shape()));
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100158 ICLKernel::configure(win);
159}
160
161void CLPoolingLayerKernel::run(const Window &window, cl::CommandQueue &queue)
162{
163 ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this);
164 ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(IKernel::window(), window);
165
166 unsigned int pool_pad_x, pool_pad_y, pool_stride_x, pool_stride_y = 0;
167 std::tie(pool_pad_x, pool_pad_y) = _pool_info.pad_stride_info().pad();
168 std::tie(pool_stride_x, pool_stride_y) = _pool_info.pad_stride_info().stride();
169
170 Window slice = window.first_slice_window_3D();
171
172 do
173 {
174 // Upsample input by pool size
175 Window in_slice(slice);
176 in_slice.set(Window::DimX, Window::Dimension(in_slice.x().start() - pool_pad_x, in_slice.x().end() * pool_stride_x, pool_stride_x));
177 in_slice.set(Window::DimY, Window::Dimension(in_slice.y().start() - pool_pad_y, in_slice.y().end() * pool_stride_y, pool_stride_y));
178
179 // Set inputs
180 unsigned int idx = 0;
181 add_3D_tensor_argument(idx, _input, in_slice);
182 add_3D_tensor_argument(idx, _output, slice);
183 enqueue(queue, *this, slice);
184 }
185 while(window.slide_window_slice_3D(slice));
186}