giuros01 | 15ecc9a | 2018-12-06 10:47:34 +0000 | [diff] [blame] | 1 | /* |
Michele Di Giorgio | d9eaf61 | 2020-07-08 11:12:57 +0100 | [diff] [blame] | 2 | * Copyright (c) 2018-2020 Arm Limited. |
giuros01 | 15ecc9a | 2018-12-06 10:47:34 +0000 | [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 |
Michalis Spyrou | aeebe4a | 2019-01-09 14:21:03 +0000 | [diff] [blame] | 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
giuros01 | 15ecc9a | 2018-12-06 10:47:34 +0000 | [diff] [blame] | 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
Michalis Spyrou | aeebe4a | 2019-01-09 14:21:03 +0000 | [diff] [blame] | 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
giuros01 | 15ecc9a | 2018-12-06 10:47:34 +0000 | [diff] [blame] | 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/NEON/kernels/NEFuseBatchNormalizationKernel.h" |
| 25 | |
Georgios Pinitas | 8f5802f | 2019-02-22 11:08:32 +0000 | [diff] [blame] | 26 | #include "arm_compute/core/CPP/Validate.h" |
giuros01 | 15ecc9a | 2018-12-06 10:47:34 +0000 | [diff] [blame] | 27 | #include "arm_compute/core/Helpers.h" |
| 28 | #include "arm_compute/core/ITensor.h" |
Manuel Bottini | 1109176 | 2019-06-17 12:04:40 +0100 | [diff] [blame] | 29 | #include "arm_compute/core/NEON/wrapper/wrapper.h" |
giuros01 | 15ecc9a | 2018-12-06 10:47:34 +0000 | [diff] [blame] | 30 | #include "arm_compute/core/TensorInfo.h" |
Georgios Pinitas | 8f5802f | 2019-02-22 11:08:32 +0000 | [diff] [blame] | 31 | #include "arm_compute/core/Utils.h" |
| 32 | #include "arm_compute/core/Validate.h" |
| 33 | #include "arm_compute/core/Window.h" |
giuros01 | 15ecc9a | 2018-12-06 10:47:34 +0000 | [diff] [blame] | 34 | |
Manuel Bottini | 1109176 | 2019-06-17 12:04:40 +0100 | [diff] [blame] | 35 | #include <map> |
| 36 | |
giuros01 | 15ecc9a | 2018-12-06 10:47:34 +0000 | [diff] [blame] | 37 | namespace arm_compute |
| 38 | { |
| 39 | namespace |
| 40 | { |
Manuel Bottini | 1109176 | 2019-06-17 12:04:40 +0100 | [diff] [blame] | 41 | Status validate_arguments(const ITensorInfo *input_weights, const ITensorInfo *bn_mean, const ITensorInfo *bn_var, |
giuros01 | 15ecc9a | 2018-12-06 10:47:34 +0000 | [diff] [blame] | 42 | const ITensorInfo *fused_weights, const ITensorInfo *fused_bias, |
Manuel Bottini | 1109176 | 2019-06-17 12:04:40 +0100 | [diff] [blame] | 43 | const ITensorInfo *input_bias, const ITensorInfo *bn_beta, const ITensorInfo *bn_gamma, |
| 44 | float epsilon, FuseBatchNormalizationType fbn_type) |
giuros01 | 15ecc9a | 2018-12-06 10:47:34 +0000 | [diff] [blame] | 45 | { |
| 46 | ARM_COMPUTE_UNUSED(epsilon); |
Manuel Bottini | 1109176 | 2019-06-17 12:04:40 +0100 | [diff] [blame] | 47 | ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(input_weights, bn_mean, bn_var); |
| 48 | ARM_COMPUTE_RETURN_ERROR_ON_CPU_F16_UNSUPPORTED(input_weights); |
| 49 | ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input_weights, 1, DataType::F16, DataType::F32); |
giuros01 | 15ecc9a | 2018-12-06 10:47:34 +0000 | [diff] [blame] | 50 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_SHAPES(bn_mean, bn_var); |
Manuel Bottini | 1109176 | 2019-06-17 12:04:40 +0100 | [diff] [blame] | 51 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input_weights, bn_mean, bn_var); |
| 52 | ARM_COMPUTE_RETURN_ERROR_ON(input_bias == nullptr && fused_bias == nullptr); |
| 53 | ARM_COMPUTE_RETURN_ERROR_ON(bn_mean->num_dimensions() > 1); |
giuros01 | 15ecc9a | 2018-12-06 10:47:34 +0000 | [diff] [blame] | 54 | |
Manuel Bottini | 1109176 | 2019-06-17 12:04:40 +0100 | [diff] [blame] | 55 | if(fbn_type == FuseBatchNormalizationType::CONVOLUTION) |
giuros01 | 15ecc9a | 2018-12-06 10:47:34 +0000 | [diff] [blame] | 56 | { |
Manuel Bottini | 1109176 | 2019-06-17 12:04:40 +0100 | [diff] [blame] | 57 | ARM_COMPUTE_RETURN_ERROR_ON(input_weights->dimension(3) != bn_mean->dimension(0)); |
| 58 | } |
| 59 | else |
| 60 | { |
| 61 | const size_t channel_idx = get_data_layout_dimension_index(input_weights->data_layout(), DataLayoutDimension::CHANNEL); |
| 62 | ARM_COMPUTE_RETURN_ERROR_ON(input_weights->dimension(channel_idx) != bn_mean->dimension(0)); |
| 63 | } |
| 64 | // Validate bias |
| 65 | if(input_bias != nullptr) |
| 66 | { |
| 67 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_SHAPES(bn_mean, input_bias); |
| 68 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input_weights, input_bias); |
giuros01 | 15ecc9a | 2018-12-06 10:47:34 +0000 | [diff] [blame] | 69 | } |
| 70 | // Validate beta |
| 71 | if(bn_beta != nullptr) |
| 72 | { |
| 73 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_SHAPES(bn_mean, bn_beta); |
Manuel Bottini | 1109176 | 2019-06-17 12:04:40 +0100 | [diff] [blame] | 74 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input_weights, bn_beta); |
giuros01 | 15ecc9a | 2018-12-06 10:47:34 +0000 | [diff] [blame] | 75 | } |
| 76 | // Validate gamma |
| 77 | if(bn_gamma != nullptr) |
| 78 | { |
| 79 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_SHAPES(bn_mean, bn_gamma); |
Manuel Bottini | 1109176 | 2019-06-17 12:04:40 +0100 | [diff] [blame] | 80 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input_weights, bn_gamma); |
giuros01 | 15ecc9a | 2018-12-06 10:47:34 +0000 | [diff] [blame] | 81 | } |
| 82 | |
| 83 | // Validate output weights |
| 84 | if(fused_weights != nullptr && fused_weights->total_size() != 0) |
| 85 | { |
Manuel Bottini | 1109176 | 2019-06-17 12:04:40 +0100 | [diff] [blame] | 86 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_SHAPES(input_weights, fused_weights); |
| 87 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_LAYOUT(input_weights, fused_weights); |
| 88 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input_weights, fused_weights); |
giuros01 | 15ecc9a | 2018-12-06 10:47:34 +0000 | [diff] [blame] | 89 | } |
| 90 | // Validate output bias |
| 91 | if(fused_bias != nullptr && fused_bias->total_size() != 0) |
| 92 | { |
| 93 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_SHAPES(bn_mean, fused_bias); |
Manuel Bottini | 1109176 | 2019-06-17 12:04:40 +0100 | [diff] [blame] | 94 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input_weights, fused_bias); |
giuros01 | 15ecc9a | 2018-12-06 10:47:34 +0000 | [diff] [blame] | 95 | } |
| 96 | |
| 97 | return Status{}; |
| 98 | } |
| 99 | |
Manuel Bottini | 1109176 | 2019-06-17 12:04:40 +0100 | [diff] [blame] | 100 | template <typename VectorType> |
| 101 | void fused_batch_normalization_conv(const ITensor *conv_weights, const ITensor *conv_bias, ITensor *fused_weights, ITensor *fused_bias, |
| 102 | const ITensor *bn_mean, const ITensor *bn_var, const ITensor *bn_beta, const ITensor *bn_gamma, float epsilon, const Window &window) |
giuros01 | 15ecc9a | 2018-12-06 10:47:34 +0000 | [diff] [blame] | 103 | { |
Manuel Bottini | 1109176 | 2019-06-17 12:04:40 +0100 | [diff] [blame] | 104 | using ScalarType = typename VectorType::scalar_type; |
| 105 | const int size = 16 / conv_weights->info()->element_size(); |
| 106 | using ExactTagType = typename VectorType::tag_type; |
giuros01 | 15ecc9a | 2018-12-06 10:47:34 +0000 | [diff] [blame] | 107 | |
| 108 | const bool run_in_place_weights = (fused_weights == nullptr) || (fused_weights == conv_weights); |
| 109 | const bool run_in_place_bias = (fused_bias == nullptr) || (conv_bias != nullptr && fused_bias == conv_bias); |
| 110 | |
| 111 | // Set build options |
| 112 | Window win = window; |
| 113 | win.set(Window::DimX, Window::Dimension(0, 1, 1)); |
| 114 | |
| 115 | const int window_step_x = size; |
| 116 | const auto window_start_x = static_cast<int>(window.x().start()); |
| 117 | const auto window_end_x = static_cast<int>(window.x().end()); |
| 118 | |
| 119 | Iterator conv_w_in(conv_weights, win); |
| 120 | Iterator conv_w_out(run_in_place_weights ? conv_weights : fused_weights, win); |
| 121 | |
| 122 | const auto conv_bias_in = (conv_bias != nullptr ? reinterpret_cast<ScalarType *>(conv_bias->ptr_to_element(Coordinates(0, 0))) : nullptr); |
| 123 | auto conv_bias_out = (run_in_place_bias ? conv_bias_in : reinterpret_cast<ScalarType *>(fused_bias->ptr_to_element(Coordinates(0, 0)))); |
| 124 | |
giuros01 | 15ecc9a | 2018-12-06 10:47:34 +0000 | [diff] [blame] | 125 | const auto input_mean = reinterpret_cast<const ScalarType *>(bn_mean->ptr_to_element(Coordinates(0, 0))); |
| 126 | const auto input_var = reinterpret_cast<const ScalarType *>(bn_var->ptr_to_element(Coordinates(0, 0))); |
| 127 | const auto input_gamma = (bn_gamma != nullptr) ? reinterpret_cast<const ScalarType *>(bn_gamma->ptr_to_element(Coordinates(0, 0))) : nullptr; |
| 128 | const auto input_beta = (bn_beta != nullptr) ? reinterpret_cast<const ScalarType *>(bn_beta->ptr_to_element(Coordinates(0, 0))) : nullptr; |
| 129 | |
| 130 | auto mean_vec = wrapper::vdup_n(ScalarType(0), ExactTagType{}); |
| 131 | auto var_vec = wrapper::vdup_n(ScalarType(0), ExactTagType{}); |
| 132 | auto gamma_vec = wrapper::vdup_n(ScalarType(1), ExactTagType{}); |
| 133 | auto beta_vec = wrapper::vdup_n(ScalarType(0), ExactTagType{}); |
| 134 | auto rvar_vec = wrapper::vdup_n(ScalarType(0), ExactTagType{}); |
| 135 | const auto epsilon_vec = wrapper::vdup_n(ScalarType(epsilon), ExactTagType{}); |
| 136 | |
| 137 | auto mean = ScalarType(0.0); |
| 138 | auto var = ScalarType(0.0); |
| 139 | auto gamma = ScalarType(1.0); |
| 140 | auto beta = ScalarType(0.0); |
| 141 | auto conv_bias_in_scalar = ScalarType(0.0); |
| 142 | execute_window_loop(win, [&](const Coordinates & id) |
| 143 | { |
Manuel Bottini | 1109176 | 2019-06-17 12:04:40 +0100 | [diff] [blame] | 144 | var = input_var[id[3]]; |
| 145 | if(input_gamma != nullptr) |
giuros01 | 15ecc9a | 2018-12-06 10:47:34 +0000 | [diff] [blame] | 146 | { |
Manuel Bottini | 1109176 | 2019-06-17 12:04:40 +0100 | [diff] [blame] | 147 | gamma = input_gamma[id[3]]; |
| 148 | } |
giuros01 | 15ecc9a | 2018-12-06 10:47:34 +0000 | [diff] [blame] | 149 | |
Manuel Bottini | 1109176 | 2019-06-17 12:04:40 +0100 | [diff] [blame] | 150 | if((id[0] == 0) && (id[1] == 0) && (id[2] == 0)) |
| 151 | { |
giuros01 | 15ecc9a | 2018-12-06 10:47:34 +0000 | [diff] [blame] | 152 | if(input_beta != nullptr) |
| 153 | { |
Manuel Bottini | 1109176 | 2019-06-17 12:04:40 +0100 | [diff] [blame] | 154 | beta = input_beta[id[3]]; |
giuros01 | 15ecc9a | 2018-12-06 10:47:34 +0000 | [diff] [blame] | 155 | beta_vec = wrapper::vdup_n(beta, ExactTagType{}); |
| 156 | } |
Manuel Bottini | 1109176 | 2019-06-17 12:04:40 +0100 | [diff] [blame] | 157 | |
| 158 | // Construct vectors |
| 159 | mean = input_mean[id[3]]; |
| 160 | mean_vec = wrapper::vdup_n(mean, ExactTagType{}); |
| 161 | |
giuros01 | 15ecc9a | 2018-12-06 10:47:34 +0000 | [diff] [blame] | 162 | if(conv_bias_in != nullptr) |
| 163 | { |
Manuel Bottini | 1109176 | 2019-06-17 12:04:40 +0100 | [diff] [blame] | 164 | conv_bias_in_scalar = conv_bias_in[id[3]]; |
giuros01 | 15ecc9a | 2018-12-06 10:47:34 +0000 | [diff] [blame] | 165 | } |
Manuel Bottini | 1109176 | 2019-06-17 12:04:40 +0100 | [diff] [blame] | 166 | auto conv_bias_tmp_scalar = (conv_bias_in_scalar - mean) / std::sqrt(var + ScalarType(epsilon)); |
| 167 | conv_bias_out[id[3]] = (conv_bias_tmp_scalar * gamma) + beta; |
giuros01 | 15ecc9a | 2018-12-06 10:47:34 +0000 | [diff] [blame] | 168 | } |
| 169 | |
| 170 | int x = window_start_x; |
| 171 | auto conv_w_in_ptr = reinterpret_cast<const ScalarType *>(conv_w_in.ptr()); |
| 172 | auto conv_w_out_ptr = reinterpret_cast<ScalarType *>(conv_w_out.ptr()); |
Manuel Bottini | 1109176 | 2019-06-17 12:04:40 +0100 | [diff] [blame] | 173 | var_vec = wrapper::vdup_n(var, ExactTagType{}); |
| 174 | gamma_vec = wrapper::vdup_n(gamma, ExactTagType{}); |
| 175 | rvar_vec = wrapper::vinvsqrt(wrapper::vadd(var_vec, epsilon_vec)); |
giuros01 | 15ecc9a | 2018-12-06 10:47:34 +0000 | [diff] [blame] | 176 | |
| 177 | for(; x <= (window_end_x - window_step_x); x += window_step_x) |
| 178 | { |
| 179 | auto wn = wrapper::vloadq(conv_w_in_ptr + x); |
| 180 | wn = wrapper::vmul(wn, rvar_vec); |
| 181 | wn = wrapper::vmul(wn, gamma_vec); |
| 182 | |
| 183 | // Store results |
| 184 | wrapper::vstore(conv_w_out_ptr + x, wn); |
| 185 | } |
| 186 | |
| 187 | // Compute left-over elements |
| 188 | for(; x < window_end_x; ++x) |
| 189 | { |
Manuel Bottini | 1109176 | 2019-06-17 12:04:40 +0100 | [diff] [blame] | 190 | *(conv_w_out_ptr + x) = *(conv_w_in_ptr + x) / std::sqrt(var + ScalarType(epsilon)) * gamma; |
giuros01 | 15ecc9a | 2018-12-06 10:47:34 +0000 | [diff] [blame] | 191 | } |
| 192 | }, |
| 193 | conv_w_in, conv_w_out); |
| 194 | } |
Manuel Bottini | 1109176 | 2019-06-17 12:04:40 +0100 | [diff] [blame] | 195 | |
| 196 | template <typename VectorType> |
| 197 | void fused_batch_normalization_dwc_nhwc(const ITensor *dwc_weights, const ITensor *dwc_bias, ITensor *fused_weights, ITensor *fused_bias, |
| 198 | const ITensor *bn_mean, const ITensor *bn_var, const ITensor *bn_beta, const ITensor *bn_gamma, float epsilon, const Window &window) |
| 199 | { |
| 200 | using ScalarType = typename VectorType::scalar_type; |
| 201 | const int size = 16 / dwc_weights->info()->element_size(); |
| 202 | using ExactTagType = typename VectorType::tag_type; |
| 203 | |
| 204 | const bool run_in_place_weights = (fused_weights == nullptr) || (fused_weights == dwc_weights); |
| 205 | const bool run_in_place_bias = (fused_bias == nullptr) || (dwc_bias != nullptr && fused_bias == dwc_bias); |
| 206 | |
| 207 | // Set build options |
| 208 | Window win = window; |
| 209 | win.set(Window::DimX, Window::Dimension(0, 1, 1)); |
| 210 | |
| 211 | const int window_step_x = size; |
| 212 | const auto window_start_x = static_cast<int>(window.x().start()); |
| 213 | const auto window_end_x = static_cast<int>(window.x().end()); |
| 214 | |
| 215 | Iterator dwc_w_in(dwc_weights, win); |
| 216 | Iterator dwc_w_out(run_in_place_weights ? dwc_weights : fused_weights, win); |
| 217 | |
| 218 | const auto dwc_bias_in = (dwc_bias != nullptr ? reinterpret_cast<ScalarType *>(dwc_bias->ptr_to_element(Coordinates(0, 0))) : nullptr); |
| 219 | auto dwc_bias_out = (run_in_place_bias ? dwc_bias_in : reinterpret_cast<ScalarType *>(fused_bias->ptr_to_element(Coordinates(0, 0)))); |
| 220 | |
| 221 | const auto input_mean = reinterpret_cast<const ScalarType *>(bn_mean->ptr_to_element(Coordinates(0, 0))); |
| 222 | const auto input_var = reinterpret_cast<const ScalarType *>(bn_var->ptr_to_element(Coordinates(0, 0))); |
| 223 | const auto input_gamma = (bn_gamma != nullptr) ? reinterpret_cast<const ScalarType *>(bn_gamma->ptr_to_element(Coordinates(0, 0))) : nullptr; |
| 224 | const auto input_beta = (bn_beta != nullptr) ? reinterpret_cast<const ScalarType *>(bn_beta->ptr_to_element(Coordinates(0, 0))) : nullptr; |
| 225 | |
| 226 | auto mean_vec = wrapper::vdup_n(ScalarType(0), ExactTagType{}); |
| 227 | auto var_vec = wrapper::vdup_n(ScalarType(0), ExactTagType{}); |
| 228 | auto gamma_vec = wrapper::vdup_n(ScalarType(1), ExactTagType{}); |
| 229 | auto beta_vec = wrapper::vdup_n(ScalarType(0), ExactTagType{}); |
| 230 | auto rvar_vec = wrapper::vdup_n(ScalarType(0), ExactTagType{}); |
| 231 | auto dwc_bias_vec = wrapper::vdup_n(ScalarType(0), ExactTagType{}); |
| 232 | const auto epsilon_vec = wrapper::vdup_n(ScalarType(epsilon), ExactTagType{}); |
| 233 | |
| 234 | auto gamma = ScalarType(1.0); |
| 235 | auto beta = ScalarType(0.0); |
| 236 | auto dwc_bias_in_scalar = ScalarType(0); |
| 237 | |
| 238 | execute_window_loop(win, [&](const Coordinates & id) |
| 239 | { |
| 240 | int x = window_start_x; |
| 241 | for(; x <= (window_end_x - window_step_x); x += window_step_x) |
| 242 | { |
| 243 | var_vec = wrapper::vloadq(input_var + x); |
| 244 | if(input_gamma != nullptr) |
| 245 | { |
| 246 | gamma_vec = wrapper::vloadq(input_gamma + x); |
| 247 | } |
| 248 | |
| 249 | if((id[2] == 0) && (id[1] == 0)) |
| 250 | { |
| 251 | mean_vec = wrapper::vloadq(input_mean + x); |
| 252 | |
| 253 | // Construct vectors |
| 254 | if(input_beta != nullptr) |
| 255 | { |
| 256 | beta_vec = wrapper::vloadq(input_beta + x); |
| 257 | } |
| 258 | |
| 259 | if(dwc_bias_in != nullptr) |
| 260 | { |
| 261 | dwc_bias_vec = wrapper::vloadq(dwc_bias_in + x); |
| 262 | } |
| 263 | |
| 264 | auto dwc_bias_tmp_vec = wrapper::vmul(wrapper::vsub(dwc_bias_vec, mean_vec), wrapper::vinvsqrt(wrapper::vadd(var_vec, epsilon_vec))); |
| 265 | dwc_bias_tmp_vec = wrapper::vadd(wrapper::vmul(dwc_bias_tmp_vec, gamma_vec), beta_vec); |
| 266 | wrapper::vstore(dwc_bias_out + x, dwc_bias_tmp_vec); |
| 267 | } |
| 268 | |
| 269 | auto dwc_w_in_ptr = reinterpret_cast<const ScalarType *>(dwc_w_in.ptr()); |
| 270 | auto dwc_w_out_ptr = reinterpret_cast<ScalarType *>(dwc_w_out.ptr()); |
| 271 | |
| 272 | auto wn = wrapper::vloadq(dwc_w_in_ptr + x); |
| 273 | rvar_vec = wrapper::vinvsqrt(wrapper::vadd(var_vec, epsilon_vec)); |
| 274 | wn = wrapper::vmul(wn, rvar_vec); |
| 275 | wn = wrapper::vmul(wn, gamma_vec); |
| 276 | |
| 277 | // Store results |
| 278 | wrapper::vstore(dwc_w_out_ptr + x, wn); |
| 279 | } |
| 280 | |
| 281 | // Compute left-over elements |
| 282 | for(; x < window_end_x; ++x) |
| 283 | { |
| 284 | auto var = input_var[x]; |
| 285 | if(input_gamma != nullptr) |
| 286 | { |
| 287 | gamma = input_gamma[x]; |
| 288 | } |
| 289 | |
| 290 | if(id[2] == 0 && id[1] == 0) |
| 291 | { |
| 292 | auto mean = input_mean[x]; |
| 293 | if(input_beta != nullptr) |
| 294 | { |
| 295 | beta = input_beta[x]; |
| 296 | } |
| 297 | if(dwc_bias_in != nullptr) |
| 298 | { |
| 299 | dwc_bias_in_scalar = dwc_bias_in[x]; |
| 300 | } |
| 301 | |
| 302 | auto dwc_bias_tmp_scalar = (dwc_bias_in_scalar - mean) / std::sqrt(var + ScalarType(epsilon)); |
| 303 | dwc_bias_out[x] = (dwc_bias_tmp_scalar * gamma) + beta; |
| 304 | } |
| 305 | |
| 306 | const auto dwc_w_in_ptr = reinterpret_cast<const ScalarType *>(dwc_w_in.ptr()); |
| 307 | auto dwc_w_out_ptr = reinterpret_cast<ScalarType *>(dwc_w_out.ptr()); |
| 308 | |
| 309 | *(dwc_w_out_ptr + x) = *(dwc_w_in_ptr + x) / std::sqrt(var + ScalarType(epsilon)) * gamma; |
| 310 | } |
| 311 | }, |
| 312 | dwc_w_in, dwc_w_out); |
| 313 | } |
| 314 | |
| 315 | template <typename VectorType> |
| 316 | void fused_batch_normalization_dwc_nchw(const ITensor *dwc_weights, const ITensor *dwc_bias, ITensor *fused_weights, ITensor *fused_bias, |
| 317 | const ITensor *bn_mean, const ITensor *bn_var, const ITensor *bn_beta, const ITensor *bn_gamma, float epsilon, const Window &window) |
| 318 | { |
| 319 | using ScalarType = typename VectorType::scalar_type; |
| 320 | const int size = 16 / dwc_weights->info()->element_size(); |
| 321 | using ExactTagType = typename VectorType::tag_type; |
| 322 | |
| 323 | const bool run_in_place_weights = (fused_weights == nullptr) || (fused_weights == dwc_weights); |
| 324 | const bool run_in_place_bias = (fused_bias == nullptr) || (dwc_bias != nullptr && fused_bias == dwc_bias); |
| 325 | |
| 326 | // Set build options |
| 327 | Window win = window; |
| 328 | win.set(Window::DimX, Window::Dimension(0, 1, 1)); |
| 329 | |
| 330 | const int window_step_x = size; |
| 331 | const auto window_start_x = static_cast<int>(window.x().start()); |
| 332 | const auto window_end_x = static_cast<int>(window.x().end()); |
| 333 | |
| 334 | Iterator dwc_w_in(dwc_weights, win); |
| 335 | Iterator dwc_w_out(run_in_place_weights ? dwc_weights : fused_weights, win); |
| 336 | |
| 337 | const auto dwc_bias_in = (dwc_bias != nullptr ? reinterpret_cast<ScalarType *>(dwc_bias->ptr_to_element(Coordinates(0, 0))) : nullptr); |
| 338 | auto dwc_bias_out = (run_in_place_bias ? dwc_bias_in : reinterpret_cast<ScalarType *>(fused_bias->ptr_to_element(Coordinates(0, 0)))); |
| 339 | |
| 340 | const auto input_mean = reinterpret_cast<const ScalarType *>(bn_mean->ptr_to_element(Coordinates(0, 0))); |
| 341 | const auto input_var = reinterpret_cast<const ScalarType *>(bn_var->ptr_to_element(Coordinates(0, 0))); |
| 342 | const auto input_gamma = (bn_gamma != nullptr) ? reinterpret_cast<const ScalarType *>(bn_gamma->ptr_to_element(Coordinates(0, 0))) : nullptr; |
| 343 | const auto input_beta = (bn_beta != nullptr) ? reinterpret_cast<const ScalarType *>(bn_beta->ptr_to_element(Coordinates(0, 0))) : nullptr; |
| 344 | |
| 345 | auto mean_vec = wrapper::vdup_n(ScalarType(0), ExactTagType{}); |
| 346 | auto var_vec = wrapper::vdup_n(ScalarType(0), ExactTagType{}); |
| 347 | auto gamma_vec = wrapper::vdup_n(ScalarType(1), ExactTagType{}); |
| 348 | auto beta_vec = wrapper::vdup_n(ScalarType(0), ExactTagType{}); |
| 349 | auto rvar_vec = wrapper::vdup_n(ScalarType(0), ExactTagType{}); |
| 350 | const auto epsilon_vec = wrapper::vdup_n(ScalarType(epsilon), ExactTagType{}); |
| 351 | |
| 352 | auto mean = ScalarType(0.0); |
| 353 | auto var = ScalarType(0.0); |
| 354 | auto gamma = ScalarType(1.0); |
| 355 | auto beta = ScalarType(0.0); |
| 356 | auto dwc_bias_in_scalar = ScalarType(0.0); |
| 357 | execute_window_loop(win, [&](const Coordinates & id) |
| 358 | { |
| 359 | var = input_var[id[2]]; |
| 360 | if(input_gamma != nullptr) |
| 361 | { |
| 362 | gamma = input_gamma[id[2]]; |
| 363 | } |
| 364 | |
| 365 | if(id[1] == 0) |
| 366 | { |
| 367 | mean = input_mean[id[2]]; |
| 368 | |
| 369 | // Construct vectors |
| 370 | mean_vec = wrapper::vdup_n(mean, ExactTagType{}); |
| 371 | if(input_beta != nullptr) |
| 372 | { |
| 373 | beta = input_beta[id[2]]; |
| 374 | beta_vec = wrapper::vdup_n(beta, ExactTagType{}); |
| 375 | } |
| 376 | |
| 377 | if(dwc_bias_in != nullptr) |
| 378 | { |
| 379 | dwc_bias_in_scalar = dwc_bias_in[id[2]]; |
| 380 | } |
| 381 | |
| 382 | auto dwc_bias_tmp_scalar = (dwc_bias_in_scalar - mean) / std::sqrt(var + ScalarType(epsilon)); |
| 383 | dwc_bias_out[id[2]] = (dwc_bias_tmp_scalar * gamma) + beta; |
| 384 | } |
| 385 | |
| 386 | int x = window_start_x; |
| 387 | auto dwc_w_in_ptr = reinterpret_cast<const ScalarType *>(dwc_w_in.ptr()); |
| 388 | auto dwc_w_out_ptr = reinterpret_cast<ScalarType *>(dwc_w_out.ptr()); |
| 389 | var_vec = wrapper::vdup_n(var, ExactTagType{}); |
| 390 | gamma_vec = wrapper::vdup_n(gamma, ExactTagType{}); |
| 391 | rvar_vec = wrapper::vinvsqrt(wrapper::vadd(var_vec, epsilon_vec)); |
| 392 | |
| 393 | for(; x <= (window_end_x - window_step_x); x += window_step_x) |
| 394 | { |
| 395 | auto wn = wrapper::vloadq(dwc_w_in_ptr + x); |
| 396 | wn = wrapper::vmul(wn, rvar_vec); |
| 397 | wn = wrapper::vmul(wn, gamma_vec); |
| 398 | |
| 399 | // Store results |
| 400 | wrapper::vstore(dwc_w_out_ptr + x, wn); |
| 401 | } |
| 402 | |
| 403 | // Compute left-over elements |
| 404 | for(; x < window_end_x; ++x) |
| 405 | { |
| 406 | *(dwc_w_out_ptr + x) = *(dwc_w_in_ptr + x) / std::sqrt(var + ScalarType(epsilon)) * gamma; |
| 407 | } |
| 408 | }, |
| 409 | dwc_w_in, dwc_w_out); |
| 410 | } |
| 411 | |
giuros01 | 15ecc9a | 2018-12-06 10:47:34 +0000 | [diff] [blame] | 412 | } // namespace |
| 413 | |
| 414 | NEFuseBatchNormalizationKernel::NEFuseBatchNormalizationKernel() |
Manuel Bottini | 1109176 | 2019-06-17 12:04:40 +0100 | [diff] [blame] | 415 | : _input_weights(nullptr), _input_bias(nullptr), _bn_mean(nullptr), _bn_var(nullptr), _bn_gamma(nullptr), _bn_beta(nullptr), _fused_weights(nullptr), _fused_bias(nullptr), _epsilon(), |
giuros01 | 15ecc9a | 2018-12-06 10:47:34 +0000 | [diff] [blame] | 416 | _run_in_place_weights(false), _run_in_place_bias(false), _func(nullptr) |
| 417 | { |
| 418 | } |
| 419 | |
Manuel Bottini | 1109176 | 2019-06-17 12:04:40 +0100 | [diff] [blame] | 420 | void NEFuseBatchNormalizationKernel::configure(const ITensor *input_weights, const ITensor *bn_mean, const ITensor *bn_var, |
giuros01 | 15ecc9a | 2018-12-06 10:47:34 +0000 | [diff] [blame] | 421 | ITensor *fused_weights, ITensor *fused_bias, |
Manuel Bottini | 1109176 | 2019-06-17 12:04:40 +0100 | [diff] [blame] | 422 | const ITensor *input_bias, const ITensor *bn_beta, const ITensor *bn_gamma, |
| 423 | float epsilon, FuseBatchNormalizationType fbn_type) |
giuros01 | 15ecc9a | 2018-12-06 10:47:34 +0000 | [diff] [blame] | 424 | { |
Manuel Bottini | 1109176 | 2019-06-17 12:04:40 +0100 | [diff] [blame] | 425 | ARM_COMPUTE_ERROR_ON_NULLPTR(input_weights, bn_mean, bn_var); |
giuros01 | 15ecc9a | 2018-12-06 10:47:34 +0000 | [diff] [blame] | 426 | |
Manuel Bottini | 1109176 | 2019-06-17 12:04:40 +0100 | [diff] [blame] | 427 | _input_weights = input_weights; |
| 428 | _input_bias = input_bias; |
giuros01 | 15ecc9a | 2018-12-06 10:47:34 +0000 | [diff] [blame] | 429 | _bn_mean = bn_mean; |
| 430 | _bn_var = bn_var; |
| 431 | _bn_beta = bn_beta; |
| 432 | _bn_gamma = bn_gamma; |
| 433 | _fused_weights = fused_weights; |
| 434 | _fused_bias = fused_bias; |
| 435 | _epsilon = epsilon; |
| 436 | |
Manuel Bottini | 1109176 | 2019-06-17 12:04:40 +0100 | [diff] [blame] | 437 | _run_in_place_weights = (fused_weights == nullptr) || (fused_weights == input_weights); |
| 438 | _run_in_place_bias = (fused_bias == nullptr) || (input_bias != nullptr && fused_bias == input_bias); |
giuros01 | 15ecc9a | 2018-12-06 10:47:34 +0000 | [diff] [blame] | 439 | |
| 440 | // Auto initialize outputs |
| 441 | if(_fused_weights != nullptr) |
| 442 | { |
| 443 | // Output tensor auto initialization if not yet initialized |
Manuel Bottini | 1109176 | 2019-06-17 12:04:40 +0100 | [diff] [blame] | 444 | auto_init_if_empty(*_fused_weights->info(), *_input_weights->info()->clone()); |
| 445 | fused_weights->info()->set_valid_region(input_weights->info()->valid_region()); |
giuros01 | 15ecc9a | 2018-12-06 10:47:34 +0000 | [diff] [blame] | 446 | } |
| 447 | if(_fused_bias != nullptr) |
| 448 | { |
| 449 | // Output tensor auto initialization if not yet initialized |
| 450 | auto_init_if_empty(*_fused_bias->info(), *_bn_mean->info()->clone()); |
| 451 | _fused_bias->info()->set_valid_region(bn_mean->info()->valid_region()); |
| 452 | } |
| 453 | |
| 454 | // Validate arguments |
Manuel Bottini | 1109176 | 2019-06-17 12:04:40 +0100 | [diff] [blame] | 455 | ARM_COMPUTE_ERROR_THROW_ON(validate_arguments(input_weights->info(), bn_mean->info(), bn_var->info(), |
giuros01 | 15ecc9a | 2018-12-06 10:47:34 +0000 | [diff] [blame] | 456 | (fused_weights != nullptr) ? fused_weights->info() : nullptr, |
| 457 | (fused_bias != nullptr) ? fused_bias->info() : nullptr, |
Manuel Bottini | 1109176 | 2019-06-17 12:04:40 +0100 | [diff] [blame] | 458 | (input_bias != nullptr) ? input_bias->info() : nullptr, |
giuros01 | 15ecc9a | 2018-12-06 10:47:34 +0000 | [diff] [blame] | 459 | (bn_beta != nullptr) ? bn_beta->info() : nullptr, |
| 460 | (bn_gamma != nullptr) ? bn_gamma->info() : nullptr, |
Manuel Bottini | 1109176 | 2019-06-17 12:04:40 +0100 | [diff] [blame] | 461 | epsilon, fbn_type)); |
giuros01 | 15ecc9a | 2018-12-06 10:47:34 +0000 | [diff] [blame] | 462 | |
| 463 | // Configure kernel window |
Manuel Bottini | 1109176 | 2019-06-17 12:04:40 +0100 | [diff] [blame] | 464 | Window win = calculate_max_window(*input_weights->info()); |
giuros01 | 15ecc9a | 2018-12-06 10:47:34 +0000 | [diff] [blame] | 465 | INEKernel::configure(win); |
| 466 | |
Manuel Bottini | 1109176 | 2019-06-17 12:04:40 +0100 | [diff] [blame] | 467 | // Configure function |
| 468 | static std::map<std::string, FuseBatchNormFunction *> map_function = |
giuros01 | 15ecc9a | 2018-12-06 10:47:34 +0000 | [diff] [blame] | 469 | { |
Manuel Bottini | 1109176 | 2019-06-17 12:04:40 +0100 | [diff] [blame] | 470 | { "fused_batch_normalization_conv_NHWC_F32", &fused_batch_normalization_conv<wrapper::traits::neon_vector<float, 4>> }, |
| 471 | { "fused_batch_normalization_conv_NCHW_F32", &fused_batch_normalization_conv<wrapper::traits::neon_vector<float, 4>> }, |
| 472 | { "fused_batch_normalization_dwc_NHWC_F32", &fused_batch_normalization_dwc_nhwc<wrapper::traits::neon_vector<float, 4>> }, |
| 473 | { "fused_batch_normalization_dwc_NCHW_F32", &fused_batch_normalization_dwc_nchw<wrapper::traits::neon_vector<float, 4>> }, |
giuros01 | 15ecc9a | 2018-12-06 10:47:34 +0000 | [diff] [blame] | 474 | #ifdef __ARM_FEATURE_FP16_VECTOR_ARITHMETIC |
Manuel Bottini | 1109176 | 2019-06-17 12:04:40 +0100 | [diff] [blame] | 475 | { "fused_batch_normalization_conv_NHWC_F16", &fused_batch_normalization_conv<wrapper::traits::neon_vector<float16_t, 8>> }, |
| 476 | { "fused_batch_normalization_conv_NCHW_F16", &fused_batch_normalization_conv<wrapper::traits::neon_vector<float16_t, 8>> }, |
| 477 | { "fused_batch_normalization_dwc_NHWC_F16", &fused_batch_normalization_dwc_nhwc<wrapper::traits::neon_vector<float16_t, 8>> }, |
| 478 | { "fused_batch_normalization_dwc_NCHW_F16", &fused_batch_normalization_dwc_nchw<wrapper::traits::neon_vector<float16_t, 8>> }, |
| 479 | #endif /* __ARM_FEATURE_FP16_VECTOR_ARITHMETIC */ |
| 480 | }; |
| 481 | |
| 482 | std::string function_to_call("fused_batch_normalization_"); |
| 483 | function_to_call += fbn_type == FuseBatchNormalizationType::CONVOLUTION ? "conv_" : "dwc_"; |
| 484 | function_to_call += string_from_data_layout(_input_weights->info()->data_layout()); |
| 485 | function_to_call += "_"; |
| 486 | function_to_call += string_from_data_type(_input_weights->info()->data_type()); |
| 487 | |
| 488 | auto it = map_function.find(function_to_call); |
| 489 | |
| 490 | if(it != map_function.end()) |
| 491 | { |
| 492 | _func = it->second; |
giuros01 | 15ecc9a | 2018-12-06 10:47:34 +0000 | [diff] [blame] | 493 | } |
| 494 | } |
| 495 | |
Manuel Bottini | 1109176 | 2019-06-17 12:04:40 +0100 | [diff] [blame] | 496 | Status NEFuseBatchNormalizationKernel::validate(const ITensorInfo *input_weights, const ITensorInfo *bn_mean, const ITensorInfo *bn_var, |
giuros01 | 15ecc9a | 2018-12-06 10:47:34 +0000 | [diff] [blame] | 497 | const ITensorInfo *fused_weights, const ITensorInfo *fused_bias, |
Manuel Bottini | 1109176 | 2019-06-17 12:04:40 +0100 | [diff] [blame] | 498 | const ITensorInfo *input_bias, const ITensorInfo *bn_beta, const ITensorInfo *bn_gamma, |
| 499 | float epsilon, FuseBatchNormalizationType fbn_type) |
giuros01 | 15ecc9a | 2018-12-06 10:47:34 +0000 | [diff] [blame] | 500 | { |
Manuel Bottini | 1109176 | 2019-06-17 12:04:40 +0100 | [diff] [blame] | 501 | ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments(input_weights, bn_mean, bn_var, fused_weights, fused_bias, input_bias, bn_beta, bn_gamma, epsilon, fbn_type)); |
giuros01 | 15ecc9a | 2018-12-06 10:47:34 +0000 | [diff] [blame] | 502 | return Status{}; |
| 503 | } |
| 504 | |
| 505 | void NEFuseBatchNormalizationKernel::run(const Window &window, const ThreadInfo &info) |
| 506 | { |
| 507 | ARM_COMPUTE_UNUSED(info); |
| 508 | ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this); |
| 509 | ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(IKernel::window(), window); |
Manuel Bottini | 1109176 | 2019-06-17 12:04:40 +0100 | [diff] [blame] | 510 | (*_func)(_input_weights, _input_bias, _fused_weights, _fused_bias, _bn_mean, _bn_var, _bn_beta, _bn_gamma, _epsilon, window); |
giuros01 | 15ecc9a | 2018-12-06 10:47:34 +0000 | [diff] [blame] | 511 | } |
| 512 | } // namespace arm_compute |