blob: ee60b8e1df4c14035d8ddabd9372383368be05d6 [file] [log] [blame]
Michalis Spyrou04f089c2017-08-08 17:42:38 +01001/*
Matthew Bentham314d3e22023-06-23 10:53:52 +00002 * Copyright (c) 2017-2021, 2023 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 */
Sang-Hoon Parkbef7fa22020-10-21 15:58:54 +010024#include "src/core/CL/kernels/CLReductionOperationKernel.h"
Michalis Spyrou04f089c2017-08-08 17:42:38 +010025
Michalis Spyrou04f089c2017-08-08 17:42:38 +010026#include "arm_compute/core/CL/CLHelpers.h"
27#include "arm_compute/core/CL/CLKernelLibrary.h"
28#include "arm_compute/core/CL/ICLTensor.h"
Michalis Spyrou04f089c2017-08-08 17:42:38 +010029#include "arm_compute/core/Helpers.h"
30#include "arm_compute/core/TensorInfo.h"
Michalis Spyrou04f089c2017-08-08 17:42:38 +010031#include "arm_compute/core/Validate.h"
Matthew Bentham314d3e22023-06-23 10:53:52 +000032#include "arm_compute/core/utils/helpers/AdjustVecSize.h"
Sang-Hoon Park2697fd82019-10-15 16:49:24 +010033#include "arm_compute/core/utils/misc/ShapeCalculator.h"
Matthew Bentham314d3e22023-06-23 10:53:52 +000034#include "arm_compute/core/utils/StringUtils.h"
Sang-Hoon Park68dd25f2020-10-19 16:00:11 +010035#include "src/core/AccessWindowStatic.h"
36#include "src/core/CL/CLValidate.h"
37#include "src/core/helpers/AutoConfiguration.h"
38#include "src/core/helpers/WindowHelpers.h"
Michalis Spyrou04f089c2017-08-08 17:42:38 +010039
Matthew Bentham758b5ba2020-03-05 23:37:48 +000040#include "support/StringSupport.h"
Michalis Spyrou04f089c2017-08-08 17:42:38 +010041
Michele Di Giorgio9637b2e2019-09-23 16:49:49 +010042namespace arm_compute
43{
John Richardson62385bc2018-04-20 13:11:36 +010044namespace
45{
Giorgio Arena3ecf9fe2021-04-28 16:11:51 +010046Status validate_arguments(const ITensorInfo *input, const ITensorInfo *output, unsigned int axis, ReductionOperation op)
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 {
Michalis Spyrou0b18d972020-01-30 18:11:13 +000052 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::QASYMM8, DataType::QASYMM8_SIGNED, DataType::S32, DataType::F16, DataType::F32);
Georgios Pinitas8be91482019-03-26 17:23:28 +000053 }
54 else
55 {
Giorgio Arenaea7de7b2020-12-10 16:49:39 +000056 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 2, DataType::F16, DataType::F32);
Giorgio Arena3ecf9fe2021-04-28 16:11:51 +010057 ARM_COMPUTE_RETURN_ERROR_ON(axis == 0);
Georgios Pinitas8be91482019-03-26 17:23:28 +000058 }
Michalis Spyrou8aaf93e2018-10-11 17:33:32 +010059 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 +010060 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 +010061 ARM_COMPUTE_RETURN_ERROR_ON_MSG(axis > 3, "Unsupported reduction axis");
Giorgio Arena3ecf9fe2021-04-28 16:11:51 +010062 ARM_COMPUTE_RETURN_ERROR_ON((op == ReductionOperation::MEAN_SUM) && (axis == 0) && (input->dimension(0) == 0) && (input->data_type() != DataType::QASYMM8)
63 && (input->data_type() != DataType::QASYMM8_SIGNED));
Michalis Spyrou0b18d972020-01-30 18:11:13 +000064 ARM_COMPUTE_RETURN_ERROR_ON_MSG((op == ReductionOperation::ARG_IDX_MAX) || (op == ReductionOperation::ARG_IDX_MIN), "Not supported reduction operation, use CLArgMinMaxLayer");
John Richardson62385bc2018-04-20 13:11:36 +010065
66 if(output->total_size() != 0)
67 {
Manuel Bottini7b9998d2019-10-21 17:59:07 +010068 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input, output);
69 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_QUANTIZATION_INFO(input, output);
John Richardson62385bc2018-04-20 13:11:36 +010070 }
71
72 return Status{};
73}
John Richardson62385bc2018-04-20 13:11:36 +010074} // namespace
75
Michalis Spyrou04f089c2017-08-08 17:42:38 +010076CLReductionOperationKernel::CLReductionOperationKernel()
Giorgio Arena3ecf9fe2021-04-28 16:11:51 +010077 : _input(nullptr), _output(nullptr), _reduction_axis(0), _op(ReductionOperation::SUM_SQUARE)
Michalis Spyrou04f089c2017-08-08 17:42:38 +010078{
Giorgio Arena4a95bba2021-06-28 11:00:27 +010079 _type = CLKernelType::ELEMENTWISE;
Michalis Spyrou04f089c2017-08-08 17:42:38 +010080}
81
Giorgio Arena3ecf9fe2021-04-28 16:11:51 +010082void CLReductionOperationKernel::configure(const ICLTensor *input, ICLTensor *output, unsigned int axis, ReductionOperation op)
Michalis Spyrou04f089c2017-08-08 17:42:38 +010083{
Giorgio Arena3ecf9fe2021-04-28 16:11:51 +010084 configure(CLKernelLibrary::get().get_compile_context(), input, output, axis, op);
Michalis Spyrou04f089c2017-08-08 17:42:38 +010085}
86
Giorgio Arena3ecf9fe2021-04-28 16:11:51 +010087void CLReductionOperationKernel::configure(const CLCompileContext &compile_context, const ICLTensor *input, ICLTensor *output, unsigned int axis, ReductionOperation op)
Manuel Bottini4c6bd512020-04-08 10:15:51 +010088{
John Richardson62385bc2018-04-20 13:11:36 +010089 ARM_COMPUTE_ERROR_ON_NULLPTR(input, output);
Michalis Spyrou04f089c2017-08-08 17:42:38 +010090
Giorgio Arena3ecf9fe2021-04-28 16:11:51 +010091 ARM_COMPUTE_ERROR_THROW_ON(validate_arguments(input->info(), output->info(), axis, op));
92
93 auto padding_info = get_padding_info({ input, output });
Michalis Spyrou04f089c2017-08-08 17:42:38 +010094
95 _input = input;
96 _output = output;
97 _reduction_axis = axis;
98 _op = op;
Michalis Spyrou343722b2018-06-05 13:04:40 +010099
Giorgio Arena3ecf9fe2021-04-28 16:11:51 +0100100 const TensorShape output_shape = arm_compute::misc::shape_calculator::compute_reduced_shape(input->info()->tensor_shape(), axis, true);
101 auto_init_if_empty(*output->info(), input->info()->clone()->set_tensor_shape(output_shape).reset_padding().set_is_resizable(true));
102
Michalis Spyrou04f089c2017-08-08 17:42:38 +0100103 // Set build options
Michalis Spyrou7e9391b2018-10-05 14:49:28 +0100104 CLBuildOptions build_opts;
Michalis Spyrou0b18d972020-01-30 18:11:13 +0000105 DataType data_type = input->info()->data_type();
106 std::string data_type_promoted{};
107
108 if(is_data_type_quantized(data_type))
Michalis Spyrou7e9391b2018-10-05 14:49:28 +0100109 {
Michalis Spyrou9bd49f92020-02-07 15:25:17 +0000110 data_type_promoted = "int";
Michalis Spyrou0b18d972020-01-30 18:11:13 +0000111 }
112 else
113 {
114 data_type_promoted = get_cl_type_from_data_type(data_type);
Michalis Spyrou7e9391b2018-10-05 14:49:28 +0100115 }
Michalis Spyroub9626ab2019-05-13 17:41:01 +0100116
Giorgio Arena3ecf9fe2021-04-28 16:11:51 +0100117 const unsigned int width = input->info()->dimension(0) * input->info()->num_channels();
118 unsigned int vec_size = (is_data_type_quantized(input->info()->data_type()) && (axis == 0)) ? 1 : 16;
119 vec_size = adjust_vec_size(vec_size, width);
120 const unsigned int vec_size_leftover = width % vec_size;
121
Michalis Spyrou0b18d972020-01-30 18:11:13 +0000122 build_opts.add_option("-DDATA_TYPE=" + get_cl_type_from_data_type(data_type));
Michalis Spyrou7e9391b2018-10-05 14:49:28 +0100123 build_opts.add_option("-DDATA_TYPE_PROMOTED=" + data_type_promoted);
Giorgio Arena3ecf9fe2021-04-28 16:11:51 +0100124 build_opts.add_option("-DVEC_SIZE=" + support::cpp11::to_string(vec_size));
125 build_opts.add_option("-DVEC_SIZE_LEFTOVER=" + support::cpp11::to_string(vec_size_leftover));
Michalis Spyrou0b18d972020-01-30 18:11:13 +0000126 build_opts.add_option_if(is_data_type_float(data_type), "-DFLOAT_DATA_TYPE");
Manuel Bottinib412fab2018-12-10 17:40:23 +0000127 build_opts.add_option_if(op == ReductionOperation::SUM_SQUARE, "-DSUM_SQUARE");
Michalis Spyrou7e9391b2018-10-05 14:49:28 +0100128 build_opts.add_option_if(op == ReductionOperation::MEAN_SUM, "-DMEAN");
Michalis Spyrou0b18d972020-01-30 18:11:13 +0000129 build_opts.add_option_if(op == ReductionOperation::SUM, "-DSUM");
Manuel Bottinib412fab2018-12-10 17:40:23 +0000130 build_opts.add_option_if(op == ReductionOperation::PROD, "-DPROD");
Usama Arifb2890502019-05-21 11:48:37 +0100131 build_opts.add_option_if(op == ReductionOperation::MIN, "-DMIN");
Usama Arif048b0f32019-05-22 16:32:27 +0100132 build_opts.add_option_if(op == ReductionOperation::MAX, "-DMAX");
Michalis Spyrou0b18d972020-01-30 18:11:13 +0000133 build_opts.add_option_if(is_data_type_quantized(data_type), "-DOFFSET=" + support::cpp11::to_string(input->info()->quantization_info().uniform().offset));
134 build_opts.add_option_if(is_data_type_quantized(data_type), "-DSCALE=" + float_to_string_with_full_precision(input->info()->quantization_info().uniform().scale));
Michalis Spyrou04f089c2017-08-08 17:42:38 +0100135
136 switch(op)
137 {
138 case ReductionOperation::SUM_SQUARE:
Michalis Spyrou7e9391b2018-10-05 14:49:28 +0100139 build_opts.add_option(("-DOPERATION=square_sum"));
Michalis Spyrou04f089c2017-08-08 17:42:38 +0100140 break;
141 case ReductionOperation::SUM:
Michalis Spyrou7e9391b2018-10-05 14:49:28 +0100142 case ReductionOperation::MEAN_SUM:
143 build_opts.add_option(("-DOPERATION=sum"));
Michalis Spyrou04f089c2017-08-08 17:42:38 +0100144 break;
Usama Arifb2890502019-05-21 11:48:37 +0100145 case ReductionOperation::MIN:
Usama Arif048b0f32019-05-22 16:32:27 +0100146 case ReductionOperation::MAX:
Michalis Spyrou7930db42018-11-22 17:36:28 +0000147 break;
Manuel Bottinib412fab2018-12-10 17:40:23 +0000148 case ReductionOperation::PROD:
149 build_opts.add_option(("-DOPERATION=product"));
150 break;
Michalis Spyrou04f089c2017-08-08 17:42:38 +0100151 default:
152 ARM_COMPUTE_ERROR("Unsupported reduction operation");
153 }
154
155 // Create kernel
Michalis Spyrou7e9391b2018-10-05 14:49:28 +0100156 std::string kernel_axis_name;
Sang-Hoon Park2697fd82019-10-15 16:49:24 +0100157 const bool is_serial_op = needs_serialized_reduction(_op, _input->info()->data_type(), _reduction_axis);
158
Michalis Spyrou7e9391b2018-10-05 14:49:28 +0100159 switch(axis)
160 {
161 case 0:
162 {
Giorgio Arena3ecf9fe2021-04-28 16:11:51 +0100163 build_opts.add_option("-DWIDTH=" + support::cpp11::to_string(width));
Giorgio Arena1fac0372021-04-30 15:09:46 +0100164 kernel_axis_name = ((is_serial_op) ? "non_parallel_x" : "x");
Michalis Spyrou7e9391b2018-10-05 14:49:28 +0100165 }
166 break;
167 case 1:
168 build_opts.add_option("-DHEIGHT=" + support::cpp11::to_string(input->info()->dimension(1)));
169 kernel_axis_name = "y";
170 break;
171 case 2:
172 build_opts.add_option("-DDEPTH=" + support::cpp11::to_string(input->info()->dimension(2)));
173 kernel_axis_name = "z";
174 break;
175 case 3:
176 build_opts.add_option("-DDEPTH=" + support::cpp11::to_string(input->info()->dimension(2)));
177 build_opts.add_option("-DBATCH=" + support::cpp11::to_string(input->info()->dimension(3)));
178 kernel_axis_name = "w";
179 break;
180 default:
181 ARM_COMPUTE_ERROR("Not supported");
182 }
Manuel Bottini4c6bd512020-04-08 10:15:51 +0100183 _kernel = create_kernel(compile_context, "reduction_operation_" + kernel_axis_name, build_opts.options());
Michalis Spyrou04f089c2017-08-08 17:42:38 +0100184
185 // Configure kernel window
Giorgio Arena3ecf9fe2021-04-28 16:11:51 +0100186 Window win = calculate_max_window(*input->info(), Steps(vec_size));
187 win.set(Window::DimX, Window::Dimension(win.x().start(), win.x().end() * _input->info()->num_channels(), win.x().step()));
188 ICLKernel::configure_internal(win);
Manuel Bottini34f88dd2019-10-18 10:37:46 +0000189
Giorgio Arena3ecf9fe2021-04-28 16:11:51 +0100190 ARM_COMPUTE_ERROR_ON(has_padding_changed(padding_info));
John Richardson62385bc2018-04-20 13:11:36 +0100191}
Michalis Spyrou04f089c2017-08-08 17:42:38 +0100192
Giorgio Arena3ecf9fe2021-04-28 16:11:51 +0100193Status CLReductionOperationKernel::validate(const ITensorInfo *input, const ITensorInfo *output, unsigned int axis, ReductionOperation op)
John Richardson62385bc2018-04-20 13:11:36 +0100194{
Giorgio Arena3ecf9fe2021-04-28 16:11:51 +0100195 ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments(input, output, axis, op));
John Richardson62385bc2018-04-20 13:11:36 +0100196 return Status{};
Michalis Spyrou04f089c2017-08-08 17:42:38 +0100197}
198
199void CLReductionOperationKernel::run(const Window &window, cl::CommandQueue &queue)
200{
201 ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this);
202 ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(IKernel::window(), window);
Manuel Bottini34f88dd2019-10-18 10:37:46 +0000203
Sang-Hoon Park2697fd82019-10-15 16:49:24 +0100204 const bool is_serial_op = needs_serialized_reduction(_op, _input->info()->data_type(), _reduction_axis);
Michalis Spyrou7e9391b2018-10-05 14:49:28 +0100205 switch(_reduction_axis)
Michalis Spyrou04f089c2017-08-08 17:42:38 +0100206 {
Michalis Spyrou7e9391b2018-10-05 14:49:28 +0100207 case 0:
208 {
209 // We use parallel reduction only in non quantized types
Manuel Bottinib412fab2018-12-10 17:40:23 +0000210 if(is_serial_op)
Michalis Spyrou7e9391b2018-10-05 14:49:28 +0100211 {
212 // Get first input and output slices
213 Window window_in{ window };
214 window_in.set(Window::DimX, Window::Dimension(0, _input->info()->dimension(0), _input->info()->dimension(0)));
215
Michele Di Giorgio03337042020-02-26 17:47:55 +0000216 Window out_window{ window };
217 out_window.set(Window::DimX, Window::Dimension(0, 0, 0));
218
219 Window in_slice = window_in.first_slice_window_1D();
220 Window out_slice = out_window.first_slice_window_1D();
Michalis Spyrou7e9391b2018-10-05 14:49:28 +0100221
222 do
223 {
224 unsigned int idx = 0;
225 add_1D_tensor_argument(idx, _input, in_slice);
226 add_1D_tensor_argument(idx, _output, out_slice);
Giorgio Arena3ecf9fe2021-04-28 16:11:51 +0100227 enqueue(queue, *this, in_slice);
Michalis Spyrou7e9391b2018-10-05 14:49:28 +0100228 }
Michele Di Giorgio03337042020-02-26 17:47:55 +0000229 while(window_in.slide_window_slice_1D(in_slice) && out_window.slide_window_slice_1D(out_slice));
Michalis Spyrou7e9391b2018-10-05 14:49:28 +0100230 }
Manuel Bottinib412fab2018-12-10 17:40:23 +0000231 else
232 {
233 // Set out window
Giorgio Arena3ecf9fe2021-04-28 16:11:51 +0100234 bool has_collapsed = true;
Giorgio Arena1fac0372021-04-30 15:09:46 +0100235 Window window_in = window.collapse_if_possible(window, 2, &has_collapsed);
Giorgio Arena3ecf9fe2021-04-28 16:11:51 +0100236 ARM_COMPUTE_ERROR_ON(!has_collapsed);
Manuel Bottinib412fab2018-12-10 17:40:23 +0000237
Giorgio Arena3ecf9fe2021-04-28 16:11:51 +0100238 Window window_out = window_in;
239 window_out.set(0, Window::Dimension());
Manuel Bottinib412fab2018-12-10 17:40:23 +0000240
Giorgio Arena3ecf9fe2021-04-28 16:11:51 +0100241 unsigned int idx = 0;
Giorgio Arena1fac0372021-04-30 15:09:46 +0100242 add_3D_tensor_argument(idx, _input, window_in);
243 add_3D_tensor_argument(idx, _output, window_out);
Giorgio Arena3ecf9fe2021-04-28 16:11:51 +0100244 enqueue(queue, *this, window_in);
Manuel Bottinib412fab2018-12-10 17:40:23 +0000245 }
Michalis Spyrou7e9391b2018-10-05 14:49:28 +0100246 }
247 break;
248 case 1:
249 {
250 // Get first input and output slices
251 Window window_in{ window };
252 window_in.set(Window::DimY, Window::Dimension(0, _input->info()->dimension(1), _input->info()->dimension(1)));
253 Window in_slice = window_in.first_slice_window_2D();
254 Window out_slice = window.first_slice_window_2D();
255
256 do
257 {
258 unsigned int idx = 0;
259 add_2D_tensor_argument(idx, _input, in_slice);
260 add_2D_tensor_argument(idx, _output, out_slice);
Giorgio Arena3ecf9fe2021-04-28 16:11:51 +0100261 enqueue(queue, *this, in_slice);
Michalis Spyrou7e9391b2018-10-05 14:49:28 +0100262 }
263 while(window_in.slide_window_slice_2D(in_slice) && window.slide_window_slice_2D(out_slice));
264 }
265 break;
266 case 2:
267 {
268 // Get first input and output slices
269 Window window_in{ window };
270 window_in.set(Window::DimZ, Window::Dimension(0, _input->info()->dimension(2), _input->info()->dimension(2)));
271 Window in_slice = window_in.first_slice_window_3D();
272 Window out_slice = window.first_slice_window_3D();
273
274 do
275 {
276 unsigned int idx = 0;
277 add_3D_tensor_argument(idx, _input, in_slice);
278 add_3D_tensor_argument(idx, _output, out_slice);
Giorgio Arena3ecf9fe2021-04-28 16:11:51 +0100279 enqueue(queue, *this, in_slice);
Michalis Spyrou7e9391b2018-10-05 14:49:28 +0100280 }
281 while(window_in.slide_window_slice_3D(in_slice) && window.slide_window_slice_3D(out_slice));
282 }
283 break;
284 case 3:
285 {
286 // Get first input and output slices
287 Window window_in{ window };
288 window_in.set(3, Window::Dimension(0, 1, 1));
289 Window in_slice = window_in.first_slice_window_4D();
290 Window out_slice = window.first_slice_window_4D();
291
292 do
293 {
294 unsigned int idx = 0;
295 add_4D_tensor_argument(idx, _input, in_slice);
296 add_4D_tensor_argument(idx, _output, out_slice);
Giorgio Arena3ecf9fe2021-04-28 16:11:51 +0100297 enqueue(queue, *this, in_slice);
Michalis Spyrou7e9391b2018-10-05 14:49:28 +0100298 }
299 while(window_in.slide_window_slice_4D(in_slice) && window.slide_window_slice_4D(out_slice));
300 }
301 break;
302 default:
303 ARM_COMPUTE_ERROR("Not supported");
Michalis Spyrou04f089c2017-08-08 17:42:38 +0100304 }
Michalis Spyrou04f089c2017-08-08 17:42:38 +0100305}
Manuel Bottini34f88dd2019-10-18 10:37:46 +0000306} // namespace arm_compute