blob: 0b022df729a0b7634de15afb0690d6b66ebd457e [file] [log] [blame]
Michalis Spyroubcf8a962018-10-12 10:51:31 +01001/*
2 * Copyright (c) 2018 ARM Limited.
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
17 * IMPLIED, INNEUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY NEAIM, DAMAGES OR OTHER
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
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
42 for(unsigned int i = 0; i < reduction_axis.num_dimensions(); ++i)
43 {
44 if(output->total_size() > 0)
45 {
46 ARM_COMPUTE_RETURN_ERROR_ON(output->dimension(reduction_axis[i]) != 1);
47 ARM_COMPUTE_RETURN_ERROR_ON(static_cast<unsigned int>(reduction_axis[i]) > input->num_dimensions() - 1);
48 }
49
50 ARM_COMPUTE_RETURN_ON_ERROR(NEReductionOperationKernel::validate(input, output, reduction_axis[i], ReductionOperation::MEAN_SUM));
51 }
52
53 return Status{};
54}
55
56void NEReduceMean::configure(ITensor *input, const Coordinates &reduction_axis, bool keep_dims, ITensor *output)
57{
58 ARM_COMPUTE_ERROR_ON_NULLPTR(input);
59
60 _reduction_ops = reduction_axis.num_dimensions();
61 _reduction_kernels = arm_compute::support::cpp14::make_unique<NEReductionOperation[]>(_reduction_ops);
62 _reduced_outs = arm_compute::support::cpp14::make_unique<Tensor[]>(_reduction_ops - (keep_dims ? 1 : 0));
63 _keep_dims = keep_dims;
64
65 // Perform reduction for every axis
66 for(unsigned int i = 0; i < _reduction_ops; ++i)
67 {
68 TensorShape out_shape = i == 0 ? input->info()->tensor_shape() : (_reduced_outs.get() + i - 1)->info()->tensor_shape();
69 out_shape.set(reduction_axis[i], 1);
70 auto in = (i == 0) ? input : (_reduced_outs.get() + i - 1);
71
72 if(i == _reduction_ops - 1 && keep_dims)
73 {
74 _reduction_kernels[i].configure(in, output, reduction_axis[i], ReductionOperation::MEAN_SUM);
75 }
76 else
77 {
78 _reduced_outs[i].allocator()->init(TensorInfo(out_shape, input->info()->num_channels(), input->info()->data_type()));
79 _memory_group.manage(_reduced_outs.get() + i);
80 _reduction_kernels[i].configure(in, _reduced_outs.get() + i, reduction_axis[i], ReductionOperation::MEAN_SUM);
81 }
82 }
83
84 // Allocate intermediate tensors
85 for(unsigned int i = 0; i < _reduction_ops - (keep_dims ? 1 : 0); ++i)
86 {
87 _reduced_outs[i].allocator()->allocate();
88 }
89
90 // Configure reshape layer if we want to drop the dimensions
91 if(!keep_dims)
92 {
93 TensorShape out_shape = input->info()->tensor_shape();
94 for(unsigned int i = 0; i < _reduction_ops; ++i)
95 {
96 out_shape.remove_dimension(reduction_axis[i]);
97 }
98 auto_init_if_empty(*output->info(), input->info()->clone()->set_tensor_shape(out_shape));
99 _reshape.configure(_reduced_outs.get() + _reduction_ops - 1, output);
100 }
101}
102
103void NEReduceMean::run()
104{
105 _memory_group.acquire();
106
107 for(unsigned int i = 0; i < _reduction_ops; ++i)
108 {
109 _reduction_kernels[i].run();
110 }
111
112 if(!_keep_dims)
113 {
114 _reshape.run();
115 }
116 _memory_group.release();
117}