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