blob: 22089b1d07677eda61e8418cdc546df66b3ce3e5 [file] [log] [blame]
Sheri Zhang8d5d78b2020-12-15 20:25:31 +00001/*
Matthew Benthamf1aeab92023-05-30 13:35:34 +00002 * Copyright (c) 2020-2021,2023 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 */
Matthew Benthamf1aeab92023-05-30 13:35:34 +000024#include "arm_compute/core/ActivationLayerInfo.h"
Sheri Zhang8d5d78b2020-12-15 20:25:31 +000025#include "arm_compute/core/Helpers.h"
26#include "arm_compute/core/ITensorPack.h"
27#include "arm_compute/core/Window.h"
28#include "src/core/NEON/SVEMath.h"
Sheri Zhang8d5d78b2020-12-15 20:25:31 +000029
30#include <cmath>
31#include <cstddef>
32
Michalis Spyrou20fca522021-06-07 14:23:57 +010033#if defined(ARM_COMPUTE_ENABLE_SVE)
Sheri Zhang8d5d78b2020-12-15 20:25:31 +000034#include <arm_sve.h>
35
36namespace arm_compute
37{
38namespace cpu
39{
40void fp32_sve_batch_normalization(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 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(src, win_collapsed);
50 Iterator output(dst, win_collapsed);
51
52 const auto input_mean = reinterpret_cast<const float *>(mean->ptr_to_element(Coordinates(0, 0)));
53 const auto input_var = reinterpret_cast<const float *>(var->ptr_to_element(Coordinates(0, 0)));
54 const auto input_gamma = (gamma != nullptr) ? reinterpret_cast<const float *>(gamma->ptr_to_element(Coordinates(0, 0))) : nullptr;
55 const auto input_beta = (beta != nullptr) ? reinterpret_cast<const float *>(beta->ptr_to_element(Coordinates(0, 0))) : nullptr;
56
57 const auto epsilon_vec = svdup_n_f32(epsilon);
58 const auto const_1 = svdup_n_f32(1.f);
59 const auto const_0 = svdup_n_f32(0.f);
60 const auto va = svdup_n_f32(act_info.a());
61 const auto vb = svdup_n_f32(act_info.b());
62 execute_window_loop(win_collapsed, [&](const Coordinates &)
63 {
64 const auto input_ptr = reinterpret_cast<const float *>(input.ptr());
65 const auto output_ptr = reinterpret_cast<float *>(output.ptr());
66
67 // Compute S elements per iteration
68 int x = window_start_x;
69 svbool_t pg = svwhilelt_b32(x, window_end_x);
70 do
71 {
72 // Conctruct vectors
73 const auto mean_vec = svld1_f32(pg, input_mean + x);
74 const auto var_vec = svld1_f32(pg, input_var + x);
75 const auto gamma_vec = (input_gamma != nullptr) ? svld1_f32(pg, input_gamma + x) : const_1;
76 const auto beta_vec = (input_beta != nullptr) ? svld1_f32(pg, input_beta + x) : const_0;
77
78 // Calculate denominator
79 const auto tmp = svadd_f32_z(pg, var_vec, epsilon_vec);
80 auto denominator = svrsqrte_f32(tmp);
81 denominator = svmul_f32_z(pg, svrsqrts_f32(svmul_f32_z(pg, tmp, denominator), denominator), denominator);
82 denominator = svmul_f32_z(pg, svrsqrts_f32(svmul_f32_z(pg, tmp, denominator), denominator), denominator);
83
84 // Calculate x bar
85 const auto numerator = svsub_f32_z(pg, svld1_f32(pg, input_ptr + x), mean_vec);
86 const auto x_bar = svmul_f32_z(pg, numerator, denominator);
87 auto res = svmla_f32_z(pg, beta_vec, x_bar, gamma_vec);
88
89 // Perform fused activation
90 if(act_info.enabled())
91 {
92 if(act_info.activation() == ActivationLayerInfo::ActivationFunction::RELU)
93 {
94 res = svmax_f32_z(pg, const_0, res);
95 }
96 else if(act_info.activation() == ActivationLayerInfo::ActivationFunction::BOUNDED_RELU)
97 {
98 res = svmin_f32_z(pg, va, svmax_f32_z(pg, const_0, res));
99 }
100 else if(act_info.activation() == ActivationLayerInfo::ActivationFunction::LU_BOUNDED_RELU)
101 {
102 res = svmin_f32_z(pg, va, svmax_f32_z(pg, vb, res));
103 }
104 }
105
106 // Store results
107 svst1_f32(pg, output_ptr + x, res);
108
109 x += svcntw();
110 pg = svwhilelt_b32(x, window_end_x);
111 }
112 while(svptest_any(svptrue_b32(), pg));
113 },
114 input, output);
115}
116} // namespace cpu
117} // namespace arm_compute
Georgios Pinitasbdcdc392021-04-22 16:42:03 +0100118#endif // ENABLE_SVE