Yair Schwarzbaum | 41a729e | 2021-11-15 20:42:47 +0200 | [diff] [blame] | 1 | /* |
Pablo Marquez Tello | 7e58980 | 2023-09-14 09:41:37 +0100 | [diff] [blame] | 2 | * Copyright (c) 2018-2023 Arm Limited. |
Yair Schwarzbaum | 41a729e | 2021-11-15 20:42:47 +0200 | [diff] [blame] | 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 | #ifndef SRC_CORE_NEON_KERNELS_FUSE_BATCH_NORMALIZATION_IMPL_H |
| 25 | #define SRC_CORE_NEON_KERNELS_FUSE_BATCH_NORMALIZATION_IMPL_H |
| 26 | |
| 27 | #include "arm_compute/core/Helpers.h" |
| 28 | #include "src/core/NEON/wrapper/wrapper.h" |
| 29 | |
| 30 | namespace arm_compute |
| 31 | { |
| 32 | namespace cpu |
| 33 | { |
| 34 | template <typename T> |
| 35 | void fused_batch_normalization_dwc_nhwc(const ITensor *dwc_weights, const ITensor *dwc_bias, ITensor *fused_weights, ITensor *fused_bias, |
Pablo Marquez Tello | 7e58980 | 2023-09-14 09:41:37 +0100 | [diff] [blame] | 36 | 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 / dwc_weights->info()->element_size(); |
| 40 | using ExactTagType = typename wrapper::traits::neon_bitvector_tag_t<T, wrapper::traits::BitWidth::W128>; |
Yair Schwarzbaum | 41a729e | 2021-11-15 20:42:47 +0200 | [diff] [blame] | 41 | |
Pablo Marquez Tello | 7e58980 | 2023-09-14 09:41:37 +0100 | [diff] [blame] | 42 | const bool run_in_place_weights = (fused_weights == nullptr) || (fused_weights == dwc_weights); |
| 43 | const bool run_in_place_bias = (fused_bias == nullptr) || (dwc_bias != nullptr && fused_bias == dwc_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 dwc_w_in(dwc_weights, win); |
| 54 | Iterator dwc_w_out(run_in_place_weights ? dwc_weights : fused_weights, win); |
| 55 | |
| 56 | const auto dwc_bias_in = (dwc_bias != nullptr ? reinterpret_cast<ScalarType *>(dwc_bias->ptr_to_element(Coordinates(0, 0))) : nullptr); |
| 57 | auto dwc_bias_out = (run_in_place_bias ? dwc_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 | auto dwc_bias_vec = wrapper::vdup_n(ScalarType(0), ExactTagType{}); |
| 70 | const auto epsilon_vec = wrapper::vdup_n(ScalarType(epsilon), ExactTagType{}); |
| 71 | |
| 72 | auto gamma = ScalarType(1.0); |
| 73 | auto beta = ScalarType(0.0); |
| 74 | auto dwc_bias_in_scalar = ScalarType(0); |
| 75 | |
| 76 | execute_window_loop(win, [&](const Coordinates & id) |
| 77 | { |
| 78 | int x = window_start_x; |
| 79 | for(; x <= (window_end_x - window_step_x); x += window_step_x) |
| 80 | { |
| 81 | var_vec = wrapper::vloadq(input_var + x); |
| 82 | if(input_gamma != nullptr) |
| 83 | { |
| 84 | gamma_vec = wrapper::vloadq(input_gamma + x); |
| 85 | } |
| 86 | |
| 87 | if((id[2] == 0) && (id[1] == 0)) |
| 88 | { |
| 89 | mean_vec = wrapper::vloadq(input_mean + x); |
| 90 | |
| 91 | // Construct vectors |
| 92 | if(input_beta != nullptr) |
| 93 | { |
| 94 | beta_vec = wrapper::vloadq(input_beta + x); |
| 95 | } |
| 96 | |
| 97 | if(dwc_bias_in != nullptr) |
| 98 | { |
| 99 | dwc_bias_vec = wrapper::vloadq(dwc_bias_in + x); |
| 100 | } |
| 101 | |
| 102 | auto dwc_bias_tmp_vec = wrapper::vmul(wrapper::vsub(dwc_bias_vec, mean_vec), wrapper::vinvsqrt(wrapper::vadd(var_vec, epsilon_vec))); |
| 103 | dwc_bias_tmp_vec = wrapper::vadd(wrapper::vmul(dwc_bias_tmp_vec, gamma_vec), beta_vec); |
| 104 | wrapper::vstore(dwc_bias_out + x, dwc_bias_tmp_vec); |
| 105 | } |
| 106 | |
| 107 | auto dwc_w_in_ptr = reinterpret_cast<const ScalarType *>(dwc_w_in.ptr()); |
| 108 | auto dwc_w_out_ptr = reinterpret_cast<ScalarType *>(dwc_w_out.ptr()); |
| 109 | |
| 110 | auto wn = wrapper::vloadq(dwc_w_in_ptr + x); |
| 111 | rvar_vec = wrapper::vinvsqrt(wrapper::vadd(var_vec, epsilon_vec)); |
| 112 | wn = wrapper::vmul(wn, rvar_vec); |
| 113 | wn = wrapper::vmul(wn, gamma_vec); |
| 114 | |
| 115 | // Store results |
| 116 | wrapper::vstore(dwc_w_out_ptr + x, wn); |
| 117 | } |
| 118 | |
| 119 | // Compute left-over elements |
| 120 | for(; x < window_end_x; ++x) |
| 121 | { |
| 122 | auto var = input_var[x]; |
| 123 | if(input_gamma != nullptr) |
| 124 | { |
| 125 | gamma = input_gamma[x]; |
| 126 | } |
| 127 | |
| 128 | if(id[2] == 0 && id[1] == 0) |
| 129 | { |
| 130 | auto mean = input_mean[x]; |
| 131 | if(input_beta != nullptr) |
| 132 | { |
| 133 | beta = input_beta[x]; |
| 134 | } |
| 135 | if(dwc_bias_in != nullptr) |
| 136 | { |
| 137 | dwc_bias_in_scalar = dwc_bias_in[x]; |
| 138 | } |
| 139 | |
| 140 | auto dwc_bias_tmp_scalar = (dwc_bias_in_scalar - mean) / std::sqrt(var + ScalarType(epsilon)); |
| 141 | dwc_bias_out[x] = (dwc_bias_tmp_scalar * gamma) + beta; |
| 142 | } |
| 143 | |
| 144 | const auto dwc_w_in_ptr = reinterpret_cast<const ScalarType *>(dwc_w_in.ptr()); |
| 145 | auto dwc_w_out_ptr = reinterpret_cast<ScalarType *>(dwc_w_out.ptr()); |
| 146 | |
| 147 | *(dwc_w_out_ptr + x) = *(dwc_w_in_ptr + x) / std::sqrt(var + ScalarType(epsilon)) * gamma; |
| 148 | } |
| 149 | }, |
| 150 | dwc_w_in, dwc_w_out); |
| 151 | } |
Yair Schwarzbaum | 41a729e | 2021-11-15 20:42:47 +0200 | [diff] [blame] | 152 | } // namespace cpu |
| 153 | } // namespace arm_compute |
| 154 | #endif //SRC_CORE_NEON_KERNELS_FUSE_BATCH_NORMALIZATION_IMPL_H |