xref: /aosp_15_r20/external/ComputeLibrary/src/cpu/kernels/l2normlayer/generic/neon/impl.cpp (revision c217d954acce2dbc11938adb493fc0abd69584f3)
1 /*
2  * Copyright (c) 2017-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/l2normlayer/generic/neon/impl.h"
25 
26 #include "arm_compute/core/Helpers.h"
27 #include "arm_compute/core/TensorInfo.h"
28 #include "src/core/NEON/wrapper/wrapper.h"
29 #include "src/core/common/Registrars.h"
30 
31 #include <cstddef>
32 
33 namespace arm_compute
34 {
35 namespace cpu
36 {
37 template <typename T, int S>
l2_normalize_x(const ITensor * in,const ITensor * sum,ITensor * out,float epsilon,const Window & window)38 void l2_normalize_x(const ITensor *in, const ITensor *sum, ITensor *out, float epsilon, const Window &window)
39 {
40     using ExactTagType = typename wrapper::traits::neon_vector<T, S>::tag_type;
41 
42     const int  window_step_x  = 16 / data_size_from_type(in->info()->data_type());
43     const auto window_start_x = static_cast<int>(window.x().start());
44     const auto window_end_x   = static_cast<int>(window.x().end());
45 
46     Window win_collapsed = window.collapse_if_possible(window, Window::DimZ);
47     win_collapsed.set(Window::DimX, Window::Dimension(0, 1, 1));
48 
49     Iterator input_it(in, win_collapsed);
50     Iterator sum_it(sum, win_collapsed);
51     Iterator output_it(out, win_collapsed);
52 
53     execute_window_loop(win_collapsed, [&](const Coordinates &)
54     {
55         const auto in_ptr  = reinterpret_cast<const T *>(input_it.ptr());
56         const auto out_ptr = reinterpret_cast<T *>(output_it.ptr());
57 
58         const T    sum_value      = *reinterpret_cast<const T *>(sum_it.ptr());
59         const T    norm_value     = static_cast<T>(1.f) / std::sqrt(std::max(sum_value, static_cast<T>(epsilon)));
60         const auto vec_norm_value = wrapper::vdup_n(norm_value, ExactTagType{});
61 
62         // Compute elements over vector steps
63         int x = window_start_x;
64         for(; x <= (window_end_x - window_step_x); x += window_step_x)
65         {
66             wrapper::vstore(out_ptr + x, wrapper::vmul(wrapper::vloadq(in_ptr + x), vec_norm_value));
67         }
68 
69         // Compute left-over elements
70         for(; x < window_end_x; ++x)
71         {
72             out_ptr[x] = in_ptr[x] * norm_value;
73         }
74     },
75     input_it, sum_it, output_it);
76 }
77 
78 template <typename T, int S>
l2_normalize_yz(const ITensor * in,const ITensor * sum,ITensor * out,float epsilon,const Window & window,size_t axis)79 void l2_normalize_yz(const ITensor *in, const ITensor *sum, ITensor *out, float epsilon, const Window &window, size_t axis)
80 {
81     using ExactTagType = typename wrapper::traits::neon_vector<T, S>::tag_type;
82 
83     const int  window_step_x  = 16 / data_size_from_type(in->info()->data_type());
84     const auto window_start_x = static_cast<int>(window.x().start());
85     const auto window_end_x   = static_cast<int>(window.x().end());
86 
87     Window win = window;
88     win.set(Window::DimX, Window::Dimension(0, 1, 1));
89 
90     Window window_sum(win);
91     window_sum.set(axis, Window::Dimension(0, 0, 0));
92 
93     Iterator input_it(in, win);
94     Iterator sum_it(sum, window_sum);
95     Iterator output_it(out, win);
96 
97     const auto vec_eps = wrapper::vdup_n(static_cast<T>(epsilon), ExactTagType{});
98 
99     execute_window_loop(win, [&](const Coordinates &)
100     {
101         const auto in_ptr  = reinterpret_cast<const T *>(input_it.ptr());
102         const auto sum_ptr = reinterpret_cast<const T *>(sum_it.ptr());
103         const auto out_ptr = reinterpret_cast<T *>(output_it.ptr());
104 
105         // Compute elements over vector steps
106         int x = window_start_x;
107         for(; x <= (window_end_x - window_step_x); x += window_step_x)
108         {
109             const auto vec_norm_value = wrapper::vinvsqrt(wrapper::vmax(wrapper::vloadq(sum_ptr + x), vec_eps));
110             wrapper::vstore(out_ptr + x, wrapper::vmul(wrapper::vloadq(in_ptr + x), vec_norm_value));
111         }
112 
113         // Compute left-over elements
114         for(; x < window_end_x; ++x)
115         {
116             const T norm_value = static_cast<T>(1.f) / std::sqrt(std::max(sum_ptr[x], static_cast<T>(epsilon)));
117             out_ptr[x]         = in_ptr[x] * norm_value;
118         }
119     },
120     input_it, sum_it, output_it);
121 }
122 
123 template void l2_normalize_yz<float, 4>(const ITensor *in, const ITensor *sum, ITensor *out, float epsilon, const Window &window, size_t axis);
124 template void l2_normalize_x<float, 4>(const ITensor *in, const ITensor *sum, ITensor *out, float epsilon, const Window &window);
125 
126 #if defined(__ARM_FEATURE_FP16_VECTOR_ARITHMETIC) && defined(ENABLE_FP16_KERNELS)
127 template void l2_normalize_yz<float16_t, 8>(const ITensor *in, const ITensor *sum, ITensor *out, float epsilon, const Window &window, size_t axis);
128 template void l2_normalize_x<float16_t, 8>(const ITensor *in, const ITensor *sum, ITensor *out, float epsilon, const Window &window);
129 #endif //defined(__ARM_FEATURE_FP16_VECTOR_ARITHMETIC) && defined(ENABLE_FP16_KERNELS)
130 } // namespace cpu
131 } // namespace arm_compute
132