Compute Library
 19.11
NEReduceMean Class Reference

Basic function to perform reduce operation. More...

#include <NEReduceMean.h>

Collaboration diagram for NEReduceMean:
[legend]

Public Member Functions

 NEReduceMean (std::shared_ptr< IMemoryManager > memory_manager=nullptr)
 Constructor. More...
 
void configure (ITensor *input, const Coordinates &reduction_axis, bool keep_dims, ITensor *output)
 Configure kernel. More...
 
void run () override
 Run the kernels contained in the function. More...
 
- Public Member Functions inherited from IFunction
virtual ~IFunction ()=default
 Destructor. More...
 
virtual void prepare ()
 Prepare the function for executing. More...
 

Static Public Member Functions

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 NEReduceMean. More...
 

Detailed Description

Basic function to perform reduce operation.

Definition at line 39 of file NEReduceMean.h.

Constructor & Destructor Documentation

◆ NEReduceMean()

NEReduceMean ( std::shared_ptr< IMemoryManager memory_manager = nullptr)

Constructor.

Definition at line 65 of file NEReduceMean.cpp.

66  : _memory_group(std::move(memory_manager)), _reduction_kernels(), _reduced_outs(), _reshape(), _reduction_ops(), _keep_dims()
67 {
68 }

Member Function Documentation

◆ configure()

void configure ( ITensor input,
const Coordinates reduction_axis,
bool  keep_dims,
ITensor output 
)

Configure kernel.

Note
Supported tensor rank: up to 4
Parameters
[in]inputSource tensor. Data type supported: QASYMM8/F16/F32
[in]reduction_axisReduction axis vector.
[in]keep_dimsIf positive, retains reduced dimensions with length 1.
[out]outputDestination tensor. Data type supported: Same as input

Definition at line 128 of file NEReduceMean.cpp.

129 {
130  // Perform validate step
131  ARM_COMPUTE_ERROR_THROW_ON(NEReduceMean::validate(input->info(), reduction_axis, keep_dims, output->info()));
132  // Output auto inizialitation if not yet initialized
133  const TensorShape output_shape = calculate_reduce_mean_shape(input, reduction_axis, keep_dims);
134  auto_init_if_empty(*output->info(), input->info()->clone()->set_tensor_shape(output_shape));
135 
136  _reduction_ops = reduction_axis.num_dimensions();
137  _reduction_kernels.resize(_reduction_ops);
138  _reduced_outs.resize(_reduction_ops - (keep_dims ? 1 : 0));
139  _keep_dims = keep_dims;
140 
141  Coordinates axis_local = reduction_axis;
142  const int input_dims = 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 ? input->info()->tensor_shape() : (&_reduced_outs[i - 1])->info()->tensor_shape();
150  out_shape.set(axis_local[i], 1);
151  auto in = (i == 0) ? input : (&_reduced_outs[i - 1]);
152 
153  if(i == _reduction_ops - 1 && keep_dims)
154  {
155  _reduction_kernels[i].configure(in, output, axis_local[i], ReductionOperation::MEAN_SUM);
156  }
157  else
158  {
159  _reduced_outs[i].allocator()->init(TensorInfo(out_shape, input->info()->num_channels(), input->info()->data_type(), input->info()->quantization_info()));
160  _memory_group.manage(&_reduced_outs[i]);
161  _reduction_kernels[i].configure(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 = input->info()->tensor_shape();
175  // We have to sort the reduction axis vectors in order for remove_dimension
176  // to work properly
177  std::sort(axis_local.begin(), axis_local.begin() + _reduction_ops);
178  for(int i = 0; i < _reduction_ops; ++i)
179  {
180  out_shape.remove_dimension(axis_local[i] - i);
181  }
182  auto_init_if_empty(*output->info(), input->info()->clone()->set_tensor_shape(out_shape));
183  _reshape.configure(&_reduced_outs[_reduction_ops - 1], output);
184  }
185 }
#define ARM_COMPUTE_ERROR_THROW_ON(status)
Definition: Error.h:455
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...
Definition: Helpers.inl:202
void manage(IMemoryManageable *obj) override
Sets a object to be managed by the given memory group.
Definition: MemoryGroup.h:79
void configure(const ITensor *input, ITensor *output)
Initialise the kernel's inputs and outputs.
Coordinates & convert_negative_axis(Coordinates &coords, int max_value)
Convert negative coordinates to positive in the range [0, num_dims_input].
Definition: Helpers.h:774
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 NEReduceMean.

References ARM_COMPUTE_ERROR_THROW_ON, arm_compute::auto_init_if_empty(), Dimensions< T >::begin(), NEReshapeLayer::configure(), arm_compute::convert_negative_axis(), ITensor::info(), arm_compute::test::validation::info, arm_compute::test::validation::input, MemoryGroup::manage(), arm_compute::MEAN_SUM, Dimensions< T >::num_dimensions(), arm_compute::test::validation::output_shape, TensorShape::remove_dimension(), TensorShape::set(), and NEReduceMean::validate().

◆ run()

void run ( )
overridevirtual

Run the kernels contained in the function.

For NEON kernels:

  • Multi-threading is used for the kernels which are parallelisable.
  • By default std::thread::hardware_concurrency() threads are used.
Note
CPPScheduler::set_num_threads() can be used to manually set the number of threads

For OpenCL kernels:

  • All the kernels are enqueued on the queue associated with CLScheduler.
  • The queue is then flushed.
Note
The function will not block until the kernels are executed. It is the user's responsibility to wait.
Will call prepare() on first run if hasn't been done

Implements IFunction.

Definition at line 187 of file NEReduceMean.cpp.

188 {
189  MemoryGroupResourceScope scope_mg(_memory_group);
190  for(auto &kernel : _reduction_kernels)
191  {
192  kernel.run();
193  }
194 
195  if(!_keep_dims)
196  {
197  _reshape.run();
198  }
199 }
void run() override final
Run the kernels contained in the function.

References INESimpleFunctionNoBorder::run().

◆ validate()

Status validate ( const ITensorInfo input,
const Coordinates reduction_axis,
bool  keep_dims,
const ITensorInfo output 
)
static

Static function to check if given info will lead to a valid configuration of NEReduceMean.

Parameters
[in]inputSource tensor. Data type supported: QASYMM8/F16/F32
[in]reduction_axisReduction axis vector.
[in]keep_dimsIf positive, retains reduced dimensions with length 1.
[in]outputDestination tensor. Data type supported: Same as input
Returns
A status

Definition at line 123 of file NEReduceMean.cpp.

124 {
125  return validate_config(input, reduction_axis, keep_dims, output);
126 }
Status validate_config(const ITensorInfo *input, const Coordinates &reduction_axis, bool keep_dims, const ITensorInfo *output)

References arm_compute::test::validation::input, and arm_compute::validate_config().

Referenced by NEReduceMean::configure().


The documentation for this class was generated from the following files: