Compute Library
 21.02
GCFillBorderKernel.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2017-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 
26 #include "arm_compute/core/Error.h"
32 #include "arm_compute/core/Utils.h"
37 #include "support/StringSupport.h"
38 
39 #include <cstdint>
40 #include <set>
41 #include <string>
42 
43 using namespace arm_compute;
44 
46  : IGCKernel(), _tensor(nullptr)
47 {
48 }
49 
51 {
52  return false;
53 }
54 
55 template <class T>
56 void GCFillBorderKernel::set_constant_border(unsigned int idx, const PixelValue &constant_border_value)
57 {
58  T value;
59  constant_border_value.get(value);
60  _kernel.set_argument(idx, static_cast<T>(value));
61 }
62 
63 void GCFillBorderKernel::configure(const IGCTensor *tensor, BorderSize border_size, BorderMode border_mode, const PixelValue &constant_border_value)
64 {
65  ARM_COMPUTE_ERROR_ON(tensor == nullptr);
67  ARM_COMPUTE_ERROR_ON(tensor->info()->num_channels() != 1);
68 
69  border_size.limit(tensor->info()->padding());
70 
71  // If there is no border: early exit
72  if(border_size.empty() || border_mode == BorderMode::UNDEFINED)
73  {
74  return;
75  }
76 
77  // Select appropriate kernel
78  std::string kernel_name = "fill_image_borders_" + lower_string(string_from_border_mode(border_mode));
79 
80  // Define build options
81  std::set<std::string> build_opts;
82  build_opts.emplace("#define LOCAL_SIZE_X " + support::cpp11::to_string(1));
83  build_opts.emplace("#define LOCAL_SIZE_Y " + support::cpp11::to_string(1));
84  build_opts.emplace("#define LOCAL_SIZE_Z " + support::cpp11::to_string(1));
85  build_opts.emplace("#define BORDER_SIZE_TOP " + support::cpp11::to_string(border_size.top));
86  build_opts.emplace("#define BORDER_SIZE_BOTTOM " + support::cpp11::to_string(border_size.bottom));
87  build_opts.emplace("#define BORDER_SIZE_LEFT " + support::cpp11::to_string(border_size.left));
88  build_opts.emplace("#define BORDER_SIZE_RIGHT " + support::cpp11::to_string(border_size.right));
89 
90  if(border_mode == BorderMode::REPLICATE)
91  {
92  build_opts.emplace("#define FILL_IMAGE_BORDERS_REPLICATE\n");
93  }
94  else
95  {
96  build_opts.emplace("#define FILL_IMAGE_BORDERS_CONSTANT\n");
97  }
98 
99  switch(tensor->info()->data_type())
100  {
101  case DataType::F16:
102  build_opts.emplace("#define DATA_TYPE_FP16");
103  break;
104 
105  case DataType::F32:
106  build_opts.emplace("#define DATA_TYPE_FP32");
107  break;
108 
109  default:
110  ARM_COMPUTE_ERROR("Current data type is not supported");
111  break;
112  }
113 
114  // Create kernel
115  _kernel = static_cast<GCKernel>(GCKernelLibrary::get().create_kernel(kernel_name, build_opts));
116  _tensor = tensor;
117 
118  // Create static kernel arguments
119  const unsigned int valid_width = tensor->info()->valid_region().shape[0];
120  const unsigned int valid_height = tensor->info()->valid_region().shape[1];
121  const unsigned int total_valid_width = border_size.left + valid_width + border_size.right;
122 
123  // Set static kernel arguments
124  unsigned int idx = num_arguments_per_3D_tensor(); //Skip the tensor parameters
125  _kernel.set_argument(idx++, valid_width);
126  _kernel.set_argument(idx++, valid_height);
127  _kernel.set_argument(idx++, tensor->info()->valid_region().anchor[0]);
128  _kernel.set_argument(idx++, tensor->info()->valid_region().anchor[1]);
129 
130  if(BorderMode::CONSTANT == border_mode)
131  {
132  set_constant_border<float>(idx++, constant_border_value);
133  }
134 
135  // Configure kernel window
136  Window win;
137  win.set(Window::DimX, Window::Dimension(0, total_valid_width + valid_height));
138  win.set(Window::DimY, Window::Dimension(0, 1, 1));
139  win.use_tensor_dimensions(tensor->info()->tensor_shape(), Window::DimZ);
140 
141  IGCKernel::configure(win);
142 }
143 
145 {
146  // Border mode undefined or border width == 0
147  if(_kernel.get_program() == 0)
148  {
149  return;
150  }
151 
154 
155  _kernel.use();
157 
158  do
159  {
160  unsigned int idx = 0;
161  add_3D_tensor_argument(idx, _tensor, 1, slice);
162 
163  _kernel.update_shader_params();
164 
165  enqueue(*this, slice);
166  }
167  while(window.slide_window_slice_3D(slice));
168 }
BorderMode
Methods available to handle borders.
Definition: Types.h:265
GCFillBorderKernel()
Default constructor.
unsigned int top
top of the border
Definition: Types.h:375
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
void add_3D_tensor_argument(unsigned int &idx, const IGCTensor *tensor, const unsigned int binding_point, const Window &window)
Add the passed 3D tensor&#39;s parameters to the object&#39;s kernel&#39;s arguments starting from the index idx...
Definition: IGCKernel.cpp:132
void enqueue(IGCKernel &kernel, const Window &window, const gles::NDRange &lws=gles::NDRange(1U, 1U, 1U))
Add the kernel to the command queue with the given window.
Definition: IGCKernel.cpp:41
TensorShape shape
Shape of the valid region.
Definition: Types.h:261
Container for 2D border size.
Definition: Types.h:273
#define ARM_COMPUTE_ERROR(msg)
Print the given message then throw an std::runtime_error.
Definition: Error.h:352
void get(uint8_t &v) const
Interpret the pixel value as a U8.
Definition: PixelValue.h:241
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.
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.
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
void run(const Window &window) override
Enqueue the OpenGL ES shader to process the given window.
Interface for GLES Compute tensor.
Definition: IGCTensor.h:35
Describe one of the image&#39;s dimensions with a start, end and step.
Definition: Window.h:77
unsigned int bottom
bottom of the border
Definition: Types.h:377
GCKernel class.
unsigned int num_arguments_per_3D_tensor() const
Returns the number of arguments enqueued per 3D tensor object.
Definition: IGCKernel.cpp:147
std::string lower_string(const std::string &val)
Lower a given string.
Definition: Utils.cpp:350
Copyright (c) 2017-2021 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:224
static constexpr size_t DimX
Alias for dimension 0 also known as X dimension.
Definition: Window.h:43
#define ARM_COMPUTE_ERROR_ON_MISMATCHING_WINDOWS(f, w)
Definition: Validate.h:183
virtual const TensorShape & tensor_shape() const =0
Size for each dimension of the tensor.
Manages all the GLES kernels compilation and caching, provides accessors for the GLES Context...
std::string kernel_name
void configure(const IGCTensor *tensor, BorderSize border_size, BorderMode border_mode, const PixelValue &constant_border_value=PixelValue())
Initialise the kernel&#39;s input, output and border mode.
virtual ITensorInfo * info() const =0
Interface to be implemented by the child class to return the tensor&#39;s metadata.
virtual BorderSize border_size() const
The size of the border for that kernel.
Definition: IKernel.cpp:46
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.
unsigned int left
left of the border
Definition: Types.h:378
bool slide_window_slice_3D(Window &slice) const
Slide the passed 3D window slice.
Definition: Window.h:335
unsigned int right
right of the border
Definition: Types.h:376
#define ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(k)
Definition: Validate.h:941
#define ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(t, c,...)
Definition: Validate.h:790
static constexpr size_t DimY
Alias for dimension 1 also known as Y dimension.
Definition: Window.h:45
Wrapper to configure the Khronos EGL and OpenGL ES C header.
static GCKernelLibrary & get()
Get the static instance of GCKernelLibrary.
constexpr bool empty() const
Check if the entire border is zero.
Definition: Types.h:300
static constexpr size_t DimZ
Alias for dimension 2 also known as Z dimension.
Definition: Window.h:47
Borders are left undefined.
Pixels outside the image are assumed to have the same value as the closest image pixel.
Common interface for all the GLES kernels.
Definition: IGCKernel.h:39
GCKernel create_kernel(const std::string &shader_name, const StringSet &build_options_set={}) const
Creates a kernel from the kernel library.
Window first_slice_window_3D() const
First 3D slice of the window.
Definition: Window.h:291
Describe a multidimensional execution window.
Definition: Window.h:39
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:367
Coordinates anchor
Anchor for the start of the valid region.
Definition: Types.h:260
bool is_parallelisable() const override
Indicates whether or not the kernel is parallelisable.
SimpleTensor< T > slice(const SimpleTensor< T > &src, Coordinates starts, Coordinates ends)