Compute Library
 23.05
CLReductionOperation Class Reference

Perform reduction operation. More...

#include <CLReductionOperation.h>

Collaboration diagram for CLReductionOperation:
[legend]

Public Member Functions

 CLReductionOperation (std::shared_ptr< IMemoryManager > memory_manager=nullptr)
 Default Constructor. More...
 
 ~CLReductionOperation ()
 Default Destructor. More...
 
 CLReductionOperation (const CLReductionOperation &)=delete
 Prevent instances of this class from being copied (As this class contains pointers) More...
 
 CLReductionOperation (CLReductionOperation &&)=default
 Default move constructor. More...
 
CLReductionOperationoperator= (const CLReductionOperation &)=delete
 Prevent instances of this class from being copied (As this class contains pointers) More...
 
CLReductionOperationoperator= (CLReductionOperation &&)=default
 Default move assignment operator. More...
 
void configure (ICLTensor *input, ICLTensor *output, unsigned int axis, ReductionOperation op, bool keep_dims=true)
 Set the input and output tensors. More...
 
void configure (const CLCompileContext &compile_context, ICLTensor *input, ICLTensor *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 CLReductionOperation. More...
 

Detailed Description

Perform reduction operation.

Definition at line 44 of file CLReductionOperation.h.

Constructor & Destructor Documentation

◆ CLReductionOperation() [1/3]

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

Default Constructor.

Parameters
[in]memory_manager(Optional) Memory manager.

Definition at line 41 of file CLReductionOperation.cpp.

References CLReductionOperation::~CLReductionOperation().

42  : _memory_group(std::move(memory_manager)), _unreshaped_output(), _reduction_kernel(), _reshape(), _reduction_axis(), _is_reshape_required(false)
43 {
44 }

◆ ~CLReductionOperation()

~CLReductionOperation ( )
default

Default Destructor.

Referenced by CLReductionOperation::CLReductionOperation().

◆ CLReductionOperation() [2/3]

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

◆ CLReductionOperation() [3/3]

Default move constructor.

Member Function Documentation

◆ configure() [1/2]

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

Set the input and output tensors.

Valid data layouts:

  • All

Valid data type configurations:

src dst
QASYMM8 QASYMM8
QASYMM8_SIGNED QASYMM8_SIGNED
F16 F16
F32 F32
S32 S32
Parameters
[in]inputSource tensor. Data types supported: QASYMM8/QASYMM8_SIGNED/F16/F32/S32.
[out]outputDestination tensor. Data types and data layouts supported: Same as input.
[in]axisAxis along which to reduce. Supported reduction axis : 0, 1, 2, 3
[in]opReduction operation to perform. Operations supported: MEAN_SUM, PROD, SUM_SQUARE, SUM, MIN, MAX
[in]keep_dims(Optional) Whether to keep the reduced dimension after the operation. Defaults to true.

Definition at line 106 of file CLReductionOperation.cpp.

References CLKernelLibrary::get().

Referenced by CLL2NormalizeLayer::configure(), and CLFFTConvolutionLayer::configure().

107 {
108  configure(CLKernelLibrary::get().get_compile_context(), input, output, axis, op, keep_dims);
109 }
static CLKernelLibrary & get()
Access the KernelLibrary singleton.
void configure(ICLTensor *input, ICLTensor *output, unsigned int axis, ReductionOperation op, bool keep_dims=true)
Set the input and output tensors.

◆ configure() [2/2]

void configure ( const CLCompileContext compile_context,
ICLTensor input,
ICLTensor output,
unsigned int  axis,
ReductionOperation  op,
bool  keep_dims = true 
)

Set the input and output tensors.

Parameters
[in]compile_contextThe compile context to be used.
[in]inputSource tensor. Data types supported: QASYMM8/QASYMM8_SIGNED/F16/F32/S32.
[out]outputDestination tensor. Data types and data layouts supported: Same as input.
[in]axisAxis along which to reduce. Supported reduction axis : 0, 1, 2, 3
[in]opReduction operation to perform. Operations supported: MEAN_SUM, PROD, SUM_SQUARE, SUM, MIN, MAX
[in]keep_dims(Optional) Whether to keep the reduced dimension after the operation. Defaults to true.

Definition at line 111 of file CLReductionOperation.cpp.

References CLTensorAllocator::allocate(), CLTensor::allocator(), ARM_COMPUTE_ERROR_ON_NULLPTR, ARM_COMPUTE_LOG_PARAMS, arm_compute::auto_init_if_empty(), ICloneable< T >::clone(), arm_compute::misc::shape_calculator::compute_reduced_shape(), CLReshapeLayer::configure(), ITensorInfo::data_type(), ITensor::info(), MemoryGroup::manage(), arm_compute::test::validation::output_shape, and ITensorInfo::tensor_shape().

112 {
114  ARM_COMPUTE_LOG_PARAMS(input, output, axis, op, keep_dims);
115  _reduction_axis = axis;
116  _is_reshape_required = !keep_dims;
117 
118  auto *output_internal = configure_intermediate_result_vector(input, output);
119 
120  if(_is_reshape_required)
121  {
122  const TensorShape output_shape = arm_compute::misc::shape_calculator::compute_reduced_shape(input->info()->tensor_shape(), axis, false);
123  const auto output_data_type = input->info()->data_type();
124  auto_init_if_empty(*output->info(), input->info()->clone()->set_tensor_shape(output_shape).set_data_type(output_data_type).reset_padding().set_is_resizable(true));
125 
126  _memory_group.manage(&_unreshaped_output);
127  }
128 
129  _reduction_kernel = std::make_unique<CLReductionOperationKernel>();
130  _reduction_kernel->configure(compile_context, input, output_internal, axis, op);
131 
132  if(_is_reshape_required)
133  {
134  _reshape.configure(compile_context, &_unreshaped_output, output);
135  _unreshaped_output.allocator()->allocate();
136  }
137 }
CLTensorAllocator * allocator()
Return a pointer to the tensor&#39;s allocator.
Definition: CLTensor.cpp:61
void manage(IMemoryManageable *obj) override
Sets a object to be managed by the given memory group.
Definition: MemoryGroup.h:79
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...
void allocate() override
Allocate size specified by TensorInfo of OpenCL memory.
#define ARM_COMPUTE_LOG_PARAMS(...)
#define ARM_COMPUTE_ERROR_ON_NULLPTR(...)
Definition: Validate.h:157
void configure(const ICLTensor *input, ICLTensor *output)
Initialise the kernel&#39;s inputs and outputs.

◆ operator=() [1/2]

CLReductionOperation& operator= ( const CLReductionOperation )
delete

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

◆ operator=() [2/2]

CLReductionOperation& operator= ( CLReductionOperation &&  )
default

Default move assignment operator.

◆ run()

void run ( )
overridevirtual

Run the kernels contained in the function.

For CPU 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 139 of file CLReductionOperation.cpp.

References CLScheduler::enqueue(), CLScheduler::get(), and CLReshapeLayer::run().

Referenced by CLL2NormalizeLayer::run(), and CLFFTConvolutionLayer::run().

140 {
141  MemoryGroupResourceScope scope_mg(_memory_group);
142 
143  CLScheduler::get().enqueue(*_reduction_kernel, false);
144 
145  if(_is_reshape_required)
146  {
147  _reshape.run();
148  }
149 }
static CLScheduler & get()
Access the scheduler singleton.
void run() override
Run the kernels contained in the function.
void enqueue(ICLKernel &kernel, bool flush=true)
Schedule the execution of the passed kernel if possible.

◆ 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 CLReductionOperation.

Parameters
[in]inputSource tensor info. Data types supported: QASYMM8/QASYMM8_SIGNED/F16/F32/S32.
[in]outputDestination tensor info. Data types and data layouts supported: Same as input.
[in]axisAxis along which to reduce. Supported reduction axis : 0, 1, 2, 3
[in]opReduction operation to perform. Operations supported: MEAN_SUM, PROD, SUM_SQUARE, SUM, MIN, MAX
[in]keep_dims(Optional) Whether to keep the reduced dimension after the operation. Defaults to true.
Returns
a status

Definition at line 48 of file CLReductionOperation.cpp.

References CLTensor::allocator(), ARM_COMPUTE_ERROR_ON_NULLPTR, 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(), arm_compute::test::validation::data_type, ITensorInfo::data_type(), ITensor::info(), ITensorAllocator::init(), arm_compute::test::validation::input, arm_compute::test::validation::input_shape, ITensorInfo::num_channels(), Dimensions< size_t >::num_max_dimensions, arm_compute::test::validation::qinfo, ITensorInfo::quantization_info(), TensorInfo::set_data_type(), ITensorInfo::set_num_channels(), ITensorInfo::set_quantization_info(), ITensorInfo::set_tensor_shape(), arm_compute::test::validation::shape, ITensorInfo::tensor_shape(), ITensorInfo::total_size(), CLReshapeLayer::validate(), and CLReductionOperationKernel::validate().

Referenced by arm_compute::test::validation::DATA_TEST_CASE(), and CLL2NormalizeLayer::validate().

49 {
51  ARM_COMPUTE_RETURN_ERROR_ON_MSG(axis >= TensorShape::num_max_dimensions, "Reduction axis greater than max number of dimensions");
52  ARM_COMPUTE_RETURN_ERROR_ON_MSG(axis > 3, "Unsupported reduction axis");
53 
54  const bool is_reshape_required = !keep_dims;
55 
56  if(is_reshape_required && output->total_size() != 0)
57  {
58  const TensorInfo expected_output_shape = output->clone()->set_tensor_shape(arm_compute::misc::shape_calculator::compute_reduced_shape(input->tensor_shape(), axis, keep_dims));
59  ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_SHAPES(&expected_output_shape, output);
60  }
61 
62  auto *output_internal = output;
63 
64  TensorInfo output_before_reshape;
65  const auto input_shape = input->tensor_shape();
66  const auto input_num_channles = input->num_channels();
67  const auto input_qinfo = input->quantization_info();
68  const auto output_data_type = output->data_type();
69 
70  auto initialize_tensorinfo = [](TensorInfo & ti, TensorShape shape, DataType data_type, int num_channels, QuantizationInfo qinfo)
71  {
72  ti.set_data_type(data_type).set_tensor_shape(shape).set_num_channels(num_channels).set_quantization_info(qinfo);
73  };
74 
75  if(is_reshape_required)
76  {
77  auto shape_before_reshape = input_shape;
78  shape_before_reshape.set(axis, 1);
79  initialize_tensorinfo(output_before_reshape, shape_before_reshape, output_data_type, input_num_channles, input_qinfo);
80  output_internal = &output_before_reshape;
81  }
82 
84 
85  if(is_reshape_required)
86  {
87  ARM_COMPUTE_RETURN_ON_ERROR(CLReshapeLayer::validate(output_internal, output));
88  }
89 
90  return Status{};
91 }
static Status validate(const ITensorInfo *input, const ITensorInfo *output)
Static function to check if given info will lead to a valid configuration of CLReshapeLayer.
#define ARM_COMPUTE_RETURN_ON_ERROR(status)
Checks if a status contains an error and returns it.
Definition: Error.h:204
const auto input_shape
Validate test suite is to test ARM_COMPUTE_RETURN_ON_* macros we use to check the validity of given a...
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:439
const QuantizationInfo qinfo
Definition: Im2Col.cpp:155
#define ARM_COMPUTE_RETURN_ERROR_ON_MSG(cond, msg)
If the condition is true, an error is returned.
Definition: Error.h:244
#define ARM_COMPUTE_ERROR_ON_NULLPTR(...)
Definition: Validate.h:157
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 CLReductionOperationKern...
static constexpr size_t num_max_dimensions
Number of dimensions the tensor has.
Definition: Dimensions.h:46
DataType
Available data types.
Definition: Types.h:79

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