Georgios Pinitas | d976958 | 2017-08-03 10:19:40 +0100 | [diff] [blame] | 1 | /* |
Michalis Spyrou | 19bd412 | 2020-01-22 10:27:06 +0000 | [diff] [blame] | 2 | * Copyright (c) 2017-2020 ARM Limited. |
Georgios Pinitas | d976958 | 2017-08-03 10:19:40 +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/NEON/functions/NEReductionOperation.h" |
| 25 | |
| 26 | #include "arm_compute/core/Helpers.h" |
Sang-Hoon Park | 2697fd8 | 2019-10-15 16:49:24 +0100 | [diff] [blame] | 27 | #include "arm_compute/core/utils/misc/ShapeCalculator.h" |
Georgios Pinitas | d976958 | 2017-08-03 10:19:40 +0100 | [diff] [blame] | 28 | #include "arm_compute/runtime/NEON/NEScheduler.h" |
| 29 | |
Michalis Spyrou | bcf8a96 | 2018-10-12 10:51:31 +0100 | [diff] [blame] | 30 | namespace arm_compute |
| 31 | { |
Georgios Pinitas | d976958 | 2017-08-03 10:19:40 +0100 | [diff] [blame] | 32 | namespace |
| 33 | { |
| 34 | /** Define dimension to split the window |
| 35 | * |
| 36 | * @param[in] axis Reduction axis |
| 37 | * |
| 38 | * @return The dimension to split the window |
| 39 | */ |
| 40 | size_t reduction_window_split_dimension(unsigned int axis) |
| 41 | { |
| 42 | switch(axis) |
| 43 | { |
| 44 | case 0: |
| 45 | return Window::DimY; |
Michalis Spyrou | bcf8a96 | 2018-10-12 10:51:31 +0100 | [diff] [blame] | 46 | case 1: |
| 47 | case 2: |
| 48 | case 3: |
| 49 | return Window::DimX; |
Georgios Pinitas | d976958 | 2017-08-03 10:19:40 +0100 | [diff] [blame] | 50 | default: |
| 51 | ARM_COMPUTE_ERROR("Unsupported reduction axis"); |
| 52 | } |
| 53 | } |
Georgios Pinitas | d976958 | 2017-08-03 10:19:40 +0100 | [diff] [blame] | 54 | } // namespace |
| 55 | |
Sang-Hoon Park | 2697fd8 | 2019-10-15 16:49:24 +0100 | [diff] [blame] | 56 | NEReductionOperation::NEReductionOperation(std::shared_ptr<IMemoryManager> memory_manager) |
Michalis Spyrou | bcd2352 | 2020-05-21 15:02:36 +0100 | [diff] [blame] | 57 | : _memory_group(memory_manager), _reduction_kernel(), _fill_border_kernel(), _reshape(), _output_internal(), _window_split(0), _reduction_axis(), _is_reshape_required(false) |
Georgios Pinitas | d976958 | 2017-08-03 10:19:40 +0100 | [diff] [blame] | 58 | { |
| 59 | } |
| 60 | |
Sang-Hoon Park | 2697fd8 | 2019-10-15 16:49:24 +0100 | [diff] [blame] | 61 | Status NEReductionOperation::validate(const ITensorInfo *input, const ITensorInfo *output, unsigned int axis, ReductionOperation op, bool keep_dims) |
John Richardson | 73d4aef | 2018-05-08 14:34:33 +0100 | [diff] [blame] | 62 | { |
Sang-Hoon Park | 2697fd8 | 2019-10-15 16:49:24 +0100 | [diff] [blame] | 63 | ARM_COMPUTE_RETURN_ERROR_ON_MSG(axis >= TensorShape::num_max_dimensions, "Reduction axis greater than max number of dimensions"); |
| 64 | ARM_COMPUTE_RETURN_ERROR_ON_MSG(axis > 3, "Unsupported reduction axis"); |
| 65 | |
| 66 | const auto is_reshape_required = !keep_dims; |
| 67 | |
| 68 | auto *output_internal = output; |
| 69 | |
| 70 | TensorInfo info_before_reshape; |
| 71 | |
| 72 | if(is_reshape_required) |
| 73 | { |
| 74 | const TensorInfo expected_output_shape = output->clone()->set_tensor_shape(arm_compute::misc::shape_calculator::compute_reduced_shape(input->tensor_shape(), axis, keep_dims)); |
| 75 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_SHAPES(&expected_output_shape, output); |
| 76 | |
| 77 | auto shape_before_reshape = input->tensor_shape(); |
| 78 | shape_before_reshape.set(axis, 1); |
| 79 | |
| 80 | const auto input_num_channles = input->num_channels(); |
| 81 | const auto input_qinfo = input->quantization_info(); |
| 82 | const auto is_arg_min_max = (op == ReductionOperation::ARG_IDX_MAX) || (op == ReductionOperation::ARG_IDX_MIN); |
Sang-Hoon Park | eaa01ab | 2019-11-11 17:33:28 +0000 | [diff] [blame] | 83 | const auto output_data_type = is_arg_min_max ? DataType::S32 : output->data_type(); |
Sang-Hoon Park | 2697fd8 | 2019-10-15 16:49:24 +0100 | [diff] [blame] | 84 | |
| 85 | info_before_reshape.set_data_type(output_data_type).set_tensor_shape(shape_before_reshape).set_num_channels(input_num_channles).set_quantization_info(input_qinfo); |
| 86 | |
| 87 | output_internal = &info_before_reshape; |
| 88 | } |
| 89 | |
| 90 | ARM_COMPUTE_RETURN_ON_ERROR(NEReductionOperationKernel::validate(input, output_internal, axis, op)); |
| 91 | |
| 92 | if(is_reshape_required) |
| 93 | { |
Michalis Spyrou | bcd2352 | 2020-05-21 15:02:36 +0100 | [diff] [blame] | 94 | ARM_COMPUTE_RETURN_ON_ERROR(NEReshapeLayer::validate(output_internal, output)); |
Sang-Hoon Park | 2697fd8 | 2019-10-15 16:49:24 +0100 | [diff] [blame] | 95 | } |
John Richardson | 73d4aef | 2018-05-08 14:34:33 +0100 | [diff] [blame] | 96 | |
| 97 | return Status{}; |
| 98 | } |
| 99 | |
Sang-Hoon Park | 2697fd8 | 2019-10-15 16:49:24 +0100 | [diff] [blame] | 100 | void NEReductionOperation::configure(ITensor *input, ITensor *output, unsigned int axis, ReductionOperation op, bool keep_dims) |
Georgios Pinitas | d976958 | 2017-08-03 10:19:40 +0100 | [diff] [blame] | 101 | { |
giuros01 | 154bc1c | 2019-03-26 17:44:40 +0000 | [diff] [blame] | 102 | ARM_COMPUTE_ERROR_ON_NULLPTR(input, output); |
Sang-Hoon Park | 2697fd8 | 2019-10-15 16:49:24 +0100 | [diff] [blame] | 103 | |
| 104 | _is_reshape_required = !keep_dims; |
| 105 | |
| 106 | auto *output_internal = output; |
| 107 | const auto is_arg_min_max = (op == ReductionOperation::ARG_IDX_MAX) || (op == ReductionOperation::ARG_IDX_MIN); |
| 108 | |
| 109 | if(_is_reshape_required) |
| 110 | { |
| 111 | const auto output_internal_shape = arm_compute::misc::shape_calculator::compute_reduced_shape(input->info()->tensor_shape(), axis); |
| 112 | const auto output_external_shape = arm_compute::misc::shape_calculator::compute_reduced_shape(input->info()->tensor_shape(), axis, false); |
Sang-Hoon Park | eaa01ab | 2019-11-11 17:33:28 +0000 | [diff] [blame] | 113 | const auto output_data_type = is_arg_min_max ? DataType::S32 : input->info()->data_type(); |
Sang-Hoon Park | 2697fd8 | 2019-10-15 16:49:24 +0100 | [diff] [blame] | 114 | const auto num_channels = input->info()->num_channels(); |
| 115 | const auto qinfo = input->info()->quantization_info(); |
| 116 | |
| 117 | _output_internal.allocator()->init(input->info()->clone()->set_data_type(output_data_type).set_tensor_shape(output_internal_shape).reset_padding().set_is_resizable(true).set_num_channels( |
| 118 | num_channels).set_quantization_info(qinfo)); |
| 119 | _memory_group.manage(&_output_internal); |
| 120 | output_internal = &_output_internal; |
| 121 | auto_init_if_empty(*output->info(), input->info()->clone()->set_data_type(output_data_type).set_tensor_shape(output_external_shape).reset_padding().set_is_resizable(true)); |
| 122 | } |
| 123 | |
| 124 | ARM_COMPUTE_ERROR_THROW_ON(NEReductionOperation::validate(input->info(), output->info(), axis, op, keep_dims)); |
Georgios Pinitas | d976958 | 2017-08-03 10:19:40 +0100 | [diff] [blame] | 125 | |
| 126 | // Configure reduction kernel |
Sang-Hoon Park | 2697fd8 | 2019-10-15 16:49:24 +0100 | [diff] [blame] | 127 | _reduction_kernel.configure(input, output_internal, axis, op); |
Michalis Spyrou | bcf8a96 | 2018-10-12 10:51:31 +0100 | [diff] [blame] | 128 | _window_split = reduction_window_split_dimension(axis); |
| 129 | _reduction_axis = axis; |
Georgios Pinitas | d976958 | 2017-08-03 10:19:40 +0100 | [diff] [blame] | 130 | |
Michalis Spyrou | bcf8a96 | 2018-10-12 10:51:31 +0100 | [diff] [blame] | 131 | if(axis == 0) |
| 132 | { |
| 133 | // Configure fill border kernel |
Manuel Bottini | 1d4f385 | 2019-01-14 15:14:43 +0000 | [diff] [blame] | 134 | const BorderSize fill_border_size = _reduction_kernel.border_size(); |
Usama Arif | a4a08ad | 2019-05-20 12:38:33 +0100 | [diff] [blame] | 135 | PixelValue pixelValue; |
| 136 | switch(op) |
| 137 | { |
| 138 | case ReductionOperation::PROD: |
| 139 | { |
| 140 | pixelValue = PixelValue(1, input->info()->data_type(), input->info()->quantization_info()); |
| 141 | break; |
| 142 | } |
| 143 | case ReductionOperation::MIN: |
| 144 | { |
Luca Foschiani | ee939fb | 2020-01-28 10:38:07 +0000 | [diff] [blame] | 145 | pixelValue = std::get<1>(get_min_max(input->info()->data_type())); |
Usama Arif | a4a08ad | 2019-05-20 12:38:33 +0100 | [diff] [blame] | 146 | break; |
| 147 | } |
Usama Arif | 28f0dd9 | 2019-05-20 13:44:34 +0100 | [diff] [blame] | 148 | case ReductionOperation::MAX: |
| 149 | { |
Luca Foschiani | ee939fb | 2020-01-28 10:38:07 +0000 | [diff] [blame] | 150 | pixelValue = std::get<0>(get_min_max(input->info()->data_type())); |
Usama Arif | 28f0dd9 | 2019-05-20 13:44:34 +0100 | [diff] [blame] | 151 | break; |
| 152 | } |
Usama Arif | a4a08ad | 2019-05-20 12:38:33 +0100 | [diff] [blame] | 153 | case ReductionOperation::ARG_IDX_MAX: |
| 154 | case ReductionOperation::ARG_IDX_MIN: |
Michalis Spyrou | 19bd412 | 2020-01-22 10:27:06 +0000 | [diff] [blame] | 155 | { |
| 156 | pixelValue = PixelValue(0, input->info()->data_type(), input->info()->quantization_info()); |
| 157 | break; |
| 158 | } |
Usama Arif | a4a08ad | 2019-05-20 12:38:33 +0100 | [diff] [blame] | 159 | case ReductionOperation::MEAN_SUM: |
| 160 | case ReductionOperation::SUM_SQUARE: |
| 161 | case ReductionOperation::SUM: |
| 162 | { |
Michalis Spyrou | 19bd412 | 2020-01-22 10:27:06 +0000 | [diff] [blame] | 163 | pixelValue = PixelValue(static_cast<uint32_t>(0)); |
Usama Arif | a4a08ad | 2019-05-20 12:38:33 +0100 | [diff] [blame] | 164 | break; |
| 165 | } |
| 166 | default: |
| 167 | ARM_COMPUTE_ERROR("Reduction Operation unsupported"); |
| 168 | } |
Sang-Hoon Park | 2697fd8 | 2019-10-15 16:49:24 +0100 | [diff] [blame] | 169 | _fill_border_kernel.configure(input, fill_border_size, (is_arg_min_max ? BorderMode::REPLICATE : BorderMode::CONSTANT), pixelValue); |
| 170 | } |
| 171 | |
| 172 | if(_is_reshape_required) |
| 173 | { |
Michalis Spyrou | bcd2352 | 2020-05-21 15:02:36 +0100 | [diff] [blame] | 174 | _reshape.configure(output_internal, output); |
Sang-Hoon Park | 2697fd8 | 2019-10-15 16:49:24 +0100 | [diff] [blame] | 175 | _output_internal.allocator()->allocate(); |
Michalis Spyrou | bcf8a96 | 2018-10-12 10:51:31 +0100 | [diff] [blame] | 176 | } |
Georgios Pinitas | d976958 | 2017-08-03 10:19:40 +0100 | [diff] [blame] | 177 | } |
| 178 | |
| 179 | void NEReductionOperation::run() |
| 180 | { |
Michalis Spyrou | bcf8a96 | 2018-10-12 10:51:31 +0100 | [diff] [blame] | 181 | if(_reduction_axis == 0) |
| 182 | { |
| 183 | NEScheduler::get().schedule(&_fill_border_kernel, Window::DimY); |
| 184 | } |
Georgios Pinitas | d976958 | 2017-08-03 10:19:40 +0100 | [diff] [blame] | 185 | NEScheduler::get().schedule(&_reduction_kernel, _window_split); |
Sang-Hoon Park | 2697fd8 | 2019-10-15 16:49:24 +0100 | [diff] [blame] | 186 | if(_is_reshape_required) |
| 187 | { |
Michalis Spyrou | bcd2352 | 2020-05-21 15:02:36 +0100 | [diff] [blame] | 188 | _reshape.run(); |
Sang-Hoon Park | 2697fd8 | 2019-10-15 16:49:24 +0100 | [diff] [blame] | 189 | } |
Georgios Pinitas | d976958 | 2017-08-03 10:19:40 +0100 | [diff] [blame] | 190 | } |
Michalis Spyrou | bcf8a96 | 2018-10-12 10:51:31 +0100 | [diff] [blame] | 191 | } // namespace arm_compute |