1*c217d954SCole Faust /*
2*c217d954SCole Faust * Copyright (c) 2018-2022 Arm Limited.
3*c217d954SCole Faust *
4*c217d954SCole Faust * SPDX-License-Identifier: MIT
5*c217d954SCole Faust *
6*c217d954SCole Faust * Permission is hereby granted, free of charge, to any person obtaining a copy
7*c217d954SCole Faust * of this software and associated documentation files (the "Software"), to
8*c217d954SCole Faust * deal in the Software without restriction, including without limitation the
9*c217d954SCole Faust * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
10*c217d954SCole Faust * sell copies of the Software, and to permit persons to whom the Software is
11*c217d954SCole Faust * furnished to do so, subject to the following conditions:
12*c217d954SCole Faust *
13*c217d954SCole Faust * The above copyright notice and this permission notice shall be included in all
14*c217d954SCole Faust * copies or substantial portions of the Software.
15*c217d954SCole Faust *
16*c217d954SCole Faust * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17*c217d954SCole Faust * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18*c217d954SCole Faust * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19*c217d954SCole Faust * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20*c217d954SCole Faust * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21*c217d954SCole Faust * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22*c217d954SCole Faust * SOFTWARE.
23*c217d954SCole Faust */
24*c217d954SCole Faust #include "arm_compute/runtime/NEON/functions/NEReduceMean.h"
25*c217d954SCole Faust
26*c217d954SCole Faust #include "arm_compute/core/Error.h"
27*c217d954SCole Faust #include "arm_compute/core/utils/misc/ShapeCalculator.h"
28*c217d954SCole Faust #include "src/common/utils/Log.h"
29*c217d954SCole Faust #include "src/core/CPP/Validate.h"
30*c217d954SCole Faust #include "src/core/NEON/kernels/NEReductionOperationKernel.h"
31*c217d954SCole Faust #include "src/core/helpers/AutoConfiguration.h"
32*c217d954SCole Faust
33*c217d954SCole Faust namespace arm_compute
34*c217d954SCole Faust {
35*c217d954SCole Faust namespace
36*c217d954SCole Faust {
validate_config(const ITensorInfo * input,const Coordinates & reduction_axis,bool keep_dims,const ITensorInfo * output)37*c217d954SCole Faust Status validate_config(const ITensorInfo *input, const Coordinates &reduction_axis, bool keep_dims, const ITensorInfo *output)
38*c217d954SCole Faust {
39*c217d954SCole Faust ARM_COMPUTE_UNUSED(keep_dims);
40*c217d954SCole Faust ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(input, output);
41*c217d954SCole Faust ARM_COMPUTE_RETURN_ERROR_ON_CPU_F16_UNSUPPORTED(input);
42*c217d954SCole Faust ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::QASYMM8_SIGNED, DataType::QASYMM8, DataType::F16, DataType::F32);
43*c217d954SCole Faust ARM_COMPUTE_RETURN_ERROR_ON(reduction_axis.num_dimensions() < 1);
44*c217d954SCole Faust ARM_COMPUTE_RETURN_ERROR_ON(reduction_axis.num_dimensions() > input->num_dimensions());
45*c217d954SCole Faust
46*c217d954SCole Faust const unsigned int reduction_ops = reduction_axis.num_dimensions();
47*c217d954SCole Faust const int input_dims = input->num_dimensions();
48*c217d954SCole Faust Coordinates axis_local = reduction_axis;
49*c217d954SCole Faust
50*c217d954SCole Faust for(unsigned int i = 0; i < axis_local.num_dimensions(); ++i)
51*c217d954SCole Faust {
52*c217d954SCole Faust //axis: The dimensions to reduce. Must be in the range [-rank(input_tensor), rank(input_tensor)).
53*c217d954SCole Faust ARM_COMPUTE_RETURN_ERROR_ON(axis_local[i] < (-static_cast<int>(input->num_dimensions())));
54*c217d954SCole Faust ARM_COMPUTE_RETURN_ERROR_ON(axis_local[i] >= static_cast<int>(input->num_dimensions()));
55*c217d954SCole Faust }
56*c217d954SCole Faust
57*c217d954SCole Faust if(output->tensor_shape().total_size() != 0)
58*c217d954SCole Faust {
59*c217d954SCole Faust // Only validate if not using auto_init for the output tensor
60*c217d954SCole Faust TensorShape out_shape = input->tensor_shape();
61*c217d954SCole Faust // Validate output_shape only if not using auto_init
62*c217d954SCole Faust convert_negative_axis(axis_local, input_dims);
63*c217d954SCole Faust std::sort(axis_local.begin(), axis_local.begin() + reduction_ops);
64*c217d954SCole Faust for(unsigned int i = 0; i < reduction_ops; ++i)
65*c217d954SCole Faust {
66*c217d954SCole Faust ARM_COMPUTE_RETURN_ERROR_ON(axis_local[i] > 3);
67*c217d954SCole Faust ARM_COMPUTE_RETURN_ERROR_ON(static_cast<unsigned int>(axis_local[i]) > input->num_dimensions() - 1);
68*c217d954SCole Faust if(output->total_size() > 0 && keep_dims)
69*c217d954SCole Faust {
70*c217d954SCole Faust ARM_COMPUTE_RETURN_ERROR_ON(output->dimension(axis_local[i]) != 1);
71*c217d954SCole Faust }
72*c217d954SCole Faust if(keep_dims)
73*c217d954SCole Faust {
74*c217d954SCole Faust out_shape.set(axis_local[i], 1);
75*c217d954SCole Faust }
76*c217d954SCole Faust else
77*c217d954SCole Faust {
78*c217d954SCole Faust ARM_COMPUTE_RETURN_ERROR_ON(i > static_cast<unsigned int>(axis_local[i]));
79*c217d954SCole Faust const unsigned int remove_index = axis_local[i] - i;
80*c217d954SCole Faust ARM_COMPUTE_RETURN_ERROR_ON(remove_index >= out_shape.num_dimensions());
81*c217d954SCole Faust out_shape.remove_dimension(remove_index);
82*c217d954SCole Faust }
83*c217d954SCole Faust }
84*c217d954SCole Faust const TensorInfo out_info = input->clone()->set_tensor_shape(out_shape);
85*c217d954SCole Faust ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_SHAPES(output, &out_info);
86*c217d954SCole Faust }
87*c217d954SCole Faust return Status{};
88*c217d954SCole Faust }
89*c217d954SCole Faust } // namespace
90*c217d954SCole Faust
91*c217d954SCole Faust NEReduceMean::~NEReduceMean() = default;
92*c217d954SCole Faust
NEReduceMean(std::shared_ptr<IMemoryManager> memory_manager)93*c217d954SCole Faust NEReduceMean::NEReduceMean(std::shared_ptr<IMemoryManager> memory_manager)
94*c217d954SCole Faust : _memory_group(std::move(memory_manager)), _reduction_kernels(), _reduced_outs(), _reshape(), _reduction_ops(), _keep_dims()
95*c217d954SCole Faust {
96*c217d954SCole Faust }
97*c217d954SCole Faust
validate(const ITensorInfo * input,const Coordinates & reduction_axis,bool keep_dims,const ITensorInfo * output)98*c217d954SCole Faust Status NEReduceMean::validate(const ITensorInfo *input, const Coordinates &reduction_axis, bool keep_dims, const ITensorInfo *output)
99*c217d954SCole Faust {
100*c217d954SCole Faust return validate_config(input, reduction_axis, keep_dims, output);
101*c217d954SCole Faust }
102*c217d954SCole Faust
configure(ITensor * input,const Coordinates & reduction_axis,bool keep_dims,ITensor * output)103*c217d954SCole Faust void NEReduceMean::configure(ITensor *input, const Coordinates &reduction_axis, bool keep_dims, ITensor *output)
104*c217d954SCole Faust {
105*c217d954SCole Faust ARM_COMPUTE_LOG_PARAMS(input, reduction_axis, keep_dims, output);
106*c217d954SCole Faust
107*c217d954SCole Faust // Perform validate step
108*c217d954SCole Faust ARM_COMPUTE_ERROR_THROW_ON(NEReduceMean::validate(input->info(), reduction_axis, keep_dims, output->info()));
109*c217d954SCole Faust // Output auto inizialitation if not yet initialized
110*c217d954SCole Faust const TensorShape output_shape = arm_compute::misc::shape_calculator::calculate_reduce_mean_shape(input->info(), reduction_axis, keep_dims);
111*c217d954SCole Faust auto_init_if_empty(*output->info(), input->info()->clone()->set_tensor_shape(output_shape));
112*c217d954SCole Faust
113*c217d954SCole Faust _reduction_ops = reduction_axis.num_dimensions();
114*c217d954SCole Faust _reduction_kernels.resize(_reduction_ops);
115*c217d954SCole Faust _reduced_outs.resize(_reduction_ops - (keep_dims ? 1 : 0));
116*c217d954SCole Faust _keep_dims = keep_dims;
117*c217d954SCole Faust
118*c217d954SCole Faust ITensor *tmp_input = input;
119*c217d954SCole Faust ITensor *tmp_output = output;
120*c217d954SCole Faust
121*c217d954SCole Faust Coordinates axis_local = reduction_axis;
122*c217d954SCole Faust const int input_dims = tmp_input->info()->num_dimensions();
123*c217d954SCole Faust
124*c217d954SCole Faust convert_negative_axis(axis_local, input_dims);
125*c217d954SCole Faust
126*c217d954SCole Faust // Perform reduction for every axis
127*c217d954SCole Faust for(int i = 0; i < _reduction_ops; ++i)
128*c217d954SCole Faust {
129*c217d954SCole Faust TensorShape out_shape = i == 0 ? tmp_input->info()->tensor_shape() : (&_reduced_outs[i - 1])->info()->tensor_shape();
130*c217d954SCole Faust out_shape.set(axis_local[i], 1);
131*c217d954SCole Faust auto in = (i == 0) ? tmp_input : (&_reduced_outs[i - 1]);
132*c217d954SCole Faust
133*c217d954SCole Faust if(i == _reduction_ops - 1 && keep_dims)
134*c217d954SCole Faust {
135*c217d954SCole Faust _reduction_kernels[i].configure(in, tmp_output, axis_local[i], ReductionOperation::MEAN_SUM);
136*c217d954SCole Faust }
137*c217d954SCole Faust else
138*c217d954SCole Faust {
139*c217d954SCole Faust _reduced_outs[i].allocator()->init(TensorInfo(out_shape, tmp_output->info()->num_channels(), tmp_output->info()->data_type(), tmp_output->info()->quantization_info()));
140*c217d954SCole Faust _memory_group.manage(&_reduced_outs[i]);
141*c217d954SCole Faust _reduction_kernels[i].configure(in, &_reduced_outs[i], axis_local[i], ReductionOperation::MEAN_SUM);
142*c217d954SCole Faust }
143*c217d954SCole Faust }
144*c217d954SCole Faust
145*c217d954SCole Faust // Allocate intermediate tensors
146*c217d954SCole Faust for(int i = 0; i < _reduction_ops - (keep_dims ? 1 : 0); ++i)
147*c217d954SCole Faust {
148*c217d954SCole Faust _reduced_outs[i].allocator()->allocate();
149*c217d954SCole Faust }
150*c217d954SCole Faust // Configure reshape layer if we want to drop the dimensions
151*c217d954SCole Faust if(!keep_dims)
152*c217d954SCole Faust {
153*c217d954SCole Faust TensorShape out_shape = tmp_input->info()->tensor_shape();
154*c217d954SCole Faust // We have to sort the reduction axis vectors in order for remove_dimension
155*c217d954SCole Faust // to work properly
156*c217d954SCole Faust std::sort(axis_local.begin(), axis_local.begin() + _reduction_ops);
157*c217d954SCole Faust for(int i = 0; i < _reduction_ops; ++i)
158*c217d954SCole Faust {
159*c217d954SCole Faust out_shape.remove_dimension(axis_local[i] - i);
160*c217d954SCole Faust }
161*c217d954SCole Faust auto_init_if_empty(*tmp_output->info(), tmp_input->info()->clone()->set_tensor_shape(out_shape));
162*c217d954SCole Faust _reshape.configure(&_reduced_outs[_reduction_ops - 1], tmp_output);
163*c217d954SCole Faust }
164*c217d954SCole Faust }
165*c217d954SCole Faust
run()166*c217d954SCole Faust void NEReduceMean::run()
167*c217d954SCole Faust {
168*c217d954SCole Faust MemoryGroupResourceScope scope_mg(_memory_group);
169*c217d954SCole Faust for(auto &kernel : _reduction_kernels)
170*c217d954SCole Faust {
171*c217d954SCole Faust kernel.run();
172*c217d954SCole Faust }
173*c217d954SCole Faust if(!_keep_dims)
174*c217d954SCole Faust {
175*c217d954SCole Faust _reshape.run();
176*c217d954SCole Faust }
177*c217d954SCole Faust }
178*c217d954SCole Faust } // namespace arm_compute
179