Manuel Bottini | 7b9998d | 2019-10-21 17:59:07 +0100 | [diff] [blame] | 1 | /* |
Michalis Spyrou | 7317e39 | 2020-01-17 11:27:49 +0000 | [diff] [blame] | 2 | * Copyright (c) 2019-2020 ARM Limited. |
Manuel Bottini | 7b9998d | 2019-10-21 17:59:07 +0100 | [diff] [blame] | 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 | |
Matthew Bentham | 758b5ba | 2020-03-05 23:37:48 +0000 | [diff] [blame] | 37 | #include "support/StringSupport.h" |
Manuel Bottini | 7b9998d | 2019-10-21 17:59:07 +0100 | [diff] [blame] | 38 | |
| 39 | namespace arm_compute |
| 40 | { |
| 41 | namespace |
| 42 | { |
| 43 | constexpr unsigned int vector_size = 16; |
| 44 | |
| 45 | Status 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 Spyrou | 7317e39 | 2020-01-17 11:27:49 +0000 | [diff] [blame] | 49 | ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::QASYMM8, DataType::S32, DataType::F16, DataType::F32); |
Manuel Bottini | 7b9998d | 2019-10-21 17:59:07 +0100 | [diff] [blame] | 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 | |
| 70 | std::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 | |
| 112 | CLArgMinMaxLayerKernel::CLArgMinMaxLayerKernel() |
| 113 | : _input(nullptr), _prev_output(nullptr), _output(nullptr), _reduction_axis(0), _op(ReductionOperation::ARG_IDX_MAX) |
| 114 | { |
| 115 | } |
| 116 | |
| 117 | void CLArgMinMaxLayerKernel::configure(const ICLTensor *input, const ICLTensor *prev_output, ICLTensor *output, unsigned int axis, ReductionOperation op) |
| 118 | { |
Manuel Bottini | 4c6bd51 | 2020-04-08 10:15:51 +0100 | [diff] [blame] | 119 | configure(CLKernelLibrary::get().get_compile_context(), input, prev_output, output, axis, op); |
| 120 | } |
| 121 | |
Manuel Bottini | 256c0b9 | 2020-04-21 13:29:30 +0100 | [diff] [blame] | 122 | void CLArgMinMaxLayerKernel::configure(const CLCompileContext &compile_context, const ICLTensor *input, const ICLTensor *prev_output, ICLTensor *output, unsigned int axis, ReductionOperation op) |
Manuel Bottini | 4c6bd51 | 2020-04-08 10:15:51 +0100 | [diff] [blame] | 123 | { |
Manuel Bottini | 7b9998d | 2019-10-21 17:59:07 +0100 | [diff] [blame] | 124 | ARM_COMPUTE_ERROR_ON_NULLPTR(input, output); |
| 125 | ARM_COMPUTE_ERROR_THROW_ON(validate_arguments(input->info(), (prev_output != nullptr) ? prev_output->info() : nullptr, output->info(), axis, op)); |
| 126 | auto win_config = validate_and_configure_window(input->info(), (prev_output != nullptr) ? prev_output->info() : nullptr, output->info(), axis, op); |
| 127 | ARM_COMPUTE_ERROR_THROW_ON(std::get<0>(win_config)); |
| 128 | |
| 129 | _input = input; |
| 130 | _prev_output = prev_output; |
| 131 | _output = output; |
| 132 | _reduction_axis = axis; |
| 133 | _op = op; |
| 134 | |
| 135 | // Set build options |
Michalis Spyrou | 7317e39 | 2020-01-17 11:27:49 +0000 | [diff] [blame] | 136 | CLBuildOptions build_opts; |
Manuel Bottini | 7b9998d | 2019-10-21 17:59:07 +0100 | [diff] [blame] | 137 | |
| 138 | build_opts.add_option_if(_prev_output != nullptr, "-DPREV_OUTPUT"); |
| 139 | build_opts.add_option("-DDATA_TYPE=" + get_cl_type_from_data_type(input->info()->data_type())); |
Manuel Bottini | 7b9998d | 2019-10-21 17:59:07 +0100 | [diff] [blame] | 140 | build_opts.add_option_if(is_data_type_float(input->info()->data_type()), "-DFLOAT_DATA_TYPE"); |
Michalis Spyrou | 7317e39 | 2020-01-17 11:27:49 +0000 | [diff] [blame] | 141 | build_opts.add_option_if_else(op == ReductionOperation::ARG_IDX_MAX, "-DARG_MAX", "-DARG_MIN"); |
Manuel Bottini | 7b9998d | 2019-10-21 17:59:07 +0100 | [diff] [blame] | 142 | build_opts.add_option("-DDATA_TYPE_OUTPUT=" + get_cl_type_from_data_type(output->info()->data_type())); |
Michalis Spyrou | 7317e39 | 2020-01-17 11:27:49 +0000 | [diff] [blame] | 143 | build_opts.add_option("-DDATA_TYPE_SELECT=" + get_cl_signed_type_from_element_size(input->info()->element_size())); |
Manuel Bottini | 7b9998d | 2019-10-21 17:59:07 +0100 | [diff] [blame] | 144 | |
| 145 | // Create kernel |
| 146 | cl::NDRange lws_hint = CLKernelLibrary::get().default_ndrange(); |
| 147 | std::string kernel_axis_name; |
| 148 | switch(axis) |
| 149 | { |
| 150 | case 0: |
| 151 | { |
| 152 | const ICLTensor *input_for_width = prev_output != nullptr ? _prev_output : _input; |
| 153 | build_opts.add_option("-DWIDTH=" + support::cpp11::to_string(input_for_width->info()->dimension(0))); |
| 154 | |
| 155 | kernel_axis_name = "x"; |
| 156 | lws_hint = create_lws_hint_parallel_implementations(input_for_width->info()->dimension(0), vector_size); |
| 157 | } |
| 158 | break; |
| 159 | case 1: |
| 160 | build_opts.add_option("-DHEIGHT=" + support::cpp11::to_string(input->info()->dimension(1))); |
| 161 | kernel_axis_name = "y"; |
| 162 | break; |
| 163 | case 2: |
| 164 | build_opts.add_option("-DDEPTH=" + support::cpp11::to_string(input->info()->dimension(2))); |
| 165 | kernel_axis_name = "z"; |
| 166 | break; |
| 167 | case 3: |
| 168 | build_opts.add_option("-DDEPTH=" + support::cpp11::to_string(input->info()->dimension(2))); |
| 169 | build_opts.add_option("-DBATCH=" + support::cpp11::to_string(input->info()->dimension(3))); |
| 170 | kernel_axis_name = "w"; |
| 171 | break; |
| 172 | default: |
| 173 | ARM_COMPUTE_ERROR("Not supported"); |
| 174 | } |
Manuel Bottini | 4c6bd51 | 2020-04-08 10:15:51 +0100 | [diff] [blame] | 175 | _kernel = create_kernel(compile_context, "arg_min_max_" + kernel_axis_name, build_opts.options()); |
Manuel Bottini | 7b9998d | 2019-10-21 17:59:07 +0100 | [diff] [blame] | 176 | |
| 177 | // Configure kernel window |
| 178 | ICLKernel::configure_internal(std::get<1>(win_config), lws_hint); |
| 179 | } |
| 180 | |
| 181 | Status CLArgMinMaxLayerKernel::validate(const ITensorInfo *input, const ITensorInfo *prev_output, const ITensorInfo *output, unsigned int axis, ReductionOperation op) |
| 182 | { |
| 183 | ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments(input, prev_output, output, axis, op)); |
| 184 | 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))); |
| 185 | return Status{}; |
| 186 | } |
| 187 | |
| 188 | void CLArgMinMaxLayerKernel::run(const Window &window, cl::CommandQueue &queue) |
| 189 | { |
| 190 | ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this); |
| 191 | ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(IKernel::window(), window); |
| 192 | |
| 193 | switch(_reduction_axis) |
| 194 | { |
| 195 | case 0: |
| 196 | { |
| 197 | // Set out window |
| 198 | Window out_window(window); |
| 199 | out_window.set(Window::DimX, Window::Dimension(0, 0, 0)); |
| 200 | |
| 201 | // Get first input and output slices |
| 202 | Window in_slice = window.first_slice_window_2D(); |
| 203 | Window out_slice = out_window.first_slice_window_2D(); |
| 204 | |
| 205 | // Reshape window |
Manuel Bottini | 7b9998d | 2019-10-21 17:59:07 +0100 | [diff] [blame] | 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 |