blob: 4ab6f3e89d53ed23f2fc4dd538bbdb437bd33f74 [file] [log] [blame]
Anthony Barbier7068f992017-10-26 15:23:08 +01001/*
Stephen Lie855c232018-01-04 14:13:22 +08002 * Copyright (c) 2017-2018 ARM Limited.
Anthony Barbier7068f992017-10-26 15:23:08 +01003 *
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 */
Stephen Lie855c232018-01-04 14:13:22 +080024
Anthony Barbier7068f992017-10-26 15:23:08 +010025#include "arm_compute/core/GLES_COMPUTE/kernels/GCIm2ColKernel.h"
26
27#include "arm_compute/core/AccessWindowStatic.h"
28#include "arm_compute/core/Error.h"
29#include "arm_compute/core/GLES_COMPUTE/GCHelpers.h"
30#include "arm_compute/core/GLES_COMPUTE/GCKernelLibrary.h"
31#include "arm_compute/core/GLES_COMPUTE/IGCTensor.h"
32#include "arm_compute/core/GLES_COMPUTE/OpenGLES.h"
33#include "arm_compute/core/Helpers.h"
Stephen Lie855c232018-01-04 14:13:22 +080034#include "arm_compute/core/Size2D.h"
Anthony Barbier7068f992017-10-26 15:23:08 +010035#include "arm_compute/core/Types.h"
36#include "arm_compute/core/Validate.h"
37#include "support/ToolchainSupport.h"
38
39#include <cmath>
40#include <tuple>
41
42using namespace arm_compute;
43
Stephen Lie855c232018-01-04 14:13:22 +080044namespace
45{
46Status validate_arguments(const ITensorInfo *input, const ITensorInfo *output)
47{
48 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::F16, DataType::F32);
49 ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(output);
50
51 // Checks performed when output is configured
52 if(output->total_size() != 0)
53 {
54 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input, output);
55 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_FIXED_POINT(input, output);
56 }
57
58 return Status{};
59}
60} // namespace
61
Anthony Barbier7068f992017-10-26 15:23:08 +010062GCIm2ColKernel::GCIm2ColKernel()
Stephen Lie855c232018-01-04 14:13:22 +080063 : _input(nullptr), _output(nullptr), _convolved_dims(), _kernel_dims(), _num_elems_processed_per_iteration(1), _run_func(nullptr)
Anthony Barbier7068f992017-10-26 15:23:08 +010064{
65}
66
Stephen Lie855c232018-01-04 14:13:22 +080067void GCIm2ColKernel::configure(const IGCTensor *input, IGCTensor *output, const Size2D &kernel_dims, const PadStrideInfo &conv_info, bool has_bias)
Anthony Barbier7068f992017-10-26 15:23:08 +010068{
Stephen Lie855c232018-01-04 14:13:22 +080069 ARM_COMPUTE_ERROR_ON_NULLPTR(input, output);
70
71 // Perform validation step
72 ARM_COMPUTE_ERROR_THROW_ON(validate_arguments(input->info(), output->info()));
Anthony Barbier7068f992017-10-26 15:23:08 +010073
74 _input = input;
75 _output = output;
Anthony Barbier7068f992017-10-26 15:23:08 +010076
Stephen Lie855c232018-01-04 14:13:22 +080077 // Create kernel
Anthony Barbier7068f992017-10-26 15:23:08 +010078 std::set<std::string> build_opts;
79 std::string dt_name = (input->info()->data_type() == DataType::F32) ? "DATA_TYPE_FP32" : "DATA_TYPE_FP16";
80 build_opts.emplace("#define LOCAL_SIZE_X " + support::cpp11::to_string(1));
81 build_opts.emplace("#define LOCAL_SIZE_Y " + support::cpp11::to_string(1));
82 build_opts.emplace("#define LOCAL_SIZE_Z " + support::cpp11::to_string(1));
83 build_opts.insert("#define " + dt_name);
84
85 if(has_bias)
86 {
87 build_opts.emplace("#define HAS_BIAS");
88 }
89
Anthony Barbier7068f992017-10-26 15:23:08 +010090 int stride_x = 0;
91 int stride_y = 0;
Stephen Lie855c232018-01-04 14:13:22 +080092
Anthony Barbier7068f992017-10-26 15:23:08 +010093 std::tie(stride_x, stride_y) = conv_info.stride();
Stephen Lie855c232018-01-04 14:13:22 +080094 _kernel_dims = std::make_pair(kernel_dims.width, kernel_dims.height);
Anthony Barbier7068f992017-10-26 15:23:08 +010095
96 const bool run_img2col_reduced = (output->info()->dimension(0) == (input->info()->dimension(0) * input->info()->dimension(1) * input->info()->dimension(2))) && (TensorShape::num_max_dimensions >= 4)
97 && (std::equal(input->info()->tensor_shape().cbegin() + 3,
98 input->info()->tensor_shape().cend(),
99 output->info()->tensor_shape().cbegin() + 1))
Stephen Lie855c232018-01-04 14:13:22 +0800100 && ((stride_x == 1) && (stride_y == 1) && !conv_info.has_padding());
Anthony Barbier7068f992017-10-26 15:23:08 +0100101
Stephen Lie855c232018-01-04 14:13:22 +0800102 std::string kernel_name = "im2col_generic";
Anthony Barbier7068f992017-10-26 15:23:08 +0100103 if(!run_img2col_reduced)
104 {
Stephen Lie855c232018-01-04 14:13:22 +0800105 if(input->info()->data_type() == DataType::F16 && _kernel_dims == std::pair<unsigned int, unsigned int>(1, 1))
106 {
107 build_opts.emplace("#define KERNEL_1x1");
108 }
Anthony Barbier7068f992017-10-26 15:23:08 +0100109
Stephen Lie855c232018-01-04 14:13:22 +0800110 build_opts.emplace("#define IM2COL_GENERIC");
111 _convolved_dims = scaled_dimensions(input->info()->dimension(0), input->info()->dimension(1),
112 kernel_dims.width, kernel_dims.height,
113 conv_info);
114 _num_elems_processed_per_iteration = 2;
115
116 build_opts.emplace("#define KERNEL_WIDTH " + support::cpp11::to_string(kernel_dims.width));
117 build_opts.emplace("#define KERNEL_HEIGHT " + support::cpp11::to_string(kernel_dims.height));
Anthony Barbier7068f992017-10-26 15:23:08 +0100118 build_opts.emplace("#define KERNEL_DEPTH " + support::cpp11::to_string(input->info()->dimension(2)));
119 build_opts.emplace("#define CONVOLVED_WIDTH " + support::cpp11::to_string(_convolved_dims.first));
120 build_opts.emplace("#define CONVOLVED_HEIGHT " + support::cpp11::to_string(_convolved_dims.second));
121 build_opts.emplace("#define STRIDE_X " + support::cpp11::to_string(conv_info.stride().first));
122 build_opts.emplace("#define STRIDE_Y " + support::cpp11::to_string(conv_info.stride().second));
Stephen Lie855c232018-01-04 14:13:22 +0800123 build_opts.emplace("#define PAD_LEFT " + support::cpp11::to_string(conv_info.pad_left()));
124 build_opts.emplace("#define PAD_TOP " + support::cpp11::to_string(conv_info.pad_top()));
125 build_opts.emplace("#define PAD_RIGHT " + support::cpp11::to_string(conv_info.pad_right()));
126 build_opts.emplace("#define PAD_BOTTOM " + support::cpp11::to_string(conv_info.pad_bottom()));
Anthony Barbier7068f992017-10-26 15:23:08 +0100127 build_opts.emplace("#define SRC_WIDTH " + support::cpp11::to_string(input->info()->dimension(0)));
128 build_opts.emplace("#define SRC_HEIGHT " + support::cpp11::to_string(input->info()->dimension(1)));
129
Anthony Barbier7068f992017-10-26 15:23:08 +0100130 _run_func = &GCIm2ColKernel::run_generic;
131 }
132 else
133 {
Stephen Lie855c232018-01-04 14:13:22 +0800134 build_opts.emplace("#define IM2COL_REDUCED");
135 kernel_name = "im2col_reduced";
Frank Leib9d38ee2017-12-05 10:43:33 +0800136
137 if(input->info()->data_type() == DataType::F32)
138 {
139 _num_elems_processed_per_iteration = 4 / input->info()->element_size();
140 }
141 else if(input->info()->data_type() == DataType::F16)
142 {
143 int input_width = input->info()->dimension(0);
144 int input_height = input->info()->dimension(1);
145
Stephen Lie855c232018-01-04 14:13:22 +0800146 build_opts.emplace("#define IMAGE_SIZE " + support::cpp11::to_string(input_width * input_height));
Frank Leib9d38ee2017-12-05 10:43:33 +0800147 if(input_width % 8 == 0)
148 {
149 _num_elems_processed_per_iteration = 8;
Stephen Lie855c232018-01-04 14:13:22 +0800150 build_opts.emplace("#define IM2COL_REDUCED_8X");
Frank Leib9d38ee2017-12-05 10:43:33 +0800151 }
152 else if(input_width % 4 == 0)
153 {
154 _num_elems_processed_per_iteration = 4;
Stephen Lie855c232018-01-04 14:13:22 +0800155 build_opts.emplace("#define IM2COL_REDUCED_4X");
Frank Leib9d38ee2017-12-05 10:43:33 +0800156 }
157 else if(input_width % 2 == 0)
158 {
159 _num_elems_processed_per_iteration = 2;
Stephen Lie855c232018-01-04 14:13:22 +0800160 build_opts.emplace("#define IM2COL_REDUCED_2X");
Frank Leib9d38ee2017-12-05 10:43:33 +0800161 }
162 else
163 {
164 _num_elems_processed_per_iteration = 2;
Stephen Lie855c232018-01-04 14:13:22 +0800165 build_opts.emplace("#define IM2COL_REDUCED_GENERIC");
Frank Leib9d38ee2017-12-05 10:43:33 +0800166 }
167 }
Anthony Barbier7068f992017-10-26 15:23:08 +0100168
Anthony Barbier7068f992017-10-26 15:23:08 +0100169 _run_func = &GCIm2ColKernel::run_reduced;
170 }
171
Stephen Lie855c232018-01-04 14:13:22 +0800172 // Create kernel
173 _kernel = static_cast<GCKernel>(GCKernelLibrary::get().create_kernel(kernel_name, build_opts));
174
Anthony Barbier7068f992017-10-26 15:23:08 +0100175 // Configure kernel window
176 Window win = calculate_max_window(*input->info(), Steps(_num_elems_processed_per_iteration));
177
178 if(input->info()->data_type() == DataType::F16)
179 {
180 // Calculate input right and bottom border
Stephen Lie855c232018-01-04 14:13:22 +0800181 const int input_width = input->info()->dimension(0);
182 const int input_height = input->info()->dimension(1);
183 int input_total_width = input->info()->padding().left + input_width + input->info()->padding().right;
184 int input_padding_right = ceil_to_multiple(input_total_width, _num_elems_processed_per_iteration) - input_total_width;
185 input_total_width = input_width + input_padding_right + input->info()->padding().right;
186 AccessWindowStatic input_access(input->info(), 0, 0, input_total_width, input_height);
Anthony Barbier7068f992017-10-26 15:23:08 +0100187
188 // Calculate output right and bottom border
189 const int output_width = output->info()->dimension(0);
190 const int output_height = output->info()->dimension(1);
191 const int output_padding_right = ceil_to_multiple(output_width, _num_elems_processed_per_iteration) - output_width;
192 AccessWindowStatic output_access(output->info(), 0, 0, output_width + output_padding_right, output_height);
193
194 update_window_and_padding(win, input_access, output_access);
195 }
196
197 output->info()->set_valid_region(ValidRegion(Coordinates(), output->info()->tensor_shape()));
198
199 if(!run_img2col_reduced)
200 {
201 // set the Z dimension's step same size as the whole dimension so that one can't split across the Z dimension
202 win.set_dimension_step(Window::DimZ, win[Window::DimZ].end() - win[Window::DimZ].start());
203 }
204
Anthony Barbier7068f992017-10-26 15:23:08 +0100205 IGCKernel::configure(win);
206}
207
Stephen Lie855c232018-01-04 14:13:22 +0800208Status GCIm2ColKernel::validate(const ITensorInfo *input, const ITensorInfo *output, const Size2D &kernel_dims, const PadStrideInfo &conv_info, bool has_bias)
209{
210 ARM_COMPUTE_UNUSED(kernel_dims);
211 ARM_COMPUTE_UNUSED(conv_info);
212 ARM_COMPUTE_UNUSED(has_bias);
213 ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments(input, output));
214 return Status{};
215}
216
Anthony Barbier7068f992017-10-26 15:23:08 +0100217void GCIm2ColKernel::run(const Window &window)
218{
219 ARM_COMPUTE_ERROR_ON(_run_func == nullptr);
220 (this->*_run_func)(window);
221}
222
223void GCIm2ColKernel::run_generic(const Window &window)
224{
225 ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this);
226 ARM_COMPUTE_ERROR_ON_MISMATCHING_WINDOWS(IGCKernel::window(), window);
227
228 // Get initial windows
229 Window window_collapsed = window.collapse_if_possible(IGCKernel::window(), Window::DimZ);
Stephen Lie855c232018-01-04 14:13:22 +0800230
Anthony Barbier7068f992017-10-26 15:23:08 +0100231 // Change the Z dimension's step back to 1
232 window_collapsed.set_dimension_step(Window::DimZ, 1);
233
234 Window slice = window_collapsed.first_slice_window_3D();
235 Window slice_in = window_collapsed.first_slice_window_3D();
236 Window slice_out = window_collapsed.first_slice_window_3D();
237
238 // Setup slice
239 slice.set(Window::DimX, Window::Dimension(0, static_cast<int>(_convolved_dims.first), 1));
240 slice.set(Window::DimY, Window::Dimension(0, static_cast<int>(_convolved_dims.second), 1));
241
Anthony Barbier7068f992017-10-26 15:23:08 +0100242 // Setup output slice
243 slice_out.set(Window::DimX, Window::Dimension(0, _output->info()->dimension(0), _num_elems_processed_per_iteration));
244 slice_out.set(Window::DimY, Window::Dimension(0, _output->info()->dimension(1), 1));
245 slice_out.set(Window::DimZ, Window::Dimension(0, 1, 1));
246
Stephen Lie855c232018-01-04 14:13:22 +0800247 // we need top/left pad to be included in valid region
248 if(_input->info()->data_type() == DataType::F16)
249 {
250 (dynamic_cast<TensorInfo *>(_input->info()))->init(_input->info()->tensor_shape(), _input->info()->num_channels(), _input->info()->data_type(), _input->info()->strides_in_bytes(), 0,
251 _input->info()->total_size(), _input->info()->fixed_point_position());
252 }
253
Anthony Barbier7068f992017-10-26 15:23:08 +0100254 _kernel.use();
255
256 do
257 {
258 unsigned int idx = 0;
259 add_3D_tensor_argument(idx, _input, 1, slice_in);
260 add_2D_tensor_argument(idx, _output, 2, slice_out);
Joel Liangf1f3ebd2017-11-10 09:59:19 +0800261 _kernel.set_argument(idx++, static_cast<unsigned int>(_input->info()->strides_in_bytes()[3]));
262 _kernel.set_argument(idx++, static_cast<unsigned int>(_output->info()->strides_in_bytes()[3]));
Anthony Barbier7068f992017-10-26 15:23:08 +0100263 _kernel.update_shader_params();
264
265 enqueue(*this, slice);
266 }
267 while(window_collapsed.slide_window_slice_3D(slice) && window_collapsed.slide_window_slice_3D(slice_out) && window_collapsed.slide_window_slice_3D(slice_in));
268}
269
270void GCIm2ColKernel::run_reduced(const Window &window)
271{
272 ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this);
273 ARM_COMPUTE_ERROR_ON_MISMATCHING_WINDOWS(IGCKernel::window(), window);
274
275 Window out_window;
276 out_window.use_tensor_dimensions(_output->info()->tensor_shape());
277
278 Window out_slice = out_window.first_slice_window_1D();
279 Window in_slice = window.first_slice_window_3D();
280
281 _kernel.use();
282
283 // Run kernel
284 do
285 {
286 // Set arguments
287 unsigned int idx = 0;
288
289 add_3D_tensor_argument(idx, _input, 1, in_slice);
290 add_1D_tensor_argument(idx, _output, 2, out_slice);
Joel Liangf1f3ebd2017-11-10 09:59:19 +0800291 _kernel.set_argument(idx++, _input->info()->dimension(0));
292 _kernel.set_argument(idx++, _input->info()->dimension(1));
Anthony Barbier7068f992017-10-26 15:23:08 +0100293 _kernel.update_shader_params();
294
295 enqueue(*this, in_slice);
296 }
297 while(window.slide_window_slice_3D(in_slice) && out_window.slide_window_slice_1D(out_slice));
298}