blob: 0b145f034dd4af85f7de3233226f7db6c0ace652 [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
Georgios Pinitasbd17a162019-05-16 14:23:00 +010026#include "arm_compute/core/CPP/Validate.h"
Michalis Spyroubcf8a962018-10-12 10:51:31 +010027#include "arm_compute/core/Helpers.h"
28#include "arm_compute/runtime/NEON/NEScheduler.h"
29
30using namespace arm_compute;
31
32NEReduceMean::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
37Status 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 Pinitasbd17a162019-05-16 14:23:00 +010041 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 Spyroubcf8a962018-10-12 10:51:31 +010043 ARM_COMPUTE_RETURN_ERROR_ON(reduction_axis.num_dimensions() > input->num_dimensions());
44
Michalis Spyrou8d1b7182019-01-02 15:54:03 +000045 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 Spyroubcf8a962018-10-12 10:51:31 +010049
Michalis Spyrou8d1b7182019-01-02 15:54:03 +000050 // 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 Spyroubcf8a962018-10-12 10:51:31 +010054 }
55
Michalis Spyrou8d1b7182019-01-02 15:54:03 +000056 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 Spyroubcf8a962018-10-12 10:51:31 +010077 return Status{};
78}
79
80void NEReduceMean::configure(ITensor *input, const Coordinates &reduction_axis, bool keep_dims, ITensor *output)
81{
82 ARM_COMPUTE_ERROR_ON_NULLPTR(input);
83
Michalis Spyrou9d0b5f82019-05-01 13:03:59 +010084 _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 Spyroubcf8a962018-10-12 10:51:31 +010088
Michalis Spyrou8d1b7182019-01-02 15:54:03 +000089 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 Spyroubcf8a962018-10-12 10:51:31 +010099 // Perform reduction for every axis
100 for(unsigned int i = 0; i < _reduction_ops; ++i)
101 {
Michalis Spyrou9d0b5f82019-05-01 13:03:59 +0100102 TensorShape out_shape = i == 0 ? input->info()->tensor_shape() : (&_reduced_outs[i - 1])->info()->tensor_shape();
Michalis Spyrou8d1b7182019-01-02 15:54:03 +0000103 out_shape.set(axis_local[i], 1);
Michalis Spyrou9d0b5f82019-05-01 13:03:59 +0100104 auto in = (i == 0) ? input : (&_reduced_outs[i - 1]);
Michalis Spyroubcf8a962018-10-12 10:51:31 +0100105
106 if(i == _reduction_ops - 1 && keep_dims)
107 {
Michalis Spyrou8d1b7182019-01-02 15:54:03 +0000108 _reduction_kernels[i].configure(in, output, axis_local[i], ReductionOperation::MEAN_SUM);
Michalis Spyroubcf8a962018-10-12 10:51:31 +0100109 }
110 else
111 {
Isabella Gottardi0a1090a2019-02-14 18:07:36 +0000112 _reduced_outs[i].allocator()->init(TensorInfo(out_shape, input->info()->num_channels(), input->info()->data_type(), input->info()->quantization_info()));
Michalis Spyrou9d0b5f82019-05-01 13:03:59 +0100113 _memory_group.manage(&_reduced_outs[i]);
114 _reduction_kernels[i].configure(in, &_reduced_outs[i], axis_local[i], ReductionOperation::MEAN_SUM);
Michalis Spyroubcf8a962018-10-12 10:51:31 +0100115 }
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 Spyrou8d1b7182019-01-02 15:54:03 +0000128
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 Spyroubcf8a962018-10-12 10:51:31 +0100132 for(unsigned int i = 0; i < _reduction_ops; ++i)
133 {
Michalis Spyrou8d1b7182019-01-02 15:54:03 +0000134 out_shape.remove_dimension(axis_local[i] - i);
Michalis Spyroubcf8a962018-10-12 10:51:31 +0100135 }
136 auto_init_if_empty(*output->info(), input->info()->clone()->set_tensor_shape(out_shape));
Michalis Spyrou9d0b5f82019-05-01 13:03:59 +0100137 _reshape.configure(&_reduced_outs[_reduction_ops - 1], output);
Michalis Spyroubcf8a962018-10-12 10:51:31 +0100138 }
139}
140
141void NEReduceMean::run()
142{
Georgios Pinitasda953f22019-04-02 17:27:03 +0100143 MemoryGroupResourceScope scope_mg(_memory_group);
Michalis Spyroubcf8a962018-10-12 10:51:31 +0100144
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 Spyroubcf8a962018-10-12 10:51:31 +0100154}