blob: 843873976443b94eb47e3cc9b74f7149e4245b61 [file] [log] [blame]
Manuel Bottini7b9998d2019-10-21 17:59:07 +01001/*
Pablo Marquez Tello9b392d72023-06-27 15:49:50 +01002 * Copyright (c) 2019-2021, 2023 Arm Limited.
Manuel Bottini7b9998d2019-10-21 17:59:07 +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/CLArgMinMaxLayerKernel.h"
Manuel Bottini7b9998d2019-10-21 17:59:07 +010025
Manuel Bottini7b9998d2019-10-21 17:59:07 +010026#include "arm_compute/core/CL/CLHelpers.h"
27#include "arm_compute/core/CL/CLKernelLibrary.h"
Manuel Bottini7b9998d2019-10-21 17:59:07 +010028#include "arm_compute/core/CL/ICLTensor.h"
29#include "arm_compute/core/Helpers.h"
30#include "arm_compute/core/TensorInfo.h"
31#include "arm_compute/core/Utils.h"
32#include "arm_compute/core/Validate.h"
Sang-Hoon Park68dd25f2020-10-19 16:00:11 +010033#include "src/core/CL/CLValidate.h"
34#include "src/core/helpers/AutoConfiguration.h"
35#include "src/core/helpers/WindowHelpers.h"
Matthew Bentham758b5ba2020-03-05 23:37:48 +000036#include "support/StringSupport.h"
Manuel Bottini7b9998d2019-10-21 17:59:07 +010037
38namespace arm_compute
39{
40namespace
41{
Pablo Marquez Tello9b392d72023-06-27 15:49:50 +010042Status validate_arguments(const ITensorInfo *input, const ITensorInfo *output, unsigned int axis, ReductionOperation op)
Manuel Bottini7b9998d2019-10-21 17:59:07 +010043{
44 ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(input, output);
45 ARM_COMPUTE_RETURN_ERROR_ON_F16_UNSUPPORTED(input);
Sheri Zhangc5b6d882020-06-26 14:46:59 +010046 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::QASYMM8, DataType::QASYMM8_SIGNED, DataType::S32, DataType::F16, DataType::F32);
Manuel Bottini7b9998d2019-10-21 17:59:07 +010047 ARM_COMPUTE_RETURN_ERROR_ON_MSG(op != ReductionOperation::ARG_IDX_MAX && op != ReductionOperation::ARG_IDX_MIN, "Only ARG_IDX_MAX and ARG_IDX_MIN are supported");
48 ARM_COMPUTE_RETURN_ERROR_ON_MSG(axis >= TensorShape::num_max_dimensions, "Reduction axis greater than max number of dimensions");
49 ARM_COMPUTE_RETURN_ERROR_ON_MSG(axis > 3, "Unsupported reduction axis");
50
51 if(output->total_size() != 0)
52 {
53 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(output, 1, DataType::U32, DataType::S32);
54 }
Manuel Bottini7b9998d2019-10-21 17:59:07 +010055
56 return Status{};
57}
Manuel Bottini7b9998d2019-10-21 17:59:07 +010058} // namespace
59
60CLArgMinMaxLayerKernel::CLArgMinMaxLayerKernel()
Pablo Marquez Tello9b392d72023-06-27 15:49:50 +010061 : _input(nullptr), _output(nullptr), _reduction_axis(0), _op(ReductionOperation::ARG_IDX_MAX)
Manuel Bottini7b9998d2019-10-21 17:59:07 +010062{
Giorgio Arena4a95bba2021-06-28 11:00:27 +010063 _type = CLKernelType::ELEMENTWISE;
Manuel Bottini7b9998d2019-10-21 17:59:07 +010064}
65
Pablo Marquez Tello9b392d72023-06-27 15:49:50 +010066void CLArgMinMaxLayerKernel::configure(const ICLTensor *input, ICLTensor *output, unsigned int axis, ReductionOperation op)
Manuel Bottini7b9998d2019-10-21 17:59:07 +010067{
Pablo Marquez Tello9b392d72023-06-27 15:49:50 +010068 configure(CLKernelLibrary::get().get_compile_context(), input, output, axis, op);
Manuel Bottini4c6bd512020-04-08 10:15:51 +010069}
70
Pablo Marquez Tello9b392d72023-06-27 15:49:50 +010071void CLArgMinMaxLayerKernel::configure(const CLCompileContext &compile_context, const ICLTensor *input, ICLTensor *output, unsigned int axis, ReductionOperation op)
Manuel Bottini4c6bd512020-04-08 10:15:51 +010072{
Manuel Bottini7b9998d2019-10-21 17:59:07 +010073 ARM_COMPUTE_ERROR_ON_NULLPTR(input, output);
Giorgio Arena3d3a01c2021-01-11 15:31:15 +000074
75 TensorShape output_shape{ input->info()->tensor_shape() };
76 output_shape.set(axis, 1);
77 auto_init_if_empty(*output->info(), input->info()->clone()->set_tensor_shape(output_shape).set_data_type(DataType::S32).reset_padding().set_is_resizable(true));
78
Pablo Marquez Tello9b392d72023-06-27 15:49:50 +010079 ARM_COMPUTE_ERROR_THROW_ON(validate_arguments(input->info(), output->info(), axis, op));
Giorgio Arena3d3a01c2021-01-11 15:31:15 +000080
Pablo Marquez Tello9b392d72023-06-27 15:49:50 +010081 auto padding_info = get_padding_info({ input, output });
Manuel Bottini7b9998d2019-10-21 17:59:07 +010082
83 _input = input;
Manuel Bottini7b9998d2019-10-21 17:59:07 +010084 _output = output;
85 _reduction_axis = axis;
86 _op = op;
87
88 // Set build options
Pablo Marquez Tello9b392d72023-06-27 15:49:50 +010089 const auto vector_size = adjust_vec_size(16U, input->info()->dimension(0));
Giorgio Arena3d3a01c2021-01-11 15:31:15 +000090 CLBuildOptions build_opts;
Manuel Bottini7b9998d2019-10-21 17:59:07 +010091 build_opts.add_option("-DDATA_TYPE=" + get_cl_type_from_data_type(input->info()->data_type()));
Giorgio Arena3d3a01c2021-01-11 15:31:15 +000092 build_opts.add_option("-DVEC_SIZE_LEFTOVER=" + support::cpp11::to_string(input->info()->dimension(0) % vector_size));
93 build_opts.add_option("-DVEC_SIZE=" + support::cpp11::to_string(vector_size));
Manuel Bottini7b9998d2019-10-21 17:59:07 +010094 build_opts.add_option_if(is_data_type_float(input->info()->data_type()), "-DFLOAT_DATA_TYPE");
Michalis Spyrou7317e392020-01-17 11:27:49 +000095 build_opts.add_option_if_else(op == ReductionOperation::ARG_IDX_MAX, "-DARG_MAX", "-DARG_MIN");
Manuel Bottini7b9998d2019-10-21 17:59:07 +010096 build_opts.add_option("-DDATA_TYPE_OUTPUT=" + get_cl_type_from_data_type(output->info()->data_type()));
Pablo Marquez Tello9b392d72023-06-27 15:49:50 +010097 build_opts.add_option("-DCOND_DATA_TYPE=" + get_cl_select_type_from_data_type(input->info()->data_type()));
98 build_opts.add_option("-DUNROLL_WITH_PRAGMA=1");
Manuel Bottini7b9998d2019-10-21 17:59:07 +010099
100 // Create kernel
Manuel Bottini7b9998d2019-10-21 17:59:07 +0100101 std::string kernel_axis_name;
102 switch(axis)
103 {
104 case 0:
Pablo Marquez Tello9b392d72023-06-27 15:49:50 +0100105 build_opts.add_option("-DWIDTH=" + support::cpp11::to_string(input->info()->dimension(0)));
Manuel Bottini7b9998d2019-10-21 17:59:07 +0100106 kernel_axis_name = "x";
Pablo Marquez Tello9b392d72023-06-27 15:49:50 +0100107 break;
Manuel Bottini7b9998d2019-10-21 17:59:07 +0100108 case 1:
109 build_opts.add_option("-DHEIGHT=" + support::cpp11::to_string(input->info()->dimension(1)));
110 kernel_axis_name = "y";
111 break;
112 case 2:
113 build_opts.add_option("-DDEPTH=" + support::cpp11::to_string(input->info()->dimension(2)));
114 kernel_axis_name = "z";
115 break;
116 case 3:
117 build_opts.add_option("-DDEPTH=" + support::cpp11::to_string(input->info()->dimension(2)));
118 build_opts.add_option("-DBATCH=" + support::cpp11::to_string(input->info()->dimension(3)));
119 kernel_axis_name = "w";
120 break;
121 default:
122 ARM_COMPUTE_ERROR("Not supported");
123 }
Manuel Bottini4c6bd512020-04-08 10:15:51 +0100124 _kernel = create_kernel(compile_context, "arg_min_max_" + kernel_axis_name, build_opts.options());
Manuel Bottini7b9998d2019-10-21 17:59:07 +0100125
126 // Configure kernel window
Pablo Marquez Tello9b392d72023-06-27 15:49:50 +0100127 Window win = calculate_max_window(*input->info(), Steps(vector_size));
128 ICLKernel::configure_internal(win);
Giorgio Arena3d3a01c2021-01-11 15:31:15 +0000129
130 ARM_COMPUTE_ERROR_ON(has_padding_changed(padding_info));
Manuel Bottini7b9998d2019-10-21 17:59:07 +0100131}
132
Pablo Marquez Tello9b392d72023-06-27 15:49:50 +0100133Status CLArgMinMaxLayerKernel::validate(const ITensorInfo *input, const ITensorInfo *output, unsigned int axis, ReductionOperation op)
Manuel Bottini7b9998d2019-10-21 17:59:07 +0100134{
Pablo Marquez Tello9b392d72023-06-27 15:49:50 +0100135 ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments(input, output, axis, op));
Manuel Bottini7b9998d2019-10-21 17:59:07 +0100136 return Status{};
137}
138
139void CLArgMinMaxLayerKernel::run(const Window &window, cl::CommandQueue &queue)
140{
141 ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this);
142 ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(IKernel::window(), window);
143
144 switch(_reduction_axis)
145 {
146 case 0:
147 {
148 // Set out window
149 Window out_window(window);
Pablo Marquez Tello9b392d72023-06-27 15:49:50 +0100150 Window in_window(window);
Manuel Bottini7b9998d2019-10-21 17:59:07 +0100151 out_window.set(Window::DimX, Window::Dimension(0, 0, 0));
Pablo Marquez Tello9b392d72023-06-27 15:49:50 +0100152 in_window.set(Window::DimX, Window::Dimension(0, _input->info()->dimension(0), _input->info()->dimension(0)));
153 in_window.set(Window::DimY, Window::Dimension(0, _input->info()->dimension(1), 1u));
Manuel Bottini7b9998d2019-10-21 17:59:07 +0100154
155 // Get first input and output slices
Pablo Marquez Tello9b392d72023-06-27 15:49:50 +0100156 Window in_slice = in_window.first_slice_window_2D();
Manuel Bottini7b9998d2019-10-21 17:59:07 +0100157 Window out_slice = out_window.first_slice_window_2D();
Manuel Bottini7b9998d2019-10-21 17:59:07 +0100158 do
159 {
160 unsigned int idx = 0;
161 add_2D_tensor_argument(idx, _input, in_slice);
Manuel Bottini7b9998d2019-10-21 17:59:07 +0100162 add_2D_tensor_argument(idx, _output, out_slice);
163 enqueue(queue, *this, in_slice, lws_hint());
164 }
Pablo Marquez Tello9b392d72023-06-27 15:49:50 +0100165 while(in_window.slide_window_slice_2D(in_slice) && out_window.slide_window_slice_2D(out_slice));
Manuel Bottini7b9998d2019-10-21 17:59:07 +0100166 }
167 break;
168 case 1:
169 {
170 // Get first input and output slices
171 Window window_in{ window };
172 window_in.set(Window::DimY, Window::Dimension(0, _input->info()->dimension(1), _input->info()->dimension(1)));
173 Window in_slice = window_in.first_slice_window_2D();
174 Window out_slice = window.first_slice_window_2D();
175
176 do
177 {
178 unsigned int idx = 0;
179 add_2D_tensor_argument(idx, _input, in_slice);
180 add_2D_tensor_argument(idx, _output, out_slice);
181 enqueue(queue, *this, in_slice, lws_hint());
182 }
183 while(window_in.slide_window_slice_2D(in_slice) && window.slide_window_slice_2D(out_slice));
184 }
185 break;
186 case 2:
187 {
188 // Get first input and output slices
189 Window window_in{ window };
190 window_in.set(Window::DimZ, Window::Dimension(0, _input->info()->dimension(2), _input->info()->dimension(2)));
191 Window in_slice = window_in.first_slice_window_3D();
192 Window out_slice = window.first_slice_window_3D();
193
194 do
195 {
196 unsigned int idx = 0;
197 add_3D_tensor_argument(idx, _input, in_slice);
198 add_3D_tensor_argument(idx, _output, out_slice);
199 enqueue(queue, *this, in_slice, lws_hint());
200 }
201 while(window_in.slide_window_slice_3D(in_slice) && window.slide_window_slice_3D(out_slice));
202 }
203 break;
204 case 3:
205 {
206 // Get first input and output slices
207 Window window_in{ window };
208 window_in.set(3, Window::Dimension(0, 1, 1));
209 Window in_slice = window_in.first_slice_window_4D();
210 Window out_slice = window.first_slice_window_4D();
211
212 do
213 {
214 unsigned int idx = 0;
215 add_4D_tensor_argument(idx, _input, in_slice);
216 add_4D_tensor_argument(idx, _output, out_slice);
217 enqueue(queue, *this, in_slice, lws_hint());
218 }
219 while(window_in.slide_window_slice_4D(in_slice) && window.slide_window_slice_4D(out_slice));
220 }
221 break;
222 default:
223 ARM_COMPUTE_ERROR("Not supported");
224 }
225}
226} // namespace arm_compute