Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 1 | /* |
Michele Di Giorgio | d9eaf61 | 2020-07-08 11:12:57 +0100 | [diff] [blame] | 2 | * Copyright (c) 2016-2020 Arm Limited. |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 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 | */ |
| 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" |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 30 | #include "arm_compute/core/TensorInfo.h" |
| 31 | #include "arm_compute/core/Utils.h" |
| 32 | #include "arm_compute/core/Validate.h" |
Sang-Hoon Park | 68dd25f | 2020-10-19 16:00:11 +0100 | [diff] [blame] | 33 | #include "src/core/helpers/WindowHelpers.h" |
| 34 | #include "support/Cast.h" |
Matthew Bentham | 758b5ba | 2020-03-05 23:37:48 +0000 | [diff] [blame] | 35 | #include "support/StringSupport.h" |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 36 | |
Michele Di Giorgio | f6f7876 | 2020-07-06 11:27:21 +0100 | [diff] [blame] | 37 | namespace arm_compute |
| 38 | { |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 39 | CLFillBorderKernel::CLFillBorderKernel() |
| 40 | : ICLKernel(), _tensor(nullptr) |
| 41 | { |
| 42 | } |
| 43 | |
| 44 | bool CLFillBorderKernel::is_parallelisable() const |
| 45 | { |
| 46 | return false; |
| 47 | } |
| 48 | |
| 49 | template <class T> |
| 50 | void CLFillBorderKernel::set_constant_border(unsigned int idx, const PixelValue &constant_border_value) |
| 51 | { |
| 52 | T value; |
| 53 | constant_border_value.get(value); |
| 54 | ICLKernel::add_argument<T>(idx, static_cast<T>(value)); |
| 55 | } |
| 56 | |
| 57 | void CLFillBorderKernel::configure(ICLTensor *tensor, BorderSize border_size, BorderMode border_mode, const PixelValue &constant_border_value) |
| 58 | { |
Manuel Bottini | 4c6bd51 | 2020-04-08 10:15:51 +0100 | [diff] [blame] | 59 | configure(CLKernelLibrary::get().get_compile_context(), tensor, border_size, border_mode, constant_border_value); |
| 60 | } |
| 61 | |
Manuel Bottini | 256c0b9 | 2020-04-21 13:29:30 +0100 | [diff] [blame] | 62 | void CLFillBorderKernel::configure(const CLCompileContext &compile_context, ICLTensor *tensor, BorderSize border_size, BorderMode border_mode, const PixelValue &constant_border_value) |
Manuel Bottini | 4c6bd51 | 2020-04-08 10:15:51 +0100 | [diff] [blame] | 63 | { |
Michalis Spyrou | ad7515d | 2020-07-24 00:02:23 +0100 | [diff] [blame] | 64 | _tensor = tensor; |
| 65 | configure(compile_context, tensor->info(), border_size, border_mode, constant_border_value); |
| 66 | } |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 67 | |
Michalis Spyrou | ad7515d | 2020-07-24 00:02:23 +0100 | [diff] [blame] | 68 | void CLFillBorderKernel::configure(const CLCompileContext &compile_context, ITensorInfo *tensor, BorderSize border_size, BorderMode border_mode, const PixelValue &constant_border_value) |
| 69 | { |
| 70 | ARM_COMPUTE_ERROR_ON(tensor == nullptr); |
| 71 | ARM_COMPUTE_ERROR_ON(tensor->num_channels() != 1); |
| 72 | |
| 73 | border_size.limit(tensor->padding()); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 74 | |
| 75 | // If there is no border: early exit |
| 76 | if(border_size.empty() || border_mode == BorderMode::UNDEFINED) |
| 77 | { |
| 78 | return; |
| 79 | } |
| 80 | |
| 81 | // Select appropriate kernel |
| 82 | std::string kernel_name = "fill_image_borders_" + lower_string(string_from_border_mode(border_mode)); |
| 83 | |
Michalis Spyrou | ad7515d | 2020-07-24 00:02:23 +0100 | [diff] [blame] | 84 | const DataType dt = tensor->data_type(); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 85 | |
| 86 | // Define build options |
Manuel Bottini | b412fab | 2018-12-10 17:40:23 +0000 | [diff] [blame] | 87 | CLBuildOptions build_opts; |
Michele Di Giorgio | f6f7876 | 2020-07-06 11:27:21 +0100 | [diff] [blame] | 88 | build_opts.add_option("-DDATA_TYPE=" + get_cl_type_from_data_type(dt)); |
Manuel Bottini | b412fab | 2018-12-10 17:40:23 +0000 | [diff] [blame] | 89 | build_opts.add_option("-DBORDER_SIZE_TOP=" + support::cpp11::to_string(border_size.top)); |
| 90 | build_opts.add_option("-DBORDER_SIZE_BOTTOM=" + support::cpp11::to_string(border_size.bottom)); |
| 91 | build_opts.add_option("-DBORDER_SIZE_LEFT=" + support::cpp11::to_string(border_size.left)); |
| 92 | build_opts.add_option("-DBORDER_SIZE_RIGHT=" + support::cpp11::to_string(border_size.right)); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 93 | |
| 94 | // Create kernel |
Manuel Bottini | 4c6bd51 | 2020-04-08 10:15:51 +0100 | [diff] [blame] | 95 | _kernel = create_kernel(compile_context, kernel_name, build_opts.options()); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 96 | |
| 97 | // Create static kernel arguments |
Michalis Spyrou | ad7515d | 2020-07-24 00:02:23 +0100 | [diff] [blame] | 98 | const unsigned int valid_width = tensor->valid_region().shape[0]; |
| 99 | const unsigned int valid_height = tensor->valid_region().shape[1]; |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 100 | const cl_int2 valid_region_coords = |
| 101 | { |
| 102 | { |
Michalis Spyrou | ad7515d | 2020-07-24 00:02:23 +0100 | [diff] [blame] | 103 | static_cast<cl_int>(tensor->valid_region().anchor[0]), |
| 104 | static_cast<cl_int>(tensor->valid_region().anchor[1]), |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 105 | } |
| 106 | }; |
| 107 | const unsigned int total_valid_width = border_size.left + valid_width + border_size.right; |
| 108 | |
| 109 | // Set static kernel arguments |
Anthony Barbier | 7ff47a3 | 2017-07-11 16:54:04 +0100 | [diff] [blame] | 110 | unsigned int idx = num_arguments_per_3D_tensor(); //Skip the tensor parameters |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 111 | ICLKernel::add_argument<cl_uint>(idx, valid_width); |
| 112 | ICLKernel::add_argument<cl_uint>(idx, valid_height); |
| 113 | ICLKernel::add_argument<cl_int2>(idx, valid_region_coords); |
| 114 | if(BorderMode::CONSTANT == border_mode) |
| 115 | { |
| 116 | switch(dt) |
| 117 | { |
| 118 | case DataType::U8: |
Chunosov | d621bca | 2017-11-03 17:33:15 +0700 | [diff] [blame] | 119 | case DataType::QASYMM8: |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 120 | set_constant_border<uint8_t>(idx, constant_border_value); |
| 121 | break; |
Georgios Pinitas | ac4e873 | 2017-07-05 17:02:25 +0100 | [diff] [blame] | 122 | case DataType::S8: |
Manuel Bottini | 8481d83 | 2019-12-10 15:28:40 +0000 | [diff] [blame] | 123 | case DataType::QASYMM8_SIGNED: |
Georgios Pinitas | ac4e873 | 2017-07-05 17:02:25 +0100 | [diff] [blame] | 124 | set_constant_border<int8_t>(idx, constant_border_value); |
| 125 | break; |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 126 | case DataType::U16: |
| 127 | set_constant_border<uint16_t>(idx, constant_border_value); |
| 128 | break; |
| 129 | case DataType::S16: |
| 130 | set_constant_border<int16_t>(idx, constant_border_value); |
| 131 | break; |
| 132 | case DataType::U32: |
| 133 | set_constant_border<uint32_t>(idx, constant_border_value); |
| 134 | break; |
| 135 | case DataType::S32: |
| 136 | set_constant_border<int32_t>(idx, constant_border_value); |
| 137 | break; |
| 138 | case DataType::F32: |
| 139 | static_assert(sizeof(float) == 4, "Float must be 32 bit"); |
| 140 | set_constant_border<float>(idx, constant_border_value); |
| 141 | break; |
| 142 | case DataType::F16: |
Manuel Bottini | b412fab | 2018-12-10 17:40:23 +0000 | [diff] [blame] | 143 | static_assert(sizeof(cl_half) == sizeof(half), "Half must be same size as cl_half"); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 144 | static_assert(sizeof(cl_half) == 2, "Half must be 16 bit"); |
Manuel Bottini | b412fab | 2018-12-10 17:40:23 +0000 | [diff] [blame] | 145 | set_constant_border<half>(idx, constant_border_value); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 146 | break; |
| 147 | default: |
| 148 | ARM_COMPUTE_ERROR("Not handled"); |
| 149 | } |
| 150 | } |
| 151 | |
| 152 | // Configure kernel window |
| 153 | Window win; |
| 154 | win.set(Window::DimX, Window::Dimension(0, total_valid_width + valid_height)); |
| 155 | win.set(Window::DimY, Window::Dimension(0, 1, 1)); |
Michalis Spyrou | ad7515d | 2020-07-24 00:02:23 +0100 | [diff] [blame] | 156 | win.use_tensor_dimensions(tensor->tensor_shape(), Window::DimZ); |
Anthony Barbier | b6eb353 | 2018-08-08 13:20:04 +0100 | [diff] [blame] | 157 | ICLKernel::configure_internal(win); |
Gary Antcliffe | fffbdbc | 2019-05-28 11:40:21 +0100 | [diff] [blame] | 158 | |
| 159 | // Set config_id for enabling LWS tuning |
| 160 | _config_id = kernel_name; |
| 161 | _config_id += "_"; |
| 162 | _config_id += lower_string(string_from_data_type(dt)); |
| 163 | _config_id += "_"; |
Michalis Spyrou | ad7515d | 2020-07-24 00:02:23 +0100 | [diff] [blame] | 164 | _config_id += support::cpp11::to_string(tensor->dimension(0)); |
Gary Antcliffe | fffbdbc | 2019-05-28 11:40:21 +0100 | [diff] [blame] | 165 | _config_id += "_"; |
Michalis Spyrou | ad7515d | 2020-07-24 00:02:23 +0100 | [diff] [blame] | 166 | _config_id += support::cpp11::to_string(tensor->dimension(1)); |
Gary Antcliffe | fffbdbc | 2019-05-28 11:40:21 +0100 | [diff] [blame] | 167 | _config_id += "_"; |
| 168 | _config_id += lower_string(string_from_border_mode(border_mode)); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 169 | } |
| 170 | |
Georgios Pinitas | 0499dff | 2020-07-31 22:21:38 +0100 | [diff] [blame] | 171 | void CLFillBorderKernel::run_op(ITensorPack &tensors, const Window &window, cl::CommandQueue &queue) |
Michalis Spyrou | ad7515d | 2020-07-24 00:02:23 +0100 | [diff] [blame] | 172 | { |
Michalis Spyrou | ad7515d | 2020-07-24 00:02:23 +0100 | [diff] [blame] | 173 | // Border mode undefined or border width == 0 |
| 174 | if(_kernel() == nullptr) |
| 175 | { |
| 176 | return; |
| 177 | } |
| 178 | |
Georgios Pinitas | 0499dff | 2020-07-31 22:21:38 +0100 | [diff] [blame] | 179 | const auto tensor = utils::cast::polymorphic_downcast<const ICLTensor *>(tensors.get_const_tensor(TensorType::ACL_SRC)); |
Michalis Spyrou | ad7515d | 2020-07-24 00:02:23 +0100 | [diff] [blame] | 180 | |
| 181 | ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this); |
| 182 | ARM_COMPUTE_ERROR_ON_MISMATCHING_WINDOWS(ICLKernel::window(), window); |
| 183 | |
| 184 | Window collapsed = window.collapse_if_possible(ICLKernel::window(), Window::DimZ); |
| 185 | Window slice = collapsed.first_slice_window_3D(); |
| 186 | |
| 187 | do |
| 188 | { |
| 189 | unsigned int idx = 0; |
| 190 | add_3D_tensor_argument(idx, tensor, slice); |
| 191 | enqueue(queue, *this, slice, lws_hint()); |
| 192 | } |
| 193 | while(collapsed.slide_window_slice_3D(slice)); |
| 194 | } |
| 195 | |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 196 | void CLFillBorderKernel::run(const Window &window, cl::CommandQueue &queue) |
| 197 | { |
| 198 | // Border mode undefined or border width == 0 |
| 199 | if(_kernel() == nullptr) |
| 200 | { |
| 201 | return; |
| 202 | } |
| 203 | |
| 204 | ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this); |
| 205 | ARM_COMPUTE_ERROR_ON_MISMATCHING_WINDOWS(ICLKernel::window(), window); |
| 206 | |
Georgios Pinitas | e55b40a | 2018-09-13 17:20:04 +0100 | [diff] [blame] | 207 | Window collapsed = window.collapse_if_possible(ICLKernel::window(), Window::DimZ); |
| 208 | Window slice = collapsed.first_slice_window_3D(); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 209 | |
| 210 | do |
| 211 | { |
| 212 | unsigned int idx = 0; |
Anthony Barbier | 7ff47a3 | 2017-07-11 16:54:04 +0100 | [diff] [blame] | 213 | add_3D_tensor_argument(idx, _tensor, slice); |
Georgios Pinitas | 275f99c | 2019-08-23 12:44:11 +0100 | [diff] [blame] | 214 | enqueue(queue, *this, slice, lws_hint()); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 215 | } |
Georgios Pinitas | e55b40a | 2018-09-13 17:20:04 +0100 | [diff] [blame] | 216 | while(collapsed.slide_window_slice_3D(slice)); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 217 | } |
Michele Di Giorgio | f6f7876 | 2020-07-06 11:27:21 +0100 | [diff] [blame] | 218 | } // namespace arm_compute |