Compute Library
 21.11
CLRemapKernel Class Reference

OpenCL kernel to perform a remap on a tensor. More...

#include <CLRemapKernel.h>

Collaboration diagram for CLRemapKernel:
[legend]

Public Member Functions

 CLRemapKernel ()
 Default constructor. More...
 
 CLRemapKernel (const CLRemapKernel &)=delete
 Prevent instances of this class from being copied (As this class contains pointers) More...
 
CLRemapKerneloperator= (const CLRemapKernel &)=delete
 Prevent instances of this class from being copied (As this class contains pointers) More...
 
 CLRemapKernel (CLRemapKernel &&)=default
 Allow instances of this class to be moved. More...
 
CLRemapKerneloperator= (CLRemapKernel &&)=default
 Allow instances of this class to be moved. More...
 
void configure (const CLCompileContext &compile_context, const ICLTensor *input, const ICLTensor *map_x, const ICLTensor *map_y, ICLTensor *output, RemapInfo info)
 Initialize the kernel's input, output and border mode. More...
 
template<class T >
void set_constant_border (unsigned int idx, const PixelValue &constant_border_value)
 Function to set the constant value on fill border kernel depending on type. 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...
 
BorderSize border_size () const override
 The size of the border for that kernel. 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...
 
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 *input, const ITensorInfo *map_x, const ITensorInfo *map_y, const ITensorInfo *output, RemapInfo info)
 Checks if the kernel's input, output and border mode will lead to a valid configuration of CLRemapKernel. 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

OpenCL kernel to perform a remap on a tensor.

Definition at line 36 of file CLRemapKernel.h.

Constructor & Destructor Documentation

◆ CLRemapKernel() [1/3]

Default constructor.

Definition at line 37 of file CLRemapKernel.cpp.

References arm_compute::ELEMENTWISE, and arm_compute::NCHW.

38  : _input(nullptr), _output(nullptr), _map_x(nullptr), _map_y(nullptr), _data_layout(DataLayout::NCHW)
39 {
41 }
Elementeise CL kernel type.
Definition: CLTypes.h:84
Num samples, channels, height, width.

◆ CLRemapKernel() [2/3]

CLRemapKernel ( const CLRemapKernel )
delete

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

◆ CLRemapKernel() [3/3]

CLRemapKernel ( CLRemapKernel &&  )
default

Allow instances of this class to be moved.

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 43 of file CLRemapKernel.cpp.

References arm_compute::NCHW.

Referenced by CLRemapKernel::configure().

44 {
45  return _data_layout == DataLayout::NCHW ? BorderSize(1) : BorderSize(0);
46 }
Num samples, channels, height, width.

◆ configure()

void configure ( const CLCompileContext compile_context,
const ICLTensor input,
const ICLTensor map_x,
const ICLTensor map_y,
ICLTensor output,
RemapInfo  info 
)

Initialize the kernel's input, output and border mode.

Parameters
[in]compile_contextThe compile context to be used.
[in]inputSource tensor. Data types supported: U8 (or F16 when layout is NHWC).
[in]map_xMap for X coordinates. Data types supported: F32.
[in]map_yMap for Y coordinates. Data types supported: F32.
[out]outputDestination tensor. Data types supported: Same as input. All but the lowest two dimensions must be the same size as in the input tensor, i.e. remapping is only performed within the XY-plane.
[in]infoRemapInfo struct:
  • policy Interpolation policy to use. Only NEAREST and BILINEAR are supported.
  • border_mode Border mode to use on the input tensor. Only CONSTANT and UNDEFINED are supported.
  • constant_border_value Constant value to use for borders if border_mode is set to CONSTANT.

Definition at line 75 of file CLRemapKernel.cpp.

References CLBuildOptions::add_option(), CLBuildOptions::add_option_if(), ARM_COMPUTE_ERROR, ARM_COMPUTE_ERROR_ON_NULLPTR, ARM_COMPUTE_ERROR_THROW_ON, RemapInfo::border_mode, CLRemapKernel::border_size(), BorderSize::bottom, arm_compute::calculate_max_window(), arm_compute::ceil_to_multiple(), arm_compute::CONSTANT, RemapInfo::constant_border_value, arm_compute::create_kernel(), ITensorInfo::data_layout(), ITensorInfo::data_type(), ITensorInfo::dimension(), arm_compute::F16, arm_compute::get_cl_type_from_data_type(), arm_compute::get_data_layout_dimension_index(), arm_compute::HEIGHT, arm_compute::test::validation::idx_height, arm_compute::test::validation::idx_width, ITensor::info(), arm_compute::test::validation::info, arm_compute::test::validation::input, input_height, input_width, kernel_name, BorderSize::left, arm_compute::lower_string(), arm_compute::NCHW, arm_compute::NHWC, ICLKernel::num_arguments_per_2D_tensor(), ICLKernel::num_arguments_per_4D_tensor(), num_elems_processed_per_iteration, CLBuildOptions::options(), RemapInfo::policy, BorderSize::right, arm_compute::string_from_data_layout(), arm_compute::string_from_interpolation_policy(), arm_compute::support::cpp11::to_string(), BorderSize::top, arm_compute::U8, arm_compute::update_window_and_padding(), CLRemapKernel::validate(), and arm_compute::WIDTH.

76 {
77  ARM_COMPUTE_ERROR_ON_NULLPTR(input, map_x, map_y, output);
78  ARM_COMPUTE_ERROR_THROW_ON(CLRemapKernel::validate(input->info(), map_x->info(), map_y->info(), output->info(), info));
79 
80  _input = input;
81  _output = output;
82  _map_x = map_x;
83  _map_y = map_y;
84  _data_layout = input->info()->data_layout();
85 
86  const bool is_nhwc = _data_layout == DataLayout::NHWC;
87  const bool is_constant_border = info.border_mode == BorderMode::CONSTANT;
88 
89  // Create kernel
90  CLBuildOptions build_opts;
91  build_opts.add_option(("-DDATA_TYPE=" + get_cl_type_from_data_type(input->info()->data_type())));
92  build_opts.add_option_if(is_nhwc, "-DDEPTH_OUT=" + support::cpp11::to_string(output->info()->dimension(2)));
93  build_opts.add_option_if(is_constant_border, "-DCONSTANT_BORDER");
94 
95  const std::string interpolation_name = lower_string(string_from_interpolation_policy(info.policy));
96  const std::string kernel_name = "remap_" + interpolation_name + "_" + lower_string(string_from_data_layout(_data_layout));
97  _kernel = create_kernel(compile_context, kernel_name, build_opts.options());
98 
99  const unsigned int num_elems_processed_per_iteration = is_nhwc ? 1 : 4;
102  const int input_height = input->info()->dimension(idx_height);
103  const int input_width = input->info()->dimension(idx_width);
104 
105  // Configure window
106  Window win = calculate_max_window(*_output->info(), Steps(num_elems_processed_per_iteration));
107 
108  // Update padding in NCHW case
109  if(_data_layout == DataLayout::NCHW)
110  {
111  const int total_right = ceil_to_multiple(input_width, num_elems_processed_per_iteration);
112  const int access_right = total_right + (((total_right - input_width) == 0) ? border_size().right : 0);
113  AccessWindowStatic input_access(input->info(), -border_size().left, -border_size().top, access_right, input_height + border_size().bottom);
114 
115  AccessWindowHorizontal output_access(output->info(), 0, num_elems_processed_per_iteration);
116 
117  update_window_and_padding(win, input_access, output_access);
118  }
119 
120  ICLKernel::configure_internal(win);
121 
122  // Set static arguments
123  unsigned int idx = 4 * (is_nhwc ? num_arguments_per_4D_tensor() : num_arguments_per_2D_tensor());
124  _kernel.setArg<cl_float>(idx++, input_width);
125  _kernel.setArg<cl_float>(idx++, input_height);
126  if(is_nhwc && is_constant_border)
127  {
128  switch(input->info()->data_type())
129  {
130  case DataType::U8:
131  set_constant_border<uint8_t>(idx, info.constant_border_value);
132  break;
133  case DataType::F16:
134  static_assert(sizeof(cl_half) == sizeof(half), "Half must be same size as cl_half");
135  static_assert(sizeof(cl_half) == 2, "Half must be 16 bit");
136  set_constant_border<half>(idx, info.constant_border_value);
137  break;
138  default:
139  ARM_COMPUTE_ERROR("Data Type not handled");
140  }
141  }
142 }
BorderMode border_mode
Border mode policy.
unsigned int top
top of the border
Definition: Types.h:377
Window calculate_max_window(const ValidRegion &valid_region, const Steps &steps, bool skip_border, BorderSize border_size)
#define ARM_COMPUTE_ERROR(msg)
Print the given message then throw an std::runtime_error.
Definition: Error.h:352
static Status validate(const ITensorInfo *input, const ITensorInfo *map_x, const ITensorInfo *map_y, const ITensorInfo *output, RemapInfo info)
Checks if the kernel&#39;s input, output and border mode will lead to a valid configuration of CLRemapKer...
1 channel, 1 U8 per channel
std::string to_string(T &&value)
Convert integer and float values to string.
half_float::half half
16-bit floating point type
Definition: Types.h:48
#define ARM_COMPUTE_ERROR_THROW_ON(status)
Definition: Error.h:455
unsigned int bottom
bottom of the border
Definition: Types.h:379
std::string lower_string(const std::string &val)
Lower a given string.
Definition: Utils.cpp:326
1 channel, 1 F16 per channel
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
const size_t input_width
bool update_window_and_padding(Window &win, Ts &&... patterns)
Update window and padding size for each of the access patterns.
Definition: WindowHelpers.h:46
auto ceil_to_multiple(S value, T divisor) -> decltype(((value+divisor - 1)/divisor) *divisor)
Computes the smallest number larger or equal to value that is a multiple of divisor.
Definition: Utils.h:71
const std::string & string_from_interpolation_policy(InterpolationPolicy policy)
Translates a given interpolation policy to a string.
Definition: Utils.cpp:187
unsigned int num_elems_processed_per_iteration
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
virtual ITensorInfo * info() const =0
Interface to be implemented by the child class to return the tensor&#39;s metadata.
static constexpr unsigned int num_arguments_per_2D_tensor()
Returns the number of arguments enqueued per 2D tensor object.
Definition: ICLKernel.h:248
static constexpr unsigned int num_arguments_per_4D_tensor()
Returns the number of arguments enqueued per 4D tensor object.
Definition: ICLKernel.h:264
unsigned int left
left of the border
Definition: Types.h:380
unsigned int right
right of the border
Definition: Types.h:378
Num samples, channels, height, width.
ScaleKernelInfo info(interpolation_policy, default_border_mode, PixelValue(), sampling_policy, false)
const std::string & string_from_data_layout(DataLayout dl)
Convert a data layout identity into a string.
Definition: Utils.cpp:123
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
Num samples, height, width, channels.
const size_t input_height
#define ARM_COMPUTE_ERROR_ON_NULLPTR(...)
Definition: Validate.h:157
PixelValue constant_border_value
Constant value to use for constant border mode policy.
std::string kernel_name
BorderSize border_size() const override
The size of the border for that kernel.

◆ operator=() [1/2]

CLRemapKernel& operator= ( const CLRemapKernel )
delete

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

◆ operator=() [2/2]

CLRemapKernel& operator= ( CLRemapKernel &&  )
default

Allow instances of this class to be moved.

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

References ICLKernel::add_2D_tensor_argument(), ICLKernel::add_4D_tensor_argument(), ARM_COMPUTE_ERROR, ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW, ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL, Window::collapse(), Window::DimZ, arm_compute::enqueue(), Window::first_slice_window_2D(), Window::first_slice_window_4D(), ICLKernel::lws_hint(), arm_compute::NCHW, arm_compute::NHWC, arm_compute::test::validation::reference::slice(), Window::slide_window_slice_2D(), and IKernel::window().

145 {
148  switch(_data_layout)
149  {
150  case DataLayout::NCHW:
151  {
153  do
154  {
155  unsigned int idx = 0;
156  add_2D_tensor_argument(idx, _input, slice);
157  add_2D_tensor_argument(idx, _output, slice);
158  add_2D_tensor_argument(idx, _map_x, slice);
159  add_2D_tensor_argument(idx, _map_y, slice);
160  enqueue(queue, *this, slice, lws_hint());
161 
162  }
163  while(window.slide_window_slice_2D(slice));
164  break;
165  }
166  case DataLayout::NHWC:
167  {
168  Window collapsed = window.collapse(ICLKernel::window(), Window::DimZ);
169  Window slice = collapsed.first_slice_window_4D();
170 
171  unsigned int idx = 0;
172  add_4D_tensor_argument(idx, _input, slice);
173  add_4D_tensor_argument(idx, _output, slice);
174  add_4D_tensor_argument(idx, _map_x, slice);
175  add_4D_tensor_argument(idx, _map_y, slice);
176  enqueue(queue, *this, slice, lws_hint());
177  break;
178  }
179  default:
180  ARM_COMPUTE_ERROR("Invalid Data layout");
181  }
182 }
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
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
#define ARM_COMPUTE_ERROR(msg)
Print the given message then throw an std::runtime_error.
Definition: Error.h:352
cl::NDRange lws_hint() const
Return the Local-Workgroup-Size hint.
Definition: ICLKernel.h:318
bool slide_window_slice_2D(Window &slice) const
Slide the passed 2D window slice.
Definition: Window.h:323
Window collapse(const Window &full_window, size_t first, size_t last=Coordinates::num_max_dimensions) const
Collapse the dimensions between first and last.
Definition: Window.inl:111
#define ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(k)
Definition: Validate.h:915
Num samples, channels, height, width.
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
static constexpr size_t DimZ
Alias for dimension 2 also known as Z dimension.
Definition: Window.h:47
Num samples, height, width, channels.
Window first_slice_window_4D() const
First 4D slice of the window.
Definition: Window.h:299
void add_4D_tensor_argument(unsigned int &idx, const ICLTensor *tensor, const Window &window)
Add the passed 4D tensor&#39;s parameters to the object&#39;s kernel&#39;s arguments starting from the index idx...
Definition: ICLKernel.h:224
#define ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(f, s)
Definition: Validate.h:201
SimpleTensor< T > slice(const SimpleTensor< T > &src, Coordinates starts, Coordinates ends)

◆ set_constant_border()

void set_constant_border ( unsigned int  idx,
const PixelValue constant_border_value 
)

Function to set the constant value on fill border kernel depending on type.

Parameters
[in]idxIndex of the kernel argument to set.
[in]constant_border_valueConstant value to use for borders if border_mode is set to CONSTANT.

Definition at line 49 of file CLRemapKernel.cpp.

References PixelValue::get().

50 {
51  T value;
52  constant_border_value.get(value);
53  ICLKernel::add_argument<T>(idx, static_cast<T>(value));
54 }

◆ validate()

Status validate ( const ITensorInfo input,
const ITensorInfo map_x,
const ITensorInfo map_y,
const ITensorInfo output,
RemapInfo  info 
)
static

Checks if the kernel's input, output and border mode will lead to a valid configuration of CLRemapKernel.

Similar to CLRemapKernel::configure(const CLCompileContext &compile_context, const ICLTensor *input, const ICLTensor *map_x, const ICLTensor *map_y, ICLTensor *output, RemapInfo info)

Definition at line 56 of file CLRemapKernel.cpp.

References arm_compute::AREA, ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN, ARM_COMPUTE_RETURN_ERROR_ON_MSG, ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR, RemapInfo::border_mode, arm_compute::CONSTANT, ITensorInfo::data_layout(), ITensorInfo::data_type(), arm_compute::F16, arm_compute::F32, arm_compute::NCHW, RemapInfo::policy, arm_compute::U8, and arm_compute::UNDEFINED.

Referenced by CLRemapKernel::configure(), and CLRemap::validate().

57 {
58  ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(input, map_x, map_y, output);
59  if(input->data_layout() == DataLayout::NCHW)
60  {
62  }
63  else
64  {
66  }
67  ARM_COMPUTE_RETURN_ERROR_ON_MSG(input->data_type() != output->data_type(), "Input/output have different data types");
70  ARM_COMPUTE_RETURN_ERROR_ON_MSG(info.policy == InterpolationPolicy::AREA, "Area interpolation is not supported!");
72  return Status{};
73 }
BorderMode border_mode
Border mode policy.
1 channel, 1 U8 per channel
1 channel, 1 F32 per channel
1 channel, 1 F16 per channel
#define ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(...)
Definition: Validate.h:159
Output values are determined by averaging the source pixels whose areas fall under the area of the de...
Num samples, channels, height, width.
ScaleKernelInfo info(interpolation_policy, default_border_mode, PixelValue(), sampling_policy, false)
Borders are left undefined.
#define ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(t, c,...)
Definition: Validate.h:788
#define ARM_COMPUTE_RETURN_ERROR_ON_MSG(cond, msg)
If the condition is true, an error is returned.
Definition: Error.h:244

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