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/SVEMath.h"
28
29 #include <cmath>
30 #include <cstddef>
31
32 #if defined(ARM_COMPUTE_ENABLE_SVE)
33 #include <arm_sve.h>
34
35 namespace arm_compute
36 {
37 namespace cpu
38 {
fp16_sve_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)39 void fp16_sve_batch_normalization(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 const auto window_start_x = static_cast<int>(window.x().start());
43 const auto window_end_x = static_cast<int>(window.x().end());
44
45 Window win_collapsed = window.collapse_if_possible(window, Window::DimZ);
46 win_collapsed.set(Window::DimX, Window::Dimension(0, 1, 1));
47
48 Iterator input(src, win_collapsed);
49 Iterator output(dst, win_collapsed);
50
51 const auto input_mean = reinterpret_cast<const float16_t *>(mean->ptr_to_element(Coordinates(0, 0)));
52 const auto input_var = reinterpret_cast<const float16_t *>(var->ptr_to_element(Coordinates(0, 0)));
53 const auto input_gamma = (gamma != nullptr) ? reinterpret_cast<const float16_t *>(gamma->ptr_to_element(Coordinates(0, 0))) : nullptr;
54 const auto input_beta = (beta != nullptr) ? reinterpret_cast<const float16_t *>(beta->ptr_to_element(Coordinates(0, 0))) : nullptr;
55
56 const auto epsilon_vec = svdup_n_f16(epsilon);
57 const auto const_1 = svdup_n_f16(1.f);
58 const auto const_0 = svdup_n_f16(0.f);
59 const auto va = svdup_n_f16(act_info.a());
60 const auto vb = svdup_n_f16(act_info.b());
61 execute_window_loop(win_collapsed, [&](const Coordinates &)
62 {
63 const auto input_ptr = reinterpret_cast<const float16_t *>(input.ptr());
64 const auto output_ptr = reinterpret_cast<float16_t *>(output.ptr());
65
66 // Compute S elements per iteration
67 int x = window_start_x;
68 svbool_t pg = svwhilelt_b16(x, window_end_x);
69 do
70 {
71 // Conctruct vectors
72 const auto mean_vec = svld1_f16(pg, input_mean + x);
73 const auto var_vec = svld1_f16(pg, input_var + x);
74 const auto gamma_vec = (input_gamma != nullptr) ? svld1_f16(pg, input_gamma + x) : const_1;
75 const auto beta_vec = (input_beta != nullptr) ? svld1_f16(pg, input_beta + x) : const_0;
76
77 // Calculate denominator
78 const auto tmp = svadd_f16_z(pg, var_vec, epsilon_vec);
79 auto denominator = svrsqrte_f16(tmp);
80 denominator = svmul_f16_z(pg, svrsqrts_f16(svmul_f16_z(pg, tmp, denominator), denominator), denominator);
81 denominator = svmul_f16_z(pg, svrsqrts_f16(svmul_f16_z(pg, tmp, denominator), denominator), denominator);
82
83 // Calculate x bar
84 const auto numerator = svsub_f16_z(pg, svld1_f16(pg, input_ptr + x), mean_vec);
85 const auto x_bar = svmul_f16_z(pg, numerator, denominator);
86 auto res = svmla_f16_z(pg, beta_vec, x_bar, gamma_vec);
87
88 // Perform fused activation
89 if(act_info.enabled())
90 {
91 if(act_info.activation() == ActivationLayerInfo::ActivationFunction::RELU)
92 {
93 res = svmax_f16_z(pg, const_0, res);
94 }
95 else if(act_info.activation() == ActivationLayerInfo::ActivationFunction::BOUNDED_RELU)
96 {
97 res = svmin_f16_z(pg, va, svmax_f16_z(pg, const_0, res));
98 }
99 else if(act_info.activation() == ActivationLayerInfo::ActivationFunction::LU_BOUNDED_RELU)
100 {
101 res = svmin_f16_z(pg, va, svmax_f16_z(pg, vb, res));
102 }
103 }
104
105 // Store results
106 svst1_f16(pg, output_ptr + x, res);
107
108 x += svcntw();
109 pg = svwhilelt_b16(x, window_end_x);
110 }
111 while(svptest_any(svptrue_b16(), pg));
112 },
113 input, output);
114 }
115 } // namespace cpu
116 } // namespace arm_compute
117 #endif // ENABLE_SVE
118