xref: /aosp_15_r20/external/ComputeLibrary/src/core/NEON/kernels/batchnormalization/impl/NEON/fp16.cpp (revision c217d954acce2dbc11938adb493fc0abd69584f3)
1 /*
2  * Copyright (c) 2020-2021 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/Helpers.h"
25 #include "arm_compute/core/ITensorPack.h"
26 #include "arm_compute/core/Window.h"
27 #include "src/core/NEON/NEMath.h"
28 #include "src/core/NEON/kernels/detail/NEActivationFunctionDetail.h"
29 #include "src/core/NEON/wrapper/wrapper.h"
30 
31 #include <arm_neon.h>
32 #include <cmath>
33 #include <cstddef>
34 
35 #if defined(__ARM_FEATURE_FP16_VECTOR_ARITHMETIC) && defined(ENABLE_FP16_KERNELS)
36 namespace arm_compute
37 {
38 namespace
39 {
40 using BatchNomalizationPtr = void (*)(ITensor *src, ITensor *dst, const ITensor *mean, const ITensor *var, const ITensor *beta, const ITensor *gamma,
41                                       float epsilon, ActivationLayerInfo &act_info, const Window &window);
42 
43 template <typename T>
batch_normalization(ITensor * src,ITensor * dst,const ITensor * mean,const ITensor * var,const ITensor * beta,const ITensor * gamma,float epsilon,ActivationLayerInfo & act_info,const Window & window)44 void batch_normalization(ITensor *src, ITensor *dst, const ITensor *mean, const ITensor *var, const ITensor *beta, const ITensor *gamma,
45                          float epsilon, ActivationLayerInfo &act_info, const Window &window)
46 {
47     /** SIMD vector tag type. */
48     using ExactTagType = typename wrapper::traits::neon_bitvector_tag_t<float16_t, wrapper::traits::BitWidth::W128>;
49 
50     const int  window_step_x  = 8;
51     const auto window_start_x = static_cast<int>(window.x().start());
52     const auto window_end_x   = static_cast<int>(window.x().end());
53 
54     Window win_collapsed = window.collapse_if_possible(window, Window::DimZ);
55     win_collapsed.set(Window::DimX, Window::Dimension(0, 1, 1));
56 
57     Iterator input(src, win_collapsed);
58     Iterator output(dst, win_collapsed);
59 
60     const auto input_mean  = reinterpret_cast<const float16_t *>(mean->ptr_to_element(Coordinates(0, 0)));
61     const auto input_var   = reinterpret_cast<const float16_t *>(var->ptr_to_element(Coordinates(0, 0)));
62     const auto input_gamma = (gamma != nullptr) ? reinterpret_cast<const float16_t *>(gamma->ptr_to_element(Coordinates(0, 0))) : nullptr;
63     const auto input_beta  = (beta != nullptr) ? reinterpret_cast<const float16_t *>(beta->ptr_to_element(Coordinates(0, 0))) : nullptr;
64 
65     T activation_functor(act_info);
66 
67     const auto epsilon_vec = wrapper::vdup_n(static_cast<float16_t>(epsilon), ExactTagType{});
68     execute_window_loop(win_collapsed, [&](const Coordinates &)
69     {
70         const auto input_ptr  = reinterpret_cast<const float16_t *>(input.ptr());
71         const auto output_ptr = reinterpret_cast<float16_t *>(output.ptr());
72 
73         // Perform core calculations using vector operations
74         int x = window_start_x;
75         for(; x <= (window_end_x - window_step_x); x += window_step_x)
76         {
77             // Conctruct vectors
78             const auto mean_vec  = wrapper::vloadq(input_mean + x);
79             const auto var_vec   = wrapper::vloadq(input_var + x);
80             const auto gamma_vec = (input_gamma != nullptr) ? wrapper::vloadq(input_gamma + x) : wrapper::vdup_n(static_cast<float16_t>(1.f), ExactTagType{});
81             const auto beta_vec  = (input_beta != nullptr) ? wrapper::vloadq(input_beta + x) : wrapper::vdup_n(static_cast<float16_t>(0.f), ExactTagType{});
82 
83             // Calculate denominator
84             const auto denominator = wrapper::vinvsqrt(wrapper::vadd(var_vec, epsilon_vec));
85 
86             // Calculate x bar
87             const auto numerator = wrapper::vsub(wrapper::vloadq(input_ptr + x), mean_vec);
88             const auto x_bar     = wrapper::vmul(numerator, denominator);
89             auto       res       = wrapper::vmla(beta_vec, x_bar, gamma_vec);
90 
91             // Perform fused activation
92             if(act_info.enabled())
93             {
94                 activation_functor(res);
95             }
96 
97             // Store results
98             wrapper::vstore(output_ptr + x, res);
99         }
100 
101         // Compute left-over elements
102         for(; x < window_end_x; ++x)
103         {
104             // Conctruct vectors
105             const float16_t gamma = (input_gamma != nullptr) ? input_gamma[x] : 1.f;
106             const float16_t beta  = (input_beta != nullptr) ? input_beta[x] : 0.f;
107 
108             const float16_t denominator = sqrt(input_var[x] + epsilon);
109             const float16_t numerator   = input_ptr[x] - input_mean[x];
110             const float16_t x_bar       = numerator / denominator;
111             float16_t       res         = beta + x_bar * gamma;
112 
113             // Perform fused activation
114             if(act_info.enabled())
115             {
116                 activation_functor(res);
117             }
118 
119             // Store results
120             *reinterpret_cast<float16_t *>(output_ptr + x) = res;
121         }
122     },
123     input, output);
124 }
125 
126 // Fused Batched Normalization with activation functions
127 static std::map<ActivationLayerInfo::ActivationFunction, BatchNomalizationPtr> fused_map =
128 {
129     { ActivationLayerInfo::ActivationFunction::RELU, &batch_normalization<detail::relu<float16_t, 8>> },
130     { ActivationLayerInfo::ActivationFunction::BOUNDED_RELU, &batch_normalization<detail::brelu<float16_t, 8>> },
131     { ActivationLayerInfo::ActivationFunction::LU_BOUNDED_RELU, &batch_normalization<detail::lubrelu<float16_t, 8>> }
132 };
133 }
134 namespace cpu
135 {
fp16_neon_batch_normalization(ITensor * src,ITensor * dst,const ITensor * mean,const ITensor * var,const ITensor * beta,const ITensor * gamma,float epsilon,ActivationLayerInfo & act_info,const Window & window)136 void fp16_neon_batch_normalization(ITensor *src, ITensor *dst, const ITensor *mean, const ITensor *var, const ITensor *beta, const ITensor *gamma,
137                                    float epsilon, ActivationLayerInfo &act_info, const Window &window)
138 {
139     if(act_info.enabled())
140     {
141         fused_map[act_info.activation()](src, dst, mean, var, beta, gamma, epsilon, act_info, window);
142     }
143     else
144     {
145         batch_normalization<detail::dummy<float16_t, 8>>(src, dst, mean, var, beta, gamma, epsilon, act_info, window);
146     }
147 }
148 } // namespace cpu
149 } // namespace arm_compute
150 
151 #endif /* defined(__ARM_FEATURE_FP16_VECTOR_ARITHMETIC) && defined(ENABLE_FP16_KERNELS) */
152