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