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