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