blob: b9017600d6332355d3d03ef3108fd415f3e2d466 [file] [log] [blame]
Yair Schwarzbaum41a729e2021-11-15 20:42:47 +02001/*
Pablo Marquez Tellocea70602023-08-17 16:18:17 +01002 * Copyright (c) 2021-2023 Arm Limited.
Yair Schwarzbaum41a729e2021-11-15 20:42:47 +02003 *
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#ifndef SRC_CORE_NEON_KERNELS_FUSE_BATCH_NORMALIZATION_GENERIC_IMPL_H
25#define SRC_CORE_NEON_KERNELS_FUSE_BATCH_NORMALIZATION_GENERIC_IMPL_H
26
27#include "arm_compute/core/Helpers.h"
28#include "src/core/NEON/wrapper/wrapper.h"
29
30namespace arm_compute
31{
32namespace cpu
33{
34template <typename T>
35void fused_batch_normalization_conv(const ITensor *conv_weights, const ITensor *conv_bias, ITensor *fused_weights, ITensor *fused_bias,
Pablo Marquez Tellocea70602023-08-17 16:18:17 +010036 const ITensor *bn_mean, const ITensor *bn_var, const ITensor *bn_beta, const ITensor *bn_gamma, float epsilon, const Window &window)
37{
38 using ScalarType = T;
39 const int size = 16 / conv_weights->info()->element_size();
40 using ExactTagType = typename wrapper::traits::neon_bitvector_tag_t<T, wrapper::traits::BitWidth::W128>;
41
42 const bool run_in_place_weights = (fused_weights == nullptr) || (fused_weights == conv_weights);
43 const bool run_in_place_bias = (fused_bias == nullptr) || (conv_bias != nullptr && fused_bias == conv_bias);
44
45 // Set build options
46 Window win = window;
47 win.set(Window::DimX, Window::Dimension(0, 1, 1));
48
49 const int window_step_x = size;
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 Iterator conv_w_in(conv_weights, win);
54 Iterator conv_w_out(run_in_place_weights ? conv_weights : fused_weights, win);
55
56 const auto conv_bias_in = (conv_bias != nullptr ? reinterpret_cast<ScalarType *>(conv_bias->ptr_to_element(Coordinates(0, 0))) : nullptr);
57 auto conv_bias_out = (run_in_place_bias ? conv_bias_in : reinterpret_cast<ScalarType *>(fused_bias->ptr_to_element(Coordinates(0, 0))));
58
59 const auto input_mean = reinterpret_cast<const ScalarType *>(bn_mean->ptr_to_element(Coordinates(0, 0)));
60 const auto input_var = reinterpret_cast<const ScalarType *>(bn_var->ptr_to_element(Coordinates(0, 0)));
61 const auto input_gamma = (bn_gamma != nullptr) ? reinterpret_cast<const ScalarType *>(bn_gamma->ptr_to_element(Coordinates(0, 0))) : nullptr;
62 const auto input_beta = (bn_beta != nullptr) ? reinterpret_cast<const ScalarType *>(bn_beta->ptr_to_element(Coordinates(0, 0))) : nullptr;
63
64 auto mean_vec = wrapper::vdup_n(ScalarType(0), ExactTagType{});
65 auto var_vec = wrapper::vdup_n(ScalarType(0), ExactTagType{});
66 auto gamma_vec = wrapper::vdup_n(ScalarType(1), ExactTagType{});
67 auto beta_vec = wrapper::vdup_n(ScalarType(0), ExactTagType{});
68 auto rvar_vec = wrapper::vdup_n(ScalarType(0), ExactTagType{});
69 const auto epsilon_vec = wrapper::vdup_n(ScalarType(epsilon), ExactTagType{});
70
71 auto mean = ScalarType(0.0);
72 auto var = ScalarType(0.0);
73 auto gamma = ScalarType(1.0);
74 auto beta = ScalarType(0.0);
75 auto conv_bias_in_scalar = ScalarType(0.0);
76 execute_window_loop(win, [&](const Coordinates & id)
77 {
78 var = input_var[id[3]];
79 if(input_gamma != nullptr)
80 {
81 gamma = input_gamma[id[3]];
82 }
83
84 if((id[0] == 0) && (id[1] == 0) && (id[2] == 0))
85 {
86 if(input_beta != nullptr)
87 {
88 beta = input_beta[id[3]];
89 beta_vec = wrapper::vdup_n(beta, ExactTagType{});
90 }
91
92 // Construct vectors
93 mean = input_mean[id[3]];
94 mean_vec = wrapper::vdup_n(mean, ExactTagType{});
95
96 if(conv_bias_in != nullptr)
97 {
98 conv_bias_in_scalar = conv_bias_in[id[3]];
99 }
100 auto conv_bias_tmp_scalar = (conv_bias_in_scalar - mean) / std::sqrt(var + ScalarType(epsilon));
101 conv_bias_out[id[3]] = (conv_bias_tmp_scalar * gamma) + beta;
102 }
103
104 int x = window_start_x;
105 auto conv_w_in_ptr = reinterpret_cast<const ScalarType *>(conv_w_in.ptr());
106 auto conv_w_out_ptr = reinterpret_cast<ScalarType *>(conv_w_out.ptr());
107 var_vec = wrapper::vdup_n(var, ExactTagType{});
108 gamma_vec = wrapper::vdup_n(gamma, ExactTagType{});
109 rvar_vec = wrapper::vinvsqrt(wrapper::vadd(var_vec, epsilon_vec));
110
111 for(; x <= (window_end_x - window_step_x); x += window_step_x)
112 {
113 auto wn = wrapper::vloadq(conv_w_in_ptr + x);
114 wn = wrapper::vmul(wn, rvar_vec);
115 wn = wrapper::vmul(wn, gamma_vec);
116
117 // Store results
118 wrapper::vstore(conv_w_out_ptr + x, wn);
119 }
120
121 // Compute left-over elements
122 for(; x < window_end_x; ++x)
123 {
124 *(conv_w_out_ptr + x) = *(conv_w_in_ptr + x) / std::sqrt(var + ScalarType(epsilon)) * gamma;
125 }
126 },
127 conv_w_in, conv_w_out);
Yair Schwarzbaum41a729e2021-11-15 20:42:47 +0200128}
129}
Pablo Marquez Tellocea70602023-08-17 16:18:17 +0100130}
131#endif //SRC_CORE_NEON_KERNELS_FUSE_BATCH_NORMALIZATION_GENERIC_IMPL_H