Compute Library
 19.08
GCPoolingLayerKernel Class Reference

Interface for the pooling layer kernel. More...

#include <GCPoolingLayerKernel.h>

Collaboration diagram for GCPoolingLayerKernel:
[legend]

Public Member Functions

 GCPoolingLayerKernel ()
 Default constructor. More...
 
 GCPoolingLayerKernel (const GCPoolingLayerKernel &)=delete
 Prevent instances of this class from being copied (As this class contains pointers) More...
 
GCPoolingLayerKerneloperator= (const GCPoolingLayerKernel &)=delete
 Prevent instances of this class from being copied (As this class contains pointers) More...
 
 GCPoolingLayerKernel (GCPoolingLayerKernel &&)=default
 Allow instances of this class to be moved. More...
 
GCPoolingLayerKerneloperator= (GCPoolingLayerKernel &&)=default
 Allow instances of this class to be moved. More...
 
 ~GCPoolingLayerKernel ()=default
 Default destructor. More...
 
void configure (const IGCTensor *input, IGCTensor *output, const PoolingLayerInfo &pool_info)
 Set the input and output tensors. More...
 
void run (const Window &window) override
 Enqueue the OpenGL ES shader to process the given window. More...
 
BorderSize border_size () const override
 The size of the border for that kernel. More...
 
- Public Member Functions inherited from IGCKernel
 IGCKernel ()
 Constructor. More...
 
GCKernelkernel ()
 Returns a reference to the GLES kernel of this object. More...
 
void add_1D_tensor_argument (unsigned int &idx, const IGCTensor *tensor, const unsigned int binding_point, const Window &window)
 Add the passed 1D tensor's parameters to the object's kernel's arguments starting from the index idx. More...
 
void add_2D_tensor_argument (unsigned int &idx, const IGCTensor *tensor, const unsigned int binding_point, const Window &window)
 Add the passed 2D tensor's parameters to the object's kernel's arguments starting from the index idx. More...
 
void add_3D_tensor_argument (unsigned int &idx, const IGCTensor *tensor, const unsigned int binding_point, const Window &window)
 Add the passed 3D tensor's parameters to the object's kernel's arguments starting from the index idx. More...
 
unsigned int num_arguments_per_1D_tensor () const
 Returns the number of arguments enqueued per 1D tensor object. More...
 
unsigned int num_arguments_per_2D_tensor () const
 Returns the number of arguments enqueued per 2D tensor object. More...
 
unsigned int num_arguments_per_3D_tensor () const
 Returns the number of arguments enqueued per 3D tensor object. More...
 
void set_lws_hint (gles::NDRange &lws_hint)
 Set the Local-Workgroup-Size hint. More...
 
void set_target (GPUTarget target)
 Set the targeted GPU architecture. More...
 
GPUTarget get_target () const
 Get the targeted GPU architecture. More...
 
- Public Member Functions inherited from IKernel
 IKernel ()
 Constructor. More...
 
virtual ~IKernel ()=default
 Destructor. More...
 
virtual bool is_parallelisable () const
 Indicates whether or not the kernel is parallelisable. More...
 
const Windowwindow () const
 The maximum window the kernel can be executed on. More...
 

Static Public Member Functions

static Status validate (const ITensorInfo *input, const ITensorInfo *output, const PoolingLayerInfo &pool_info)
 Static function to check if given info will lead to a valid configuration of GCPoolingLayerKernel. More...
 

Detailed Description

Interface for the pooling layer kernel.

Definition at line 36 of file GCPoolingLayerKernel.h.

Constructor & Destructor Documentation

◆ GCPoolingLayerKernel() [1/3]

Default constructor.

Definition at line 216 of file GCPoolingLayerKernel.cpp.

217  : _input(nullptr), _output(nullptr), _pool_info(), _border_size(0), _num_elems_processed_per_iteration(1)
218 {
219 }

◆ GCPoolingLayerKernel() [2/3]

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

◆ GCPoolingLayerKernel() [3/3]

Allow instances of this class to be moved.

◆ ~GCPoolingLayerKernel()

~GCPoolingLayerKernel ( )
default

Default destructor.

Member Function Documentation

◆ border_size()

BorderSize border_size ( ) const
overridevirtual

The size of the border for that kernel.

Returns
The width in number of elements of the border.

Reimplemented from IKernel.

Definition at line 221 of file GCPoolingLayerKernel.cpp.

222 {
223  return _border_size;
224 }

◆ configure()

void configure ( const IGCTensor input,
IGCTensor output,
const PoolingLayerInfo pool_info 
)

Set the input and output tensors.

Parameters
[in]inputSource tensor. Data types supported: F16/F32.
[out]outputDestination tensor. Data types supported: Same as input.
[in]pool_infoContains pooling operation information described in PoolingLayerInfo.

Definition at line 226 of file GCPoolingLayerKernel.cpp.

227 {
228  int pool_pad_x = 0;
229  int pool_pad_y = 0;
230  int pool_stride_x = 0;
231  int pool_stride_y = 0;
232  unsigned int pooled_w = 0;
233  unsigned int pooled_h = 0;
234  const PoolingType pool_type = pool_info.pool_type();
235  int pool_size = pool_info.pool_size().width;
236  const PadStrideInfo pad_stride_info = pool_info.pad_stride_info();
237  const bool exclude_padding = pool_info.exclude_padding();
238  std::tie(pool_pad_x, pool_pad_y) = pad_stride_info.pad();
239  std::tie(pool_stride_x, pool_stride_y) = pad_stride_info.stride();
240 
241  ARM_COMPUTE_ERROR_ON_NULLPTR(input, output);
242 
243  // Update pool size in case of global pooling
244  pool_size = pool_info.is_global_pooling() ? input->info()->dimension(0) : pool_size;
245 
246  // Check output dimensions
247  std::tie(pooled_w, pooled_h) = scaled_dimensions(input->info()->dimension(0),
248  input->info()->dimension(1),
249  pool_size,
250  pool_size,
251  pad_stride_info);
252 
253  auto_init(input->info(), output->info(), pooled_w, pooled_h);
254 
255  ARM_COMPUTE_ERROR_THROW_ON(validate_arguments(input->info(), output->info(), pool_info));
256 
257  // Set instance variables
258  _input = input;
259  _output = output;
260  _pool_info = pool_info;
261 
262  // Set build options
263  std::set<std::string> build_opts;
264  build_opts.emplace("#define LOCAL_SIZE_X " + support::cpp11::to_string(1));
265  build_opts.emplace("#define LOCAL_SIZE_Y " + support::cpp11::to_string(1));
266  build_opts.emplace("#define LOCAL_SIZE_Z " + support::cpp11::to_string(1));
267  if(input->info()->data_type() == DataType::F32)
268  {
269  build_opts.insert("#define DATA_TYPE_FP32");
270  }
271  else
272  {
273  build_opts.insert("#define DATA_TYPE_FP16");
274  }
275  if(exclude_padding)
276  {
277  build_opts.emplace("#define EXCLUDE_PADDING");
278  }
279  build_opts.emplace(("#define POOL_" + string_from_pooling_type(pool_type)));
280  build_opts.emplace(("#define STRIDE_X " + support::cpp11::to_string(pool_stride_x)));
281  build_opts.emplace(("#define MAX_WIDTH " + support::cpp11::to_string(input->info()->dimension(0) + (exclude_padding ? 0 : pool_pad_x))));
282  build_opts.emplace(("#define MAX_HEIGHT " + support::cpp11::to_string(input->info()->dimension(1) + (exclude_padding ? 0 : pool_pad_y))));
283  build_opts.emplace(("#define STRIDE_Y " + support::cpp11::to_string(pool_stride_y)));
284  build_opts.emplace(("#define PAD_X " + support::cpp11::to_string(pool_pad_x)));
285  build_opts.emplace(("#define PAD_Y " + support::cpp11::to_string(pool_pad_y)));
286 
287  // Create kernel
288  if((pool_size == 2) || (pool_size == 3) || (pool_size == 7))
289  {
290  // Check if we have pool3x3 with stride_x less equal than 3. In these cases, run an optimized OpenGLES kernel where
291  // each thread computes 4 output elements
292  const bool is_pool3x3_stride_le3 = (pool_size == 3) && (pool_stride_x <= 3);
293 
294  std::string kernel_name = "pooling_layer_" + support::cpp11::to_string(pool_size);
295  if(is_pool3x3_stride_le3)
296  {
297  build_opts.insert("#define POOLING_LAYER_3_OPTIMIZED");
298  _kernel = static_cast<GCKernel>(GCKernelLibrary::get().create_kernel(kernel_name + "_optimized", build_opts));
299  }
300  else
301  {
302  build_opts.insert("#define POOLING_LAYER_" + support::cpp11::to_string(pool_size));
303  _kernel = static_cast<GCKernel>(GCKernelLibrary::get().create_kernel(kernel_name, build_opts));
304  }
305  }
306  else // Run general case
307  {
308  build_opts.emplace(("#define POOL_SIZE " + support::cpp11::to_string(pool_size)));
309 
310  build_opts.insert("#define POOLING_LAYER_N");
311  _kernel = static_cast<GCKernel>(GCKernelLibrary::get().create_kernel("pooling_layer_n", build_opts));
312  }
313  // Configure kernel window
314  auto win_config = validate_and_configure_window(input->info(), output->info(), pool_info);
315  ARM_COMPUTE_ERROR_THROW_ON(std::get<0>(win_config));
316 
317  IGCKernel::configure(std::get<1>(win_config));
318  GCPoolingConfig pooling_config = std::get<2>(win_config);
319  _num_elems_processed_per_iteration = pooling_config.first;
320  _border_size = pooling_config.second;
321 }
bool exclude_padding() const
Check if padding is excluded in calculations.
Definition: Types.h:1257
virtual size_t dimension(size_t index) const =0
Return the size of the requested dimension.
std::pair< unsigned int, unsigned int > scaled_dimensions(unsigned int width, unsigned int height, unsigned int kernel_width, unsigned int kernel_height, const PadStrideInfo &pad_stride_info, const Size2D &dilation=Size2D(1U, 1U))
Returns expected width and height of output scaled tensor depending on dimensions rounding mode.
Definition: Utils.cpp:387
std::pair< Status, Window > validate_and_configure_window(ITensorInfo *input, ITensorInfo *weights, ITensorInfo *biases, ITensorInfo *output, const PadStrideInfo &conv_info, unsigned int depth_multiplier, const Size2D &dilation)
std::string to_string(T &&value)
Convert integer and float values to string.
virtual DataType data_type() const =0
Data type used for each element of the tensor.
1 channel, 1 F32 per channel
#define ARM_COMPUTE_ERROR_THROW_ON(status)
Definition: Error.h:327
std::pair< unsigned int, unsigned int > stride() const
Get the stride.
Definition: Types.h:724
virtual ITensorInfo * info() const =0
Interface to be implemented by the child class to return the tensor's metadata.
Padding and stride information class.
Definition: Types.h:676
std::unique_ptr< Kernel > create_kernel()
Helper function to create and return a unique_ptr pointed to a CL/GLES kernel object.
Definition: Helpers.h:86
PadStrideInfo pad_stride_info() const
Get the padding and stride.
Definition: Types.h:1252
const Size2D & pool_size() const
Get the pooling size.
Definition: Types.h:1247
static GCKernelLibrary & get()
Get the static instance of GCKernelLibrary.
PoolingType
Available pooling types.
Definition: Types.h:564
#define ARM_COMPUTE_ERROR_ON_NULLPTR(...)
Definition: Validate.h:161
size_t width
Width of the image region or rectangle.
Definition: Size2D.h:92
PoolingType pool_type() const
Get the pooling type.
Definition: Types.h:1242
std::pair< unsigned int, unsigned int > pad() const
Get the padding.
Definition: Types.h:742
const std::string & string_from_pooling_type(PoolingType type)
Translates a given pooling type to a string.
Definition: Utils.cpp:252
bool is_global_pooling() const
Check if is global pooling.
Definition: Types.h:1262

References ARM_COMPUTE_ERROR_ON_NULLPTR, ARM_COMPUTE_ERROR_THROW_ON, arm_compute::create_kernel(), ITensorInfo::data_type(), ITensorInfo::dimension(), PoolingLayerInfo::exclude_padding(), arm_compute::F32, GCKernelLibrary::get(), ITensor::info(), PoolingLayerInfo::is_global_pooling(), PadStrideInfo::pad(), PoolingLayerInfo::pad_stride_info(), PoolingLayerInfo::pool_size(), PoolingLayerInfo::pool_type(), arm_compute::scaled_dimensions(), PadStrideInfo::stride(), arm_compute::string_from_pooling_type(), arm_compute::support::cpp11::to_string(), arm_compute::validate_and_configure_window(), and Size2D::width.

◆ operator=() [1/2]

GCPoolingLayerKernel& operator= ( const GCPoolingLayerKernel )
delete

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

◆ operator=() [2/2]

GCPoolingLayerKernel& operator= ( GCPoolingLayerKernel &&  )
default

Allow instances of this class to be moved.

◆ run()

void run ( const Window window)
overridevirtual

Enqueue the OpenGL ES shader to process the given window.

Parameters
[in]windowRegion on which to execute the kernel. (Must be a valid region of the window returned by window()).

Implements IGCKernel.

Definition at line 331 of file GCPoolingLayerKernel.cpp.

332 {
335 
336  unsigned int pool_pad_x;
337  unsigned int pool_pad_y;
338  unsigned int pool_stride_x;
339  unsigned int pool_stride_y;
340  std::tie(pool_pad_x, pool_pad_y) = _pool_info.pad_stride_info().pad();
341  std::tie(pool_stride_x, pool_stride_y) = _pool_info.pad_stride_info().stride();
342 
343  _kernel.use();
344 
345  _output->set_needs_shifting(true);
346 
348 
349  Window slice = window_collapsed.first_slice_window_3D();
350  Window slice_in_orig = window_collapsed.first_slice_window_3D();
351 
352  slice.shift(Window::DimX, -(_output->info()->padding()).left);
353 
354  do
355  {
356  // Upsample input by pool size
357  Window in_slice(slice_in_orig); // NOLINT
358  in_slice.set(Window::DimX, Window::Dimension(in_slice.x().start() - pool_pad_x, in_slice.x().end() * pool_stride_x, pool_stride_x * _num_elems_processed_per_iteration));
359  in_slice.set(Window::DimY, Window::Dimension(in_slice.y().start() - pool_pad_y, in_slice.y().end() * pool_stride_y, pool_stride_y));
360 
361  // Set inputs
362  unsigned int idx = 0;
363  add_3D_tensor_argument(idx, _input, 1, in_slice);
364  add_3D_tensor_argument(idx, _output, 2, slice);
365 
366  _kernel.update_shader_params();
367  enqueue(*this, slice);
368  }
369  while(window_collapsed.slide_window_slice_3D(slice) && window_collapsed.slide_window_slice_3D(slice_in_orig));
370 }
const Window & window() const
The maximum window the kernel can be executed on.
Definition: IKernel.cpp:28
void add_3D_tensor_argument(unsigned int &idx, const IGCTensor *tensor, const unsigned int binding_point, const Window &window)
Add the passed 3D tensor's parameters to the object's kernel's arguments starting from the index idx.
Definition: IGCKernel.cpp:132
void enqueue(cl::CommandQueue &queue, ICLKernel &kernel, const Window &window, const cl::NDRange &lws_hint=CLKernelLibrary::get().default_ndrange(), bool use_dummy_work_items=false)
Add the kernel to the command queue with the given window.
Definition: ICLKernel.cpp:39
Describe one of the image's dimensions with a start, end and step.
Definition: Window.h:75
static constexpr size_t DimX
Alias for dimension 0 also known as X dimension.
Definition: Window.h:43
Window collapse_if_possible(const Window &full_window, size_t first, size_t last, bool *has_collapsed=nullptr) const
Collapse the dimensions between first and last if possible.
Definition: Window.inl:54
void set_needs_shifting(bool needs_shifting)
Set the flag indicating whether or not a tensor needs shifting.
Definition: IGCTensor.cpp:61
std::pair< unsigned int, unsigned int > stride() const
Get the stride.
Definition: Types.h:724
virtual ITensorInfo * info() const =0
Interface to be implemented by the child class to return the tensor's metadata.
virtual PaddingSize padding() const =0
Padding of tensor.
bool slide_window_slice_3D(Window &slice) const
Slide the passed 3D window slice.
Definition: Window.h:319
PadStrideInfo pad_stride_info() const
Get the padding and stride.
Definition: Types.h:1252
static constexpr size_t DimY
Alias for dimension 1 also known as Y dimension.
Definition: Window.h:45
static constexpr size_t DimZ
Alias for dimension 2 also known as Z dimension.
Definition: Window.h:47
std::pair< unsigned int, unsigned int > pad() const
Get the padding.
Definition: Types.h:742
Window first_slice_window_3D() const
First 3D slice of the window.
Definition: Window.h:275
#define ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(f, s)
Definition: Validate.h:205
Describe a multidimensional execution window.
Definition: Window.h:39
#define ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(k)
Definition: Validate.h:940
SimpleTensor< T > slice(const SimpleTensor< T > &src, Coordinates starts, Coordinates ends)

References IGCKernel::add_3D_tensor_argument(), ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW, ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL, Window::collapse_if_possible(), Window::DimX, Window::DimY, Window::DimZ, Window::Dimension::end(), arm_compute::enqueue(), Window::first_slice_window_3D(), ITensor::info(), PadStrideInfo::pad(), PoolingLayerInfo::pad_stride_info(), ITensorInfo::padding(), Window::set(), IGCTensor::set_needs_shifting(), arm_compute::test::validation::reference::slice(), Window::slide_window_slice_3D(), Window::Dimension::start(), PadStrideInfo::stride(), IKernel::window(), Window::x(), and Window::y().

◆ validate()

Status validate ( const ITensorInfo input,
const ITensorInfo output,
const PoolingLayerInfo pool_info 
)
static

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

Parameters
[in]inputSource tensor info. Data types supported: F16/F32.
[in]outputDestination tensor info. Data types supported: Same as input.
[in]pool_infoContains pooling operation information described in PoolingLayerInfo.
Returns
a status

Definition at line 323 of file GCPoolingLayerKernel.cpp.

324 {
325  ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments(input, output, pool_info));
326  ARM_COMPUTE_RETURN_ON_ERROR(std::get<0>(validate_and_configure_window(input->clone().get(), output->clone().get(), pool_info)));
327 
328  return Status{};
329 }
std::pair< Status, Window > validate_and_configure_window(ITensorInfo *input, ITensorInfo *weights, ITensorInfo *biases, ITensorInfo *output, const PadStrideInfo &conv_info, unsigned int depth_multiplier, const Size2D &dilation)
#define ARM_COMPUTE_RETURN_ON_ERROR(status)
Checks if a status contains an error and returns it.
Definition: Error.h:193
Status class.
Definition: Error.h:52
virtual std::unique_ptr< T > clone() const =0
Provide a clone of the current object of class T.

References ARM_COMPUTE_RETURN_ON_ERROR, ICloneable< T >::clone(), and arm_compute::validate_and_configure_window().

Referenced by GCPoolingLayer::validate().


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