Michalis Spyrou | bcf8a96 | 2018-10-12 10:51:31 +0100 | [diff] [blame] | 1 | /* |
George Wort | 5a97b28 | 2018-12-21 16:21:04 +0000 | [diff] [blame] | 2 | * Copyright (c) 2018-2019 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 | |
Georgios Pinitas | 32bd4dd | 2019-05-16 14:23:00 +0100 | [diff] [blame] | 26 | #include "arm_compute/core/CPP/Validate.h" |
Pablo Tello | 9397515 | 2019-11-08 13:47:53 +0000 | [diff] [blame] | 27 | #include "arm_compute/core/Error.h" |
Michalis Spyrou | bcf8a96 | 2018-10-12 10:51:31 +0100 | [diff] [blame] | 28 | #include "arm_compute/core/Helpers.h" |
| 29 | #include "arm_compute/runtime/NEON/NEScheduler.h" |
| 30 | |
Pablo Tello | 9397515 | 2019-11-08 13:47:53 +0000 | [diff] [blame] | 31 | namespace arm_compute |
| 32 | { |
| 33 | namespace |
| 34 | { |
| 35 | inline TensorShape calculate_reduce_mean_shape(ITensor *input, const Coordinates &reduction_axis, bool keep_dims) |
| 36 | { |
| 37 | const int reduction_ops = reduction_axis.num_dimensions(); |
| 38 | Coordinates axis_local = reduction_axis; |
| 39 | const int input_dims = input->info()->num_dimensions(); |
| 40 | convert_negative_axis(axis_local, input_dims); |
| 41 | TensorShape out_shape = input->info()->tensor_shape(); |
| 42 | // Configure reshape layer if we want to drop the dimensions |
| 43 | if(!keep_dims) |
| 44 | { |
| 45 | // We have to sort the reduction axis vectors in order for remove_dimension |
| 46 | // to work properly |
| 47 | std::sort(axis_local.begin(), axis_local.begin() + reduction_ops); |
| 48 | for(int i = 0; i < reduction_ops; ++i) |
| 49 | { |
| 50 | out_shape.remove_dimension(axis_local[i] - i); |
| 51 | } |
| 52 | return out_shape; |
| 53 | } |
| 54 | else |
| 55 | { |
| 56 | for(int i = 0; i < reduction_ops; ++i) |
| 57 | { |
| 58 | out_shape.set(axis_local[i], 1); |
| 59 | } |
| 60 | return out_shape; |
| 61 | } |
| 62 | } |
| 63 | } // namespace |
Michalis Spyrou | bcf8a96 | 2018-10-12 10:51:31 +0100 | [diff] [blame] | 64 | |
| 65 | NEReduceMean::NEReduceMean(std::shared_ptr<IMemoryManager> memory_manager) |
| 66 | : _memory_group(std::move(memory_manager)), _reduction_kernels(), _reduced_outs(), _reshape(), _reduction_ops(), _keep_dims() |
| 67 | { |
| 68 | } |
| 69 | |
Pablo Tello | 9397515 | 2019-11-08 13:47:53 +0000 | [diff] [blame] | 70 | 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] | 71 | { |
| 72 | ARM_COMPUTE_UNUSED(keep_dims); |
Pablo Tello | 9397515 | 2019-11-08 13:47:53 +0000 | [diff] [blame] | 73 | ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(input, output); |
Georgios Pinitas | 32bd4dd | 2019-05-16 14:23:00 +0100 | [diff] [blame] | 74 | ARM_COMPUTE_RETURN_ERROR_ON_CPU_F16_UNSUPPORTED(input); |
| 75 | ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::QASYMM8, DataType::F16, DataType::F32); |
Pablo Tello | 9397515 | 2019-11-08 13:47:53 +0000 | [diff] [blame] | 76 | ARM_COMPUTE_RETURN_ERROR_ON(reduction_axis.num_dimensions() < 1); |
Michalis Spyrou | bcf8a96 | 2018-10-12 10:51:31 +0100 | [diff] [blame] | 77 | ARM_COMPUTE_RETURN_ERROR_ON(reduction_axis.num_dimensions() > input->num_dimensions()); |
| 78 | |
Michalis Spyrou | 8d1b718 | 2019-01-02 15:54:03 +0000 | [diff] [blame] | 79 | const unsigned int reduction_ops = reduction_axis.num_dimensions(); |
| 80 | const int input_dims = input->num_dimensions(); |
| 81 | Coordinates axis_local = reduction_axis; |
Michalis Spyrou | bcf8a96 | 2018-10-12 10:51:31 +0100 | [diff] [blame] | 82 | |
Pablo Tello | 9397515 | 2019-11-08 13:47:53 +0000 | [diff] [blame] | 83 | for(unsigned int i = 0; i < axis_local.num_dimensions(); ++i) |
Michalis Spyrou | 8d1b718 | 2019-01-02 15:54:03 +0000 | [diff] [blame] | 84 | { |
Pablo Tello | 9397515 | 2019-11-08 13:47:53 +0000 | [diff] [blame] | 85 | //axis: The dimensions to reduce. Must be in the range [-rank(input_tensor), rank(input_tensor)). |
| 86 | ARM_COMPUTE_RETURN_ERROR_ON(axis_local[i] < (-static_cast<int>(input->num_dimensions()))); |
| 87 | 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] | 88 | } |
| 89 | |
Pablo Tello | 9397515 | 2019-11-08 13:47:53 +0000 | [diff] [blame] | 90 | if(output->tensor_shape().total_size() != 0) |
Michalis Spyrou | 8d1b718 | 2019-01-02 15:54:03 +0000 | [diff] [blame] | 91 | { |
Pablo Tello | 9397515 | 2019-11-08 13:47:53 +0000 | [diff] [blame] | 92 | // Only validate if not using auto_init for the output tensor |
| 93 | TensorShape out_shape = input->tensor_shape(); |
| 94 | // Validate output_shape only if not using auto_init |
| 95 | convert_negative_axis(axis_local, input_dims); |
| 96 | std::sort(axis_local.begin(), axis_local.begin() + reduction_ops); |
| 97 | for(unsigned int i = 0; i < reduction_ops; ++i) |
Michalis Spyrou | 8d1b718 | 2019-01-02 15:54:03 +0000 | [diff] [blame] | 98 | { |
Pablo Tello | 9397515 | 2019-11-08 13:47:53 +0000 | [diff] [blame] | 99 | ARM_COMPUTE_RETURN_ERROR_ON(axis_local[i] > 3); |
| 100 | ARM_COMPUTE_RETURN_ERROR_ON(static_cast<unsigned int>(axis_local[i]) > input->num_dimensions() - 1); |
| 101 | if(output->total_size() > 0 && keep_dims) |
| 102 | { |
| 103 | ARM_COMPUTE_RETURN_ERROR_ON(output->dimension(axis_local[i]) != 1); |
| 104 | } |
| 105 | if(keep_dims) |
| 106 | { |
| 107 | out_shape.set(axis_local[i], 1); |
| 108 | } |
| 109 | else |
| 110 | { |
| 111 | ARM_COMPUTE_RETURN_ERROR_ON(i > static_cast<unsigned int>(axis_local[i])); |
| 112 | const unsigned int remove_index = axis_local[i] - i; |
| 113 | ARM_COMPUTE_RETURN_ERROR_ON(remove_index >= out_shape.num_dimensions()); |
| 114 | out_shape.remove_dimension(remove_index); |
| 115 | } |
Michalis Spyrou | 8d1b718 | 2019-01-02 15:54:03 +0000 | [diff] [blame] | 116 | } |
Pablo Tello | 9397515 | 2019-11-08 13:47:53 +0000 | [diff] [blame] | 117 | const TensorInfo out_info = input->clone()->set_tensor_shape(out_shape); |
| 118 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_SHAPES(output, &out_info); |
Michalis Spyrou | 8d1b718 | 2019-01-02 15:54:03 +0000 | [diff] [blame] | 119 | } |
Michalis Spyrou | bcf8a96 | 2018-10-12 10:51:31 +0100 | [diff] [blame] | 120 | return Status{}; |
| 121 | } |
| 122 | |
Pablo Tello | 9397515 | 2019-11-08 13:47:53 +0000 | [diff] [blame] | 123 | Status NEReduceMean::validate(const ITensorInfo *input, const Coordinates &reduction_axis, bool keep_dims, const ITensorInfo *output) |
| 124 | { |
| 125 | return validate_config(input, reduction_axis, keep_dims, output); |
| 126 | } |
| 127 | |
Michalis Spyrou | bcf8a96 | 2018-10-12 10:51:31 +0100 | [diff] [blame] | 128 | void NEReduceMean::configure(ITensor *input, const Coordinates &reduction_axis, bool keep_dims, ITensor *output) |
| 129 | { |
Pablo Tello | 9397515 | 2019-11-08 13:47:53 +0000 | [diff] [blame] | 130 | // Perform validate step |
| 131 | ARM_COMPUTE_ERROR_THROW_ON(NEReduceMean::validate(input->info(), reduction_axis, keep_dims, output->info())); |
| 132 | // Output auto inizialitation if not yet initialized |
| 133 | const TensorShape output_shape = calculate_reduce_mean_shape(input, reduction_axis, keep_dims); |
| 134 | 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] | 135 | |
Michalis Spyrou | bcfd09a | 2019-05-01 13:03:59 +0100 | [diff] [blame] | 136 | _reduction_ops = reduction_axis.num_dimensions(); |
| 137 | _reduction_kernels.resize(_reduction_ops); |
| 138 | _reduced_outs.resize(_reduction_ops - (keep_dims ? 1 : 0)); |
| 139 | _keep_dims = keep_dims; |
Michalis Spyrou | bcf8a96 | 2018-10-12 10:51:31 +0100 | [diff] [blame] | 140 | |
Pablo Tello | 9397515 | 2019-11-08 13:47:53 +0000 | [diff] [blame] | 141 | Coordinates axis_local = reduction_axis; |
| 142 | const int input_dims = input->info()->num_dimensions(); |
Michalis Spyrou | 8d1b718 | 2019-01-02 15:54:03 +0000 | [diff] [blame] | 143 | |
Pablo Tello | 9397515 | 2019-11-08 13:47:53 +0000 | [diff] [blame] | 144 | convert_negative_axis(axis_local, input_dims); |
Michalis Spyrou | 8d1b718 | 2019-01-02 15:54:03 +0000 | [diff] [blame] | 145 | |
Michalis Spyrou | bcf8a96 | 2018-10-12 10:51:31 +0100 | [diff] [blame] | 146 | // Perform reduction for every axis |
Pablo Tello | 9397515 | 2019-11-08 13:47:53 +0000 | [diff] [blame] | 147 | for(int i = 0; i < _reduction_ops; ++i) |
Michalis Spyrou | bcf8a96 | 2018-10-12 10:51:31 +0100 | [diff] [blame] | 148 | { |
Michalis Spyrou | bcfd09a | 2019-05-01 13:03:59 +0100 | [diff] [blame] | 149 | TensorShape out_shape = i == 0 ? input->info()->tensor_shape() : (&_reduced_outs[i - 1])->info()->tensor_shape(); |
Michalis Spyrou | 8d1b718 | 2019-01-02 15:54:03 +0000 | [diff] [blame] | 150 | out_shape.set(axis_local[i], 1); |
Michalis Spyrou | bcfd09a | 2019-05-01 13:03:59 +0100 | [diff] [blame] | 151 | auto in = (i == 0) ? input : (&_reduced_outs[i - 1]); |
Michalis Spyrou | bcf8a96 | 2018-10-12 10:51:31 +0100 | [diff] [blame] | 152 | |
| 153 | if(i == _reduction_ops - 1 && keep_dims) |
| 154 | { |
Michalis Spyrou | 8d1b718 | 2019-01-02 15:54:03 +0000 | [diff] [blame] | 155 | _reduction_kernels[i].configure(in, output, axis_local[i], ReductionOperation::MEAN_SUM); |
Michalis Spyrou | bcf8a96 | 2018-10-12 10:51:31 +0100 | [diff] [blame] | 156 | } |
| 157 | else |
| 158 | { |
Isabella Gottardi | 0a1090a | 2019-02-14 18:07:36 +0000 | [diff] [blame] | 159 | _reduced_outs[i].allocator()->init(TensorInfo(out_shape, input->info()->num_channels(), input->info()->data_type(), input->info()->quantization_info())); |
Michalis Spyrou | bcfd09a | 2019-05-01 13:03:59 +0100 | [diff] [blame] | 160 | _memory_group.manage(&_reduced_outs[i]); |
| 161 | _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] | 162 | } |
| 163 | } |
| 164 | |
| 165 | // Allocate intermediate tensors |
Pablo Tello | 9397515 | 2019-11-08 13:47:53 +0000 | [diff] [blame] | 166 | for(int i = 0; i < _reduction_ops - (keep_dims ? 1 : 0); ++i) |
Michalis Spyrou | bcf8a96 | 2018-10-12 10:51:31 +0100 | [diff] [blame] | 167 | { |
| 168 | _reduced_outs[i].allocator()->allocate(); |
| 169 | } |
| 170 | |
| 171 | // Configure reshape layer if we want to drop the dimensions |
| 172 | if(!keep_dims) |
| 173 | { |
| 174 | TensorShape out_shape = input->info()->tensor_shape(); |
Michalis Spyrou | 8d1b718 | 2019-01-02 15:54:03 +0000 | [diff] [blame] | 175 | // We have to sort the reduction axis vectors in order for remove_dimension |
| 176 | // to work properly |
| 177 | std::sort(axis_local.begin(), axis_local.begin() + _reduction_ops); |
Pablo Tello | 9397515 | 2019-11-08 13:47:53 +0000 | [diff] [blame] | 178 | for(int i = 0; i < _reduction_ops; ++i) |
Michalis Spyrou | bcf8a96 | 2018-10-12 10:51:31 +0100 | [diff] [blame] | 179 | { |
Michalis Spyrou | 8d1b718 | 2019-01-02 15:54:03 +0000 | [diff] [blame] | 180 | out_shape.remove_dimension(axis_local[i] - i); |
Michalis Spyrou | bcf8a96 | 2018-10-12 10:51:31 +0100 | [diff] [blame] | 181 | } |
| 182 | auto_init_if_empty(*output->info(), input->info()->clone()->set_tensor_shape(out_shape)); |
Michalis Spyrou | bcfd09a | 2019-05-01 13:03:59 +0100 | [diff] [blame] | 183 | _reshape.configure(&_reduced_outs[_reduction_ops - 1], output); |
Michalis Spyrou | bcf8a96 | 2018-10-12 10:51:31 +0100 | [diff] [blame] | 184 | } |
| 185 | } |
| 186 | |
| 187 | void NEReduceMean::run() |
| 188 | { |
Georgios Pinitas | da953f2 | 2019-04-02 17:27:03 +0100 | [diff] [blame] | 189 | MemoryGroupResourceScope scope_mg(_memory_group); |
Pablo Tello | 9397515 | 2019-11-08 13:47:53 +0000 | [diff] [blame] | 190 | for(auto &kernel : _reduction_kernels) |
Michalis Spyrou | bcf8a96 | 2018-10-12 10:51:31 +0100 | [diff] [blame] | 191 | { |
Pablo Tello | 9397515 | 2019-11-08 13:47:53 +0000 | [diff] [blame] | 192 | kernel.run(); |
Michalis Spyrou | bcf8a96 | 2018-10-12 10:51:31 +0100 | [diff] [blame] | 193 | } |
| 194 | |
| 195 | if(!_keep_dims) |
| 196 | { |
| 197 | _reshape.run(); |
| 198 | } |
Michalis Spyrou | bcf8a96 | 2018-10-12 10:51:31 +0100 | [diff] [blame] | 199 | } |
Pablo Tello | 9397515 | 2019-11-08 13:47:53 +0000 | [diff] [blame] | 200 | } // namespace arm_compute |