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