blob: b50a925f44fcadfc57221b4caa62cfe0787ebaf0 [file] [log] [blame]
Michalis Spyroubcf8a962018-10-12 10:51:31 +01001/*
Michele Di Giorgiod9eaf612020-07-08 11:12:57 +01002 * Copyright (c) 2018-2020 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
Pablo Tello93975152019-11-08 13:47:53 +000026#include "arm_compute/core/Error.h"
Michalis Spyroubcf8a962018-10-12 10:51:31 +010027#include "arm_compute/core/Helpers.h"
Pablo Telloa0a4ba12019-12-11 13:04:34 +000028#include "arm_compute/core/utils/misc/ShapeCalculator.h"
Michalis Spyroubcf8a962018-10-12 10:51:31 +010029#include "arm_compute/runtime/NEON/NEScheduler.h"
Sang-Hoon Park68dd25f2020-10-19 16:00:11 +010030#include "src/core/CPP/Validate.h"
Michalis Spyrouebcebf12020-10-21 00:04:14 +010031#include "src/core/NEON/kernels/NEReductionOperationKernel.h"
Sang-Hoon Park68dd25f2020-10-19 16:00:11 +010032#include "src/core/helpers/AutoConfiguration.h"
Michalis Spyroubcf8a962018-10-12 10:51:31 +010033
Pablo Tello93975152019-11-08 13:47:53 +000034namespace arm_compute
35{
36namespace
37{
Pablo Tello93975152019-11-08 13:47:53 +000038Status validate_config(const ITensorInfo *input, const Coordinates &reduction_axis, bool keep_dims, const ITensorInfo *output)
Michalis Spyroubcf8a962018-10-12 10:51:31 +010039{
40 ARM_COMPUTE_UNUSED(keep_dims);
Pablo Tello93975152019-11-08 13:47:53 +000041 ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(input, output);
Georgios Pinitas32bd4dd2019-05-16 14:23:00 +010042 ARM_COMPUTE_RETURN_ERROR_ON_CPU_F16_UNSUPPORTED(input);
Luca Foschianiee939fb2020-01-28 10:38:07 +000043 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::QASYMM8_SIGNED, DataType::QASYMM8, DataType::F16, DataType::F32);
Pablo Tello93975152019-11-08 13:47:53 +000044 ARM_COMPUTE_RETURN_ERROR_ON(reduction_axis.num_dimensions() < 1);
Michalis Spyroubcf8a962018-10-12 10:51:31 +010045 ARM_COMPUTE_RETURN_ERROR_ON(reduction_axis.num_dimensions() > input->num_dimensions());
46
Michalis Spyrou8d1b7182019-01-02 15:54:03 +000047 const unsigned int reduction_ops = reduction_axis.num_dimensions();
48 const int input_dims = input->num_dimensions();
49 Coordinates axis_local = reduction_axis;
Michalis Spyroubcf8a962018-10-12 10:51:31 +010050
Pablo Tello93975152019-11-08 13:47:53 +000051 for(unsigned int i = 0; i < axis_local.num_dimensions(); ++i)
Michalis Spyrou8d1b7182019-01-02 15:54:03 +000052 {
Pablo Tello93975152019-11-08 13:47:53 +000053 //axis: The dimensions to reduce. Must be in the range [-rank(input_tensor), rank(input_tensor)).
54 ARM_COMPUTE_RETURN_ERROR_ON(axis_local[i] < (-static_cast<int>(input->num_dimensions())));
55 ARM_COMPUTE_RETURN_ERROR_ON(axis_local[i] >= static_cast<int>(input->num_dimensions()));
Michalis Spyroubcf8a962018-10-12 10:51:31 +010056 }
57
Pablo Tello93975152019-11-08 13:47:53 +000058 if(output->tensor_shape().total_size() != 0)
Michalis Spyrou8d1b7182019-01-02 15:54:03 +000059 {
Pablo Tello93975152019-11-08 13:47:53 +000060 // Only validate if not using auto_init for the output tensor
61 TensorShape out_shape = input->tensor_shape();
62 // Validate output_shape only if not using auto_init
63 convert_negative_axis(axis_local, input_dims);
64 std::sort(axis_local.begin(), axis_local.begin() + reduction_ops);
65 for(unsigned int i = 0; i < reduction_ops; ++i)
Michalis Spyrou8d1b7182019-01-02 15:54:03 +000066 {
Pablo Tello93975152019-11-08 13:47:53 +000067 ARM_COMPUTE_RETURN_ERROR_ON(axis_local[i] > 3);
68 ARM_COMPUTE_RETURN_ERROR_ON(static_cast<unsigned int>(axis_local[i]) > input->num_dimensions() - 1);
69 if(output->total_size() > 0 && keep_dims)
70 {
71 ARM_COMPUTE_RETURN_ERROR_ON(output->dimension(axis_local[i]) != 1);
72 }
73 if(keep_dims)
74 {
75 out_shape.set(axis_local[i], 1);
76 }
77 else
78 {
79 ARM_COMPUTE_RETURN_ERROR_ON(i > static_cast<unsigned int>(axis_local[i]));
80 const unsigned int remove_index = axis_local[i] - i;
81 ARM_COMPUTE_RETURN_ERROR_ON(remove_index >= out_shape.num_dimensions());
82 out_shape.remove_dimension(remove_index);
83 }
Michalis Spyrou8d1b7182019-01-02 15:54:03 +000084 }
Pablo Tello93975152019-11-08 13:47:53 +000085 const TensorInfo out_info = input->clone()->set_tensor_shape(out_shape);
86 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_SHAPES(output, &out_info);
Manuel Bottinic58f0ad2020-08-07 16:49:15 +010087 const bool requant = is_data_type_quantized(input->data_type()) && input->quantization_info() != output->quantization_info();
88 if(requant)
89 {
90 TensorInfo input_no_quant(input->clone()->set_data_type(DataType::F32));
91 NEDequantizationLayer::validate(input, &input_no_quant);
92 TensorInfo output_no_quant(output->clone()->set_data_type(DataType::F32));
93 NEQuantizationLayer::validate(&output_no_quant, output);
94 }
Michalis Spyrou8d1b7182019-01-02 15:54:03 +000095 }
Michalis Spyroubcf8a962018-10-12 10:51:31 +010096 return Status{};
97}
Manuel Bottinic58f0ad2020-08-07 16:49:15 +010098} // namespace
99
Michalis Spyrouebcebf12020-10-21 00:04:14 +0100100NEReduceMean::~NEReduceMean() = default;
101
Manuel Bottinic58f0ad2020-08-07 16:49:15 +0100102NEReduceMean::NEReduceMean(std::shared_ptr<IMemoryManager> memory_manager)
103 : _memory_group(std::move(memory_manager)), _reduction_kernels(), _reduced_outs(), _reshape(), _dequant(), _requant(), _reduction_ops(), _keep_dims(), _do_requant(), _input_no_quant(),
104 _output_no_quant()
105{
106}
Michalis Spyroubcf8a962018-10-12 10:51:31 +0100107
Pablo Tello93975152019-11-08 13:47:53 +0000108Status NEReduceMean::validate(const ITensorInfo *input, const Coordinates &reduction_axis, bool keep_dims, const ITensorInfo *output)
109{
110 return validate_config(input, reduction_axis, keep_dims, output);
111}
112
Michalis Spyroubcf8a962018-10-12 10:51:31 +0100113void NEReduceMean::configure(ITensor *input, const Coordinates &reduction_axis, bool keep_dims, ITensor *output)
114{
Pablo Tello93975152019-11-08 13:47:53 +0000115 // Perform validate step
116 ARM_COMPUTE_ERROR_THROW_ON(NEReduceMean::validate(input->info(), reduction_axis, keep_dims, output->info()));
117 // Output auto inizialitation if not yet initialized
Manuel Bottinic58f0ad2020-08-07 16:49:15 +0100118 const TensorShape output_shape = arm_compute::misc::shape_calculator::calculate_reduce_mean_shape(input->info(), reduction_axis, keep_dims);
Pablo Tello93975152019-11-08 13:47:53 +0000119 auto_init_if_empty(*output->info(), input->info()->clone()->set_tensor_shape(output_shape));
Michalis Spyroubcf8a962018-10-12 10:51:31 +0100120
Manuel Bottinic58f0ad2020-08-07 16:49:15 +0100121 _do_requant = is_data_type_quantized(input->info()->data_type()) && input->info()->quantization_info() != output->info()->quantization_info();
Michalis Spyroubcfd09a2019-05-01 13:03:59 +0100122 _reduction_ops = reduction_axis.num_dimensions();
123 _reduction_kernels.resize(_reduction_ops);
124 _reduced_outs.resize(_reduction_ops - (keep_dims ? 1 : 0));
125 _keep_dims = keep_dims;
Michalis Spyroubcf8a962018-10-12 10:51:31 +0100126
Manuel Bottinic58f0ad2020-08-07 16:49:15 +0100127 ITensor *tmp_input = input;
128 ITensor *tmp_output = output;
129 if(_do_requant)
130 {
131 _memory_group.manage(&_input_no_quant);
132 _memory_group.manage(&_output_no_quant);
133 TensorInfo output_no_quant_info = input->info()->clone()->set_tensor_shape(output_shape);
134 output_no_quant_info.set_data_type(DataType::F32);
135 auto_init_if_empty(*_output_no_quant.info(), output_no_quant_info);
136 auto_init_if_empty(*_input_no_quant.info(), input->info()->clone()->set_data_type(DataType::F32));
137 _dequant.configure(input, &_input_no_quant);
138 tmp_input = &_input_no_quant;
139 tmp_output = &_output_no_quant;
140 }
141
Pablo Tello93975152019-11-08 13:47:53 +0000142 Coordinates axis_local = reduction_axis;
Manuel Bottinic58f0ad2020-08-07 16:49:15 +0100143 const int input_dims = tmp_input->info()->num_dimensions();
Michalis Spyrou8d1b7182019-01-02 15:54:03 +0000144
Pablo Tello93975152019-11-08 13:47:53 +0000145 convert_negative_axis(axis_local, input_dims);
Michalis Spyrou8d1b7182019-01-02 15:54:03 +0000146
Michalis Spyroubcf8a962018-10-12 10:51:31 +0100147 // Perform reduction for every axis
Pablo Tello93975152019-11-08 13:47:53 +0000148 for(int i = 0; i < _reduction_ops; ++i)
Michalis Spyroubcf8a962018-10-12 10:51:31 +0100149 {
Manuel Bottinic58f0ad2020-08-07 16:49:15 +0100150 TensorShape out_shape = i == 0 ? tmp_input->info()->tensor_shape() : (&_reduced_outs[i - 1])->info()->tensor_shape();
Michalis Spyrou8d1b7182019-01-02 15:54:03 +0000151 out_shape.set(axis_local[i], 1);
Manuel Bottinic58f0ad2020-08-07 16:49:15 +0100152 auto in = (i == 0) ? tmp_input : (&_reduced_outs[i - 1]);
Michalis Spyroubcf8a962018-10-12 10:51:31 +0100153
154 if(i == _reduction_ops - 1 && keep_dims)
155 {
Manuel Bottinic58f0ad2020-08-07 16:49:15 +0100156 _reduction_kernels[i].configure(in, tmp_output, axis_local[i], ReductionOperation::MEAN_SUM);
Michalis Spyroubcf8a962018-10-12 10:51:31 +0100157 }
158 else
159 {
Manuel Bottinic58f0ad2020-08-07 16:49:15 +0100160 _reduced_outs[i].allocator()->init(TensorInfo(out_shape, tmp_input->info()->num_channels(), tmp_input->info()->data_type(), tmp_input->info()->quantization_info()));
Michalis Spyroubcfd09a2019-05-01 13:03:59 +0100161 _memory_group.manage(&_reduced_outs[i]);
162 _reduction_kernels[i].configure(in, &_reduced_outs[i], axis_local[i], ReductionOperation::MEAN_SUM);
Michalis Spyroubcf8a962018-10-12 10:51:31 +0100163 }
164 }
165
166 // Allocate intermediate tensors
Pablo Tello93975152019-11-08 13:47:53 +0000167 for(int i = 0; i < _reduction_ops - (keep_dims ? 1 : 0); ++i)
Michalis Spyroubcf8a962018-10-12 10:51:31 +0100168 {
169 _reduced_outs[i].allocator()->allocate();
170 }
171
172 // Configure reshape layer if we want to drop the dimensions
173 if(!keep_dims)
174 {
Manuel Bottinic58f0ad2020-08-07 16:49:15 +0100175 TensorShape out_shape = tmp_input->info()->tensor_shape();
Michalis Spyrou8d1b7182019-01-02 15:54:03 +0000176 // We have to sort the reduction axis vectors in order for remove_dimension
177 // to work properly
178 std::sort(axis_local.begin(), axis_local.begin() + _reduction_ops);
Pablo Tello93975152019-11-08 13:47:53 +0000179 for(int i = 0; i < _reduction_ops; ++i)
Michalis Spyroubcf8a962018-10-12 10:51:31 +0100180 {
Michalis Spyrou8d1b7182019-01-02 15:54:03 +0000181 out_shape.remove_dimension(axis_local[i] - i);
Michalis Spyroubcf8a962018-10-12 10:51:31 +0100182 }
Manuel Bottinic58f0ad2020-08-07 16:49:15 +0100183 auto_init_if_empty(*tmp_output->info(), tmp_input->info()->clone()->set_tensor_shape(out_shape));
184 _reshape.configure(&_reduced_outs[_reduction_ops - 1], tmp_output);
185 }
186 if(_do_requant)
187 {
188 _requant.configure(&_output_no_quant, output);
189 _input_no_quant.allocator()->allocate();
190 _output_no_quant.allocator()->allocate();
Michalis Spyroubcf8a962018-10-12 10:51:31 +0100191 }
192}
193
194void NEReduceMean::run()
195{
Georgios Pinitasda953f22019-04-02 17:27:03 +0100196 MemoryGroupResourceScope scope_mg(_memory_group);
Manuel Bottinic58f0ad2020-08-07 16:49:15 +0100197 if(_do_requant)
198 {
199 _dequant.run();
200 }
Pablo Tello93975152019-11-08 13:47:53 +0000201 for(auto &kernel : _reduction_kernels)
Michalis Spyroubcf8a962018-10-12 10:51:31 +0100202 {
Pablo Tello93975152019-11-08 13:47:53 +0000203 kernel.run();
Michalis Spyroubcf8a962018-10-12 10:51:31 +0100204 }
Michalis Spyroubcf8a962018-10-12 10:51:31 +0100205 if(!_keep_dims)
206 {
207 _reshape.run();
208 }
Manuel Bottinic58f0ad2020-08-07 16:49:15 +0100209 if(_do_requant)
210 {
211 _requant.run();
212 }
Michalis Spyroubcf8a962018-10-12 10:51:31 +0100213}
Pablo Tello93975152019-11-08 13:47:53 +0000214} // namespace arm_compute