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