blob: 747b8b1bfeb5b53d286667e33611d64c53151c4f [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{
Georgios Pinitas55186712018-01-08 17:37:12 +0000108 ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(tensor, 1, DataType::U8, DataType::QS8, DataType::QASYMM8,
109 DataType::QS16, DataType::U16, DataType::S16,
110 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;
150 case DataType::QS8:
151 case DataType::S8:
152 fill_constant_value_single_channel<int8_t>(window);
153 break;
154 case DataType::U16:
155 fill_constant_value_single_channel<uint16_t>(window);
156 break;
157 case DataType::S16:
158 case DataType::QS16:
159 fill_constant_value_single_channel<int16_t>(window);
160 break;
161 case DataType::U32:
162 fill_constant_value_single_channel<uint32_t>(window);
163 break;
164 case DataType::S32:
165 fill_constant_value_single_channel<int32_t>(window);
166 break;
Pablo Tello0c34fe22017-06-26 17:17:42 +0100167 case DataType::F16:
Georgios Pinitas583137c2017-08-31 18:12:42 +0100168 static_assert(sizeof(half) == 2, "Float16_t must be 16 bit");
169 fill_constant_value_single_channel<half>(window);
Pablo Tello0c34fe22017-06-26 17:17:42 +0100170 break;
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100171 case DataType::F32:
172 static_assert(sizeof(float) == 4, "Float must be 32 bit");
Pablo Tello62eeae42017-08-09 16:33:49 +0100173 if(_border_size.left == 1 && _border_size.top == 1)
Georgios Pinitas583137c2017-08-31 18:12:42 +0100174 {
Pablo Tello62eeae42017-08-09 16:33:49 +0100175 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 +0100176 }
Pablo Tello62eeae42017-08-09 16:33:49 +0100177 else
Georgios Pinitas583137c2017-08-31 18:12:42 +0100178 {
Pablo Tello62eeae42017-08-09 16:33:49 +0100179 fill_constant_value_single_channel<float>(window);
Georgios Pinitas583137c2017-08-31 18:12:42 +0100180 }
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100181 break;
182 default:
183 ARM_COMPUTE_ERROR("Not handled");
184 }
185 break;
186 }
187 case BorderMode::REPLICATE:
188 {
189 switch(_tensor->info()->data_type())
190 {
Georgios Pinitas55186712018-01-08 17:37:12 +0000191 case DataType::QASYMM8:
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100192 case DataType::U8:
193 fill_replicate_single_channel<uint8_t>(window);
194 break;
195 case DataType::QS8:
196 case DataType::S8:
197 fill_replicate_single_channel<int8_t>(window);
198 break;
199 case DataType::U16:
200 fill_replicate_single_channel<uint16_t>(window);
201 break;
202 case DataType::S16:
203 case DataType::QS16:
204 fill_replicate_single_channel<int16_t>(window);
205 break;
206 case DataType::U32:
207 fill_replicate_single_channel<uint32_t>(window);
208 break;
209 case DataType::S32:
210 fill_replicate_single_channel<int32_t>(window);
211 break;
Pablo Tello0c34fe22017-06-26 17:17:42 +0100212 case DataType::F16:
Georgios Pinitas583137c2017-08-31 18:12:42 +0100213 static_assert(sizeof(half) == 2, "Float16_t must be 16 bit");
214 fill_replicate_single_channel<half>(window);
Pablo Tello0c34fe22017-06-26 17:17:42 +0100215 break;
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100216 case DataType::F32:
217 static_assert(sizeof(float) == 4, "Float must be 32 bit");
218 fill_replicate_single_channel<float>(window);
219 break;
220 default:
221 ARM_COMPUTE_ERROR("Not handled");
222 }
223 break;
224 }
225 case BorderMode::UNDEFINED:
226 break; // Nothing to do here
227 default:
228 ARM_COMPUTE_ERROR("Unknown border mode");
229 }
230}
231
232template <typename T>
233void NEFillBorderKernel::fill_replicate_single_channel(const Window &window)
234{
235 uint8_t *const start_valid_region = _tensor->ptr_to_element(_tensor->info()->valid_region().anchor);
Georgios Pinitas424eb5d2017-12-06 19:49:38 +0000236 const size_t width = _tensor->info()->valid_region().shape[0];
237 const size_t height = _tensor->info()->valid_region().shape[1];
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100238
239 // Left and right border
240 Window vertical(window);
241 vertical.set(Window::DimY, Window::Dimension(0, height, 1));
242
243 Iterator vertical_it(_tensor, vertical);
244
245 execute_window_loop(vertical, [&](const Coordinates & id)
246 {
247 const auto row_start = reinterpret_cast<T *>(start_valid_region + vertical_it.offset());
248 const auto left_val = *reinterpret_cast<T *>(vertical_it.ptr());
249 const auto right_val = *(reinterpret_cast<T *>(vertical_it.ptr()) + width - 1);
250
251 // Fill left and right borders
252 std::fill_n(row_start - _border_size.left, _border_size.left, left_val);
253 std::fill_n(row_start + width, _border_size.right, right_val);
254 },
255 vertical_it);
256
257 // Top and bottom border
258 Iterator plane_it(_tensor, window);
259
260 // Iterate over all XY planes
261 execute_window_loop(window, [&](const Coordinates & id)
262 {
263 const auto first_row = reinterpret_cast<T *>(start_valid_region + plane_it.offset());
264
265 // Top border
266 for(int i = -_border_size.top; i < 0; ++i)
267 {
268 const auto row_start = reinterpret_cast<T *>(start_valid_region + plane_it.offset() + i * _tensor->info()->strides_in_bytes()[1]);
269
270 // Copy top rows including left/right borders
271 std::copy_n(first_row - _border_size.left, _border_size.left + width + _border_size.right, row_start - _border_size.left);
272 }
273
274 const auto last_row = reinterpret_cast<T *>(start_valid_region + plane_it.offset() + (height - 1) * _tensor->info()->strides_in_bytes()[1]);
275
276 // Bottom border
277 for(unsigned int i = height; i < height + _border_size.bottom; ++i)
278 {
279 const auto row_start = reinterpret_cast<T *>(start_valid_region + plane_it.offset() + i * _tensor->info()->strides_in_bytes()[1]);
280
281 // Copy bottom rows including left/right borders
282 std::copy_n(last_row - _border_size.left, _border_size.left + width + _border_size.right, row_start - _border_size.left);
283 }
284 },
285 plane_it);
286}
287
288template <typename T>
289void NEFillBorderKernel::fill_constant_value_single_channel(const Window &window)
290{
291 T constant_border_value;
292 _constant_border_value.get(constant_border_value);
293
294 uint8_t *const start_valid_region = _tensor->ptr_to_element(_tensor->info()->valid_region().anchor);
Georgios Pinitas424eb5d2017-12-06 19:49:38 +0000295 const size_t width = _tensor->info()->valid_region().shape[0];
296 const size_t height = _tensor->info()->valid_region().shape[1];
Pablo Tello62eeae42017-08-09 16:33:49 +0100297 const int stridey = _tensor->info()->strides_in_bytes()[1];
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100298
299 // Left and right border
300 Window vertical(window);
301 vertical.set(Window::DimY, Window::Dimension(0, height, 1));
302
303 Iterator vertical_it(_tensor, vertical);
304
305 execute_window_loop(vertical, [&](const Coordinates & id)
306 {
307 const auto row_start = reinterpret_cast<T *>(start_valid_region + vertical_it.offset());
308
309 // Fill left and right borders
310 std::fill_n(row_start - _border_size.left, _border_size.left, constant_border_value);
311 std::fill_n(row_start + width, _border_size.right, constant_border_value);
312 },
313 vertical_it);
314
315 // Top and bottom border
316 Iterator plane_it(_tensor, window);
317
318 // Iterate over all XY planes
319 execute_window_loop(window, [&](const Coordinates & id)
320 {
Pablo Tello62eeae42017-08-09 16:33:49 +0100321 uint8_t *base_addr = start_valid_region + plane_it.offset();
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100322 // Top border
323 for(int i = -_border_size.top; i < 0; ++i)
324 {
Pablo Tello62eeae42017-08-09 16:33:49 +0100325 const auto row_start = reinterpret_cast<T *>(base_addr + i * stridey);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100326
327 // Fill top rows including left/right borders
328 std::fill_n(row_start - _border_size.left, _border_size.left + width + _border_size.right, constant_border_value);
329 }
330
331 // Bottom border
Pablo Tello62eeae42017-08-09 16:33:49 +0100332 const unsigned low_border_size = height + _border_size.bottom;
333 for(unsigned int i = height; i < low_border_size; ++i)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100334 {
Pablo Tello62eeae42017-08-09 16:33:49 +0100335 const auto row_start = reinterpret_cast<T *>(base_addr + i * stridey);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100336
337 // Fill bottom rows including left/right borders
338 std::fill_n(row_start - _border_size.left, _border_size.left + width + _border_size.right, constant_border_value);
339 }
340 },
341 plane_it);
342}