2 * Copyright (c) 2016, 2017 ARM Limited.
4 * SPDX-License-Identifier: MIT
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:
13 * The above copyright notice and this permission notice shall be included in all
14 * copies or substantial portions of the Software.
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
24 #include "arm_compute/core/NEON/kernels/NENonMaximaSuppression3x3Kernel.h"
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"
30 #include "arm_compute/core/Types.h"
31 #include "arm_compute/core/Utils.h"
32 #include "arm_compute/core/Validate.h"
37 using namespace arm_compute;
42 } // namespace arm_compute
44 #ifdef ARM_COMPUTE_ENABLE_FP16
47 inline void mask_top(const float16x8_t &vc, const float16x8_t &in0, const float16x8_t &in1, uint16x8_t &mask)
49 // vc > nc.val[0], vc > nc.val[1], vc > nc.val[2]
50 mask = vandq_u16(mask, vcgeq_f16(vc, in0));
51 mask = vandq_u16(mask, vcgeq_f16(vc, vextq_f16(in0, in1, 1)));
52 mask = vandq_u16(mask, vcgeq_f16(vc, vextq_f16(in0, in1, 2)));
55 inline void mask_middle(const float16x8_t &vc, const float16x8_t &in0, const float16x8_t &in1, uint16x8_t &mask)
57 // vc >= nc.val[0], vc > nc.val[2]
58 mask = vandq_u16(mask, vcgeq_f16(vc, in0));
59 mask = vandq_u16(mask, vcgtq_f16(vc, vextq_f16(in0, in1, 2)));
62 inline void mask_bottom(const float16x8_t &vc, const float16x8_t &in0, const float16x8_t &in1, uint16x8_t &mask)
64 // vc > nc.val[0], vc > nc.val[1], vc > nc.val[2]
65 mask = vandq_u16(mask, vcgtq_f16(vc, in0));
66 mask = vandq_u16(mask, vcgtq_f16(vc, vextq_f16(in0, in1, 1)));
67 mask = vandq_u16(mask, vcgtq_f16(vc, vextq_f16(in0, in1, 2)));
70 inline void non_maxima_suppression3x3_F32_F32(const void *__restrict in_ptr, void *__restrict out_ptr, const uint32_t in_stride)
72 auto in = static_cast<const float *__restrict>(in_ptr) - 1;
73 const auto out = static_cast<float *__restrict>(out_ptr);
76 const float16x8x2_t vc =
78 vcombine_f16(vcvt_f16_f32(vld1q_f32(in + 1)), vcvt_f16_f32(vld1q_f32(in + 5))),
79 vcombine_f16(vcvt_f16_f32(vld1q_f32(in + 9)), vcvt_f16_f32(vld1q_f32(in + 13)))
85 static const float16x4_t zero_f16x4 = vdup_n_f16(0);
86 static const uint16x8_t zero_u16 = vdupq_n_u16(0);
87 static const uint16x8_t true_mask = vceqq_u16(zero_u16, zero_u16);
88 static const uint16x8x2_t true_mask_x2 =
94 uint16x8x2_t mask = true_mask_x2;
97 const float16x8_t tmp_top0 = vcombine_f16(vcvt_f16_f32(vld1q_f32(in)), vcvt_f16_f32(vld1q_f32(in + 4)));
98 const float16x8_t tmp_top1 = vcombine_f16(vcvt_f16_f32(vld1q_f32(in + 8)), vcvt_f16_f32(vld1q_f32(in + 12)));
99 const float16x8_t tmp_top2 = vcombine_f16(vcvt_f16_f32(vld1q_f32(in + 16)), zero_f16x4);
101 // vc >= nc.val[0], vc >= nc.val[1], vc >= nc.val[2]
102 mask_top(vc.val[0], tmp_top0, tmp_top1, mask.val[0]);
103 mask_top(vc.val[1], tmp_top1, tmp_top2, mask.val[1]);
108 const float16x8_t tmp_mid0 = vcombine_f16(vcvt_f16_f32(vld1q_f32(in)), vcvt_f16_f32(vld1q_f32(in + 4)));
109 const float16x8_t tmp_mid1 = vcombine_f16(vcvt_f16_f32(vld1q_f32(in + 8)), vcvt_f16_f32(vld1q_f32(in + 12)));
110 const float16x8_t tmp_mid2 = vcombine_f16(vcvt_f16_f32(vld1q_f32(in + 16)), zero_f16x4);
112 // vc >= nc.val[0], vc > nc.val[2]
113 mask_middle(vc.val[0], tmp_mid0, tmp_mid1, mask.val[0]);
114 mask_middle(vc.val[1], tmp_mid1, tmp_mid2, mask.val[1]);
119 const float16x8_t tmp_bot0 = vcombine_f16(vcvt_f16_f32(vld1q_f32(in)), vcvt_f16_f32(vld1q_f32(in + 4)));
120 const float16x8_t tmp_bot1 = vcombine_f16(vcvt_f16_f32(vld1q_f32(in + 8)), vcvt_f16_f32(vld1q_f32(in + 12)));
121 const float16x8_t tmp_bot2 = vcombine_f16(vcvt_f16_f32(vld1q_f32(in + 16)), zero_f16x4);
123 // vc > nc.val[0], vc > nc.val[1], vc > nc.val[2]
124 mask_bottom(vc.val[0], tmp_bot0, tmp_bot1, mask.val[0]);
125 mask_bottom(vc.val[1], tmp_bot1, tmp_bot2, mask.val[1]);
128 static const float16x8_t zero_f16x8 = vdupq_n_f16(0);
130 const float16x8_t suppressed0 = vbslq_f16(mask.val[0], vc.val[0], zero_f16x8);
131 vst1q_f32(out + 0, vcvt_f32_f16(vget_low_f16(suppressed0)));
132 vst1q_f32(out + 4, vcvt_f32_f16(vget_high_f16(suppressed0)));
134 const float16x8_t suppressed1 = vbslq_f16(mask.val[1], vc.val[1], zero_f16x8);
135 vst1q_f32(out + 8, vcvt_f32_f16(vget_low_f16(suppressed1)));
136 vst1q_f32(out + 12, vcvt_f32_f16(vget_high_f16(suppressed1)));
139 inline void non_maxima_suppression3x3_U8_U8(const void *__restrict in_ptr, void *__restrict out_ptr, const uint32_t in_stride)
141 auto in = static_cast<const uint8_t *__restrict>(in_ptr) - 1;
142 const auto out = static_cast<uint8_t *__restrict>(out_ptr);
145 const uint8x16_t vc = vld1q_u8(in + 1);
147 // Neighboring pixels
151 const uint8x16_t l_nc_0 = vld1q_u8(in);
152 const uint8x16_t m_nc_0 = vld1q_u8(in + 1);
153 const uint8x16_t r_nc_0 = vld1q_u8(in + 2);
155 // Keep center scores if ...
156 // vc >= l_nc_0, vc >= m_nc_0, vc >= r_nc_0
157 uint8x16_t mask = vcgeq_u8(vc, l_nc_0);
158 mask = vandq_u8(mask, vcgeq_u8(vc, m_nc_0));
159 mask = vandq_u8(mask, vcgeq_u8(vc, r_nc_0));
164 const uint8x16_t l_nc_1 = vld1q_u8(in);
165 const uint8x16_t r_nc_1 = vld1q_u8(in + 2);
168 // vc >= l_nc_1, vc > r_nc_1
169 mask = vandq_u8(mask, vcgeq_u8(vc, l_nc_1));
170 mask = vandq_u8(mask, vcgtq_u8(vc, r_nc_1));
175 const uint8x16_t l_nc_2 = vld1q_u8(in);
176 const uint8x16_t m_nc_2 = vld1q_u8(in + 1);
177 const uint8x16_t r_nc_2 = vld1q_u8(in + 2);
180 // vc > l_nc_2, vc > m_nc_2, vc > r_nc_2
181 mask = vandq_u8(mask, vcgtq_u8(vc, l_nc_2));
182 mask = vandq_u8(mask, vcgtq_u8(vc, m_nc_2));
183 mask = vandq_u8(mask, vcgtq_u8(vc, r_nc_2));
186 static const uint8x16_t zero = vdupq_n_u8(0);
187 vst1q_u8(out, vbslq_u8(mask, vc, zero));
191 void NENonMaximaSuppression3x3FP16Kernel::configure(const ITensor *input, ITensor *output, bool border_undefined)
193 ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::U8, DataType::F32);
194 ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(output, 1, DataType::U8, DataType::F32);
195 ARM_COMPUTE_ERROR_ON_MISMATCHING_DATA_TYPES(input, output);
200 switch(input->info()->data_type())
203 _func = &fp16::non_maxima_suppression3x3_U8_U8;
206 _func = &fp16::non_maxima_suppression3x3_F32_F32;
210 constexpr unsigned int num_elems_processed_per_iteration = 16;
211 const unsigned int num_elems_read_per_iteration = 16 + 2 * border_size().left + (input->info()->data_type() == DataType::U8 ? 0 : 3);
212 constexpr unsigned int num_elems_written_per_iteration = 16;
213 constexpr unsigned int num_rows_read_per_iteration = 3;
215 // Configure kernel window
216 Window win = calculate_max_window(*input->info(), Steps(num_elems_processed_per_iteration), border_undefined, border_size());
217 AccessWindowHorizontal output_access(output->info(), 0, num_elems_written_per_iteration);
219 update_window_and_padding(win,
220 AccessWindowRectangle(input->info(), -border_size().left, -border_size().top, num_elems_read_per_iteration, num_rows_read_per_iteration),
223 output_access.set_valid_region(win, input->info()->valid_region(), border_undefined, border_size());
225 INEKernel::configure(win);
231 inline void non_maxima_suppression3x3_FLOAT_FLOAT(const void *__restrict input_ptr, void *__restrict output_ptr, const uint32_t input_stride)
233 auto input = static_cast<const float *__restrict>(input_ptr) - 1;
234 const auto output = static_cast<float *__restrict>(output_ptr);
237 const float32x4x4_t vc =
240 vld1q_f32(input + 1),
241 vld1q_f32(input + 5),
242 vld1q_f32(input + 9),
243 vld1q_f32(input + 13)
247 // Neighboring pixels
248 float32x4x4_t l_nc{ {} };
249 float32x4x4_t m_nc{ {} };
250 float32x4x4_t r_nc{ {} };
252 input -= input_stride;
255 float32x4_t tmp_low = vld1q_f32(input);
256 float32x4_t tmp_high = vld1q_f32(input + 4);
257 float32x4_t tmp_high1 = vld1q_f32(input + 8);
259 l_nc.val[0] = tmp_low;
260 m_nc.val[0] = vextq_f32(tmp_low, tmp_high, 1);
261 r_nc.val[0] = vextq_f32(tmp_low, tmp_high, 2);
264 tmp_high = tmp_high1;
266 l_nc.val[1] = tmp_low;
267 m_nc.val[1] = vextq_f32(tmp_low, tmp_high, 1);
268 r_nc.val[1] = vextq_f32(tmp_low, tmp_high, 2);
272 tmp_high = vld1q_f32(input + 12);
273 tmp_high1 = vld1q_f32(input + 16);
275 l_nc.val[2] = tmp_low;
276 m_nc.val[2] = vextq_f32(tmp_low, tmp_high, 1);
277 r_nc.val[2] = vextq_f32(tmp_low, tmp_high, 2);
280 tmp_high = tmp_high1;
282 l_nc.val[3] = tmp_low;
283 m_nc.val[3] = vextq_f32(tmp_low, tmp_high, 1);
284 r_nc.val[3] = vextq_f32(tmp_low, tmp_high, 2);
286 // mc >= nc.val[0], mc >= nc.val[1], mc >= nc.val[2]
287 uint32x4x4_t mask{ {} };
288 mask.val[0] = vcgeq_f32(vc.val[0], l_nc.val[0]);
289 mask.val[0] = vandq_u32(mask.val[0], vcgeq_f32(vc.val[0], m_nc.val[0]));
290 mask.val[0] = vandq_u32(mask.val[0], vcgeq_f32(vc.val[0], r_nc.val[0]));
291 mask.val[1] = vcgeq_f32(vc.val[1], l_nc.val[1]);
292 mask.val[1] = vandq_u32(mask.val[1], vcgeq_f32(vc.val[1], m_nc.val[1]));
293 mask.val[1] = vandq_u32(mask.val[1], vcgeq_f32(vc.val[1], r_nc.val[1]));
294 mask.val[2] = vcgeq_f32(vc.val[2], l_nc.val[2]);
295 mask.val[2] = vandq_u32(mask.val[2], vcgeq_f32(vc.val[2], m_nc.val[2]));
296 mask.val[2] = vandq_u32(mask.val[2], vcgeq_f32(vc.val[2], r_nc.val[2]));
297 mask.val[3] = vcgeq_f32(vc.val[3], l_nc.val[3]);
298 mask.val[3] = vandq_u32(mask.val[3], vcgeq_f32(vc.val[3], m_nc.val[3]));
299 mask.val[3] = vandq_u32(mask.val[3], vcgeq_f32(vc.val[3], r_nc.val[3]));
301 input += input_stride;
304 tmp_low = vld1q_f32(input);
305 tmp_high = vld1q_f32(input + 4);
306 tmp_high1 = vld1q_f32(input + 8);
308 l_nc.val[0] = tmp_low;
309 r_nc.val[0] = vextq_f32(tmp_low, tmp_high, 2);
312 tmp_high = tmp_high1;
314 l_nc.val[1] = tmp_low;
315 r_nc.val[1] = vextq_f32(tmp_low, tmp_high, 2);
319 tmp_high = vld1q_f32(input + 12);
320 tmp_high1 = vld1q_f32(input + 16);
322 l_nc.val[2] = tmp_low;
323 r_nc.val[2] = vextq_f32(tmp_low, tmp_high, 2);
326 tmp_high = tmp_high1;
328 l_nc.val[3] = tmp_low;
329 r_nc.val[3] = vextq_f32(tmp_low, tmp_high, 2);
331 // mc >= nc.val[0], mc > nc.val[2]
332 mask.val[0] = vandq_u32(mask.val[0], vcgeq_f32(vc.val[0], l_nc.val[0]));
333 mask.val[0] = vandq_u32(mask.val[0], vcgtq_f32(vc.val[0], r_nc.val[0]));
334 mask.val[1] = vandq_u32(mask.val[1], vcgeq_f32(vc.val[1], l_nc.val[1]));
335 mask.val[1] = vandq_u32(mask.val[1], vcgtq_f32(vc.val[1], r_nc.val[1]));
336 mask.val[2] = vandq_u32(mask.val[2], vcgeq_f32(vc.val[2], l_nc.val[2]));
337 mask.val[2] = vandq_u32(mask.val[2], vcgtq_f32(vc.val[2], r_nc.val[2]));
338 mask.val[3] = vandq_u32(mask.val[3], vcgeq_f32(vc.val[3], l_nc.val[3]));
339 mask.val[3] = vandq_u32(mask.val[3], vcgtq_f32(vc.val[3], r_nc.val[3]));
341 input += input_stride;
344 tmp_low = vld1q_f32(input);
345 tmp_high = vld1q_f32(input + 4);
346 tmp_high1 = vld1q_f32(input + 8);
348 l_nc.val[0] = tmp_low;
349 m_nc.val[0] = vextq_f32(tmp_low, tmp_high, 1);
350 r_nc.val[0] = vextq_f32(tmp_low, tmp_high, 2);
353 tmp_high = tmp_high1;
355 l_nc.val[1] = tmp_low;
356 m_nc.val[1] = vextq_f32(tmp_low, tmp_high, 1);
357 r_nc.val[1] = vextq_f32(tmp_low, tmp_high, 2);
361 tmp_high = vld1q_f32(input + 12);
362 tmp_high1 = vld1q_f32(input + 16);
364 l_nc.val[2] = tmp_low;
365 m_nc.val[2] = vextq_f32(tmp_low, tmp_high, 1);
366 r_nc.val[2] = vextq_f32(tmp_low, tmp_high, 2);
369 tmp_high = tmp_high1;
371 l_nc.val[3] = tmp_low;
372 m_nc.val[3] = vextq_f32(tmp_low, tmp_high, 1);
373 r_nc.val[3] = vextq_f32(tmp_low, tmp_high, 2);
375 // mc > nc.val[0], mc > nc.val[1], mc > nc.val[2]
376 mask.val[0] = vandq_u32(mask.val[0], vcgtq_f32(vc.val[0], l_nc.val[0]));
377 mask.val[0] = vandq_u32(mask.val[0], vcgtq_f32(vc.val[0], m_nc.val[0]));
378 mask.val[0] = vandq_u32(mask.val[0], vcgtq_f32(vc.val[0], r_nc.val[0]));
379 mask.val[1] = vandq_u32(mask.val[1], vcgtq_f32(vc.val[1], l_nc.val[1]));
380 mask.val[1] = vandq_u32(mask.val[1], vcgtq_f32(vc.val[1], m_nc.val[1]));
381 mask.val[1] = vandq_u32(mask.val[1], vcgtq_f32(vc.val[1], r_nc.val[1]));
382 mask.val[2] = vandq_u32(mask.val[2], vcgtq_f32(vc.val[2], l_nc.val[2]));
383 mask.val[2] = vandq_u32(mask.val[2], vcgtq_f32(vc.val[2], m_nc.val[2]));
384 mask.val[2] = vandq_u32(mask.val[2], vcgtq_f32(vc.val[2], r_nc.val[2]));
385 mask.val[3] = vandq_u32(mask.val[3], vcgtq_f32(vc.val[3], l_nc.val[3]));
386 mask.val[3] = vandq_u32(mask.val[3], vcgtq_f32(vc.val[3], m_nc.val[3]));
387 mask.val[3] = vandq_u32(mask.val[3], vcgtq_f32(vc.val[3], r_nc.val[3]));
389 static const float32x4_t zero = vdupq_n_f32(0.f);
392 vst1q_f32(output + 0, vbslq_f32(mask.val[0], vc.val[0], zero));
393 vst1q_f32(output + 4, vbslq_f32(mask.val[1], vc.val[1], zero));
394 vst1q_f32(output + 8, vbslq_f32(mask.val[2], vc.val[2], zero));
395 vst1q_f32(output + 12, vbslq_f32(mask.val[3], vc.val[3], zero));
398 inline void non_maxima_suppression3x3_U8_U8(const void *__restrict input_ptr, void *__restrict output_ptr, const uint32_t input_stride)
400 auto input = static_cast<const uint8_t *__restrict>(input_ptr) - 1;
401 const auto output = static_cast<uint8_t *__restrict>(output_ptr);
404 const uint8x16_t vc = vld1q_u8(input + 1);
406 // Neighboring pixels
411 input -= input_stride;
414 l_nc = vld1q_u8(input);
415 m_nc = vld1q_u8(input + 1);
416 r_nc = vld1q_u8(input + 2);
418 // mc >= l_nc, mc >= m_nc, mc >= r_nc
419 uint8x16_t mask = vcgeq_u8(vc, l_nc);
420 mask = vandq_u8(mask, vcgeq_u8(vc, m_nc));
421 mask = vandq_u8(mask, vcgeq_u8(vc, r_nc));
423 input += input_stride;
426 l_nc = vld1q_u8(input);
427 r_nc = vld1q_u8(input + 2);
429 // mc >= l_nc, mc > r_nc
430 mask = vandq_u8(mask, vcgeq_u8(vc, l_nc));
431 mask = vandq_u8(mask, vcgtq_u8(vc, r_nc));
433 input += input_stride;
436 l_nc = vld1q_u8(input);
437 m_nc = vld1q_u8(input + 1);
438 r_nc = vld1q_u8(input + 2);
440 // mc > l_nc, mc > m_nc, mc > r_nc
441 mask = vandq_u8(mask, vcgtq_u8(vc, l_nc));
442 mask = vandq_u8(mask, vcgtq_u8(vc, m_nc));
443 mask = vandq_u8(mask, vcgtq_u8(vc, r_nc));
445 static const uint8x16_t zero = vdupq_n_u8(0);
448 vst1q_u8(output, vbslq_u8(mask, vc, zero));
452 NENonMaximaSuppression3x3Kernel::NENonMaximaSuppression3x3Kernel()
453 : _func(nullptr), _input(nullptr), _output(nullptr)
457 BorderSize NENonMaximaSuppression3x3Kernel::border_size() const
459 return BorderSize(1);
462 void NENonMaximaSuppression3x3Kernel::configure(const ITensor *input, ITensor *output, bool border_undefined)
464 ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::U8, DataType::F32);
465 ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(output, 1, DataType::U8, DataType::F32);
466 ARM_COMPUTE_ERROR_ON_MISMATCHING_DATA_TYPES(input, output);
471 if(input->info()->data_type() == DataType::U8)
473 _func = &non_maxima_suppression3x3_U8_U8;
477 _func = &non_maxima_suppression3x3_FLOAT_FLOAT;
480 constexpr unsigned int num_elems_processed_per_iteration = 16;
481 const unsigned int num_elems_read_per_iteration = 16 + 2 * border_size().left + (input->info()->data_type() == DataType::U8 ? 0 : 3);
482 constexpr unsigned int num_elems_written_per_iteration = 16;
483 constexpr unsigned int num_rows_read_per_iteration = 3;
485 // Configure kernel window
486 Window win = calculate_max_window(*input->info(), Steps(num_elems_processed_per_iteration), border_undefined, border_size());
487 AccessWindowHorizontal output_access(output->info(), 0, num_elems_written_per_iteration);
489 update_window_and_padding(win,
490 AccessWindowRectangle(input->info(), -border_size().left, -border_size().top, num_elems_read_per_iteration, num_rows_read_per_iteration),
493 output_access.set_valid_region(win, input->info()->valid_region(), border_undefined, border_size());
495 INEKernel::configure(win);
498 void NENonMaximaSuppression3x3Kernel::run(const Window &window)
500 ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this);
501 ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(INEKernel::window(), window);
502 ARM_COMPUTE_ERROR_ON(_func == nullptr);
503 Iterator input(_input, window);
504 Iterator output(_output, window);
506 const size_t input_stride = _input->info()->strides_in_bytes()[1] / element_size_from_data_type(_input->info()->data_type());
508 execute_window_loop(window, [&](const Coordinates & id)
510 _func(input.ptr(), output.ptr(), input_stride);