blob: bdb7ab76d56723cda72885ba8555366f26de71fa [file] [log] [blame]
Manuel Bottini7b9998d2019-10-21 17:59:07 +01001/*
Michalis Spyrou7317e392020-01-17 11:27:49 +00002 * Copyright (c) 2019-2020 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 */
24#include "arm_compute/core/CL/kernels/CLArgMinMaxLayerKernel.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"
29#include "arm_compute/core/CL/CLValidate.h"
30#include "arm_compute/core/CL/ICLTensor.h"
31#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
39namespace arm_compute
40{
41namespace
42{
43constexpr unsigned int vector_size = 16;
44
45Status validate_arguments(const ITensorInfo *input, const ITensorInfo *prev_output, const ITensorInfo *output, unsigned int axis, ReductionOperation op)
46{
47 ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(input, output);
48 ARM_COMPUTE_RETURN_ERROR_ON_F16_UNSUPPORTED(input);
Michalis Spyrou7317e392020-01-17 11:27:49 +000049 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::QASYMM8, DataType::S32, DataType::F16, DataType::F32);
Manuel Bottini7b9998d2019-10-21 17:59:07 +010050 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");
51 ARM_COMPUTE_RETURN_ERROR_ON_MSG(axis >= TensorShape::num_max_dimensions, "Reduction axis greater than max number of dimensions");
52 ARM_COMPUTE_RETURN_ERROR_ON_MSG(axis > 3, "Unsupported reduction axis");
53
54 if(output->total_size() != 0)
55 {
56 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(output, 1, DataType::U32, DataType::S32);
57 }
58 if(prev_output != nullptr && prev_output->total_size() != 0)
59 {
60 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(prev_output, 1, DataType::U32, DataType::S32);
61 if(output->total_size() != 0)
62 {
63 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(prev_output, output);
64 }
65 }
66
67 return Status{};
68}
69
70std::tuple<Status, Window> validate_and_configure_window(ITensorInfo *input, ITensorInfo *prev_output, ITensorInfo *output, unsigned int axis, ReductionOperation op)
71{
72 ARM_COMPUTE_UNUSED(op);
73 // Output tensor auto initialization if not yet initialized
74 TensorShape output_shape{ input->tensor_shape() };
75 output_shape.set(axis, 1);
76 DataType output_data_type = DataType::S32;
77 auto_init_if_empty(*output, input->clone()->set_tensor_shape(output_shape).set_data_type(output_data_type).reset_padding().set_is_resizable(true));
78
79 Window win = calculate_max_window((prev_output != nullptr) ? (*prev_output) : (*input), Steps(vector_size));
80 bool window_changed = false;
81
82 switch(axis)
83 {
84 case 0:
85 {
86 ITensorInfo *input_tensor_access = prev_output != nullptr ? prev_output : input;
87 AccessWindowStatic input_access(input_tensor_access, 0, 0, static_cast<int>(input_tensor_access->dimension(0)), 1);
88 AccessWindowHorizontal output_access(output, 0, 1);
89 window_changed = update_window_and_padding(win, input_access, output_access);
90 output_access.set_valid_region(win, ValidRegion(Coordinates(), output->tensor_shape()));
91 }
92 break;
93 case 1:
94 case 2:
95 case 3:
96 {
97 AccessWindowHorizontal input_access(input, 0, vector_size);
98 AccessWindowHorizontal output_access(output, 0, vector_size);
99 window_changed = update_window_and_padding(win, input_access, output_access);
100 output_access.set_valid_region(win, ValidRegion(Coordinates(), output->tensor_shape()));
101 }
102 break;
103 default:
104 ARM_COMPUTE_ERROR("Not supported");
105 }
106
107 Status err = (window_changed) ? ARM_COMPUTE_CREATE_ERROR(ErrorCode::RUNTIME_ERROR, "Insufficient Padding!") : Status{};
108 return std::make_tuple(err, win);
109}
110} // namespace
111
112CLArgMinMaxLayerKernel::CLArgMinMaxLayerKernel()
113 : _input(nullptr), _prev_output(nullptr), _output(nullptr), _reduction_axis(0), _op(ReductionOperation::ARG_IDX_MAX)
114{
115}
116
117void CLArgMinMaxLayerKernel::configure(const ICLTensor *input, const ICLTensor *prev_output, ICLTensor *output, unsigned int axis, ReductionOperation op)
118{
119 ARM_COMPUTE_ERROR_ON_NULLPTR(input, output);
120 ARM_COMPUTE_ERROR_THROW_ON(validate_arguments(input->info(), (prev_output != nullptr) ? prev_output->info() : nullptr, output->info(), axis, op));
121 auto win_config = validate_and_configure_window(input->info(), (prev_output != nullptr) ? prev_output->info() : nullptr, output->info(), axis, op);
122 ARM_COMPUTE_ERROR_THROW_ON(std::get<0>(win_config));
123
124 _input = input;
125 _prev_output = prev_output;
126 _output = output;
127 _reduction_axis = axis;
128 _op = op;
129
130 // Set build options
Michalis Spyrou7317e392020-01-17 11:27:49 +0000131 CLBuildOptions build_opts;
Manuel Bottini7b9998d2019-10-21 17:59:07 +0100132
133 build_opts.add_option_if(_prev_output != nullptr, "-DPREV_OUTPUT");
134 build_opts.add_option("-DDATA_TYPE=" + get_cl_type_from_data_type(input->info()->data_type()));
Manuel Bottini7b9998d2019-10-21 17:59:07 +0100135 build_opts.add_option_if(is_data_type_float(input->info()->data_type()), "-DFLOAT_DATA_TYPE");
Michalis Spyrou7317e392020-01-17 11:27:49 +0000136 build_opts.add_option_if_else(op == ReductionOperation::ARG_IDX_MAX, "-DARG_MAX", "-DARG_MIN");
Manuel Bottini7b9998d2019-10-21 17:59:07 +0100137 build_opts.add_option("-DDATA_TYPE_OUTPUT=" + get_cl_type_from_data_type(output->info()->data_type()));
Michalis Spyrou7317e392020-01-17 11:27:49 +0000138 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 +0100139
140 // Create kernel
141 cl::NDRange lws_hint = CLKernelLibrary::get().default_ndrange();
142 std::string kernel_axis_name;
143 switch(axis)
144 {
145 case 0:
146 {
147 const ICLTensor *input_for_width = prev_output != nullptr ? _prev_output : _input;
148 build_opts.add_option("-DWIDTH=" + support::cpp11::to_string(input_for_width->info()->dimension(0)));
149
150 kernel_axis_name = "x";
151 lws_hint = create_lws_hint_parallel_implementations(input_for_width->info()->dimension(0), vector_size);
152 }
153 break;
154 case 1:
155 build_opts.add_option("-DHEIGHT=" + support::cpp11::to_string(input->info()->dimension(1)));
156 kernel_axis_name = "y";
157 break;
158 case 2:
159 build_opts.add_option("-DDEPTH=" + support::cpp11::to_string(input->info()->dimension(2)));
160 kernel_axis_name = "z";
161 break;
162 case 3:
163 build_opts.add_option("-DDEPTH=" + support::cpp11::to_string(input->info()->dimension(2)));
164 build_opts.add_option("-DBATCH=" + support::cpp11::to_string(input->info()->dimension(3)));
165 kernel_axis_name = "w";
166 break;
167 default:
168 ARM_COMPUTE_ERROR("Not supported");
169 }
170 _kernel = static_cast<cl::Kernel>(CLKernelLibrary::get().create_kernel("arg_min_max_" + kernel_axis_name, build_opts.options()));
171
172 // Configure kernel window
173 ICLKernel::configure_internal(std::get<1>(win_config), lws_hint);
174}
175
176Status CLArgMinMaxLayerKernel::validate(const ITensorInfo *input, const ITensorInfo *prev_output, const ITensorInfo *output, unsigned int axis, ReductionOperation op)
177{
178 ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments(input, prev_output, output, axis, op));
179 ARM_COMPUTE_RETURN_ON_ERROR(std::get<0>(validate_and_configure_window(input->clone().get(), (prev_output != nullptr) ? prev_output->clone().get() : nullptr, output->clone().get(), axis, op)));
180 return Status{};
181}
182
183void CLArgMinMaxLayerKernel::run(const Window &window, cl::CommandQueue &queue)
184{
185 ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this);
186 ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(IKernel::window(), window);
187
188 switch(_reduction_axis)
189 {
190 case 0:
191 {
192 // Set out window
193 Window out_window(window);
194 out_window.set(Window::DimX, Window::Dimension(0, 0, 0));
195
196 // Get first input and output slices
197 Window in_slice = window.first_slice_window_2D();
198 Window out_slice = out_window.first_slice_window_2D();
199
200 // Reshape window
201 const unsigned int border_width = ((in_slice.x().end() % vector_size) != 0) ? vector_size - in_slice.x().end() % vector_size : 0;
202 in_slice.set(Window::DimX, Window::Dimension(in_slice.x().start(), in_slice.x().end() + border_width, in_slice.x().step()));
203 const unsigned int num_tensors = _prev_output != nullptr ? 3 : 2;
204
205 // Set local sums buffer
206 unsigned int local_res_size = lws_hint()[0] * _output->info()->element_size();
207 _kernel.setArg(num_arguments_per_2D_tensor() * num_tensors, local_res_size, nullptr);
208 do
209 {
210 unsigned int idx = 0;
211 add_2D_tensor_argument(idx, _input, in_slice);
212 if(_prev_output != nullptr)
213 {
214 add_2D_tensor_argument(idx, _prev_output, in_slice);
215 }
216 add_2D_tensor_argument(idx, _output, out_slice);
217 enqueue(queue, *this, in_slice, lws_hint());
218 }
219 while(window.slide_window_slice_2D(in_slice) && window.slide_window_slice_2D(out_slice));
220 }
221 break;
222 case 1:
223 {
224 // Get first input and output slices
225 Window window_in{ window };
226 window_in.set(Window::DimY, Window::Dimension(0, _input->info()->dimension(1), _input->info()->dimension(1)));
227 Window in_slice = window_in.first_slice_window_2D();
228 Window out_slice = window.first_slice_window_2D();
229
230 do
231 {
232 unsigned int idx = 0;
233 add_2D_tensor_argument(idx, _input, in_slice);
234 add_2D_tensor_argument(idx, _output, out_slice);
235 enqueue(queue, *this, in_slice, lws_hint());
236 }
237 while(window_in.slide_window_slice_2D(in_slice) && window.slide_window_slice_2D(out_slice));
238 }
239 break;
240 case 2:
241 {
242 // Get first input and output slices
243 Window window_in{ window };
244 window_in.set(Window::DimZ, Window::Dimension(0, _input->info()->dimension(2), _input->info()->dimension(2)));
245 Window in_slice = window_in.first_slice_window_3D();
246 Window out_slice = window.first_slice_window_3D();
247
248 do
249 {
250 unsigned int idx = 0;
251 add_3D_tensor_argument(idx, _input, in_slice);
252 add_3D_tensor_argument(idx, _output, out_slice);
253 enqueue(queue, *this, in_slice, lws_hint());
254 }
255 while(window_in.slide_window_slice_3D(in_slice) && window.slide_window_slice_3D(out_slice));
256 }
257 break;
258 case 3:
259 {
260 // Get first input and output slices
261 Window window_in{ window };
262 window_in.set(3, Window::Dimension(0, 1, 1));
263 Window in_slice = window_in.first_slice_window_4D();
264 Window out_slice = window.first_slice_window_4D();
265
266 do
267 {
268 unsigned int idx = 0;
269 add_4D_tensor_argument(idx, _input, in_slice);
270 add_4D_tensor_argument(idx, _output, out_slice);
271 enqueue(queue, *this, in_slice, lws_hint());
272 }
273 while(window_in.slide_window_slice_4D(in_slice) && window.slide_window_slice_4D(out_slice));
274 }
275 break;
276 default:
277 ARM_COMPUTE_ERROR("Not supported");
278 }
279}
280} // namespace arm_compute