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