Manuel Bottini | 7b9998d | 2019-10-21 17:59:07 +0100 | [diff] [blame] | 1 | /* |
Pablo Marquez Tello | 9b392d7 | 2023-06-27 15:49:50 +0100 | [diff] [blame] | 2 | * Copyright (c) 2019-2021, 2023 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 | */ |
Sang-Hoon Park | bef7fa2 | 2020-10-21 15:58:54 +0100 | [diff] [blame] | 24 | #include "src/core/CL/kernels/CLArgMinMaxLayerKernel.h" |
Manuel Bottini | 7b9998d | 2019-10-21 17:59:07 +0100 | [diff] [blame] | 25 | |
Manuel Bottini | 7b9998d | 2019-10-21 17:59:07 +0100 | [diff] [blame] | 26 | #include "arm_compute/core/CL/CLHelpers.h" |
| 27 | #include "arm_compute/core/CL/CLKernelLibrary.h" |
Manuel Bottini | 7b9998d | 2019-10-21 17:59:07 +0100 | [diff] [blame] | 28 | #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 Park | 68dd25f | 2020-10-19 16:00:11 +0100 | [diff] [blame] | 33 | #include "src/core/CL/CLValidate.h" |
| 34 | #include "src/core/helpers/AutoConfiguration.h" |
| 35 | #include "src/core/helpers/WindowHelpers.h" |
Matthew Bentham | 758b5ba | 2020-03-05 23:37:48 +0000 | [diff] [blame] | 36 | #include "support/StringSupport.h" |
Manuel Bottini | 7b9998d | 2019-10-21 17:59:07 +0100 | [diff] [blame] | 37 | |
| 38 | namespace arm_compute |
| 39 | { |
| 40 | namespace |
| 41 | { |
Pablo Marquez Tello | 9b392d7 | 2023-06-27 15:49:50 +0100 | [diff] [blame] | 42 | Status validate_arguments(const ITensorInfo *input, const ITensorInfo *output, unsigned int axis, ReductionOperation op) |
Manuel Bottini | 7b9998d | 2019-10-21 17:59:07 +0100 | [diff] [blame] | 43 | { |
| 44 | ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(input, output); |
| 45 | ARM_COMPUTE_RETURN_ERROR_ON_F16_UNSUPPORTED(input); |
Sheri Zhang | c5b6d88 | 2020-06-26 14:46:59 +0100 | [diff] [blame] | 46 | ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::QASYMM8, DataType::QASYMM8_SIGNED, DataType::S32, DataType::F16, DataType::F32); |
Manuel Bottini | 7b9998d | 2019-10-21 17:59:07 +0100 | [diff] [blame] | 47 | 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"); |
| 48 | ARM_COMPUTE_RETURN_ERROR_ON_MSG(axis >= TensorShape::num_max_dimensions, "Reduction axis greater than max number of dimensions"); |
| 49 | ARM_COMPUTE_RETURN_ERROR_ON_MSG(axis > 3, "Unsupported reduction axis"); |
| 50 | |
| 51 | if(output->total_size() != 0) |
| 52 | { |
| 53 | ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(output, 1, DataType::U32, DataType::S32); |
| 54 | } |
Manuel Bottini | 7b9998d | 2019-10-21 17:59:07 +0100 | [diff] [blame] | 55 | |
| 56 | return Status{}; |
| 57 | } |
Manuel Bottini | 7b9998d | 2019-10-21 17:59:07 +0100 | [diff] [blame] | 58 | } // namespace |
| 59 | |
| 60 | CLArgMinMaxLayerKernel::CLArgMinMaxLayerKernel() |
Pablo Marquez Tello | 9b392d7 | 2023-06-27 15:49:50 +0100 | [diff] [blame] | 61 | : _input(nullptr), _output(nullptr), _reduction_axis(0), _op(ReductionOperation::ARG_IDX_MAX) |
Manuel Bottini | 7b9998d | 2019-10-21 17:59:07 +0100 | [diff] [blame] | 62 | { |
Giorgio Arena | 4a95bba | 2021-06-28 11:00:27 +0100 | [diff] [blame] | 63 | _type = CLKernelType::ELEMENTWISE; |
Manuel Bottini | 7b9998d | 2019-10-21 17:59:07 +0100 | [diff] [blame] | 64 | } |
| 65 | |
Pablo Marquez Tello | 9b392d7 | 2023-06-27 15:49:50 +0100 | [diff] [blame] | 66 | void CLArgMinMaxLayerKernel::configure(const ICLTensor *input, ICLTensor *output, unsigned int axis, ReductionOperation op) |
Manuel Bottini | 7b9998d | 2019-10-21 17:59:07 +0100 | [diff] [blame] | 67 | { |
Pablo Marquez Tello | 9b392d7 | 2023-06-27 15:49:50 +0100 | [diff] [blame] | 68 | configure(CLKernelLibrary::get().get_compile_context(), input, output, axis, op); |
Manuel Bottini | 4c6bd51 | 2020-04-08 10:15:51 +0100 | [diff] [blame] | 69 | } |
| 70 | |
Pablo Marquez Tello | 9b392d7 | 2023-06-27 15:49:50 +0100 | [diff] [blame] | 71 | void CLArgMinMaxLayerKernel::configure(const CLCompileContext &compile_context, const ICLTensor *input, ICLTensor *output, unsigned int axis, ReductionOperation op) |
Manuel Bottini | 4c6bd51 | 2020-04-08 10:15:51 +0100 | [diff] [blame] | 72 | { |
Manuel Bottini | 7b9998d | 2019-10-21 17:59:07 +0100 | [diff] [blame] | 73 | ARM_COMPUTE_ERROR_ON_NULLPTR(input, output); |
Giorgio Arena | 3d3a01c | 2021-01-11 15:31:15 +0000 | [diff] [blame] | 74 | |
| 75 | TensorShape output_shape{ input->info()->tensor_shape() }; |
| 76 | output_shape.set(axis, 1); |
| 77 | 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)); |
| 78 | |
Pablo Marquez Tello | 9b392d7 | 2023-06-27 15:49:50 +0100 | [diff] [blame] | 79 | ARM_COMPUTE_ERROR_THROW_ON(validate_arguments(input->info(), output->info(), axis, op)); |
Giorgio Arena | 3d3a01c | 2021-01-11 15:31:15 +0000 | [diff] [blame] | 80 | |
Pablo Marquez Tello | 9b392d7 | 2023-06-27 15:49:50 +0100 | [diff] [blame] | 81 | auto padding_info = get_padding_info({ input, output }); |
Manuel Bottini | 7b9998d | 2019-10-21 17:59:07 +0100 | [diff] [blame] | 82 | |
| 83 | _input = input; |
Manuel Bottini | 7b9998d | 2019-10-21 17:59:07 +0100 | [diff] [blame] | 84 | _output = output; |
| 85 | _reduction_axis = axis; |
| 86 | _op = op; |
| 87 | |
| 88 | // Set build options |
Pablo Marquez Tello | 4c0a38a | 2023-07-10 15:23:44 +0100 | [diff] [blame^] | 89 | const auto adjusted_vector_size = adjust_vec_size(16U, input->info()->dimension(0)); |
| 90 | const auto vector_size = (adjusted_vector_size == 3U && axis == 0U) ? 2U : adjusted_vector_size; // the opencl kernel only supports sizes 2, 4, 8 and 16. |
| 91 | |
Giorgio Arena | 3d3a01c | 2021-01-11 15:31:15 +0000 | [diff] [blame] | 92 | CLBuildOptions build_opts; |
Manuel Bottini | 7b9998d | 2019-10-21 17:59:07 +0100 | [diff] [blame] | 93 | build_opts.add_option("-DDATA_TYPE=" + get_cl_type_from_data_type(input->info()->data_type())); |
Giorgio Arena | 3d3a01c | 2021-01-11 15:31:15 +0000 | [diff] [blame] | 94 | build_opts.add_option("-DVEC_SIZE_LEFTOVER=" + support::cpp11::to_string(input->info()->dimension(0) % vector_size)); |
| 95 | build_opts.add_option("-DVEC_SIZE=" + support::cpp11::to_string(vector_size)); |
Manuel Bottini | 7b9998d | 2019-10-21 17:59:07 +0100 | [diff] [blame] | 96 | 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] | 97 | 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] | 98 | build_opts.add_option("-DDATA_TYPE_OUTPUT=" + get_cl_type_from_data_type(output->info()->data_type())); |
Pablo Marquez Tello | 9b392d7 | 2023-06-27 15:49:50 +0100 | [diff] [blame] | 99 | build_opts.add_option("-DCOND_DATA_TYPE=" + get_cl_select_type_from_data_type(input->info()->data_type())); |
| 100 | build_opts.add_option("-DUNROLL_WITH_PRAGMA=1"); |
Manuel Bottini | 7b9998d | 2019-10-21 17:59:07 +0100 | [diff] [blame] | 101 | |
| 102 | // Create kernel |
Manuel Bottini | 7b9998d | 2019-10-21 17:59:07 +0100 | [diff] [blame] | 103 | std::string kernel_axis_name; |
| 104 | switch(axis) |
| 105 | { |
| 106 | case 0: |
Pablo Marquez Tello | 9b392d7 | 2023-06-27 15:49:50 +0100 | [diff] [blame] | 107 | build_opts.add_option("-DWIDTH=" + support::cpp11::to_string(input->info()->dimension(0))); |
Manuel Bottini | 7b9998d | 2019-10-21 17:59:07 +0100 | [diff] [blame] | 108 | kernel_axis_name = "x"; |
Pablo Marquez Tello | 9b392d7 | 2023-06-27 15:49:50 +0100 | [diff] [blame] | 109 | break; |
Manuel Bottini | 7b9998d | 2019-10-21 17:59:07 +0100 | [diff] [blame] | 110 | case 1: |
| 111 | build_opts.add_option("-DHEIGHT=" + support::cpp11::to_string(input->info()->dimension(1))); |
| 112 | kernel_axis_name = "y"; |
| 113 | break; |
| 114 | case 2: |
| 115 | build_opts.add_option("-DDEPTH=" + support::cpp11::to_string(input->info()->dimension(2))); |
| 116 | kernel_axis_name = "z"; |
| 117 | break; |
| 118 | case 3: |
| 119 | build_opts.add_option("-DDEPTH=" + support::cpp11::to_string(input->info()->dimension(2))); |
| 120 | build_opts.add_option("-DBATCH=" + support::cpp11::to_string(input->info()->dimension(3))); |
| 121 | kernel_axis_name = "w"; |
| 122 | break; |
| 123 | default: |
| 124 | ARM_COMPUTE_ERROR("Not supported"); |
| 125 | } |
Manuel Bottini | 4c6bd51 | 2020-04-08 10:15:51 +0100 | [diff] [blame] | 126 | _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] | 127 | |
| 128 | // Configure kernel window |
Pablo Marquez Tello | 9b392d7 | 2023-06-27 15:49:50 +0100 | [diff] [blame] | 129 | Window win = calculate_max_window(*input->info(), Steps(vector_size)); |
| 130 | ICLKernel::configure_internal(win); |
Giorgio Arena | 3d3a01c | 2021-01-11 15:31:15 +0000 | [diff] [blame] | 131 | |
| 132 | ARM_COMPUTE_ERROR_ON(has_padding_changed(padding_info)); |
Manuel Bottini | 7b9998d | 2019-10-21 17:59:07 +0100 | [diff] [blame] | 133 | } |
| 134 | |
Pablo Marquez Tello | 9b392d7 | 2023-06-27 15:49:50 +0100 | [diff] [blame] | 135 | Status CLArgMinMaxLayerKernel::validate(const ITensorInfo *input, const ITensorInfo *output, unsigned int axis, ReductionOperation op) |
Manuel Bottini | 7b9998d | 2019-10-21 17:59:07 +0100 | [diff] [blame] | 136 | { |
Pablo Marquez Tello | 9b392d7 | 2023-06-27 15:49:50 +0100 | [diff] [blame] | 137 | ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments(input, output, axis, op)); |
Manuel Bottini | 7b9998d | 2019-10-21 17:59:07 +0100 | [diff] [blame] | 138 | return Status{}; |
| 139 | } |
| 140 | |
| 141 | void CLArgMinMaxLayerKernel::run(const Window &window, cl::CommandQueue &queue) |
| 142 | { |
| 143 | ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this); |
| 144 | ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(IKernel::window(), window); |
| 145 | |
| 146 | switch(_reduction_axis) |
| 147 | { |
| 148 | case 0: |
| 149 | { |
| 150 | // Set out window |
| 151 | Window out_window(window); |
Pablo Marquez Tello | 9b392d7 | 2023-06-27 15:49:50 +0100 | [diff] [blame] | 152 | Window in_window(window); |
Manuel Bottini | 7b9998d | 2019-10-21 17:59:07 +0100 | [diff] [blame] | 153 | out_window.set(Window::DimX, Window::Dimension(0, 0, 0)); |
Pablo Marquez Tello | 9b392d7 | 2023-06-27 15:49:50 +0100 | [diff] [blame] | 154 | in_window.set(Window::DimX, Window::Dimension(0, _input->info()->dimension(0), _input->info()->dimension(0))); |
| 155 | in_window.set(Window::DimY, Window::Dimension(0, _input->info()->dimension(1), 1u)); |
Manuel Bottini | 7b9998d | 2019-10-21 17:59:07 +0100 | [diff] [blame] | 156 | |
| 157 | // Get first input and output slices |
Pablo Marquez Tello | 9b392d7 | 2023-06-27 15:49:50 +0100 | [diff] [blame] | 158 | Window in_slice = in_window.first_slice_window_2D(); |
Manuel Bottini | 7b9998d | 2019-10-21 17:59:07 +0100 | [diff] [blame] | 159 | Window out_slice = out_window.first_slice_window_2D(); |
Manuel Bottini | 7b9998d | 2019-10-21 17:59:07 +0100 | [diff] [blame] | 160 | do |
| 161 | { |
| 162 | unsigned int idx = 0; |
| 163 | add_2D_tensor_argument(idx, _input, in_slice); |
Manuel Bottini | 7b9998d | 2019-10-21 17:59:07 +0100 | [diff] [blame] | 164 | add_2D_tensor_argument(idx, _output, out_slice); |
| 165 | enqueue(queue, *this, in_slice, lws_hint()); |
| 166 | } |
Pablo Marquez Tello | 9b392d7 | 2023-06-27 15:49:50 +0100 | [diff] [blame] | 167 | while(in_window.slide_window_slice_2D(in_slice) && out_window.slide_window_slice_2D(out_slice)); |
Manuel Bottini | 7b9998d | 2019-10-21 17:59:07 +0100 | [diff] [blame] | 168 | } |
| 169 | break; |
| 170 | case 1: |
| 171 | { |
| 172 | // Get first input and output slices |
| 173 | Window window_in{ window }; |
| 174 | window_in.set(Window::DimY, Window::Dimension(0, _input->info()->dimension(1), _input->info()->dimension(1))); |
| 175 | Window in_slice = window_in.first_slice_window_2D(); |
| 176 | Window out_slice = window.first_slice_window_2D(); |
| 177 | |
| 178 | do |
| 179 | { |
| 180 | unsigned int idx = 0; |
| 181 | add_2D_tensor_argument(idx, _input, in_slice); |
| 182 | add_2D_tensor_argument(idx, _output, out_slice); |
| 183 | enqueue(queue, *this, in_slice, lws_hint()); |
| 184 | } |
| 185 | while(window_in.slide_window_slice_2D(in_slice) && window.slide_window_slice_2D(out_slice)); |
| 186 | } |
| 187 | break; |
| 188 | case 2: |
| 189 | { |
| 190 | // Get first input and output slices |
| 191 | Window window_in{ window }; |
| 192 | window_in.set(Window::DimZ, Window::Dimension(0, _input->info()->dimension(2), _input->info()->dimension(2))); |
| 193 | Window in_slice = window_in.first_slice_window_3D(); |
| 194 | Window out_slice = window.first_slice_window_3D(); |
| 195 | |
| 196 | do |
| 197 | { |
| 198 | unsigned int idx = 0; |
| 199 | add_3D_tensor_argument(idx, _input, in_slice); |
| 200 | add_3D_tensor_argument(idx, _output, out_slice); |
| 201 | enqueue(queue, *this, in_slice, lws_hint()); |
| 202 | } |
| 203 | while(window_in.slide_window_slice_3D(in_slice) && window.slide_window_slice_3D(out_slice)); |
| 204 | } |
| 205 | break; |
| 206 | case 3: |
| 207 | { |
| 208 | // Get first input and output slices |
| 209 | Window window_in{ window }; |
| 210 | window_in.set(3, Window::Dimension(0, 1, 1)); |
| 211 | Window in_slice = window_in.first_slice_window_4D(); |
| 212 | Window out_slice = window.first_slice_window_4D(); |
| 213 | |
| 214 | do |
| 215 | { |
| 216 | unsigned int idx = 0; |
| 217 | add_4D_tensor_argument(idx, _input, in_slice); |
| 218 | add_4D_tensor_argument(idx, _output, out_slice); |
| 219 | enqueue(queue, *this, in_slice, lws_hint()); |
| 220 | } |
| 221 | while(window_in.slide_window_slice_4D(in_slice) && window.slide_window_slice_4D(out_slice)); |
| 222 | } |
| 223 | break; |
| 224 | default: |
| 225 | ARM_COMPUTE_ERROR("Not supported"); |
| 226 | } |
| 227 | } |
| 228 | } // namespace arm_compute |