Compute Library
 23.11
CLFillBorderKernel.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2016-2021, 2023 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 
31 #include "arm_compute/core/Utils.h"
34 
36 #include "support/Cast.h"
37 #include "support/StringSupport.h"
38 
39 namespace arm_compute
40 {
42 {
44 }
45 
47 {
48  return false;
49 }
50 
51 template <class T>
52 void CLFillBorderKernel::set_constant_border(unsigned int idx, const PixelValue &constant_border_value)
53 {
54  T value;
55  constant_border_value.get(value);
56  ICLKernel::add_argument<T>(idx, static_cast<T>(value));
57 }
58 
60  BorderSize border_size,
61  BorderMode border_mode,
62  const PixelValue &constant_border_value)
63 {
64  configure(CLKernelLibrary::get().get_compile_context(), tensor, border_size, border_mode, constant_border_value);
65 }
66 
67 void CLFillBorderKernel::configure(const CLCompileContext &compile_context,
69  BorderSize border_size,
70  BorderMode border_mode,
71  const PixelValue &constant_border_value)
72 {
73  _tensor = tensor;
74  configure(compile_context, tensor->info(), border_size, border_mode, constant_border_value);
75 }
76 
77 void CLFillBorderKernel::configure(const CLCompileContext &compile_context,
79  BorderSize border_size,
80  BorderMode border_mode,
81  const PixelValue &constant_border_value)
82 {
83  ARM_COMPUTE_ERROR_ON(tensor == nullptr);
84  ARM_COMPUTE_ERROR_ON(tensor->num_channels() != 1);
85  auto padding_info = get_padding_info({tensor});
86 
87  border_size.limit(tensor->padding());
88 
89  // If there is no border: early exit
90  if (border_size.empty() || border_mode == BorderMode::UNDEFINED)
91  {
92  return;
93  }
94 
95  // Select appropriate kernel
96  std::string kernel_name = "fill_image_borders_" + lower_string(string_from_border_mode(border_mode));
97 
98  const DataType dt = tensor->data_type();
99 
100  // Define build options
101  CLBuildOptions build_opts;
102  build_opts.add_option("-DDATA_TYPE=" + get_cl_type_from_data_type(dt));
103  build_opts.add_option("-DBORDER_SIZE_TOP=" + support::cpp11::to_string(border_size.top));
104  build_opts.add_option("-DBORDER_SIZE_BOTTOM=" + support::cpp11::to_string(border_size.bottom));
105  build_opts.add_option("-DBORDER_SIZE_LEFT=" + support::cpp11::to_string(border_size.left));
106  build_opts.add_option("-DBORDER_SIZE_RIGHT=" + support::cpp11::to_string(border_size.right));
107 
108  // Create kernel
109  _kernel = create_kernel(compile_context, kernel_name, build_opts.options());
110 
111  // Create static kernel arguments
112  const unsigned int valid_width = tensor->valid_region().shape[0];
113  const unsigned int valid_height = tensor->valid_region().shape[1];
114  const cl_int2 valid_region_coords = {{
115  static_cast<cl_int>(tensor->valid_region().anchor[0]),
116  static_cast<cl_int>(tensor->valid_region().anchor[1]),
117  }};
118  const unsigned int total_valid_width = border_size.left + valid_width + border_size.right;
119 
120  // Set static kernel arguments
121  unsigned int idx = num_arguments_per_3D_tensor(); //Skip the tensor parameters
122  ICLKernel::add_argument<cl_uint>(idx, valid_width);
123  ICLKernel::add_argument<cl_uint>(idx, valid_height);
124  ICLKernel::add_argument<cl_int2>(idx, valid_region_coords);
125  if (BorderMode::CONSTANT == border_mode)
126  {
127  switch (dt)
128  {
129  case DataType::U8:
130  case DataType::QASYMM8:
131  set_constant_border<uint8_t>(idx, constant_border_value);
132  break;
133  case DataType::S8:
135  set_constant_border<int8_t>(idx, constant_border_value);
136  break;
137  case DataType::U16:
138  set_constant_border<uint16_t>(idx, constant_border_value);
139  break;
140  case DataType::S16:
141  set_constant_border<int16_t>(idx, constant_border_value);
142  break;
143  case DataType::U32:
144  set_constant_border<uint32_t>(idx, constant_border_value);
145  break;
146  case DataType::S32:
147  set_constant_border<int32_t>(idx, constant_border_value);
148  break;
149  case DataType::F32:
150  static_assert(sizeof(float) == 4, "Float must be 32 bit");
151  set_constant_border<float>(idx, constant_border_value);
152  break;
153  case DataType::F16:
154  static_assert(sizeof(cl_half) == sizeof(half), "Half must be same size as cl_half");
155  static_assert(sizeof(cl_half) == 2, "Half must be 16 bit");
156  set_constant_border<half>(idx, constant_border_value);
157  break;
158  default:
159  ARM_COMPUTE_ERROR("Not handled");
160  }
161  }
162 
163  // Configure kernel window
164  Window win;
165  win.set(Window::DimX, Window::Dimension(0, total_valid_width + valid_height));
166  win.set(Window::DimY, Window::Dimension(0, 1, 1));
167  win.use_tensor_dimensions(tensor->tensor_shape(), Window::DimZ);
168  ICLKernel::configure_internal(win);
169 
170  // Set config_id for enabling LWS tuning
171  _config_id = kernel_name;
172  _config_id += "_";
173  _config_id += lower_string(string_from_data_type(dt));
174  _config_id += "_";
175  _config_id += support::cpp11::to_string(tensor->dimension(0));
176  _config_id += "_";
177  _config_id += support::cpp11::to_string(tensor->dimension(1));
178  _config_id += "_";
179  _config_id += lower_string(string_from_border_mode(border_mode));
181 }
182 
183 void CLFillBorderKernel::run_op(ITensorPack &tensors, const Window &window, cl::CommandQueue &queue)
184 {
185  // Border mode undefined or border width == 0
186  if (_kernel() == nullptr)
187  {
188  return;
189  }
190 
191  const auto tensor =
192  utils::cast::polymorphic_downcast<const ICLTensor *>(tensors.get_const_tensor(TensorType::ACL_SRC));
193 
196 
198  Window slice = collapsed.first_slice_window_3D();
199 
200  do
201  {
202  unsigned int idx = 0;
204  enqueue(queue, *this, slice, lws_hint());
205  } while (collapsed.slide_window_slice_3D(slice));
206 }
207 
208 void CLFillBorderKernel::run(const Window &window, cl::CommandQueue &queue)
209 {
210  // Border mode undefined or border width == 0
211  if (_kernel() == nullptr)
212  {
213  return;
214  }
215 
218 
220  Window slice = collapsed.first_slice_window_3D();
221 
222  do
223  {
224  unsigned int idx = 0;
225  add_3D_tensor_argument(idx, _tensor, slice);
226  enqueue(queue, *this, slice, lws_hint());
227  } while (collapsed.slide_window_slice_3D(slice));
228 }
229 } // namespace arm_compute
arm_compute::support::cpp11::to_string
std::string to_string(T &&value)
Convert integer and float values to string.
Definition: StringSupport.h:168
arm_compute::BorderSize::right
unsigned int right
right of the border
Definition: Types.h:340
arm_compute::BorderMode::CONSTANT
@ CONSTANT
Pixels outside the image are assumed to have a constant value.
Cast.h
StringSupport.h
arm_compute::CLFillBorderKernel::run_op
void run_op(ITensorPack &tensors, const Window &window, cl::CommandQueue &queue) override
Enqueue the OpenCL kernel to process the given window on the passed OpenCL command queue.
Definition: CLFillBorderKernel.cpp:183
ICLTensor.h
arm_compute::PixelValue
Class describing the value of a pixel for any image format.
Definition: PixelValue.h:35
arm_compute::CLBuildOptions::options
const StringSet & options() const
Gets the current options list set.
Definition: CLCompileContext.cpp:72
arm_compute::CLFillBorderKernel::is_parallelisable
bool is_parallelisable() const override
Indicates whether or not the kernel is parallelisable.
Definition: CLFillBorderKernel.cpp:46
arm_compute::BorderSize
Container for 2D border size.
Definition: Types.h:239
arm_compute::BorderMode::UNDEFINED
@ UNDEFINED
Borders are left undefined.
arm_compute::DataType::QASYMM8
@ QASYMM8
quantized, asymmetric fixed-point 8-bit number unsigned
arm_compute::PixelValue::get
void get(uint8_t &v) const
Interpret the pixel value as a U8.
Definition: PixelValue.h:228
arm_compute::DataType::U16
@ U16
unsigned 16-bit number
arm_compute::lower_string
std::string lower_string(const std::string &val)
Lower a given string.
Definition: StringUtils.cpp:38
ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL
#define ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(k)
Definition: Validate.h:1079
arm_compute::Window::DimX
static constexpr size_t DimX
Alias for dimension 0 also known as X dimension.
Definition: Window.h:43
arm_compute::Window::collapse_if_possible
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:72
arm_compute::ICLTensor
Interface for OpenCL tensor.
Definition: ICLTensor.h:41
ARM_COMPUTE_ERROR
#define ARM_COMPUTE_ERROR(msg)
Print the given message then throw an std::runtime_error.
Definition: Error.h:354
arm_compute::BorderSize::top
unsigned int top
top of the border
Definition: Types.h:339
arm_compute::Window::use_tensor_dimensions
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:290
arm_compute::string_from_border_mode
const std::string & string_from_border_mode(BorderMode border_mode)
Translates a given border mode policy to a string.
Definition: Utils.cpp:96
TensorInfo.h
arm_compute::string_from_data_type
const std::string & string_from_data_type(DataType dt)
Convert a data type identity into a string.
Definition: DataTypeUtils.cpp:31
arm_compute::IKernel::border_size
virtual BorderSize border_size() const
The size of the border for that kernel.
Definition: IKernel.cpp:45
arm_compute::BorderSize::empty
constexpr bool empty() const
Check if the entire border is zero.
Definition: Types.h:264
arm_compute::DataType::S8
@ S8
signed 8-bit number
arm_compute::CLKernelLibrary::get
static CLKernelLibrary & get()
Access the KernelLibrary singleton.
Definition: CLKernelLibrary.cpp:41
CLKernelLibrary.h
Manages all the OpenCL kernels compilation and caching, provides accessors for the OpenCL Context.
StringUtils.h
arm_compute::half
half_float::half half
16-bit floating point type
Definition: CoreTypes.h:36
ARM_COMPUTE_ERROR_ON
#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
arm_compute::BorderSize::bottom
unsigned int bottom
bottom of the border
Definition: Types.h:341
arm_compute::CLFillBorderKernel::CLFillBorderKernel
CLFillBorderKernel()
Default constructor.
Definition: CLFillBorderKernel.cpp:41
arm_compute::BorderSize::limit
void limit(const BorderSize &limit)
Limit this border size.
Definition: Types.h:331
arm_compute::ITensorPack::get_const_tensor
const ITensor * get_const_tensor(int id) const
Get constant tensor of a given id.
Definition: ITensorPack.cpp:53
arm_compute::DataType::U32
@ U32
unsigned 32-bit number
arm_compute::ITensorPack
Tensor packing service.
Definition: ITensorPack.h:39
arm_compute::CLBuildOptions::add_option
void add_option(std::string option)
Adds option to the existing build option list.
Definition: CLCompileContext.cpp:41
arm_compute::CLFillBorderKernel::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.
Definition: CLFillBorderKernel.cpp:52
arm_compute::CLCompileContext
CLCompileContext class.
Definition: CLCompileContext.h:204
CLFillBorderKernel.h
arm_compute::DataType::U8
@ U8
unsigned 8-bit number
arm_compute::DataType::S16
@ S16
signed 16-bit number
arm_compute::create_kernel
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:409
arm_compute::DataType::QASYMM8_SIGNED
@ QASYMM8_SIGNED
quantized, asymmetric fixed-point 8-bit number signed
WindowHelpers.h
arm_compute::Window::slide_window_slice_3D
bool slide_window_slice_3D(Window &slice) const
Slide the passed 3D window slice.
Definition: Window.h:350
OpenCL.h
Wrapper to configure the Khronos OpenCL C++ header.
dt
DataType dt
Definition: NEBatchNormalizationLayerKernel.cpp:50
tensor
CLTensor * tensor
Pointer to the auxiliary tensor.
Definition: ClWorkloadRuntime.cpp:67
arm_compute::Window::Dimension
Describe one of the image's dimensions with a start, end and step.
Definition: Window.h:79
arm_compute::ICLKernel
Common interface for all the OpenCL kernels.
Definition: ICLKernel.h:67
arm_compute::Window::set
void set(size_t dimension, const Dimension &dim)
Set the values of a given dimension.
Definition: Window.inl:53
arm_compute::Window::DimY
static constexpr size_t DimY
Alias for dimension 1 also known as Y dimension.
Definition: Window.h:45
arm_compute::Window::first_slice_window_3D
Window first_slice_window_3D() const
First 3D slice of the window.
Definition: Window.h:306
arm_compute::IKernel::window
const Window & window() const
The maximum window the kernel can be executed on.
Definition: IKernel.cpp:28
arm_compute::get_cl_type_from_data_type
std::string get_cl_type_from_data_type(const DataType &dt)
Translates a tensor data type to the appropriate OpenCL type.
Definition: CLHelpers.cpp:41
arm_compute::ICLKernel::add_3D_tensor_argument
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:234
Utils.h
arm_compute::Window
Describe a multidimensional execution window.
Definition: Window.h:39
arm_compute::ELEMENTWISE
@ ELEMENTWISE
Elementwise CL kernel type.
Definition: CLTypes.h:83
arm_compute::ICLKernel::num_arguments_per_3D_tensor
constexpr static unsigned int num_arguments_per_3D_tensor()
Returns the number of arguments enqueued per 3D tensor object.
Definition: ICLKernel.h:321
arm_compute
Copyright (c) 2017-2023 Arm Limited.
Definition: introduction.dox:24
arm_compute::CLFillBorderKernel::run
void run(const Window &window, cl::CommandQueue &queue) override
Enqueue the OpenCL kernel to process the given window on the passed OpenCL command queue.
Definition: CLFillBorderKernel.cpp:208
arm_compute::DataType::F16
@ F16
16-bit floating-point number
arm_compute::BorderSize::left
unsigned int left
left of the border
Definition: Types.h:342
arm_compute::has_padding_changed
bool has_padding_changed(const std::unordered_map< const ITensorInfo *, PaddingSize > &padding_map)
Check if the previously stored padding info has changed after configuring a kernel.
Definition: Utils.cpp:491
arm_compute::Window::DimZ
static constexpr size_t DimZ
Alias for dimension 2 also known as Z dimension.
Definition: Window.h:47
arm_compute::DataType::S32
@ S32
signed 32-bit number
arm_compute::ICLKernel::lws_hint
cl::NDRange lws_hint() const
Return the Local-Workgroup-Size hint.
Definition: ICLKernel.h:383
arm_compute::BorderMode
BorderMode
Methods available to handle borders.
Definition: Types.h:231
arm_compute::ACL_SRC
@ ACL_SRC
Definition: Types.h:44
arm_compute::ITensorInfo
Store the tensor's metadata.
Definition: ITensorInfo.h:44
arm_compute::DataType::F32
@ F32
32-bit floating-point number
arm_compute::CLBuildOptions
Build options.
Definition: CLCompileContext.h:38
ARM_COMPUTE_ERROR_ON_MISMATCHING_WINDOWS
#define ARM_COMPUTE_ERROR_ON_MISMATCHING_WINDOWS(f, w)
Definition: Validate.h:181
arm_compute::CLFillBorderKernel::configure
void configure(const CLCompileContext &compile_context, ICLTensor *tensor, BorderSize border_size, BorderMode border_mode, const PixelValue &constant_border_value=PixelValue())
Initialise the kernel's input, output and border mode.
Definition: CLFillBorderKernel.cpp:67
Validate.h
arm_compute::DataType
DataType
Available data types.
Definition: CoreTypes.h:83
arm_compute::get_padding_info
std::unordered_map< const ITensorInfo *, PaddingSize > get_padding_info(std::initializer_list< const ITensorInfo * > infos)
Stores padding information before configuring a kernel.
Definition: Utils.cpp:476
arm_compute::test::validation::reference::slice
SimpleTensor< T > slice(const SimpleTensor< T > &src, Coordinates starts, Coordinates ends)
Definition: SliceOperations.cpp:38
CLHelpers.h
kernel_name
std::string kernel_name
Definition: ClIm2ColKernel.cpp:58
arm_compute::enqueue
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:33