Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2016, 2017 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 | */ |
| 24 | #include "arm_compute/core/NEON/kernels/NEFillBorderKernel.h" |
| 25 | |
| 26 | #include "arm_compute/core/Error.h" |
| 27 | #include "arm_compute/core/Helpers.h" |
| 28 | #include "arm_compute/core/ITensor.h" |
| 29 | #include "arm_compute/core/TensorInfo.h" |
| 30 | #include "arm_compute/core/Validate.h" |
| 31 | #include "arm_compute/core/Window.h" |
| 32 | |
| 33 | #include <algorithm> |
| 34 | #include <cstdint> |
| 35 | |
Pablo Tello | 0c34fe2 | 2017-06-26 17:17:42 +0100 | [diff] [blame] | 36 | #if ARM_COMPUTE_ENABLE_FP16 |
| 37 | #include <arm_fp16.h> // needed for float16_t |
| 38 | #endif /* ARM_COMPUTE_ENABLE_FP16 */ |
| 39 | |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 40 | using namespace arm_compute; |
| 41 | |
Pablo Tello | 62eeae4 | 2017-08-09 16:33:49 +0100 | [diff] [blame^] | 42 | namespace |
| 43 | { |
| 44 | template <typename T, unsigned int leftx, unsigned int rightx> |
| 45 | void fill_constant_value_single_channel_special(ITensor *tensor, const Window &window, unsigned int right, unsigned int bottom, const PixelValue &constant_border_value); |
| 46 | |
| 47 | template <> |
| 48 | inline void fill_constant_value_single_channel_special<float, 1u, 1u>(ITensor *tensor, const Window &window, unsigned int right, unsigned int bottom, const PixelValue &constant_border_value) |
| 49 | { |
| 50 | float border_value; |
| 51 | constant_border_value.get(border_value); |
| 52 | uint8_t *const start_valid_region = tensor->ptr_to_element(tensor->info()->valid_region().anchor); |
| 53 | const size_t &width = tensor->info()->valid_region().shape[0]; |
| 54 | const size_t &height = tensor->info()->valid_region().shape[1]; |
| 55 | const int stridey = tensor->info()->strides_in_bytes()[1]; |
| 56 | |
| 57 | // Left and right border |
| 58 | Window vertical(window); |
| 59 | vertical.set(Window::DimY, Window::Dimension(0, height, 1)); |
| 60 | |
| 61 | Iterator vertical_it(tensor, vertical); |
| 62 | |
| 63 | execute_window_loop(vertical, [&](const Coordinates &) |
| 64 | { |
| 65 | const auto row_start = reinterpret_cast<float *>(start_valid_region + vertical_it.offset()); |
| 66 | |
| 67 | // Fill left and right borders |
| 68 | *(row_start - 1) = border_value; |
| 69 | std::fill_n(row_start + width, right, border_value); |
| 70 | }, |
| 71 | vertical_it); |
| 72 | |
| 73 | // Top and bottom border |
| 74 | Iterator plane_it(tensor, window); |
| 75 | |
| 76 | // Iterate over all XY planes |
| 77 | execute_window_loop(window, [&](const Coordinates &) |
| 78 | { |
| 79 | uint8_t *base_addr = start_valid_region + plane_it.offset(); |
| 80 | // Top border |
| 81 | const auto row_start = reinterpret_cast<float *>(base_addr - stridey); |
| 82 | // Fill top rows including left/right borders |
| 83 | std::fill_n(row_start - 1, 1 + width + right, border_value); |
| 84 | |
| 85 | // Bottom border |
| 86 | const unsigned low_border_size = height + bottom; |
| 87 | for(unsigned int i = height; i < low_border_size; ++i) |
| 88 | { |
| 89 | const auto row_start = reinterpret_cast<float *>(base_addr + i * stridey); |
| 90 | |
| 91 | // Fill bottom rows including left/right borders |
| 92 | std::fill_n(row_start - 1, 1 + width + right, border_value); |
| 93 | } |
| 94 | }, |
| 95 | plane_it); |
| 96 | } |
| 97 | } // namespace |
| 98 | |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 99 | namespace arm_compute |
| 100 | { |
| 101 | class Coordinates; |
| 102 | } // namespace arm_compute |
| 103 | |
| 104 | NEFillBorderKernel::NEFillBorderKernel() |
| 105 | : _tensor(nullptr), _border_size(0), _mode(BorderMode::UNDEFINED), _constant_border_value(0) |
| 106 | { |
| 107 | } |
| 108 | |
| 109 | void NEFillBorderKernel::configure(ITensor *tensor, BorderSize border_size, BorderMode border_mode, const PixelValue &constant_border_value) |
| 110 | { |
Pablo Tello | 0c34fe2 | 2017-06-26 17:17:42 +0100 | [diff] [blame] | 111 | ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(tensor, 1, DataType::U8, DataType::QS8, DataType::QS16, DataType::U16, DataType::S16, DataType::F16, DataType::U32, DataType::S32, DataType::F32); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 112 | |
| 113 | _tensor = tensor; |
| 114 | _border_size = border_size; |
| 115 | _mode = border_mode; |
| 116 | _constant_border_value = constant_border_value; |
| 117 | |
| 118 | _border_size.limit(tensor->info()->padding()); |
| 119 | |
| 120 | Window win; |
| 121 | win.set(Window::DimX, Window::Dimension(0, 1, 1)); |
| 122 | win.set(Window::DimY, Window::Dimension(0, 1, 1)); |
| 123 | win.use_tensor_dimensions(_tensor->info(), Window::DimZ); |
| 124 | INEKernel::configure(win); |
| 125 | } |
| 126 | |
| 127 | void NEFillBorderKernel::run(const Window &window) |
| 128 | { |
| 129 | // If there is no border: early exit |
| 130 | if(_border_size.empty()) |
| 131 | { |
| 132 | return; |
| 133 | } |
| 134 | |
| 135 | ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this); |
| 136 | ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(INEKernel::window(), window); |
| 137 | |
| 138 | switch(_mode) |
| 139 | { |
| 140 | case BorderMode::CONSTANT: |
| 141 | { |
| 142 | switch(_tensor->info()->data_type()) |
| 143 | { |
| 144 | case DataType::U8: |
| 145 | fill_constant_value_single_channel<uint8_t>(window); |
| 146 | break; |
| 147 | case DataType::QS8: |
| 148 | case DataType::S8: |
| 149 | fill_constant_value_single_channel<int8_t>(window); |
| 150 | break; |
| 151 | case DataType::U16: |
| 152 | fill_constant_value_single_channel<uint16_t>(window); |
| 153 | break; |
| 154 | case DataType::S16: |
| 155 | case DataType::QS16: |
| 156 | fill_constant_value_single_channel<int16_t>(window); |
| 157 | break; |
| 158 | case DataType::U32: |
| 159 | fill_constant_value_single_channel<uint32_t>(window); |
| 160 | break; |
| 161 | case DataType::S32: |
| 162 | fill_constant_value_single_channel<int32_t>(window); |
| 163 | break; |
Pablo Tello | 0c34fe2 | 2017-06-26 17:17:42 +0100 | [diff] [blame] | 164 | #ifdef ARM_COMPUTE_ENABLE_FP16 |
| 165 | case DataType::F16: |
| 166 | static_assert(sizeof(float16_t) == 2, "Float16_t must be 16 bit"); |
| 167 | fill_constant_value_single_channel<float16_t>(window); |
| 168 | break; |
| 169 | #endif /* ARM_COMPUTE_ENABLE_FP16 */ |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 170 | case DataType::F32: |
| 171 | static_assert(sizeof(float) == 4, "Float must be 32 bit"); |
Pablo Tello | 62eeae4 | 2017-08-09 16:33:49 +0100 | [diff] [blame^] | 172 | if(_border_size.left == 1 && _border_size.top == 1) |
| 173 | fill_constant_value_single_channel_special<float, 1u, 1u>(_tensor, window, _border_size.right, _border_size.bottom, _constant_border_value); |
| 174 | else |
| 175 | fill_constant_value_single_channel<float>(window); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 176 | break; |
| 177 | default: |
| 178 | ARM_COMPUTE_ERROR("Not handled"); |
| 179 | } |
| 180 | break; |
| 181 | } |
| 182 | case BorderMode::REPLICATE: |
| 183 | { |
| 184 | switch(_tensor->info()->data_type()) |
| 185 | { |
| 186 | case DataType::U8: |
| 187 | fill_replicate_single_channel<uint8_t>(window); |
| 188 | break; |
| 189 | case DataType::QS8: |
| 190 | case DataType::S8: |
| 191 | fill_replicate_single_channel<int8_t>(window); |
| 192 | break; |
| 193 | case DataType::U16: |
| 194 | fill_replicate_single_channel<uint16_t>(window); |
| 195 | break; |
| 196 | case DataType::S16: |
| 197 | case DataType::QS16: |
| 198 | fill_replicate_single_channel<int16_t>(window); |
| 199 | break; |
| 200 | case DataType::U32: |
| 201 | fill_replicate_single_channel<uint32_t>(window); |
| 202 | break; |
| 203 | case DataType::S32: |
| 204 | fill_replicate_single_channel<int32_t>(window); |
| 205 | break; |
Pablo Tello | 0c34fe2 | 2017-06-26 17:17:42 +0100 | [diff] [blame] | 206 | #ifdef ARM_COMPUTE_ENABLE_FP16 |
| 207 | case DataType::F16: |
| 208 | static_assert(sizeof(float16_t) == 2, "Float16_t must be 16 bit"); |
| 209 | fill_replicate_single_channel<float16_t>(window); |
| 210 | break; |
| 211 | #endif /* ARM_COMPUTE_ENABLE_FP16 */ |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 212 | case DataType::F32: |
| 213 | static_assert(sizeof(float) == 4, "Float must be 32 bit"); |
| 214 | fill_replicate_single_channel<float>(window); |
| 215 | break; |
| 216 | default: |
| 217 | ARM_COMPUTE_ERROR("Not handled"); |
| 218 | } |
| 219 | break; |
| 220 | } |
| 221 | case BorderMode::UNDEFINED: |
| 222 | break; // Nothing to do here |
| 223 | default: |
| 224 | ARM_COMPUTE_ERROR("Unknown border mode"); |
| 225 | } |
| 226 | } |
| 227 | |
| 228 | template <typename T> |
| 229 | void NEFillBorderKernel::fill_replicate_single_channel(const Window &window) |
| 230 | { |
| 231 | uint8_t *const start_valid_region = _tensor->ptr_to_element(_tensor->info()->valid_region().anchor); |
| 232 | const size_t &width = _tensor->info()->valid_region().shape[0]; |
| 233 | const size_t &height = _tensor->info()->valid_region().shape[1]; |
| 234 | |
| 235 | // Left and right border |
| 236 | Window vertical(window); |
| 237 | vertical.set(Window::DimY, Window::Dimension(0, height, 1)); |
| 238 | |
| 239 | Iterator vertical_it(_tensor, vertical); |
| 240 | |
| 241 | execute_window_loop(vertical, [&](const Coordinates & id) |
| 242 | { |
| 243 | const auto row_start = reinterpret_cast<T *>(start_valid_region + vertical_it.offset()); |
| 244 | const auto left_val = *reinterpret_cast<T *>(vertical_it.ptr()); |
| 245 | const auto right_val = *(reinterpret_cast<T *>(vertical_it.ptr()) + width - 1); |
| 246 | |
| 247 | // Fill left and right borders |
| 248 | std::fill_n(row_start - _border_size.left, _border_size.left, left_val); |
| 249 | std::fill_n(row_start + width, _border_size.right, right_val); |
| 250 | }, |
| 251 | vertical_it); |
| 252 | |
| 253 | // Top and bottom border |
| 254 | Iterator plane_it(_tensor, window); |
| 255 | |
| 256 | // Iterate over all XY planes |
| 257 | execute_window_loop(window, [&](const Coordinates & id) |
| 258 | { |
| 259 | const auto first_row = reinterpret_cast<T *>(start_valid_region + plane_it.offset()); |
| 260 | |
| 261 | // Top border |
| 262 | for(int i = -_border_size.top; i < 0; ++i) |
| 263 | { |
| 264 | const auto row_start = reinterpret_cast<T *>(start_valid_region + plane_it.offset() + i * _tensor->info()->strides_in_bytes()[1]); |
| 265 | |
| 266 | // Copy top rows including left/right borders |
| 267 | std::copy_n(first_row - _border_size.left, _border_size.left + width + _border_size.right, row_start - _border_size.left); |
| 268 | } |
| 269 | |
| 270 | const auto last_row = reinterpret_cast<T *>(start_valid_region + plane_it.offset() + (height - 1) * _tensor->info()->strides_in_bytes()[1]); |
| 271 | |
| 272 | // Bottom border |
| 273 | for(unsigned int i = height; i < height + _border_size.bottom; ++i) |
| 274 | { |
| 275 | const auto row_start = reinterpret_cast<T *>(start_valid_region + plane_it.offset() + i * _tensor->info()->strides_in_bytes()[1]); |
| 276 | |
| 277 | // Copy bottom rows including left/right borders |
| 278 | std::copy_n(last_row - _border_size.left, _border_size.left + width + _border_size.right, row_start - _border_size.left); |
| 279 | } |
| 280 | }, |
| 281 | plane_it); |
| 282 | } |
| 283 | |
| 284 | template <typename T> |
| 285 | void NEFillBorderKernel::fill_constant_value_single_channel(const Window &window) |
| 286 | { |
| 287 | T constant_border_value; |
| 288 | _constant_border_value.get(constant_border_value); |
| 289 | |
| 290 | uint8_t *const start_valid_region = _tensor->ptr_to_element(_tensor->info()->valid_region().anchor); |
| 291 | const size_t &width = _tensor->info()->valid_region().shape[0]; |
| 292 | const size_t &height = _tensor->info()->valid_region().shape[1]; |
Pablo Tello | 62eeae4 | 2017-08-09 16:33:49 +0100 | [diff] [blame^] | 293 | const int stridey = _tensor->info()->strides_in_bytes()[1]; |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 294 | |
| 295 | // Left and right border |
| 296 | Window vertical(window); |
| 297 | vertical.set(Window::DimY, Window::Dimension(0, height, 1)); |
| 298 | |
| 299 | Iterator vertical_it(_tensor, vertical); |
| 300 | |
| 301 | execute_window_loop(vertical, [&](const Coordinates & id) |
| 302 | { |
| 303 | const auto row_start = reinterpret_cast<T *>(start_valid_region + vertical_it.offset()); |
| 304 | |
| 305 | // Fill left and right borders |
| 306 | std::fill_n(row_start - _border_size.left, _border_size.left, constant_border_value); |
| 307 | std::fill_n(row_start + width, _border_size.right, constant_border_value); |
| 308 | }, |
| 309 | vertical_it); |
| 310 | |
| 311 | // Top and bottom border |
| 312 | Iterator plane_it(_tensor, window); |
| 313 | |
| 314 | // Iterate over all XY planes |
| 315 | execute_window_loop(window, [&](const Coordinates & id) |
| 316 | { |
Pablo Tello | 62eeae4 | 2017-08-09 16:33:49 +0100 | [diff] [blame^] | 317 | uint8_t *base_addr = start_valid_region + plane_it.offset(); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 318 | // Top border |
| 319 | for(int i = -_border_size.top; i < 0; ++i) |
| 320 | { |
Pablo Tello | 62eeae4 | 2017-08-09 16:33:49 +0100 | [diff] [blame^] | 321 | const auto row_start = reinterpret_cast<T *>(base_addr + i * stridey); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 322 | |
| 323 | // Fill top rows including left/right borders |
| 324 | std::fill_n(row_start - _border_size.left, _border_size.left + width + _border_size.right, constant_border_value); |
| 325 | } |
| 326 | |
| 327 | // Bottom border |
Pablo Tello | 62eeae4 | 2017-08-09 16:33:49 +0100 | [diff] [blame^] | 328 | const unsigned low_border_size = height + _border_size.bottom; |
| 329 | for(unsigned int i = height; i < low_border_size; ++i) |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 330 | { |
Pablo Tello | 62eeae4 | 2017-08-09 16:33:49 +0100 | [diff] [blame^] | 331 | const auto row_start = reinterpret_cast<T *>(base_addr + i * stridey); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 332 | |
| 333 | // Fill bottom rows including left/right borders |
| 334 | std::fill_n(row_start - _border_size.left, _border_size.left + width + _border_size.right, constant_border_value); |
| 335 | } |
| 336 | }, |
| 337 | plane_it); |
| 338 | } |