blob: 3136d698c4cb13fddeb9fea696b5337a6f525fba [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 Tello4c0a38a2023-07-10 15:23:44 +010089 const auto adjusted_vector_size = adjust_vec_size(16U, input->info()->dimension(0));
90 const auto vector_size = (adjusted_vector_size == 3U && axis == 0U) ? 2U : adjusted_vector_size; // the opencl kernel only supports sizes 2, 4, 8 and 16.
91
Giorgio Arena3d3a01c2021-01-11 15:31:15 +000092 CLBuildOptions build_opts;
Manuel Bottini7b9998d2019-10-21 17:59:07 +010093 build_opts.add_option("-DDATA_TYPE=" + get_cl_type_from_data_type(input->info()->data_type()));
Giorgio Arena3d3a01c2021-01-11 15:31:15 +000094 build_opts.add_option("-DVEC_SIZE_LEFTOVER=" + support::cpp11::to_string(input->info()->dimension(0) % vector_size));
95 build_opts.add_option("-DVEC_SIZE=" + support::cpp11::to_string(vector_size));
Manuel Bottini7b9998d2019-10-21 17:59:07 +010096 build_opts.add_option_if(is_data_type_float(input->info()->data_type()), "-DFLOAT_DATA_TYPE");
Michalis Spyrou7317e392020-01-17 11:27:49 +000097 build_opts.add_option_if_else(op == ReductionOperation::ARG_IDX_MAX, "-DARG_MAX", "-DARG_MIN");
Manuel Bottini7b9998d2019-10-21 17:59:07 +010098 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 +010099 build_opts.add_option("-DCOND_DATA_TYPE=" + get_cl_select_type_from_data_type(input->info()->data_type()));
100 build_opts.add_option("-DUNROLL_WITH_PRAGMA=1");
Manuel Bottini7b9998d2019-10-21 17:59:07 +0100101
102 // Create kernel
Manuel Bottini7b9998d2019-10-21 17:59:07 +0100103 std::string kernel_axis_name;
104 switch(axis)
105 {
106 case 0:
Pablo Marquez Tello9b392d72023-06-27 15:49:50 +0100107 build_opts.add_option("-DWIDTH=" + support::cpp11::to_string(input->info()->dimension(0)));
Manuel Bottini7b9998d2019-10-21 17:59:07 +0100108 kernel_axis_name = "x";
Pablo Marquez Tello9b392d72023-06-27 15:49:50 +0100109 break;
Manuel Bottini7b9998d2019-10-21 17:59:07 +0100110 case 1:
111 build_opts.add_option("-DHEIGHT=" + support::cpp11::to_string(input->info()->dimension(1)));
112 kernel_axis_name = "y";
113 break;
114 case 2:
115 build_opts.add_option("-DDEPTH=" + support::cpp11::to_string(input->info()->dimension(2)));
116 kernel_axis_name = "z";
117 break;
118 case 3:
119 build_opts.add_option("-DDEPTH=" + support::cpp11::to_string(input->info()->dimension(2)));
120 build_opts.add_option("-DBATCH=" + support::cpp11::to_string(input->info()->dimension(3)));
121 kernel_axis_name = "w";
122 break;
123 default:
124 ARM_COMPUTE_ERROR("Not supported");
125 }
Manuel Bottini4c6bd512020-04-08 10:15:51 +0100126 _kernel = create_kernel(compile_context, "arg_min_max_" + kernel_axis_name, build_opts.options());
Manuel Bottini7b9998d2019-10-21 17:59:07 +0100127
128 // Configure kernel window
Pablo Marquez Tello9b392d72023-06-27 15:49:50 +0100129 Window win = calculate_max_window(*input->info(), Steps(vector_size));
130 ICLKernel::configure_internal(win);
Giorgio Arena3d3a01c2021-01-11 15:31:15 +0000131
132 ARM_COMPUTE_ERROR_ON(has_padding_changed(padding_info));
Manuel Bottini7b9998d2019-10-21 17:59:07 +0100133}
134
Pablo Marquez Tello9b392d72023-06-27 15:49:50 +0100135Status CLArgMinMaxLayerKernel::validate(const ITensorInfo *input, const ITensorInfo *output, unsigned int axis, ReductionOperation op)
Manuel Bottini7b9998d2019-10-21 17:59:07 +0100136{
Pablo Marquez Tello9b392d72023-06-27 15:49:50 +0100137 ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments(input, output, axis, op));
Manuel Bottini7b9998d2019-10-21 17:59:07 +0100138 return Status{};
139}
140
141void CLArgMinMaxLayerKernel::run(const Window &window, cl::CommandQueue &queue)
142{
143 ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this);
144 ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(IKernel::window(), window);
145
146 switch(_reduction_axis)
147 {
148 case 0:
149 {
150 // Set out window
151 Window out_window(window);
Pablo Marquez Tello9b392d72023-06-27 15:49:50 +0100152 Window in_window(window);
Manuel Bottini7b9998d2019-10-21 17:59:07 +0100153 out_window.set(Window::DimX, Window::Dimension(0, 0, 0));
Pablo Marquez Tello9b392d72023-06-27 15:49:50 +0100154 in_window.set(Window::DimX, Window::Dimension(0, _input->info()->dimension(0), _input->info()->dimension(0)));
155 in_window.set(Window::DimY, Window::Dimension(0, _input->info()->dimension(1), 1u));
Manuel Bottini7b9998d2019-10-21 17:59:07 +0100156
157 // Get first input and output slices
Pablo Marquez Tello9b392d72023-06-27 15:49:50 +0100158 Window in_slice = in_window.first_slice_window_2D();
Manuel Bottini7b9998d2019-10-21 17:59:07 +0100159 Window out_slice = out_window.first_slice_window_2D();
Manuel Bottini7b9998d2019-10-21 17:59:07 +0100160 do
161 {
162 unsigned int idx = 0;
163 add_2D_tensor_argument(idx, _input, in_slice);
Manuel Bottini7b9998d2019-10-21 17:59:07 +0100164 add_2D_tensor_argument(idx, _output, out_slice);
165 enqueue(queue, *this, in_slice, lws_hint());
166 }
Pablo Marquez Tello9b392d72023-06-27 15:49:50 +0100167 while(in_window.slide_window_slice_2D(in_slice) && out_window.slide_window_slice_2D(out_slice));
Manuel Bottini7b9998d2019-10-21 17:59:07 +0100168 }
169 break;
170 case 1:
171 {
172 // Get first input and output slices
173 Window window_in{ window };
174 window_in.set(Window::DimY, Window::Dimension(0, _input->info()->dimension(1), _input->info()->dimension(1)));
175 Window in_slice = window_in.first_slice_window_2D();
176 Window out_slice = window.first_slice_window_2D();
177
178 do
179 {
180 unsigned int idx = 0;
181 add_2D_tensor_argument(idx, _input, in_slice);
182 add_2D_tensor_argument(idx, _output, out_slice);
183 enqueue(queue, *this, in_slice, lws_hint());
184 }
185 while(window_in.slide_window_slice_2D(in_slice) && window.slide_window_slice_2D(out_slice));
186 }
187 break;
188 case 2:
189 {
190 // Get first input and output slices
191 Window window_in{ window };
192 window_in.set(Window::DimZ, Window::Dimension(0, _input->info()->dimension(2), _input->info()->dimension(2)));
193 Window in_slice = window_in.first_slice_window_3D();
194 Window out_slice = window.first_slice_window_3D();
195
196 do
197 {
198 unsigned int idx = 0;
199 add_3D_tensor_argument(idx, _input, in_slice);
200 add_3D_tensor_argument(idx, _output, out_slice);
201 enqueue(queue, *this, in_slice, lws_hint());
202 }
203 while(window_in.slide_window_slice_3D(in_slice) && window.slide_window_slice_3D(out_slice));
204 }
205 break;
206 case 3:
207 {
208 // Get first input and output slices
209 Window window_in{ window };
210 window_in.set(3, Window::Dimension(0, 1, 1));
211 Window in_slice = window_in.first_slice_window_4D();
212 Window out_slice = window.first_slice_window_4D();
213
214 do
215 {
216 unsigned int idx = 0;
217 add_4D_tensor_argument(idx, _input, in_slice);
218 add_4D_tensor_argument(idx, _output, out_slice);
219 enqueue(queue, *this, in_slice, lws_hint());
220 }
221 while(window_in.slide_window_slice_4D(in_slice) && window.slide_window_slice_4D(out_slice));
222 }
223 break;
224 default:
225 ARM_COMPUTE_ERROR("Not supported");
226 }
227}
228} // namespace arm_compute