2 * Copyright (c) 2020 Samsung Electronics Co., Ltd. All Rights Reserved
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
18 * Copyright (c) 2019-2020 ARM Limited.
20 * SPDX-License-Identifier: MIT
22 * Permission is hereby granted, free of charge, to any person obtaining a copy
23 * of this software and associated documentation files (the "Software"), to
24 * deal in the Software without restriction, including without limitation the
25 * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
26 * sell copies of the Software, and to permit persons to whom the Software is
27 * furnished to do so, subject to the following conditions:
29 * The above copyright notice and this permission notice shall be included in all
30 * copies or substantial portions of the Software.
32 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
33 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
34 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
35 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
36 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
37 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
40 #include "arm_compute/core/CL/kernels/CLArgMinMaxLayerKernelEx.h"
42 #include "arm_compute/core/AccessWindowStatic.h"
43 #include "arm_compute/core/CL/CLHelpers.h"
44 #include "arm_compute/core/CL/CLKernelLibraryEx.h"
45 #include "arm_compute/core/CL/CLValidate.h"
46 #include "arm_compute/core/CL/ICLTensor.h"
47 #include "arm_compute/core/Helpers.h"
48 #include "arm_compute/core/TensorInfo.h"
49 #include "arm_compute/core/Utils.h"
50 #include "arm_compute/core/Validate.h"
51 #include "arm_compute/core/Window.h"
53 #include "support/StringSupport.h"
59 constexpr unsigned int vector_size = 16;
61 Status validate_arguments(const ITensorInfo *input, const ITensorInfo *prev_output,
62 const ITensorInfo *output, unsigned int axis, ReductionOperation op)
64 ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(input, output);
65 ARM_COMPUTE_RETURN_ERROR_ON_F16_UNSUPPORTED(input);
66 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::QASYMM8, DataType::S32,
67 DataType::F16, DataType::F32);
68 ARM_COMPUTE_RETURN_ERROR_ON_MSG(op != ReductionOperation::ARG_IDX_MAX &&
69 op != ReductionOperation::ARG_IDX_MIN,
70 "Only ARG_IDX_MAX and ARG_IDX_MIN are supported");
71 ARM_COMPUTE_RETURN_ERROR_ON_MSG(axis >= TensorShape::num_max_dimensions,
72 "Reduction axis greater than max number of dimensions");
73 ARM_COMPUTE_RETURN_ERROR_ON_MSG(axis > 3, "Unsupported reduction axis");
75 if (output->total_size() != 0)
77 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(output, 1, DataType::U32, DataType::S32,
80 if (prev_output != nullptr && prev_output->total_size() != 0)
82 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(prev_output, 1, DataType::U32,
83 DataType::S32, DataType::S64);
84 if (output->total_size() != 0)
86 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(prev_output, output);
93 std::tuple<Status, Window> validate_and_configure_window(ITensorInfo *input,
94 ITensorInfo *prev_output,
95 ITensorInfo *output, unsigned int axis,
96 ReductionOperation op)
98 ARM_COMPUTE_UNUSED(op);
99 // Output tensor auto initialization if not yet initialized
100 TensorShape output_shape{input->tensor_shape()};
101 output_shape.set(axis, 1);
102 DataType output_data_type = (prev_output != nullptr) ? (prev_output->data_type()) : DataType::S32;
103 auto_init_if_empty(*output, input->clone()
104 ->set_tensor_shape(output_shape)
105 .set_data_type(output_data_type)
107 .set_is_resizable(true));
109 Window win = calculate_max_window((prev_output != nullptr) ? (*prev_output) : (*input),
111 bool window_changed = false;
117 ITensorInfo *input_tensor_access = prev_output != nullptr ? prev_output : input;
118 AccessWindowStatic input_access(input_tensor_access, 0, 0,
119 static_cast<int>(input_tensor_access->dimension(0)), 1);
120 AccessWindowHorizontal output_access(output, 0, 1);
121 window_changed = update_window_and_padding(win, input_access, output_access);
122 output_access.set_valid_region(win, ValidRegion(Coordinates(), output->tensor_shape()));
129 AccessWindowHorizontal input_access(input, 0, vector_size);
130 AccessWindowHorizontal output_access(output, 0, vector_size);
131 window_changed = update_window_and_padding(win, input_access, output_access);
132 output_access.set_valid_region(win, ValidRegion(Coordinates(), output->tensor_shape()));
136 ARM_COMPUTE_ERROR("Not supported");
139 Status err = (window_changed)
140 ? ARM_COMPUTE_CREATE_ERROR(ErrorCode::RUNTIME_ERROR, "Insufficient Padding!")
142 return std::make_tuple(err, win);
146 CLArgMinMaxLayerKernelEx::CLArgMinMaxLayerKernelEx()
147 : _input(nullptr), _prev_output(nullptr), _output(nullptr), _reduction_axis(0),
148 _op(ReductionOperation::ARG_IDX_MAX)
152 void CLArgMinMaxLayerKernelEx::configure(const ICLTensor *input, const ICLTensor *prev_output,
153 ICLTensor *output, unsigned int axis,
154 ReductionOperation op)
156 ARM_COMPUTE_ERROR_ON_NULLPTR(input, output);
157 ARM_COMPUTE_ERROR_THROW_ON(
158 validate_arguments(input->info(), (prev_output != nullptr) ? prev_output->info() : nullptr,
159 output->info(), axis, op));
160 auto win_config = validate_and_configure_window(
161 input->info(), (prev_output != nullptr) ? prev_output->info() : nullptr, output->info(), axis,
163 ARM_COMPUTE_ERROR_THROW_ON(std::get<0>(win_config));
166 _prev_output = prev_output;
168 _reduction_axis = axis;
172 CLBuildOptions build_opts;
174 build_opts.add_option_if(_prev_output != nullptr, "-DPREV_OUTPUT");
175 build_opts.add_option("-DDATA_TYPE=" + get_cl_type_from_data_type(input->info()->data_type()));
176 build_opts.add_option_if(is_data_type_float(input->info()->data_type()), "-DFLOAT_DATA_TYPE");
177 build_opts.add_option_if_else(op == ReductionOperation::ARG_IDX_MAX, "-DARG_MAX", "-DARG_MIN");
178 build_opts.add_option("-DDATA_TYPE_OUTPUT=" +
179 get_cl_type_from_data_type(output->info()->data_type()));
180 build_opts.add_option("-DDATA_TYPE_SELECT=" +
181 get_cl_signed_type_from_element_size(input->info()->element_size()));
184 cl::NDRange lws_hint = CLKernelLibrary::get().default_ndrange();
185 std::string kernel_axis_name;
190 const ICLTensor *input_for_width = prev_output != nullptr ? _prev_output : _input;
191 build_opts.add_option("-DWIDTH=" +
192 support::cpp11::to_string(input_for_width->info()->dimension(0)));
194 kernel_axis_name = "x";
195 lws_hint = create_lws_hint_parallel_implementations(input_for_width->info()->dimension(0),
200 build_opts.add_option("-DHEIGHT=" + support::cpp11::to_string(input->info()->dimension(1)));
201 kernel_axis_name = "y";
204 build_opts.add_option("-DDEPTH=" + support::cpp11::to_string(input->info()->dimension(2)));
205 kernel_axis_name = "z";
208 build_opts.add_option("-DDEPTH=" + support::cpp11::to_string(input->info()->dimension(2)));
209 build_opts.add_option("-DBATCH=" + support::cpp11::to_string(input->info()->dimension(3)));
210 kernel_axis_name = "w";
213 ARM_COMPUTE_ERROR("Not supported");
215 _kernel = static_cast<cl::Kernel>(CLKernelLibraryEx::get().create_kernel(
216 "arg_min_max_ex_" + kernel_axis_name, build_opts.options()));
218 // Configure kernel window
219 ICLKernel::configure_internal(std::get<1>(win_config), lws_hint);
222 Status CLArgMinMaxLayerKernelEx::validate(const ITensorInfo *input, const ITensorInfo *prev_output,
223 const ITensorInfo *output, unsigned int axis,
224 ReductionOperation op)
226 ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments(input, prev_output, output, axis, op));
227 ARM_COMPUTE_RETURN_ON_ERROR(std::get<0>(validate_and_configure_window(
228 input->clone().get(), (prev_output != nullptr) ? prev_output->clone().get() : nullptr,
229 output->clone().get(), axis, op)));
233 void CLArgMinMaxLayerKernelEx::run(const Window &window, cl::CommandQueue &queue)
235 ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this);
236 ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(IKernel::window(), window);
238 switch (_reduction_axis)
243 Window out_window(window);
244 out_window.set(Window::DimX, Window::Dimension(0, 0, 0));
246 // Get first input and output slices
247 Window in_slice = window.first_slice_window_2D();
248 Window out_slice = out_window.first_slice_window_2D();
251 const unsigned int num_tensors = _prev_output != nullptr ? 3 : 2;
253 // Set local sums buffer
254 unsigned int local_res_size = lws_hint()[0] * _output->info()->element_size();
255 _kernel.setArg(num_arguments_per_2D_tensor() * num_tensors, local_res_size, nullptr);
258 unsigned int idx = 0;
259 add_2D_tensor_argument(idx, _input, in_slice);
260 if (_prev_output != nullptr)
262 add_2D_tensor_argument(idx, _prev_output, in_slice);
264 add_2D_tensor_argument(idx, _output, out_slice);
265 enqueue(queue, *this, in_slice, lws_hint());
266 } while (window.slide_window_slice_2D(in_slice) && window.slide_window_slice_2D(out_slice));
271 // Get first input and output slices
272 Window window_in{window};
273 window_in.set(Window::DimY, Window::Dimension(0, _input->info()->dimension(1),
274 _input->info()->dimension(1)));
275 Window in_slice = window_in.first_slice_window_2D();
276 Window out_slice = window.first_slice_window_2D();
280 unsigned int idx = 0;
281 add_2D_tensor_argument(idx, _input, in_slice);
282 add_2D_tensor_argument(idx, _output, out_slice);
283 enqueue(queue, *this, in_slice, lws_hint());
284 } while (window_in.slide_window_slice_2D(in_slice) &&
285 window.slide_window_slice_2D(out_slice));
290 // Get first input and output slices
291 Window window_in{window};
292 window_in.set(Window::DimZ, Window::Dimension(0, _input->info()->dimension(2),
293 _input->info()->dimension(2)));
294 Window in_slice = window_in.first_slice_window_3D();
295 Window out_slice = window.first_slice_window_3D();
299 unsigned int idx = 0;
300 add_3D_tensor_argument(idx, _input, in_slice);
301 add_3D_tensor_argument(idx, _output, out_slice);
302 enqueue(queue, *this, in_slice, lws_hint());
303 } while (window_in.slide_window_slice_3D(in_slice) &&
304 window.slide_window_slice_3D(out_slice));
309 // Get first input and output slices
310 Window window_in{window};
311 window_in.set(3, Window::Dimension(0, 1, 1));
312 Window in_slice = window_in.first_slice_window_4D();
313 Window out_slice = window.first_slice_window_4D();
317 unsigned int idx = 0;
318 add_4D_tensor_argument(idx, _input, in_slice);
319 add_4D_tensor_argument(idx, _output, out_slice);
320 enqueue(queue, *this, in_slice, lws_hint());
321 } while (window_in.slide_window_slice_4D(in_slice) &&
322 window.slide_window_slice_4D(out_slice));
326 ARM_COMPUTE_ERROR("Not supported");
329 } // namespace arm_compute