blob: bef8d887fdd2f817b549810dd662ed2c45e30e91 [file] [log] [blame]
Michalis Spyrou7e9391b2018-10-05 14:49:28 +01001/*
Jakub Sujak5d7a93a2024-01-10 10:21:22 +00002 * Copyright (c) 2018-2021, 2023-2024 Arm Limited.
Michalis Spyrou7e9391b2018-10-05 14:49:28 +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
17 * IMPLIED, INCLUDING 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 CLAIM, 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/CL/functions/CLReduceMean.h"
25
26#include "arm_compute/core/CL/ICLTensor.h"
Pablo Telloa0a4ba12019-12-11 13:04:34 +000027#include "arm_compute/core/Error.h"
Michalis Spyrou7e9391b2018-10-05 14:49:28 +010028#include "arm_compute/core/Types.h"
Pablo Telloa0a4ba12019-12-11 13:04:34 +000029#include "arm_compute/core/utils/misc/ShapeCalculator.h"
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010030
31#include "src/common/utils/Log.h"
Sang-Hoon Park68dd25f2020-10-19 16:00:11 +010032#include "src/core/CL/CLValidate.h"
Sang-Hoon Parkbef7fa22020-10-21 15:58:54 +010033#include "src/core/CL/kernels/CLFillBorderKernel.h"
34#include "src/core/CL/kernels/CLReductionOperationKernel.h"
Sang-Hoon Park68dd25f2020-10-19 16:00:11 +010035#include "src/core/helpers/AutoConfiguration.h"
Michalis Spyrou7e9391b2018-10-05 14:49:28 +010036
37namespace arm_compute
38{
Pablo Telloa0a4ba12019-12-11 13:04:34 +000039namespace
40{
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010041Status
42validate_config(const ITensorInfo *input, const Coordinates &reduction_axis, bool keep_dims, const ITensorInfo *output)
Pablo Telloa0a4ba12019-12-11 13:04:34 +000043{
44 ARM_COMPUTE_UNUSED(keep_dims);
45 ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(input, output);
46 ARM_COMPUTE_RETURN_ERROR_ON_F16_UNSUPPORTED(input);
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010047 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::QASYMM8, DataType::QASYMM8_SIGNED,
48 DataType::F16, DataType::F32);
Pablo Telloa0a4ba12019-12-11 13:04:34 +000049 ARM_COMPUTE_RETURN_ERROR_ON(reduction_axis.num_dimensions() < 1);
50 ARM_COMPUTE_RETURN_ERROR_ON(reduction_axis.num_dimensions() > input->num_dimensions());
51
52 const unsigned int reduction_ops = reduction_axis.num_dimensions();
53 const int input_dims = input->num_dimensions();
54 Coordinates axis_local = reduction_axis;
55
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010056 for (unsigned int i = 0; i < axis_local.num_dimensions(); ++i)
Pablo Telloa0a4ba12019-12-11 13:04:34 +000057 {
58 //axis: The dimensions to reduce. Must be in the range [-rank(input_tensor), rank(input_tensor)).
59 ARM_COMPUTE_RETURN_ERROR_ON(axis_local[i] < (-static_cast<int>(input->num_dimensions())));
60 ARM_COMPUTE_RETURN_ERROR_ON(axis_local[i] >= static_cast<int>(input->num_dimensions()));
61 }
62
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010063 if (output->tensor_shape().total_size() != 0)
Pablo Telloa0a4ba12019-12-11 13:04:34 +000064 {
65 // Only validate if not using auto_init for the output tensor
66 TensorShape out_shape = input->tensor_shape();
67 // Validate output_shape only if not using auto_init
68 convert_negative_axis(axis_local, input_dims);
Jakub Sujak5d7a93a2024-01-10 10:21:22 +000069
70// Suppress warning produced by a compiler bug in GCC
71// https://gcc.gnu.org/bugzilla/show_bug.cgi?id=104165
72#pragma GCC diagnostic push
73#pragma GCC diagnostic ignored "-Warray-bounds"
Pablo Telloa0a4ba12019-12-11 13:04:34 +000074 std::sort(axis_local.begin(), axis_local.begin() + reduction_ops);
Jakub Sujak5d7a93a2024-01-10 10:21:22 +000075#pragma GCC diagnostic pop
76
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010077 for (unsigned int i = 0; i < reduction_ops; ++i)
Pablo Telloa0a4ba12019-12-11 13:04:34 +000078 {
79 ARM_COMPUTE_RETURN_ERROR_ON(axis_local[i] > 3);
80 ARM_COMPUTE_RETURN_ERROR_ON(static_cast<unsigned int>(axis_local[i]) > input->num_dimensions() - 1);
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010081 if (output->total_size() > 0 && keep_dims)
Pablo Telloa0a4ba12019-12-11 13:04:34 +000082 {
83 ARM_COMPUTE_RETURN_ERROR_ON(output->dimension(axis_local[i]) != 1);
84 }
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010085 if (keep_dims)
Pablo Telloa0a4ba12019-12-11 13:04:34 +000086 {
87 out_shape.set(axis_local[i], 1);
88 }
89 else
90 {
91 ARM_COMPUTE_RETURN_ERROR_ON(i > static_cast<unsigned int>(axis_local[i]));
92 const unsigned int remove_index = axis_local[i] - i;
93 ARM_COMPUTE_RETURN_ERROR_ON(remove_index >= out_shape.num_dimensions());
Viet-Hoa Do4f76a002023-08-02 11:59:07 +010094 out_shape.remove_dimension(remove_index, false);
Pablo Telloa0a4ba12019-12-11 13:04:34 +000095 }
96 }
97 const TensorInfo out_info = input->clone()->set_tensor_shape(out_shape);
98 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_SHAPES(output, &out_info);
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010099 const bool requant =
100 is_data_type_quantized(input->data_type()) && input->quantization_info() != output->quantization_info();
101 if (requant)
Manuel Bottinic58f0ad2020-08-07 16:49:15 +0100102 {
103 TensorInfo input_no_quant(input->clone()->set_data_type(DataType::F32));
104 CLDequantizationLayer::validate(input, &input_no_quant);
105 TensorInfo output_no_quant(output->clone()->set_data_type(DataType::F32));
106 CLQuantizationLayer::validate(&output_no_quant, output);
107 }
Pablo Telloa0a4ba12019-12-11 13:04:34 +0000108 }
109 return Status{};
110}
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100111} // namespace
Manuel Bottinic58f0ad2020-08-07 16:49:15 +0100112
Michalis Spyrou7e9391b2018-10-05 14:49:28 +0100113CLReduceMean::CLReduceMean(std::shared_ptr<IMemoryManager> memory_manager)
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100114 : _memory_group(std::move(memory_manager)),
115 _reduction_kernels(),
116 _reduced_outs(),
117 _reshape(),
118 _dequant(),
119 _requant(),
120 _reduction_ops(),
121 _keep_dims(),
122 _do_requant(),
123 _input_no_quant(),
Manuel Bottinic58f0ad2020-08-07 16:49:15 +0100124 _output_no_quant()
Michalis Spyrou7e9391b2018-10-05 14:49:28 +0100125{
126}
Manuel Bottinic58f0ad2020-08-07 16:49:15 +0100127
Michalis Spyrou7e9391b2018-10-05 14:49:28 +0100128void CLReduceMean::configure(ICLTensor *input, const Coordinates &reduction_axis, bool keep_dims, ICLTensor *output)
129{
Manuel Bottini2b84be52020-04-08 10:15:51 +0100130 configure(CLKernelLibrary::get().get_compile_context(), input, reduction_axis, keep_dims, output);
131}
132
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100133void CLReduceMean::configure(const CLCompileContext &compile_context,
134 ICLTensor *input,
135 const Coordinates &reduction_axis,
136 bool keep_dims,
137 ICLTensor *output)
Manuel Bottini2b84be52020-04-08 10:15:51 +0100138{
Pablo Telloa0a4ba12019-12-11 13:04:34 +0000139 // Perform validate step
140 ARM_COMPUTE_ERROR_THROW_ON(CLReduceMean::validate(input->info(), reduction_axis, keep_dims, output->info()));
ramelg016d891572021-09-29 10:05:09 +0100141 ARM_COMPUTE_LOG_PARAMS(input, reduction_axis, keep_dims, output);
142
Pablo Telloa0a4ba12019-12-11 13:04:34 +0000143 // Output auto inizialitation if not yet initialized
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100144 const TensorShape output_shape =
145 arm_compute::misc::shape_calculator::calculate_reduce_mean_shape(input->info(), reduction_axis, keep_dims);
Pablo Telloa0a4ba12019-12-11 13:04:34 +0000146 auto_init_if_empty(*output->info(), input->info()->clone()->set_tensor_shape(output_shape));
Michalis Spyrou7e9391b2018-10-05 14:49:28 +0100147
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100148 _do_requant = is_data_type_quantized(input->info()->data_type()) &&
149 input->info()->quantization_info() != output->info()->quantization_info();
Michalis Spyroubcfd09a2019-05-01 13:03:59 +0100150 _reduction_ops = reduction_axis.num_dimensions();
151 _reduction_kernels.resize(_reduction_ops);
152 _reduced_outs.resize(_reduction_ops - (keep_dims ? 1 : 0));
153 _keep_dims = keep_dims;
Michalis Spyrou7e9391b2018-10-05 14:49:28 +0100154
Manuel Bottinic58f0ad2020-08-07 16:49:15 +0100155 ICLTensor *tmp_input = input;
156 ICLTensor *tmp_output = output;
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100157 if (_do_requant)
Manuel Bottinic58f0ad2020-08-07 16:49:15 +0100158 {
159 _memory_group.manage(&_input_no_quant);
160 _memory_group.manage(&_output_no_quant);
161 TensorInfo output_no_quant_info = input->info()->clone()->set_tensor_shape(output_shape);
162 output_no_quant_info.set_data_type(DataType::F32);
163 auto_init_if_empty(*_output_no_quant.info(), output_no_quant_info);
164 auto_init_if_empty(*_input_no_quant.info(), input->info()->clone()->set_data_type(DataType::F32));
165 _dequant.configure(compile_context, input, &_input_no_quant);
166 tmp_input = &_input_no_quant;
167 tmp_output = &_output_no_quant;
168 }
169
Michalis Spyrou8d1b7182019-01-02 15:54:03 +0000170 Coordinates axis_local = reduction_axis;
Manuel Bottinic58f0ad2020-08-07 16:49:15 +0100171 const int input_dims = tmp_input->info()->num_dimensions();
Michalis Spyrou8d1b7182019-01-02 15:54:03 +0000172
Pablo Telloa0a4ba12019-12-11 13:04:34 +0000173 convert_negative_axis(axis_local, input_dims);
Michalis Spyrou8d1b7182019-01-02 15:54:03 +0000174
Michalis Spyrou7e9391b2018-10-05 14:49:28 +0100175 // Perform reduction for every axis
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100176 for (int i = 0; i < _reduction_ops; ++i)
Michalis Spyrou7e9391b2018-10-05 14:49:28 +0100177 {
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100178 TensorShape out_shape =
179 i == 0 ? tmp_input->info()->tensor_shape() : (&_reduced_outs[i - 1])->info()->tensor_shape();
Michalis Spyrou8d1b7182019-01-02 15:54:03 +0000180 out_shape.set(axis_local[i], 1);
Manuel Bottinic58f0ad2020-08-07 16:49:15 +0100181 auto in = (i == 0) ? tmp_input : (&_reduced_outs[i - 1]);
Michalis Spyrou7e9391b2018-10-05 14:49:28 +0100182
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100183 if (i == _reduction_ops - 1 && keep_dims)
Michalis Spyrou7e9391b2018-10-05 14:49:28 +0100184 {
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100185 _reduction_kernels[i].configure(compile_context, in, tmp_output, axis_local[i],
186 ReductionOperation::MEAN_SUM);
Michalis Spyrou7e9391b2018-10-05 14:49:28 +0100187 }
188 else
189 {
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100190 _reduced_outs[i].allocator()->init(TensorInfo(out_shape, tmp_input->info()->num_channels(),
191 tmp_input->info()->data_type(),
192 tmp_input->info()->quantization_info()));
Michalis Spyroubcfd09a2019-05-01 13:03:59 +0100193 _memory_group.manage(&_reduced_outs[i]);
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100194 _reduction_kernels[i].configure(compile_context, in, &_reduced_outs[i], axis_local[i],
195 ReductionOperation::MEAN_SUM);
Michalis Spyrou7e9391b2018-10-05 14:49:28 +0100196 }
197 }
198
199 // Allocate intermediate tensors
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100200 for (int i = 0; i < _reduction_ops - (keep_dims ? 1 : 0); ++i)
Michalis Spyrou7e9391b2018-10-05 14:49:28 +0100201 {
202 _reduced_outs[i].allocator()->allocate();
203 }
204
205 // Configure reshape layer if we want to drop the dimensions
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100206 if (!_keep_dims)
Michalis Spyrou7e9391b2018-10-05 14:49:28 +0100207 {
Manuel Bottinic58f0ad2020-08-07 16:49:15 +0100208 TensorShape out_shape = tmp_input->info()->tensor_shape();
Michalis Spyrou96f84612018-10-24 14:01:04 +0100209
210 // We have to sort the reduction axis vectors in order for remove_dimension
211 // to work properly
Jakub Sujak5d7a93a2024-01-10 10:21:22 +0000212
213// Suppress warning produced by a compiler bug in GCC
214// https://gcc.gnu.org/bugzilla/show_bug.cgi?id=104165
215#pragma GCC diagnostic push
216#pragma GCC diagnostic ignored "-Warray-bounds"
Michalis Spyrou8d1b7182019-01-02 15:54:03 +0000217 std::sort(axis_local.begin(), axis_local.begin() + _reduction_ops);
Jakub Sujak5d7a93a2024-01-10 10:21:22 +0000218#pragma GCC diagnostic pop
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100219 for (int i = 0; i < _reduction_ops; ++i)
Michalis Spyrou7e9391b2018-10-05 14:49:28 +0100220 {
Viet-Hoa Do4f76a002023-08-02 11:59:07 +0100221 out_shape.remove_dimension(axis_local[i] - i, false);
Michalis Spyrou7e9391b2018-10-05 14:49:28 +0100222 }
Manuel Bottinic58f0ad2020-08-07 16:49:15 +0100223 auto_init_if_empty(*tmp_output->info(), tmp_input->info()->clone()->set_tensor_shape(out_shape));
224 _reshape.configure(compile_context, &_reduced_outs[_reduction_ops - 1], tmp_output);
225 }
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100226 if (_do_requant)
Manuel Bottinic58f0ad2020-08-07 16:49:15 +0100227 {
228 _requant.configure(compile_context, &_output_no_quant, output);
229 _input_no_quant.allocator()->allocate();
230 _output_no_quant.allocator()->allocate();
Michalis Spyrou7e9391b2018-10-05 14:49:28 +0100231 }
232}
233
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100234Status CLReduceMean::validate(const ITensorInfo *input,
235 const Coordinates &reduction_axis,
236 bool keep_dims,
237 const ITensorInfo *output)
Michalis Spyrou7e9391b2018-10-05 14:49:28 +0100238{
Pablo Telloa0a4ba12019-12-11 13:04:34 +0000239 return validate_config(input, reduction_axis, keep_dims, output);
Michalis Spyrou7e9391b2018-10-05 14:49:28 +0100240}
241
242void CLReduceMean::run()
243{
Georgios Pinitasda953f22019-04-02 17:27:03 +0100244 MemoryGroupResourceScope scope_mg(_memory_group);
Michalis Spyrou7e9391b2018-10-05 14:49:28 +0100245
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100246 if (_do_requant)
Manuel Bottinic58f0ad2020-08-07 16:49:15 +0100247 {
248 _dequant.run();
249 }
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100250 for (auto &kernel : _reduction_kernels)
Michalis Spyrou7e9391b2018-10-05 14:49:28 +0100251 {
Pablo Telloa0a4ba12019-12-11 13:04:34 +0000252 kernel.run();
Michalis Spyrou7e9391b2018-10-05 14:49:28 +0100253 }
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100254 if (!_keep_dims)
Michalis Spyrou7e9391b2018-10-05 14:49:28 +0100255 {
256 _reshape.run();
257 }
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100258 if (_do_requant)
Manuel Bottinic58f0ad2020-08-07 16:49:15 +0100259 {
260 _requant.run();
261 }
Michalis Spyrou7e9391b2018-10-05 14:49:28 +0100262}
263} // namespace arm_compute