blob: 99206178807ba5b3f78c63881fb5aa971cf1666d [file] [log] [blame]
Michalis Spyrou7e9391b2018-10-05 14:49:28 +01001/*
Michalis Spyrou0b18d972020-01-30 18:11:13 +00002 * Copyright (c) 2018-2020 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
Georgios Pinitas32bd4dd2019-05-16 14:23:00 +010026#include "arm_compute/core/CL/CLValidate.h"
Michalis Spyrou7e9391b2018-10-05 14:49:28 +010027#include "arm_compute/core/CL/ICLTensor.h"
28#include "arm_compute/core/CL/kernels/CLReductionOperationKernel.h"
Pablo Telloa0a4ba12019-12-11 13:04:34 +000029#include "arm_compute/core/Error.h"
Michalis Spyrou7e9391b2018-10-05 14:49:28 +010030#include "arm_compute/core/Types.h"
Pablo Telloa0a4ba12019-12-11 13:04:34 +000031#include "arm_compute/core/utils/misc/ShapeCalculator.h"
Michalis Spyrou7e9391b2018-10-05 14:49:28 +010032#include "support/ToolchainSupport.h"
33
34namespace arm_compute
35{
Pablo Telloa0a4ba12019-12-11 13:04:34 +000036namespace
37{
38Status validate_config(const ITensorInfo *input, const Coordinates &reduction_axis, bool keep_dims, const ITensorInfo *output)
39{
40 ARM_COMPUTE_UNUSED(keep_dims);
41 ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(input, output);
42 ARM_COMPUTE_RETURN_ERROR_ON_F16_UNSUPPORTED(input);
Michalis Spyrou0b18d972020-01-30 18:11:13 +000043 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::QASYMM8, DataType::QASYMM8_SIGNED, DataType::F16, DataType::F32);
Pablo Telloa0a4ba12019-12-11 13:04:34 +000044 ARM_COMPUTE_RETURN_ERROR_ON(reduction_axis.num_dimensions() < 1);
45 ARM_COMPUTE_RETURN_ERROR_ON(reduction_axis.num_dimensions() > input->num_dimensions());
46
47 const unsigned int reduction_ops = reduction_axis.num_dimensions();
48 const int input_dims = input->num_dimensions();
49 Coordinates axis_local = reduction_axis;
50
51 for(unsigned int i = 0; i < axis_local.num_dimensions(); ++i)
52 {
53 //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()));
56 }
57
58 if(output->tensor_shape().total_size() != 0)
59 {
60 // 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)
66 {
67 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 }
84 }
85 const TensorInfo out_info = input->clone()->set_tensor_shape(out_shape);
86 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_SHAPES(output, &out_info);
87 }
88 return Status{};
89}
90}
Michalis Spyrou7e9391b2018-10-05 14:49:28 +010091CLReduceMean::CLReduceMean(std::shared_ptr<IMemoryManager> memory_manager)
92 : _memory_group(std::move(memory_manager)), _reduction_kernels(), _reduced_outs(), _reshape(), _reduction_ops(), _keep_dims()
93{
94}
95void CLReduceMean::configure(ICLTensor *input, const Coordinates &reduction_axis, bool keep_dims, ICLTensor *output)
96{
Pablo Telloa0a4ba12019-12-11 13:04:34 +000097 // Perform validate step
98 ARM_COMPUTE_ERROR_THROW_ON(CLReduceMean::validate(input->info(), reduction_axis, keep_dims, output->info()));
99 // Output auto inizialitation if not yet initialized
100 const TensorShape output_shape = arm_compute::misc::shape_calculator::calculate_reduce_mean_shape(input, reduction_axis, keep_dims);
101 auto_init_if_empty(*output->info(), input->info()->clone()->set_tensor_shape(output_shape));
Michalis Spyrou7e9391b2018-10-05 14:49:28 +0100102
Michalis Spyroubcfd09a2019-05-01 13:03:59 +0100103 _reduction_ops = reduction_axis.num_dimensions();
104 _reduction_kernels.resize(_reduction_ops);
105 _reduced_outs.resize(_reduction_ops - (keep_dims ? 1 : 0));
106 _keep_dims = keep_dims;
Michalis Spyrou7e9391b2018-10-05 14:49:28 +0100107
Michalis Spyrou8d1b7182019-01-02 15:54:03 +0000108 Coordinates axis_local = reduction_axis;
109 const int input_dims = input->info()->num_dimensions();
110
Pablo Telloa0a4ba12019-12-11 13:04:34 +0000111 convert_negative_axis(axis_local, input_dims);
Michalis Spyrou8d1b7182019-01-02 15:54:03 +0000112
Michalis Spyrou7e9391b2018-10-05 14:49:28 +0100113 // Perform reduction for every axis
Pablo Telloa0a4ba12019-12-11 13:04:34 +0000114 for(int i = 0; i < _reduction_ops; ++i)
Michalis Spyrou7e9391b2018-10-05 14:49:28 +0100115 {
Michalis Spyroubcfd09a2019-05-01 13:03:59 +0100116 TensorShape out_shape = i == 0 ? input->info()->tensor_shape() : (&_reduced_outs[i - 1])->info()->tensor_shape();
Michalis Spyrou8d1b7182019-01-02 15:54:03 +0000117 out_shape.set(axis_local[i], 1);
Michalis Spyroubcfd09a2019-05-01 13:03:59 +0100118 auto in = (i == 0) ? input : (&_reduced_outs[i - 1]);
Michalis Spyrou7e9391b2018-10-05 14:49:28 +0100119
120 if(i == _reduction_ops - 1 && keep_dims)
121 {
Michalis Spyrou8d1b7182019-01-02 15:54:03 +0000122 _reduction_kernels[i].configure(in, output, axis_local[i], ReductionOperation::MEAN_SUM);
Michalis Spyrou7e9391b2018-10-05 14:49:28 +0100123 }
124 else
125 {
126 _reduced_outs[i].allocator()->init(TensorInfo(out_shape, input->info()->num_channels(), input->info()->data_type(), input->info()->quantization_info()));
Michalis Spyroubcfd09a2019-05-01 13:03:59 +0100127 _memory_group.manage(&_reduced_outs[i]);
128 _reduction_kernels[i].configure(in, &_reduced_outs[i], axis_local[i], ReductionOperation::MEAN_SUM);
Michalis Spyrou7e9391b2018-10-05 14:49:28 +0100129 }
130 }
131
132 // Allocate intermediate tensors
Pablo Telloa0a4ba12019-12-11 13:04:34 +0000133 for(int i = 0; i < _reduction_ops - (keep_dims ? 1 : 0); ++i)
Michalis Spyrou7e9391b2018-10-05 14:49:28 +0100134 {
135 _reduced_outs[i].allocator()->allocate();
136 }
137
138 // Configure reshape layer if we want to drop the dimensions
139 if(!keep_dims)
140 {
141 TensorShape out_shape = input->info()->tensor_shape();
Michalis Spyrou96f84612018-10-24 14:01:04 +0100142
143 // We have to sort the reduction axis vectors in order for remove_dimension
144 // to work properly
Michalis Spyrou8d1b7182019-01-02 15:54:03 +0000145 std::sort(axis_local.begin(), axis_local.begin() + _reduction_ops);
Pablo Telloa0a4ba12019-12-11 13:04:34 +0000146 for(int i = 0; i < _reduction_ops; ++i)
Michalis Spyrou7e9391b2018-10-05 14:49:28 +0100147 {
Michalis Spyrou8d1b7182019-01-02 15:54:03 +0000148 out_shape.remove_dimension(axis_local[i] - i);
Michalis Spyrou7e9391b2018-10-05 14:49:28 +0100149 }
150 auto_init_if_empty(*output->info(), input->info()->clone()->set_tensor_shape(out_shape));
Michalis Spyroubcfd09a2019-05-01 13:03:59 +0100151 _reshape.configure(&_reduced_outs[_reduction_ops - 1], output);
Michalis Spyrou7e9391b2018-10-05 14:49:28 +0100152 }
153}
154
155Status CLReduceMean::validate(const ITensorInfo *input, const Coordinates &reduction_axis, bool keep_dims, const ITensorInfo *output)
156{
Pablo Telloa0a4ba12019-12-11 13:04:34 +0000157 return validate_config(input, reduction_axis, keep_dims, output);
Michalis Spyrou7e9391b2018-10-05 14:49:28 +0100158}
159
160void CLReduceMean::run()
161{
Georgios Pinitasda953f22019-04-02 17:27:03 +0100162 MemoryGroupResourceScope scope_mg(_memory_group);
Michalis Spyrou7e9391b2018-10-05 14:49:28 +0100163
Pablo Telloa0a4ba12019-12-11 13:04:34 +0000164 for(auto &kernel : _reduction_kernels)
Michalis Spyrou7e9391b2018-10-05 14:49:28 +0100165 {
Pablo Telloa0a4ba12019-12-11 13:04:34 +0000166 kernel.run();
Michalis Spyrou7e9391b2018-10-05 14:49:28 +0100167 }
168
169 if(!_keep_dims)
170 {
171 _reshape.run();
172 }
Michalis Spyrou7e9391b2018-10-05 14:49:28 +0100173}
174} // namespace arm_compute