Michalis Spyrou | 04f089c | 2017-08-08 17:42:38 +0100 | [diff] [blame] | 1 | /* |
Manuel Bottini | b412fab | 2018-12-10 17:40:23 +0000 | [diff] [blame] | 2 | * Copyright (c) 2017-2019 ARM Limited. |
Michalis Spyrou | 04f089c | 2017-08-08 17:42:38 +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/CLReductionOperationKernel.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" |
Georgios Pinitas | 8f5802f | 2019-02-22 11:08:32 +0000 | [diff] [blame] | 29 | #include "arm_compute/core/CL/CLValidate.h" |
Michalis Spyrou | 04f089c | 2017-08-08 17:42:38 +0100 | [diff] [blame] | 30 | #include "arm_compute/core/CL/ICLTensor.h" |
Michalis Spyrou | 04f089c | 2017-08-08 17:42:38 +0100 | [diff] [blame] | 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 | |
| 39 | using namespace arm_compute; |
| 40 | |
John Richardson | 62385bc | 2018-04-20 13:11:36 +0100 | [diff] [blame] | 41 | namespace |
| 42 | { |
Michalis Spyrou | 7e9391b | 2018-10-05 14:49:28 +0100 | [diff] [blame] | 43 | // OpenCL kernel requires input width to be a power of 2 for x-axis. |
Michalis Spyrou | 25747e2 | 2018-08-08 17:12:38 +0100 | [diff] [blame] | 44 | constexpr unsigned int border_val = 64; |
| 45 | |
Michalis Spyrou | 7e9391b | 2018-10-05 14:49:28 +0100 | [diff] [blame] | 46 | Status validate_arguments(const ITensorInfo *input, const ITensorInfo *output, unsigned int axis, ReductionOperation op, unsigned int width) |
John Richardson | 62385bc | 2018-04-20 13:11:36 +0100 | [diff] [blame] | 47 | { |
John Richardson | 62385bc | 2018-04-20 13:11:36 +0100 | [diff] [blame] | 48 | ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(input, output); |
Georgios Pinitas | 8f5802f | 2019-02-22 11:08:32 +0000 | [diff] [blame] | 49 | ARM_COMPUTE_RETURN_ERROR_ON_F16_UNSUPPORTED(input); |
Georgios Pinitas | 8be9148 | 2019-03-26 17:23:28 +0000 | [diff] [blame] | 50 | if(input->num_channels() == 1) |
| 51 | { |
Michalis Spyrou | b9626ab | 2019-05-13 17:41:01 +0100 | [diff] [blame] | 52 | ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::QASYMM8, DataType::S32, DataType::F16, DataType::F32); |
Georgios Pinitas | 8be9148 | 2019-03-26 17:23:28 +0000 | [diff] [blame] | 53 | } |
| 54 | else |
| 55 | { |
| 56 | ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 2, DataType::F32); |
| 57 | } |
Michalis Spyrou | 8aaf93e | 2018-10-11 17:33:32 +0100 | [diff] [blame] | 58 | ARM_COMPUTE_RETURN_ERROR_ON_MSG(op == ReductionOperation::SUM_SQUARE && input->data_type() == DataType::QASYMM8, "Not supported reduction operation for QASYMM8"); |
John Richardson | 62385bc | 2018-04-20 13:11:36 +0100 | [diff] [blame] | 59 | ARM_COMPUTE_RETURN_ERROR_ON_MSG(axis >= TensorShape::num_max_dimensions, "Reduction axis greater than max number of dimensions"); |
Michalis Spyrou | 7e9391b | 2018-10-05 14:49:28 +0100 | [diff] [blame] | 60 | ARM_COMPUTE_RETURN_ERROR_ON_MSG(axis > 3, "Unsupported reduction axis"); |
| 61 | ARM_COMPUTE_RETURN_ERROR_ON(op == ReductionOperation::MEAN_SUM && axis == 0 && width == 0 && input->data_type() != DataType::QASYMM8); |
John Richardson | 62385bc | 2018-04-20 13:11:36 +0100 | [diff] [blame] | 62 | |
| 63 | if(output->total_size() != 0) |
| 64 | { |
Michalis Spyrou | 7930db4 | 2018-11-22 17:36:28 +0000 | [diff] [blame] | 65 | if(op == ReductionOperation::ARG_IDX_MAX || op == ReductionOperation::ARG_IDX_MIN) |
| 66 | { |
| 67 | ARM_COMPUTE_RETURN_ERROR_ON_MSG(input->data_type() == DataType::QASYMM8, "Not supported operation for QASYMM8"); |
| 68 | ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(output, 1, DataType::U32); |
| 69 | } |
| 70 | else |
| 71 | { |
| 72 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input, output); |
Isabella Gottardi | 0a1090a | 2019-02-14 18:07:36 +0000 | [diff] [blame] | 73 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_QUANTIZATION_INFO(input, output); |
Michalis Spyrou | 7930db4 | 2018-11-22 17:36:28 +0000 | [diff] [blame] | 74 | } |
John Richardson | 62385bc | 2018-04-20 13:11:36 +0100 | [diff] [blame] | 75 | } |
| 76 | |
| 77 | return Status{}; |
| 78 | } |
| 79 | |
Michalis Spyrou | 7930db4 | 2018-11-22 17:36:28 +0000 | [diff] [blame] | 80 | std::tuple<Status, Window> validate_and_configure_window(ITensorInfo *input, ITensorInfo *output, unsigned int axis, ReductionOperation op) |
John Richardson | 62385bc | 2018-04-20 13:11:36 +0100 | [diff] [blame] | 81 | { |
| 82 | // Output tensor auto initialization if not yet initialized |
| 83 | TensorShape output_shape{ input->tensor_shape() }; |
| 84 | output_shape.set(axis, 1); |
Michalis Spyrou | 7930db4 | 2018-11-22 17:36:28 +0000 | [diff] [blame] | 85 | const bool is_arg_min_max = (op == ReductionOperation::ARG_IDX_MIN || op == ReductionOperation::ARG_IDX_MAX); |
| 86 | DataType output_data_type = is_arg_min_max ? DataType::U32 : input->data_type(); |
Georgios Pinitas | 8be9148 | 2019-03-26 17:23:28 +0000 | [diff] [blame] | 87 | auto_init_if_empty(*output, input->clone()->set_tensor_shape(output_shape).set_data_type(output_data_type).reset_padding().set_is_resizable(true)); |
John Richardson | 62385bc | 2018-04-20 13:11:36 +0100 | [diff] [blame] | 88 | |
Michalis Spyrou | 7e9391b | 2018-10-05 14:49:28 +0100 | [diff] [blame] | 89 | const unsigned int num_elems_processed_per_iteration = (is_data_type_quantized(input->data_type()) && (axis == 0)) ? 1 : 16; |
| 90 | Window win = calculate_max_window(*input, Steps(num_elems_processed_per_iteration)); |
| 91 | bool window_changed = false; |
Usama Arif | 048b0f3 | 2019-05-22 16:32:27 +0100 | [diff] [blame] | 92 | const bool is_serial_op = (op == ReductionOperation::ARG_IDX_MAX || op == ReductionOperation::ARG_IDX_MIN || op == ReductionOperation::MIN |
| 93 | || op == ReductionOperation::MAX || is_data_type_quantized(input->data_type())); |
John Richardson | 62385bc | 2018-04-20 13:11:36 +0100 | [diff] [blame] | 94 | |
Michalis Spyrou | 7e9391b | 2018-10-05 14:49:28 +0100 | [diff] [blame] | 95 | switch(axis) |
| 96 | { |
| 97 | case 0: |
| 98 | { |
Manuel Bottini | b412fab | 2018-12-10 17:40:23 +0000 | [diff] [blame] | 99 | if(is_serial_op) |
Michalis Spyrou | 7e9391b | 2018-10-05 14:49:28 +0100 | [diff] [blame] | 100 | { |
| 101 | AccessWindowHorizontal input_access(input, 0, input->dimension(0)); |
| 102 | AccessWindowHorizontal output_access(output, 0, 1); |
| 103 | window_changed = update_window_and_padding(win, input_access, output_access); |
| 104 | output_access.set_valid_region(win, ValidRegion(Coordinates(), output->tensor_shape())); |
| 105 | } |
| 106 | else |
| 107 | { |
| 108 | const unsigned int border_width = ((input->dimension(0) % border_val) != 0) ? border_val - input->dimension(0) % border_val : 0; |
| 109 | AccessWindowStatic input_access(input, 0, 0, input->dimension(0) + border_width, 1); |
| 110 | AccessWindowHorizontal output_access(output, 0, 1); |
| 111 | window_changed = update_window_and_padding(win, input_access, output_access); |
| 112 | output_access.set_valid_region(win, ValidRegion(Coordinates(), output->tensor_shape())); |
| 113 | } |
| 114 | } |
| 115 | break; |
| 116 | case 1: |
| 117 | case 2: |
| 118 | case 3: |
| 119 | { |
| 120 | AccessWindowHorizontal input_access(input, 0, num_elems_processed_per_iteration); |
| 121 | AccessWindowHorizontal output_access(output, 0, num_elems_processed_per_iteration); |
| 122 | window_changed = update_window_and_padding(win, input_access, output_access); |
| 123 | output_access.set_valid_region(win, ValidRegion(Coordinates(), output->tensor_shape())); |
| 124 | } |
| 125 | break; |
| 126 | default: |
| 127 | ARM_COMPUTE_ERROR("Not supported"); |
| 128 | } |
John Richardson | 62385bc | 2018-04-20 13:11:36 +0100 | [diff] [blame] | 129 | |
| 130 | Status err = (window_changed) ? ARM_COMPUTE_CREATE_ERROR(ErrorCode::RUNTIME_ERROR, "Insufficient Padding!") : Status{}; |
| 131 | |
| 132 | return std::make_tuple(err, win); |
| 133 | } |
| 134 | } // namespace |
| 135 | |
Michalis Spyrou | 04f089c | 2017-08-08 17:42:38 +0100 | [diff] [blame] | 136 | CLReductionOperationKernel::CLReductionOperationKernel() |
| 137 | : _input(nullptr), _output(nullptr), _reduction_axis(0), _op(ReductionOperation::SUM_SQUARE), _border_size() |
| 138 | { |
| 139 | } |
| 140 | |
| 141 | BorderSize CLReductionOperationKernel::border_size() const |
| 142 | { |
| 143 | return _border_size; |
| 144 | } |
| 145 | |
Michalis Spyrou | 7e9391b | 2018-10-05 14:49:28 +0100 | [diff] [blame] | 146 | void CLReductionOperationKernel::configure(const ICLTensor *input, ICLTensor *output, unsigned int axis, ReductionOperation op, unsigned int width) |
Michalis Spyrou | 04f089c | 2017-08-08 17:42:38 +0100 | [diff] [blame] | 147 | { |
John Richardson | 62385bc | 2018-04-20 13:11:36 +0100 | [diff] [blame] | 148 | ARM_COMPUTE_ERROR_ON_NULLPTR(input, output); |
Michalis Spyrou | 04f089c | 2017-08-08 17:42:38 +0100 | [diff] [blame] | 149 | |
Michalis Spyrou | 7e9391b | 2018-10-05 14:49:28 +0100 | [diff] [blame] | 150 | ARM_COMPUTE_ERROR_THROW_ON(validate_arguments(input->info(), output->info(), axis, op, width)); |
Michalis Spyrou | 04f089c | 2017-08-08 17:42:38 +0100 | [diff] [blame] | 151 | |
| 152 | _input = input; |
| 153 | _output = output; |
| 154 | _reduction_axis = axis; |
| 155 | _op = op; |
Michalis Spyrou | 343722b | 2018-06-05 13:04:40 +0100 | [diff] [blame] | 156 | |
Michalis Spyrou | 04f089c | 2017-08-08 17:42:38 +0100 | [diff] [blame] | 157 | // Set build options |
Michalis Spyrou | 7e9391b | 2018-10-05 14:49:28 +0100 | [diff] [blame] | 158 | CLBuildOptions build_opts; |
| 159 | std::string data_type_promoted = get_cl_type_from_data_type(input->info()->data_type()); |
Michalis Spyrou | 7930db4 | 2018-11-22 17:36:28 +0000 | [diff] [blame] | 160 | if(is_data_type_quantized(input->info()->data_type())) |
Michalis Spyrou | 7e9391b | 2018-10-05 14:49:28 +0100 | [diff] [blame] | 161 | { |
| 162 | data_type_promoted = "uint"; |
| 163 | } |
Michalis Spyrou | b9626ab | 2019-05-13 17:41:01 +0100 | [diff] [blame] | 164 | |
Michalis Spyrou | 7e9391b | 2018-10-05 14:49:28 +0100 | [diff] [blame] | 165 | build_opts.add_option("-DDATA_TYPE=" + get_cl_type_from_data_type(input->info()->data_type())); |
| 166 | build_opts.add_option("-DDATA_TYPE_PROMOTED=" + data_type_promoted); |
Michalis Spyrou | b9626ab | 2019-05-13 17:41:01 +0100 | [diff] [blame] | 167 | build_opts.add_option_if(is_data_type_float(input->info()->data_type()), "-DFLOAT_DATA_TYPE"); |
Manuel Bottini | b412fab | 2018-12-10 17:40:23 +0000 | [diff] [blame] | 168 | build_opts.add_option_if(op == ReductionOperation::SUM_SQUARE, "-DSUM_SQUARE"); |
Michalis Spyrou | 7e9391b | 2018-10-05 14:49:28 +0100 | [diff] [blame] | 169 | build_opts.add_option_if(op == ReductionOperation::MEAN_SUM, "-DMEAN"); |
Michalis Spyrou | 7930db4 | 2018-11-22 17:36:28 +0000 | [diff] [blame] | 170 | build_opts.add_option_if(op == ReductionOperation::ARG_IDX_MAX, "-DARG_MAX"); |
| 171 | build_opts.add_option_if(op == ReductionOperation::ARG_IDX_MIN, "-DARG_MIN"); |
Manuel Bottini | b412fab | 2018-12-10 17:40:23 +0000 | [diff] [blame] | 172 | build_opts.add_option_if(op == ReductionOperation::PROD, "-DPROD"); |
Usama Arif | b289050 | 2019-05-21 11:48:37 +0100 | [diff] [blame] | 173 | build_opts.add_option_if(op == ReductionOperation::MIN, "-DMIN"); |
Usama Arif | 048b0f3 | 2019-05-22 16:32:27 +0100 | [diff] [blame] | 174 | build_opts.add_option_if(op == ReductionOperation::MAX, "-DMAX"); |
Georgios Pinitas | 8be9148 | 2019-03-26 17:23:28 +0000 | [diff] [blame] | 175 | build_opts.add_option_if(input->info()->num_channels() == 2, "-DCOMPLEX"); |
Michalis Spyrou | 04f089c | 2017-08-08 17:42:38 +0100 | [diff] [blame] | 176 | |
| 177 | switch(op) |
| 178 | { |
| 179 | case ReductionOperation::SUM_SQUARE: |
Michalis Spyrou | 7e9391b | 2018-10-05 14:49:28 +0100 | [diff] [blame] | 180 | build_opts.add_option(("-DOPERATION=square_sum")); |
Michalis Spyrou | 04f089c | 2017-08-08 17:42:38 +0100 | [diff] [blame] | 181 | break; |
| 182 | case ReductionOperation::SUM: |
Michalis Spyrou | 7e9391b | 2018-10-05 14:49:28 +0100 | [diff] [blame] | 183 | case ReductionOperation::MEAN_SUM: |
| 184 | build_opts.add_option(("-DOPERATION=sum")); |
Michalis Spyrou | 04f089c | 2017-08-08 17:42:38 +0100 | [diff] [blame] | 185 | break; |
Michalis Spyrou | 7930db4 | 2018-11-22 17:36:28 +0000 | [diff] [blame] | 186 | case ReductionOperation::ARG_IDX_MAX: |
| 187 | case ReductionOperation::ARG_IDX_MIN: |
Usama Arif | b289050 | 2019-05-21 11:48:37 +0100 | [diff] [blame] | 188 | case ReductionOperation::MIN: |
Usama Arif | 048b0f3 | 2019-05-22 16:32:27 +0100 | [diff] [blame] | 189 | case ReductionOperation::MAX: |
Michalis Spyrou | 7930db4 | 2018-11-22 17:36:28 +0000 | [diff] [blame] | 190 | break; |
Manuel Bottini | b412fab | 2018-12-10 17:40:23 +0000 | [diff] [blame] | 191 | case ReductionOperation::PROD: |
| 192 | build_opts.add_option(("-DOPERATION=product")); |
| 193 | break; |
Michalis Spyrou | 04f089c | 2017-08-08 17:42:38 +0100 | [diff] [blame] | 194 | default: |
| 195 | ARM_COMPUTE_ERROR("Unsupported reduction operation"); |
| 196 | } |
| 197 | |
| 198 | // Create kernel |
Michalis Spyrou | 7e9391b | 2018-10-05 14:49:28 +0100 | [diff] [blame] | 199 | cl::NDRange lws_hint = CLKernelLibrary::get().default_ndrange(); |
| 200 | std::string kernel_axis_name; |
Usama Arif | 048b0f3 | 2019-05-22 16:32:27 +0100 | [diff] [blame] | 201 | const bool is_serial_op = (op == ReductionOperation::ARG_IDX_MAX || op == ReductionOperation::ARG_IDX_MIN || op == ReductionOperation::MIN || op == ReductionOperation::MAX |
| 202 | || is_data_type_quantized(input->info()->data_type())); |
Michalis Spyrou | 7e9391b | 2018-10-05 14:49:28 +0100 | [diff] [blame] | 203 | switch(axis) |
| 204 | { |
| 205 | case 0: |
| 206 | { |
Manuel Bottini | b412fab | 2018-12-10 17:40:23 +0000 | [diff] [blame] | 207 | if(is_serial_op) |
| 208 | { |
| 209 | build_opts.add_option("-DWIDTH=" + support::cpp11::to_string(input->info()->dimension(0))); |
Michalis Spyrou | b9626ab | 2019-05-13 17:41:01 +0100 | [diff] [blame] | 210 | build_opts.add_option_if_else(_input->info()->data_type() == DataType::F16, "-DCOND_DATA_TYPE=short", "-DCOND_DATA_TYPE=int"); |
Manuel Bottini | b412fab | 2018-12-10 17:40:23 +0000 | [diff] [blame] | 211 | kernel_axis_name = "non_parallel_x"; |
| 212 | } |
| 213 | else |
Michalis Spyrou | 7e9391b | 2018-10-05 14:49:28 +0100 | [diff] [blame] | 214 | { |
| 215 | build_opts.add_option_if(op == ReductionOperation::MEAN_SUM, "-DWIDTH=" + support::cpp11::to_string(width)); |
| 216 | const unsigned int width_leftover = input->info()->dimension(0) % border_val; |
| 217 | const unsigned int border_width = (width_leftover != 0) ? border_val - width_leftover : 0; |
| 218 | const unsigned int num_of_threads = ((input->info()->dimension(0) + border_width) / 16); |
| 219 | kernel_axis_name = "x"; |
| 220 | |
| 221 | // Set the number of WG based on the input size. If input width is < 128 |
| 222 | // we can use fewer threads than 8. |
| 223 | lws_hint = cl::NDRange(std::min(8U, num_of_threads)); |
| 224 | _border_size = BorderSize(0, border_width, 0, 0); |
| 225 | } |
Michalis Spyrou | 7e9391b | 2018-10-05 14:49:28 +0100 | [diff] [blame] | 226 | } |
| 227 | break; |
| 228 | case 1: |
| 229 | build_opts.add_option("-DHEIGHT=" + support::cpp11::to_string(input->info()->dimension(1))); |
| 230 | kernel_axis_name = "y"; |
| 231 | break; |
| 232 | case 2: |
| 233 | build_opts.add_option("-DDEPTH=" + support::cpp11::to_string(input->info()->dimension(2))); |
| 234 | kernel_axis_name = "z"; |
| 235 | break; |
| 236 | case 3: |
| 237 | build_opts.add_option("-DDEPTH=" + support::cpp11::to_string(input->info()->dimension(2))); |
| 238 | build_opts.add_option("-DBATCH=" + support::cpp11::to_string(input->info()->dimension(3))); |
| 239 | kernel_axis_name = "w"; |
| 240 | break; |
| 241 | default: |
| 242 | ARM_COMPUTE_ERROR("Not supported"); |
| 243 | } |
| 244 | _kernel = static_cast<cl::Kernel>(CLKernelLibrary::get().create_kernel("reduction_operation_" + kernel_axis_name, build_opts.options())); |
Michalis Spyrou | 04f089c | 2017-08-08 17:42:38 +0100 | [diff] [blame] | 245 | |
| 246 | // Configure kernel window |
Michalis Spyrou | 7930db4 | 2018-11-22 17:36:28 +0000 | [diff] [blame] | 247 | auto win_config = validate_and_configure_window(_input->info(), _output->info(), axis, op); |
Michalis Spyrou | 04f089c | 2017-08-08 17:42:38 +0100 | [diff] [blame] | 248 | |
John Richardson | 62385bc | 2018-04-20 13:11:36 +0100 | [diff] [blame] | 249 | ARM_COMPUTE_ERROR_THROW_ON(std::get<0>(win_config)); |
Michalis Spyrou | 04f089c | 2017-08-08 17:42:38 +0100 | [diff] [blame] | 250 | |
Anthony Barbier | b6eb353 | 2018-08-08 13:20:04 +0100 | [diff] [blame] | 251 | ICLKernel::configure_internal(std::get<1>(win_config), lws_hint); |
John Richardson | 62385bc | 2018-04-20 13:11:36 +0100 | [diff] [blame] | 252 | } |
Michalis Spyrou | 04f089c | 2017-08-08 17:42:38 +0100 | [diff] [blame] | 253 | |
Michalis Spyrou | 7e9391b | 2018-10-05 14:49:28 +0100 | [diff] [blame] | 254 | Status CLReductionOperationKernel::validate(const ITensorInfo *input, const ITensorInfo *output, unsigned int axis, ReductionOperation op, unsigned int width) |
John Richardson | 62385bc | 2018-04-20 13:11:36 +0100 | [diff] [blame] | 255 | { |
Michalis Spyrou | 7e9391b | 2018-10-05 14:49:28 +0100 | [diff] [blame] | 256 | ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments(input, output, axis, op, width)); |
Michalis Spyrou | 7930db4 | 2018-11-22 17:36:28 +0000 | [diff] [blame] | 257 | ARM_COMPUTE_RETURN_ON_ERROR(std::get<0>(validate_and_configure_window(input->clone().get(), output->clone().get(), axis, op))); |
John Richardson | 62385bc | 2018-04-20 13:11:36 +0100 | [diff] [blame] | 258 | |
| 259 | return Status{}; |
Michalis Spyrou | 04f089c | 2017-08-08 17:42:38 +0100 | [diff] [blame] | 260 | } |
| 261 | |
| 262 | void CLReductionOperationKernel::run(const Window &window, cl::CommandQueue &queue) |
| 263 | { |
| 264 | ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this); |
| 265 | ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(IKernel::window(), window); |
| 266 | |
Usama Arif | 048b0f3 | 2019-05-22 16:32:27 +0100 | [diff] [blame] | 267 | const bool is_serial_op = (_op == ReductionOperation::ARG_IDX_MAX || _op == ReductionOperation::ARG_IDX_MIN || _op == ReductionOperation::MIN || _op == ReductionOperation::MAX |
| 268 | || is_data_type_quantized(_input->info()->data_type())); |
Michalis Spyrou | 7e9391b | 2018-10-05 14:49:28 +0100 | [diff] [blame] | 269 | switch(_reduction_axis) |
Michalis Spyrou | 04f089c | 2017-08-08 17:42:38 +0100 | [diff] [blame] | 270 | { |
Michalis Spyrou | 7e9391b | 2018-10-05 14:49:28 +0100 | [diff] [blame] | 271 | case 0: |
| 272 | { |
| 273 | // We use parallel reduction only in non quantized types |
Manuel Bottini | b412fab | 2018-12-10 17:40:23 +0000 | [diff] [blame] | 274 | if(is_serial_op) |
Michalis Spyrou | 7e9391b | 2018-10-05 14:49:28 +0100 | [diff] [blame] | 275 | { |
| 276 | // Get first input and output slices |
| 277 | Window window_in{ window }; |
| 278 | window_in.set(Window::DimX, Window::Dimension(0, _input->info()->dimension(0), _input->info()->dimension(0))); |
| 279 | |
| 280 | Window in_slice = window.first_slice_window_1D(); |
| 281 | Window out_slice = window.first_slice_window_1D(); |
| 282 | |
| 283 | do |
| 284 | { |
| 285 | unsigned int idx = 0; |
| 286 | add_1D_tensor_argument(idx, _input, in_slice); |
| 287 | add_1D_tensor_argument(idx, _output, out_slice); |
| 288 | enqueue(queue, *this, in_slice); |
| 289 | } |
| 290 | while(window_in.slide_window_slice_1D(in_slice) && window.slide_window_slice_1D(out_slice)); |
| 291 | } |
Manuel Bottini | b412fab | 2018-12-10 17:40:23 +0000 | [diff] [blame] | 292 | else |
| 293 | { |
| 294 | // Set out window |
| 295 | Window out_window(window); |
| 296 | out_window.set(Window::DimX, Window::Dimension(0, 0, 0)); |
| 297 | |
| 298 | // Get first input and output slices |
| 299 | Window in_slice = window.first_slice_window_2D(); |
| 300 | Window out_slice = out_window.first_slice_window_2D(); |
| 301 | |
| 302 | // Reshape window |
| 303 | const unsigned int border_width = ((in_slice.x().end() % border_val) != 0) ? border_val - in_slice.x().end() % border_val : 0; |
| 304 | in_slice.set(Window::DimX, Window::Dimension(in_slice.x().start(), in_slice.x().end() + border_width, in_slice.x().step())); |
| 305 | |
| 306 | // Set local sums buffer |
| 307 | unsigned int local_res_size = lws_hint()[0] * _input->info()->element_size(); |
| 308 | _kernel.setArg(num_arguments_per_2D_tensor() * 2, local_res_size, nullptr); |
| 309 | |
| 310 | do |
| 311 | { |
| 312 | unsigned int idx = 0; |
| 313 | add_2D_tensor_argument(idx, _input, in_slice); |
| 314 | add_2D_tensor_argument(idx, _output, out_slice); |
| 315 | enqueue(queue, *this, in_slice, lws_hint()); |
| 316 | } |
| 317 | while(window.slide_window_slice_2D(in_slice) && window.slide_window_slice_2D(out_slice)); |
| 318 | } |
Michalis Spyrou | 7e9391b | 2018-10-05 14:49:28 +0100 | [diff] [blame] | 319 | } |
| 320 | break; |
| 321 | case 1: |
| 322 | { |
| 323 | // Get first input and output slices |
| 324 | Window window_in{ window }; |
| 325 | window_in.set(Window::DimY, Window::Dimension(0, _input->info()->dimension(1), _input->info()->dimension(1))); |
| 326 | Window in_slice = window_in.first_slice_window_2D(); |
| 327 | Window out_slice = window.first_slice_window_2D(); |
| 328 | |
| 329 | do |
| 330 | { |
| 331 | unsigned int idx = 0; |
| 332 | add_2D_tensor_argument(idx, _input, in_slice); |
| 333 | add_2D_tensor_argument(idx, _output, out_slice); |
| 334 | enqueue(queue, *this, in_slice); |
| 335 | } |
| 336 | while(window_in.slide_window_slice_2D(in_slice) && window.slide_window_slice_2D(out_slice)); |
| 337 | } |
| 338 | break; |
| 339 | case 2: |
| 340 | { |
| 341 | // Get first input and output slices |
| 342 | Window window_in{ window }; |
| 343 | window_in.set(Window::DimZ, Window::Dimension(0, _input->info()->dimension(2), _input->info()->dimension(2))); |
| 344 | Window in_slice = window_in.first_slice_window_3D(); |
| 345 | Window out_slice = window.first_slice_window_3D(); |
| 346 | |
| 347 | do |
| 348 | { |
| 349 | unsigned int idx = 0; |
| 350 | add_3D_tensor_argument(idx, _input, in_slice); |
| 351 | add_3D_tensor_argument(idx, _output, out_slice); |
| 352 | enqueue(queue, *this, in_slice); |
| 353 | } |
| 354 | while(window_in.slide_window_slice_3D(in_slice) && window.slide_window_slice_3D(out_slice)); |
| 355 | } |
| 356 | break; |
| 357 | case 3: |
| 358 | { |
| 359 | // Get first input and output slices |
| 360 | Window window_in{ window }; |
| 361 | window_in.set(3, Window::Dimension(0, 1, 1)); |
| 362 | Window in_slice = window_in.first_slice_window_4D(); |
| 363 | Window out_slice = window.first_slice_window_4D(); |
| 364 | |
| 365 | do |
| 366 | { |
| 367 | unsigned int idx = 0; |
| 368 | add_4D_tensor_argument(idx, _input, in_slice); |
| 369 | add_4D_tensor_argument(idx, _output, out_slice); |
| 370 | enqueue(queue, *this, in_slice); |
| 371 | } |
| 372 | while(window_in.slide_window_slice_4D(in_slice) && window.slide_window_slice_4D(out_slice)); |
| 373 | } |
| 374 | break; |
| 375 | default: |
| 376 | ARM_COMPUTE_ERROR("Not supported"); |
Michalis Spyrou | 04f089c | 2017-08-08 17:42:38 +0100 | [diff] [blame] | 377 | } |
Michalis Spyrou | 04f089c | 2017-08-08 17:42:38 +0100 | [diff] [blame] | 378 | } |