blob: 593a529c3f6fe27e3d46367eaeee5c1ebe113fa7 [file] [log] [blame]
Anthony Barbier6ff3b192017-09-04 18:44:23 +01001/*
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"
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);
50 const size_t &width = tensor->info()->valid_region().shape[0];
51 const size_t &height = tensor->info()->valid_region().shape[1];
52 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{
Pablo Tello0c34fe22017-06-26 17:17:42 +0100108 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 Barbier6ff3b192017-09-04 18:44:23 +0100109
110 _tensor = tensor;
111 _border_size = border_size;
112 _mode = border_mode;
113 _constant_border_value = constant_border_value;
114
115 _border_size.limit(tensor->info()->padding());
116
117 Window win;
118 win.set(Window::DimX, Window::Dimension(0, 1, 1));
119 win.set(Window::DimY, Window::Dimension(0, 1, 1));
SiCong Li86b53332017-08-23 11:02:43 +0100120 win.use_tensor_dimensions(_tensor->info()->tensor_shape(), Window::DimZ);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100121 INEKernel::configure(win);
122}
123
Moritz Pflanzerc186b572017-09-07 09:48:04 +0100124void NEFillBorderKernel::run(const Window &window, const ThreadInfo &info)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100125{
Moritz Pflanzerc186b572017-09-07 09:48:04 +0100126 ARM_COMPUTE_UNUSED(info);
127
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100128 // If there is no border: early exit
129 if(_border_size.empty())
130 {
131 return;
132 }
133
134 ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this);
135 ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(INEKernel::window(), window);
136
137 switch(_mode)
138 {
139 case BorderMode::CONSTANT:
140 {
141 switch(_tensor->info()->data_type())
142 {
143 case DataType::U8:
144 fill_constant_value_single_channel<uint8_t>(window);
145 break;
146 case DataType::QS8:
147 case DataType::S8:
148 fill_constant_value_single_channel<int8_t>(window);
149 break;
150 case DataType::U16:
151 fill_constant_value_single_channel<uint16_t>(window);
152 break;
153 case DataType::S16:
154 case DataType::QS16:
155 fill_constant_value_single_channel<int16_t>(window);
156 break;
157 case DataType::U32:
158 fill_constant_value_single_channel<uint32_t>(window);
159 break;
160 case DataType::S32:
161 fill_constant_value_single_channel<int32_t>(window);
162 break;
Pablo Tello0c34fe22017-06-26 17:17:42 +0100163 case DataType::F16:
Georgios Pinitas583137c2017-08-31 18:12:42 +0100164 static_assert(sizeof(half) == 2, "Float16_t must be 16 bit");
165 fill_constant_value_single_channel<half>(window);
Pablo Tello0c34fe22017-06-26 17:17:42 +0100166 break;
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100167 case DataType::F32:
168 static_assert(sizeof(float) == 4, "Float must be 32 bit");
Pablo Tello62eeae42017-08-09 16:33:49 +0100169 if(_border_size.left == 1 && _border_size.top == 1)
Georgios Pinitas583137c2017-08-31 18:12:42 +0100170 {
Pablo Tello62eeae42017-08-09 16:33:49 +0100171 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 +0100172 }
Pablo Tello62eeae42017-08-09 16:33:49 +0100173 else
Georgios Pinitas583137c2017-08-31 18:12:42 +0100174 {
Pablo Tello62eeae42017-08-09 16:33:49 +0100175 fill_constant_value_single_channel<float>(window);
Georgios Pinitas583137c2017-08-31 18:12:42 +0100176 }
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100177 break;
178 default:
179 ARM_COMPUTE_ERROR("Not handled");
180 }
181 break;
182 }
183 case BorderMode::REPLICATE:
184 {
185 switch(_tensor->info()->data_type())
186 {
187 case DataType::U8:
188 fill_replicate_single_channel<uint8_t>(window);
189 break;
190 case DataType::QS8:
191 case DataType::S8:
192 fill_replicate_single_channel<int8_t>(window);
193 break;
194 case DataType::U16:
195 fill_replicate_single_channel<uint16_t>(window);
196 break;
197 case DataType::S16:
198 case DataType::QS16:
199 fill_replicate_single_channel<int16_t>(window);
200 break;
201 case DataType::U32:
202 fill_replicate_single_channel<uint32_t>(window);
203 break;
204 case DataType::S32:
205 fill_replicate_single_channel<int32_t>(window);
206 break;
Pablo Tello0c34fe22017-06-26 17:17:42 +0100207 case DataType::F16:
Georgios Pinitas583137c2017-08-31 18:12:42 +0100208 static_assert(sizeof(half) == 2, "Float16_t must be 16 bit");
209 fill_replicate_single_channel<half>(window);
Pablo Tello0c34fe22017-06-26 17:17:42 +0100210 break;
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100211 case DataType::F32:
212 static_assert(sizeof(float) == 4, "Float must be 32 bit");
213 fill_replicate_single_channel<float>(window);
214 break;
215 default:
216 ARM_COMPUTE_ERROR("Not handled");
217 }
218 break;
219 }
220 case BorderMode::UNDEFINED:
221 break; // Nothing to do here
222 default:
223 ARM_COMPUTE_ERROR("Unknown border mode");
224 }
225}
226
227template <typename T>
228void NEFillBorderKernel::fill_replicate_single_channel(const Window &window)
229{
230 uint8_t *const start_valid_region = _tensor->ptr_to_element(_tensor->info()->valid_region().anchor);
231 const size_t &width = _tensor->info()->valid_region().shape[0];
232 const size_t &height = _tensor->info()->valid_region().shape[1];
233
234 // Left and right border
235 Window vertical(window);
236 vertical.set(Window::DimY, Window::Dimension(0, height, 1));
237
238 Iterator vertical_it(_tensor, vertical);
239
240 execute_window_loop(vertical, [&](const Coordinates & id)
241 {
242 const auto row_start = reinterpret_cast<T *>(start_valid_region + vertical_it.offset());
243 const auto left_val = *reinterpret_cast<T *>(vertical_it.ptr());
244 const auto right_val = *(reinterpret_cast<T *>(vertical_it.ptr()) + width - 1);
245
246 // Fill left and right borders
247 std::fill_n(row_start - _border_size.left, _border_size.left, left_val);
248 std::fill_n(row_start + width, _border_size.right, right_val);
249 },
250 vertical_it);
251
252 // Top and bottom border
253 Iterator plane_it(_tensor, window);
254
255 // Iterate over all XY planes
256 execute_window_loop(window, [&](const Coordinates & id)
257 {
258 const auto first_row = reinterpret_cast<T *>(start_valid_region + plane_it.offset());
259
260 // Top border
261 for(int i = -_border_size.top; i < 0; ++i)
262 {
263 const auto row_start = reinterpret_cast<T *>(start_valid_region + plane_it.offset() + i * _tensor->info()->strides_in_bytes()[1]);
264
265 // Copy top rows including left/right borders
266 std::copy_n(first_row - _border_size.left, _border_size.left + width + _border_size.right, row_start - _border_size.left);
267 }
268
269 const auto last_row = reinterpret_cast<T *>(start_valid_region + plane_it.offset() + (height - 1) * _tensor->info()->strides_in_bytes()[1]);
270
271 // Bottom border
272 for(unsigned int i = height; i < height + _border_size.bottom; ++i)
273 {
274 const auto row_start = reinterpret_cast<T *>(start_valid_region + plane_it.offset() + i * _tensor->info()->strides_in_bytes()[1]);
275
276 // Copy bottom rows including left/right borders
277 std::copy_n(last_row - _border_size.left, _border_size.left + width + _border_size.right, row_start - _border_size.left);
278 }
279 },
280 plane_it);
281}
282
283template <typename T>
284void NEFillBorderKernel::fill_constant_value_single_channel(const Window &window)
285{
286 T constant_border_value;
287 _constant_border_value.get(constant_border_value);
288
289 uint8_t *const start_valid_region = _tensor->ptr_to_element(_tensor->info()->valid_region().anchor);
290 const size_t &width = _tensor->info()->valid_region().shape[0];
291 const size_t &height = _tensor->info()->valid_region().shape[1];
Pablo Tello62eeae42017-08-09 16:33:49 +0100292 const int stridey = _tensor->info()->strides_in_bytes()[1];
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100293
294 // Left and right border
295 Window vertical(window);
296 vertical.set(Window::DimY, Window::Dimension(0, height, 1));
297
298 Iterator vertical_it(_tensor, vertical);
299
300 execute_window_loop(vertical, [&](const Coordinates & id)
301 {
302 const auto row_start = reinterpret_cast<T *>(start_valid_region + vertical_it.offset());
303
304 // Fill left and right borders
305 std::fill_n(row_start - _border_size.left, _border_size.left, constant_border_value);
306 std::fill_n(row_start + width, _border_size.right, constant_border_value);
307 },
308 vertical_it);
309
310 // Top and bottom border
311 Iterator plane_it(_tensor, window);
312
313 // Iterate over all XY planes
314 execute_window_loop(window, [&](const Coordinates & id)
315 {
Pablo Tello62eeae42017-08-09 16:33:49 +0100316 uint8_t *base_addr = start_valid_region + plane_it.offset();
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100317 // Top border
318 for(int i = -_border_size.top; i < 0; ++i)
319 {
Pablo Tello62eeae42017-08-09 16:33:49 +0100320 const auto row_start = reinterpret_cast<T *>(base_addr + i * stridey);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100321
322 // Fill top rows including left/right borders
323 std::fill_n(row_start - _border_size.left, _border_size.left + width + _border_size.right, constant_border_value);
324 }
325
326 // Bottom border
Pablo Tello62eeae42017-08-09 16:33:49 +0100327 const unsigned low_border_size = height + _border_size.bottom;
328 for(unsigned int i = height; i < low_border_size; ++i)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100329 {
Pablo Tello62eeae42017-08-09 16:33:49 +0100330 const auto row_start = reinterpret_cast<T *>(base_addr + i * stridey);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100331
332 // Fill bottom rows including left/right borders
333 std::fill_n(row_start - _border_size.left, _border_size.left + width + _border_size.right, constant_border_value);
334 }
335 },
336 plane_it);
337}