blob: 8215d3ce07507d5e6f67e29e9f65c2ca54890c25 [file] [log] [blame]
Manuel Bottini7b9998d2019-10-21 17:59:07 +01001/*
Giorgio Arena3d3a01c2021-01-11 15:31:15 +00002 * Copyright (c) 2019-2021 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"
Manuel Bottini7b9998d2019-10-21 17:59:07 +010036
Matthew Bentham758b5ba2020-03-05 23:37:48 +000037#include "support/StringSupport.h"
Manuel Bottini7b9998d2019-10-21 17:59:07 +010038
39namespace arm_compute
40{
41namespace
42{
Manuel Bottini7b9998d2019-10-21 17:59:07 +010043Status validate_arguments(const ITensorInfo *input, const ITensorInfo *prev_output, const ITensorInfo *output, unsigned int axis, ReductionOperation op)
44{
45 ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(input, output);
46 ARM_COMPUTE_RETURN_ERROR_ON_F16_UNSUPPORTED(input);
Sheri Zhangc5b6d882020-06-26 14:46:59 +010047 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 +010048 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");
49 ARM_COMPUTE_RETURN_ERROR_ON_MSG(axis >= TensorShape::num_max_dimensions, "Reduction axis greater than max number of dimensions");
50 ARM_COMPUTE_RETURN_ERROR_ON_MSG(axis > 3, "Unsupported reduction axis");
51
52 if(output->total_size() != 0)
53 {
54 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(output, 1, DataType::U32, DataType::S32);
55 }
56 if(prev_output != nullptr && prev_output->total_size() != 0)
57 {
58 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(prev_output, 1, DataType::U32, DataType::S32);
59 if(output->total_size() != 0)
60 {
61 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(prev_output, output);
62 }
63 }
64
65 return Status{};
66}
Manuel Bottini7b9998d2019-10-21 17:59:07 +010067} // namespace
68
69CLArgMinMaxLayerKernel::CLArgMinMaxLayerKernel()
70 : _input(nullptr), _prev_output(nullptr), _output(nullptr), _reduction_axis(0), _op(ReductionOperation::ARG_IDX_MAX)
71{
72}
73
74void CLArgMinMaxLayerKernel::configure(const ICLTensor *input, const ICLTensor *prev_output, ICLTensor *output, unsigned int axis, ReductionOperation op)
75{
Manuel Bottini4c6bd512020-04-08 10:15:51 +010076 configure(CLKernelLibrary::get().get_compile_context(), input, prev_output, output, axis, op);
77}
78
Manuel Bottini256c0b92020-04-21 13:29:30 +010079void CLArgMinMaxLayerKernel::configure(const CLCompileContext &compile_context, const ICLTensor *input, const ICLTensor *prev_output, ICLTensor *output, unsigned int axis, ReductionOperation op)
Manuel Bottini4c6bd512020-04-08 10:15:51 +010080{
Manuel Bottini7b9998d2019-10-21 17:59:07 +010081 ARM_COMPUTE_ERROR_ON_NULLPTR(input, output);
Giorgio Arena3d3a01c2021-01-11 15:31:15 +000082
83 TensorShape output_shape{ input->info()->tensor_shape() };
84 output_shape.set(axis, 1);
85 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));
86
Manuel Bottini7b9998d2019-10-21 17:59:07 +010087 ARM_COMPUTE_ERROR_THROW_ON(validate_arguments(input->info(), (prev_output != nullptr) ? prev_output->info() : nullptr, output->info(), axis, op));
Giorgio Arena3d3a01c2021-01-11 15:31:15 +000088
89 auto padding_info = get_padding_info({ input, prev_output, output });
Manuel Bottini7b9998d2019-10-21 17:59:07 +010090
91 _input = input;
92 _prev_output = prev_output;
93 _output = output;
94 _reduction_axis = axis;
95 _op = op;
96
97 // Set build options
Giorgio Arena3d3a01c2021-01-11 15:31:15 +000098 const auto vector_size = (axis == 0) ? 16U : adjust_vec_size(16U, input->info()->dimension(0));
Manuel Bottini7b9998d2019-10-21 17:59:07 +010099
Giorgio Arena3d3a01c2021-01-11 15:31:15 +0000100 CLBuildOptions build_opts;
Manuel Bottini7b9998d2019-10-21 17:59:07 +0100101 build_opts.add_option_if(_prev_output != nullptr, "-DPREV_OUTPUT");
102 build_opts.add_option("-DDATA_TYPE=" + get_cl_type_from_data_type(input->info()->data_type()));
Giorgio Arena3d3a01c2021-01-11 15:31:15 +0000103 build_opts.add_option("-DVEC_SIZE_LEFTOVER=" + support::cpp11::to_string(input->info()->dimension(0) % vector_size));
104 build_opts.add_option("-DVEC_SIZE=" + support::cpp11::to_string(vector_size));
Manuel Bottini7b9998d2019-10-21 17:59:07 +0100105 build_opts.add_option_if(is_data_type_float(input->info()->data_type()), "-DFLOAT_DATA_TYPE");
Michalis Spyrou7317e392020-01-17 11:27:49 +0000106 build_opts.add_option_if_else(op == ReductionOperation::ARG_IDX_MAX, "-DARG_MAX", "-DARG_MIN");
Manuel Bottini7b9998d2019-10-21 17:59:07 +0100107 build_opts.add_option("-DDATA_TYPE_OUTPUT=" + get_cl_type_from_data_type(output->info()->data_type()));
Michalis Spyrou7317e392020-01-17 11:27:49 +0000108 build_opts.add_option("-DDATA_TYPE_SELECT=" + get_cl_signed_type_from_element_size(input->info()->element_size()));
Manuel Bottini7b9998d2019-10-21 17:59:07 +0100109
110 // Create kernel
111 cl::NDRange lws_hint = CLKernelLibrary::get().default_ndrange();
112 std::string kernel_axis_name;
113 switch(axis)
114 {
115 case 0:
116 {
117 const ICLTensor *input_for_width = prev_output != nullptr ? _prev_output : _input;
118 build_opts.add_option("-DWIDTH=" + support::cpp11::to_string(input_for_width->info()->dimension(0)));
119
120 kernel_axis_name = "x";
121 lws_hint = create_lws_hint_parallel_implementations(input_for_width->info()->dimension(0), vector_size);
122 }
123 break;
124 case 1:
125 build_opts.add_option("-DHEIGHT=" + support::cpp11::to_string(input->info()->dimension(1)));
126 kernel_axis_name = "y";
127 break;
128 case 2:
129 build_opts.add_option("-DDEPTH=" + support::cpp11::to_string(input->info()->dimension(2)));
130 kernel_axis_name = "z";
131 break;
132 case 3:
133 build_opts.add_option("-DDEPTH=" + support::cpp11::to_string(input->info()->dimension(2)));
134 build_opts.add_option("-DBATCH=" + support::cpp11::to_string(input->info()->dimension(3)));
135 kernel_axis_name = "w";
136 break;
137 default:
138 ARM_COMPUTE_ERROR("Not supported");
139 }
Manuel Bottini4c6bd512020-04-08 10:15:51 +0100140 _kernel = create_kernel(compile_context, "arg_min_max_" + kernel_axis_name, build_opts.options());
Manuel Bottini7b9998d2019-10-21 17:59:07 +0100141
142 // Configure kernel window
Giorgio Arena3d3a01c2021-01-11 15:31:15 +0000143 Window win = calculate_max_window((prev_output != nullptr) ? (*prev_output->info()) : (*input->info()), Steps(vector_size));
144 ICLKernel::configure_internal(win, lws_hint);
145
146 ARM_COMPUTE_ERROR_ON(has_padding_changed(padding_info));
Manuel Bottini7b9998d2019-10-21 17:59:07 +0100147}
148
149Status CLArgMinMaxLayerKernel::validate(const ITensorInfo *input, const ITensorInfo *prev_output, const ITensorInfo *output, unsigned int axis, ReductionOperation op)
150{
151 ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments(input, prev_output, output, axis, op));
Manuel Bottini7b9998d2019-10-21 17:59:07 +0100152 return Status{};
153}
154
155void CLArgMinMaxLayerKernel::run(const Window &window, cl::CommandQueue &queue)
156{
157 ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this);
158 ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(IKernel::window(), window);
159
160 switch(_reduction_axis)
161 {
162 case 0:
163 {
164 // Set out window
165 Window out_window(window);
166 out_window.set(Window::DimX, Window::Dimension(0, 0, 0));
167
168 // Get first input and output slices
169 Window in_slice = window.first_slice_window_2D();
170 Window out_slice = out_window.first_slice_window_2D();
171
172 // Reshape window
Manuel Bottini7b9998d2019-10-21 17:59:07 +0100173 const unsigned int num_tensors = _prev_output != nullptr ? 3 : 2;
174
175 // Set local sums buffer
176 unsigned int local_res_size = lws_hint()[0] * _output->info()->element_size();
177 _kernel.setArg(num_arguments_per_2D_tensor() * num_tensors, local_res_size, nullptr);
178 do
179 {
180 unsigned int idx = 0;
181 add_2D_tensor_argument(idx, _input, in_slice);
182 if(_prev_output != nullptr)
183 {
184 add_2D_tensor_argument(idx, _prev_output, in_slice);
185 }
186 add_2D_tensor_argument(idx, _output, out_slice);
187 enqueue(queue, *this, in_slice, lws_hint());
188 }
189 while(window.slide_window_slice_2D(in_slice) && window.slide_window_slice_2D(out_slice));
190 }
191 break;
192 case 1:
193 {
194 // Get first input and output slices
195 Window window_in{ window };
196 window_in.set(Window::DimY, Window::Dimension(0, _input->info()->dimension(1), _input->info()->dimension(1)));
197 Window in_slice = window_in.first_slice_window_2D();
198 Window out_slice = window.first_slice_window_2D();
199
200 do
201 {
202 unsigned int idx = 0;
203 add_2D_tensor_argument(idx, _input, in_slice);
204 add_2D_tensor_argument(idx, _output, out_slice);
205 enqueue(queue, *this, in_slice, lws_hint());
206 }
207 while(window_in.slide_window_slice_2D(in_slice) && window.slide_window_slice_2D(out_slice));
208 }
209 break;
210 case 2:
211 {
212 // Get first input and output slices
213 Window window_in{ window };
214 window_in.set(Window::DimZ, Window::Dimension(0, _input->info()->dimension(2), _input->info()->dimension(2)));
215 Window in_slice = window_in.first_slice_window_3D();
216 Window out_slice = window.first_slice_window_3D();
217
218 do
219 {
220 unsigned int idx = 0;
221 add_3D_tensor_argument(idx, _input, in_slice);
222 add_3D_tensor_argument(idx, _output, out_slice);
223 enqueue(queue, *this, in_slice, lws_hint());
224 }
225 while(window_in.slide_window_slice_3D(in_slice) && window.slide_window_slice_3D(out_slice));
226 }
227 break;
228 case 3:
229 {
230 // Get first input and output slices
231 Window window_in{ window };
232 window_in.set(3, Window::Dimension(0, 1, 1));
233 Window in_slice = window_in.first_slice_window_4D();
234 Window out_slice = window.first_slice_window_4D();
235
236 do
237 {
238 unsigned int idx = 0;
239 add_4D_tensor_argument(idx, _input, in_slice);
240 add_4D_tensor_argument(idx, _output, out_slice);
241 enqueue(queue, *this, in_slice, lws_hint());
242 }
243 while(window_in.slide_window_slice_4D(in_slice) && window.slide_window_slice_4D(out_slice));
244 }
245 break;
246 default:
247 ARM_COMPUTE_ERROR("Not supported");
248 }
249}
250} // namespace arm_compute