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