blob: b8a540158b4040f97d24c392d254734c4c265e6d [file] [log] [blame]
Sheri Zhang8d5d78b2020-12-15 20:25:31 +00001/*
Georgios Pinitas70eb53b2021-01-06 19:42:21 +00002 * Copyright (c) 2020-2021 Arm Limited.
Sheri Zhang8d5d78b2020-12-15 20:25:31 +00003 *
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"
Sheri Zhang8d5d78b2020-12-15 20:25:31 +000028
29#include <cmath>
30#include <cstddef>
31
Michalis Spyrou20fca522021-06-07 14:23:57 +010032#if defined(ARM_COMPUTE_ENABLE_SVE)
Sheri Zhang8d5d78b2020-12-15 20:25:31 +000033#include <arm_sve.h>
34
35namespace arm_compute
36{
37namespace cpu
38{
39void fp32_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 float *>(mean->ptr_to_element(Coordinates(0, 0)));
52 const auto input_var = reinterpret_cast<const float *>(var->ptr_to_element(Coordinates(0, 0)));
53 const auto input_gamma = (gamma != nullptr) ? reinterpret_cast<const float *>(gamma->ptr_to_element(Coordinates(0, 0))) : nullptr;
54 const auto input_beta = (beta != nullptr) ? reinterpret_cast<const float *>(beta->ptr_to_element(Coordinates(0, 0))) : nullptr;
55
56 const auto epsilon_vec = svdup_n_f32(epsilon);
57 const auto const_1 = svdup_n_f32(1.f);
58 const auto const_0 = svdup_n_f32(0.f);
59 const auto va = svdup_n_f32(act_info.a());
60 const auto vb = svdup_n_f32(act_info.b());
61 execute_window_loop(win_collapsed, [&](const Coordinates &)
62 {
63 const auto input_ptr = reinterpret_cast<const float *>(input.ptr());
64 const auto output_ptr = reinterpret_cast<float *>(output.ptr());
65
66 // Compute S elements per iteration
67 int x = window_start_x;
68 svbool_t pg = svwhilelt_b32(x, window_end_x);
69 do
70 {
71 // Conctruct vectors
72 const auto mean_vec = svld1_f32(pg, input_mean + x);
73 const auto var_vec = svld1_f32(pg, input_var + x);
74 const auto gamma_vec = (input_gamma != nullptr) ? svld1_f32(pg, input_gamma + x) : const_1;
75 const auto beta_vec = (input_beta != nullptr) ? svld1_f32(pg, input_beta + x) : const_0;
76
77 // Calculate denominator
78 const auto tmp = svadd_f32_z(pg, var_vec, epsilon_vec);
79 auto denominator = svrsqrte_f32(tmp);
80 denominator = svmul_f32_z(pg, svrsqrts_f32(svmul_f32_z(pg, tmp, denominator), denominator), denominator);
81 denominator = svmul_f32_z(pg, svrsqrts_f32(svmul_f32_z(pg, tmp, denominator), denominator), denominator);
82
83 // Calculate x bar
84 const auto numerator = svsub_f32_z(pg, svld1_f32(pg, input_ptr + x), mean_vec);
85 const auto x_bar = svmul_f32_z(pg, numerator, denominator);
86 auto res = svmla_f32_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_f32_z(pg, const_0, res);
94 }
95 else if(act_info.activation() == ActivationLayerInfo::ActivationFunction::BOUNDED_RELU)
96 {
97 res = svmin_f32_z(pg, va, svmax_f32_z(pg, const_0, res));
98 }
99 else if(act_info.activation() == ActivationLayerInfo::ActivationFunction::LU_BOUNDED_RELU)
100 {
101 res = svmin_f32_z(pg, va, svmax_f32_z(pg, vb, res));
102 }
103 }
104
105 // Store results
106 svst1_f32(pg, output_ptr + x, res);
107
108 x += svcntw();
109 pg = svwhilelt_b32(x, window_end_x);
110 }
111 while(svptest_any(svptrue_b32(), pg));
112 },
113 input, output);
114}
115} // namespace cpu
116} // namespace arm_compute
Georgios Pinitasbdcdc392021-04-22 16:42:03 +0100117#endif // ENABLE_SVE