Compute Library
 20.02.1
CLFillBorderKernel.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2016-2020 ARM Limited.
3  *
4  * SPDX-License-Identifier: MIT
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining a copy
7  * of this software and associated documentation files (the "Software"), to
8  * deal in the Software without restriction, including without limitation the
9  * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
10  * sell copies of the Software, and to permit persons to whom the Software is
11  * furnished to do so, subject to the following conditions:
12  *
13  * The above copyright notice and this permission notice shall be included in all
14  * copies or substantial portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22  * SOFTWARE.
23  */
25 
30 #include "arm_compute/core/Error.h"
32 #include "arm_compute/core/Types.h"
33 #include "arm_compute/core/Utils.h"
36 
37 #include <cstdint>
38 #include <set>
39 #include <sstream>
40 #include <string>
41 
42 using namespace arm_compute;
43 
45  : ICLKernel(), _tensor(nullptr)
46 {
47 }
48 
50 {
51  return false;
52 }
53 
54 template <class T>
55 void CLFillBorderKernel::set_constant_border(unsigned int idx, const PixelValue &constant_border_value)
56 {
57  T value;
58  constant_border_value.get(value);
59  ICLKernel::add_argument<T>(idx, static_cast<T>(value));
60 }
61 
62 void CLFillBorderKernel::configure(ICLTensor *tensor, BorderSize border_size, BorderMode border_mode, const PixelValue &constant_border_value)
63 {
64  ARM_COMPUTE_ERROR_ON(tensor == nullptr);
65  ARM_COMPUTE_ERROR_ON(tensor->info()->num_channels() != 1);
66 
67  border_size.limit(tensor->info()->padding());
68 
69  // If there is no border: early exit
71  {
72  return;
73  }
74 
75  // Select appropriate kernel
76  std::string kernel_name = "fill_image_borders_" + lower_string(string_from_border_mode(border_mode));
77 
78  const DataType dt = tensor->info()->data_type();
79 
80  // Define build options
81  CLBuildOptions build_opts;
82  build_opts.add_option("-DDATA_TYPE=" + get_underlying_cl_type_from_data_type(dt));
83  build_opts.add_option("-DBORDER_SIZE_TOP=" + support::cpp11::to_string(border_size.top));
84  build_opts.add_option("-DBORDER_SIZE_BOTTOM=" + support::cpp11::to_string(border_size.bottom));
85  build_opts.add_option("-DBORDER_SIZE_LEFT=" + support::cpp11::to_string(border_size.left));
86  build_opts.add_option("-DBORDER_SIZE_RIGHT=" + support::cpp11::to_string(border_size.right));
87 
88  // Create kernel
89  _kernel = static_cast<cl::Kernel>(CLKernelLibrary::get().create_kernel(kernel_name, build_opts.options()));
90  _tensor = tensor;
91 
92  // Create static kernel arguments
93  const unsigned int valid_width = tensor->info()->valid_region().shape[0];
94  const unsigned int valid_height = tensor->info()->valid_region().shape[1];
95  const cl_int2 valid_region_coords =
96  {
97  {
98  static_cast<cl_int>(tensor->info()->valid_region().anchor[0]),
99  static_cast<cl_int>(tensor->info()->valid_region().anchor[1]),
100  }
101  };
102  const unsigned int total_valid_width = border_size.left + valid_width + border_size.right;
103 
104  // Set static kernel arguments
105  unsigned int idx = num_arguments_per_3D_tensor(); //Skip the tensor parameters
106  ICLKernel::add_argument<cl_uint>(idx, valid_width);
107  ICLKernel::add_argument<cl_uint>(idx, valid_height);
108  ICLKernel::add_argument<cl_int2>(idx, valid_region_coords);
110  {
111  switch(dt)
112  {
113  case DataType::U8:
114  case DataType::QASYMM8:
115  set_constant_border<uint8_t>(idx, constant_border_value);
116  break;
117  case DataType::S8:
119  set_constant_border<int8_t>(idx, constant_border_value);
120  break;
121  case DataType::U16:
122  set_constant_border<uint16_t>(idx, constant_border_value);
123  break;
124  case DataType::S16:
125  set_constant_border<int16_t>(idx, constant_border_value);
126  break;
127  case DataType::U32:
128  set_constant_border<uint32_t>(idx, constant_border_value);
129  break;
130  case DataType::S32:
131  set_constant_border<int32_t>(idx, constant_border_value);
132  break;
133  case DataType::F32:
134  static_assert(sizeof(float) == 4, "Float must be 32 bit");
135  set_constant_border<float>(idx, constant_border_value);
136  break;
137  case DataType::F16:
138  static_assert(sizeof(cl_half) == sizeof(half), "Half must be same size as cl_half");
139  static_assert(sizeof(cl_half) == 2, "Half must be 16 bit");
140  set_constant_border<half>(idx, constant_border_value);
141  break;
142  default:
143  ARM_COMPUTE_ERROR("Not handled");
144  }
145  }
146 
147  // Configure kernel window
148  Window win;
149  win.set(Window::DimX, Window::Dimension(0, total_valid_width + valid_height));
150  win.set(Window::DimY, Window::Dimension(0, 1, 1));
152  ICLKernel::configure_internal(win);
153 
154  // Set config_id for enabling LWS tuning
155  _config_id = kernel_name;
156  _config_id += "_";
157  _config_id += lower_string(string_from_data_type(dt));
158  _config_id += "_";
159  _config_id += support::cpp11::to_string(tensor->info()->dimension(0));
160  _config_id += "_";
161  _config_id += support::cpp11::to_string(tensor->info()->dimension(1));
162  _config_id += "_";
164 }
165 
166 void CLFillBorderKernel::run(const Window &window, cl::CommandQueue &queue)
167 {
168  // Border mode undefined or border width == 0
169  if(_kernel() == nullptr)
170  {
171  return;
172  }
173 
176 
178  Window slice = collapsed.first_slice_window_3D();
179 
180  do
181  {
182  unsigned int idx = 0;
183  add_3D_tensor_argument(idx, _tensor, slice);
184  enqueue(queue, *this, slice, lws_hint());
185  }
186  while(collapsed.slide_window_slice_3D(slice));
187 }
BorderMode
Methods available to handle borders.
Definition: Types.h:261
unsigned int top
top of the border
Definition: Types.h:349
Class describing the value of a pixel for any image format.
Definition: PixelValue.h:34
const Window & window() const
The maximum window the kernel can be executed on.
Definition: IKernel.cpp:28
TensorShape shape
Shape of the valid region.
Definition: Types.h:257
virtual size_t dimension(size_t index) const =0
Return the size of the requested dimension.
Container for 2D border size.
Definition: Types.h:269
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
const StringSet & options() const
Gets the current options list set.
#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:247
1 channel, 1 U8 per channel
void get(uint8_t &v) const
Interpret the pixel value as a U8.
Definition: PixelValue.h:230
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.
half_float::half half
16-bit floating point type
Definition: Types.h:45
1 channel, 1 F32 per channel
#define ARM_COMPUTE_ERROR_ON(cond)
If the condition is true then an error message is printed and an exception thrown.
Definition: Error.h:466
bool is_parallelisable() const override
Indicates whether or not the kernel is parallelisable.
static CLKernelLibrary & get()
Access the KernelLibrary singleton.
Describe one of the image's dimensions with a start, end and step.
Definition: Window.h:75
1 channel, 1 U16 per channel
unsigned int bottom
bottom of the border
Definition: Types.h:351
std::string lower_string(const std::string &val)
Lower a given string.
Definition: Utils.cpp:333
Common interface for all the OpenCL kernels.
Definition: ICLKernel.h:43
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.
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
void configure(ICLTensor *tensor, BorderSize border_size, BorderMode border_mode, const PixelValue &constant_border_value=PixelValue())
Initialise the kernel's input, output and border mode.
void use_tensor_dimensions(const TensorShape &shape, size_t first_dimension=Window::DimX)
Use the tensor's dimensions to fill the window dimensions.
Definition: Window.inl:264
Copyright (c) 2017-2020 ARM Limited.
1 channel, 1 F16 per channel
virtual ValidRegion valid_region() const =0
Valid region of the tensor.
const std::string & string_from_border_mode(BorderMode border_mode)
Translates a given border mode policy to a string.
Definition: Utils.cpp:231
1 channel, 1 S32 per channel
void add_option(std::string option)
Adds option to the existing build option list.
const std::string & string_from_data_type(DataType dt)
Convert a data type identity into a string.
Definition: Utils.cpp:144
static constexpr size_t DimX
Alias for dimension 0 also known as X dimension.
Definition: Window.h:43
static constexpr unsigned int num_arguments_per_3D_tensor()
Returns the number of arguments enqueued per 3D tensor object.
Definition: ICLKernel.h:200
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:68
1 channel, 1 U32 per channel
virtual const TensorShape & tensor_shape() const =0
Size for each dimension of the tensor.
quantized, asymmetric fixed-point 8-bit number unsigned
std::string kernel_name
CLFillBorderKernel()
Default constructor.
virtual ITensorInfo * info() const =0
Interface to be implemented by the child class to return the tensor's metadata.
virtual BorderSize border_size() const
The size of the border for that kernel.
Definition: IKernel.cpp:46
#define ARM_COMPUTE_ERROR_ON_MISMATCHING_WINDOWS(f, w)
Definition: Validate.h:183
void set(size_t dimension, const Dimension &dim)
Set the values of a given dimension.
Definition: Window.inl:49
virtual PaddingSize padding() const =0
Padding of tensor.
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
unsigned int left
left of the border
Definition: Types.h:352
bool slide_window_slice_3D(Window &slice) const
Slide the passed 3D window slice.
Definition: Window.h:333
std::string get_underlying_cl_type_from_data_type(const DataType &dt)
Translates fixed point tensor data type to the underlying OpenCL type.
Definition: CLHelpers.cpp:221
unsigned int right
right of the border
Definition: Types.h:350
1 channel, 1 S16 per channel
void run(const Window &window, cl::CommandQueue &queue) override
Enqueue the OpenCL kernel to process the given window on the passed OpenCL command queue.
static constexpr size_t DimY
Alias for dimension 1 also known as Y dimension.
Definition: Window.h:45
Interface for OpenCL tensor.
Definition: ICLTensor.h:42
constexpr bool empty() const
Check if the entire border is zero.
Definition: Types.h:296
static constexpr size_t DimZ
Alias for dimension 2 also known as Z dimension.
Definition: Window.h:47
Borders are left undefined.
quantized, asymmetric fixed-point 8-bit number signed
Window first_slice_window_3D() const
First 3D slice of the window.
Definition: Window.h:289
DataType
Available data types.
Definition: Types.h:75
signed 8-bit number
Describe a multidimensional execution window.
Definition: Window.h:39
#define ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(k)
Definition: Validate.h:941
virtual size_t num_channels() const =0
The number of channels for each tensor element.
void limit(const BorderSize &limit)
Limit this border size.
Definition: Types.h:341
Coordinates anchor
Anchor for the start of the valid region.
Definition: Types.h:256
SimpleTensor< T > slice(const SimpleTensor< T > &src, Coordinates starts, Coordinates ends)