Compute Library
 19.08
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 40 of file NEReduceMean.h.

Constructor & Destructor Documentation

◆ NEReduceMean()

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

Constructor.

Definition at line 32 of file NEReduceMean.cpp.

33  : _memory_group(std::move(memory_manager)), _reduction_kernels(), _reduced_outs(), _reshape(), _reduction_ops(), _keep_dims()
34 {
35 }

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 80 of file NEReduceMean.cpp.

81 {
83 
84  _reduction_ops = reduction_axis.num_dimensions();
85  _reduction_kernels.resize(_reduction_ops);
86  _reduced_outs.resize(_reduction_ops - (keep_dims ? 1 : 0));
87  _keep_dims = keep_dims;
88 
89  Coordinates axis_local = reduction_axis;
90  const int input_dims = input->info()->num_dimensions();
91  const unsigned int reduction_ops = reduction_axis.num_dimensions();
92 
93  // Convert negative axis
94  for(unsigned int i = 0; i < reduction_ops; ++i)
95  {
96  axis_local[i] = wrap_around(axis_local[i], input_dims);
97  }
98 
99  // Perform reduction for every axis
100  for(unsigned int i = 0; i < _reduction_ops; ++i)
101  {
102  TensorShape out_shape = i == 0 ? input->info()->tensor_shape() : (&_reduced_outs[i - 1])->info()->tensor_shape();
103  out_shape.set(axis_local[i], 1);
104  auto in = (i == 0) ? input : (&_reduced_outs[i - 1]);
105 
106  if(i == _reduction_ops - 1 && keep_dims)
107  {
108  _reduction_kernels[i].configure(in, output, axis_local[i], ReductionOperation::MEAN_SUM);
109  }
110  else
111  {
112  _reduced_outs[i].allocator()->init(TensorInfo(out_shape, input->info()->num_channels(), input->info()->data_type(), input->info()->quantization_info()));
113  _memory_group.manage(&_reduced_outs[i]);
114  _reduction_kernels[i].configure(in, &_reduced_outs[i], axis_local[i], ReductionOperation::MEAN_SUM);
115  }
116  }
117 
118  // Allocate intermediate tensors
119  for(unsigned int i = 0; i < _reduction_ops - (keep_dims ? 1 : 0); ++i)
120  {
121  _reduced_outs[i].allocator()->allocate();
122  }
123 
124  // Configure reshape layer if we want to drop the dimensions
125  if(!keep_dims)
126  {
127  TensorShape out_shape = input->info()->tensor_shape();
128 
129  // We have to sort the reduction axis vectors in order for remove_dimension
130  // to work properly
131  std::sort(axis_local.begin(), axis_local.begin() + _reduction_ops);
132  for(unsigned int i = 0; i < _reduction_ops; ++i)
133  {
134  out_shape.remove_dimension(axis_local[i] - i);
135  }
136  auto_init_if_empty(*output->info(), input->info()->clone()->set_tensor_shape(out_shape));
137  _reshape.configure(&_reduced_outs[_reduction_ops - 1], output);
138  }
139 }
virtual size_t num_dimensions() const =0
The number of dimensions of the tensor (rank)
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:110
virtual DataType data_type() const =0
Data type used for each element of the tensor.
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:201
T wrap_around(T x, T m)
Wrap-around a number within the range 0 <= x < m.
Definition: Helpers.h:764
void manage(TensorType *obj)
Sets a object to be managed by the given memory group.
virtual const TensorShape & tensor_shape() const =0
Size for each dimension of the tensor.
Coordinates of an item.
Definition: Coordinates.h:37
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's metadata.
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:194
virtual QuantizationInfo quantization_info() const =0
Get the quantization settings (scale and offset) of the tensor.
#define ARM_COMPUTE_ERROR_ON_NULLPTR(...)
Definition: Validate.h:161
unsigned int num_dimensions() const
Returns the effective dimensionality of the tensor.
Definition: Dimensions.h:122
void configure(const ITensor *input, ITensor *output)
Initialise the kernel's inputs and outputs.
TensorShape & set(size_t dimension, size_t value, bool apply_dim_correction=true)
Accessor to set the value of one of the dimensions.
Definition: TensorShape.h:78
Store the tensor's metadata.
Definition: TensorInfo.h:45
virtual size_t num_channels() const =0
The number of channels for each tensor element.

References ARM_COMPUTE_ERROR_ON_NULLPTR, arm_compute::auto_init_if_empty(), Dimensions< T >::begin(), ICloneable< T >::clone(), NEReshapeLayer::configure(), ITensorInfo::data_type(), ITensor::info(), arm_compute::test::validation::info, MemoryGroupBase< TensorType >::manage(), arm_compute::MEAN_SUM, ITensorInfo::num_channels(), Dimensions< T >::num_dimensions(), ITensorInfo::num_dimensions(), ITensorInfo::quantization_info(), TensorShape::remove_dimension(), TensorShape::set(), ITensorInfo::tensor_shape(), and arm_compute::wrap_around().

◆ 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 141 of file NEReduceMean.cpp.

142 {
143  MemoryGroupResourceScope scope_mg(_memory_group);
144 
145  for(unsigned int i = 0; i < _reduction_ops; ++i)
146  {
147  _reduction_kernels[i].run();
148  }
149 
150  if(!_keep_dims)
151  {
152  _reshape.run();
153  }
154 }
void run() override final
Run the kernels contained in the function.
Memory group resources scope handling class.
Definition: IMemoryGroup.h:46

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 37 of file NEReduceMean.cpp.

38 {
39  ARM_COMPUTE_UNUSED(keep_dims);
43  ARM_COMPUTE_RETURN_ERROR_ON(reduction_axis.num_dimensions() > input->num_dimensions());
44 
45  TensorShape out_shape = input->tensor_shape();
46  const unsigned int reduction_ops = reduction_axis.num_dimensions();
47  const int input_dims = input->num_dimensions();
48  Coordinates axis_local = reduction_axis;
49 
50  // Convert negative axis
51  for(unsigned int i = 0; i < reduction_ops; ++i)
52  {
53  axis_local[i] = wrap_around(axis_local[i], input_dims);
54  }
55 
56  std::sort(axis_local.begin(), axis_local.begin() + reduction_ops);
57  for(unsigned int i = 0; i < reduction_ops; ++i)
58  {
59  ARM_COMPUTE_RETURN_ERROR_ON(axis_local[i] > 3);
60  ARM_COMPUTE_RETURN_ERROR_ON(static_cast<unsigned int>(axis_local[i]) > input->num_dimensions() - 1);
61  if(output->total_size() > 0 && keep_dims)
62  {
63  ARM_COMPUTE_RETURN_ERROR_ON(output->dimension(axis_local[i]) != 1);
64  }
65  if(keep_dims)
66  {
67  out_shape.set(axis_local[i], 1);
68  }
69  else
70  {
71  out_shape.remove_dimension(axis_local[i] - i);
72  }
73  }
74  const TensorInfo out_info = input->clone()->set_tensor_shape(out_shape);
76 
77  return Status{};
78 }
virtual size_t num_dimensions() const =0
The number of dimensions of the tensor (rank)
Shape of a tensor.
Definition: TensorShape.h:39
virtual size_t dimension(size_t index) const =0
Return the size of the requested dimension.
#define ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(t, c,...)
Definition: Validate.h:791
1 channel, 1 F32 per channel
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:244
#define ARM_COMPUTE_RETURN_ERROR_ON_CPU_F16_UNSUPPORTED(tensor)
Definition: Validate.h:71
1 channel, 1 F16 per channel
T wrap_around(T x, T m)
Wrap-around a number within the range 0 <= x < m.
Definition: Helpers.h:764
#define ARM_COMPUTE_UNUSED(...)
To avoid unused variables warnings.
Definition: Error.h:160
#define ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_SHAPES(...)
Definition: Validate.h:443
virtual const TensorShape & tensor_shape() const =0
Size for each dimension of the tensor.
quantized, asymmetric fixed-point 8-bit number
Coordinates of an item.
Definition: Coordinates.h:37
virtual std::unique_ptr< T > clone() const =0
Provide a clone of the current object of class T.
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:194
#define ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(...)
Definition: Validate.h:163
virtual size_t total_size() const =0
Returns the total size of the tensor in bytes.
unsigned int num_dimensions() const
Returns the effective dimensionality of the tensor.
Definition: Dimensions.h:122
Store the tensor's metadata.
Definition: TensorInfo.h:45

References ARM_COMPUTE_RETURN_ERROR_ON, ARM_COMPUTE_RETURN_ERROR_ON_CPU_F16_UNSUPPORTED, ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN, ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_SHAPES, ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR, ARM_COMPUTE_UNUSED, Dimensions< T >::begin(), ICloneable< T >::clone(), ITensorInfo::dimension(), arm_compute::F16, arm_compute::F32, Dimensions< T >::num_dimensions(), ITensorInfo::num_dimensions(), arm_compute::QASYMM8, ITensorInfo::tensor_shape(), ITensorInfo::total_size(), and arm_compute::wrap_around().


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