blob: db4850f14ea2f4a896763d2e0f930db25af62611 [file] [log] [blame]
Michalis Spyrou04f089c2017-08-08 17:42:38 +01001/*
Manuel Bottinib412fab2018-12-10 17:40:23 +00002 * Copyright (c) 2017-2019 ARM Limited.
Michalis Spyrou04f089c2017-08-08 17:42:38 +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 */
24#include "arm_compute/core/CL/kernels/CLReductionOperationKernel.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"
Georgios Pinitas8f5802f2019-02-22 11:08:32 +000029#include "arm_compute/core/CL/CLValidate.h"
Michalis Spyrou04f089c2017-08-08 17:42:38 +010030#include "arm_compute/core/CL/ICLTensor.h"
Michalis Spyrou04f089c2017-08-08 17:42:38 +010031#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 "support/ToolchainSupport.h"
38
39using namespace arm_compute;
40
John Richardson62385bc2018-04-20 13:11:36 +010041namespace
42{
Michalis Spyrou7e9391b2018-10-05 14:49:28 +010043// OpenCL kernel requires input width to be a power of 2 for x-axis.
Michalis Spyrou25747e22018-08-08 17:12:38 +010044constexpr unsigned int border_val = 64;
45
Michalis Spyrou7e9391b2018-10-05 14:49:28 +010046Status validate_arguments(const ITensorInfo *input, const ITensorInfo *output, unsigned int axis, ReductionOperation op, unsigned int width)
John Richardson62385bc2018-04-20 13:11:36 +010047{
John Richardson62385bc2018-04-20 13:11:36 +010048 ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(input, output);
Georgios Pinitas8f5802f2019-02-22 11:08:32 +000049 ARM_COMPUTE_RETURN_ERROR_ON_F16_UNSUPPORTED(input);
Georgios Pinitas8be91482019-03-26 17:23:28 +000050 if(input->num_channels() == 1)
51 {
52 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::QASYMM8, DataType::F16, DataType::F32);
53 }
54 else
55 {
56 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 2, DataType::F32);
57 }
Michalis Spyrou8aaf93e2018-10-11 17:33:32 +010058 ARM_COMPUTE_RETURN_ERROR_ON_MSG(op == ReductionOperation::SUM_SQUARE && input->data_type() == DataType::QASYMM8, "Not supported reduction operation for QASYMM8");
John Richardson62385bc2018-04-20 13:11:36 +010059 ARM_COMPUTE_RETURN_ERROR_ON_MSG(axis >= TensorShape::num_max_dimensions, "Reduction axis greater than max number of dimensions");
Michalis Spyrou7e9391b2018-10-05 14:49:28 +010060 ARM_COMPUTE_RETURN_ERROR_ON_MSG(axis > 3, "Unsupported reduction axis");
61 ARM_COMPUTE_RETURN_ERROR_ON(op == ReductionOperation::MEAN_SUM && axis == 0 && width == 0 && input->data_type() != DataType::QASYMM8);
John Richardson62385bc2018-04-20 13:11:36 +010062
63 if(output->total_size() != 0)
64 {
Michalis Spyrou7930db42018-11-22 17:36:28 +000065 if(op == ReductionOperation::ARG_IDX_MAX || op == ReductionOperation::ARG_IDX_MIN)
66 {
67 ARM_COMPUTE_RETURN_ERROR_ON_MSG(input->data_type() == DataType::QASYMM8, "Not supported operation for QASYMM8");
68 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(output, 1, DataType::U32);
69 }
70 else
71 {
72 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input, output);
Isabella Gottardi0a1090a2019-02-14 18:07:36 +000073 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_QUANTIZATION_INFO(input, output);
Michalis Spyrou7930db42018-11-22 17:36:28 +000074 }
John Richardson62385bc2018-04-20 13:11:36 +010075 }
76
77 return Status{};
78}
79
Michalis Spyrou7930db42018-11-22 17:36:28 +000080std::tuple<Status, Window> validate_and_configure_window(ITensorInfo *input, ITensorInfo *output, unsigned int axis, ReductionOperation op)
John Richardson62385bc2018-04-20 13:11:36 +010081{
82 // Output tensor auto initialization if not yet initialized
83 TensorShape output_shape{ input->tensor_shape() };
84 output_shape.set(axis, 1);
Michalis Spyrou7930db42018-11-22 17:36:28 +000085 const bool is_arg_min_max = (op == ReductionOperation::ARG_IDX_MIN || op == ReductionOperation::ARG_IDX_MAX);
86 DataType output_data_type = is_arg_min_max ? DataType::U32 : input->data_type();
Georgios Pinitas8be91482019-03-26 17:23:28 +000087 auto_init_if_empty(*output, input->clone()->set_tensor_shape(output_shape).set_data_type(output_data_type).reset_padding().set_is_resizable(true));
John Richardson62385bc2018-04-20 13:11:36 +010088
Michalis Spyrou7e9391b2018-10-05 14:49:28 +010089 const unsigned int num_elems_processed_per_iteration = (is_data_type_quantized(input->data_type()) && (axis == 0)) ? 1 : 16;
90 Window win = calculate_max_window(*input, Steps(num_elems_processed_per_iteration));
91 bool window_changed = false;
Manuel Bottinib412fab2018-12-10 17:40:23 +000092 const bool is_serial_op = (op == ReductionOperation::ARG_IDX_MAX || op == ReductionOperation::ARG_IDX_MIN || is_data_type_quantized(input->data_type()));
John Richardson62385bc2018-04-20 13:11:36 +010093
Michalis Spyrou7e9391b2018-10-05 14:49:28 +010094 switch(axis)
95 {
96 case 0:
97 {
Manuel Bottinib412fab2018-12-10 17:40:23 +000098 if(is_serial_op)
Michalis Spyrou7e9391b2018-10-05 14:49:28 +010099 {
100 AccessWindowHorizontal input_access(input, 0, input->dimension(0));
101 AccessWindowHorizontal output_access(output, 0, 1);
102 window_changed = update_window_and_padding(win, input_access, output_access);
103 output_access.set_valid_region(win, ValidRegion(Coordinates(), output->tensor_shape()));
104 }
105 else
106 {
107 const unsigned int border_width = ((input->dimension(0) % border_val) != 0) ? border_val - input->dimension(0) % border_val : 0;
108 AccessWindowStatic input_access(input, 0, 0, input->dimension(0) + border_width, 1);
109 AccessWindowHorizontal output_access(output, 0, 1);
110 window_changed = update_window_and_padding(win, input_access, output_access);
111 output_access.set_valid_region(win, ValidRegion(Coordinates(), output->tensor_shape()));
112 }
113 }
114 break;
115 case 1:
116 case 2:
117 case 3:
118 {
119 AccessWindowHorizontal input_access(input, 0, num_elems_processed_per_iteration);
120 AccessWindowHorizontal output_access(output, 0, num_elems_processed_per_iteration);
121 window_changed = update_window_and_padding(win, input_access, output_access);
122 output_access.set_valid_region(win, ValidRegion(Coordinates(), output->tensor_shape()));
123 }
124 break;
125 default:
126 ARM_COMPUTE_ERROR("Not supported");
127 }
John Richardson62385bc2018-04-20 13:11:36 +0100128
129 Status err = (window_changed) ? ARM_COMPUTE_CREATE_ERROR(ErrorCode::RUNTIME_ERROR, "Insufficient Padding!") : Status{};
130
131 return std::make_tuple(err, win);
132}
133} // namespace
134
Michalis Spyrou04f089c2017-08-08 17:42:38 +0100135CLReductionOperationKernel::CLReductionOperationKernel()
136 : _input(nullptr), _output(nullptr), _reduction_axis(0), _op(ReductionOperation::SUM_SQUARE), _border_size()
137{
138}
139
140BorderSize CLReductionOperationKernel::border_size() const
141{
142 return _border_size;
143}
144
Michalis Spyrou7e9391b2018-10-05 14:49:28 +0100145void CLReductionOperationKernel::configure(const ICLTensor *input, ICLTensor *output, unsigned int axis, ReductionOperation op, unsigned int width)
Michalis Spyrou04f089c2017-08-08 17:42:38 +0100146{
John Richardson62385bc2018-04-20 13:11:36 +0100147 ARM_COMPUTE_ERROR_ON_NULLPTR(input, output);
Michalis Spyrou04f089c2017-08-08 17:42:38 +0100148
Michalis Spyrou7e9391b2018-10-05 14:49:28 +0100149 ARM_COMPUTE_ERROR_THROW_ON(validate_arguments(input->info(), output->info(), axis, op, width));
Michalis Spyrou04f089c2017-08-08 17:42:38 +0100150
151 _input = input;
152 _output = output;
153 _reduction_axis = axis;
154 _op = op;
Michalis Spyrou343722b2018-06-05 13:04:40 +0100155
Michalis Spyrou04f089c2017-08-08 17:42:38 +0100156 // Set build options
Michalis Spyrou7e9391b2018-10-05 14:49:28 +0100157 CLBuildOptions build_opts;
158 std::string data_type_promoted = get_cl_type_from_data_type(input->info()->data_type());
Michalis Spyrou7930db42018-11-22 17:36:28 +0000159 if(is_data_type_quantized(input->info()->data_type()))
Michalis Spyrou7e9391b2018-10-05 14:49:28 +0100160 {
161 data_type_promoted = "uint";
162 }
163 build_opts.add_option("-DDATA_TYPE=" + get_cl_type_from_data_type(input->info()->data_type()));
164 build_opts.add_option("-DDATA_TYPE_PROMOTED=" + data_type_promoted);
Manuel Bottinib412fab2018-12-10 17:40:23 +0000165 build_opts.add_option_if(op == ReductionOperation::SUM_SQUARE, "-DSUM_SQUARE");
Michalis Spyrou7e9391b2018-10-05 14:49:28 +0100166 build_opts.add_option_if(op == ReductionOperation::MEAN_SUM, "-DMEAN");
Michalis Spyrou7930db42018-11-22 17:36:28 +0000167 build_opts.add_option_if(op == ReductionOperation::ARG_IDX_MAX, "-DARG_MAX");
168 build_opts.add_option_if(op == ReductionOperation::ARG_IDX_MIN, "-DARG_MIN");
Manuel Bottinib412fab2018-12-10 17:40:23 +0000169 build_opts.add_option_if(op == ReductionOperation::PROD, "-DPROD");
Georgios Pinitas8be91482019-03-26 17:23:28 +0000170 build_opts.add_option_if(input->info()->num_channels() == 2, "-DCOMPLEX");
Michalis Spyrou04f089c2017-08-08 17:42:38 +0100171
172 switch(op)
173 {
174 case ReductionOperation::SUM_SQUARE:
Michalis Spyrou7e9391b2018-10-05 14:49:28 +0100175 build_opts.add_option(("-DOPERATION=square_sum"));
Michalis Spyrou04f089c2017-08-08 17:42:38 +0100176 break;
177 case ReductionOperation::SUM:
Michalis Spyrou7e9391b2018-10-05 14:49:28 +0100178 case ReductionOperation::MEAN_SUM:
179 build_opts.add_option(("-DOPERATION=sum"));
Michalis Spyrou04f089c2017-08-08 17:42:38 +0100180 break;
Michalis Spyrou7930db42018-11-22 17:36:28 +0000181 case ReductionOperation::ARG_IDX_MAX:
182 case ReductionOperation::ARG_IDX_MIN:
183 break;
Manuel Bottinib412fab2018-12-10 17:40:23 +0000184 case ReductionOperation::PROD:
185 build_opts.add_option(("-DOPERATION=product"));
186 break;
Michalis Spyrou04f089c2017-08-08 17:42:38 +0100187 default:
188 ARM_COMPUTE_ERROR("Unsupported reduction operation");
189 }
190
191 // Create kernel
Michalis Spyrou7e9391b2018-10-05 14:49:28 +0100192 cl::NDRange lws_hint = CLKernelLibrary::get().default_ndrange();
193 std::string kernel_axis_name;
Manuel Bottinib412fab2018-12-10 17:40:23 +0000194 const bool is_serial_op = (op == ReductionOperation::ARG_IDX_MAX || op == ReductionOperation::ARG_IDX_MIN || is_data_type_quantized(input->info()->data_type()));
Michalis Spyrou7e9391b2018-10-05 14:49:28 +0100195 switch(axis)
196 {
197 case 0:
198 {
Manuel Bottinib412fab2018-12-10 17:40:23 +0000199 if(is_serial_op)
200 {
201 build_opts.add_option("-DWIDTH=" + support::cpp11::to_string(input->info()->dimension(0)));
202 build_opts.add_option_if_else(_input->info()->data_type() == DataType::F32, "-DCOND_DATA_TYPE=int", "-DCOND_DATA_TYPE=short");
203 kernel_axis_name = "non_parallel_x";
204 }
205 else
Michalis Spyrou7e9391b2018-10-05 14:49:28 +0100206 {
207 build_opts.add_option_if(op == ReductionOperation::MEAN_SUM, "-DWIDTH=" + support::cpp11::to_string(width));
208 const unsigned int width_leftover = input->info()->dimension(0) % border_val;
209 const unsigned int border_width = (width_leftover != 0) ? border_val - width_leftover : 0;
210 const unsigned int num_of_threads = ((input->info()->dimension(0) + border_width) / 16);
211 kernel_axis_name = "x";
212
213 // Set the number of WG based on the input size. If input width is < 128
214 // we can use fewer threads than 8.
215 lws_hint = cl::NDRange(std::min(8U, num_of_threads));
216 _border_size = BorderSize(0, border_width, 0, 0);
217 }
Michalis Spyrou7e9391b2018-10-05 14:49:28 +0100218 }
219 break;
220 case 1:
221 build_opts.add_option("-DHEIGHT=" + support::cpp11::to_string(input->info()->dimension(1)));
222 kernel_axis_name = "y";
223 break;
224 case 2:
225 build_opts.add_option("-DDEPTH=" + support::cpp11::to_string(input->info()->dimension(2)));
226 kernel_axis_name = "z";
227 break;
228 case 3:
229 build_opts.add_option("-DDEPTH=" + support::cpp11::to_string(input->info()->dimension(2)));
230 build_opts.add_option("-DBATCH=" + support::cpp11::to_string(input->info()->dimension(3)));
231 kernel_axis_name = "w";
232 break;
233 default:
234 ARM_COMPUTE_ERROR("Not supported");
235 }
236 _kernel = static_cast<cl::Kernel>(CLKernelLibrary::get().create_kernel("reduction_operation_" + kernel_axis_name, build_opts.options()));
Michalis Spyrou04f089c2017-08-08 17:42:38 +0100237
238 // Configure kernel window
Michalis Spyrou7930db42018-11-22 17:36:28 +0000239 auto win_config = validate_and_configure_window(_input->info(), _output->info(), axis, op);
Michalis Spyrou04f089c2017-08-08 17:42:38 +0100240
John Richardson62385bc2018-04-20 13:11:36 +0100241 ARM_COMPUTE_ERROR_THROW_ON(std::get<0>(win_config));
Michalis Spyrou04f089c2017-08-08 17:42:38 +0100242
Anthony Barbierb6eb3532018-08-08 13:20:04 +0100243 ICLKernel::configure_internal(std::get<1>(win_config), lws_hint);
John Richardson62385bc2018-04-20 13:11:36 +0100244}
Michalis Spyrou04f089c2017-08-08 17:42:38 +0100245
Michalis Spyrou7e9391b2018-10-05 14:49:28 +0100246Status CLReductionOperationKernel::validate(const ITensorInfo *input, const ITensorInfo *output, unsigned int axis, ReductionOperation op, unsigned int width)
John Richardson62385bc2018-04-20 13:11:36 +0100247{
Michalis Spyrou7e9391b2018-10-05 14:49:28 +0100248 ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments(input, output, axis, op, width));
Michalis Spyrou7930db42018-11-22 17:36:28 +0000249 ARM_COMPUTE_RETURN_ON_ERROR(std::get<0>(validate_and_configure_window(input->clone().get(), output->clone().get(), axis, op)));
John Richardson62385bc2018-04-20 13:11:36 +0100250
251 return Status{};
Michalis Spyrou04f089c2017-08-08 17:42:38 +0100252}
253
254void CLReductionOperationKernel::run(const Window &window, cl::CommandQueue &queue)
255{
256 ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this);
257 ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(IKernel::window(), window);
258
Manuel Bottinib412fab2018-12-10 17:40:23 +0000259 const bool is_serial_op = (_op == ReductionOperation::ARG_IDX_MAX || _op == ReductionOperation::ARG_IDX_MIN || is_data_type_quantized(_input->info()->data_type()));
Michalis Spyrou7e9391b2018-10-05 14:49:28 +0100260 switch(_reduction_axis)
Michalis Spyrou04f089c2017-08-08 17:42:38 +0100261 {
Michalis Spyrou7e9391b2018-10-05 14:49:28 +0100262 case 0:
263 {
264 // We use parallel reduction only in non quantized types
Manuel Bottinib412fab2018-12-10 17:40:23 +0000265 if(is_serial_op)
Michalis Spyrou7e9391b2018-10-05 14:49:28 +0100266 {
267 // Get first input and output slices
268 Window window_in{ window };
269 window_in.set(Window::DimX, Window::Dimension(0, _input->info()->dimension(0), _input->info()->dimension(0)));
270
271 Window in_slice = window.first_slice_window_1D();
272 Window out_slice = window.first_slice_window_1D();
273
274 do
275 {
276 unsigned int idx = 0;
277 add_1D_tensor_argument(idx, _input, in_slice);
278 add_1D_tensor_argument(idx, _output, out_slice);
279 enqueue(queue, *this, in_slice);
280 }
281 while(window_in.slide_window_slice_1D(in_slice) && window.slide_window_slice_1D(out_slice));
282 }
Manuel Bottinib412fab2018-12-10 17:40:23 +0000283 else
284 {
285 // Set out window
286 Window out_window(window);
287 out_window.set(Window::DimX, Window::Dimension(0, 0, 0));
288
289 // Get first input and output slices
290 Window in_slice = window.first_slice_window_2D();
291 Window out_slice = out_window.first_slice_window_2D();
292
293 // Reshape window
294 const unsigned int border_width = ((in_slice.x().end() % border_val) != 0) ? border_val - in_slice.x().end() % border_val : 0;
295 in_slice.set(Window::DimX, Window::Dimension(in_slice.x().start(), in_slice.x().end() + border_width, in_slice.x().step()));
296
297 // Set local sums buffer
298 unsigned int local_res_size = lws_hint()[0] * _input->info()->element_size();
299 _kernel.setArg(num_arguments_per_2D_tensor() * 2, local_res_size, nullptr);
300
301 do
302 {
303 unsigned int idx = 0;
304 add_2D_tensor_argument(idx, _input, in_slice);
305 add_2D_tensor_argument(idx, _output, out_slice);
306 enqueue(queue, *this, in_slice, lws_hint());
307 }
308 while(window.slide_window_slice_2D(in_slice) && window.slide_window_slice_2D(out_slice));
309 }
Michalis Spyrou7e9391b2018-10-05 14:49:28 +0100310 }
311 break;
312 case 1:
313 {
314 // Get first input and output slices
315 Window window_in{ window };
316 window_in.set(Window::DimY, Window::Dimension(0, _input->info()->dimension(1), _input->info()->dimension(1)));
317 Window in_slice = window_in.first_slice_window_2D();
318 Window out_slice = window.first_slice_window_2D();
319
320 do
321 {
322 unsigned int idx = 0;
323 add_2D_tensor_argument(idx, _input, in_slice);
324 add_2D_tensor_argument(idx, _output, out_slice);
325 enqueue(queue, *this, in_slice);
326 }
327 while(window_in.slide_window_slice_2D(in_slice) && window.slide_window_slice_2D(out_slice));
328 }
329 break;
330 case 2:
331 {
332 // Get first input and output slices
333 Window window_in{ window };
334 window_in.set(Window::DimZ, Window::Dimension(0, _input->info()->dimension(2), _input->info()->dimension(2)));
335 Window in_slice = window_in.first_slice_window_3D();
336 Window out_slice = window.first_slice_window_3D();
337
338 do
339 {
340 unsigned int idx = 0;
341 add_3D_tensor_argument(idx, _input, in_slice);
342 add_3D_tensor_argument(idx, _output, out_slice);
343 enqueue(queue, *this, in_slice);
344 }
345 while(window_in.slide_window_slice_3D(in_slice) && window.slide_window_slice_3D(out_slice));
346 }
347 break;
348 case 3:
349 {
350 // Get first input and output slices
351 Window window_in{ window };
352 window_in.set(3, Window::Dimension(0, 1, 1));
353 Window in_slice = window_in.first_slice_window_4D();
354 Window out_slice = window.first_slice_window_4D();
355
356 do
357 {
358 unsigned int idx = 0;
359 add_4D_tensor_argument(idx, _input, in_slice);
360 add_4D_tensor_argument(idx, _output, out_slice);
361 enqueue(queue, *this, in_slice);
362 }
363 while(window_in.slide_window_slice_4D(in_slice) && window.slide_window_slice_4D(out_slice));
364 }
365 break;
366 default:
367 ARM_COMPUTE_ERROR("Not supported");
Michalis Spyrou04f089c2017-08-08 17:42:38 +0100368 }
Michalis Spyrou04f089c2017-08-08 17:42:38 +0100369}