blob: dc610d55de8b6739a17a699ccf1bbbdd850a4ac0 [file] [log] [blame]
Michalis Spyroubcf8a962018-10-12 10:51:31 +01001/*
George Wort5a97b282018-12-21 16:21:04 +00002 * Copyright (c) 2018-2019 ARM Limited.
Michalis Spyroubcf8a962018-10-12 10:51:31 +01003 *
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 Wort5a97b282018-12-21 16:21:04 +000017 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
Michalis Spyroubcf8a962018-10-12 10:51:31 +010018 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
George Wort5a97b282018-12-21 16:21:04 +000019 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
Michalis Spyroubcf8a962018-10-12 10:51:31 +010020 * 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
29using namespace arm_compute;
30
31NEReduceMean::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
36Status 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 Spyrou8d1b7182019-01-02 15:54:03 +000042 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 Spyroubcf8a962018-10-12 10:51:31 +010046
Michalis Spyrou8d1b7182019-01-02 15:54:03 +000047 // 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 Spyroubcf8a962018-10-12 10:51:31 +010051 }
52
Michalis Spyrou8d1b7182019-01-02 15:54:03 +000053 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 Spyroubcf8a962018-10-12 10:51:31 +010074 return Status{};
75}
76
77void 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 Spyrou8d1b7182019-01-02 15:54:03 +000086 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 Spyroubcf8a962018-10-12 10:51:31 +010096 // 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 Spyrou8d1b7182019-01-02 15:54:03 +0000100 out_shape.set(axis_local[i], 1);
Michalis Spyroubcf8a962018-10-12 10:51:31 +0100101 auto in = (i == 0) ? input : (_reduced_outs.get() + i - 1);
102
103 if(i == _reduction_ops - 1 && keep_dims)
104 {
Michalis Spyrou8d1b7182019-01-02 15:54:03 +0000105 _reduction_kernels[i].configure(in, output, axis_local[i], ReductionOperation::MEAN_SUM);
Michalis Spyroubcf8a962018-10-12 10:51:31 +0100106 }
107 else
108 {
109 _reduced_outs[i].allocator()->init(TensorInfo(out_shape, input->info()->num_channels(), input->info()->data_type()));
110 _memory_group.manage(_reduced_outs.get() + i);
Michalis Spyrou8d1b7182019-01-02 15:54:03 +0000111 _reduction_kernels[i].configure(in, _reduced_outs.get() + i, axis_local[i], ReductionOperation::MEAN_SUM);
Michalis Spyroubcf8a962018-10-12 10:51:31 +0100112 }
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 Spyrou8d1b7182019-01-02 15:54:03 +0000125
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 Spyroubcf8a962018-10-12 10:51:31 +0100129 for(unsigned int i = 0; i < _reduction_ops; ++i)
130 {
Michalis Spyrou8d1b7182019-01-02 15:54:03 +0000131 out_shape.remove_dimension(axis_local[i] - i);
Michalis Spyroubcf8a962018-10-12 10:51:31 +0100132 }
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
138void NEReduceMean::run()
139{
140 _memory_group.acquire();
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 }
151 _memory_group.release();
152}