xref: /aosp_15_r20/external/ComputeLibrary/src/cpu/kernels/genproposals/generic/neon/impl.cpp (revision c217d954acce2dbc11938adb493fc0abd69584f3)
1 /*
2  * Copyright (c) 2019-2022 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 "src/cpu/kernels/genproposals/generic/neon/impl.h"
25 namespace arm_compute
26 {
27 class ITensor;
28 class Window;
29 namespace cpu
30 {
31 template <typename T>
compute_all_anchors(const ITensor * anchors,ITensor * all_anchors,ComputeAnchorsInfo anchors_info,const Window & window)32 void compute_all_anchors(const ITensor *anchors, ITensor *all_anchors, ComputeAnchorsInfo anchors_info, const Window &window)
33 {
34     Iterator all_anchors_it(all_anchors, window);
35     Iterator anchors_it(all_anchors, window);
36 
37     const size_t num_anchors = anchors->info()->dimension(1);
38     const T      stride      = 1.f / anchors_info.spatial_scale();
39     const size_t feat_width  = anchors_info.feat_width();
40 
41     execute_window_loop(window, [&](const Coordinates & id)
42     {
43         const size_t anchor_offset = id.y() % num_anchors;
44 
45         const auto out_anchor_ptr = reinterpret_cast<T *>(all_anchors_it.ptr());
46         const auto anchor_ptr     = reinterpret_cast<T *>(anchors->ptr_to_element(Coordinates(0, anchor_offset)));
47 
48         const size_t shift_idy = id.y() / num_anchors;
49         const T      shiftx    = (shift_idy % feat_width) * stride;
50         const T      shifty    = (shift_idy / feat_width) * stride;
51 
52         *out_anchor_ptr       = *anchor_ptr + shiftx;
53         *(out_anchor_ptr + 1) = *(1 + anchor_ptr) + shifty;
54         *(out_anchor_ptr + 2) = *(2 + anchor_ptr) + shiftx;
55         *(out_anchor_ptr + 3) = *(3 + anchor_ptr) + shifty;
56     },
57     all_anchors_it);
58 }
59 
60 template void compute_all_anchors<float>(const ITensor *anchors, ITensor *all_anchors, ComputeAnchorsInfo anchors_info, const Window &window);
61 #if defined(__ARM_FEATURE_FP16_VECTOR_ARITHMETIC) && defined(ENABLE_FP16_KERNELS)
62 template void compute_all_anchors<float16_t>(const ITensor *anchors, ITensor *all_anchors, ComputeAnchorsInfo anchors_info, const Window &window);
63 #endif //defined(__ARM_FEATURE_FP16_VECTOR_ARITHMETIC) && defined(ENABLE_FP16_KERNELS)
64 
compute_all_anchors_qasymm16(const ITensor * anchors,ITensor * all_anchors,ComputeAnchorsInfo anchors_info,const Window & window)65 void compute_all_anchors_qasymm16(const ITensor *anchors, ITensor *all_anchors, ComputeAnchorsInfo anchors_info, const Window &window)
66 {
67     Iterator all_anchors_it(all_anchors, window);
68     Iterator anchors_it(all_anchors, window);
69 
70     const size_t num_anchors = anchors->info()->dimension(1);
71     const float  stride      = 1.f / anchors_info.spatial_scale();
72     const size_t feat_width  = anchors_info.feat_width();
73 
74     const UniformQuantizationInfo qinfo = anchors->info()->quantization_info().uniform();
75 
76     execute_window_loop(window, [&](const Coordinates & id)
77     {
78         const size_t anchor_offset = id.y() % num_anchors;
79 
80         const auto out_anchor_ptr = reinterpret_cast<int16_t *>(all_anchors_it.ptr());
81         const auto anchor_ptr     = reinterpret_cast<int16_t *>(anchors->ptr_to_element(Coordinates(0, anchor_offset)));
82 
83         const size_t shift_idy = id.y() / num_anchors;
84         const float  shiftx    = (shift_idy % feat_width) * stride;
85         const float  shifty    = (shift_idy / feat_width) * stride;
86 
87         const float new_anchor_x1 = dequantize_qsymm16(*anchor_ptr, qinfo.scale) + shiftx;
88         const float new_anchor_y1 = dequantize_qsymm16(*(1 + anchor_ptr), qinfo.scale) + shifty;
89         const float new_anchor_x2 = dequantize_qsymm16(*(2 + anchor_ptr), qinfo.scale) + shiftx;
90         const float new_anchor_y2 = dequantize_qsymm16(*(3 + anchor_ptr), qinfo.scale) + shifty;
91 
92         *out_anchor_ptr       = quantize_qsymm16(new_anchor_x1, qinfo.scale);
93         *(out_anchor_ptr + 1) = quantize_qsymm16(new_anchor_y1, qinfo.scale);
94         *(out_anchor_ptr + 2) = quantize_qsymm16(new_anchor_x2, qinfo.scale);
95         *(out_anchor_ptr + 3) = quantize_qsymm16(new_anchor_y2, qinfo.scale);
96     },
97     all_anchors_it);
98 }
99 } // namespace cpu
100 } // namespace arm_compute
101