Michalis Spyrou | bcf8a96 | 2018-10-12 10:51:31 +0100 | [diff] [blame] | 1 | /* |
ramelg01 | cbbb038 | 2021-09-17 17:36:57 +0100 | [diff] [blame] | 2 | * Copyright (c) 2018-2021 Arm Limited. |
Michalis Spyrou | bcf8a96 | 2018-10-12 10:51:31 +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 |
George Wort | 5a97b28 | 2018-12-21 16:21:04 +0000 | [diff] [blame] | 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
Michalis Spyrou | bcf8a96 | 2018-10-12 10:51:31 +0100 | [diff] [blame] | 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
George Wort | 5a97b28 | 2018-12-21 16:21:04 +0000 | [diff] [blame] | 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
Michalis Spyrou | bcf8a96 | 2018-10-12 10:51:31 +0100 | [diff] [blame] | 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/NEReduceMean.h" |
| 25 | |
Pablo Tello | 9397515 | 2019-11-08 13:47:53 +0000 | [diff] [blame] | 26 | #include "arm_compute/core/Error.h" |
Michalis Spyrou | bcf8a96 | 2018-10-12 10:51:31 +0100 | [diff] [blame] | 27 | #include "arm_compute/core/Helpers.h" |
Pablo Tello | a0a4ba1 | 2019-12-11 13:04:34 +0000 | [diff] [blame] | 28 | #include "arm_compute/core/utils/misc/ShapeCalculator.h" |
Michalis Spyrou | bcf8a96 | 2018-10-12 10:51:31 +0100 | [diff] [blame] | 29 | #include "arm_compute/runtime/NEON/NEScheduler.h" |
ramelg01 | cbbb038 | 2021-09-17 17:36:57 +0100 | [diff] [blame] | 30 | #include "src/common/utils/Log.h" |
Sang-Hoon Park | 68dd25f | 2020-10-19 16:00:11 +0100 | [diff] [blame] | 31 | #include "src/core/CPP/Validate.h" |
Michalis Spyrou | ebcebf1 | 2020-10-21 00:04:14 +0100 | [diff] [blame] | 32 | #include "src/core/NEON/kernels/NEReductionOperationKernel.h" |
Sang-Hoon Park | 68dd25f | 2020-10-19 16:00:11 +0100 | [diff] [blame] | 33 | #include "src/core/helpers/AutoConfiguration.h" |
Michalis Spyrou | bcf8a96 | 2018-10-12 10:51:31 +0100 | [diff] [blame] | 34 | |
Pablo Tello | 9397515 | 2019-11-08 13:47:53 +0000 | [diff] [blame] | 35 | namespace arm_compute |
| 36 | { |
| 37 | namespace |
| 38 | { |
Pablo Tello | 9397515 | 2019-11-08 13:47:53 +0000 | [diff] [blame] | 39 | Status validate_config(const ITensorInfo *input, const Coordinates &reduction_axis, bool keep_dims, const ITensorInfo *output) |
Michalis Spyrou | bcf8a96 | 2018-10-12 10:51:31 +0100 | [diff] [blame] | 40 | { |
| 41 | ARM_COMPUTE_UNUSED(keep_dims); |
Pablo Tello | 9397515 | 2019-11-08 13:47:53 +0000 | [diff] [blame] | 42 | ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(input, output); |
Georgios Pinitas | 32bd4dd | 2019-05-16 14:23:00 +0100 | [diff] [blame] | 43 | ARM_COMPUTE_RETURN_ERROR_ON_CPU_F16_UNSUPPORTED(input); |
Luca Foschiani | ee939fb | 2020-01-28 10:38:07 +0000 | [diff] [blame] | 44 | ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::QASYMM8_SIGNED, DataType::QASYMM8, DataType::F16, DataType::F32); |
Pablo Tello | 9397515 | 2019-11-08 13:47:53 +0000 | [diff] [blame] | 45 | ARM_COMPUTE_RETURN_ERROR_ON(reduction_axis.num_dimensions() < 1); |
Michalis Spyrou | bcf8a96 | 2018-10-12 10:51:31 +0100 | [diff] [blame] | 46 | ARM_COMPUTE_RETURN_ERROR_ON(reduction_axis.num_dimensions() > input->num_dimensions()); |
| 47 | |
Michalis Spyrou | 8d1b718 | 2019-01-02 15:54:03 +0000 | [diff] [blame] | 48 | const unsigned int reduction_ops = reduction_axis.num_dimensions(); |
| 49 | const int input_dims = input->num_dimensions(); |
| 50 | Coordinates axis_local = reduction_axis; |
Michalis Spyrou | bcf8a96 | 2018-10-12 10:51:31 +0100 | [diff] [blame] | 51 | |
Pablo Tello | 9397515 | 2019-11-08 13:47:53 +0000 | [diff] [blame] | 52 | for(unsigned int i = 0; i < axis_local.num_dimensions(); ++i) |
Michalis Spyrou | 8d1b718 | 2019-01-02 15:54:03 +0000 | [diff] [blame] | 53 | { |
Pablo Tello | 9397515 | 2019-11-08 13:47:53 +0000 | [diff] [blame] | 54 | //axis: The dimensions to reduce. Must be in the range [-rank(input_tensor), rank(input_tensor)). |
| 55 | ARM_COMPUTE_RETURN_ERROR_ON(axis_local[i] < (-static_cast<int>(input->num_dimensions()))); |
| 56 | ARM_COMPUTE_RETURN_ERROR_ON(axis_local[i] >= static_cast<int>(input->num_dimensions())); |
Michalis Spyrou | bcf8a96 | 2018-10-12 10:51:31 +0100 | [diff] [blame] | 57 | } |
| 58 | |
Pablo Tello | 9397515 | 2019-11-08 13:47:53 +0000 | [diff] [blame] | 59 | if(output->tensor_shape().total_size() != 0) |
Michalis Spyrou | 8d1b718 | 2019-01-02 15:54:03 +0000 | [diff] [blame] | 60 | { |
Pablo Tello | 9397515 | 2019-11-08 13:47:53 +0000 | [diff] [blame] | 61 | // Only validate if not using auto_init for the output tensor |
| 62 | TensorShape out_shape = input->tensor_shape(); |
| 63 | // Validate output_shape only if not using auto_init |
| 64 | convert_negative_axis(axis_local, input_dims); |
| 65 | std::sort(axis_local.begin(), axis_local.begin() + reduction_ops); |
| 66 | for(unsigned int i = 0; i < reduction_ops; ++i) |
Michalis Spyrou | 8d1b718 | 2019-01-02 15:54:03 +0000 | [diff] [blame] | 67 | { |
Pablo Tello | 9397515 | 2019-11-08 13:47:53 +0000 | [diff] [blame] | 68 | ARM_COMPUTE_RETURN_ERROR_ON(axis_local[i] > 3); |
| 69 | ARM_COMPUTE_RETURN_ERROR_ON(static_cast<unsigned int>(axis_local[i]) > input->num_dimensions() - 1); |
| 70 | if(output->total_size() > 0 && keep_dims) |
| 71 | { |
| 72 | ARM_COMPUTE_RETURN_ERROR_ON(output->dimension(axis_local[i]) != 1); |
| 73 | } |
| 74 | if(keep_dims) |
| 75 | { |
| 76 | out_shape.set(axis_local[i], 1); |
| 77 | } |
| 78 | else |
| 79 | { |
| 80 | ARM_COMPUTE_RETURN_ERROR_ON(i > static_cast<unsigned int>(axis_local[i])); |
| 81 | const unsigned int remove_index = axis_local[i] - i; |
| 82 | ARM_COMPUTE_RETURN_ERROR_ON(remove_index >= out_shape.num_dimensions()); |
| 83 | out_shape.remove_dimension(remove_index); |
| 84 | } |
Michalis Spyrou | 8d1b718 | 2019-01-02 15:54:03 +0000 | [diff] [blame] | 85 | } |
Pablo Tello | 9397515 | 2019-11-08 13:47:53 +0000 | [diff] [blame] | 86 | const TensorInfo out_info = input->clone()->set_tensor_shape(out_shape); |
| 87 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_SHAPES(output, &out_info); |
Manuel Bottini | c58f0ad | 2020-08-07 16:49:15 +0100 | [diff] [blame] | 88 | const bool requant = is_data_type_quantized(input->data_type()) && input->quantization_info() != output->quantization_info(); |
| 89 | if(requant) |
| 90 | { |
| 91 | TensorInfo input_no_quant(input->clone()->set_data_type(DataType::F32)); |
| 92 | NEDequantizationLayer::validate(input, &input_no_quant); |
| 93 | TensorInfo output_no_quant(output->clone()->set_data_type(DataType::F32)); |
| 94 | NEQuantizationLayer::validate(&output_no_quant, output); |
| 95 | } |
Michalis Spyrou | 8d1b718 | 2019-01-02 15:54:03 +0000 | [diff] [blame] | 96 | } |
Michalis Spyrou | bcf8a96 | 2018-10-12 10:51:31 +0100 | [diff] [blame] | 97 | return Status{}; |
| 98 | } |
Manuel Bottini | c58f0ad | 2020-08-07 16:49:15 +0100 | [diff] [blame] | 99 | } // namespace |
| 100 | |
Michalis Spyrou | ebcebf1 | 2020-10-21 00:04:14 +0100 | [diff] [blame] | 101 | NEReduceMean::~NEReduceMean() = default; |
| 102 | |
Manuel Bottini | c58f0ad | 2020-08-07 16:49:15 +0100 | [diff] [blame] | 103 | NEReduceMean::NEReduceMean(std::shared_ptr<IMemoryManager> memory_manager) |
| 104 | : _memory_group(std::move(memory_manager)), _reduction_kernels(), _reduced_outs(), _reshape(), _dequant(), _requant(), _reduction_ops(), _keep_dims(), _do_requant(), _input_no_quant(), |
| 105 | _output_no_quant() |
| 106 | { |
| 107 | } |
Michalis Spyrou | bcf8a96 | 2018-10-12 10:51:31 +0100 | [diff] [blame] | 108 | |
Pablo Tello | 9397515 | 2019-11-08 13:47:53 +0000 | [diff] [blame] | 109 | Status NEReduceMean::validate(const ITensorInfo *input, const Coordinates &reduction_axis, bool keep_dims, const ITensorInfo *output) |
| 110 | { |
| 111 | return validate_config(input, reduction_axis, keep_dims, output); |
| 112 | } |
| 113 | |
Michalis Spyrou | bcf8a96 | 2018-10-12 10:51:31 +0100 | [diff] [blame] | 114 | void NEReduceMean::configure(ITensor *input, const Coordinates &reduction_axis, bool keep_dims, ITensor *output) |
| 115 | { |
ramelg01 | cbbb038 | 2021-09-17 17:36:57 +0100 | [diff] [blame] | 116 | ARM_COMPUTE_LOG_PARAMS(input, reduction_axis, keep_dims, output); |
| 117 | |
Pablo Tello | 9397515 | 2019-11-08 13:47:53 +0000 | [diff] [blame] | 118 | // Perform validate step |
| 119 | ARM_COMPUTE_ERROR_THROW_ON(NEReduceMean::validate(input->info(), reduction_axis, keep_dims, output->info())); |
| 120 | // Output auto inizialitation if not yet initialized |
Manuel Bottini | c58f0ad | 2020-08-07 16:49:15 +0100 | [diff] [blame] | 121 | const TensorShape output_shape = arm_compute::misc::shape_calculator::calculate_reduce_mean_shape(input->info(), reduction_axis, keep_dims); |
Pablo Tello | 9397515 | 2019-11-08 13:47:53 +0000 | [diff] [blame] | 122 | auto_init_if_empty(*output->info(), input->info()->clone()->set_tensor_shape(output_shape)); |
Michalis Spyrou | bcf8a96 | 2018-10-12 10:51:31 +0100 | [diff] [blame] | 123 | |
Manuel Bottini | c58f0ad | 2020-08-07 16:49:15 +0100 | [diff] [blame] | 124 | _do_requant = is_data_type_quantized(input->info()->data_type()) && input->info()->quantization_info() != output->info()->quantization_info(); |
Michalis Spyrou | bcfd09a | 2019-05-01 13:03:59 +0100 | [diff] [blame] | 125 | _reduction_ops = reduction_axis.num_dimensions(); |
| 126 | _reduction_kernels.resize(_reduction_ops); |
| 127 | _reduced_outs.resize(_reduction_ops - (keep_dims ? 1 : 0)); |
| 128 | _keep_dims = keep_dims; |
Michalis Spyrou | bcf8a96 | 2018-10-12 10:51:31 +0100 | [diff] [blame] | 129 | |
Manuel Bottini | c58f0ad | 2020-08-07 16:49:15 +0100 | [diff] [blame] | 130 | ITensor *tmp_input = input; |
| 131 | ITensor *tmp_output = output; |
| 132 | if(_do_requant) |
| 133 | { |
| 134 | _memory_group.manage(&_input_no_quant); |
| 135 | _memory_group.manage(&_output_no_quant); |
| 136 | TensorInfo output_no_quant_info = input->info()->clone()->set_tensor_shape(output_shape); |
| 137 | output_no_quant_info.set_data_type(DataType::F32); |
| 138 | auto_init_if_empty(*_output_no_quant.info(), output_no_quant_info); |
| 139 | auto_init_if_empty(*_input_no_quant.info(), input->info()->clone()->set_data_type(DataType::F32)); |
| 140 | _dequant.configure(input, &_input_no_quant); |
| 141 | tmp_input = &_input_no_quant; |
| 142 | tmp_output = &_output_no_quant; |
| 143 | } |
| 144 | |
Pablo Tello | 9397515 | 2019-11-08 13:47:53 +0000 | [diff] [blame] | 145 | Coordinates axis_local = reduction_axis; |
Manuel Bottini | c58f0ad | 2020-08-07 16:49:15 +0100 | [diff] [blame] | 146 | const int input_dims = tmp_input->info()->num_dimensions(); |
Michalis Spyrou | 8d1b718 | 2019-01-02 15:54:03 +0000 | [diff] [blame] | 147 | |
Pablo Tello | 9397515 | 2019-11-08 13:47:53 +0000 | [diff] [blame] | 148 | convert_negative_axis(axis_local, input_dims); |
Michalis Spyrou | 8d1b718 | 2019-01-02 15:54:03 +0000 | [diff] [blame] | 149 | |
Michalis Spyrou | bcf8a96 | 2018-10-12 10:51:31 +0100 | [diff] [blame] | 150 | // Perform reduction for every axis |
Pablo Tello | 9397515 | 2019-11-08 13:47:53 +0000 | [diff] [blame] | 151 | for(int i = 0; i < _reduction_ops; ++i) |
Michalis Spyrou | bcf8a96 | 2018-10-12 10:51:31 +0100 | [diff] [blame] | 152 | { |
Manuel Bottini | c58f0ad | 2020-08-07 16:49:15 +0100 | [diff] [blame] | 153 | TensorShape out_shape = i == 0 ? tmp_input->info()->tensor_shape() : (&_reduced_outs[i - 1])->info()->tensor_shape(); |
Michalis Spyrou | 8d1b718 | 2019-01-02 15:54:03 +0000 | [diff] [blame] | 154 | out_shape.set(axis_local[i], 1); |
Manuel Bottini | c58f0ad | 2020-08-07 16:49:15 +0100 | [diff] [blame] | 155 | auto in = (i == 0) ? tmp_input : (&_reduced_outs[i - 1]); |
Michalis Spyrou | bcf8a96 | 2018-10-12 10:51:31 +0100 | [diff] [blame] | 156 | |
| 157 | if(i == _reduction_ops - 1 && keep_dims) |
| 158 | { |
Manuel Bottini | c58f0ad | 2020-08-07 16:49:15 +0100 | [diff] [blame] | 159 | _reduction_kernels[i].configure(in, tmp_output, axis_local[i], ReductionOperation::MEAN_SUM); |
Michalis Spyrou | bcf8a96 | 2018-10-12 10:51:31 +0100 | [diff] [blame] | 160 | } |
| 161 | else |
| 162 | { |
Manuel Bottini | c58f0ad | 2020-08-07 16:49:15 +0100 | [diff] [blame] | 163 | _reduced_outs[i].allocator()->init(TensorInfo(out_shape, tmp_input->info()->num_channels(), tmp_input->info()->data_type(), tmp_input->info()->quantization_info())); |
Michalis Spyrou | bcfd09a | 2019-05-01 13:03:59 +0100 | [diff] [blame] | 164 | _memory_group.manage(&_reduced_outs[i]); |
| 165 | _reduction_kernels[i].configure(in, &_reduced_outs[i], axis_local[i], ReductionOperation::MEAN_SUM); |
Michalis Spyrou | bcf8a96 | 2018-10-12 10:51:31 +0100 | [diff] [blame] | 166 | } |
| 167 | } |
| 168 | |
| 169 | // Allocate intermediate tensors |
Pablo Tello | 9397515 | 2019-11-08 13:47:53 +0000 | [diff] [blame] | 170 | for(int i = 0; i < _reduction_ops - (keep_dims ? 1 : 0); ++i) |
Michalis Spyrou | bcf8a96 | 2018-10-12 10:51:31 +0100 | [diff] [blame] | 171 | { |
| 172 | _reduced_outs[i].allocator()->allocate(); |
| 173 | } |
| 174 | |
| 175 | // Configure reshape layer if we want to drop the dimensions |
| 176 | if(!keep_dims) |
| 177 | { |
Manuel Bottini | c58f0ad | 2020-08-07 16:49:15 +0100 | [diff] [blame] | 178 | TensorShape out_shape = tmp_input->info()->tensor_shape(); |
Michalis Spyrou | 8d1b718 | 2019-01-02 15:54:03 +0000 | [diff] [blame] | 179 | // We have to sort the reduction axis vectors in order for remove_dimension |
| 180 | // to work properly |
| 181 | std::sort(axis_local.begin(), axis_local.begin() + _reduction_ops); |
Pablo Tello | 9397515 | 2019-11-08 13:47:53 +0000 | [diff] [blame] | 182 | for(int i = 0; i < _reduction_ops; ++i) |
Michalis Spyrou | bcf8a96 | 2018-10-12 10:51:31 +0100 | [diff] [blame] | 183 | { |
Michalis Spyrou | 8d1b718 | 2019-01-02 15:54:03 +0000 | [diff] [blame] | 184 | out_shape.remove_dimension(axis_local[i] - i); |
Michalis Spyrou | bcf8a96 | 2018-10-12 10:51:31 +0100 | [diff] [blame] | 185 | } |
Manuel Bottini | c58f0ad | 2020-08-07 16:49:15 +0100 | [diff] [blame] | 186 | auto_init_if_empty(*tmp_output->info(), tmp_input->info()->clone()->set_tensor_shape(out_shape)); |
| 187 | _reshape.configure(&_reduced_outs[_reduction_ops - 1], tmp_output); |
| 188 | } |
| 189 | if(_do_requant) |
| 190 | { |
| 191 | _requant.configure(&_output_no_quant, output); |
| 192 | _input_no_quant.allocator()->allocate(); |
| 193 | _output_no_quant.allocator()->allocate(); |
Michalis Spyrou | bcf8a96 | 2018-10-12 10:51:31 +0100 | [diff] [blame] | 194 | } |
| 195 | } |
| 196 | |
| 197 | void NEReduceMean::run() |
| 198 | { |
Georgios Pinitas | da953f2 | 2019-04-02 17:27:03 +0100 | [diff] [blame] | 199 | MemoryGroupResourceScope scope_mg(_memory_group); |
Manuel Bottini | c58f0ad | 2020-08-07 16:49:15 +0100 | [diff] [blame] | 200 | if(_do_requant) |
| 201 | { |
| 202 | _dequant.run(); |
| 203 | } |
Pablo Tello | 9397515 | 2019-11-08 13:47:53 +0000 | [diff] [blame] | 204 | for(auto &kernel : _reduction_kernels) |
Michalis Spyrou | bcf8a96 | 2018-10-12 10:51:31 +0100 | [diff] [blame] | 205 | { |
Pablo Tello | 9397515 | 2019-11-08 13:47:53 +0000 | [diff] [blame] | 206 | kernel.run(); |
Michalis Spyrou | bcf8a96 | 2018-10-12 10:51:31 +0100 | [diff] [blame] | 207 | } |
Michalis Spyrou | bcf8a96 | 2018-10-12 10:51:31 +0100 | [diff] [blame] | 208 | if(!_keep_dims) |
| 209 | { |
| 210 | _reshape.run(); |
| 211 | } |
Manuel Bottini | c58f0ad | 2020-08-07 16:49:15 +0100 | [diff] [blame] | 212 | if(_do_requant) |
| 213 | { |
| 214 | _requant.run(); |
| 215 | } |
Michalis Spyrou | bcf8a96 | 2018-10-12 10:51:31 +0100 | [diff] [blame] | 216 | } |
Pablo Tello | 9397515 | 2019-11-08 13:47:53 +0000 | [diff] [blame] | 217 | } // namespace arm_compute |