xref: /aosp_15_r20/external/ComputeLibrary/src/runtime/CL/functions/CLReduceMean.cpp (revision c217d954acce2dbc11938adb493fc0abd69584f3)
1*c217d954SCole Faust /*
2*c217d954SCole Faust  * Copyright (c) 2018-2021 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/CL/functions/CLReduceMean.h"
25*c217d954SCole Faust 
26*c217d954SCole Faust #include "arm_compute/core/CL/ICLTensor.h"
27*c217d954SCole Faust #include "arm_compute/core/Error.h"
28*c217d954SCole Faust #include "arm_compute/core/Types.h"
29*c217d954SCole Faust #include "arm_compute/core/utils/misc/ShapeCalculator.h"
30*c217d954SCole Faust #include "src/core/CL/CLValidate.h"
31*c217d954SCole Faust #include "src/core/CL/kernels/CLFillBorderKernel.h"
32*c217d954SCole Faust #include "src/core/CL/kernels/CLReductionOperationKernel.h"
33*c217d954SCole Faust #include "src/core/helpers/AutoConfiguration.h"
34*c217d954SCole Faust 
35*c217d954SCole Faust #include "src/common/utils/Log.h"
36*c217d954SCole Faust 
37*c217d954SCole Faust namespace arm_compute
38*c217d954SCole Faust {
39*c217d954SCole Faust namespace
40*c217d954SCole Faust {
validate_config(const ITensorInfo * input,const Coordinates & reduction_axis,bool keep_dims,const ITensorInfo * output)41*c217d954SCole Faust Status validate_config(const ITensorInfo *input, const Coordinates &reduction_axis, bool keep_dims, const ITensorInfo *output)
42*c217d954SCole Faust {
43*c217d954SCole Faust     ARM_COMPUTE_UNUSED(keep_dims);
44*c217d954SCole Faust     ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(input, output);
45*c217d954SCole Faust     ARM_COMPUTE_RETURN_ERROR_ON_F16_UNSUPPORTED(input);
46*c217d954SCole Faust     ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::QASYMM8, DataType::QASYMM8_SIGNED, DataType::F16, DataType::F32);
47*c217d954SCole Faust     ARM_COMPUTE_RETURN_ERROR_ON(reduction_axis.num_dimensions() < 1);
48*c217d954SCole Faust     ARM_COMPUTE_RETURN_ERROR_ON(reduction_axis.num_dimensions() > input->num_dimensions());
49*c217d954SCole Faust 
50*c217d954SCole Faust     const unsigned int reduction_ops = reduction_axis.num_dimensions();
51*c217d954SCole Faust     const int          input_dims    = input->num_dimensions();
52*c217d954SCole Faust     Coordinates        axis_local    = reduction_axis;
53*c217d954SCole Faust 
54*c217d954SCole Faust     for(unsigned int i = 0; i < axis_local.num_dimensions(); ++i)
55*c217d954SCole Faust     {
56*c217d954SCole Faust         //axis: The dimensions to reduce. Must be in the range [-rank(input_tensor), rank(input_tensor)).
57*c217d954SCole Faust         ARM_COMPUTE_RETURN_ERROR_ON(axis_local[i] < (-static_cast<int>(input->num_dimensions())));
58*c217d954SCole Faust         ARM_COMPUTE_RETURN_ERROR_ON(axis_local[i] >= static_cast<int>(input->num_dimensions()));
59*c217d954SCole Faust     }
60*c217d954SCole Faust 
61*c217d954SCole Faust     if(output->tensor_shape().total_size() != 0)
62*c217d954SCole Faust     {
63*c217d954SCole Faust         // Only validate if not using auto_init for the output tensor
64*c217d954SCole Faust         TensorShape out_shape = input->tensor_shape();
65*c217d954SCole Faust         // Validate output_shape only if not using auto_init
66*c217d954SCole Faust         convert_negative_axis(axis_local, input_dims);
67*c217d954SCole Faust         std::sort(axis_local.begin(), axis_local.begin() + reduction_ops);
68*c217d954SCole Faust         for(unsigned int i = 0; i < reduction_ops; ++i)
69*c217d954SCole Faust         {
70*c217d954SCole Faust             ARM_COMPUTE_RETURN_ERROR_ON(axis_local[i] > 3);
71*c217d954SCole Faust             ARM_COMPUTE_RETURN_ERROR_ON(static_cast<unsigned int>(axis_local[i]) > input->num_dimensions() - 1);
72*c217d954SCole Faust             if(output->total_size() > 0 && keep_dims)
73*c217d954SCole Faust             {
74*c217d954SCole Faust                 ARM_COMPUTE_RETURN_ERROR_ON(output->dimension(axis_local[i]) != 1);
75*c217d954SCole Faust             }
76*c217d954SCole Faust             if(keep_dims)
77*c217d954SCole Faust             {
78*c217d954SCole Faust                 out_shape.set(axis_local[i], 1);
79*c217d954SCole Faust             }
80*c217d954SCole Faust             else
81*c217d954SCole Faust             {
82*c217d954SCole Faust                 ARM_COMPUTE_RETURN_ERROR_ON(i > static_cast<unsigned int>(axis_local[i]));
83*c217d954SCole Faust                 const unsigned int remove_index = axis_local[i] - i;
84*c217d954SCole Faust                 ARM_COMPUTE_RETURN_ERROR_ON(remove_index >= out_shape.num_dimensions());
85*c217d954SCole Faust                 out_shape.remove_dimension(remove_index);
86*c217d954SCole Faust             }
87*c217d954SCole Faust         }
88*c217d954SCole Faust         const TensorInfo out_info = input->clone()->set_tensor_shape(out_shape);
89*c217d954SCole Faust         ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_SHAPES(output, &out_info);
90*c217d954SCole Faust         const bool requant = is_data_type_quantized(input->data_type()) && input->quantization_info() != output->quantization_info();
91*c217d954SCole Faust         if(requant)
92*c217d954SCole Faust         {
93*c217d954SCole Faust             TensorInfo input_no_quant(input->clone()->set_data_type(DataType::F32));
94*c217d954SCole Faust             CLDequantizationLayer::validate(input, &input_no_quant);
95*c217d954SCole Faust             TensorInfo output_no_quant(output->clone()->set_data_type(DataType::F32));
96*c217d954SCole Faust             CLQuantizationLayer::validate(&output_no_quant, output);
97*c217d954SCole Faust         }
98*c217d954SCole Faust     }
99*c217d954SCole Faust     return Status{};
100*c217d954SCole Faust }
101*c217d954SCole Faust }
102*c217d954SCole Faust 
CLReduceMean(std::shared_ptr<IMemoryManager> memory_manager)103*c217d954SCole Faust CLReduceMean::CLReduceMean(std::shared_ptr<IMemoryManager> memory_manager)
104*c217d954SCole Faust     : _memory_group(std::move(memory_manager)), _reduction_kernels(), _reduced_outs(), _reshape(), _dequant(), _requant(), _reduction_ops(), _keep_dims(), _do_requant(), _input_no_quant(),
105*c217d954SCole Faust       _output_no_quant()
106*c217d954SCole Faust {
107*c217d954SCole Faust }
108*c217d954SCole Faust 
configure(ICLTensor * input,const Coordinates & reduction_axis,bool keep_dims,ICLTensor * output)109*c217d954SCole Faust void CLReduceMean::configure(ICLTensor *input, const Coordinates &reduction_axis, bool keep_dims, ICLTensor *output)
110*c217d954SCole Faust {
111*c217d954SCole Faust     configure(CLKernelLibrary::get().get_compile_context(), input, reduction_axis, keep_dims, output);
112*c217d954SCole Faust }
113*c217d954SCole Faust 
configure(const CLCompileContext & compile_context,ICLTensor * input,const Coordinates & reduction_axis,bool keep_dims,ICLTensor * output)114*c217d954SCole Faust void CLReduceMean::configure(const CLCompileContext &compile_context, ICLTensor *input, const Coordinates &reduction_axis, bool keep_dims, ICLTensor *output)
115*c217d954SCole Faust {
116*c217d954SCole Faust     // Perform validate step
117*c217d954SCole Faust     ARM_COMPUTE_ERROR_THROW_ON(CLReduceMean::validate(input->info(), reduction_axis, keep_dims, output->info()));
118*c217d954SCole Faust     ARM_COMPUTE_LOG_PARAMS(input, reduction_axis, keep_dims, output);
119*c217d954SCole Faust 
120*c217d954SCole Faust     // Output auto inizialitation if not yet initialized
121*c217d954SCole Faust     const TensorShape output_shape = arm_compute::misc::shape_calculator::calculate_reduce_mean_shape(input->info(), reduction_axis, keep_dims);
122*c217d954SCole Faust     auto_init_if_empty(*output->info(), input->info()->clone()->set_tensor_shape(output_shape));
123*c217d954SCole Faust 
124*c217d954SCole Faust     _do_requant    = is_data_type_quantized(input->info()->data_type()) && input->info()->quantization_info() != output->info()->quantization_info();
125*c217d954SCole Faust     _reduction_ops = reduction_axis.num_dimensions();
126*c217d954SCole Faust     _reduction_kernels.resize(_reduction_ops);
127*c217d954SCole Faust     _reduced_outs.resize(_reduction_ops - (keep_dims ? 1 : 0));
128*c217d954SCole Faust     _keep_dims = keep_dims;
129*c217d954SCole Faust 
130*c217d954SCole Faust     ICLTensor *tmp_input  = input;
131*c217d954SCole Faust     ICLTensor *tmp_output = output;
132*c217d954SCole Faust     if(_do_requant)
133*c217d954SCole Faust     {
134*c217d954SCole Faust         _memory_group.manage(&_input_no_quant);
135*c217d954SCole Faust         _memory_group.manage(&_output_no_quant);
136*c217d954SCole Faust         TensorInfo output_no_quant_info = input->info()->clone()->set_tensor_shape(output_shape);
137*c217d954SCole Faust         output_no_quant_info.set_data_type(DataType::F32);
138*c217d954SCole Faust         auto_init_if_empty(*_output_no_quant.info(), output_no_quant_info);
139*c217d954SCole Faust         auto_init_if_empty(*_input_no_quant.info(), input->info()->clone()->set_data_type(DataType::F32));
140*c217d954SCole Faust         _dequant.configure(compile_context, input, &_input_no_quant);
141*c217d954SCole Faust         tmp_input  = &_input_no_quant;
142*c217d954SCole Faust         tmp_output = &_output_no_quant;
143*c217d954SCole Faust     }
144*c217d954SCole Faust 
145*c217d954SCole Faust     Coordinates axis_local = reduction_axis;
146*c217d954SCole Faust     const int   input_dims = tmp_input->info()->num_dimensions();
147*c217d954SCole Faust 
148*c217d954SCole Faust     convert_negative_axis(axis_local, input_dims);
149*c217d954SCole Faust 
150*c217d954SCole Faust     // Perform reduction for every axis
151*c217d954SCole Faust     for(int i = 0; i < _reduction_ops; ++i)
152*c217d954SCole Faust     {
153*c217d954SCole Faust         TensorShape out_shape = i == 0 ? tmp_input->info()->tensor_shape() : (&_reduced_outs[i - 1])->info()->tensor_shape();
154*c217d954SCole Faust         out_shape.set(axis_local[i], 1);
155*c217d954SCole Faust         auto in = (i == 0) ? tmp_input : (&_reduced_outs[i - 1]);
156*c217d954SCole Faust 
157*c217d954SCole Faust         if(i == _reduction_ops - 1 && keep_dims)
158*c217d954SCole Faust         {
159*c217d954SCole Faust             _reduction_kernels[i].configure(compile_context, in, tmp_output, axis_local[i], ReductionOperation::MEAN_SUM);
160*c217d954SCole Faust         }
161*c217d954SCole Faust         else
162*c217d954SCole Faust         {
163*c217d954SCole Faust             _reduced_outs[i].allocator()->init(TensorInfo(out_shape, tmp_input->info()->num_channels(), tmp_input->info()->data_type(), tmp_input->info()->quantization_info()));
164*c217d954SCole Faust             _memory_group.manage(&_reduced_outs[i]);
165*c217d954SCole Faust             _reduction_kernels[i].configure(compile_context, in, &_reduced_outs[i], axis_local[i], ReductionOperation::MEAN_SUM);
166*c217d954SCole Faust         }
167*c217d954SCole Faust     }
168*c217d954SCole Faust 
169*c217d954SCole Faust     // Allocate intermediate tensors
170*c217d954SCole Faust     for(int i = 0; i < _reduction_ops - (keep_dims ? 1 : 0); ++i)
171*c217d954SCole Faust     {
172*c217d954SCole Faust         _reduced_outs[i].allocator()->allocate();
173*c217d954SCole Faust     }
174*c217d954SCole Faust 
175*c217d954SCole Faust     // Configure reshape layer if we want to drop the dimensions
176*c217d954SCole Faust     if(!_keep_dims)
177*c217d954SCole Faust     {
178*c217d954SCole Faust         TensorShape out_shape = tmp_input->info()->tensor_shape();
179*c217d954SCole Faust 
180*c217d954SCole Faust         // We have to sort the reduction axis vectors in order for remove_dimension
181*c217d954SCole Faust         // to work properly
182*c217d954SCole Faust         std::sort(axis_local.begin(), axis_local.begin() + _reduction_ops);
183*c217d954SCole Faust         for(int i = 0; i < _reduction_ops; ++i)
184*c217d954SCole Faust         {
185*c217d954SCole Faust             out_shape.remove_dimension(axis_local[i] - i);
186*c217d954SCole Faust         }
187*c217d954SCole Faust         auto_init_if_empty(*tmp_output->info(), tmp_input->info()->clone()->set_tensor_shape(out_shape));
188*c217d954SCole Faust         _reshape.configure(compile_context, &_reduced_outs[_reduction_ops - 1], tmp_output);
189*c217d954SCole Faust     }
190*c217d954SCole Faust     if(_do_requant)
191*c217d954SCole Faust     {
192*c217d954SCole Faust         _requant.configure(compile_context, &_output_no_quant, output);
193*c217d954SCole Faust         _input_no_quant.allocator()->allocate();
194*c217d954SCole Faust         _output_no_quant.allocator()->allocate();
195*c217d954SCole Faust     }
196*c217d954SCole Faust }
197*c217d954SCole Faust 
validate(const ITensorInfo * input,const Coordinates & reduction_axis,bool keep_dims,const ITensorInfo * output)198*c217d954SCole Faust Status CLReduceMean::validate(const ITensorInfo *input, const Coordinates &reduction_axis, bool keep_dims, const ITensorInfo *output)
199*c217d954SCole Faust {
200*c217d954SCole Faust     return validate_config(input, reduction_axis, keep_dims, output);
201*c217d954SCole Faust }
202*c217d954SCole Faust 
run()203*c217d954SCole Faust void CLReduceMean::run()
204*c217d954SCole Faust {
205*c217d954SCole Faust     MemoryGroupResourceScope scope_mg(_memory_group);
206*c217d954SCole Faust 
207*c217d954SCole Faust     if(_do_requant)
208*c217d954SCole Faust     {
209*c217d954SCole Faust         _dequant.run();
210*c217d954SCole Faust     }
211*c217d954SCole Faust     for(auto &kernel : _reduction_kernels)
212*c217d954SCole Faust     {
213*c217d954SCole Faust         kernel.run();
214*c217d954SCole Faust     }
215*c217d954SCole Faust     if(!_keep_dims)
216*c217d954SCole Faust     {
217*c217d954SCole Faust         _reshape.run();
218*c217d954SCole Faust     }
219*c217d954SCole Faust     if(_do_requant)
220*c217d954SCole Faust     {
221*c217d954SCole Faust         _requant.run();
222*c217d954SCole Faust     }
223*c217d954SCole Faust }
224*c217d954SCole Faust } // namespace arm_compute
225