Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 1 | /* |
Manuel Bottini | b412fab | 2018-12-10 17:40:23 +0000 | [diff] [blame] | 2 | * Copyright (c) 2016-2019 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/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" |
Georgios Pinitas | 583137c | 2017-08-31 18:12:42 +0100 | [diff] [blame] | 30 | #include "arm_compute/core/Types.h" |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 31 | #include "arm_compute/core/Validate.h" |
| 32 | #include "arm_compute/core/Window.h" |
| 33 | |
| 34 | #include <algorithm> |
| 35 | #include <cstdint> |
| 36 | |
Michalis Spyrou | 95abfdd | 2018-11-28 14:59:47 +0000 | [diff] [blame] | 37 | namespace arm_compute |
| 38 | { |
| 39 | class Coordinates; |
Pablo Tello | 62eeae4 | 2017-08-09 16:33:49 +0100 | [diff] [blame] | 40 | namespace |
| 41 | { |
Michalis Spyrou | 95abfdd | 2018-11-28 14:59:47 +0000 | [diff] [blame] | 42 | inline void fill_constant_value_single_channel_special(ITensor *tensor, const Window &window, unsigned int right, unsigned int bottom, const PixelValue &constant_border_value) |
Pablo Tello | 62eeae4 | 2017-08-09 16:33:49 +0100 | [diff] [blame] | 43 | { |
| 44 | float border_value; |
| 45 | constant_border_value.get(border_value); |
| 46 | uint8_t *const start_valid_region = tensor->ptr_to_element(tensor->info()->valid_region().anchor); |
Georgios Pinitas | 0223a78 | 2017-12-12 11:44:44 +0000 | [diff] [blame] | 47 | const size_t width = tensor->info()->valid_region().shape[0]; |
| 48 | const size_t height = tensor->info()->valid_region().shape[1]; |
Pablo Tello | 62eeae4 | 2017-08-09 16:33:49 +0100 | [diff] [blame] | 49 | const int stridey = tensor->info()->strides_in_bytes()[1]; |
| 50 | |
| 51 | // Left and right border |
| 52 | Window vertical(window); |
| 53 | vertical.set(Window::DimY, Window::Dimension(0, height, 1)); |
| 54 | |
| 55 | Iterator vertical_it(tensor, vertical); |
| 56 | |
| 57 | execute_window_loop(vertical, [&](const Coordinates &) |
| 58 | { |
| 59 | const auto row_start = reinterpret_cast<float *>(start_valid_region + vertical_it.offset()); |
| 60 | |
| 61 | // Fill left and right borders |
| 62 | *(row_start - 1) = border_value; |
| 63 | std::fill_n(row_start + width, right, border_value); |
| 64 | }, |
| 65 | vertical_it); |
| 66 | |
| 67 | // Top and bottom border |
| 68 | Iterator plane_it(tensor, window); |
| 69 | |
| 70 | // Iterate over all XY planes |
| 71 | execute_window_loop(window, [&](const Coordinates &) |
| 72 | { |
| 73 | uint8_t *base_addr = start_valid_region + plane_it.offset(); |
| 74 | // Top border |
| 75 | const auto row_start = reinterpret_cast<float *>(base_addr - stridey); |
| 76 | // Fill top rows including left/right borders |
| 77 | std::fill_n(row_start - 1, 1 + width + right, border_value); |
| 78 | |
| 79 | // Bottom border |
| 80 | const unsigned low_border_size = height + bottom; |
| 81 | for(unsigned int i = height; i < low_border_size; ++i) |
| 82 | { |
| 83 | const auto row_start = reinterpret_cast<float *>(base_addr + i * stridey); |
| 84 | |
| 85 | // Fill bottom rows including left/right borders |
| 86 | std::fill_n(row_start - 1, 1 + width + right, border_value); |
| 87 | } |
| 88 | }, |
| 89 | plane_it); |
| 90 | } |
| 91 | } // namespace |
| 92 | |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 93 | NEFillBorderKernel::NEFillBorderKernel() |
Michalis Spyrou | 490bf2e | 2017-09-29 11:24:55 +0100 | [diff] [blame] | 94 | : _tensor(nullptr), _border_size(0), _mode(BorderMode::UNDEFINED), _constant_border_value(static_cast<float>(0.f)) |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 95 | { |
| 96 | } |
| 97 | |
| 98 | void NEFillBorderKernel::configure(ITensor *tensor, BorderSize border_size, BorderMode border_mode, const PixelValue &constant_border_value) |
| 99 | { |
Anthony Barbier | eaefd00 | 2018-07-20 17:49:35 +0100 | [diff] [blame] | 100 | //Note: ARM_COMPUTE_RETURN_ERROR_ON_CPU_F16_UNSUPPORTED(input) is not needed here as this kernel doesn't use NEON FP16 instructions. |
Vidhya Sudhan Loganathan | 7485d5a | 2018-07-04 09:34:00 +0100 | [diff] [blame] | 101 | ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(tensor, 1, DataType::U8, DataType::QASYMM8, |
| 102 | DataType::U16, DataType::S16, |
Georgios Pinitas | 5518671 | 2018-01-08 17:37:12 +0000 | [diff] [blame] | 103 | DataType::U32, DataType::S32, |
| 104 | DataType::F16, DataType::F32); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 105 | |
| 106 | _tensor = tensor; |
| 107 | _border_size = border_size; |
| 108 | _mode = border_mode; |
| 109 | _constant_border_value = constant_border_value; |
| 110 | |
| 111 | _border_size.limit(tensor->info()->padding()); |
| 112 | |
| 113 | Window win; |
| 114 | win.set(Window::DimX, Window::Dimension(0, 1, 1)); |
| 115 | win.set(Window::DimY, Window::Dimension(0, 1, 1)); |
SiCong Li | 86b5333 | 2017-08-23 11:02:43 +0100 | [diff] [blame] | 116 | win.use_tensor_dimensions(_tensor->info()->tensor_shape(), Window::DimZ); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 117 | INEKernel::configure(win); |
| 118 | } |
| 119 | |
Moritz Pflanzer | c186b57 | 2017-09-07 09:48:04 +0100 | [diff] [blame] | 120 | void NEFillBorderKernel::run(const Window &window, const ThreadInfo &info) |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 121 | { |
Moritz Pflanzer | c186b57 | 2017-09-07 09:48:04 +0100 | [diff] [blame] | 122 | ARM_COMPUTE_UNUSED(info); |
| 123 | |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 124 | // If there is no border: early exit |
| 125 | if(_border_size.empty()) |
| 126 | { |
| 127 | return; |
| 128 | } |
| 129 | |
| 130 | ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this); |
| 131 | ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(INEKernel::window(), window); |
| 132 | |
| 133 | switch(_mode) |
| 134 | { |
| 135 | case BorderMode::CONSTANT: |
| 136 | { |
Michalis Spyrou | 95abfdd | 2018-11-28 14:59:47 +0000 | [diff] [blame] | 137 | if(_border_size.left == 1 && _border_size.top == 1 && _tensor->info()->data_type() == DataType::F32) |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 138 | { |
Michalis Spyrou | 95abfdd | 2018-11-28 14:59:47 +0000 | [diff] [blame] | 139 | fill_constant_value_single_channel_special(_tensor, window, _border_size.right, _border_size.bottom, _constant_border_value); |
| 140 | } |
| 141 | else |
| 142 | { |
| 143 | fill_constant_value_single_channel(window); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 144 | } |
| 145 | break; |
| 146 | } |
| 147 | case BorderMode::REPLICATE: |
| 148 | { |
Michalis Spyrou | 95abfdd | 2018-11-28 14:59:47 +0000 | [diff] [blame] | 149 | fill_replicate_single_channel(window); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 150 | break; |
| 151 | } |
| 152 | case BorderMode::UNDEFINED: |
| 153 | break; // Nothing to do here |
| 154 | default: |
| 155 | ARM_COMPUTE_ERROR("Unknown border mode"); |
| 156 | } |
| 157 | } |
| 158 | |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 159 | void NEFillBorderKernel::fill_replicate_single_channel(const Window &window) |
| 160 | { |
| 161 | uint8_t *const start_valid_region = _tensor->ptr_to_element(_tensor->info()->valid_region().anchor); |
Georgios Pinitas | 424eb5d | 2017-12-06 19:49:38 +0000 | [diff] [blame] | 162 | const size_t width = _tensor->info()->valid_region().shape[0]; |
| 163 | const size_t height = _tensor->info()->valid_region().shape[1]; |
Michalis Spyrou | 95abfdd | 2018-11-28 14:59:47 +0000 | [diff] [blame] | 164 | const size_t element_size = _tensor->info()->element_size(); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 165 | // Left and right border |
| 166 | Window vertical(window); |
| 167 | vertical.set(Window::DimY, Window::Dimension(0, height, 1)); |
| 168 | |
| 169 | Iterator vertical_it(_tensor, vertical); |
| 170 | |
| 171 | execute_window_loop(vertical, [&](const Coordinates & id) |
| 172 | { |
Michalis Spyrou | 95abfdd | 2018-11-28 14:59:47 +0000 | [diff] [blame] | 173 | uint8_t *base_addr = start_valid_region + vertical_it.offset(); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 174 | // Fill left and right borders |
Michalis Spyrou | 95abfdd | 2018-11-28 14:59:47 +0000 | [diff] [blame] | 175 | for(unsigned int i = 0; i < _border_size.left; ++i) |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 176 | { |
Michalis Spyrou | 95abfdd | 2018-11-28 14:59:47 +0000 | [diff] [blame] | 177 | std::memcpy(base_addr + static_cast<int>(i - _border_size.left) * element_size, vertical_it.ptr(), element_size); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 178 | } |
| 179 | |
Michalis Spyrou | 95abfdd | 2018-11-28 14:59:47 +0000 | [diff] [blame] | 180 | for(unsigned int i = 0; i < _border_size.right; ++i) |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 181 | { |
Michalis Spyrou | 95abfdd | 2018-11-28 14:59:47 +0000 | [diff] [blame] | 182 | std::memcpy(base_addr + (width + i) * element_size, vertical_it.ptr() + (width - 1) * element_size, element_size); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 183 | } |
| 184 | }, |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 185 | vertical_it); |
| 186 | |
| 187 | // Top and bottom border |
| 188 | Iterator plane_it(_tensor, window); |
| 189 | |
| 190 | // Iterate over all XY planes |
| 191 | execute_window_loop(window, [&](const Coordinates & id) |
| 192 | { |
Pablo Tello | 62eeae4 | 2017-08-09 16:33:49 +0100 | [diff] [blame] | 193 | uint8_t *base_addr = start_valid_region + plane_it.offset(); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 194 | // Top border |
| 195 | for(int i = -_border_size.top; i < 0; ++i) |
| 196 | { |
Michalis Spyrou | 95abfdd | 2018-11-28 14:59:47 +0000 | [diff] [blame] | 197 | // Copy top rows including left/right borders |
| 198 | std::memcpy(base_addr + i * _tensor->info()->strides_in_bytes()[1] - _border_size.left * element_size, |
| 199 | base_addr - _border_size.left * element_size, (_border_size.left + width + _border_size.right) * element_size); |
| 200 | } |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 201 | |
Michalis Spyrou | 95abfdd | 2018-11-28 14:59:47 +0000 | [diff] [blame] | 202 | // Bottom border |
| 203 | for(unsigned int i = height; i < height + _border_size.bottom; ++i) |
| 204 | { |
| 205 | // Copy bottom rows including left/right borders |
| 206 | std::memcpy(base_addr + i * _tensor->info()->strides_in_bytes()[1] - _border_size.left * element_size, |
| 207 | base_addr + (height - 1) * _tensor->info()->strides_in_bytes()[1] - _border_size.left * element_size, (_border_size.left + width + _border_size.right) * element_size); |
| 208 | } |
| 209 | }, |
| 210 | plane_it); |
| 211 | } |
| 212 | |
| 213 | void NEFillBorderKernel::fill_constant_value_single_channel(const Window &window) |
| 214 | { |
| 215 | uint8_t *const start_valid_region = _tensor->ptr_to_element(_tensor->info()->valid_region().anchor); |
| 216 | const size_t width = _tensor->info()->valid_region().shape[0]; |
| 217 | const size_t height = _tensor->info()->valid_region().shape[1]; |
| 218 | const int stridey = _tensor->info()->strides_in_bytes()[1]; |
| 219 | const size_t element_size = _tensor->info()->element_size(); |
| 220 | |
| 221 | // Left and right border |
| 222 | Window vertical(window); |
| 223 | vertical.set(Window::DimY, Window::Dimension(0, height, 1)); |
| 224 | |
| 225 | Iterator vertical_it(_tensor, vertical); |
| 226 | |
| 227 | execute_window_loop(vertical, [&](const Coordinates & id) |
| 228 | { |
| 229 | uint8_t *base_addr = start_valid_region + vertical_it.offset(); |
| 230 | // Fill left and right borders |
| 231 | for(unsigned int i = 0; i < _border_size.left; ++i) |
| 232 | { |
| 233 | std::memcpy(base_addr + static_cast<int>(i - _border_size.left) * element_size, &_constant_border_value, element_size); |
| 234 | } |
| 235 | |
| 236 | for(unsigned int i = 0; i < _border_size.right; ++i) |
| 237 | { |
| 238 | std::memcpy(base_addr + (width + i) * element_size, &_constant_border_value, element_size); |
| 239 | } |
| 240 | }, |
| 241 | vertical_it); |
| 242 | |
| 243 | // Top and bottom border |
| 244 | Iterator plane_it(_tensor, window); |
| 245 | |
| 246 | // Iterate over all XY planes |
| 247 | execute_window_loop(window, [&](const Coordinates & id) |
| 248 | { |
| 249 | uint8_t *base_addr = start_valid_region + plane_it.offset(); |
| 250 | // Top border |
| 251 | for(int i = -_border_size.top; i < 0; ++i) |
| 252 | { |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 253 | // Fill top rows including left/right borders |
Michalis Spyrou | 95abfdd | 2018-11-28 14:59:47 +0000 | [diff] [blame] | 254 | for(unsigned int j = 0; j < (_border_size.left + width + _border_size.right); ++j) |
| 255 | { |
| 256 | std::memcpy(base_addr + i * stridey + static_cast<int>(j - _border_size.left) * element_size, &_constant_border_value, element_size); |
| 257 | } |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 258 | } |
| 259 | |
| 260 | // Bottom border |
Pablo Tello | 62eeae4 | 2017-08-09 16:33:49 +0100 | [diff] [blame] | 261 | const unsigned low_border_size = height + _border_size.bottom; |
| 262 | for(unsigned int i = height; i < low_border_size; ++i) |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 263 | { |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 264 | // Fill bottom rows including left/right borders |
Michalis Spyrou | 95abfdd | 2018-11-28 14:59:47 +0000 | [diff] [blame] | 265 | for(unsigned int j = 0; j < (_border_size.left + width + _border_size.right); ++j) |
| 266 | { |
| 267 | std::memcpy(base_addr + i * stridey + static_cast<int>(j - _border_size.left) * element_size, &_constant_border_value, element_size); |
| 268 | } |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 269 | } |
| 270 | }, |
| 271 | plane_it); |
| 272 | } |
Michalis Spyrou | 95abfdd | 2018-11-28 14:59:47 +0000 | [diff] [blame] | 273 | } // namespace arm_compute |