Compute Library
 19.08
CLCropKernel Class Reference

OpenCL kernel to perform a copy between two tensors. More...

#include <CLCropKernel.h>

Collaboration diagram for CLCropKernel:
[legend]

Public Member Functions

 CLCropKernel ()
 Default constructor. More...
 
 CLCropKernel (const CLCropKernel &)=delete
 Prevent instances of this class from being copied (As this class contains pointers). More...
 
CLCropKerneloperator= (const CLCropKernel &)=delete
 Prevent instances of this class from being copied (As this class contains pointers). More...
 
 CLCropKernel (CLCropKernel &&)=default
 Allow instances of this class to be moved. More...
 
CLCropKerneloperator= (CLCropKernel &&)=default
 Allow instances of this class to be moved. More...
 
void configure (const ICLTensor *input, ICLTensor *output, Coordinates2D start, Coordinates2D end, uint32_t batch_index, float extrapolation_value=0, Window *output_window=nullptr)
 Configure kernel. 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...
 
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...
 
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...
 
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<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...
 
template<unsigned int dimension_size>
void add_tensor_argument (unsigned &idx, const ICLTensor *tensor, const Window &window)
 
- 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...
 

Static Public Member Functions

static Status validate (const ITensorInfo *input, const ITensorInfo *output, Coordinates2D start, Coordinates2D end, uint32_t batch_index, float extrapolation_value=0, Window *output_window=nullptr)
 Static function to check if given info will lead to a valid configuration of CLStridedSliceKernel. 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 copy between two tensors.

Definition at line 35 of file CLCropKernel.h.

Constructor & Destructor Documentation

◆ CLCropKernel() [1/3]

Default constructor.

Definition at line 44 of file CLCropKernel.cpp.

45  : _input(nullptr), _output(nullptr), _start(), _batch_index(0), _extrapolation_value(0)
46 {
47 }

◆ CLCropKernel() [2/3]

CLCropKernel ( const CLCropKernel )
delete

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

◆ CLCropKernel() [3/3]

CLCropKernel ( CLCropKernel &&  )
default

Allow instances of this class to be moved.

Member Function Documentation

◆ configure()

void configure ( const ICLTensor input,
ICLTensor output,
Coordinates2D  start,
Coordinates2D  end,
uint32_t  batch_index,
float  extrapolation_value = 0,
Window output_window = nullptr 
)

Configure kernel.

Note
Supported tensor rank: up to 4
Parameters
[in]inputSource tensor. Data type supported: U16/S16/U32/S32/F16/F32. Data layouts supported: NHWC.
[out]outputDestination tensor. Data type supported: F32
[in]startCoordinates of where to start cropping the image.
[in]endCoordinates of where to end cropping the image.
[in]batch_indexFourth dimension index of the 3D image to crop in input.
[in]extrapolation_valueValue to be used for values outside of the image. Default is 0.
[in]output_windowOutput window to be used in case cropped image is being copied into a tensor. Default is nullptr.

Definition at line 49 of file CLCropKernel.cpp.

50 {
51  ARM_COMPUTE_ERROR_ON_NULLPTR(input, output);
52  ARM_COMPUTE_ERROR_THROW_ON(validate(input->info(), output->info(), start, end, batch_index, extrapolation_value, output_window));
53 
54  _input = input;
55  _output = output;
56  _start = start;
57  _batch_index = batch_index;
58  _extrapolation_value = extrapolation_value;
59 
60  const int vec_size_x = 4;
61  // Create and update the window (if needed)
62  Window win = calculate_max_window(*output->info());
63 
64  if(output_window != nullptr)
65  {
66  ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(win, *output_window);
67  win = *output_window;
68  }
69 
70  const int output_width_x = win.num_iterations(0);
71  const bool multi_access_x = output_width_x >= vec_size_x;
72  const bool remainder_x = output_width_x % vec_size_x > 0;
73 
74  if(multi_access_x)
75  {
76  win.set(Window::DimX,
77  Window::Dimension(win.x().start(), ceil_to_multiple(win.x().end(), vec_size_x), vec_size_x));
78  }
79  ICLKernel::configure_internal(win);
80 
81  // Create kernel
82  CLBuildOptions build_opts;
83  build_opts.add_option("-DDATA_TYPE=" + get_cl_type_from_data_type(input->info()->data_type()));
84  build_opts.add_option_if(multi_access_x, "-DVEC_SIZE=" + support::cpp11::to_string(vec_size_x));
85  build_opts.add_option_if(multi_access_x && remainder_x, "-DLAST_ACCESSED_X=" + support::cpp11::to_string(std::max<int>(output_width_x - vec_size_x, 0)));
86  build_opts.add_option_if(start.x > end.x, "-DWIDTH_FLIPPED=");
87  build_opts.add_option_if(start.y > end.y, "-DHEIGHT_FLIPPED=");
88  _kernel = static_cast<cl::Kernel>(CLKernelLibrary::get().create_kernel("crop_tensor", build_opts.options()));
89 }
static Status validate(const ITensorInfo *input, const ITensorInfo *output, Coordinates2D start, Coordinates2D end, uint32_t batch_index, float extrapolation_value=0, Window *output_window=nullptr)
Static function to check if given info will lead to a valid configuration of CLStridedSliceKernel.
std::string to_string(T &&value)
Convert integer and float values to string.
static CLKernelLibrary & get()
Access the KernelLibrary singleton.
#define ARM_COMPUTE_ERROR_THROW_ON(status)
Definition: Error.h:327
Window calculate_max_window(const ValidRegion &valid_region, const Steps &steps=Steps(), bool skip_border=false, BorderSize border_size=BorderSize())
Calculate the maximum window for a given tensor shape and border setting.
Definition: Helpers.cpp:28
static constexpr size_t DimX
Alias for dimension 0 also known as X dimension.
Definition: Window.h:43
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:66
std::string get_cl_type_from_data_type(const DataType &dt)
Translates a tensor data type to the appropriate OpenCL type.
Definition: CLHelpers.cpp:35
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
int x
The x coordinate.
Definition: types.h:30
#define ARM_COMPUTE_ERROR_ON_NULLPTR(...)
Definition: Validate.h:161
int y
The y coordinate.
Definition: types.h:31
#define ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(f, s)
Definition: Validate.h:205

References CLBuildOptions::add_option(), CLBuildOptions::add_option_if(), ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW, ARM_COMPUTE_ERROR_ON_NULLPTR, ARM_COMPUTE_ERROR_THROW_ON, arm_compute::calculate_max_window(), arm_compute::ceil_to_multiple(), arm_compute::create_kernel(), ITensorInfo::data_type(), Window::DimX, Window::Dimension::end(), CLKernelLibrary::get(), arm_compute::get_cl_type_from_data_type(), ITensor::info(), Window::num_iterations(), CLBuildOptions::options(), Window::set(), Window::Dimension::start(), arm_compute::support::cpp11::to_string(), CLCropKernel::validate(), Window::x(), Coordinates2D::x, and Coordinates2D::y.

◆ operator=() [1/2]

CLCropKernel& operator= ( const CLCropKernel )
delete

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

◆ operator=() [2/2]

CLCropKernel& operator= ( CLCropKernel &&  )
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.

Implements ICLKernel.

Definition at line 115 of file CLCropKernel.cpp.

116 {
119 
120  Window in_slice = Window();
121  in_slice.use_tensor_dimensions(_input->info()->tensor_shape());
122  in_slice.set(Window::DimX, Window::Dimension(in_slice.x().start(), ceil_to_multiple(in_slice.x().end(), window.x().step()), window.x().step()));
123  in_slice.set(3, Window::Dimension(_batch_index, _batch_index + 1, 1));
124 
125  unsigned int idx = 0;
126  add_3D_tensor_argument(idx, _input, in_slice);
127  add_3D_tensor_argument(idx, _output, window);
128  add_argument(idx, _start.x);
129  add_argument(idx, _start.y);
130  enqueue(queue, *this, window);
131 }
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:39
constexpr int step() const
Return the step of the dimension.
Definition: Window.h:102
void add_argument(unsigned int &idx, T value)
Add the passed parameters to the object's kernel's arguments starting from the index idx.
Definition: ICLKernel.h:226
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.
Definition: ICLKernel.h:158
int32_t x
X coordinates.
Definition: Types.h:429
int32_t y
Y coordinates.
Definition: Types.h:430
static constexpr size_t DimX
Alias for dimension 0 also known as X dimension.
Definition: Window.h:43
virtual const TensorShape & tensor_shape() const =0
Size for each dimension of the tensor.
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:66
virtual ITensorInfo * info() const =0
Interface to be implemented by the child class to return the tensor's metadata.
#define ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(f, s)
Definition: Validate.h:205
#define ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(k)
Definition: Validate.h:940
constexpr const Dimension & x() const
Alias to access the first dimension of the window.
Definition: Window.h:143

References ICLKernel::add_3D_tensor_argument(), ICLKernel::add_argument(), ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW, ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL, arm_compute::ceil_to_multiple(), Window::DimX, Window::Dimension::end(), arm_compute::enqueue(), ITensor::info(), Window::set(), Window::Dimension::start(), Window::Dimension::step(), ITensorInfo::tensor_shape(), Window::use_tensor_dimensions(), IKernel::window(), Window::x(), Coordinates2D::x, and Coordinates2D::y.

◆ validate()

Status validate ( const ITensorInfo input,
const ITensorInfo output,
Coordinates2D  start,
Coordinates2D  end,
uint32_t  batch_index,
float  extrapolation_value = 0,
Window output_window = nullptr 
)
static

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

Note
Supported tensor rank: up to 4
Parameters
[in]inputSource tensor info. Data type supported: U16/S16/U32/S32/F16/F32. Data layouts supported: NHWC.
[in]outputDestination tensor info. Data type supported: F32
[in]startCoordinates of where to start cropping the image.
[in]endCoordinates of where to end cropping the image.
[in]batch_indexFourth dimension index of the 3D image to crop in input.
[in]extrapolation_valueValue to be used for values outside of the image. Default is 0.
[in]output_windowOutput window to be used in case cropped image is being copied into a tensor. Default is nullptr.

Definition at line 91 of file CLCropKernel.cpp.

92 {
93  ARM_COMPUTE_UNUSED(extrapolation_value, output_window);
97  ARM_COMPUTE_RETURN_ERROR_ON(input->tensor_shape().num_dimensions() > 4);
98  ARM_COMPUTE_RETURN_ERROR_ON(start.x < 0 || start.y < 0 || end.x < 0 || end.y < 0);
99  ARM_COMPUTE_RETURN_ERROR_ON(start.x >= static_cast<int32_t>(input->dimension(1)) || start.y >= static_cast<int32_t>(input->dimension(2))
100  || end.x >= static_cast<int32_t>(input->dimension(1)) || end.y >= static_cast<int32_t>(input->dimension(2)));
101  ARM_COMPUTE_RETURN_ERROR_ON(batch_index >= input->dimension(3));
102  if(output_window != nullptr)
103  {
104  ARM_COMPUTE_RETURN_ERROR_ON(output_window->x().step() != 1);
105  }
106  if(output->total_size() > 0)
107  {
110  ARM_COMPUTE_RETURN_ERROR_ON(output->num_dimensions() > 3);
111  }
112  return Status{};
113 }
#define ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_LAYOUT(...)
Definition: Validate.h:494
#define ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(t, c,...)
Definition: Validate.h:791
1 channel, 1 F32 per channel
1 channel, 1 U16 per channel
#define ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_NOT_IN(t,...)
Definition: Validate.h:693
#define ARM_COMPUTE_RETURN_ERROR_ON(cond)
If the condition is true, an error is returned.
Definition: Error.h:244
#define ARM_COMPUTE_RETURN_ERROR_ON_CPU_F16_UNSUPPORTED(tensor)
Definition: Validate.h:71
1 channel, 1 F16 per channel
1 channel, 1 S32 per channel
#define ARM_COMPUTE_UNUSED(...)
To avoid unused variables warnings.
Definition: Error.h:160
1 channel, 1 U32 per channel
int x
The x coordinate.
Definition: types.h:30
1 channel, 1 S16 per channel
Num samples, height, width, channels.
int y
The y coordinate.
Definition: types.h:31
#define ARM_COMPUTE_RETURN_ERROR_ON_DATA_LAYOUT_NOT_IN(t,...)
Definition: Validate.h:745

References ARM_COMPUTE_RETURN_ERROR_ON, ARM_COMPUTE_RETURN_ERROR_ON_CPU_F16_UNSUPPORTED, ARM_COMPUTE_RETURN_ERROR_ON_DATA_LAYOUT_NOT_IN, ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN, ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_NOT_IN, ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_LAYOUT, ARM_COMPUTE_UNUSED, ITensorInfo::dimension(), arm_compute::F16, arm_compute::F32, arm_compute::NHWC, Dimensions< T >::num_dimensions(), ITensorInfo::num_dimensions(), arm_compute::S16, arm_compute::S32, Window::Dimension::step(), ITensorInfo::tensor_shape(), ITensorInfo::total_size(), arm_compute::U16, arm_compute::U32, Window::x(), Coordinates2D::x, and Coordinates2D::y.

Referenced by CLCropKernel::configure(), and CLCropResize::validate().


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