blob: f4046e08514eb78383f57f496c14606d3d6f9ff1 [file] [log] [blame]
Anthony Barbier6ff3b192017-09-04 18:44:23 +01001/*
Manuel Bottinib412fab2018-12-10 17:40:23 +00002 * Copyright (c) 2016-2019 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
Michalis Spyrou95abfdd2018-11-28 14:59:47 +000037namespace arm_compute
38{
39class Coordinates;
Pablo Tello62eeae42017-08-09 16:33:49 +010040namespace
41{
Michalis Spyrou95abfdd2018-11-28 14:59:47 +000042inline void fill_constant_value_single_channel_special(ITensor *tensor, const Window &window, unsigned int right, unsigned int bottom, const PixelValue &constant_border_value)
Pablo Tello62eeae42017-08-09 16:33:49 +010043{
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 Pinitas0223a782017-12-12 11:44:44 +000047 const size_t width = tensor->info()->valid_region().shape[0];
48 const size_t height = tensor->info()->valid_region().shape[1];
Pablo Tello62eeae42017-08-09 16:33:49 +010049 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 Barbier6ff3b192017-09-04 18:44:23 +010093NEFillBorderKernel::NEFillBorderKernel()
Michalis Spyrou490bf2e2017-09-29 11:24:55 +010094 : _tensor(nullptr), _border_size(0), _mode(BorderMode::UNDEFINED), _constant_border_value(static_cast<float>(0.f))
Anthony Barbier6ff3b192017-09-04 18:44:23 +010095{
96}
97
98void NEFillBorderKernel::configure(ITensor *tensor, BorderSize border_size, BorderMode border_mode, const PixelValue &constant_border_value)
99{
Anthony Barbiereaefd002018-07-20 17:49:35 +0100100 //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 Loganathan7485d5a2018-07-04 09:34:00 +0100101 ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(tensor, 1, DataType::U8, DataType::QASYMM8,
102 DataType::U16, DataType::S16,
Georgios Pinitas55186712018-01-08 17:37:12 +0000103 DataType::U32, DataType::S32,
104 DataType::F16, DataType::F32);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100105
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 Li86b53332017-08-23 11:02:43 +0100116 win.use_tensor_dimensions(_tensor->info()->tensor_shape(), Window::DimZ);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100117 INEKernel::configure(win);
118}
119
Moritz Pflanzerc186b572017-09-07 09:48:04 +0100120void NEFillBorderKernel::run(const Window &window, const ThreadInfo &info)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100121{
Moritz Pflanzerc186b572017-09-07 09:48:04 +0100122 ARM_COMPUTE_UNUSED(info);
123
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100124 // 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 Spyrou95abfdd2018-11-28 14:59:47 +0000137 if(_border_size.left == 1 && _border_size.top == 1 && _tensor->info()->data_type() == DataType::F32)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100138 {
Michalis Spyrou95abfdd2018-11-28 14:59:47 +0000139 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 Barbier6ff3b192017-09-04 18:44:23 +0100144 }
145 break;
146 }
147 case BorderMode::REPLICATE:
148 {
Michalis Spyrou95abfdd2018-11-28 14:59:47 +0000149 fill_replicate_single_channel(window);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100150 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 Barbier6ff3b192017-09-04 18:44:23 +0100159void 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 Pinitas424eb5d2017-12-06 19:49:38 +0000162 const size_t width = _tensor->info()->valid_region().shape[0];
163 const size_t height = _tensor->info()->valid_region().shape[1];
Michalis Spyrou95abfdd2018-11-28 14:59:47 +0000164 const size_t element_size = _tensor->info()->element_size();
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100165 // 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 Spyrou95abfdd2018-11-28 14:59:47 +0000173 uint8_t *base_addr = start_valid_region + vertical_it.offset();
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100174 // Fill left and right borders
Michalis Spyrou95abfdd2018-11-28 14:59:47 +0000175 for(unsigned int i = 0; i < _border_size.left; ++i)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100176 {
Michalis Spyrou95abfdd2018-11-28 14:59:47 +0000177 std::memcpy(base_addr + static_cast<int>(i - _border_size.left) * element_size, vertical_it.ptr(), element_size);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100178 }
179
Michalis Spyrou95abfdd2018-11-28 14:59:47 +0000180 for(unsigned int i = 0; i < _border_size.right; ++i)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100181 {
Michalis Spyrou95abfdd2018-11-28 14:59:47 +0000182 std::memcpy(base_addr + (width + i) * element_size, vertical_it.ptr() + (width - 1) * element_size, element_size);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100183 }
184 },
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100185 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 Tello62eeae42017-08-09 16:33:49 +0100193 uint8_t *base_addr = start_valid_region + plane_it.offset();
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100194 // Top border
195 for(int i = -_border_size.top; i < 0; ++i)
196 {
Michalis Spyrou95abfdd2018-11-28 14:59:47 +0000197 // 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 Barbier6ff3b192017-09-04 18:44:23 +0100201
Michalis Spyrou95abfdd2018-11-28 14:59:47 +0000202 // 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
213void 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 Barbier6ff3b192017-09-04 18:44:23 +0100253 // Fill top rows including left/right borders
Michalis Spyrou95abfdd2018-11-28 14:59:47 +0000254 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 Barbier6ff3b192017-09-04 18:44:23 +0100258 }
259
260 // Bottom border
Pablo Tello62eeae42017-08-09 16:33:49 +0100261 const unsigned low_border_size = height + _border_size.bottom;
262 for(unsigned int i = height; i < low_border_size; ++i)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100263 {
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100264 // Fill bottom rows including left/right borders
Michalis Spyrou95abfdd2018-11-28 14:59:47 +0000265 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 Barbier6ff3b192017-09-04 18:44:23 +0100269 }
270 },
271 plane_it);
272}
Michalis Spyrou95abfdd2018-11-28 14:59:47 +0000273} // namespace arm_compute