blob: c5de43da351876b4e14ec10d87edbcdc830a2834 [file] [log] [blame]
Jenkinsb9abeae2018-11-22 11:58:08 +00001/*
Jenkins514be652019-02-28 12:25:18 +00002 * Copyright (c) 2018-2019 ARM Limited.
Jenkinsb9abeae2018-11-22 11:58:08 +00003 *
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
Jenkins4ba87db2019-05-23 17:11:51 +010026#include "arm_compute/core/CL/CLValidate.h"
Jenkinsb9abeae2018-11-22 11:58:08 +000027#include "arm_compute/core/CL/ICLTensor.h"
28#include "arm_compute/core/CL/kernels/CLReductionOperationKernel.h"
Jenkins7f09cf72020-01-22 18:08:16 +000029#include "arm_compute/core/Error.h"
Jenkinsb9abeae2018-11-22 11:58:08 +000030#include "arm_compute/core/Types.h"
31#include "arm_compute/core/utils/helpers/tensor_transform.h"
Jenkins7f09cf72020-01-22 18:08:16 +000032#include "arm_compute/core/utils/misc/ShapeCalculator.h"
Jenkinsb9abeae2018-11-22 11:58:08 +000033#include "arm_compute/runtime/CL/CLScheduler.h"
34#include "support/ToolchainSupport.h"
35
36namespace arm_compute
37{
Jenkins7f09cf72020-01-22 18:08:16 +000038namespace
39{
40Status validate_config(const ITensorInfo *input, const Coordinates &reduction_axis, bool keep_dims, const ITensorInfo *output)
41{
42 ARM_COMPUTE_UNUSED(keep_dims);
43 ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(input, output);
44 ARM_COMPUTE_RETURN_ERROR_ON_F16_UNSUPPORTED(input);
45 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::QASYMM8, DataType::F16, DataType::F32);
46 ARM_COMPUTE_RETURN_ERROR_ON(reduction_axis.num_dimensions() < 1);
47 ARM_COMPUTE_RETURN_ERROR_ON(reduction_axis.num_dimensions() > input->num_dimensions());
48
49 const unsigned int reduction_ops = reduction_axis.num_dimensions();
50 const int input_dims = input->num_dimensions();
51 Coordinates axis_local = reduction_axis;
52
53 for(unsigned int i = 0; i < axis_local.num_dimensions(); ++i)
54 {
55 //axis: The dimensions to reduce. Must be in the range [-rank(input_tensor), rank(input_tensor)).
56 ARM_COMPUTE_RETURN_ERROR_ON(axis_local[i] < (-static_cast<int>(input->num_dimensions())));
57 ARM_COMPUTE_RETURN_ERROR_ON(axis_local[i] >= static_cast<int>(input->num_dimensions()));
58 }
59
60 if(output->tensor_shape().total_size() != 0)
61 {
62 // Only validate if not using auto_init for the output tensor
63 TensorShape out_shape = input->tensor_shape();
64 // Validate output_shape only if not using auto_init
65 convert_negative_axis(axis_local, input_dims);
66 std::sort(axis_local.begin(), axis_local.begin() + reduction_ops);
67 for(unsigned int i = 0; i < reduction_ops; ++i)
68 {
69 ARM_COMPUTE_RETURN_ERROR_ON(axis_local[i] > 3);
70 ARM_COMPUTE_RETURN_ERROR_ON(static_cast<unsigned int>(axis_local[i]) > input->num_dimensions() - 1);
71 if(output->total_size() > 0 && keep_dims)
72 {
73 ARM_COMPUTE_RETURN_ERROR_ON(output->dimension(axis_local[i]) != 1);
74 }
75 if(keep_dims)
76 {
77 out_shape.set(axis_local[i], 1);
78 }
79 else
80 {
81 ARM_COMPUTE_RETURN_ERROR_ON(i > static_cast<unsigned int>(axis_local[i]));
82 const unsigned int remove_index = axis_local[i] - i;
83 ARM_COMPUTE_RETURN_ERROR_ON(remove_index >= out_shape.num_dimensions());
84 out_shape.remove_dimension(remove_index);
85 }
86 }
87 const TensorInfo out_info = input->clone()->set_tensor_shape(out_shape);
88 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_SHAPES(output, &out_info);
89 }
90 return Status{};
91}
92}
Jenkinsb9abeae2018-11-22 11:58:08 +000093CLReduceMean::CLReduceMean(std::shared_ptr<IMemoryManager> memory_manager)
94 : _memory_group(std::move(memory_manager)), _reduction_kernels(), _reduced_outs(), _reshape(), _reduction_ops(), _keep_dims()
95{
96}
97void CLReduceMean::configure(ICLTensor *input, const Coordinates &reduction_axis, bool keep_dims, ICLTensor *output)
98{
Jenkins7f09cf72020-01-22 18:08:16 +000099 // Perform validate step
100 ARM_COMPUTE_ERROR_THROW_ON(CLReduceMean::validate(input->info(), reduction_axis, keep_dims, output->info()));
101 // Output auto inizialitation if not yet initialized
102 const TensorShape output_shape = arm_compute::misc::shape_calculator::calculate_reduce_mean_shape(input, reduction_axis, keep_dims);
103 auto_init_if_empty(*output->info(), input->info()->clone()->set_tensor_shape(output_shape));
Jenkinsb9abeae2018-11-22 11:58:08 +0000104
Jenkins4ba87db2019-05-23 17:11:51 +0100105 _reduction_ops = reduction_axis.num_dimensions();
106 _reduction_kernels.resize(_reduction_ops);
107 _reduced_outs.resize(_reduction_ops - (keep_dims ? 1 : 0));
108 _keep_dims = keep_dims;
Jenkinsb9abeae2018-11-22 11:58:08 +0000109
Jenkins514be652019-02-28 12:25:18 +0000110 Coordinates axis_local = reduction_axis;
111 const int input_dims = input->info()->num_dimensions();
112
Jenkins7f09cf72020-01-22 18:08:16 +0000113 convert_negative_axis(axis_local, input_dims);
Jenkins514be652019-02-28 12:25:18 +0000114
Jenkinsb9abeae2018-11-22 11:58:08 +0000115 // Perform reduction for every axis
Jenkins7f09cf72020-01-22 18:08:16 +0000116 for(int i = 0; i < _reduction_ops; ++i)
Jenkinsb9abeae2018-11-22 11:58:08 +0000117 {
Jenkins4ba87db2019-05-23 17:11:51 +0100118 TensorShape out_shape = i == 0 ? input->info()->tensor_shape() : (&_reduced_outs[i - 1])->info()->tensor_shape();
Jenkins514be652019-02-28 12:25:18 +0000119 out_shape.set(axis_local[i], 1);
Jenkins4ba87db2019-05-23 17:11:51 +0100120 auto in = (i == 0) ? input : (&_reduced_outs[i - 1]);
Jenkinsb9abeae2018-11-22 11:58:08 +0000121
122 if(i == _reduction_ops - 1 && keep_dims)
123 {
Jenkins514be652019-02-28 12:25:18 +0000124 _reduction_kernels[i].configure(in, output, axis_local[i], ReductionOperation::MEAN_SUM);
Jenkinsb9abeae2018-11-22 11:58:08 +0000125 }
126 else
127 {
128 _reduced_outs[i].allocator()->init(TensorInfo(out_shape, input->info()->num_channels(), input->info()->data_type(), input->info()->quantization_info()));
Jenkins4ba87db2019-05-23 17:11:51 +0100129 _memory_group.manage(&_reduced_outs[i]);
130 _reduction_kernels[i].configure(in, &_reduced_outs[i], axis_local[i], ReductionOperation::MEAN_SUM);
Jenkinsb9abeae2018-11-22 11:58:08 +0000131 }
132 }
133
134 // Allocate intermediate tensors
Jenkins7f09cf72020-01-22 18:08:16 +0000135 for(int i = 0; i < _reduction_ops - (keep_dims ? 1 : 0); ++i)
Jenkinsb9abeae2018-11-22 11:58:08 +0000136 {
137 _reduced_outs[i].allocator()->allocate();
138 }
139
140 // Configure reshape layer if we want to drop the dimensions
141 if(!keep_dims)
142 {
143 TensorShape out_shape = input->info()->tensor_shape();
144
145 // We have to sort the reduction axis vectors in order for remove_dimension
146 // to work properly
Jenkins514be652019-02-28 12:25:18 +0000147 std::sort(axis_local.begin(), axis_local.begin() + _reduction_ops);
Jenkins7f09cf72020-01-22 18:08:16 +0000148 for(int i = 0; i < _reduction_ops; ++i)
Jenkinsb9abeae2018-11-22 11:58:08 +0000149 {
Jenkins514be652019-02-28 12:25:18 +0000150 out_shape.remove_dimension(axis_local[i] - i);
Jenkinsb9abeae2018-11-22 11:58:08 +0000151 }
152 auto_init_if_empty(*output->info(), input->info()->clone()->set_tensor_shape(out_shape));
Jenkins4ba87db2019-05-23 17:11:51 +0100153 _reshape.configure(&_reduced_outs[_reduction_ops - 1], output);
Jenkinsb9abeae2018-11-22 11:58:08 +0000154 }
155}
156
157Status CLReduceMean::validate(const ITensorInfo *input, const Coordinates &reduction_axis, bool keep_dims, const ITensorInfo *output)
158{
Jenkins7f09cf72020-01-22 18:08:16 +0000159 return validate_config(input, reduction_axis, keep_dims, output);
Jenkinsb9abeae2018-11-22 11:58:08 +0000160}
161
162void CLReduceMean::run()
163{
Jenkins4ba87db2019-05-23 17:11:51 +0100164 MemoryGroupResourceScope scope_mg(_memory_group);
Jenkinsb9abeae2018-11-22 11:58:08 +0000165
Jenkins7f09cf72020-01-22 18:08:16 +0000166 for(auto &kernel : _reduction_kernels)
Jenkinsb9abeae2018-11-22 11:58:08 +0000167 {
Jenkins7f09cf72020-01-22 18:08:16 +0000168 kernel.run();
Jenkinsb9abeae2018-11-22 11:58:08 +0000169 }
170
171 if(!_keep_dims)
172 {
173 _reshape.run();
174 }
Jenkinsb9abeae2018-11-22 11:58:08 +0000175}
176} // namespace arm_compute