ONE - On-device Neural Engine
Loading...
Searching...
No Matches
arm_compute::CLReduceOperationKernel Class Reference

Class to define interface for the reduce operation kernel. More...

#include <CLReduceOperationKernel.h>

Collaboration diagram for arm_compute::CLReduceOperationKernel:

Public Member Functions

 CLReduceOperationKernel ()
 Default constructor.
 
 CLReduceOperationKernel (const CLReduceOperationKernel &)=delete
 Prevent instances of this class from being copied (As this class contains pointers)
 
CLReduceOperationKerneloperator= (const CLReduceOperationKernel &)=delete
 Prevent instances of this class from being copied (As this class contains pointers)
 
 CLReduceOperationKernel (CLReduceOperationKernel &&)=default
 Allow instances of this class to be moved.
 
CLReduceOperationKerneloperator= (CLReduceOperationKernel &&)=default
 Allow instances of this class to be moved.
 
 ~CLReduceOperationKernel ()=default
 Default destructor.
 
void configure (const ICLTensor *input, ICLTensor *output, const uint32_t axis, ReductionOperation op)
 Set the input and output tensors.
 
void run (const Window &window, cl::CommandQueue &queue) override
 

Static Public Member Functions

static Status validate (const ITensorInfo *input, const ITensorInfo *output, const uint32_t axis, ReductionOperation op)
 Static function to check if given info will lead to a valid configuration of CLReduceOperationKernel.
 

Detailed Description

Class to define interface for the reduce operation kernel.

Definition at line 60 of file CLReduceOperationKernel.h.

Constructor & Destructor Documentation

◆ CLReduceOperationKernel() [1/3]

CLReduceOperationKernel::CLReduceOperationKernel ( )

Default constructor.

Definition at line 101 of file CLReduceOperationKernel.cpp.

101: _input(nullptr), _output(nullptr), _axis() {}

◆ CLReduceOperationKernel() [2/3]

arm_compute::CLReduceOperationKernel::CLReduceOperationKernel ( const CLReduceOperationKernel )
delete

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

◆ CLReduceOperationKernel() [3/3]

arm_compute::CLReduceOperationKernel::CLReduceOperationKernel ( CLReduceOperationKernel &&  )
default

Allow instances of this class to be moved.

◆ ~CLReduceOperationKernel()

arm_compute::CLReduceOperationKernel::~CLReduceOperationKernel ( )
default

Default destructor.

References validate().

Member Function Documentation

◆ configure()

void CLReduceOperationKernel::configure ( const ICLTensor *  input,
ICLTensor *  output,
const uint32_t  axis,
ReductionOperation  op 
)

Set the input and output tensors.

Parameters
[in]inputSource tensor. Data types supported: U8/S32/F32.
[out]outputDestination tensor. Data types supported: Same as input. Output will have the same number of dimensions as input.
[in]axisAxis along which to reduce.
[in]opReduce operation to perform.
Returns
N/A

Definition at line 103 of file CLReduceOperationKernel.cpp.

105{
106 ARM_COMPUTE_ERROR_ON_NULLPTR(input, output);
107
108 ARM_COMPUTE_ERROR_THROW_ON(validate_arguments(input->info(), output->info(), axis, op));
109
110 _input = input;
111 _output = output;
112 _axis = axis;
113
114 std::unique_ptr<ITensorInfo> output_info = output->info()->clone();
115 output_info->set_tensor_shape(inferOutputShape(input->info()->tensor_shape(), axis));
116
117 // Construct kernel name
118 std::string kernel_name;
119 int op_code = 0;
120 if (op == ReductionOperation::MAX)
121 {
122 kernel_name = "reduce_min_max";
123 op_code = 1;
124 }
125 else if (op == ReductionOperation::MIN)
126 {
127 kernel_name = "reduce_min_max";
128 op_code = 2;
129 }
130 else if (op == ReductionOperation::SUM)
131 {
132 kernel_name = "reduce_sum_mean";
133 op_code = 3;
134 }
135 else if (op == ReductionOperation::MEAN_SUM)
136 {
137 kernel_name = "reduce_sum_mean";
138 op_code = 4;
139 }
140 else
141 throw std::runtime_error("Operation not supported, yet");
142
143 // Set kernel build options
144 std::set<std::string> build_opts;
145 build_opts.emplace("-DDATA_TYPE=" + get_cl_type_from_data_type(output_info->data_type()));
146 build_opts.emplace("-DDEPTH_OUT=" + support::cpp11::to_string(output_info->dimension(2)));
147 build_opts.emplace("-DOP_CODE=" + support::cpp11::to_string(op_code));
148
149 // Create kernel
150 _kernel =
151 static_cast<cl::Kernel>(CLKernelLibraryEx::get().create_kernel(kernel_name, build_opts));
152
153 // Configure kernel window
154 Window win = calculate_max_window(*output_info, Steps());
155
156 Coordinates coord;
157 coord.set_num_dimensions(output_info->num_dimensions());
158 output->info()->set_valid_region(ValidRegion(coord, output_info->tensor_shape()));
159
160 ICLKernel::configure_internal(win);
161}
static CLKernelLibraryEx & get()
Get the KernelLibrary singleton.
Kernel create_kernel(const std::string &kernel_name, const StringSet &build_options_set={}) const
Create a kernel from the kernel library.

References arm_compute::CLKernelLibraryEx::create_kernel(), and arm_compute::CLKernelLibraryEx::get().

◆ operator=() [1/2]

CLReduceOperationKernel & arm_compute::CLReduceOperationKernel::operator= ( CLReduceOperationKernel &&  )
default

Allow instances of this class to be moved.

◆ operator=() [2/2]

CLReduceOperationKernel & arm_compute::CLReduceOperationKernel::operator= ( const CLReduceOperationKernel )
delete

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

◆ run()

void CLReduceOperationKernel::run ( const Window &  window,
cl::CommandQueue &  queue 
)
override

Definition at line 172 of file CLReduceOperationKernel.cpp.

173{
174 ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this);
175 ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(ICLKernel::window(), window);
176
177 const TensorShape &shape_in = _input->info()->tensor_shape();
178
179 unsigned int idx = 2 * num_arguments_per_4D_tensor(); // Skip the input and output parameters
180
181 _kernel.setArg<cl_int>(idx++, _axis);
182 _kernel.setArg<cl_int>(idx++, shape_in[_axis]);
183
184 // Support dimensions up to 4
185 Window slice_out = window.collapse(ICLKernel::window(), 2, 4);
186
187 // Setup input slice
188 Window slice_in(slice_out);
189 slice_in.set(Window::DimX, Window::Dimension(0, 0, 0));
190 slice_in.set(Window::DimY, Window::Dimension(0, 0, 0));
191 slice_in.set(Window::DimZ, Window::Dimension(0, 0, 0));
192 slice_in.set(3, Window::Dimension(0, 0, 0));
193
194 // Copy output's shape in order to use for recovering at end of this method
195 // TODO Remove changing and recovering output's shape if it is guaranteed that the axis positions
196 // of input and output are the same
197 const TensorShape shape_out = _output->info()->tensor_shape();
198 _output->info()->set_tensor_shape(inferOutputShape(shape_in, _axis));
199
200 idx = 0;
201 add_4D_tensor_argument(idx, _input, slice_in);
202 add_4D_tensor_argument(idx, _output, slice_out);
203 enqueue(queue, *this, slice_out, lws_hint());
204
205 // Recover output's shape of output tensor
206 _output->info()->set_tensor_shape(shape_out);
207}
::nncc::core::ADT::tensor::Shape TensorShape
Definition TensorShape.h:25

Referenced by package.infer.session::inference().

◆ validate()

Status CLReduceOperationKernel::validate ( const ITensorInfo *  input,
const ITensorInfo *  output,
const uint32_t  axis,
ReductionOperation  op 
)
static

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

Parameters
[in]inputSource tensor info. Data types supported: U8/S32/F32.
[in]outputDestination tensor info. Data types supported: Same as input. Output will have the same number of dimensions as input.
[in]axisAxis along which to reduce.
[in]opReduce operation to perform.
Returns
a status

Definition at line 163 of file CLReduceOperationKernel.cpp.

165{
166 ARM_COMPUTE_ERROR_ON_NULLPTR(input, output);
167 ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments(input, output, axis, op));
168
169 return Status{};
170}

Referenced by arm_compute::CLReduceOperation::validate(), and ~CLReduceOperationKernel().


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