Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 1 | /* |
Georgios Pinitas | 57c033b | 2018-02-15 12:29:44 +0000 | [diff] [blame] | 2 | * Copyright (c) 2017-2018 ARM Limited. |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [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 | #include "arm_compute/core/NEON/kernels/NEBatchNormalizationLayerKernel.h" |
| 25 | |
| 26 | #include "arm_compute/core/Helpers.h" |
| 27 | #include "arm_compute/core/NEON/NEFixedPoint.h" |
| 28 | #include "arm_compute/core/NEON/NEMath.h" |
Georgios Pinitas | 57c033b | 2018-02-15 12:29:44 +0000 | [diff] [blame] | 29 | #include "arm_compute/core/NEON/kernels/detail/NEActivationFunctionDetail.h" |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 30 | #include "arm_compute/core/TensorInfo.h" |
| 31 | #include "arm_compute/core/Utils.h" |
| 32 | #include "arm_compute/core/Validate.h" |
| 33 | #include "arm_compute/core/Window.h" |
| 34 | |
Georgios Pinitas | 57c033b | 2018-02-15 12:29:44 +0000 | [diff] [blame] | 35 | #include <map> |
| 36 | |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 37 | using namespace arm_compute; |
| 38 | |
Ioan-Cristian Szabo | 303be90 | 2017-11-27 16:31:10 +0000 | [diff] [blame] | 39 | namespace |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 40 | { |
Georgios Pinitas | 57c033b | 2018-02-15 12:29:44 +0000 | [diff] [blame] | 41 | Status |
| 42 | validate_arguments(const ITensorInfo *input, const ITensorInfo *output, const ITensorInfo *mean, const ITensorInfo *var, |
| 43 | const ITensorInfo *beta, const ITensorInfo *gamma, float epsilon, ActivationLayerInfo act_info) |
Ioan-Cristian Szabo | 303be90 | 2017-11-27 16:31:10 +0000 | [diff] [blame] | 44 | { |
| 45 | ARM_COMPUTE_UNUSED(epsilon); |
Vidhya Sudhan Loganathan | 7485d5a | 2018-07-04 09:34:00 +0100 | [diff] [blame^] | 46 | ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::F16, |
Georgios Pinitas | 57c033b | 2018-02-15 12:29:44 +0000 | [diff] [blame] | 47 | DataType::F32); |
| 48 | |
| 49 | if(act_info.enabled()) |
| 50 | { |
| 51 | ActivationLayerInfo::ActivationFunction act = act_info.activation(); |
| 52 | ARM_COMPUTE_RETURN_ERROR_ON(input->data_type() != DataType::F32); |
| 53 | ARM_COMPUTE_RETURN_ERROR_ON(act != ActivationLayerInfo::ActivationLayerInfo::ActivationFunction::RELU && act != ActivationLayerInfo::ActivationLayerInfo::ActivationFunction::BOUNDED_RELU |
| 54 | && act != ActivationLayerInfo::ActivationLayerInfo::ActivationFunction::LU_BOUNDED_RELU); |
| 55 | ARM_COMPUTE_RETURN_ERROR_ON(act_info.b() > act_info.a()); |
| 56 | } |
Ioan-Cristian Szabo | 303be90 | 2017-11-27 16:31:10 +0000 | [diff] [blame] | 57 | |
| 58 | if(nullptr != output) |
| 59 | { |
| 60 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_SHAPES(input, output); |
Michele Di Giorgio | 0cbb927 | 2018-03-01 16:56:48 +0000 | [diff] [blame] | 61 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_LAYOUT(input, output); |
Ioan-Cristian Szabo | 303be90 | 2017-11-27 16:31:10 +0000 | [diff] [blame] | 62 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input, output); |
Ioan-Cristian Szabo | 303be90 | 2017-11-27 16:31:10 +0000 | [diff] [blame] | 63 | } |
| 64 | |
Michele Di Giorgio | 4d33630 | 2018-03-02 09:43:54 +0000 | [diff] [blame] | 65 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input, mean, var); |
Michele Di Giorgio | 4d33630 | 2018-03-02 09:43:54 +0000 | [diff] [blame] | 66 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_SHAPES(mean, var); |
| 67 | if(beta != nullptr) |
| 68 | { |
| 69 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input, beta); |
Michele Di Giorgio | 4d33630 | 2018-03-02 09:43:54 +0000 | [diff] [blame] | 70 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_SHAPES(mean, beta); |
| 71 | } |
| 72 | if(gamma != nullptr) |
| 73 | { |
| 74 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input, gamma); |
Michele Di Giorgio | 4d33630 | 2018-03-02 09:43:54 +0000 | [diff] [blame] | 75 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_SHAPES(mean, gamma); |
| 76 | } |
Michele Di Giorgio | 0cbb927 | 2018-03-01 16:56:48 +0000 | [diff] [blame] | 77 | ARM_COMPUTE_RETURN_ERROR_ON(input->dimension(get_data_layout_dimension_index(input->data_layout(), DataLayoutDimension::CHANNEL)) != mean->dimension(0)); |
Ioan-Cristian Szabo | 303be90 | 2017-11-27 16:31:10 +0000 | [diff] [blame] | 78 | |
Georgios Pinitas | 631c41a | 2017-12-06 11:53:03 +0000 | [diff] [blame] | 79 | return Status{}; |
Ioan-Cristian Szabo | 303be90 | 2017-11-27 16:31:10 +0000 | [diff] [blame] | 80 | } |
| 81 | |
Georgios Pinitas | 631c41a | 2017-12-06 11:53:03 +0000 | [diff] [blame] | 82 | std::pair<Status, Window> validate_and_configure_window(ITensorInfo *input, ITensorInfo *output) |
Ioan-Cristian Szabo | 303be90 | 2017-11-27 16:31:10 +0000 | [diff] [blame] | 83 | { |
Michele Di Giorgio | 4d33630 | 2018-03-02 09:43:54 +0000 | [diff] [blame] | 84 | if(output != nullptr) |
| 85 | { |
| 86 | // Output tensor auto initialization if not yet initialized |
| 87 | auto_init_if_empty(*output, *input->clone()); |
| 88 | } |
| 89 | |
Ioan-Cristian Szabo | 303be90 | 2017-11-27 16:31:10 +0000 | [diff] [blame] | 90 | unsigned int num_elems_processed_per_iteration = 16 / input->element_size(); |
| 91 | |
| 92 | Window win = calculate_max_window(*input, Steps(num_elems_processed_per_iteration)); |
| 93 | AccessWindowHorizontal input_access(input, 0, num_elems_processed_per_iteration); |
| 94 | AccessWindowHorizontal output_access(output, 0, num_elems_processed_per_iteration); |
| 95 | bool window_changed = update_window_and_padding(win, input_access, output_access); |
| 96 | output_access.set_valid_region(win, input->valid_region()); |
Georgios Pinitas | 631c41a | 2017-12-06 11:53:03 +0000 | [diff] [blame] | 97 | Status err = (window_changed) ? ARM_COMPUTE_CREATE_ERROR(ErrorCode::RUNTIME_ERROR, "Insufficient Padding!") : Status{}; |
Ioan-Cristian Szabo | 303be90 | 2017-11-27 16:31:10 +0000 | [diff] [blame] | 98 | return std::make_pair(err, win); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 99 | } |
Georgios Pinitas | 57c033b | 2018-02-15 12:29:44 +0000 | [diff] [blame] | 100 | } //namespace |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 101 | |
Georgios Pinitas | 57c033b | 2018-02-15 12:29:44 +0000 | [diff] [blame] | 102 | template <bool fused_activation> |
Michele Di Giorgio | 0cbb927 | 2018-03-01 16:56:48 +0000 | [diff] [blame] | 103 | void NEBatchNormalizationLayerKernel::batch_normalization_fp16_nchw(const Window &window) |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 104 | { |
Michele Di Giorgio | 0cbb927 | 2018-03-01 16:56:48 +0000 | [diff] [blame] | 105 | static_assert(!fused_activation, "Activation is not supported for FP16"); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 106 | |
Georgios Pinitas | 57c033b | 2018-02-15 12:29:44 +0000 | [diff] [blame] | 107 | ARM_COMPUTE_UNUSED(window); |
Ioan-Cristian Szabo | 5edbd1c | 2017-11-13 13:34:08 +0000 | [diff] [blame] | 108 | #ifdef __ARM_FEATURE_FP16_VECTOR_ARITHMETIC |
Georgios Pinitas | 57c033b | 2018-02-15 12:29:44 +0000 | [diff] [blame] | 109 | Iterator input(_input, window); |
| 110 | Iterator output(_output, window); |
Pablo Tello | 8fda1cb | 2017-07-05 15:20:38 +0100 | [diff] [blame] | 111 | |
| 112 | // Hold information about the current feature map we are iterating. |
| 113 | // Only compute denominator and NEON vectors once per feature map. |
| 114 | int slice = -1; |
| 115 | |
Georgios Pinitas | 57c033b | 2018-02-15 12:29:44 +0000 | [diff] [blame] | 116 | const auto input_mean = reinterpret_cast<const float16_t *>(_mean->ptr_to_element(Coordinates(0, 0))); |
| 117 | const auto input_var = reinterpret_cast<const float16_t *>(_var->ptr_to_element(Coordinates(0, 0))); |
Michele Di Giorgio | 4d33630 | 2018-03-02 09:43:54 +0000 | [diff] [blame] | 118 | const auto input_gamma = (_gamma != nullptr) ? reinterpret_cast<const float16_t *>(_gamma->ptr_to_element(Coordinates(0, 0))) : nullptr; |
| 119 | const auto input_beta = (_beta != nullptr) ? reinterpret_cast<const float16_t *>(_beta->ptr_to_element(Coordinates(0, 0))) : nullptr; |
Pablo Tello | 8fda1cb | 2017-07-05 15:20:38 +0100 | [diff] [blame] | 120 | |
| 121 | float16x8_t mean_vec = vdupq_n_f16(0.0); |
| 122 | float16x8_t var_vec = vdupq_n_f16(0.0); |
Michele Di Giorgio | 4d33630 | 2018-03-02 09:43:54 +0000 | [diff] [blame] | 123 | float16x8_t gamma_vec = vdupq_n_f16(1.0); |
Pablo Tello | 8fda1cb | 2017-07-05 15:20:38 +0100 | [diff] [blame] | 124 | float16x8_t beta_vec = vdupq_n_f16(0.0); |
| 125 | float16x8_t denominator = vdupq_n_f16(0.0); |
Georgios Pinitas | 57c033b | 2018-02-15 12:29:44 +0000 | [diff] [blame] | 126 | const float16x8_t epsilon_vec = vdupq_n_f16(_epsilon); |
Pablo Tello | 8fda1cb | 2017-07-05 15:20:38 +0100 | [diff] [blame] | 127 | execute_window_loop(window, [&](const Coordinates & id) |
| 128 | { |
| 129 | if(slice != id.z()) |
| 130 | { |
| 131 | // Conctruct vectors |
Michele Di Giorgio | 4d33630 | 2018-03-02 09:43:54 +0000 | [diff] [blame] | 132 | mean_vec = vdupq_n_f16(*(input_mean + id.z())); |
| 133 | var_vec = vdupq_n_f16(*(input_var + id.z())); |
| 134 | if(input_gamma != nullptr) |
| 135 | { |
| 136 | gamma_vec = vdupq_n_f16(*(input_gamma + id.z())); |
| 137 | } |
| 138 | if(input_beta != nullptr) |
| 139 | { |
| 140 | beta_vec = vdupq_n_f16(*(input_beta + id.z())); |
| 141 | } |
Pablo Tello | 8fda1cb | 2017-07-05 15:20:38 +0100 | [diff] [blame] | 142 | |
| 143 | // Calculate denominator |
| 144 | denominator = vinvsqrtq_f16(vaddq_f16(var_vec, epsilon_vec)); |
| 145 | slice = id.z(); |
| 146 | } |
| 147 | |
| 148 | // Calculate x bar and store results |
| 149 | const float16x8_t numerator = vsubq_f16(vld1q_f16(reinterpret_cast<const float16_t *>(input.ptr())), mean_vec); |
| 150 | const float16x8_t x_bar = vmulq_f16(numerator, denominator); |
| 151 | vst1q_f16(reinterpret_cast<float16_t *>(output.ptr()), vaddq_f16(beta_vec, vmulq_f16(x_bar, gamma_vec))); |
| 152 | }, |
| 153 | input, output); |
Ioan-Cristian Szabo | 5edbd1c | 2017-11-13 13:34:08 +0000 | [diff] [blame] | 154 | #endif /* __ARM_FEATURE_FP16_VECTOR_ARITHMETIC */ |
Georgios Pinitas | 57c033b | 2018-02-15 12:29:44 +0000 | [diff] [blame] | 155 | } |
| 156 | |
Michele Di Giorgio | 0cbb927 | 2018-03-01 16:56:48 +0000 | [diff] [blame] | 157 | template <bool fused_activation> |
| 158 | void NEBatchNormalizationLayerKernel::batch_normalization_fp16_nhwc(const Window &window) |
| 159 | { |
| 160 | static_assert(!fused_activation, "Activation is not supported for FP16"); |
| 161 | |
| 162 | ARM_COMPUTE_UNUSED(window); |
| 163 | #ifdef __ARM_FEATURE_FP16_VECTOR_ARITHMETIC |
| 164 | Iterator input(_input, window); |
| 165 | Iterator output(_output, window); |
| 166 | |
| 167 | const auto input_mean = reinterpret_cast<const float16_t *>(_mean->ptr_to_element(Coordinates(0, 0))); |
| 168 | const auto input_var = reinterpret_cast<const float16_t *>(_var->ptr_to_element(Coordinates(0, 0))); |
| 169 | const auto input_gamma = (_gamma != nullptr) ? reinterpret_cast<const float16_t *>(_gamma->ptr_to_element(Coordinates(0, 0))) : nullptr; |
| 170 | const auto input_beta = (_beta != nullptr) ? reinterpret_cast<const float16_t *>(_beta->ptr_to_element(Coordinates(0, 0))) : nullptr; |
| 171 | |
| 172 | const float16x8_t epsilon_vec = vdupq_n_f16(_epsilon); |
| 173 | execute_window_loop(window, [&](const Coordinates & id) |
| 174 | { |
| 175 | // Conctruct vectors |
| 176 | const float16x8_t mean_vec = vld1q_f16(input_mean + id.x()); |
| 177 | const float16x8_t var_vec = vld1q_f16(input_var + id.x()); |
| 178 | const float16x8_t gamma_vec = (input_gamma != nullptr) ? vld1q_f16(input_gamma + id.x()) : vdupq_n_f16(1.0); |
| 179 | const float16x8_t beta_vec = (input_beta != nullptr) ? vld1q_f16(input_beta + id.x()) : vdupq_n_f16(0.0); |
| 180 | // Calculate denominator |
| 181 | const float16x8_t denominator = vinvsqrtq_f16(vaddq_f16(var_vec, epsilon_vec)); |
| 182 | |
| 183 | // Calculate x bar and store results |
| 184 | const float16x8_t numerator = vsubq_f16(vld1q_f16(reinterpret_cast<const float16_t *>(input.ptr())), mean_vec); |
| 185 | const float16x8_t x_bar = vmulq_f16(numerator, denominator); |
| 186 | vst1q_f16(reinterpret_cast<float16_t *>(output.ptr()), vaddq_f16(beta_vec, vmulq_f16(x_bar, gamma_vec))); |
| 187 | }, |
| 188 | input, output); |
| 189 | #endif /* __ARM_FEATURE_FP16_VECTOR_ARITHMETIC */ |
| 190 | } |
| 191 | |
Georgios Pinitas | 57c033b | 2018-02-15 12:29:44 +0000 | [diff] [blame] | 192 | template <bool fused_activation, typename F> |
Michele Di Giorgio | 0cbb927 | 2018-03-01 16:56:48 +0000 | [diff] [blame] | 193 | void NEBatchNormalizationLayerKernel::batch_normalization_fp32_nchw(const Window &window) |
Georgios Pinitas | 57c033b | 2018-02-15 12:29:44 +0000 | [diff] [blame] | 194 | { |
| 195 | Iterator input(_input, window); |
| 196 | Iterator output(_output, window); |
| 197 | |
| 198 | F activation_functor(_act_info); |
| 199 | |
| 200 | // Hold information about the current feature map we are iterating. |
| 201 | // Only compute denominator and NEON vectors once per feature map. |
| 202 | int slice = -1; |
| 203 | |
| 204 | const auto input_mean = reinterpret_cast<const float *>(_mean->ptr_to_element(Coordinates(0, 0))); |
| 205 | const auto input_var = reinterpret_cast<const float *>(_var->ptr_to_element(Coordinates(0, 0))); |
Michele Di Giorgio | 4d33630 | 2018-03-02 09:43:54 +0000 | [diff] [blame] | 206 | const auto input_gamma = (_gamma != nullptr) ? reinterpret_cast<const float *>(_gamma->ptr_to_element(Coordinates(0, 0))) : nullptr; |
| 207 | const auto input_beta = (_beta != nullptr) ? reinterpret_cast<const float *>(_beta->ptr_to_element(Coordinates(0, 0))) : nullptr; |
Georgios Pinitas | 57c033b | 2018-02-15 12:29:44 +0000 | [diff] [blame] | 208 | |
| 209 | float32x4_t mean_vec = vdupq_n_f32(0.0); |
| 210 | float32x4_t var_vec = vdupq_n_f32(0.0); |
Michele Di Giorgio | 4d33630 | 2018-03-02 09:43:54 +0000 | [diff] [blame] | 211 | float32x4_t gamma_vec = vdupq_n_f32(1.0); |
Georgios Pinitas | 57c033b | 2018-02-15 12:29:44 +0000 | [diff] [blame] | 212 | float32x4_t beta_vec = vdupq_n_f32(0.0); |
| 213 | float32x4_t denominator = vdupq_n_f32(0.0); |
| 214 | const float32x4_t epsilon_vec = vdupq_n_f32(_epsilon); |
| 215 | execute_window_loop(window, [&](const Coordinates & id) |
| 216 | { |
| 217 | if(slice != id.z()) |
| 218 | { |
| 219 | // Conctruct vectors |
Michele Di Giorgio | 4d33630 | 2018-03-02 09:43:54 +0000 | [diff] [blame] | 220 | mean_vec = vdupq_n_f32(*(input_mean + id.z())); |
| 221 | var_vec = vdupq_n_f32(*(input_var + id.z())); |
| 222 | if(input_gamma != nullptr) |
| 223 | { |
| 224 | gamma_vec = vdupq_n_f32(*(input_gamma + id.z())); |
| 225 | } |
| 226 | if(input_beta != nullptr) |
| 227 | { |
| 228 | beta_vec = vdupq_n_f32(*(input_beta + id.z())); |
| 229 | } |
Georgios Pinitas | 57c033b | 2018-02-15 12:29:44 +0000 | [diff] [blame] | 230 | |
| 231 | // Calculate denominator |
| 232 | denominator = vinvsqrtq_f32(vaddq_f32(var_vec, epsilon_vec)); |
| 233 | slice = id.z(); |
| 234 | } |
| 235 | |
| 236 | // Calculate x bar |
| 237 | const float32x4_t numerator = vsubq_f32(vld1q_f32(reinterpret_cast<const float *>(input.ptr())), mean_vec); |
| 238 | const float32x4_t x_bar = vmulq_f32(numerator, denominator); |
| 239 | float32x4_t res = vmlaq_f32(beta_vec, x_bar, gamma_vec); |
| 240 | |
| 241 | // Perform fused activation |
| 242 | if(fused_activation) |
| 243 | { |
| 244 | activation_functor(res); |
| 245 | } |
| 246 | |
| 247 | // Store results |
| 248 | vst1q_f32(reinterpret_cast<float *>(output.ptr()), res); |
| 249 | }, |
| 250 | input, output); |
| 251 | } |
| 252 | |
Michele Di Giorgio | 0cbb927 | 2018-03-01 16:56:48 +0000 | [diff] [blame] | 253 | template <bool fused_activation, typename F> |
| 254 | void NEBatchNormalizationLayerKernel::batch_normalization_fp32_nhwc(const Window &window) |
| 255 | { |
| 256 | Iterator input(_input, window); |
| 257 | Iterator output(_output, window); |
| 258 | |
| 259 | F activation_functor(_act_info); |
| 260 | |
| 261 | const auto input_mean = reinterpret_cast<const float *>(_mean->ptr_to_element(Coordinates(0, 0))); |
| 262 | const auto input_var = reinterpret_cast<const float *>(_var->ptr_to_element(Coordinates(0, 0))); |
| 263 | const auto input_gamma = (_gamma != nullptr) ? reinterpret_cast<const float *>(_gamma->ptr_to_element(Coordinates(0, 0))) : nullptr; |
| 264 | const auto input_beta = (_beta != nullptr) ? reinterpret_cast<const float *>(_beta->ptr_to_element(Coordinates(0, 0))) : nullptr; |
| 265 | |
| 266 | const float32x4_t epsilon_vec = vdupq_n_f32(_epsilon); |
| 267 | execute_window_loop(window, [&](const Coordinates & id) |
| 268 | { |
| 269 | // Conctruct vectors |
| 270 | const float32x4_t mean_vec = vld1q_f32(input_mean + id.x()); |
| 271 | const float32x4_t var_vec = vld1q_f32(input_var + id.x()); |
| 272 | const float32x4_t gamma_vec = (input_gamma != nullptr) ? vld1q_f32(input_gamma + id.x()) : vdupq_n_f32(1.0); |
| 273 | const float32x4_t beta_vec = (input_beta != nullptr) ? vld1q_f32(input_beta + id.x()) : vdupq_n_f32(0.0); |
| 274 | // Calculate denominator |
| 275 | const float32x4_t denominator = vinvsqrtq_f32(vaddq_f32(var_vec, epsilon_vec)); |
| 276 | |
| 277 | // Calculate x bar |
| 278 | const float32x4_t numerator = vsubq_f32(vld1q_f32(reinterpret_cast<const float *>(input.ptr())), mean_vec); |
| 279 | const float32x4_t x_bar = vmulq_f32(numerator, denominator); |
| 280 | float32x4_t res = vmlaq_f32(beta_vec, x_bar, gamma_vec); |
| 281 | |
| 282 | // Perform fused activation |
| 283 | if(fused_activation) |
| 284 | { |
| 285 | activation_functor(res); |
| 286 | } |
| 287 | |
| 288 | // Store results |
| 289 | vst1q_f32(reinterpret_cast<float *>(output.ptr()), res); |
| 290 | }, |
| 291 | input, output); |
| 292 | } |
| 293 | |
Georgios Pinitas | 57c033b | 2018-02-15 12:29:44 +0000 | [diff] [blame] | 294 | void NEBatchNormalizationLayerKernel::configure_non_fused() |
| 295 | { |
Michele Di Giorgio | 0cbb927 | 2018-03-01 16:56:48 +0000 | [diff] [blame] | 296 | const bool is_nhwc = _input->info()->data_layout() == DataLayout::NHWC; |
Georgios Pinitas | 57c033b | 2018-02-15 12:29:44 +0000 | [diff] [blame] | 297 | switch(_input->info()->data_type()) |
| 298 | { |
Georgios Pinitas | 57c033b | 2018-02-15 12:29:44 +0000 | [diff] [blame] | 299 | case DataType::F16: |
Michele Di Giorgio | 0cbb927 | 2018-03-01 16:56:48 +0000 | [diff] [blame] | 300 | _func = (is_nhwc) ? &NEBatchNormalizationLayerKernel::batch_normalization_fp16_nhwc<false> : &NEBatchNormalizationLayerKernel::batch_normalization_fp16_nchw<false>; |
Georgios Pinitas | 57c033b | 2018-02-15 12:29:44 +0000 | [diff] [blame] | 301 | break; |
| 302 | case DataType::F32: |
Michele Di Giorgio | 0cbb927 | 2018-03-01 16:56:48 +0000 | [diff] [blame] | 303 | _func = (is_nhwc) ? &NEBatchNormalizationLayerKernel::batch_normalization_fp32_nhwc<false, ::detail::dummy<float, 4>> : |
| 304 | &NEBatchNormalizationLayerKernel::batch_normalization_fp32_nchw<false, ::detail::dummy<float, 4>>; |
Georgios Pinitas | 57c033b | 2018-02-15 12:29:44 +0000 | [diff] [blame] | 305 | break; |
| 306 | default: |
| 307 | ARM_COMPUTE_ERROR("Element size not supported"); |
| 308 | break; |
| 309 | } |
| 310 | } |
| 311 | |
| 312 | void NEBatchNormalizationLayerKernel::configure_fused() |
| 313 | { |
Michele Di Giorgio | 0cbb927 | 2018-03-01 16:56:48 +0000 | [diff] [blame] | 314 | // NCHW Fused Batched Normalization with activation functions : FP32 |
| 315 | static std::map<ActivationLayerInfo::ActivationFunction, BatchNormFunctionPtr> bn_fused_map_f32_nchw = |
Georgios Pinitas | 57c033b | 2018-02-15 12:29:44 +0000 | [diff] [blame] | 316 | { |
Michele Di Giorgio | 0cbb927 | 2018-03-01 16:56:48 +0000 | [diff] [blame] | 317 | { ActivationLayerInfo::ActivationFunction::RELU, &NEBatchNormalizationLayerKernel::batch_normalization_fp32_nchw<true, ::detail::relu<float, 4>> }, |
| 318 | { ActivationLayerInfo::ActivationFunction::BOUNDED_RELU, &NEBatchNormalizationLayerKernel::batch_normalization_fp32_nchw<true, ::detail::brelu<float, 4>> }, |
| 319 | { ActivationLayerInfo::ActivationFunction::LU_BOUNDED_RELU, &NEBatchNormalizationLayerKernel::batch_normalization_fp32_nchw<true, ::detail::lubrelu<float, 4>> } |
| 320 | }; |
| 321 | // NHWC Fused Batched Normalization with activation functions : FP32 |
| 322 | static std::map<ActivationLayerInfo::ActivationFunction, BatchNormFunctionPtr> bn_fused_map_f32_nhwc = |
| 323 | { |
| 324 | { ActivationLayerInfo::ActivationFunction::RELU, &NEBatchNormalizationLayerKernel::batch_normalization_fp32_nhwc<true, ::detail::relu<float, 4>> }, |
| 325 | { ActivationLayerInfo::ActivationFunction::BOUNDED_RELU, &NEBatchNormalizationLayerKernel::batch_normalization_fp32_nhwc<true, ::detail::brelu<float, 4>> }, |
| 326 | { ActivationLayerInfo::ActivationFunction::LU_BOUNDED_RELU, &NEBatchNormalizationLayerKernel::batch_normalization_fp32_nhwc<true, ::detail::lubrelu<float, 4>> } |
Georgios Pinitas | 57c033b | 2018-02-15 12:29:44 +0000 | [diff] [blame] | 327 | }; |
| 328 | |
| 329 | switch(_input->info()->data_type()) |
| 330 | { |
| 331 | case DataType::F32: |
Michele Di Giorgio | 0cbb927 | 2018-03-01 16:56:48 +0000 | [diff] [blame] | 332 | _func = (_input->info()->data_layout() == DataLayout::NHWC) ? bn_fused_map_f32_nhwc[_act_info.activation()] : bn_fused_map_f32_nchw[_act_info.activation()]; |
Georgios Pinitas | 57c033b | 2018-02-15 12:29:44 +0000 | [diff] [blame] | 333 | break; |
| 334 | default: |
| 335 | ARM_COMPUTE_ERROR("Element size not supported"); |
| 336 | break; |
| 337 | } |
| 338 | } |
Ioan-Cristian Szabo | 303be90 | 2017-11-27 16:31:10 +0000 | [diff] [blame] | 339 | |
| 340 | NEBatchNormalizationLayerKernel::NEBatchNormalizationLayerKernel() |
Georgios Pinitas | 57c033b | 2018-02-15 12:29:44 +0000 | [diff] [blame] | 341 | : _func(nullptr), _input(nullptr), _output(nullptr), _mean(nullptr), _var(nullptr), _gamma(nullptr), _beta(nullptr), _epsilon(), _act_info() |
Ioan-Cristian Szabo | 303be90 | 2017-11-27 16:31:10 +0000 | [diff] [blame] | 342 | { |
| 343 | } |
Pablo Tello | 8fda1cb | 2017-07-05 15:20:38 +0100 | [diff] [blame] | 344 | |
Georgios Pinitas | 57c033b | 2018-02-15 12:29:44 +0000 | [diff] [blame] | 345 | void NEBatchNormalizationLayerKernel::configure(ITensor *input, ITensor *output, |
| 346 | const ITensor *mean, const ITensor *var, |
| 347 | const ITensor *beta, const ITensor *gamma, |
| 348 | float epsilon, ActivationLayerInfo act_info) |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 349 | { |
Michele Di Giorgio | 4d33630 | 2018-03-02 09:43:54 +0000 | [diff] [blame] | 350 | ARM_COMPUTE_ERROR_ON_NULLPTR(input, mean, var); |
Ioan-Cristian Szabo | 303be90 | 2017-11-27 16:31:10 +0000 | [diff] [blame] | 351 | |
Michele Di Giorgio | 4d33630 | 2018-03-02 09:43:54 +0000 | [diff] [blame] | 352 | ARM_COMPUTE_ERROR_THROW_ON(validate_arguments(input->info(), (output != nullptr) ? output->info() : nullptr, |
Georgios Pinitas | 57c033b | 2018-02-15 12:29:44 +0000 | [diff] [blame] | 353 | mean->info(), var->info(), |
Michele Di Giorgio | 4d33630 | 2018-03-02 09:43:54 +0000 | [diff] [blame] | 354 | (beta != nullptr) ? beta->info() : nullptr, |
| 355 | (gamma != nullptr) ? gamma->info() : nullptr, |
Georgios Pinitas | 57c033b | 2018-02-15 12:29:44 +0000 | [diff] [blame] | 356 | epsilon, act_info)); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 357 | |
Georgios Pinitas | 57c033b | 2018-02-15 12:29:44 +0000 | [diff] [blame] | 358 | _input = input; |
| 359 | _output = input; |
| 360 | _mean = mean; |
| 361 | _var = var; |
| 362 | _gamma = gamma; |
| 363 | _beta = beta; |
| 364 | _epsilon = epsilon; |
| 365 | _act_info = act_info; |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 366 | |
Michele Di Giorgio | 4d33630 | 2018-03-02 09:43:54 +0000 | [diff] [blame] | 367 | const bool run_in_place = (output == nullptr) || (output == input); |
| 368 | if(!run_in_place) |
Georgios Pinitas | 409ee0a | 2017-08-18 10:16:09 +0100 | [diff] [blame] | 369 | { |
Georgios Pinitas | 409ee0a | 2017-08-18 10:16:09 +0100 | [diff] [blame] | 370 | _output = output; |
| 371 | } |
| 372 | |
Georgios Pinitas | 57c033b | 2018-02-15 12:29:44 +0000 | [diff] [blame] | 373 | // Configure activation function to run |
| 374 | if(_act_info.enabled()) |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 375 | { |
Georgios Pinitas | 57c033b | 2018-02-15 12:29:44 +0000 | [diff] [blame] | 376 | configure_fused(); |
| 377 | } |
| 378 | else |
| 379 | { |
| 380 | configure_non_fused(); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 381 | } |
| 382 | |
Ioan-Cristian Szabo | 303be90 | 2017-11-27 16:31:10 +0000 | [diff] [blame] | 383 | // Configure kernel window |
Michele Di Giorgio | 4d33630 | 2018-03-02 09:43:54 +0000 | [diff] [blame] | 384 | auto win_config = validate_and_configure_window(input->info(), (run_in_place) ? nullptr : output->info()); |
Ioan-Cristian Szabo | 303be90 | 2017-11-27 16:31:10 +0000 | [diff] [blame] | 385 | ARM_COMPUTE_ERROR_THROW_ON(win_config.first); |
| 386 | INEKernel::configure(win_config.second); |
| 387 | } |
| 388 | |
Georgios Pinitas | 57c033b | 2018-02-15 12:29:44 +0000 | [diff] [blame] | 389 | Status NEBatchNormalizationLayerKernel::validate(const ITensorInfo *input, const ITensorInfo *output, |
| 390 | const ITensorInfo *mean, const ITensorInfo *var, |
| 391 | const ITensorInfo *beta, const ITensorInfo *gamma, |
| 392 | float epsilon, ActivationLayerInfo act_info) |
Ioan-Cristian Szabo | 303be90 | 2017-11-27 16:31:10 +0000 | [diff] [blame] | 393 | { |
Georgios Pinitas | 57c033b | 2018-02-15 12:29:44 +0000 | [diff] [blame] | 394 | ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments(input, output, mean, var, beta, gamma, epsilon, act_info)); |
Ioan-Cristian Szabo | 303be90 | 2017-11-27 16:31:10 +0000 | [diff] [blame] | 395 | ARM_COMPUTE_RETURN_ON_ERROR(validate_and_configure_window(input->clone().get(), output ? output->clone().get() : nullptr).first); |
| 396 | |
Georgios Pinitas | 631c41a | 2017-12-06 11:53:03 +0000 | [diff] [blame] | 397 | return Status{}; |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 398 | } |
| 399 | |
Moritz Pflanzer | c186b57 | 2017-09-07 09:48:04 +0100 | [diff] [blame] | 400 | void NEBatchNormalizationLayerKernel::run(const Window &window, const ThreadInfo &info) |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 401 | { |
Moritz Pflanzer | c186b57 | 2017-09-07 09:48:04 +0100 | [diff] [blame] | 402 | ARM_COMPUTE_UNUSED(info); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 403 | ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this); |
| 404 | ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(INEKernel::window(), window); |
| 405 | ARM_COMPUTE_ERROR_ON(_func == nullptr); |
| 406 | |
Georgios Pinitas | 57c033b | 2018-02-15 12:29:44 +0000 | [diff] [blame] | 407 | (this->*_func)(window); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 408 | } |