Compute Library
 21.11
CLPriorBoxLayerKernel Class Reference

Interface for the PriorBox layer kernel. More...

#include <CLPriorBoxLayerKernel.h>

Collaboration diagram for CLPriorBoxLayerKernel:
[legend]

Public Member Functions

 CLPriorBoxLayerKernel ()
 Constructor. More...
 
 CLPriorBoxLayerKernel (const CLPriorBoxLayerKernel &)=delete
 Prevent instances of this class from being copied (As this class contains pointers) More...
 
CLPriorBoxLayerKerneloperator= (const CLPriorBoxLayerKernel &)=delete
 Prevent instances of this class from being copied (As this class contains pointers) More...
 
 CLPriorBoxLayerKernel (CLPriorBoxLayerKernel &&)=default
 Default Move Constructor. More...
 
CLPriorBoxLayerKerneloperator= (CLPriorBoxLayerKernel &&)=default
 Default move assignment operator. More...
 
 ~CLPriorBoxLayerKernel ()=default
 Default destructor. More...
 
void configure (const ICLTensor *input1, const ICLTensor *input2, ICLTensor *output, const PriorBoxLayerInfo &info, cl::Buffer *min, cl::Buffer *max, cl::Buffer *aspect_ratios)
 Set the input and output tensors. More...
 
void configure (const CLCompileContext &compile_context, const ICLTensor *input1, const ICLTensor *input2, ICLTensor *output, const PriorBoxLayerInfo &info, cl::Buffer *min, cl::Buffer *max, cl::Buffer *aspect_ratios)
 Set the input and output tensors. More...
 
void run (const Window &window, cl::CommandQueue &queue) override
 Enqueue the OpenCL kernel to process the given window on the passed OpenCL command queue. More...
 
- Public Member Functions inherited from ICLKernel
 ICLKernel ()
 Constructor. More...
 
cl::Kernel & kernel ()
 Returns a reference to the OpenCL kernel of this object. More...
 
CLKernelType type () const
 Returns the CL kernel type. More...
 
template<typename T >
void add_1D_array_argument (unsigned int &idx, const ICLArray< T > *array, const Strides &strides, unsigned int num_dimensions, const Window &window)
 Add the passed 1D array's parameters to the object's kernel's arguments starting from the index idx. More...
 
void add_1D_tensor_argument (unsigned int &idx, const ICLTensor *tensor, 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_1D_tensor_argument_if (bool cond, unsigned int &idx, const ICLTensor *tensor, const Window &window)
 Add the passed 1D tensor's parameters to the object's kernel's arguments starting from the index idx if the condition is true. More...
 
void add_2D_tensor_argument (unsigned int &idx, const ICLTensor *tensor, 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_2D_tensor_argument_if (bool cond, unsigned int &idx, const ICLTensor *tensor, const Window &window)
 Add the passed 2D tensor's parameters to the object's kernel's arguments starting from the index idx if the condition is true. More...
 
void add_3D_tensor_argument (unsigned int &idx, const ICLTensor *tensor, const Window &window)
 Add the passed 3D tensor's parameters to the object's kernel's arguments starting from the index idx. More...
 
void add_4D_tensor_argument (unsigned int &idx, const ICLTensor *tensor, const Window &window)
 Add the passed 4D tensor's parameters to the object's kernel's arguments starting from the index idx. More...
 
virtual void run_op (ITensorPack &tensors, const Window &window, cl::CommandQueue &queue)
 Enqueue the OpenCL kernel to process the given window on the passed OpenCL command queue. More...
 
template<typename T >
void add_argument (unsigned int &idx, T value)
 Add the passed parameters to the object's kernel's arguments starting from the index idx. More...
 
void set_lws_hint (const cl::NDRange &lws_hint)
 Set the Local-Workgroup-Size hint. More...
 
cl::NDRange lws_hint () const
 Return the Local-Workgroup-Size hint. More...
 
void set_wbsm_hint (const cl_int &wbsm_hint)
 Set the workgroup batch size modifier hint. More...
 
cl_int wbsm_hint () const
 Return the workgroup batch size modifier hint. More...
 
const std::string & config_id () const
 Get the configuration ID. More...
 
void set_target (GPUTarget target)
 Set the targeted GPU architecture. More...
 
void set_target (cl::Device &device)
 Set the targeted GPU architecture according to the CL device. More...
 
GPUTarget get_target () const
 Get the targeted GPU architecture. More...
 
size_t get_max_workgroup_size ()
 Get the maximum workgroup size for the device the CLKernelLibrary uses. More...
 
template<unsigned int dimension_size>
void add_tensor_argument (unsigned &idx, const ICLTensor *tensor, const Window &window)
 
template<typename T , unsigned int dimension_size>
void add_array_argument (unsigned &idx, const ICLArray< T > *array, const Strides &strides, unsigned int num_dimensions, const Window &window)
 Add the passed array's parameters to the object's kernel's arguments starting from the index idx. 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...
 
virtual BorderSize border_size () const
 The size of the border for that kernel. More...
 
const Windowwindow () const
 The maximum window the kernel can be executed on. More...
 
bool is_window_configured () const
 Function to check if the embedded window of this kernel has been configured. More...
 

Static Public Member Functions

static Status validate (const ITensorInfo *input1, const ITensorInfo *input2, const ITensorInfo *output, const PriorBoxLayerInfo &info)
 Static function to check if given info will lead to a valid configuration of CLPriorBoxLayerKernel. More...
 
- Static Public Member Functions inherited from ICLKernel
static constexpr unsigned int num_arguments_per_1D_array ()
 Returns the number of arguments enqueued per 1D array object. More...
 
static constexpr unsigned int num_arguments_per_1D_tensor ()
 Returns the number of arguments enqueued per 1D tensor object. More...
 
static constexpr unsigned int num_arguments_per_2D_tensor ()
 Returns the number of arguments enqueued per 2D tensor object. More...
 
static constexpr unsigned int num_arguments_per_3D_tensor ()
 Returns the number of arguments enqueued per 3D tensor object. More...
 
static constexpr unsigned int num_arguments_per_4D_tensor ()
 Returns the number of arguments enqueued per 4D tensor object. More...
 
static cl::NDRange gws_from_window (const Window &window)
 Get the global work size given an execution window. More...
 

Detailed Description

Interface for the PriorBox layer kernel.

Definition at line 34 of file CLPriorBoxLayerKernel.h.

Constructor & Destructor Documentation

◆ CLPriorBoxLayerKernel() [1/3]

Constructor.

Definition at line 99 of file CLPriorBoxLayerKernel.cpp.

100  : _input1(nullptr), _input2(nullptr), _output(nullptr), _info(), _num_priors(), _min(), _max(), _aspect_ratios()
101 {
103 }
Elementeise CL kernel type.
Definition: CLTypes.h:84

◆ CLPriorBoxLayerKernel() [2/3]

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

◆ CLPriorBoxLayerKernel() [3/3]

Default Move Constructor.

◆ ~CLPriorBoxLayerKernel()

~CLPriorBoxLayerKernel ( )
default

Default destructor.

Member Function Documentation

◆ configure() [1/2]

void configure ( const ICLTensor input1,
const ICLTensor input2,
ICLTensor output,
const PriorBoxLayerInfo info,
cl::Buffer *  min,
cl::Buffer *  max,
cl::Buffer *  aspect_ratios 
)

Set the input and output tensors.

Parameters
[in]input1First source tensor. Data types supported: F32. Data layouts supported: NCHW/NHWC.
[in]input2Second source tensor. Data types and layouts supported: same as input1
[out]outputDestination tensor. Output dimensions are [W * H * num_priors * 4, 2]. Data types and layouts supported: same as input1
[in]infoPrior box layer info.
[in]minMinimum prior box values
[in]maxMaximum prior box values
[in]aspect_ratiosAspect ratio values

Definition at line 105 of file CLPriorBoxLayerKernel.cpp.

References CLKernelLibrary::get().

106 {
107  configure(CLKernelLibrary::get().get_compile_context(), input1, input2, output, info, min, max, aspect_ratios);
108 }
void configure(const ICLTensor *input1, const ICLTensor *input2, ICLTensor *output, const PriorBoxLayerInfo &info, cl::Buffer *min, cl::Buffer *max, cl::Buffer *aspect_ratios)
Set the input and output tensors.
static CLKernelLibrary & get()
Access the KernelLibrary singleton.
ScaleKernelInfo info(interpolation_policy, default_border_mode, PixelValue(), sampling_policy, false)

◆ configure() [2/2]

void configure ( const CLCompileContext compile_context,
const ICLTensor input1,
const ICLTensor input2,
ICLTensor output,
const PriorBoxLayerInfo info,
cl::Buffer *  min,
cl::Buffer *  max,
cl::Buffer *  aspect_ratios 
)

Set the input and output tensors.

Parameters
[in]compile_contextThe compile context to be used.
[in]input1First source tensor. Data types supported: F32. Data layouts supported: NCHW/NHWC.
[in]input2Second source tensor. Data types and layouts supported: same as input1
[out]outputDestination tensor. Output dimensions are [W * H * num_priors * 4, 2]. Data types and layouts supported: same as input1
[in]infoPrior box layer info.
[in]minMinimum prior box values
[in]maxMaximum prior box values
[in]aspect_ratiosAspect ratio values

Definition at line 110 of file CLPriorBoxLayerKernel.cpp.

References CLBuildOptions::add_option(), CLBuildOptions::add_option_if(), ARM_COMPUTE_ERROR_ON_NULLPTR, ARM_COMPUTE_ERROR_THROW_ON, PriorBoxLayerInfo::aspect_ratios(), PriorBoxLayerInfo::clip(), arm_compute::create_kernel(), ITensorInfo::data_layout(), arm_compute::test::validation::data_layout, ITensorInfo::data_type(), ITensorInfo::dimension(), arm_compute::get_cl_type_from_data_type(), arm_compute::get_data_layout_dimension_index(), arm_compute::HEIGHT, PriorBoxLayerInfo::img_size(), ITensor::info(), arm_compute::test::validation::info, PriorBoxLayerInfo::max_sizes(), PriorBoxLayerInfo::min_sizes(), ICLKernel::num_arguments_per_2D_tensor(), PriorBoxLayerInfo::offset(), CLBuildOptions::options(), PriorBoxLayerInfo::steps(), arm_compute::support::cpp11::to_string(), PriorBoxLayerInfo::variances(), arm_compute::WIDTH, Coordinates2D::x, and Coordinates2D::y.

112 {
113  ARM_COMPUTE_ERROR_ON_NULLPTR(input1, input2, output);
114 
115  _input1 = input1;
116  _input2 = input2;
117  _output = output;
118  _info = info;
119  _min = min;
120  _max = max;
121  _aspect_ratios = aspect_ratios;
122 
123  ARM_COMPUTE_ERROR_THROW_ON(validate_arguments(input1->info(), input2->info(), output->info(), info));
124 
125  // Calculate number of aspect ratios
126  _num_priors = info.aspect_ratios().size() * info.min_sizes().size() + info.max_sizes().size();
127 
128  const DataLayout data_layout = input1->info()->data_layout();
129 
130  const int width_idx = get_data_layout_dimension_index(data_layout, DataLayoutDimension::WIDTH);
131  const int height_idx = get_data_layout_dimension_index(data_layout, DataLayoutDimension::HEIGHT);
132 
133  const int layer_width = input1->info()->dimension(width_idx);
134  const int layer_height = input1->info()->dimension(height_idx);
135 
136  int img_width = info.img_size().x;
137  int img_height = info.img_size().y;
138  if(img_width == 0 || img_height == 0)
139  {
140  img_width = input2->info()->dimension(width_idx);
141  img_height = input2->info()->dimension(height_idx);
142  }
143 
144  float step_x = info.steps()[0];
145  float step_y = info.steps()[0];
146  if(step_x == 0.f || step_y == 0.f)
147  {
148  step_x = static_cast<float>(img_width) / layer_width;
149  step_y = static_cast<float>(img_height) / layer_height;
150  }
151 
152  // Set build options
153  CLBuildOptions build_opts;
154  build_opts.add_option("-DDATA_TYPE=" + get_cl_type_from_data_type(input1->info()->data_type()));
155  build_opts.add_option("-DWIDTH=" + support::cpp11::to_string(img_width));
156  build_opts.add_option("-DHEIGHT=" + support::cpp11::to_string(img_height));
157  build_opts.add_option("-DLAYER_WIDTH=" + support::cpp11::to_string(layer_width));
158  build_opts.add_option("-DLAYER_HEIGHT=" + support::cpp11::to_string(layer_height));
159  build_opts.add_option("-DSTEP_X=" + support::cpp11::to_string(step_x));
160  build_opts.add_option("-DSTEP_Y=" + support::cpp11::to_string(step_y));
161  build_opts.add_option("-DNUM_PRIORS=" + support::cpp11::to_string(_num_priors));
162  build_opts.add_option("-DOFFSET=" + support::cpp11::to_string(info.offset()));
163  build_opts.add_option_if(info.clip(), "-DIN_PLACE");
164 
165  if(info.variances().size() > 1)
166  {
167  for(unsigned int i = 0; i < info.variances().size(); ++i)
168  {
169  build_opts.add_option("-DVARIANCE_" + support::cpp11::to_string(i) + "=" + support::cpp11::to_string(info.variances().at(i)));
170  }
171  }
172  else
173  {
174  for(unsigned int i = 0; i < 4; ++i)
175  {
176  build_opts.add_option("-DVARIANCE_" + support::cpp11::to_string(i) + "=" + support::cpp11::to_string(info.variances().at(0)));
177  }
178  }
179 
180  unsigned int idx = num_arguments_per_2D_tensor();
181  _kernel = create_kernel(compile_context, "prior_box_layer_nchw", build_opts.options());
182 
183  _kernel.setArg(idx++, *_min);
184  _kernel.setArg(idx++, *_max);
185  _kernel.setArg(idx++, *_aspect_ratios);
186  _kernel.setArg<unsigned int>(idx++, info.min_sizes().size());
187  _kernel.setArg<unsigned int>(idx++, info.max_sizes().size());
188  _kernel.setArg<unsigned int>(idx++, info.aspect_ratios().size());
189 
190  // Configure kernel window
191  auto win_config = validate_and_configure_window(input1->info(), input2->info(), output->info(), info, _num_priors);
192 
193  ARM_COMPUTE_ERROR_THROW_ON(win_config.first);
194  ICLKernel::configure_internal(win_config.second);
195 }
std::string to_string(T &&value)
Convert integer and float values to string.
#define ARM_COMPUTE_ERROR_THROW_ON(status)
Definition: Error.h:455
cl::Kernel create_kernel(const CLCompileContext &ctx, const std::string &kernel_name, const std::set< std::string > &build_opts=std::set< std::string >())
Creates an opencl kernel using a compile context.
Definition: CLHelpers.cpp:391
std::string get_cl_type_from_data_type(const DataType &dt)
Translates a tensor data type to the appropriate OpenCL type.
Definition: CLHelpers.cpp:39
static constexpr unsigned int num_arguments_per_2D_tensor()
Returns the number of arguments enqueued per 2D tensor object.
Definition: ICLKernel.h:248
ScaleKernelInfo info(interpolation_policy, default_border_mode, PixelValue(), sampling_policy, false)
size_t get_data_layout_dimension_index(const DataLayout &data_layout, const DataLayoutDimension &data_layout_dimension)
Get the index of the given dimension.
Definition: Helpers.inl:193
#define ARM_COMPUTE_ERROR_ON_NULLPTR(...)
Definition: Validate.h:157
DataLayout
[DataLayout enum definition]
Definition: Types.h:113

◆ operator=() [1/2]

CLPriorBoxLayerKernel& operator= ( const CLPriorBoxLayerKernel )
delete

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

◆ operator=() [2/2]

CLPriorBoxLayerKernel& operator= ( CLPriorBoxLayerKernel &&  )
default

Default move assignment operator.

◆ run()

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

Enqueue the OpenCL kernel to process the given window on the passed OpenCL command queue.

Note
The queue is not flushed by this method, and therefore the kernel will not have been executed by the time this method returns.
Parameters
[in]windowRegion on which to execute the kernel. (Must be a valid region of the window returned by window()).
[in,out]queueCommand queue on which to enqueue the kernel.

Reimplemented from ICLKernel.

Definition at line 208 of file CLPriorBoxLayerKernel.cpp.

References ICLKernel::add_2D_tensor_argument(), ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW, ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL, PriorBoxLayerInfo::aspect_ratios(), ITensorInfo::dimension(), Window::DimY, arm_compute::enqueue(), Window::first_slice_window_2D(), ITensor::info(), ICLKernel::lws_hint(), PriorBoxLayerInfo::max_sizes(), PriorBoxLayerInfo::min_sizes(), Window::set(), arm_compute::test::validation::reference::slice(), and IKernel::window().

209 {
212 
213  queue.enqueueWriteBuffer(*_min, CL_TRUE, 0, _info.min_sizes().size() * sizeof(float), _info.min_sizes().data());
214  queue.enqueueWriteBuffer(*_aspect_ratios, CL_TRUE, 0, _info.aspect_ratios().size() * sizeof(float), _info.aspect_ratios().data());
215  if(!_info.max_sizes().empty())
216  {
217  queue.enqueueWriteBuffer(*_max, CL_TRUE, 0, _info.max_sizes().size() * sizeof(float), _info.max_sizes().data());
218  }
219 
221  slice.set(Window::DimY, Window::Dimension(0, _output->info()->dimension(1), 2));
222 
223  unsigned int idx = 0;
224  add_2D_tensor_argument(idx, _output, slice);
225  enqueue(queue, *this, slice, lws_hint());
226 }
Window first_slice_window_2D() const
First 2D slice of the window.
Definition: Window.h:283
const Window & window() const
The maximum window the kernel can be executed on.
Definition: IKernel.cpp:28
virtual size_t dimension(size_t index) const =0
Return the size of the requested dimension.
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:32
cl::NDRange lws_hint() const
Return the Local-Workgroup-Size hint.
Definition: ICLKernel.h:318
std::vector< float > aspect_ratios() const
Get aspect ratios.
Definition: Types.h:903
virtual ITensorInfo * info() const =0
Interface to be implemented by the child class to return the tensor&#39;s metadata.
void set(size_t dimension, const Dimension &dim)
Set the values of a given dimension.
Definition: Window.inl:49
#define ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(k)
Definition: Validate.h:915
static constexpr size_t DimY
Alias for dimension 1 also known as Y dimension.
Definition: Window.h:45
void add_2D_tensor_argument(unsigned int &idx, const ICLTensor *tensor, const Window &window)
Add the passed 2D tensor&#39;s parameters to the object&#39;s kernel&#39;s arguments starting from the index idx...
Definition: ICLKernel.h:190
std::vector< float > max_sizes() const
Get max sizes.
Definition: Types.h:898
#define ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(f, s)
Definition: Validate.h:201
SimpleTensor< T > slice(const SimpleTensor< T > &src, Coordinates starts, Coordinates ends)
std::vector< float > min_sizes() const
Get min sizes.
Definition: Types.h:863

◆ validate()

Status validate ( const ITensorInfo input1,
const ITensorInfo input2,
const ITensorInfo output,
const PriorBoxLayerInfo info 
)
static

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

Parameters
[in]input1First source tensor info. Data types supported: F32. Data layouts supported: NCHW/NHWC.
[in]input2Second source tensor info. Data types and layouts supported: same as input1
[in]outputDestination tensor info. Output dimensions are [W * H * num_priors * 4, 2]. Data type supported: same as input1
[in]infoPrior box layer info.
Returns
a status

Definition at line 197 of file CLPriorBoxLayerKernel.cpp.

References ARM_COMPUTE_ERROR_ON_NULLPTR, ARM_COMPUTE_RETURN_ON_ERROR, PriorBoxLayerInfo::aspect_ratios(), ICloneable< T >::clone(), arm_compute::test::validation::info, PriorBoxLayerInfo::max_sizes(), and PriorBoxLayerInfo::min_sizes().

Referenced by CLPriorBoxLayer::validate().

198 {
199  ARM_COMPUTE_ERROR_ON_NULLPTR(input1, input2, output);
200  ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments(input1, input2, output, info));
201  const int num_priors = info.aspect_ratios().size() * info.min_sizes().size() + info.max_sizes().size();
202  ARM_COMPUTE_RETURN_ON_ERROR(validate_and_configure_window(input1->clone().get(), input2->clone().get(), output->clone().get(), info, num_priors)
203  .first);
204 
205  return Status{};
206 }
#define ARM_COMPUTE_RETURN_ON_ERROR(status)
Checks if a status contains an error and returns it.
Definition: Error.h:204
ScaleKernelInfo info(interpolation_policy, default_border_mode, PixelValue(), sampling_policy, false)
#define ARM_COMPUTE_ERROR_ON_NULLPTR(...)
Definition: Validate.h:157

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