blob: 7648025caa4c08c9bac84bef8c5e15adcb1a8f0d [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{
55 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();
64 DimensionRoundingType pool_round = pad_stride_info.round();
65 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 Pinitasce093142017-06-19 16:11:53 +010068 static const std::set<int> supported_pool_sizes = { 2, 3, 7 };
69 ARM_COMPUTE_UNUSED(supported_pool_sizes);
70
Anthony Barbier6ff3b192017-09-04 18:44:23 +010071 ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::F16, DataType::F32);
72 ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(output, 1, DataType::F16, DataType::F32);
73 ARM_COMPUTE_ERROR_ON_MISMATCHING_DATA_TYPES(input, output);
Georgios Pinitasce093142017-06-19 16:11:53 +010074 ARM_COMPUTE_ERROR_ON(supported_pool_sizes.find(pool_size) == supported_pool_sizes.end());
Anthony Barbier6ff3b192017-09-04 18:44:23 +010075 ARM_COMPUTE_ERROR_ON(pool_pad_x >= pool_size || pool_pad_y >= pool_size);
76
77 // Check output dimensions
78 std::tie(pooled_w, pooled_h) = scaled_dimensions(input->info()->dimension(0),
79 input->info()->dimension(1),
80 pool_size,
81 pool_stride_x, pool_stride_y,
82 pool_pad_x, pool_pad_y,
83 pool_round);
84 ARM_COMPUTE_UNUSED(pooled_w);
85 ARM_COMPUTE_UNUSED(pooled_h);
86 ARM_COMPUTE_ERROR_ON((output->info()->dimension(0) != pooled_w) || (output->info()->dimension(1) != pooled_h));
87
Georgios Pinitasce093142017-06-19 16:11:53 +010088 const int num_elements_read_per_iteration = (pool_size == 7) ? 8 : pool_size;
89 const int input_width = input->info()->dimension(0);
90 const int input_height = input->info()->dimension(1);
91 const int upper_bound_w = ((pooled_w - 1) * pool_stride_x - pool_pad_x + num_elements_read_per_iteration) - input_width;
92 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 +010093
94 // Set instance variables
95 _input = input;
96 _output = output;
97 _pool_info = pool_info;
98 _border_size = BorderSize(pool_pad_y, pool_pad_x);
99 _border_size.right = std::max(upper_bound_w, pool_pad_x);
100 _border_size.bottom = std::max(upper_bound_h, pool_pad_y);
101
102 // Set build options
103 std::set<std::string> build_opts;
104 build_opts.emplace(("-DDATA_TYPE=" + get_cl_type_from_data_type(input->info()->data_type())));
105 build_opts.emplace(("-DPOOL_" + ((PoolingType::MAX == pool_type) ? std::string("MAX") : std::string("AVG"))));
106
107 // Create kernel
108 std::string kernel_name = "pooling_layer_" + val_to_string(pool_size);
109 _kernel = static_cast<cl::Kernel>(CLKernelLibrary::get().create_kernel(kernel_name, build_opts));
110
111 // Set static kernel arguments
112 if(pool_type == PoolingType::AVG)
113 {
114 // Create static kernel arguments
115 const cl_int2 max_dims =
116 {
117 {
118 static_cast<cl_int>(input->info()->dimension(0)) + pool_pad_x,
119 static_cast<cl_int>(input->info()->dimension(1)) + pool_pad_y,
120 }
121 };
122 const cl_int2 strides =
123 {
124 {
125 pool_stride_x,
126 pool_stride_y,
127 }
128 };
129 const cl_int2 paddings =
130 {
131 {
132 pool_pad_x,
133 pool_pad_y,
134 }
135 };
136
137 // Set static kernel arguments
138 unsigned int idx = 2 * num_arguments_per_3D_tensor();
139 _kernel.setArg<cl_int2>(idx++, max_dims);
140 _kernel.setArg<cl_int2>(idx++, strides);
141 _kernel.setArg<cl_int2>(idx++, paddings);
142 }
143
144 // Configure kernel window
Georgios Pinitasce093142017-06-19 16:11:53 +0100145 const unsigned int num_elems_processed_per_iteration = 1;
146 Window win = calculate_max_window(*output->info(), Steps(num_elems_processed_per_iteration));
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100147 AccessWindowStatic input_access(input->info(), -pool_pad_x, -pool_pad_y, input_width + _border_size.right, input_height + _border_size.bottom);
148 AccessWindowHorizontal output_access(output->info(), 0, num_elems_processed_per_iteration);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100149 update_window_and_padding(win, input_access, output_access);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100150 output_access.set_valid_region(win, ValidRegion(Coordinates(), output->info()->tensor_shape()));
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100151 ICLKernel::configure(win);
152}
153
154void CLPoolingLayerKernel::run(const Window &window, cl::CommandQueue &queue)
155{
156 ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this);
157 ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(IKernel::window(), window);
158
159 unsigned int pool_pad_x, pool_pad_y, pool_stride_x, pool_stride_y = 0;
160 std::tie(pool_pad_x, pool_pad_y) = _pool_info.pad_stride_info().pad();
161 std::tie(pool_stride_x, pool_stride_y) = _pool_info.pad_stride_info().stride();
162
163 Window slice = window.first_slice_window_3D();
164
165 do
166 {
167 // Upsample input by pool size
168 Window in_slice(slice);
169 in_slice.set(Window::DimX, Window::Dimension(in_slice.x().start() - pool_pad_x, in_slice.x().end() * pool_stride_x, pool_stride_x));
170 in_slice.set(Window::DimY, Window::Dimension(in_slice.y().start() - pool_pad_y, in_slice.y().end() * pool_stride_y, pool_stride_y));
171
172 // Set inputs
173 unsigned int idx = 0;
174 add_3D_tensor_argument(idx, _input, in_slice);
175 add_3D_tensor_argument(idx, _output, slice);
176 enqueue(queue, *this, slice);
177 }
178 while(window.slide_window_slice_3D(slice));
179}