Compute Library
 20.11
NEReductionOperation Class Reference

Basic function to simulate a reduction operation. More...

#include <NEReductionOperation.h>

Collaboration diagram for NEReductionOperation:
[legend]

Public Member Functions

 NEReductionOperation (std::shared_ptr< IMemoryManager > memory_manager=nullptr)
 Default constructor. More...
 
 NEReductionOperation (const NEReductionOperation &)=delete
 Prevent instances of this class from being copied (As this class contains pointers) More...
 
 NEReductionOperation (NEReductionOperation &&)=default
 Default move constructor. More...
 
NEReductionOperationoperator= (const NEReductionOperation &)=delete
 Prevent instances of this class from being copied (As this class contains pointers) More...
 
NEReductionOperationoperator= (NEReductionOperation &&)=default
 Default move assignment operator. More...
 
 ~NEReductionOperation ()
 Default destructor. More...
 
void configure (ITensor *input, ITensor *output, unsigned int axis, ReductionOperation op, bool keep_dims=true)
 Set the input and output tensors. 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 ITensorInfo *output, unsigned int axis, ReductionOperation op, bool keep_dims=true)
 Static function to check if given info will lead to a valid configuration of NEReductionOperation. More...
 

Detailed Description

Basic function to simulate a reduction operation.

This function calls the following NEON kernels:

  1. NEReshapeLayer
  2. NEReductionOperationKernel

Definition at line 44 of file NEReductionOperation.h.

Constructor & Destructor Documentation

◆ NEReductionOperation() [1/3]

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

Default constructor.

Definition at line 61 of file NEReductionOperation.cpp.

62  : _memory_group(memory_manager), _reduction_kernel(), _reshape(), _output_internal(), _window_split(0), _reduction_axis(), _is_reshape_required(false)
63 {
64 }

◆ NEReductionOperation() [2/3]

Prevent instances of this class from being copied (As this class contains pointers)

◆ NEReductionOperation() [3/3]

Default move constructor.

◆ ~NEReductionOperation()

~NEReductionOperation ( )
default

Default destructor.

Member Function Documentation

◆ configure()

void configure ( ITensor input,
ITensor output,
unsigned int  axis,
ReductionOperation  op,
bool  keep_dims = true 
)

Set the input and output tensors.

Parameters
[in,out]inputSource tensor. Data type supported: QASYMM8_SIGNED/QASYMM8/F16/F32/S32. Data layouts supported: NCHW. (Written to only for border_size != 0)
[out]outputDestination tensor. Data types and data layouts supported: same as input.
[in]axisDimension along which to reduce. Supported reduction axis : 0
[in]opReduction operation to perform.
[in]keep_dims(Optional) Whether to keep the reduced dimension after the operation. Defaults to true.

Definition at line 105 of file NEReductionOperation.cpp.

106 {
108 
109  _is_reshape_required = !keep_dims;
110 
111  auto *output_internal = output;
112  const auto is_arg_min_max = (op == ReductionOperation::ARG_IDX_MAX) || (op == ReductionOperation::ARG_IDX_MIN);
113 
114  if(_is_reshape_required)
115  {
116  const auto output_internal_shape = arm_compute::misc::shape_calculator::compute_reduced_shape(input->info()->tensor_shape(), axis);
117  const auto output_external_shape = arm_compute::misc::shape_calculator::compute_reduced_shape(input->info()->tensor_shape(), axis, false);
118  const auto output_data_type = is_arg_min_max ? DataType::S32 : input->info()->data_type();
119  const auto num_channels = input->info()->num_channels();
120  const auto qinfo = input->info()->quantization_info();
121 
122  _output_internal.allocator()->init(input->info()->clone()->set_data_type(output_data_type).set_tensor_shape(output_internal_shape).reset_padding().set_is_resizable(true).set_num_channels(
123  num_channels).set_quantization_info(qinfo));
124  _memory_group.manage(&_output_internal);
125  output_internal = &_output_internal;
126  auto_init_if_empty(*output->info(), input->info()->clone()->set_data_type(output_data_type).set_tensor_shape(output_external_shape).reset_padding().set_is_resizable(true));
127  }
128 
129  ARM_COMPUTE_ERROR_THROW_ON(NEReductionOperation::validate(input->info(), output->info(), axis, op, keep_dims));
130 
131  // Configure reduction kernel
132  _reduction_kernel = arm_compute::support::cpp14::make_unique<NEReductionOperationKernel>();
133  _reduction_kernel->configure(input, output_internal, axis, op);
134  _window_split = reduction_window_split_dimension(axis);
135  _reduction_axis = axis;
136 
137  if(_is_reshape_required)
138  {
139  _reshape.configure(output_internal, output);
140  _output_internal.allocator()->allocate();
141  }
142 }
void init(const TensorAllocator &allocator, const Coordinates &coords, TensorInfo &sub_info)
Shares the same backing memory with another tensor allocator, while the tensor info might be differen...
#define ARM_COMPUTE_ERROR_THROW_ON(status)
Definition: Error.h:455
TensorAllocator * allocator()
Return a pointer to the tensor's allocator.
Definition: Tensor.cpp:48
1 channel, 1 S32 per channel
void manage(IMemoryManageable *obj) override
Sets a object to be managed by the given memory group.
Definition: MemoryGroup.h:79
static Status validate(const ITensorInfo *input, const ITensorInfo *output, unsigned int axis, ReductionOperation op, bool keep_dims=true)
Static function to check if given info will lead to a valid configuration of NEReductionOperation.
void allocate() override
Allocate size specified by TensorInfo of CPU memory.
TensorShape compute_reduced_shape(const TensorShape &input, unsigned int axis, bool keep_dims=true)
Calculate the reduced shape of a tensor given an axis.
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...
const QuantizationInfo qinfo
Definition: Im2Col.cpp:150
void configure(const ITensor *input, ITensor *output)
Initialise the kernel's inputs and outputs.
#define ARM_COMPUTE_ERROR_ON_NULLPTR(...)
Definition: Validate.h:161

References TensorAllocator::allocate(), Tensor::allocator(), arm_compute::ARG_IDX_MAX, arm_compute::ARG_IDX_MIN, ARM_COMPUTE_ERROR_ON_NULLPTR, ARM_COMPUTE_ERROR_THROW_ON, arm_compute::auto_init_if_empty(), arm_compute::misc::shape_calculator::compute_reduced_shape(), NEReshapeLayer::configure(), ITensor::info(), TensorAllocator::init(), arm_compute::test::validation::input, MemoryGroup::manage(), arm_compute::test::validation::qinfo, arm_compute::S32, and NEReductionOperation::validate().

Referenced by NEL2NormalizeLayer::configure(), and NEFFTConvolutionLayer::configure().

◆ operator=() [1/2]

NEReductionOperation& operator= ( const NEReductionOperation )
delete

Prevent instances of this class from being copied (As this class contains pointers)

◆ operator=() [2/2]

NEReductionOperation& operator= ( NEReductionOperation &&  )
default

Default move assignment operator.

◆ 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 144 of file NEReductionOperation.cpp.

145 {
146  MemoryGroupResourceScope scope_mg(_memory_group);
147  NEScheduler::get().schedule(_reduction_kernel.get(), _window_split);
148  if(_is_reshape_required)
149  {
150  _reshape.run();
151  }
152 }
virtual void schedule(ICPPKernel *kernel, const Hints &hints)=0
Runs the kernel in the same thread as the caller synchronously.
void run() override
Run the kernels contained in the function.
static IScheduler & get()
Access the scheduler singleton.
Definition: Scheduler.cpp:95

References Scheduler::get(), NEReshapeLayer::run(), and IScheduler::schedule().

Referenced by NEL2NormalizeLayer::run(), and NEFFTConvolutionLayer::run().

◆ validate()

Status validate ( const ITensorInfo input,
const ITensorInfo output,
unsigned int  axis,
ReductionOperation  op,
bool  keep_dims = true 
)
static

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

Parameters
[in]inputSource tensor info. Data type supported: QASYMM8_SIGNED/QASYMM8/F16/F32/S32. Data layouts supported: NCHW.
[in]outputDestination tensor info. Data types and data layouts supported: same as input.
[in]axisDimension along which to reduce. Supported reduction axis : 0
[in]opReduction operation to perform.
[in]keep_dims(Optional) Whether to keep the reduced dimension after the operation. Defaults to true.
Returns
a status

Definition at line 66 of file NEReductionOperation.cpp.

67 {
68  ARM_COMPUTE_RETURN_ERROR_ON_MSG(axis >= TensorShape::num_max_dimensions, "Reduction axis greater than max number of dimensions");
69  ARM_COMPUTE_RETURN_ERROR_ON_MSG(axis > 3, "Unsupported reduction axis");
70 
71  const auto is_reshape_required = !keep_dims;
72 
73  auto *output_internal = output;
74 
75  TensorInfo info_before_reshape;
76 
77  if(is_reshape_required)
78  {
79  const TensorInfo expected_output_shape = output->clone()->set_tensor_shape(arm_compute::misc::shape_calculator::compute_reduced_shape(input->tensor_shape(), axis, keep_dims));
80  ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_SHAPES(&expected_output_shape, output);
81 
82  auto shape_before_reshape = input->tensor_shape();
83  shape_before_reshape.set(axis, 1);
84 
85  const auto input_num_channles = input->num_channels();
86  const auto input_qinfo = input->quantization_info();
87  const auto is_arg_min_max = (op == ReductionOperation::ARG_IDX_MAX) || (op == ReductionOperation::ARG_IDX_MIN);
88  const auto output_data_type = is_arg_min_max ? DataType::S32 : output->data_type();
89 
90  info_before_reshape.set_data_type(output_data_type).set_tensor_shape(shape_before_reshape).set_num_channels(input_num_channles).set_quantization_info(input_qinfo);
91 
92  output_internal = &info_before_reshape;
93  }
94 
96 
97  if(is_reshape_required)
98  {
99  ARM_COMPUTE_RETURN_ON_ERROR(NEReshapeLayer::validate(output_internal, output));
100  }
101 
102  return Status{};
103 }
static Status validate(const ITensorInfo *input, const ITensorInfo *output)
Static function to check if given info will lead to a valid configuration of NEReshapeLayer.
#define ARM_COMPUTE_RETURN_ON_ERROR(status)
Checks if a status contains an error and returns it.
Definition: Error.h:204
1 channel, 1 S32 per channel
static Status validate(const ITensorInfo *input, const ITensorInfo *output, unsigned int axis, ReductionOperation op)
Static function to check if given info will lead to a valid configuration of NEReductionOperationKern...
TensorShape compute_reduced_shape(const TensorShape &input, unsigned int axis, bool keep_dims=true)
Calculate the reduced shape of a tensor given an axis.
#define ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_SHAPES(...)
Definition: Validate.h:443
#define ARM_COMPUTE_RETURN_ERROR_ON_MSG(cond, msg)
If the condition is true, an error is returned.
Definition: Error.h:244
static constexpr size_t num_max_dimensions
Number of dimensions the tensor has.
Definition: Dimensions.h:45

References arm_compute::ARG_IDX_MAX, arm_compute::ARG_IDX_MIN, ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_SHAPES, ARM_COMPUTE_RETURN_ERROR_ON_MSG, ARM_COMPUTE_RETURN_ON_ERROR, ICloneable< T >::clone(), arm_compute::misc::shape_calculator::compute_reduced_shape(), ITensorInfo::data_type(), arm_compute::test::validation::input, Dimensions< size_t >::num_max_dimensions, arm_compute::S32, TensorInfo::set_data_type(), ITensorInfo::set_num_channels(), ITensorInfo::set_quantization_info(), ITensorInfo::set_tensor_shape(), NEReshapeLayer::validate(), and NEReductionOperationKernel::validate().

Referenced by NEReductionOperation::configure(), NEL2NormalizeLayer::validate(), and NEArgMinMaxLayer::validate().


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