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