blob: 5fdb826f8b84fcdb5360ebd77be2f3f628c5183e [file] [log] [blame]
Anthony Barbier6ff3b192017-09-04 18:44:23 +01001/*
Manuel Bottinib412fab2018-12-10 17:40:23 +00002 * Copyright (c) 2016-2019 ARM Limited.
Anthony Barbier6ff3b192017-09-04 18:44:23 +01003 *
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 */
24#include "arm_compute/core/CL/kernels/CLFillBorderKernel.h"
25
26#include "arm_compute/core/CL/CLHelpers.h"
27#include "arm_compute/core/CL/CLKernelLibrary.h"
28#include "arm_compute/core/CL/ICLTensor.h"
29#include "arm_compute/core/CL/OpenCL.h"
30#include "arm_compute/core/Error.h"
31#include "arm_compute/core/TensorInfo.h"
Georgios Pinitas583137c2017-08-31 18:12:42 +010032#include "arm_compute/core/Types.h"
Anthony Barbier6ff3b192017-09-04 18:44:23 +010033#include "arm_compute/core/Utils.h"
34#include "arm_compute/core/Validate.h"
35#include "arm_compute/core/Window.h"
36
37#include <cstdint>
38#include <set>
39#include <sstream>
40#include <string>
41
42using namespace arm_compute;
43
44CLFillBorderKernel::CLFillBorderKernel()
45 : ICLKernel(), _tensor(nullptr)
46{
47}
48
49bool CLFillBorderKernel::is_parallelisable() const
50{
51 return false;
52}
53
54template <class T>
55void 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
62void 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
70 if(border_size.empty() || border_mode == BorderMode::UNDEFINED)
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
Manuel Bottinib412fab2018-12-10 17:40:23 +000078 const DataType dt = tensor->info()->data_type();
Anthony Barbier6ff3b192017-09-04 18:44:23 +010079
80 // Define build options
Manuel Bottinib412fab2018-12-10 17:40:23 +000081 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));
Anthony Barbier6ff3b192017-09-04 18:44:23 +010087
88 // Create kernel
Manuel Bottinib412fab2018-12-10 17:40:23 +000089 _kernel = static_cast<cl::Kernel>(CLKernelLibrary::get().create_kernel(kernel_name, build_opts.options()));
Anthony Barbier6ff3b192017-09-04 18:44:23 +010090 _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
Anthony Barbier7ff47a32017-07-11 16:54:04 +0100105 unsigned int idx = num_arguments_per_3D_tensor(); //Skip the tensor parameters
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100106 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);
109 if(BorderMode::CONSTANT == border_mode)
110 {
111 switch(dt)
112 {
113 case DataType::U8:
Chunosovd621bca2017-11-03 17:33:15 +0700114 case DataType::QASYMM8:
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100115 set_constant_border<uint8_t>(idx, constant_border_value);
116 break;
Georgios Pinitasac4e8732017-07-05 17:02:25 +0100117 case DataType::S8:
118 set_constant_border<int8_t>(idx, constant_border_value);
119 break;
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100120 case DataType::U16:
121 set_constant_border<uint16_t>(idx, constant_border_value);
122 break;
123 case DataType::S16:
124 set_constant_border<int16_t>(idx, constant_border_value);
125 break;
126 case DataType::U32:
127 set_constant_border<uint32_t>(idx, constant_border_value);
128 break;
129 case DataType::S32:
130 set_constant_border<int32_t>(idx, constant_border_value);
131 break;
132 case DataType::F32:
133 static_assert(sizeof(float) == 4, "Float must be 32 bit");
134 set_constant_border<float>(idx, constant_border_value);
135 break;
136 case DataType::F16:
Manuel Bottinib412fab2018-12-10 17:40:23 +0000137 static_assert(sizeof(cl_half) == sizeof(half), "Half must be same size as cl_half");
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100138 static_assert(sizeof(cl_half) == 2, "Half must be 16 bit");
Manuel Bottinib412fab2018-12-10 17:40:23 +0000139 set_constant_border<half>(idx, constant_border_value);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100140 break;
141 default:
142 ARM_COMPUTE_ERROR("Not handled");
143 }
144 }
145
146 // Configure kernel window
147 Window win;
148 win.set(Window::DimX, Window::Dimension(0, total_valid_width + valid_height));
149 win.set(Window::DimY, Window::Dimension(0, 1, 1));
SiCong Li86b53332017-08-23 11:02:43 +0100150 win.use_tensor_dimensions(tensor->info()->tensor_shape(), Window::DimZ);
Anthony Barbierb6eb3532018-08-08 13:20:04 +0100151 ICLKernel::configure_internal(win);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100152}
153
154void CLFillBorderKernel::run(const Window &window, cl::CommandQueue &queue)
155{
156 // Border mode undefined or border width == 0
157 if(_kernel() == nullptr)
158 {
159 return;
160 }
161
162 ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this);
163 ARM_COMPUTE_ERROR_ON_MISMATCHING_WINDOWS(ICLKernel::window(), window);
164
Georgios Pinitase55b40a2018-09-13 17:20:04 +0100165 Window collapsed = window.collapse_if_possible(ICLKernel::window(), Window::DimZ);
166 Window slice = collapsed.first_slice_window_3D();
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100167
168 do
169 {
170 unsigned int idx = 0;
Anthony Barbier7ff47a32017-07-11 16:54:04 +0100171 add_3D_tensor_argument(idx, _tensor, slice);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100172 enqueue(queue, *this, slice, cl::NullRange);
173 }
Georgios Pinitase55b40a2018-09-13 17:20:04 +0100174 while(collapsed.slide_window_slice_3D(slice));
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100175}