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" |
Michalis Spyrou | bcf8a96 | 2018-10-12 10:51:31 +0100 | [diff] [blame] | 27 | #include "arm_compute/core/Helpers.h" |
| 28 | #include "arm_compute/runtime/NEON/NEScheduler.h" |
| 29 | |
| 30 | using namespace arm_compute; |
| 31 | |
| 32 | NEReduceMean::NEReduceMean(std::shared_ptr<IMemoryManager> memory_manager) |
| 33 | : _memory_group(std::move(memory_manager)), _reduction_kernels(), _reduced_outs(), _reshape(), _reduction_ops(), _keep_dims() |
| 34 | { |
| 35 | } |
| 36 | |
| 37 | Status NEReduceMean::validate(const ITensorInfo *input, const Coordinates &reduction_axis, bool keep_dims, const ITensorInfo *output) |
| 38 | { |
| 39 | ARM_COMPUTE_UNUSED(keep_dims); |
| 40 | ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(input); |
Georgios Pinitas | 32bd4dd | 2019-05-16 14:23:00 +0100 | [diff] [blame] | 41 | ARM_COMPUTE_RETURN_ERROR_ON_CPU_F16_UNSUPPORTED(input); |
| 42 | ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::QASYMM8, DataType::F16, DataType::F32); |
Michalis Spyrou | bcf8a96 | 2018-10-12 10:51:31 +0100 | [diff] [blame] | 43 | ARM_COMPUTE_RETURN_ERROR_ON(reduction_axis.num_dimensions() > input->num_dimensions()); |
| 44 | |
Michalis Spyrou | 8d1b718 | 2019-01-02 15:54:03 +0000 | [diff] [blame] | 45 | TensorShape out_shape = input->tensor_shape(); |
| 46 | const unsigned int reduction_ops = reduction_axis.num_dimensions(); |
| 47 | const int input_dims = input->num_dimensions(); |
| 48 | Coordinates axis_local = reduction_axis; |
Michalis Spyrou | bcf8a96 | 2018-10-12 10:51:31 +0100 | [diff] [blame] | 49 | |
Michalis Spyrou | 8d1b718 | 2019-01-02 15:54:03 +0000 | [diff] [blame] | 50 | // Convert negative axis |
| 51 | for(unsigned int i = 0; i < reduction_ops; ++i) |
| 52 | { |
| 53 | axis_local[i] = wrap_around(axis_local[i], input_dims); |
Michalis Spyrou | bcf8a96 | 2018-10-12 10:51:31 +0100 | [diff] [blame] | 54 | } |
| 55 | |
Michalis Spyrou | 8d1b718 | 2019-01-02 15:54:03 +0000 | [diff] [blame] | 56 | std::sort(axis_local.begin(), axis_local.begin() + reduction_ops); |
| 57 | for(unsigned int i = 0; i < reduction_ops; ++i) |
| 58 | { |
| 59 | ARM_COMPUTE_RETURN_ERROR_ON(axis_local[i] > 3); |
| 60 | ARM_COMPUTE_RETURN_ERROR_ON(static_cast<unsigned int>(axis_local[i]) > input->num_dimensions() - 1); |
| 61 | if(output->total_size() > 0 && keep_dims) |
| 62 | { |
| 63 | ARM_COMPUTE_RETURN_ERROR_ON(output->dimension(axis_local[i]) != 1); |
| 64 | } |
| 65 | if(keep_dims) |
| 66 | { |
| 67 | out_shape.set(axis_local[i], 1); |
| 68 | } |
| 69 | else |
| 70 | { |
| 71 | out_shape.remove_dimension(axis_local[i] - i); |
| 72 | } |
| 73 | } |
| 74 | const TensorInfo out_info = input->clone()->set_tensor_shape(out_shape); |
| 75 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_SHAPES(output, &out_info); |
| 76 | |
Michalis Spyrou | bcf8a96 | 2018-10-12 10:51:31 +0100 | [diff] [blame] | 77 | return Status{}; |
| 78 | } |
| 79 | |
| 80 | void NEReduceMean::configure(ITensor *input, const Coordinates &reduction_axis, bool keep_dims, ITensor *output) |
| 81 | { |
| 82 | ARM_COMPUTE_ERROR_ON_NULLPTR(input); |
| 83 | |
Michalis Spyrou | bcfd09a | 2019-05-01 13:03:59 +0100 | [diff] [blame] | 84 | _reduction_ops = reduction_axis.num_dimensions(); |
| 85 | _reduction_kernels.resize(_reduction_ops); |
| 86 | _reduced_outs.resize(_reduction_ops - (keep_dims ? 1 : 0)); |
| 87 | _keep_dims = keep_dims; |
Michalis Spyrou | bcf8a96 | 2018-10-12 10:51:31 +0100 | [diff] [blame] | 88 | |
Michalis Spyrou | 8d1b718 | 2019-01-02 15:54:03 +0000 | [diff] [blame] | 89 | Coordinates axis_local = reduction_axis; |
| 90 | const int input_dims = input->info()->num_dimensions(); |
| 91 | const unsigned int reduction_ops = reduction_axis.num_dimensions(); |
| 92 | |
| 93 | // Convert negative axis |
| 94 | for(unsigned int i = 0; i < reduction_ops; ++i) |
| 95 | { |
| 96 | axis_local[i] = wrap_around(axis_local[i], input_dims); |
| 97 | } |
| 98 | |
Michalis Spyrou | bcf8a96 | 2018-10-12 10:51:31 +0100 | [diff] [blame] | 99 | // Perform reduction for every axis |
| 100 | for(unsigned int i = 0; i < _reduction_ops; ++i) |
| 101 | { |
Michalis Spyrou | bcfd09a | 2019-05-01 13:03:59 +0100 | [diff] [blame] | 102 | 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] | 103 | out_shape.set(axis_local[i], 1); |
Michalis Spyrou | bcfd09a | 2019-05-01 13:03:59 +0100 | [diff] [blame] | 104 | auto in = (i == 0) ? input : (&_reduced_outs[i - 1]); |
Michalis Spyrou | bcf8a96 | 2018-10-12 10:51:31 +0100 | [diff] [blame] | 105 | |
| 106 | if(i == _reduction_ops - 1 && keep_dims) |
| 107 | { |
Michalis Spyrou | 8d1b718 | 2019-01-02 15:54:03 +0000 | [diff] [blame] | 108 | _reduction_kernels[i].configure(in, output, axis_local[i], ReductionOperation::MEAN_SUM); |
Michalis Spyrou | bcf8a96 | 2018-10-12 10:51:31 +0100 | [diff] [blame] | 109 | } |
| 110 | else |
| 111 | { |
Isabella Gottardi | 0a1090a | 2019-02-14 18:07:36 +0000 | [diff] [blame] | 112 | _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] | 113 | _memory_group.manage(&_reduced_outs[i]); |
| 114 | _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] | 115 | } |
| 116 | } |
| 117 | |
| 118 | // Allocate intermediate tensors |
| 119 | for(unsigned int i = 0; i < _reduction_ops - (keep_dims ? 1 : 0); ++i) |
| 120 | { |
| 121 | _reduced_outs[i].allocator()->allocate(); |
| 122 | } |
| 123 | |
| 124 | // Configure reshape layer if we want to drop the dimensions |
| 125 | if(!keep_dims) |
| 126 | { |
| 127 | TensorShape out_shape = input->info()->tensor_shape(); |
Michalis Spyrou | 8d1b718 | 2019-01-02 15:54:03 +0000 | [diff] [blame] | 128 | |
| 129 | // We have to sort the reduction axis vectors in order for remove_dimension |
| 130 | // to work properly |
| 131 | std::sort(axis_local.begin(), axis_local.begin() + _reduction_ops); |
Michalis Spyrou | bcf8a96 | 2018-10-12 10:51:31 +0100 | [diff] [blame] | 132 | for(unsigned int i = 0; i < _reduction_ops; ++i) |
| 133 | { |
Michalis Spyrou | 8d1b718 | 2019-01-02 15:54:03 +0000 | [diff] [blame] | 134 | out_shape.remove_dimension(axis_local[i] - i); |
Michalis Spyrou | bcf8a96 | 2018-10-12 10:51:31 +0100 | [diff] [blame] | 135 | } |
| 136 | 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] | 137 | _reshape.configure(&_reduced_outs[_reduction_ops - 1], output); |
Michalis Spyrou | bcf8a96 | 2018-10-12 10:51:31 +0100 | [diff] [blame] | 138 | } |
| 139 | } |
| 140 | |
| 141 | void NEReduceMean::run() |
| 142 | { |
Georgios Pinitas | da953f2 | 2019-04-02 17:27:03 +0100 | [diff] [blame] | 143 | MemoryGroupResourceScope scope_mg(_memory_group); |
Michalis Spyrou | bcf8a96 | 2018-10-12 10:51:31 +0100 | [diff] [blame] | 144 | |
| 145 | for(unsigned int i = 0; i < _reduction_ops; ++i) |
| 146 | { |
| 147 | _reduction_kernels[i].run(); |
| 148 | } |
| 149 | |
| 150 | if(!_keep_dims) |
| 151 | { |
| 152 | _reshape.run(); |
| 153 | } |
Michalis Spyrou | bcf8a96 | 2018-10-12 10:51:31 +0100 | [diff] [blame] | 154 | } |