blob: c8e87ba5cedcc830de6da672593da8bccfa36d78 [file] [log] [blame]
Manuel Bottini7b9998d2019-10-21 17:59:07 +01001/*
2 * Copyright (c) 2019 ARM Limited.
3 *
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);
49 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::S32, DataType::F16, DataType::F32);
50 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
131 CLBuildOptions build_opts;
132 const std::string data_type_promoted = get_cl_type_from_data_type(input->info()->data_type());
133
134 build_opts.add_option_if(_prev_output != nullptr, "-DPREV_OUTPUT");
135 build_opts.add_option("-DDATA_TYPE=" + get_cl_type_from_data_type(input->info()->data_type()));
136 build_opts.add_option("-DDATA_TYPE_PROMOTED=" + data_type_promoted);
137 build_opts.add_option_if(is_data_type_float(input->info()->data_type()), "-DFLOAT_DATA_TYPE");
138 build_opts.add_option_if(op == ReductionOperation::ARG_IDX_MAX, "-DARG_MAX");
139 build_opts.add_option_if(op == ReductionOperation::ARG_IDX_MIN, "-DARG_MIN");
140 build_opts.add_option("-DCOND_DATA_TYPE=" + get_cl_select_type_from_data_type(input->info()->data_type()));
141 build_opts.add_option("-DDATA_TYPE_OUTPUT=" + get_cl_type_from_data_type(output->info()->data_type()));
142
143 // Create kernel
144 cl::NDRange lws_hint = CLKernelLibrary::get().default_ndrange();
145 std::string kernel_axis_name;
146 switch(axis)
147 {
148 case 0:
149 {
150 const ICLTensor *input_for_width = prev_output != nullptr ? _prev_output : _input;
151 build_opts.add_option("-DWIDTH=" + support::cpp11::to_string(input_for_width->info()->dimension(0)));
152
153 kernel_axis_name = "x";
154 lws_hint = create_lws_hint_parallel_implementations(input_for_width->info()->dimension(0), vector_size);
155 }
156 break;
157 case 1:
158 build_opts.add_option("-DHEIGHT=" + support::cpp11::to_string(input->info()->dimension(1)));
159 kernel_axis_name = "y";
160 break;
161 case 2:
162 build_opts.add_option("-DDEPTH=" + support::cpp11::to_string(input->info()->dimension(2)));
163 kernel_axis_name = "z";
164 break;
165 case 3:
166 build_opts.add_option("-DDEPTH=" + support::cpp11::to_string(input->info()->dimension(2)));
167 build_opts.add_option("-DBATCH=" + support::cpp11::to_string(input->info()->dimension(3)));
168 kernel_axis_name = "w";
169 break;
170 default:
171 ARM_COMPUTE_ERROR("Not supported");
172 }
173 _kernel = static_cast<cl::Kernel>(CLKernelLibrary::get().create_kernel("arg_min_max_" + kernel_axis_name, build_opts.options()));
174
175 // Configure kernel window
176 ICLKernel::configure_internal(std::get<1>(win_config), lws_hint);
177}
178
179Status CLArgMinMaxLayerKernel::validate(const ITensorInfo *input, const ITensorInfo *prev_output, const ITensorInfo *output, unsigned int axis, ReductionOperation op)
180{
181 ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments(input, prev_output, output, axis, op));
182 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)));
183 return Status{};
184}
185
186void CLArgMinMaxLayerKernel::run(const Window &window, cl::CommandQueue &queue)
187{
188 ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this);
189 ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(IKernel::window(), window);
190
191 switch(_reduction_axis)
192 {
193 case 0:
194 {
195 // Set out window
196 Window out_window(window);
197 out_window.set(Window::DimX, Window::Dimension(0, 0, 0));
198
199 // Get first input and output slices
200 Window in_slice = window.first_slice_window_2D();
201 Window out_slice = out_window.first_slice_window_2D();
202
203 // Reshape window
204 const unsigned int border_width = ((in_slice.x().end() % vector_size) != 0) ? vector_size - in_slice.x().end() % vector_size : 0;
205 in_slice.set(Window::DimX, Window::Dimension(in_slice.x().start(), in_slice.x().end() + border_width, in_slice.x().step()));
206 const unsigned int num_tensors = _prev_output != nullptr ? 3 : 2;
207
208 // Set local sums buffer
209 unsigned int local_res_size = lws_hint()[0] * _output->info()->element_size();
210 _kernel.setArg(num_arguments_per_2D_tensor() * num_tensors, local_res_size, nullptr);
211 do
212 {
213 unsigned int idx = 0;
214 add_2D_tensor_argument(idx, _input, in_slice);
215 if(_prev_output != nullptr)
216 {
217 add_2D_tensor_argument(idx, _prev_output, in_slice);
218 }
219 add_2D_tensor_argument(idx, _output, out_slice);
220 enqueue(queue, *this, in_slice, lws_hint());
221 }
222 while(window.slide_window_slice_2D(in_slice) && window.slide_window_slice_2D(out_slice));
223 }
224 break;
225 case 1:
226 {
227 // Get first input and output slices
228 Window window_in{ window };
229 window_in.set(Window::DimY, Window::Dimension(0, _input->info()->dimension(1), _input->info()->dimension(1)));
230 Window in_slice = window_in.first_slice_window_2D();
231 Window out_slice = window.first_slice_window_2D();
232
233 do
234 {
235 unsigned int idx = 0;
236 add_2D_tensor_argument(idx, _input, in_slice);
237 add_2D_tensor_argument(idx, _output, out_slice);
238 enqueue(queue, *this, in_slice, lws_hint());
239 }
240 while(window_in.slide_window_slice_2D(in_slice) && window.slide_window_slice_2D(out_slice));
241 }
242 break;
243 case 2:
244 {
245 // Get first input and output slices
246 Window window_in{ window };
247 window_in.set(Window::DimZ, Window::Dimension(0, _input->info()->dimension(2), _input->info()->dimension(2)));
248 Window in_slice = window_in.first_slice_window_3D();
249 Window out_slice = window.first_slice_window_3D();
250
251 do
252 {
253 unsigned int idx = 0;
254 add_3D_tensor_argument(idx, _input, in_slice);
255 add_3D_tensor_argument(idx, _output, out_slice);
256 enqueue(queue, *this, in_slice, lws_hint());
257 }
258 while(window_in.slide_window_slice_3D(in_slice) && window.slide_window_slice_3D(out_slice));
259 }
260 break;
261 case 3:
262 {
263 // Get first input and output slices
264 Window window_in{ window };
265 window_in.set(3, Window::Dimension(0, 1, 1));
266 Window in_slice = window_in.first_slice_window_4D();
267 Window out_slice = window.first_slice_window_4D();
268
269 do
270 {
271 unsigned int idx = 0;
272 add_4D_tensor_argument(idx, _input, in_slice);
273 add_4D_tensor_argument(idx, _output, out_slice);
274 enqueue(queue, *this, in_slice, lws_hint());
275 }
276 while(window_in.slide_window_slice_4D(in_slice) && window.slide_window_slice_4D(out_slice));
277 }
278 break;
279 default:
280 ARM_COMPUTE_ERROR("Not supported");
281 }
282}
283} // namespace arm_compute