Michalis Spyrou | 04f089c | 2017-08-08 17:42:38 +0100 | [diff] [blame] | 1 | /* |
Matthew Bentham | 314d3e2 | 2023-06-23 10:53:52 +0000 | [diff] [blame] | 2 | * Copyright (c) 2017-2021, 2023 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 | */ |
Sang-Hoon Park | bef7fa2 | 2020-10-21 15:58:54 +0100 | [diff] [blame] | 24 | #include "src/core/CL/kernels/CLReductionOperationKernel.h" |
Michalis Spyrou | 04f089c | 2017-08-08 17:42:38 +0100 | [diff] [blame] | 25 | |
Michalis Spyrou | 04f089c | 2017-08-08 17:42:38 +0100 | [diff] [blame] | 26 | #include "arm_compute/core/CL/CLHelpers.h" |
| 27 | #include "arm_compute/core/CL/CLKernelLibrary.h" |
| 28 | #include "arm_compute/core/CL/ICLTensor.h" |
Michalis Spyrou | 04f089c | 2017-08-08 17:42:38 +0100 | [diff] [blame] | 29 | #include "arm_compute/core/Helpers.h" |
| 30 | #include "arm_compute/core/TensorInfo.h" |
Matthew Bentham | 314d3e2 | 2023-06-23 10:53:52 +0000 | [diff] [blame] | 31 | #include "arm_compute/core/utils/helpers/AdjustVecSize.h" |
Sang-Hoon Park | 2697fd8 | 2019-10-15 16:49:24 +0100 | [diff] [blame] | 32 | #include "arm_compute/core/utils/misc/ShapeCalculator.h" |
Matthew Bentham | 314d3e2 | 2023-06-23 10:53:52 +0000 | [diff] [blame] | 33 | #include "arm_compute/core/utils/StringUtils.h" |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 34 | #include "arm_compute/core/Validate.h" |
| 35 | |
Sang-Hoon Park | 68dd25f | 2020-10-19 16:00:11 +0100 | [diff] [blame] | 36 | #include "src/core/AccessWindowStatic.h" |
| 37 | #include "src/core/CL/CLValidate.h" |
| 38 | #include "src/core/helpers/AutoConfiguration.h" |
| 39 | #include "src/core/helpers/WindowHelpers.h" |
Matthew Bentham | 758b5ba | 2020-03-05 23:37:48 +0000 | [diff] [blame] | 40 | #include "support/StringSupport.h" |
Michalis Spyrou | 04f089c | 2017-08-08 17:42:38 +0100 | [diff] [blame] | 41 | |
Michele Di Giorgio | 9637b2e | 2019-09-23 16:49:49 +0100 | [diff] [blame] | 42 | namespace arm_compute |
| 43 | { |
John Richardson | 62385bc | 2018-04-20 13:11:36 +0100 | [diff] [blame] | 44 | namespace |
| 45 | { |
Giorgio Arena | 3ecf9fe | 2021-04-28 16:11:51 +0100 | [diff] [blame] | 46 | Status validate_arguments(const ITensorInfo *input, const ITensorInfo *output, unsigned int axis, ReductionOperation op) |
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); |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 50 | if (input->num_channels() == 1) |
Georgios Pinitas | 8be9148 | 2019-03-26 17:23:28 +0000 | [diff] [blame] | 51 | { |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 52 | ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::QASYMM8, DataType::QASYMM8_SIGNED, |
| 53 | DataType::S32, DataType::F16, DataType::F32); |
Georgios Pinitas | 8be9148 | 2019-03-26 17:23:28 +0000 | [diff] [blame] | 54 | } |
| 55 | else |
| 56 | { |
Giorgio Arena | ea7de7b | 2020-12-10 16:49:39 +0000 | [diff] [blame] | 57 | ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 2, DataType::F16, DataType::F32); |
Giorgio Arena | 3ecf9fe | 2021-04-28 16:11:51 +0100 | [diff] [blame] | 58 | ARM_COMPUTE_RETURN_ERROR_ON(axis == 0); |
Georgios Pinitas | 8be9148 | 2019-03-26 17:23:28 +0000 | [diff] [blame] | 59 | } |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 60 | ARM_COMPUTE_RETURN_ERROR_ON_MSG(op == ReductionOperation::SUM_SQUARE && input->data_type() == DataType::QASYMM8, |
| 61 | "Not supported reduction operation for QASYMM8"); |
| 62 | ARM_COMPUTE_RETURN_ERROR_ON_MSG(axis >= TensorShape::num_max_dimensions, |
| 63 | "Reduction axis greater than max number of dimensions"); |
Michalis Spyrou | 7e9391b | 2018-10-05 14:49:28 +0100 | [diff] [blame] | 64 | ARM_COMPUTE_RETURN_ERROR_ON_MSG(axis > 3, "Unsupported reduction axis"); |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 65 | ARM_COMPUTE_RETURN_ERROR_ON((op == ReductionOperation::MEAN_SUM) && (axis == 0) && (input->dimension(0) == 0) && |
| 66 | (input->data_type() != DataType::QASYMM8) && |
| 67 | (input->data_type() != DataType::QASYMM8_SIGNED)); |
| 68 | ARM_COMPUTE_RETURN_ERROR_ON_MSG((op == ReductionOperation::ARG_IDX_MAX) || (op == ReductionOperation::ARG_IDX_MIN), |
| 69 | "Not supported reduction operation, use CLArgMinMaxLayer"); |
John Richardson | 62385bc | 2018-04-20 13:11:36 +0100 | [diff] [blame] | 70 | |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 71 | if (output->total_size() != 0) |
John Richardson | 62385bc | 2018-04-20 13:11:36 +0100 | [diff] [blame] | 72 | { |
Manuel Bottini | 7b9998d | 2019-10-21 17:59:07 +0100 | [diff] [blame] | 73 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input, output); |
| 74 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_QUANTIZATION_INFO(input, output); |
John Richardson | 62385bc | 2018-04-20 13:11:36 +0100 | [diff] [blame] | 75 | } |
| 76 | |
| 77 | return Status{}; |
| 78 | } |
John Richardson | 62385bc | 2018-04-20 13:11:36 +0100 | [diff] [blame] | 79 | } // namespace |
| 80 | |
Michalis Spyrou | 04f089c | 2017-08-08 17:42:38 +0100 | [diff] [blame] | 81 | CLReductionOperationKernel::CLReductionOperationKernel() |
Giorgio Arena | 3ecf9fe | 2021-04-28 16:11:51 +0100 | [diff] [blame] | 82 | : _input(nullptr), _output(nullptr), _reduction_axis(0), _op(ReductionOperation::SUM_SQUARE) |
Michalis Spyrou | 04f089c | 2017-08-08 17:42:38 +0100 | [diff] [blame] | 83 | { |
Giorgio Arena | 4a95bba | 2021-06-28 11:00:27 +0100 | [diff] [blame] | 84 | _type = CLKernelType::ELEMENTWISE; |
Michalis Spyrou | 04f089c | 2017-08-08 17:42:38 +0100 | [diff] [blame] | 85 | } |
| 86 | |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 87 | void CLReductionOperationKernel::configure(const ICLTensor *input, |
| 88 | ICLTensor *output, |
| 89 | unsigned int axis, |
| 90 | ReductionOperation op) |
Michalis Spyrou | 04f089c | 2017-08-08 17:42:38 +0100 | [diff] [blame] | 91 | { |
Giorgio Arena | 3ecf9fe | 2021-04-28 16:11:51 +0100 | [diff] [blame] | 92 | configure(CLKernelLibrary::get().get_compile_context(), input, output, axis, op); |
Michalis Spyrou | 04f089c | 2017-08-08 17:42:38 +0100 | [diff] [blame] | 93 | } |
| 94 | |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 95 | void CLReductionOperationKernel::configure(const CLCompileContext &compile_context, |
| 96 | const ICLTensor *input, |
| 97 | ICLTensor *output, |
| 98 | unsigned int axis, |
| 99 | ReductionOperation op) |
Manuel Bottini | 4c6bd51 | 2020-04-08 10:15:51 +0100 | [diff] [blame] | 100 | { |
John Richardson | 62385bc | 2018-04-20 13:11:36 +0100 | [diff] [blame] | 101 | ARM_COMPUTE_ERROR_ON_NULLPTR(input, output); |
Michalis Spyrou | 04f089c | 2017-08-08 17:42:38 +0100 | [diff] [blame] | 102 | |
Giorgio Arena | 3ecf9fe | 2021-04-28 16:11:51 +0100 | [diff] [blame] | 103 | ARM_COMPUTE_ERROR_THROW_ON(validate_arguments(input->info(), output->info(), axis, op)); |
| 104 | |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 105 | auto padding_info = get_padding_info({input, output}); |
Michalis Spyrou | 04f089c | 2017-08-08 17:42:38 +0100 | [diff] [blame] | 106 | |
| 107 | _input = input; |
| 108 | _output = output; |
| 109 | _reduction_axis = axis; |
| 110 | _op = op; |
Michalis Spyrou | 343722b | 2018-06-05 13:04:40 +0100 | [diff] [blame] | 111 | |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 112 | const TensorShape output_shape = |
| 113 | arm_compute::misc::shape_calculator::compute_reduced_shape(input->info()->tensor_shape(), axis, true); |
| 114 | auto_init_if_empty(*output->info(), |
| 115 | input->info()->clone()->set_tensor_shape(output_shape).reset_padding().set_is_resizable(true)); |
Giorgio Arena | 3ecf9fe | 2021-04-28 16:11:51 +0100 | [diff] [blame] | 116 | |
Michalis Spyrou | 04f089c | 2017-08-08 17:42:38 +0100 | [diff] [blame] | 117 | // Set build options |
Michalis Spyrou | 7e9391b | 2018-10-05 14:49:28 +0100 | [diff] [blame] | 118 | CLBuildOptions build_opts; |
Michalis Spyrou | 0b18d97 | 2020-01-30 18:11:13 +0000 | [diff] [blame] | 119 | DataType data_type = input->info()->data_type(); |
| 120 | std::string data_type_promoted{}; |
| 121 | |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 122 | if (is_data_type_quantized(data_type)) |
Michalis Spyrou | 7e9391b | 2018-10-05 14:49:28 +0100 | [diff] [blame] | 123 | { |
Michalis Spyrou | 9bd49f9 | 2020-02-07 15:25:17 +0000 | [diff] [blame] | 124 | data_type_promoted = "int"; |
Michalis Spyrou | 0b18d97 | 2020-01-30 18:11:13 +0000 | [diff] [blame] | 125 | } |
| 126 | else |
| 127 | { |
| 128 | data_type_promoted = get_cl_type_from_data_type(data_type); |
Michalis Spyrou | 7e9391b | 2018-10-05 14:49:28 +0100 | [diff] [blame] | 129 | } |
Michalis Spyrou | b9626ab | 2019-05-13 17:41:01 +0100 | [diff] [blame] | 130 | |
Giorgio Arena | 3ecf9fe | 2021-04-28 16:11:51 +0100 | [diff] [blame] | 131 | const unsigned int width = input->info()->dimension(0) * input->info()->num_channels(); |
| 132 | unsigned int vec_size = (is_data_type_quantized(input->info()->data_type()) && (axis == 0)) ? 1 : 16; |
| 133 | vec_size = adjust_vec_size(vec_size, width); |
| 134 | const unsigned int vec_size_leftover = width % vec_size; |
| 135 | |
Michalis Spyrou | 0b18d97 | 2020-01-30 18:11:13 +0000 | [diff] [blame] | 136 | 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] | 137 | build_opts.add_option("-DDATA_TYPE_PROMOTED=" + data_type_promoted); |
Giorgio Arena | 3ecf9fe | 2021-04-28 16:11:51 +0100 | [diff] [blame] | 138 | build_opts.add_option("-DVEC_SIZE=" + support::cpp11::to_string(vec_size)); |
| 139 | build_opts.add_option("-DVEC_SIZE_LEFTOVER=" + support::cpp11::to_string(vec_size_leftover)); |
Michalis Spyrou | 0b18d97 | 2020-01-30 18:11:13 +0000 | [diff] [blame] | 140 | 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] | 141 | build_opts.add_option_if(op == ReductionOperation::SUM_SQUARE, "-DSUM_SQUARE"); |
Michalis Spyrou | 7e9391b | 2018-10-05 14:49:28 +0100 | [diff] [blame] | 142 | build_opts.add_option_if(op == ReductionOperation::MEAN_SUM, "-DMEAN"); |
Michalis Spyrou | 0b18d97 | 2020-01-30 18:11:13 +0000 | [diff] [blame] | 143 | build_opts.add_option_if(op == ReductionOperation::SUM, "-DSUM"); |
Manuel Bottini | b412fab | 2018-12-10 17:40:23 +0000 | [diff] [blame] | 144 | build_opts.add_option_if(op == ReductionOperation::PROD, "-DPROD"); |
Usama Arif | b289050 | 2019-05-21 11:48:37 +0100 | [diff] [blame] | 145 | build_opts.add_option_if(op == ReductionOperation::MIN, "-DMIN"); |
Usama Arif | 048b0f3 | 2019-05-22 16:32:27 +0100 | [diff] [blame] | 146 | build_opts.add_option_if(op == ReductionOperation::MAX, "-DMAX"); |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 147 | build_opts.add_option_if(is_data_type_quantized(data_type), |
| 148 | "-DOFFSET=" + |
| 149 | support::cpp11::to_string(input->info()->quantization_info().uniform().offset)); |
| 150 | build_opts.add_option_if( |
| 151 | is_data_type_quantized(data_type), |
| 152 | "-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] | 153 | |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 154 | switch (op) |
Michalis Spyrou | 04f089c | 2017-08-08 17:42:38 +0100 | [diff] [blame] | 155 | { |
| 156 | case ReductionOperation::SUM_SQUARE: |
Michalis Spyrou | 7e9391b | 2018-10-05 14:49:28 +0100 | [diff] [blame] | 157 | build_opts.add_option(("-DOPERATION=square_sum")); |
Michalis Spyrou | 04f089c | 2017-08-08 17:42:38 +0100 | [diff] [blame] | 158 | break; |
| 159 | case ReductionOperation::SUM: |
Michalis Spyrou | 7e9391b | 2018-10-05 14:49:28 +0100 | [diff] [blame] | 160 | case ReductionOperation::MEAN_SUM: |
| 161 | build_opts.add_option(("-DOPERATION=sum")); |
Michalis Spyrou | 04f089c | 2017-08-08 17:42:38 +0100 | [diff] [blame] | 162 | break; |
Usama Arif | b289050 | 2019-05-21 11:48:37 +0100 | [diff] [blame] | 163 | case ReductionOperation::MIN: |
Gunes Bayir | 338ef46 | 2023-07-18 15:57:23 +0100 | [diff] [blame] | 164 | build_opts.add_option(("-DOPERATION=min_")); |
| 165 | break; |
Usama Arif | 048b0f3 | 2019-05-22 16:32:27 +0100 | [diff] [blame] | 166 | case ReductionOperation::MAX: |
Gunes Bayir | 338ef46 | 2023-07-18 15:57:23 +0100 | [diff] [blame] | 167 | build_opts.add_option(("-DOPERATION=max_")); |
Michalis Spyrou | 7930db4 | 2018-11-22 17:36:28 +0000 | [diff] [blame] | 168 | break; |
Manuel Bottini | b412fab | 2018-12-10 17:40:23 +0000 | [diff] [blame] | 169 | case ReductionOperation::PROD: |
| 170 | build_opts.add_option(("-DOPERATION=product")); |
| 171 | break; |
Michalis Spyrou | 04f089c | 2017-08-08 17:42:38 +0100 | [diff] [blame] | 172 | default: |
| 173 | ARM_COMPUTE_ERROR("Unsupported reduction operation"); |
| 174 | } |
| 175 | |
| 176 | // Create kernel |
Michalis Spyrou | 7e9391b | 2018-10-05 14:49:28 +0100 | [diff] [blame] | 177 | std::string kernel_axis_name; |
Sang-Hoon Park | 2697fd8 | 2019-10-15 16:49:24 +0100 | [diff] [blame] | 178 | const bool is_serial_op = needs_serialized_reduction(_op, _input->info()->data_type(), _reduction_axis); |
| 179 | |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 180 | switch (axis) |
Michalis Spyrou | 7e9391b | 2018-10-05 14:49:28 +0100 | [diff] [blame] | 181 | { |
| 182 | case 0: |
| 183 | { |
Giorgio Arena | 3ecf9fe | 2021-04-28 16:11:51 +0100 | [diff] [blame] | 184 | build_opts.add_option("-DWIDTH=" + support::cpp11::to_string(width)); |
Giorgio Arena | 1fac037 | 2021-04-30 15:09:46 +0100 | [diff] [blame] | 185 | kernel_axis_name = ((is_serial_op) ? "non_parallel_x" : "x"); |
Michalis Spyrou | 7e9391b | 2018-10-05 14:49:28 +0100 | [diff] [blame] | 186 | } |
| 187 | break; |
| 188 | case 1: |
| 189 | build_opts.add_option("-DHEIGHT=" + support::cpp11::to_string(input->info()->dimension(1))); |
| 190 | kernel_axis_name = "y"; |
| 191 | break; |
| 192 | case 2: |
| 193 | build_opts.add_option("-DDEPTH=" + support::cpp11::to_string(input->info()->dimension(2))); |
| 194 | kernel_axis_name = "z"; |
| 195 | break; |
| 196 | case 3: |
| 197 | build_opts.add_option("-DDEPTH=" + support::cpp11::to_string(input->info()->dimension(2))); |
| 198 | build_opts.add_option("-DBATCH=" + support::cpp11::to_string(input->info()->dimension(3))); |
| 199 | kernel_axis_name = "w"; |
| 200 | break; |
| 201 | default: |
| 202 | ARM_COMPUTE_ERROR("Not supported"); |
| 203 | } |
Manuel Bottini | 4c6bd51 | 2020-04-08 10:15:51 +0100 | [diff] [blame] | 204 | _kernel = create_kernel(compile_context, "reduction_operation_" + kernel_axis_name, build_opts.options()); |
Michalis Spyrou | 04f089c | 2017-08-08 17:42:38 +0100 | [diff] [blame] | 205 | |
| 206 | // Configure kernel window |
Viet-Hoa Do | c210c85 | 2023-10-09 10:58:35 +0100 | [diff] [blame] | 207 | TensorShape actual_input_shape = input->info()->tensor_shape(); |
| 208 | actual_input_shape[0] = width; |
| 209 | |
| 210 | Window win = calculate_max_window(actual_input_shape, Steps(vec_size)); |
Giorgio Arena | 3ecf9fe | 2021-04-28 16:11:51 +0100 | [diff] [blame] | 211 | ICLKernel::configure_internal(win); |
Manuel Bottini | 34f88dd | 2019-10-18 10:37:46 +0000 | [diff] [blame] | 212 | |
Giorgio Arena | 3ecf9fe | 2021-04-28 16:11:51 +0100 | [diff] [blame] | 213 | ARM_COMPUTE_ERROR_ON(has_padding_changed(padding_info)); |
John Richardson | 62385bc | 2018-04-20 13:11:36 +0100 | [diff] [blame] | 214 | } |
Michalis Spyrou | 04f089c | 2017-08-08 17:42:38 +0100 | [diff] [blame] | 215 | |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 216 | Status CLReductionOperationKernel::validate(const ITensorInfo *input, |
| 217 | const ITensorInfo *output, |
| 218 | unsigned int axis, |
| 219 | ReductionOperation op) |
John Richardson | 62385bc | 2018-04-20 13:11:36 +0100 | [diff] [blame] | 220 | { |
Giorgio Arena | 3ecf9fe | 2021-04-28 16:11:51 +0100 | [diff] [blame] | 221 | ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments(input, output, axis, op)); |
John Richardson | 62385bc | 2018-04-20 13:11:36 +0100 | [diff] [blame] | 222 | return Status{}; |
Michalis Spyrou | 04f089c | 2017-08-08 17:42:38 +0100 | [diff] [blame] | 223 | } |
| 224 | |
| 225 | void CLReductionOperationKernel::run(const Window &window, cl::CommandQueue &queue) |
| 226 | { |
| 227 | ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this); |
| 228 | ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(IKernel::window(), window); |
Manuel Bottini | 34f88dd | 2019-10-18 10:37:46 +0000 | [diff] [blame] | 229 | |
Sang-Hoon Park | 2697fd8 | 2019-10-15 16:49:24 +0100 | [diff] [blame] | 230 | const bool is_serial_op = needs_serialized_reduction(_op, _input->info()->data_type(), _reduction_axis); |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 231 | switch (_reduction_axis) |
Michalis Spyrou | 04f089c | 2017-08-08 17:42:38 +0100 | [diff] [blame] | 232 | { |
Michalis Spyrou | 7e9391b | 2018-10-05 14:49:28 +0100 | [diff] [blame] | 233 | case 0: |
| 234 | { |
| 235 | // We use parallel reduction only in non quantized types |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 236 | if (is_serial_op) |
Michalis Spyrou | 7e9391b | 2018-10-05 14:49:28 +0100 | [diff] [blame] | 237 | { |
| 238 | // Get first input and output slices |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 239 | Window window_in{window}; |
| 240 | window_in.set(Window::DimX, |
| 241 | Window::Dimension(0, _input->info()->dimension(0), _input->info()->dimension(0))); |
Michalis Spyrou | 7e9391b | 2018-10-05 14:49:28 +0100 | [diff] [blame] | 242 | |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 243 | Window out_window{window}; |
Michele Di Giorgio | 0333704 | 2020-02-26 17:47:55 +0000 | [diff] [blame] | 244 | out_window.set(Window::DimX, Window::Dimension(0, 0, 0)); |
| 245 | |
| 246 | Window in_slice = window_in.first_slice_window_1D(); |
| 247 | Window out_slice = out_window.first_slice_window_1D(); |
Michalis Spyrou | 7e9391b | 2018-10-05 14:49:28 +0100 | [diff] [blame] | 248 | |
| 249 | do |
| 250 | { |
| 251 | unsigned int idx = 0; |
| 252 | add_1D_tensor_argument(idx, _input, in_slice); |
| 253 | add_1D_tensor_argument(idx, _output, out_slice); |
Giorgio Arena | 3ecf9fe | 2021-04-28 16:11:51 +0100 | [diff] [blame] | 254 | enqueue(queue, *this, in_slice); |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 255 | } 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] | 256 | } |
Manuel Bottini | b412fab | 2018-12-10 17:40:23 +0000 | [diff] [blame] | 257 | else |
| 258 | { |
| 259 | // Set out window |
Giorgio Arena | 3ecf9fe | 2021-04-28 16:11:51 +0100 | [diff] [blame] | 260 | bool has_collapsed = true; |
Giorgio Arena | 1fac037 | 2021-04-30 15:09:46 +0100 | [diff] [blame] | 261 | Window window_in = window.collapse_if_possible(window, 2, &has_collapsed); |
Giorgio Arena | 3ecf9fe | 2021-04-28 16:11:51 +0100 | [diff] [blame] | 262 | ARM_COMPUTE_ERROR_ON(!has_collapsed); |
Manuel Bottini | b412fab | 2018-12-10 17:40:23 +0000 | [diff] [blame] | 263 | |
Giorgio Arena | 3ecf9fe | 2021-04-28 16:11:51 +0100 | [diff] [blame] | 264 | Window window_out = window_in; |
| 265 | window_out.set(0, Window::Dimension()); |
Manuel Bottini | b412fab | 2018-12-10 17:40:23 +0000 | [diff] [blame] | 266 | |
Giorgio Arena | 3ecf9fe | 2021-04-28 16:11:51 +0100 | [diff] [blame] | 267 | unsigned int idx = 0; |
Giorgio Arena | 1fac037 | 2021-04-30 15:09:46 +0100 | [diff] [blame] | 268 | add_3D_tensor_argument(idx, _input, window_in); |
| 269 | add_3D_tensor_argument(idx, _output, window_out); |
Giorgio Arena | 3ecf9fe | 2021-04-28 16:11:51 +0100 | [diff] [blame] | 270 | enqueue(queue, *this, window_in); |
Manuel Bottini | b412fab | 2018-12-10 17:40:23 +0000 | [diff] [blame] | 271 | } |
Michalis Spyrou | 7e9391b | 2018-10-05 14:49:28 +0100 | [diff] [blame] | 272 | } |
| 273 | break; |
| 274 | case 1: |
| 275 | { |
Viet-Hoa Do | c210c85 | 2023-10-09 10:58:35 +0100 | [diff] [blame] | 276 | bool has_collapsed = true; |
| 277 | Window actual_window = window.collapse_if_possible(window, 2, &has_collapsed); |
| 278 | ARM_COMPUTE_ERROR_ON(!has_collapsed); |
Michalis Spyrou | 7e9391b | 2018-10-05 14:49:28 +0100 | [diff] [blame] | 279 | |
Viet-Hoa Do | c210c85 | 2023-10-09 10:58:35 +0100 | [diff] [blame] | 280 | actual_window = actual_window.shift_dimensions(1, Window::DimY); |
| 281 | |
| 282 | const ITensorInfo *input_info = _input->info(); |
| 283 | const Strides &input_strides = input_info->strides_in_bytes(); |
| 284 | |
| 285 | const ITensorInfo *output_info = _output->info(); |
| 286 | const Strides &output_strides = output_info->strides_in_bytes(); |
| 287 | |
| 288 | unsigned int idx = 0; |
| 289 | |
| 290 | _kernel.setArg(idx++, _input->cl_buffer()); |
| 291 | _kernel.setArg<cl_uint>(idx++, input_strides[1]); |
| 292 | _kernel.setArg<cl_uint>(idx++, input_strides[2]); |
| 293 | _kernel.setArg<cl_uint>(idx++, input_info->offset_first_element_in_bytes()); |
| 294 | |
| 295 | _kernel.setArg(idx++, _output->cl_buffer()); |
| 296 | _kernel.setArg<cl_uint>(idx++, output_strides[2]); |
| 297 | _kernel.setArg<cl_uint>(idx++, output_info->offset_first_element_in_bytes()); |
| 298 | |
| 299 | enqueue(queue, *this, actual_window); |
Michalis Spyrou | 7e9391b | 2018-10-05 14:49:28 +0100 | [diff] [blame] | 300 | } |
| 301 | break; |
| 302 | case 2: |
| 303 | { |
Viet-Hoa Do | c210c85 | 2023-10-09 10:58:35 +0100 | [diff] [blame] | 304 | bool has_collapsed = true; |
| 305 | Window actual_window = window.collapse_if_possible(window, 3, &has_collapsed); |
| 306 | ARM_COMPUTE_ERROR_ON(!has_collapsed); |
Michalis Spyrou | 7e9391b | 2018-10-05 14:49:28 +0100 | [diff] [blame] | 307 | |
Viet-Hoa Do | c210c85 | 2023-10-09 10:58:35 +0100 | [diff] [blame] | 308 | actual_window = actual_window.shift_dimensions(1, Window::DimZ); |
| 309 | |
| 310 | const ITensorInfo *input_info = _input->info(); |
| 311 | const Strides &input_strides = input_info->strides_in_bytes(); |
| 312 | |
| 313 | const ITensorInfo *output_info = _output->info(); |
| 314 | const Strides &output_strides = output_info->strides_in_bytes(); |
| 315 | |
| 316 | unsigned int idx = 0; |
| 317 | |
| 318 | _kernel.setArg(idx++, _input->cl_buffer()); |
| 319 | _kernel.setArg<cl_uint>(idx++, input_strides[1]); |
| 320 | _kernel.setArg<cl_uint>(idx++, input_strides[2]); |
| 321 | _kernel.setArg<cl_uint>(idx++, input_strides[3]); |
| 322 | _kernel.setArg<cl_uint>(idx++, input_info->offset_first_element_in_bytes()); |
| 323 | |
| 324 | _kernel.setArg(idx++, _output->cl_buffer()); |
| 325 | _kernel.setArg<cl_uint>(idx++, output_strides[1]); |
| 326 | _kernel.setArg<cl_uint>(idx++, output_strides[3]); |
| 327 | _kernel.setArg<cl_uint>(idx++, output_info->offset_first_element_in_bytes()); |
| 328 | |
| 329 | enqueue(queue, *this, actual_window); |
Michalis Spyrou | 7e9391b | 2018-10-05 14:49:28 +0100 | [diff] [blame] | 330 | } |
| 331 | break; |
| 332 | case 3: |
| 333 | { |
Viet-Hoa Do | c210c85 | 2023-10-09 10:58:35 +0100 | [diff] [blame] | 334 | bool has_collapsed = true; |
| 335 | Window actual_window = window.shift_dimensions(1, Window::DimW); |
Michalis Spyrou | 7e9391b | 2018-10-05 14:49:28 +0100 | [diff] [blame] | 336 | |
Viet-Hoa Do | c210c85 | 2023-10-09 10:58:35 +0100 | [diff] [blame] | 337 | actual_window = actual_window.collapse_if_possible(actual_window, 2, &has_collapsed); |
| 338 | ARM_COMPUTE_ERROR_ON(!has_collapsed); |
| 339 | |
| 340 | const ITensorInfo *input_info = _input->info(); |
| 341 | const Strides &input_strides = input_info->strides_in_bytes(); |
| 342 | |
| 343 | const ITensorInfo *output_info = _output->info(); |
| 344 | const Strides &output_strides = output_info->strides_in_bytes(); |
| 345 | |
| 346 | unsigned int idx = 0; |
| 347 | |
| 348 | _kernel.setArg(idx++, _input->cl_buffer()); |
| 349 | _kernel.setArg<cl_uint>(idx++, input_strides[1]); |
| 350 | _kernel.setArg<cl_uint>(idx++, input_strides[2]); |
| 351 | _kernel.setArg<cl_uint>(idx++, input_strides[3]); |
| 352 | _kernel.setArg<cl_uint>(idx++, input_strides[4]); |
| 353 | _kernel.setArg<cl_uint>(idx++, input_info->offset_first_element_in_bytes()); |
| 354 | |
| 355 | _kernel.setArg(idx++, _output->cl_buffer()); |
| 356 | _kernel.setArg<cl_uint>(idx++, output_strides[1]); |
| 357 | _kernel.setArg<cl_uint>(idx++, output_strides[2]); |
| 358 | _kernel.setArg<cl_uint>(idx++, output_strides[4]); |
| 359 | _kernel.setArg<cl_uint>(idx++, output_info->offset_first_element_in_bytes()); |
| 360 | |
| 361 | enqueue(queue, *this, actual_window); |
Michalis Spyrou | 7e9391b | 2018-10-05 14:49:28 +0100 | [diff] [blame] | 362 | } |
| 363 | break; |
| 364 | default: |
| 365 | ARM_COMPUTE_ERROR("Not supported"); |
Michalis Spyrou | 04f089c | 2017-08-08 17:42:38 +0100 | [diff] [blame] | 366 | } |
Michalis Spyrou | 04f089c | 2017-08-08 17:42:38 +0100 | [diff] [blame] | 367 | } |
Manuel Bottini | 34f88dd | 2019-10-18 10:37:46 +0000 | [diff] [blame] | 368 | } // namespace arm_compute |