Compute Library
 21.11
CLReduceMean.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2018-2021 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  */
25 
27 #include "arm_compute/core/Error.h"
28 #include "arm_compute/core/Types.h"
30 #include "src/core/CL/CLValidate.h"
34 
35 #include "src/common/utils/Log.h"
36 
37 namespace arm_compute
38 {
39 namespace
40 {
41 Status validate_config(const ITensorInfo *input, const Coordinates &reduction_axis, bool keep_dims, const ITensorInfo *output)
42 {
43  ARM_COMPUTE_UNUSED(keep_dims);
47  ARM_COMPUTE_RETURN_ERROR_ON(reduction_axis.num_dimensions() < 1);
48  ARM_COMPUTE_RETURN_ERROR_ON(reduction_axis.num_dimensions() > input->num_dimensions());
49 
50  const unsigned int reduction_ops = reduction_axis.num_dimensions();
51  const int input_dims = input->num_dimensions();
52  Coordinates axis_local = reduction_axis;
53 
54  for(unsigned int i = 0; i < axis_local.num_dimensions(); ++i)
55  {
56  //axis: The dimensions to reduce. Must be in the range [-rank(input_tensor), rank(input_tensor)).
57  ARM_COMPUTE_RETURN_ERROR_ON(axis_local[i] < (-static_cast<int>(input->num_dimensions())));
58  ARM_COMPUTE_RETURN_ERROR_ON(axis_local[i] >= static_cast<int>(input->num_dimensions()));
59  }
60 
61  if(output->tensor_shape().total_size() != 0)
62  {
63  // Only validate if not using auto_init for the output tensor
64  TensorShape out_shape = input->tensor_shape();
65  // Validate output_shape only if not using auto_init
66  convert_negative_axis(axis_local, input_dims);
67  std::sort(axis_local.begin(), axis_local.begin() + reduction_ops);
68  for(unsigned int i = 0; i < reduction_ops; ++i)
69  {
70  ARM_COMPUTE_RETURN_ERROR_ON(axis_local[i] > 3);
71  ARM_COMPUTE_RETURN_ERROR_ON(static_cast<unsigned int>(axis_local[i]) > input->num_dimensions() - 1);
72  if(output->total_size() > 0 && keep_dims)
73  {
74  ARM_COMPUTE_RETURN_ERROR_ON(output->dimension(axis_local[i]) != 1);
75  }
76  if(keep_dims)
77  {
78  out_shape.set(axis_local[i], 1);
79  }
80  else
81  {
82  ARM_COMPUTE_RETURN_ERROR_ON(i > static_cast<unsigned int>(axis_local[i]));
83  const unsigned int remove_index = axis_local[i] - i;
84  ARM_COMPUTE_RETURN_ERROR_ON(remove_index >= out_shape.num_dimensions());
85  out_shape.remove_dimension(remove_index);
86  }
87  }
88  const TensorInfo out_info = input->clone()->set_tensor_shape(out_shape);
90  const bool requant = is_data_type_quantized(input->data_type()) && input->quantization_info() != output->quantization_info();
91  if(requant)
92  {
93  TensorInfo input_no_quant(input->clone()->set_data_type(DataType::F32));
94  CLDequantizationLayer::validate(input, &input_no_quant);
95  TensorInfo output_no_quant(output->clone()->set_data_type(DataType::F32));
96  CLQuantizationLayer::validate(&output_no_quant, output);
97  }
98  }
99  return Status{};
100 }
101 }
102 
103 CLReduceMean::CLReduceMean(std::shared_ptr<IMemoryManager> memory_manager)
104  : _memory_group(std::move(memory_manager)), _reduction_kernels(), _reduced_outs(), _reshape(), _dequant(), _requant(), _reduction_ops(), _keep_dims(), _do_requant(), _input_no_quant(),
105  _output_no_quant()
106 {
107 }
108 
109 void CLReduceMean::configure(ICLTensor *input, const Coordinates &reduction_axis, bool keep_dims, ICLTensor *output)
110 {
111  configure(CLKernelLibrary::get().get_compile_context(), input, reduction_axis, keep_dims, output);
112 }
113 
114 void CLReduceMean::configure(const CLCompileContext &compile_context, ICLTensor *input, const Coordinates &reduction_axis, bool keep_dims, ICLTensor *output)
115 {
116  // Perform validate step
117  ARM_COMPUTE_ERROR_THROW_ON(CLReduceMean::validate(input->info(), reduction_axis, keep_dims, output->info()));
118  ARM_COMPUTE_LOG_PARAMS(input, reduction_axis, keep_dims, output);
119 
120  // Output auto inizialitation if not yet initialized
122  auto_init_if_empty(*output->info(), input->info()->clone()->set_tensor_shape(output_shape));
123 
124  _do_requant = is_data_type_quantized(input->info()->data_type()) && input->info()->quantization_info() != output->info()->quantization_info();
125  _reduction_ops = reduction_axis.num_dimensions();
126  _reduction_kernels.resize(_reduction_ops);
127  _reduced_outs.resize(_reduction_ops - (keep_dims ? 1 : 0));
128  _keep_dims = keep_dims;
129 
130  ICLTensor *tmp_input = input;
131  ICLTensor *tmp_output = output;
132  if(_do_requant)
133  {
134  _memory_group.manage(&_input_no_quant);
135  _memory_group.manage(&_output_no_quant);
136  TensorInfo output_no_quant_info = input->info()->clone()->set_tensor_shape(output_shape);
137  output_no_quant_info.set_data_type(DataType::F32);
138  auto_init_if_empty(*_output_no_quant.info(), output_no_quant_info);
139  auto_init_if_empty(*_input_no_quant.info(), input->info()->clone()->set_data_type(DataType::F32));
140  _dequant.configure(compile_context, input, &_input_no_quant);
141  tmp_input = &_input_no_quant;
142  tmp_output = &_output_no_quant;
143  }
144 
145  Coordinates axis_local = reduction_axis;
146  const int input_dims = tmp_input->info()->num_dimensions();
147 
148  convert_negative_axis(axis_local, input_dims);
149 
150  // Perform reduction for every axis
151  for(int i = 0; i < _reduction_ops; ++i)
152  {
153  TensorShape out_shape = i == 0 ? tmp_input->info()->tensor_shape() : (&_reduced_outs[i - 1])->info()->tensor_shape();
154  out_shape.set(axis_local[i], 1);
155  auto in = (i == 0) ? tmp_input : (&_reduced_outs[i - 1]);
156 
157  if(i == _reduction_ops - 1 && keep_dims)
158  {
159  _reduction_kernels[i].configure(compile_context, in, tmp_output, axis_local[i], ReductionOperation::MEAN_SUM);
160  }
161  else
162  {
163  _reduced_outs[i].allocator()->init(TensorInfo(out_shape, tmp_input->info()->num_channels(), tmp_input->info()->data_type(), tmp_input->info()->quantization_info()));
164  _memory_group.manage(&_reduced_outs[i]);
165  _reduction_kernels[i].configure(compile_context, in, &_reduced_outs[i], axis_local[i], ReductionOperation::MEAN_SUM);
166  }
167  }
168 
169  // Allocate intermediate tensors
170  for(int i = 0; i < _reduction_ops - (keep_dims ? 1 : 0); ++i)
171  {
172  _reduced_outs[i].allocator()->allocate();
173  }
174 
175  // Configure reshape layer if we want to drop the dimensions
176  if(!_keep_dims)
177  {
178  TensorShape out_shape = tmp_input->info()->tensor_shape();
179 
180  // We have to sort the reduction axis vectors in order for remove_dimension
181  // to work properly
182  std::sort(axis_local.begin(), axis_local.begin() + _reduction_ops);
183  for(int i = 0; i < _reduction_ops; ++i)
184  {
185  out_shape.remove_dimension(axis_local[i] - i);
186  }
187  auto_init_if_empty(*tmp_output->info(), tmp_input->info()->clone()->set_tensor_shape(out_shape));
188  _reshape.configure(compile_context, &_reduced_outs[_reduction_ops - 1], tmp_output);
189  }
190  if(_do_requant)
191  {
192  _requant.configure(compile_context, &_output_no_quant, output);
193  _input_no_quant.allocator()->allocate();
194  _output_no_quant.allocator()->allocate();
195  }
196 }
197 
198 Status CLReduceMean::validate(const ITensorInfo *input, const Coordinates &reduction_axis, bool keep_dims, const ITensorInfo *output)
199 {
200  return validate_config(input, reduction_axis, keep_dims, output);
201 }
202 
204 {
205  MemoryGroupResourceScope scope_mg(_memory_group);
206 
207  if(_do_requant)
208  {
209  _dequant.run();
210  }
211  for(auto &kernel : _reduction_kernels)
212  {
213  kernel.run();
214  }
215  if(!_keep_dims)
216  {
217  _reshape.run();
218  }
219  if(_do_requant)
220  {
221  _requant.run();
222  }
223 }
224 } // namespace arm_compute
bool is_data_type_quantized(DataType dt)
Check if a given data type is of quantized type.
Definition: Utils.h:981
virtual size_t num_dimensions() const =0
The number of dimensions of the tensor (rank)
#define ARM_COMPUTE_RETURN_ERROR_ON_F16_UNSUPPORTED(tensor)
Definition: CLValidate.h:35
static Status validate(const ITensorInfo *input, const ITensorInfo *output)
Static function to check if given info will lead to a valid configuration of CLDequantizationLayer.
Shape of a tensor.
Definition: TensorShape.h:39
void remove_dimension(size_t n)
Accessor to remove the dimension n from the tensor shape.
Definition: TensorShape.h:111
TensorInfo * info() const override
Interface to be implemented by the child class to return the tensor&#39;s metadata.
Definition: CLTensor.cpp:41
virtual DataType data_type() const =0
Data type used for each element of the tensor.
1 channel, 1 F32 per channel
ITensorInfo & set_data_type(DataType data_type) override
Set the data type to the specified value.
Definition: TensorInfo.cpp:287
static CLKernelLibrary & get()
Access the KernelLibrary singleton.
Store the tensor&#39;s metadata.
Definition: ITensorInfo.h:40
CLTensorAllocator * allocator()
Return a pointer to the tensor&#39;s allocator.
Definition: CLTensor.cpp:61
#define ARM_COMPUTE_ERROR_THROW_ON(status)
Definition: Error.h:455
Status class.
Definition: Error.h:52
#define ARM_COMPUTE_RETURN_ERROR_ON(cond)
If the condition is true, an error is returned.
Definition: Error.h:296
void configure(ICLTensor *input, const Coordinates &reduction_axis, bool keep_dims, ICLTensor *output)
Configure kernel.
Copyright (c) 2017-2021 Arm Limited.
void run() override
Run the kernels contained in the function.
1 channel, 1 F16 per channel
void run() override
Run the kernels contained in the function.
void run() override
Run the kernels contained in the function.
#define ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(...)
Definition: Validate.h:159
void manage(IMemoryManageable *obj) override
Sets a object to be managed by the given memory group.
Definition: MemoryGroup.h:79
#define ARM_COMPUTE_UNUSED(...)
To avoid unused variables warnings.
Definition: Error.h:152
TensorShape calculate_reduce_mean_shape(ITensorInfo *input, const Coordinates &reduction_axis, bool keep_dims)
Calculate the output tensor shape for the reduce mean operation.
virtual const TensorShape & tensor_shape() const =0
Size for each dimension of the tensor.
quantized, asymmetric fixed-point 8-bit number unsigned
Coordinates of an item.
Definition: Coordinates.h:37
bool auto_init_if_empty(ITensorInfo &info, const TensorShape &shape, int num_channels, DataType data_type, QuantizationInfo quantization_info=QuantizationInfo())
Auto initialize the tensor info (shape, number of channels and data type) if the current assignment i...
virtual std::unique_ptr< T > clone() const =0
Provide a clone of the current object of class T.
virtual ITensorInfo * info() const =0
Interface to be implemented by the child class to return the tensor&#39;s metadata.
void configure(const ICLTensor *input, ICLTensor *output)
Set the input and output tensors.
std::array< T, num_max_dimensions >::iterator begin()
Returns a read/write iterator that points to the first element in the dimension array.
Definition: Dimensions.h:215
virtual QuantizationInfo quantization_info() const =0
Get the quantization settings (scale and offset) of the tensor.
CLCompileContext class.
static Status validate(const ITensorInfo *input, const Coordinates &reduction_axis, bool keep_dims, const ITensorInfo *output)
Static function to check if given info will lead to a valid configuration of CLReduceMean.
void allocate() override
Allocate size specified by TensorInfo of OpenCL memory.
ScaleKernelInfo info(interpolation_policy, default_border_mode, PixelValue(), sampling_policy, false)
Memory group resources scope handling class.
Definition: IMemoryGroup.h:82
Interface for OpenCL tensor.
Definition: ICLTensor.h:42
#define ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_SHAPES(...)
Definition: Validate.h:439
void configure(const ICLTensor *input, ICLTensor *output)
Set the input and output tensors.
unsigned int num_dimensions() const
Returns the effective dimensionality of the tensor.
Definition: Dimensions.h:143
#define ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(t, c,...)
Definition: Validate.h:788
CLReduceMean(std::shared_ptr< IMemoryManager > memory_manager=nullptr)
Default constructor.
#define ARM_COMPUTE_LOG_PARAMS(...)
Store the tensor&#39;s metadata.
Definition: TensorInfo.h:43
void configure(const ICLTensor *input, ICLTensor *output)
Initialise the kernel&#39;s inputs and outputs.
quantized, asymmetric fixed-point 8-bit number signed
Coordinates & convert_negative_axis(Coordinates &coords, int max_value)
Convert negative coordinates to positive in the range [0, num_dims_input].
Definition: Helpers.h:257
static Status validate(const ITensorInfo *input, const ITensorInfo *output)
Static function to check if given info will lead to a valid configuration of CLQuantizationLayer.
void run() override
Run the kernels contained in the function.
virtual size_t num_channels() const =0
The number of channels for each tensor element.
TensorShape & set(size_t dimension, size_t value, bool apply_dim_correction=true, bool increase_dim_unit=true)
Accessor to set the value of one of the dimensions.
Definition: TensorShape.h:79