Michalis Spyrou | 04f089c | 2017-08-08 17:42:38 +0100 | [diff] [blame] | 1 | /* |
Michele Di Giorgio | d9eaf61 | 2020-07-08 11:12:57 +0100 | [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/runtime/CL/functions/CLReductionOperation.h" |
| 25 | |
| 26 | #include "arm_compute/core/CL/ICLTensor.h" |
Sang-Hoon Park | 2697fd8 | 2019-10-15 16:49:24 +0100 | [diff] [blame] | 27 | #include "arm_compute/core/Helpers.h" |
Michalis Spyrou | 04f089c | 2017-08-08 17:42:38 +0100 | [diff] [blame] | 28 | #include "arm_compute/core/PixelValue.h" |
| 29 | #include "arm_compute/core/TensorInfo.h" |
| 30 | #include "arm_compute/core/Validate.h" |
Sang-Hoon Park | 2697fd8 | 2019-10-15 16:49:24 +0100 | [diff] [blame] | 31 | #include "arm_compute/core/utils/misc/ShapeCalculator.h" |
Michalis Spyrou | 04f089c | 2017-08-08 17:42:38 +0100 | [diff] [blame] | 32 | #include "arm_compute/runtime/CL/CLScheduler.h" |
Sang-Hoon Park | bef7fa2 | 2020-10-21 15:58:54 +0100 | [diff] [blame] | 33 | #include "src/core/CL/kernels/CLFillBorderKernel.h" |
| 34 | #include "src/core/CL/kernels/CLReductionOperationKernel.h" |
Sang-Hoon Park | 68dd25f | 2020-10-19 16:00:11 +0100 | [diff] [blame] | 35 | #include "src/core/helpers/AutoConfiguration.h" |
| 36 | #include "src/runtime/Utils.h" |
Michalis Spyrou | 04f089c | 2017-08-08 17:42:38 +0100 | [diff] [blame] | 37 | |
Sang-Hoon Park | 2697fd8 | 2019-10-15 16:49:24 +0100 | [diff] [blame] | 38 | namespace arm_compute |
| 39 | { |
Georgios Pinitas | 8a94e7c | 2017-09-15 19:06:47 +0100 | [diff] [blame] | 40 | CLReductionOperation::CLReductionOperation(std::shared_ptr<IMemoryManager> memory_manager) |
Michele Di Giorgio | f6f7876 | 2020-07-06 11:27:21 +0100 | [diff] [blame] | 41 | : _memory_group(std::move(memory_manager)), _results_vector(), _reduction_kernels_vector(), _border_handlers_vector(), _reshape(), _num_of_stages(), _reduction_axis(), _is_serial(), |
Sang-Hoon Park | 2697fd8 | 2019-10-15 16:49:24 +0100 | [diff] [blame] | 42 | _is_reshape_required(false) |
Michalis Spyrou | 04f089c | 2017-08-08 17:42:38 +0100 | [diff] [blame] | 43 | { |
| 44 | } |
| 45 | |
Sang-Hoon Park | bef7fa2 | 2020-10-21 15:58:54 +0100 | [diff] [blame] | 46 | CLReductionOperation::~CLReductionOperation() = default; |
| 47 | |
Sang-Hoon Park | 2697fd8 | 2019-10-15 16:49:24 +0100 | [diff] [blame] | 48 | Status CLReductionOperation::validate(const ITensorInfo *input, const ITensorInfo *output, unsigned int axis, ReductionOperation op, bool keep_dims) |
John Richardson | 62385bc | 2018-04-20 13:11:36 +0100 | [diff] [blame] | 49 | { |
Manuel Bottini | 7b9998d | 2019-10-21 17:59:07 +0100 | [diff] [blame] | 50 | ARM_COMPUTE_ERROR_ON_NULLPTR(input, output); |
Sang-Hoon Park | 2697fd8 | 2019-10-15 16:49:24 +0100 | [diff] [blame] | 51 | ARM_COMPUTE_RETURN_ERROR_ON_MSG(axis >= TensorShape::num_max_dimensions, "Reduction axis greater than max number of dimensions"); |
| 52 | ARM_COMPUTE_RETURN_ERROR_ON_MSG(axis > 3, "Unsupported reduction axis"); |
| 53 | |
Sang-Hoon Park | 68dd25f | 2020-10-19 16:00:11 +0100 | [diff] [blame] | 54 | const unsigned int num_of_stages = utils::calculate_number_of_stages_only_x_axis(input->dimension(0), axis); |
Sang-Hoon Park | 2697fd8 | 2019-10-15 16:49:24 +0100 | [diff] [blame] | 55 | const bool is_serial = needs_serialized_reduction(op, input->data_type(), axis); |
Manuel Bottini | 7b9998d | 2019-10-21 17:59:07 +0100 | [diff] [blame] | 56 | const bool is_reshape_required = !keep_dims; |
Sang-Hoon Park | 2697fd8 | 2019-10-15 16:49:24 +0100 | [diff] [blame] | 57 | |
Manuel Bottini | 7b9998d | 2019-10-21 17:59:07 +0100 | [diff] [blame] | 58 | if(is_reshape_required && output->total_size() != 0) |
Sang-Hoon Park | 2697fd8 | 2019-10-15 16:49:24 +0100 | [diff] [blame] | 59 | { |
| 60 | const TensorInfo expected_output_shape = output->clone()->set_tensor_shape(arm_compute::misc::shape_calculator::compute_reduced_shape(input->tensor_shape(), axis, keep_dims)); |
| 61 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_SHAPES(&expected_output_shape, output); |
| 62 | } |
| 63 | |
| 64 | auto *output_internal = output; |
| 65 | |
| 66 | TensorInfo output_before_reshape; |
| 67 | const auto input_shape = input->tensor_shape(); |
| 68 | const auto input_data_type = input->data_type(); |
| 69 | const auto input_num_channles = input->num_channels(); |
| 70 | const auto input_qinfo = input->quantization_info(); |
Manuel Bottini | 7b9998d | 2019-10-21 17:59:07 +0100 | [diff] [blame] | 71 | const auto output_data_type = output->data_type(); |
Sang-Hoon Park | 2697fd8 | 2019-10-15 16:49:24 +0100 | [diff] [blame] | 72 | |
| 73 | auto initialize_tensorinfo = [](TensorInfo & ti, TensorShape shape, DataType data_type, int num_channels, QuantizationInfo qinfo) |
| 74 | { |
| 75 | ti.set_data_type(data_type).set_tensor_shape(shape).set_num_channels(num_channels).set_quantization_info(qinfo); |
| 76 | }; |
| 77 | |
| 78 | if(is_reshape_required) |
| 79 | { |
| 80 | auto shape_before_reshape = input_shape; |
| 81 | shape_before_reshape.set(axis, 1); |
| 82 | initialize_tensorinfo(output_before_reshape, shape_before_reshape, output_data_type, input_num_channles, input_qinfo); |
| 83 | output_internal = &output_before_reshape; |
| 84 | } |
| 85 | |
Manuel Bottini | b412fab | 2018-12-10 17:40:23 +0000 | [diff] [blame] | 86 | if(is_serial) |
| 87 | { |
Sang-Hoon Park | 2697fd8 | 2019-10-15 16:49:24 +0100 | [diff] [blame] | 88 | ARM_COMPUTE_RETURN_ON_ERROR(CLReductionOperationKernel::validate(input, output_internal, axis, op)); |
Manuel Bottini | b412fab | 2018-12-10 17:40:23 +0000 | [diff] [blame] | 89 | } |
| 90 | else |
John Richardson | 62385bc | 2018-04-20 13:11:36 +0100 | [diff] [blame] | 91 | { |
Michalis Spyrou | 7e9391b | 2018-10-05 14:49:28 +0100 | [diff] [blame] | 92 | // Create temporary tensor infos |
Michalis Spyrou | bcfd09a | 2019-05-01 13:03:59 +0100 | [diff] [blame] | 93 | std::vector<TensorInfo> sums_vector(num_of_stages - 1); |
Michalis Spyrou | 7e9391b | 2018-10-05 14:49:28 +0100 | [diff] [blame] | 94 | |
| 95 | // Create intermediate tensor info |
Sang-Hoon Park | 2697fd8 | 2019-10-15 16:49:24 +0100 | [diff] [blame] | 96 | TensorShape shape{ input_shape }; |
| 97 | |
| 98 | shape.set(0, ceil(shape.x() / 128.f)); |
Michalis Spyrou | 7e9391b | 2018-10-05 14:49:28 +0100 | [diff] [blame] | 99 | |
| 100 | for(unsigned int i = 0; i < num_of_stages - 1; i++) |
| 101 | { |
Sang-Hoon Park | 2697fd8 | 2019-10-15 16:49:24 +0100 | [diff] [blame] | 102 | initialize_tensorinfo(sums_vector[i], shape, input_data_type, input_num_channles, input_qinfo); |
Michalis Spyrou | 7e9391b | 2018-10-05 14:49:28 +0100 | [diff] [blame] | 103 | } |
| 104 | |
Michalis Spyrou | e55a013 | 2018-10-26 10:48:56 +0100 | [diff] [blame] | 105 | ReductionOperation first_kernel_op; |
Manuel Bottini | b412fab | 2018-12-10 17:40:23 +0000 | [diff] [blame] | 106 | ReductionOperation intermediate_kernel_op; |
Michalis Spyrou | e55a013 | 2018-10-26 10:48:56 +0100 | [diff] [blame] | 107 | ReductionOperation last_kernel_op; |
| 108 | switch(op) |
| 109 | { |
| 110 | case ReductionOperation::SUM: |
| 111 | case ReductionOperation::MEAN_SUM: |
Manuel Bottini | b412fab | 2018-12-10 17:40:23 +0000 | [diff] [blame] | 112 | first_kernel_op = ReductionOperation::SUM; |
| 113 | intermediate_kernel_op = ReductionOperation::SUM; |
| 114 | last_kernel_op = op; |
Michalis Spyrou | e55a013 | 2018-10-26 10:48:56 +0100 | [diff] [blame] | 115 | break; |
| 116 | case ReductionOperation::SUM_SQUARE: |
Manuel Bottini | b412fab | 2018-12-10 17:40:23 +0000 | [diff] [blame] | 117 | first_kernel_op = ReductionOperation::SUM_SQUARE; |
| 118 | intermediate_kernel_op = ReductionOperation::SUM; |
| 119 | last_kernel_op = ReductionOperation::SUM; |
| 120 | break; |
| 121 | case ReductionOperation::PROD: |
| 122 | first_kernel_op = ReductionOperation::PROD; |
| 123 | intermediate_kernel_op = ReductionOperation::PROD; |
| 124 | last_kernel_op = ReductionOperation::PROD; |
Michalis Spyrou | e55a013 | 2018-10-26 10:48:56 +0100 | [diff] [blame] | 125 | break; |
Usama Arif | b289050 | 2019-05-21 11:48:37 +0100 | [diff] [blame] | 126 | case ReductionOperation::MIN: |
| 127 | first_kernel_op = ReductionOperation::MIN; |
| 128 | intermediate_kernel_op = ReductionOperation::MIN; |
| 129 | last_kernel_op = ReductionOperation::MIN; |
| 130 | break; |
Usama Arif | 048b0f3 | 2019-05-22 16:32:27 +0100 | [diff] [blame] | 131 | case ReductionOperation::MAX: |
| 132 | first_kernel_op = ReductionOperation::MAX; |
| 133 | intermediate_kernel_op = ReductionOperation::MAX; |
| 134 | last_kernel_op = ReductionOperation::MAX; |
| 135 | break; |
Michalis Spyrou | e55a013 | 2018-10-26 10:48:56 +0100 | [diff] [blame] | 136 | default: |
| 137 | ARM_COMPUTE_ERROR("Not supported"); |
| 138 | } |
| 139 | |
Michalis Spyrou | 7e9391b | 2018-10-05 14:49:28 +0100 | [diff] [blame] | 140 | // Validate ReductionOperation only on first kernel |
Michalis Spyrou | bcfd09a | 2019-05-01 13:03:59 +0100 | [diff] [blame] | 141 | ARM_COMPUTE_RETURN_ON_ERROR(CLReductionOperationKernel::validate(input, &sums_vector[0], axis, first_kernel_op)); |
Michalis Spyrou | 7e9391b | 2018-10-05 14:49:28 +0100 | [diff] [blame] | 142 | |
| 143 | // Validate ReductionOperation on intermediate stages |
| 144 | for(unsigned int i = 1; i < num_of_stages - 1; ++i) |
| 145 | { |
Michalis Spyrou | bcfd09a | 2019-05-01 13:03:59 +0100 | [diff] [blame] | 146 | ARM_COMPUTE_RETURN_ON_ERROR(CLReductionOperationKernel::validate(&sums_vector[i - 1], &sums_vector[i], axis, intermediate_kernel_op)); |
Michalis Spyrou | 7e9391b | 2018-10-05 14:49:28 +0100 | [diff] [blame] | 147 | } |
| 148 | |
| 149 | // Validate ReductionOperation on the last stage |
| 150 | const unsigned int last_stage = num_of_stages - 1; |
Sang-Hoon Park | 2697fd8 | 2019-10-15 16:49:24 +0100 | [diff] [blame] | 151 | ARM_COMPUTE_RETURN_ON_ERROR(CLReductionOperationKernel::validate(&sums_vector[last_stage - 1], output_internal, axis, last_kernel_op, input->dimension(0))); |
| 152 | } |
| 153 | |
| 154 | if(is_reshape_required) |
| 155 | { |
Michalis Spyrou | 2aad21a | 2020-07-02 12:43:53 +0100 | [diff] [blame] | 156 | ARM_COMPUTE_RETURN_ON_ERROR(CLReshapeLayer::validate(output_internal, output)); |
John Richardson | 62385bc | 2018-04-20 13:11:36 +0100 | [diff] [blame] | 157 | } |
John Richardson | 62385bc | 2018-04-20 13:11:36 +0100 | [diff] [blame] | 158 | |
John Richardson | 62385bc | 2018-04-20 13:11:36 +0100 | [diff] [blame] | 159 | return Status{}; |
| 160 | } |
| 161 | |
Sang-Hoon Park | 2697fd8 | 2019-10-15 16:49:24 +0100 | [diff] [blame] | 162 | ICLTensor *CLReductionOperation::configure_intermediate_result_vector(ICLTensor *input, ICLTensor *output) |
Michalis Spyrou | 04f089c | 2017-08-08 17:42:38 +0100 | [diff] [blame] | 163 | { |
Sang-Hoon Park | 2697fd8 | 2019-10-15 16:49:24 +0100 | [diff] [blame] | 164 | if(!_is_reshape_required && _is_serial) |
| 165 | { |
| 166 | return output; |
| 167 | } |
| 168 | |
Manuel Bottini | 7b9998d | 2019-10-21 17:59:07 +0100 | [diff] [blame] | 169 | auto intermediate_result_vector_size = _is_serial ? 1 : _num_of_stages; |
Sang-Hoon Park | 2697fd8 | 2019-10-15 16:49:24 +0100 | [diff] [blame] | 170 | |
| 171 | if(!_is_reshape_required) |
| 172 | { |
| 173 | --intermediate_result_vector_size; |
| 174 | } |
| 175 | |
| 176 | _results_vector.resize(intermediate_result_vector_size); |
| 177 | auto shape = input->info()->tensor_shape(); |
| 178 | |
| 179 | shape.set(_reduction_axis, _is_serial ? 1 : ceil(shape.x() / 128.f)); |
| 180 | |
| 181 | for(auto &v : _results_vector) |
| 182 | { |
| 183 | if(&v == &_results_vector.back() && _is_reshape_required) |
| 184 | { |
| 185 | shape.set(_reduction_axis, 1); |
| 186 | } |
| 187 | v.allocator()->init(input->info()->clone()->set_tensor_shape(shape)); |
| 188 | } |
| 189 | |
Sang-Hoon Park | 2697fd8 | 2019-10-15 16:49:24 +0100 | [diff] [blame] | 190 | return _is_reshape_required ? &_results_vector.back() : output; |
| 191 | } |
| 192 | |
| 193 | void CLReductionOperation::configure(ICLTensor *input, ICLTensor *output, unsigned int axis, ReductionOperation op, bool keep_dims) |
| 194 | { |
Manuel Bottini | 2b84be5 | 2020-04-08 10:15:51 +0100 | [diff] [blame] | 195 | configure(CLKernelLibrary::get().get_compile_context(), input, output, axis, op, keep_dims); |
| 196 | } |
| 197 | |
| 198 | void CLReductionOperation::configure(const CLCompileContext &compile_context, ICLTensor *input, ICLTensor *output, unsigned int axis, ReductionOperation op, bool keep_dims) |
| 199 | { |
Manuel Bottini | 7b9998d | 2019-10-21 17:59:07 +0100 | [diff] [blame] | 200 | ARM_COMPUTE_ERROR_ON_NULLPTR(input, output); |
Sang-Hoon Park | 68dd25f | 2020-10-19 16:00:11 +0100 | [diff] [blame] | 201 | _num_of_stages = utils::calculate_number_of_stages_only_x_axis(input->info()->dimension(0), axis); |
Manuel Bottini | 7b9998d | 2019-10-21 17:59:07 +0100 | [diff] [blame] | 202 | _reduction_axis = axis; |
| 203 | _is_serial = needs_serialized_reduction(op, input->info()->data_type(), axis); |
| 204 | _is_reshape_required = !keep_dims; |
Sang-Hoon Park | 2697fd8 | 2019-10-15 16:49:24 +0100 | [diff] [blame] | 205 | |
| 206 | auto *output_internal = configure_intermediate_result_vector(input, output); |
| 207 | |
Sang-Hoon Park | 2697fd8 | 2019-10-15 16:49:24 +0100 | [diff] [blame] | 208 | if(_is_reshape_required) |
| 209 | { |
| 210 | const TensorShape output_shape = arm_compute::misc::shape_calculator::compute_reduced_shape(input->info()->tensor_shape(), axis, false); |
Manuel Bottini | 7b9998d | 2019-10-21 17:59:07 +0100 | [diff] [blame] | 211 | const auto output_data_type = input->info()->data_type(); |
Sang-Hoon Park | 2697fd8 | 2019-10-15 16:49:24 +0100 | [diff] [blame] | 212 | auto_init_if_empty(*output->info(), input->info()->clone()->set_tensor_shape(output_shape).set_data_type(output_data_type).reset_padding().set_is_resizable(true)); |
| 213 | } |
Georgios Pinitas | aec513c | 2017-09-15 19:36:30 +0100 | [diff] [blame] | 214 | |
Michalis Spyrou | 04f089c | 2017-08-08 17:42:38 +0100 | [diff] [blame] | 215 | // Configure reduction operation kernels |
Sang-Hoon Park | bef7fa2 | 2020-10-21 15:58:54 +0100 | [diff] [blame] | 216 | _reduction_kernels_vector.reserve(_num_of_stages); |
Michalis Spyrou | 04f089c | 2017-08-08 17:42:38 +0100 | [diff] [blame] | 217 | |
Michalis Spyrou | 7e9391b | 2018-10-05 14:49:28 +0100 | [diff] [blame] | 218 | // Create temporary tensors |
Manuel Bottini | b412fab | 2018-12-10 17:40:23 +0000 | [diff] [blame] | 219 | if(_is_serial) |
| 220 | { |
Sang-Hoon Park | 2697fd8 | 2019-10-15 16:49:24 +0100 | [diff] [blame] | 221 | if(_is_reshape_required) |
| 222 | { |
| 223 | _memory_group.manage(&_results_vector.back()); |
| 224 | } |
| 225 | |
Georgios Pinitas | 40f51a6 | 2020-11-21 03:04:18 +0000 | [diff] [blame] | 226 | _reduction_kernels_vector.emplace_back(std::make_unique<CLReductionOperationKernel>()); |
Sang-Hoon Park | bef7fa2 | 2020-10-21 15:58:54 +0100 | [diff] [blame] | 227 | _reduction_kernels_vector[0]->configure(compile_context, input, output_internal, axis, op, 0); |
Manuel Bottini | b412fab | 2018-12-10 17:40:23 +0000 | [diff] [blame] | 228 | } |
| 229 | else |
Michalis Spyrou | 04f089c | 2017-08-08 17:42:38 +0100 | [diff] [blame] | 230 | { |
Sang-Hoon Park | bef7fa2 | 2020-10-21 15:58:54 +0100 | [diff] [blame] | 231 | _border_handlers_vector.reserve(_num_of_stages); |
Michalis Spyrou | bcfd09a | 2019-05-01 13:03:59 +0100 | [diff] [blame] | 232 | _memory_group.manage(&_results_vector[0]); |
Michalis Spyrou | 7e9391b | 2018-10-05 14:49:28 +0100 | [diff] [blame] | 233 | |
| 234 | ReductionOperation first_kernel_op; |
Manuel Bottini | b412fab | 2018-12-10 17:40:23 +0000 | [diff] [blame] | 235 | ReductionOperation intermediate_kernel_op; |
Michalis Spyrou | 7e9391b | 2018-10-05 14:49:28 +0100 | [diff] [blame] | 236 | ReductionOperation last_kernel_op; |
Manuel Bottini | b412fab | 2018-12-10 17:40:23 +0000 | [diff] [blame] | 237 | PixelValue pixelValue; |
Michalis Spyrou | 7e9391b | 2018-10-05 14:49:28 +0100 | [diff] [blame] | 238 | switch(op) |
| 239 | { |
| 240 | case ReductionOperation::SUM: |
| 241 | case ReductionOperation::MEAN_SUM: |
Manuel Bottini | b412fab | 2018-12-10 17:40:23 +0000 | [diff] [blame] | 242 | first_kernel_op = ReductionOperation::SUM; |
| 243 | intermediate_kernel_op = ReductionOperation::SUM; |
| 244 | last_kernel_op = op; |
Manuel Bottini | 55e1678 | 2019-01-15 13:21:57 +0000 | [diff] [blame] | 245 | pixelValue = PixelValue(); |
Michalis Spyrou | 7e9391b | 2018-10-05 14:49:28 +0100 | [diff] [blame] | 246 | break; |
| 247 | case ReductionOperation::SUM_SQUARE: |
Manuel Bottini | b412fab | 2018-12-10 17:40:23 +0000 | [diff] [blame] | 248 | first_kernel_op = ReductionOperation::SUM_SQUARE; |
| 249 | intermediate_kernel_op = ReductionOperation::SUM; |
| 250 | last_kernel_op = ReductionOperation::SUM; |
Manuel Bottini | 55e1678 | 2019-01-15 13:21:57 +0000 | [diff] [blame] | 251 | pixelValue = PixelValue(); |
Manuel Bottini | b412fab | 2018-12-10 17:40:23 +0000 | [diff] [blame] | 252 | break; |
| 253 | case ReductionOperation::PROD: |
| 254 | first_kernel_op = ReductionOperation::PROD; |
| 255 | intermediate_kernel_op = ReductionOperation::PROD; |
| 256 | last_kernel_op = ReductionOperation::PROD; |
| 257 | pixelValue = PixelValue(1, input->info()->data_type()); |
Michalis Spyrou | 7e9391b | 2018-10-05 14:49:28 +0100 | [diff] [blame] | 258 | break; |
Usama Arif | b289050 | 2019-05-21 11:48:37 +0100 | [diff] [blame] | 259 | case ReductionOperation::MIN: |
| 260 | first_kernel_op = ReductionOperation::MIN; |
| 261 | intermediate_kernel_op = ReductionOperation::MIN; |
| 262 | last_kernel_op = ReductionOperation::MIN; |
Michele Di Giorgio | f6f7876 | 2020-07-06 11:27:21 +0100 | [diff] [blame] | 263 | pixelValue = std::get<1>(get_min_max(input->info()->data_type())); |
Usama Arif | b289050 | 2019-05-21 11:48:37 +0100 | [diff] [blame] | 264 | break; |
Usama Arif | 048b0f3 | 2019-05-22 16:32:27 +0100 | [diff] [blame] | 265 | case ReductionOperation::MAX: |
| 266 | first_kernel_op = ReductionOperation::MAX; |
| 267 | intermediate_kernel_op = ReductionOperation::MAX; |
| 268 | last_kernel_op = ReductionOperation::MAX; |
Michele Di Giorgio | f6f7876 | 2020-07-06 11:27:21 +0100 | [diff] [blame] | 269 | pixelValue = std::get<0>(get_min_max(input->info()->data_type())); |
Usama Arif | 048b0f3 | 2019-05-22 16:32:27 +0100 | [diff] [blame] | 270 | break; |
Michalis Spyrou | 7e9391b | 2018-10-05 14:49:28 +0100 | [diff] [blame] | 271 | default: |
| 272 | ARM_COMPUTE_ERROR("Not supported"); |
| 273 | } |
| 274 | |
Georgios Pinitas | 40f51a6 | 2020-11-21 03:04:18 +0000 | [diff] [blame] | 275 | _reduction_kernels_vector.emplace_back(std::make_unique<CLReductionOperationKernel>()); |
Sang-Hoon Park | bef7fa2 | 2020-10-21 15:58:54 +0100 | [diff] [blame] | 276 | _reduction_kernels_vector[0]->configure(compile_context, input, &_results_vector[0], axis, first_kernel_op); |
| 277 | |
Georgios Pinitas | 40f51a6 | 2020-11-21 03:04:18 +0000 | [diff] [blame] | 278 | _border_handlers_vector.emplace_back(std::make_unique<CLFillBorderKernel>()); |
Sang-Hoon Park | bef7fa2 | 2020-10-21 15:58:54 +0100 | [diff] [blame] | 279 | _border_handlers_vector[0]->configure(compile_context, input, _reduction_kernels_vector[0]->border_size(), BorderMode::CONSTANT, pixelValue); |
Michalis Spyrou | 7e9391b | 2018-10-05 14:49:28 +0100 | [diff] [blame] | 280 | |
| 281 | // Apply ReductionOperation on intermediate stages |
| 282 | for(unsigned int i = 1; i < _num_of_stages - 1; ++i) |
| 283 | { |
Michalis Spyrou | bcfd09a | 2019-05-01 13:03:59 +0100 | [diff] [blame] | 284 | _memory_group.manage(&_results_vector[i]); |
Sang-Hoon Park | bef7fa2 | 2020-10-21 15:58:54 +0100 | [diff] [blame] | 285 | |
Georgios Pinitas | 40f51a6 | 2020-11-21 03:04:18 +0000 | [diff] [blame] | 286 | _reduction_kernels_vector.emplace_back(std::make_unique<CLReductionOperationKernel>()); |
Sang-Hoon Park | bef7fa2 | 2020-10-21 15:58:54 +0100 | [diff] [blame] | 287 | _reduction_kernels_vector[i]->configure(compile_context, &_results_vector[i - 1], &_results_vector[i], axis, intermediate_kernel_op); |
| 288 | |
Georgios Pinitas | 40f51a6 | 2020-11-21 03:04:18 +0000 | [diff] [blame] | 289 | _border_handlers_vector.emplace_back(std::make_unique<CLFillBorderKernel>()); |
Sang-Hoon Park | bef7fa2 | 2020-10-21 15:58:54 +0100 | [diff] [blame] | 290 | _border_handlers_vector[i]->configure(compile_context, &_results_vector[i - 1], _reduction_kernels_vector[i]->border_size(), BorderMode::CONSTANT, pixelValue); |
| 291 | |
Manuel Bottini | b412fab | 2018-12-10 17:40:23 +0000 | [diff] [blame] | 292 | _results_vector[i - 1].allocator()->allocate(); |
Michalis Spyrou | 7e9391b | 2018-10-05 14:49:28 +0100 | [diff] [blame] | 293 | } |
| 294 | |
| 295 | // Apply ReductionOperation on the last stage |
| 296 | const unsigned int last_stage = _num_of_stages - 1; |
| 297 | const unsigned int input_width = input->info()->dimension(0); |
Sang-Hoon Park | 2697fd8 | 2019-10-15 16:49:24 +0100 | [diff] [blame] | 298 | |
| 299 | if(_is_reshape_required) |
| 300 | { |
| 301 | _memory_group.manage(&_results_vector.back()); |
| 302 | } |
| 303 | |
Georgios Pinitas | 40f51a6 | 2020-11-21 03:04:18 +0000 | [diff] [blame] | 304 | _reduction_kernels_vector.emplace_back(std::make_unique<CLReductionOperationKernel>()); |
Sang-Hoon Park | bef7fa2 | 2020-10-21 15:58:54 +0100 | [diff] [blame] | 305 | _reduction_kernels_vector[last_stage]->configure(compile_context, &_results_vector[last_stage - 1], output_internal, axis, last_kernel_op, input_width); |
| 306 | |
Georgios Pinitas | 40f51a6 | 2020-11-21 03:04:18 +0000 | [diff] [blame] | 307 | _border_handlers_vector.emplace_back(std::make_unique<CLFillBorderKernel>()); |
Sang-Hoon Park | bef7fa2 | 2020-10-21 15:58:54 +0100 | [diff] [blame] | 308 | _border_handlers_vector[last_stage]->configure(compile_context, &_results_vector[last_stage - 1], _reduction_kernels_vector[last_stage]->border_size(), BorderMode::CONSTANT, pixelValue); |
| 309 | |
Manuel Bottini | b412fab | 2018-12-10 17:40:23 +0000 | [diff] [blame] | 310 | _results_vector[last_stage - 1].allocator()->allocate(); |
Michalis Spyrou | 04f089c | 2017-08-08 17:42:38 +0100 | [diff] [blame] | 311 | } |
Sang-Hoon Park | 2697fd8 | 2019-10-15 16:49:24 +0100 | [diff] [blame] | 312 | |
| 313 | if(_is_reshape_required) |
| 314 | { |
Michalis Spyrou | 2aad21a | 2020-07-02 12:43:53 +0100 | [diff] [blame] | 315 | _reshape.configure(compile_context, &_results_vector.back(), output); |
Sang-Hoon Park | 2697fd8 | 2019-10-15 16:49:24 +0100 | [diff] [blame] | 316 | _results_vector.back().allocator()->allocate(); |
| 317 | } |
Michalis Spyrou | 04f089c | 2017-08-08 17:42:38 +0100 | [diff] [blame] | 318 | } |
| 319 | |
| 320 | void CLReductionOperation::run() |
| 321 | { |
Georgios Pinitas | da953f2 | 2019-04-02 17:27:03 +0100 | [diff] [blame] | 322 | MemoryGroupResourceScope scope_mg(_memory_group); |
Georgios Pinitas | 8a94e7c | 2017-09-15 19:06:47 +0100 | [diff] [blame] | 323 | |
Manuel Bottini | b412fab | 2018-12-10 17:40:23 +0000 | [diff] [blame] | 324 | if(_is_serial) |
| 325 | { |
Sang-Hoon Park | bef7fa2 | 2020-10-21 15:58:54 +0100 | [diff] [blame] | 326 | CLScheduler::get().enqueue(*_reduction_kernels_vector[0], false); |
Manuel Bottini | b412fab | 2018-12-10 17:40:23 +0000 | [diff] [blame] | 327 | } |
| 328 | else |
Michalis Spyrou | 04f089c | 2017-08-08 17:42:38 +0100 | [diff] [blame] | 329 | { |
Michalis Spyrou | 7e9391b | 2018-10-05 14:49:28 +0100 | [diff] [blame] | 330 | for(unsigned int i = 0; i < _num_of_stages; ++i) |
| 331 | { |
Sang-Hoon Park | bef7fa2 | 2020-10-21 15:58:54 +0100 | [diff] [blame] | 332 | CLScheduler::get().enqueue(*_border_handlers_vector[i], false); |
| 333 | CLScheduler::get().enqueue(*_reduction_kernels_vector[i], false); |
Michalis Spyrou | 7e9391b | 2018-10-05 14:49:28 +0100 | [diff] [blame] | 334 | } |
| 335 | } |
Sang-Hoon Park | 2697fd8 | 2019-10-15 16:49:24 +0100 | [diff] [blame] | 336 | |
| 337 | if(_is_reshape_required) |
| 338 | { |
Michalis Spyrou | 2aad21a | 2020-07-02 12:43:53 +0100 | [diff] [blame] | 339 | _reshape.run(); |
Sang-Hoon Park | 2697fd8 | 2019-10-15 16:49:24 +0100 | [diff] [blame] | 340 | } |
John Richardson | 62385bc | 2018-04-20 13:11:36 +0100 | [diff] [blame] | 341 | } |
Sang-Hoon Park | 2697fd8 | 2019-10-15 16:49:24 +0100 | [diff] [blame] | 342 | } // namespace arm_compute |