blob: 3d08cafa935faa828adcc8715d2a446fa892ff9e [file] [log] [blame]
Anthony Barbier6ff3b192017-09-04 18:44:23 +01001/*
Georgios Pinitas55186712018-01-08 17:37:12 +00002 * Copyright (c) 2016-2018 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/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 Pinitas583137c2017-08-31 18:12:42 +010030#include "arm_compute/core/Types.h"
Anthony Barbier6ff3b192017-09-04 18:44:23 +010031#include "arm_compute/core/Validate.h"
32#include "arm_compute/core/Window.h"
33
34#include <algorithm>
35#include <cstdint>
36
37using namespace arm_compute;
38
Pablo Tello62eeae42017-08-09 16:33:49 +010039namespace
40{
41template <typename T, unsigned int leftx, unsigned int rightx>
42void fill_constant_value_single_channel_special(ITensor *tensor, const Window &window, unsigned int right, unsigned int bottom, const PixelValue &constant_border_value);
43
44template <>
45inline 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)
46{
47 float border_value;
48 constant_border_value.get(border_value);
49 uint8_t *const start_valid_region = tensor->ptr_to_element(tensor->info()->valid_region().anchor);
Georgios Pinitas0223a782017-12-12 11:44:44 +000050 const size_t width = tensor->info()->valid_region().shape[0];
51 const size_t height = tensor->info()->valid_region().shape[1];
Pablo Tello62eeae42017-08-09 16:33:49 +010052 const int stridey = tensor->info()->strides_in_bytes()[1];
53
54 // Left and right border
55 Window vertical(window);
56 vertical.set(Window::DimY, Window::Dimension(0, height, 1));
57
58 Iterator vertical_it(tensor, vertical);
59
60 execute_window_loop(vertical, [&](const Coordinates &)
61 {
62 const auto row_start = reinterpret_cast<float *>(start_valid_region + vertical_it.offset());
63
64 // Fill left and right borders
65 *(row_start - 1) = border_value;
66 std::fill_n(row_start + width, right, border_value);
67 },
68 vertical_it);
69
70 // Top and bottom border
71 Iterator plane_it(tensor, window);
72
73 // Iterate over all XY planes
74 execute_window_loop(window, [&](const Coordinates &)
75 {
76 uint8_t *base_addr = start_valid_region + plane_it.offset();
77 // Top border
78 const auto row_start = reinterpret_cast<float *>(base_addr - stridey);
79 // Fill top rows including left/right borders
80 std::fill_n(row_start - 1, 1 + width + right, border_value);
81
82 // Bottom border
83 const unsigned low_border_size = height + bottom;
84 for(unsigned int i = height; i < low_border_size; ++i)
85 {
86 const auto row_start = reinterpret_cast<float *>(base_addr + i * stridey);
87
88 // Fill bottom rows including left/right borders
89 std::fill_n(row_start - 1, 1 + width + right, border_value);
90 }
91 },
92 plane_it);
93}
94} // namespace
95
Anthony Barbier6ff3b192017-09-04 18:44:23 +010096namespace arm_compute
97{
98class Coordinates;
99} // namespace arm_compute
100
101NEFillBorderKernel::NEFillBorderKernel()
Michalis Spyrou490bf2e2017-09-29 11:24:55 +0100102 : _tensor(nullptr), _border_size(0), _mode(BorderMode::UNDEFINED), _constant_border_value(static_cast<float>(0.f))
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100103{
104}
105
106void NEFillBorderKernel::configure(ITensor *tensor, BorderSize border_size, BorderMode border_mode, const PixelValue &constant_border_value)
107{
Vidhya Sudhan Loganathan7485d5a2018-07-04 09:34:00 +0100108 ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(tensor, 1, DataType::U8, DataType::QASYMM8,
109 DataType::U16, DataType::S16,
Georgios Pinitas55186712018-01-08 17:37:12 +0000110 DataType::U32, DataType::S32,
111 DataType::F16, DataType::F32);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100112
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));
SiCong Li86b53332017-08-23 11:02:43 +0100123 win.use_tensor_dimensions(_tensor->info()->tensor_shape(), Window::DimZ);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100124 INEKernel::configure(win);
125}
126
Moritz Pflanzerc186b572017-09-07 09:48:04 +0100127void NEFillBorderKernel::run(const Window &window, const ThreadInfo &info)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100128{
Moritz Pflanzerc186b572017-09-07 09:48:04 +0100129 ARM_COMPUTE_UNUSED(info);
130
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100131 // If there is no border: early exit
132 if(_border_size.empty())
133 {
134 return;
135 }
136
137 ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this);
138 ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(INEKernel::window(), window);
139
140 switch(_mode)
141 {
142 case BorderMode::CONSTANT:
143 {
144 switch(_tensor->info()->data_type())
145 {
Georgios Pinitas55186712018-01-08 17:37:12 +0000146 case DataType::QASYMM8:
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100147 case DataType::U8:
148 fill_constant_value_single_channel<uint8_t>(window);
149 break;
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100150 case DataType::S8:
151 fill_constant_value_single_channel<int8_t>(window);
152 break;
153 case DataType::U16:
154 fill_constant_value_single_channel<uint16_t>(window);
155 break;
156 case DataType::S16:
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100157 fill_constant_value_single_channel<int16_t>(window);
158 break;
159 case DataType::U32:
160 fill_constant_value_single_channel<uint32_t>(window);
161 break;
162 case DataType::S32:
163 fill_constant_value_single_channel<int32_t>(window);
164 break;
Pablo Tello0c34fe22017-06-26 17:17:42 +0100165 case DataType::F16:
Georgios Pinitas583137c2017-08-31 18:12:42 +0100166 static_assert(sizeof(half) == 2, "Float16_t must be 16 bit");
167 fill_constant_value_single_channel<half>(window);
Pablo Tello0c34fe22017-06-26 17:17:42 +0100168 break;
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100169 case DataType::F32:
170 static_assert(sizeof(float) == 4, "Float must be 32 bit");
Pablo Tello62eeae42017-08-09 16:33:49 +0100171 if(_border_size.left == 1 && _border_size.top == 1)
Georgios Pinitas583137c2017-08-31 18:12:42 +0100172 {
Pablo Tello62eeae42017-08-09 16:33:49 +0100173 fill_constant_value_single_channel_special<float, 1u, 1u>(_tensor, window, _border_size.right, _border_size.bottom, _constant_border_value);
Georgios Pinitas583137c2017-08-31 18:12:42 +0100174 }
Pablo Tello62eeae42017-08-09 16:33:49 +0100175 else
Georgios Pinitas583137c2017-08-31 18:12:42 +0100176 {
Pablo Tello62eeae42017-08-09 16:33:49 +0100177 fill_constant_value_single_channel<float>(window);
Georgios Pinitas583137c2017-08-31 18:12:42 +0100178 }
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100179 break;
180 default:
181 ARM_COMPUTE_ERROR("Not handled");
182 }
183 break;
184 }
185 case BorderMode::REPLICATE:
186 {
187 switch(_tensor->info()->data_type())
188 {
Georgios Pinitas55186712018-01-08 17:37:12 +0000189 case DataType::QASYMM8:
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100190 case DataType::U8:
191 fill_replicate_single_channel<uint8_t>(window);
192 break;
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100193 case DataType::S8:
194 fill_replicate_single_channel<int8_t>(window);
195 break;
196 case DataType::U16:
197 fill_replicate_single_channel<uint16_t>(window);
198 break;
199 case DataType::S16:
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100200 fill_replicate_single_channel<int16_t>(window);
201 break;
202 case DataType::U32:
203 fill_replicate_single_channel<uint32_t>(window);
204 break;
205 case DataType::S32:
206 fill_replicate_single_channel<int32_t>(window);
207 break;
Pablo Tello0c34fe22017-06-26 17:17:42 +0100208 case DataType::F16:
Georgios Pinitas583137c2017-08-31 18:12:42 +0100209 static_assert(sizeof(half) == 2, "Float16_t must be 16 bit");
210 fill_replicate_single_channel<half>(window);
Pablo Tello0c34fe22017-06-26 17:17:42 +0100211 break;
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100212 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
228template <typename T>
229void 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);
Georgios Pinitas424eb5d2017-12-06 19:49:38 +0000232 const size_t width = _tensor->info()->valid_region().shape[0];
233 const size_t height = _tensor->info()->valid_region().shape[1];
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100234
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
284template <typename T>
285void 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);
Georgios Pinitas424eb5d2017-12-06 19:49:38 +0000291 const size_t width = _tensor->info()->valid_region().shape[0];
292 const size_t height = _tensor->info()->valid_region().shape[1];
Pablo Tello62eeae42017-08-09 16:33:49 +0100293 const int stridey = _tensor->info()->strides_in_bytes()[1];
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100294
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 Tello62eeae42017-08-09 16:33:49 +0100317 uint8_t *base_addr = start_valid_region + plane_it.offset();
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100318 // Top border
319 for(int i = -_border_size.top; i < 0; ++i)
320 {
Pablo Tello62eeae42017-08-09 16:33:49 +0100321 const auto row_start = reinterpret_cast<T *>(base_addr + i * stridey);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100322
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 Tello62eeae42017-08-09 16:33:49 +0100328 const unsigned low_border_size = height + _border_size.bottom;
329 for(unsigned int i = height; i < low_border_size; ++i)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100330 {
Pablo Tello62eeae42017-08-09 16:33:49 +0100331 const auto row_start = reinterpret_cast<T *>(base_addr + i * stridey);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100332
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}