Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 1 | /* |
Sheri Zhang | 97b3f11 | 2021-01-04 17:14:23 +0000 | [diff] [blame] | 2 | * Copyright (c) 2017-2021 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 | */ |
Michalis Spyrou | ebcebf1 | 2020-10-21 00:04:14 +0100 | [diff] [blame] | 24 | #include "src/core/NEON/kernels/NEBatchNormalizationLayerKernel.h" |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 25 | |
| 26 | #include "arm_compute/core/Helpers.h" |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 27 | #include "arm_compute/core/TensorInfo.h" |
| 28 | #include "arm_compute/core/Utils.h" |
| 29 | #include "arm_compute/core/Validate.h" |
| 30 | #include "arm_compute/core/Window.h" |
Sang-Hoon Park | 68dd25f | 2020-10-19 16:00:11 +0100 | [diff] [blame] | 31 | #include "src/core/CPP/Validate.h" |
Georgios Pinitas | ddb93bb | 2020-10-02 16:38:59 +0100 | [diff] [blame] | 32 | #include "src/core/NEON/NEFixedPoint.h" |
| 33 | #include "src/core/NEON/NEMath.h" |
Sang-Hoon Park | 68dd25f | 2020-10-19 16:00:11 +0100 | [diff] [blame] | 34 | #include "src/core/helpers/AutoConfiguration.h" |
| 35 | #include "src/core/helpers/WindowHelpers.h" |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 36 | |
Georgios Pinitas | ddb93bb | 2020-10-02 16:38:59 +0100 | [diff] [blame] | 37 | #include "src/core/NEON/kernels/detail/NEActivationFunctionDetail.h" |
| 38 | #include "src/core/NEON/wrapper/wrapper.h" |
Georgios Pinitas | 980a916 | 2020-06-03 20:16:46 +0100 | [diff] [blame] | 39 | |
Sheri Zhang | 8d5d78b | 2020-12-15 20:25:31 +0000 | [diff] [blame] | 40 | #include "src/core/NEON/kernels/batchnormalization/impl/list.h" |
| 41 | #include "src/core/common/Registrars.h" |
| 42 | |
Georgios Pinitas | 57c033b | 2018-02-15 12:29:44 +0000 | [diff] [blame] | 43 | #include <map> |
| 44 | |
Georgios Pinitas | 980a916 | 2020-06-03 20:16:46 +0100 | [diff] [blame] | 45 | namespace arm_compute |
| 46 | { |
Ioan-Cristian Szabo | 303be90 | 2017-11-27 16:31:10 +0000 | [diff] [blame] | 47 | namespace |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 48 | { |
Sheri Zhang | 8d5d78b | 2020-12-15 20:25:31 +0000 | [diff] [blame] | 49 | struct BatchNormalizationSelectorData |
| 50 | { |
Michalis Spyrou | 20fca52 | 2021-06-07 14:23:57 +0100 | [diff] [blame] | 51 | DataType dt; |
| 52 | const CPUInfo &ci; |
Sheri Zhang | 8d5d78b | 2020-12-15 20:25:31 +0000 | [diff] [blame] | 53 | }; |
| 54 | using BatchNormalizationSelectorPtr = std::add_pointer<bool(const BatchNormalizationSelectorData &data)>::type; |
| 55 | using BatchNormalizationKernelPtr = std::add_pointer<void(ITensor *, ITensor *, const ITensor *, const ITensor *, const ITensor *, const ITensor *, |
| 56 | float, ActivationLayerInfo &, const Window &)>::type; |
| 57 | |
| 58 | struct BatchNormalizationKernel |
| 59 | { |
| 60 | const char *name; |
| 61 | const BatchNormalizationSelectorPtr is_selected; |
| 62 | BatchNormalizationKernelPtr ukernel; |
| 63 | }; |
| 64 | |
| 65 | static const BatchNormalizationKernel available_kernels[] = |
| 66 | { |
Michalis Spyrou | 20fca52 | 2021-06-07 14:23:57 +0100 | [diff] [blame] | 67 | #if defined(ARM_COMPUTE_ENABLE_SVE) |
Sheri Zhang | 8d5d78b | 2020-12-15 20:25:31 +0000 | [diff] [blame] | 68 | { |
Georgios Pinitas | 5fdde99 | 2021-06-25 05:42:57 +0100 | [diff] [blame] | 69 | "sve_fp16_batch_normalization", |
Michalis Spyrou | 20fca52 | 2021-06-07 14:23:57 +0100 | [diff] [blame] | 70 | [](const BatchNormalizationSelectorData & data) { return data.dt == DataType::F16 && data.ci.has_sve(); }, |
Sheri Zhang | 8d5d78b | 2020-12-15 20:25:31 +0000 | [diff] [blame] | 71 | REGISTER_FP16_SVE(arm_compute::cpu::fp16_sve_batch_normalization) |
| 72 | }, |
| 73 | { |
Georgios Pinitas | 5fdde99 | 2021-06-25 05:42:57 +0100 | [diff] [blame] | 74 | "sve_fp32_batch_normalization", |
Michalis Spyrou | 20fca52 | 2021-06-07 14:23:57 +0100 | [diff] [blame] | 75 | [](const BatchNormalizationSelectorData & data) { return data.dt == DataType::F32 && data.ci.has_sve(); }, |
Sheri Zhang | 8d5d78b | 2020-12-15 20:25:31 +0000 | [diff] [blame] | 76 | REGISTER_FP32_SVE(arm_compute::cpu::fp32_sve_batch_normalization) |
| 77 | }, |
Michalis Spyrou | 20fca52 | 2021-06-07 14:23:57 +0100 | [diff] [blame] | 78 | #endif /* !defined(ARM_COMPUTE_ENABLE_SVE) */ |
| 79 | #if defined(ARM_COMPUTE_ENABLE_NEON) |
Sheri Zhang | 97b3f11 | 2021-01-04 17:14:23 +0000 | [diff] [blame] | 80 | #if defined(__ARM_FEATURE_FP16_VECTOR_ARITHMETIC) |
Sheri Zhang | 8d5d78b | 2020-12-15 20:25:31 +0000 | [diff] [blame] | 81 | { |
Georgios Pinitas | 5fdde99 | 2021-06-25 05:42:57 +0100 | [diff] [blame] | 82 | "neon_fp16_batch_normalization", |
Sheri Zhang | 8d5d78b | 2020-12-15 20:25:31 +0000 | [diff] [blame] | 83 | [](const BatchNormalizationSelectorData & data) { return data.dt == DataType::F16; }, |
| 84 | REGISTER_FP16_NEON(arm_compute::cpu::fp16_neon_batch_normalization) |
| 85 | }, |
Sheri Zhang | 97b3f11 | 2021-01-04 17:14:23 +0000 | [diff] [blame] | 86 | #endif /* __ARM_FEATURE_FP16_VECTOR_ARITHMETIC */ |
Sheri Zhang | 8d5d78b | 2020-12-15 20:25:31 +0000 | [diff] [blame] | 87 | { |
Georgios Pinitas | 5fdde99 | 2021-06-25 05:42:57 +0100 | [diff] [blame] | 88 | "neon_fp32_batch_normalization", |
Sheri Zhang | 8d5d78b | 2020-12-15 20:25:31 +0000 | [diff] [blame] | 89 | [](const BatchNormalizationSelectorData & data) { return data.dt == DataType::F32; }, |
| 90 | REGISTER_FP32_NEON(arm_compute::cpu::fp32_neon_batch_normalization) |
| 91 | }, |
Michalis Spyrou | 20fca52 | 2021-06-07 14:23:57 +0100 | [diff] [blame] | 92 | #endif /* !defined(ARM_COMPUTE_ENABLE_NEON) */ |
Sheri Zhang | 8d5d78b | 2020-12-15 20:25:31 +0000 | [diff] [blame] | 93 | }; |
| 94 | |
| 95 | const BatchNormalizationKernel *get_implementation(const BatchNormalizationSelectorData &data) |
| 96 | { |
| 97 | for(const auto &uk : available_kernels) |
| 98 | { |
| 99 | if(uk.is_selected(data)) |
| 100 | { |
| 101 | return &uk; |
| 102 | } |
| 103 | } |
| 104 | return nullptr; |
| 105 | } |
| 106 | |
Georgios Pinitas | 57c033b | 2018-02-15 12:29:44 +0000 | [diff] [blame] | 107 | Status |
| 108 | validate_arguments(const ITensorInfo *input, const ITensorInfo *output, const ITensorInfo *mean, const ITensorInfo *var, |
| 109 | const ITensorInfo *beta, const ITensorInfo *gamma, float epsilon, ActivationLayerInfo act_info) |
Ioan-Cristian Szabo | 303be90 | 2017-11-27 16:31:10 +0000 | [diff] [blame] | 110 | { |
| 111 | ARM_COMPUTE_UNUSED(epsilon); |
Sheri Zhang | 8d5d78b | 2020-12-15 20:25:31 +0000 | [diff] [blame] | 112 | |
Michalis Spyrou | 20fca52 | 2021-06-07 14:23:57 +0100 | [diff] [blame] | 113 | const auto *uk = get_implementation(BatchNormalizationSelectorData{ input->data_type(), CPUInfo::get() }); |
Sheri Zhang | 8d5d78b | 2020-12-15 20:25:31 +0000 | [diff] [blame] | 114 | ARM_COMPUTE_RETURN_ERROR_ON(uk == nullptr || uk->ukernel == nullptr); |
Georgios Pinitas | 57c033b | 2018-02-15 12:29:44 +0000 | [diff] [blame] | 115 | |
| 116 | if(act_info.enabled()) |
| 117 | { |
| 118 | ActivationLayerInfo::ActivationFunction act = act_info.activation(); |
Georgios Pinitas | 6f109bd | 2018-07-16 12:57:42 +0100 | [diff] [blame] | 119 | ARM_COMPUTE_RETURN_ERROR_ON(act != ActivationLayerInfo::ActivationLayerInfo::ActivationFunction::RELU |
| 120 | && act != ActivationLayerInfo::ActivationLayerInfo::ActivationFunction::BOUNDED_RELU |
Georgios Pinitas | 57c033b | 2018-02-15 12:29:44 +0000 | [diff] [blame] | 121 | && act != ActivationLayerInfo::ActivationLayerInfo::ActivationFunction::LU_BOUNDED_RELU); |
| 122 | ARM_COMPUTE_RETURN_ERROR_ON(act_info.b() > act_info.a()); |
| 123 | } |
Ioan-Cristian Szabo | 303be90 | 2017-11-27 16:31:10 +0000 | [diff] [blame] | 124 | |
| 125 | if(nullptr != output) |
| 126 | { |
| 127 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_SHAPES(input, output); |
Michele Di Giorgio | 0cbb927 | 2018-03-01 16:56:48 +0000 | [diff] [blame] | 128 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_LAYOUT(input, output); |
Ioan-Cristian Szabo | 303be90 | 2017-11-27 16:31:10 +0000 | [diff] [blame] | 129 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input, output); |
Ioan-Cristian Szabo | 303be90 | 2017-11-27 16:31:10 +0000 | [diff] [blame] | 130 | } |
| 131 | |
Michele Di Giorgio | 4d33630 | 2018-03-02 09:43:54 +0000 | [diff] [blame] | 132 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input, mean, var); |
Michele Di Giorgio | 4d33630 | 2018-03-02 09:43:54 +0000 | [diff] [blame] | 133 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_SHAPES(mean, var); |
| 134 | if(beta != nullptr) |
| 135 | { |
| 136 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input, beta); |
Michele Di Giorgio | 4d33630 | 2018-03-02 09:43:54 +0000 | [diff] [blame] | 137 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_SHAPES(mean, beta); |
| 138 | } |
| 139 | if(gamma != nullptr) |
| 140 | { |
| 141 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input, gamma); |
Michele Di Giorgio | 4d33630 | 2018-03-02 09:43:54 +0000 | [diff] [blame] | 142 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_SHAPES(mean, gamma); |
| 143 | } |
Michele Di Giorgio | 0cbb927 | 2018-03-01 16:56:48 +0000 | [diff] [blame] | 144 | 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] | 145 | |
Georgios Pinitas | 631c41a | 2017-12-06 11:53:03 +0000 | [diff] [blame] | 146 | return Status{}; |
Ioan-Cristian Szabo | 303be90 | 2017-11-27 16:31:10 +0000 | [diff] [blame] | 147 | } |
Georgios Pinitas | 57c033b | 2018-02-15 12:29:44 +0000 | [diff] [blame] | 148 | } //namespace |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 149 | |
Georgios Pinitas | 980a916 | 2020-06-03 20:16:46 +0100 | [diff] [blame] | 150 | template <typename T, bool fused_activation, typename F> |
| 151 | void NEBatchNormalizationLayerKernel::batch_normalization_nchw(const Window &window) |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 152 | { |
Michele Di Giorgio | 33f41fa | 2021-03-09 14:09:08 +0000 | [diff] [blame] | 153 | /** SIMD vector tag type. */ |
Georgios Pinitas | 980a916 | 2020-06-03 20:16:46 +0100 | [diff] [blame] | 154 | using ExactTagType = typename wrapper::traits::neon_bitvector_tag_t<T, wrapper::traits::BitWidth::W128>; |
| 155 | |
| 156 | const int window_step_x = 16 / sizeof(T); |
| 157 | const auto window_start_x = static_cast<int>(window.x().start()); |
| 158 | const auto window_end_x = static_cast<int>(window.x().end()); |
| 159 | |
| 160 | Window win_to_use = window; |
| 161 | win_to_use.set(Window::DimX, Window::Dimension(0, 1, 1)); |
| 162 | |
| 163 | Iterator input(_input, win_to_use); |
| 164 | Iterator output(_output, win_to_use); |
Pablo Tello | 8fda1cb | 2017-07-05 15:20:38 +0100 | [diff] [blame] | 165 | |
Georgios Pinitas | aaba4c6 | 2018-08-22 16:20:21 +0100 | [diff] [blame] | 166 | F activation_functor(_act_info); |
| 167 | |
Pablo Tello | 8fda1cb | 2017-07-05 15:20:38 +0100 | [diff] [blame] | 168 | // Hold information about the current feature map we are iterating. |
Michele Di Giorgio | 33f41fa | 2021-03-09 14:09:08 +0000 | [diff] [blame] | 169 | // Only compute denominator and constants once per feature map. |
Pablo Tello | 8fda1cb | 2017-07-05 15:20:38 +0100 | [diff] [blame] | 170 | int slice = -1; |
| 171 | |
Georgios Pinitas | 980a916 | 2020-06-03 20:16:46 +0100 | [diff] [blame] | 172 | const auto input_mean = reinterpret_cast<const T *>(_mean->ptr_to_element(Coordinates(0, 0))); |
| 173 | const auto input_var = reinterpret_cast<const T *>(_var->ptr_to_element(Coordinates(0, 0))); |
| 174 | const auto input_gamma = (_gamma != nullptr) ? reinterpret_cast<const T *>(_gamma->ptr_to_element(Coordinates(0, 0))) : nullptr; |
| 175 | const auto input_beta = (_beta != nullptr) ? reinterpret_cast<const T *>(_beta->ptr_to_element(Coordinates(0, 0))) : nullptr; |
Pablo Tello | 8fda1cb | 2017-07-05 15:20:38 +0100 | [diff] [blame] | 176 | |
Georgios Pinitas | 980a916 | 2020-06-03 20:16:46 +0100 | [diff] [blame] | 177 | T mean = static_cast<T>(0); |
| 178 | T var = static_cast<T>(0); |
| 179 | T gamma = static_cast<T>(1); |
| 180 | T beta = static_cast<T>(0); |
| 181 | T denominator = static_cast<T>(0); |
| 182 | |
| 183 | auto mean_vec = wrapper::vdup_n(mean, ExactTagType{}); |
| 184 | auto var_vec = wrapper::vdup_n(var, ExactTagType{}); |
| 185 | auto gamma_vec = wrapper::vdup_n(gamma, ExactTagType{}); |
| 186 | auto beta_vec = wrapper::vdup_n(beta, ExactTagType{}); |
| 187 | auto denominator_vec = wrapper::vdup_n(denominator, ExactTagType{}); |
| 188 | const auto epsilon_vec = wrapper::vdup_n(static_cast<T>(_epsilon), ExactTagType{}); |
| 189 | execute_window_loop(win_to_use, [&](const Coordinates & id) |
Pablo Tello | 8fda1cb | 2017-07-05 15:20:38 +0100 | [diff] [blame] | 190 | { |
Georgios Pinitas | 980a916 | 2020-06-03 20:16:46 +0100 | [diff] [blame] | 191 | const auto input_ptr = reinterpret_cast<const T *>(input.ptr()); |
| 192 | const auto output_ptr = reinterpret_cast<T *>(output.ptr()); |
| 193 | |
Pablo Tello | 8fda1cb | 2017-07-05 15:20:38 +0100 | [diff] [blame] | 194 | if(slice != id.z()) |
| 195 | { |
Georgios Pinitas | 980a916 | 2020-06-03 20:16:46 +0100 | [diff] [blame] | 196 | mean = input_mean[id.z()]; |
| 197 | var = input_var[id.z()]; |
| 198 | mean_vec = wrapper::vdup_n(mean, ExactTagType{}); |
| 199 | var_vec = wrapper::vdup_n(var, ExactTagType{}); |
Michele Di Giorgio | 4d33630 | 2018-03-02 09:43:54 +0000 | [diff] [blame] | 200 | if(input_gamma != nullptr) |
| 201 | { |
Georgios Pinitas | 980a916 | 2020-06-03 20:16:46 +0100 | [diff] [blame] | 202 | gamma = input_gamma[id.z()]; |
| 203 | gamma_vec = wrapper::vdup_n(gamma, ExactTagType{}); |
Michele Di Giorgio | 4d33630 | 2018-03-02 09:43:54 +0000 | [diff] [blame] | 204 | } |
| 205 | if(input_beta != nullptr) |
| 206 | { |
Georgios Pinitas | 980a916 | 2020-06-03 20:16:46 +0100 | [diff] [blame] | 207 | beta = input_beta[id.z()]; |
| 208 | beta_vec = wrapper::vdup_n(beta, ExactTagType{}); |
Michele Di Giorgio | 4d33630 | 2018-03-02 09:43:54 +0000 | [diff] [blame] | 209 | } |
Pablo Tello | 8fda1cb | 2017-07-05 15:20:38 +0100 | [diff] [blame] | 210 | |
| 211 | // Calculate denominator |
Georgios Pinitas | 980a916 | 2020-06-03 20:16:46 +0100 | [diff] [blame] | 212 | denominator_vec = wrapper::vinvsqrt(wrapper::vadd(var_vec, epsilon_vec)); |
| 213 | denominator = wrapper::vgetlane(denominator_vec, 0); |
| 214 | slice = id.z(); |
Pablo Tello | 8fda1cb | 2017-07-05 15:20:38 +0100 | [diff] [blame] | 215 | } |
| 216 | |
Georgios Pinitas | 980a916 | 2020-06-03 20:16:46 +0100 | [diff] [blame] | 217 | // Perform core calculations using vector operations |
| 218 | int x = window_start_x; |
| 219 | for(; x <= (window_end_x - window_step_x); x += window_step_x) |
Georgios Pinitas | aaba4c6 | 2018-08-22 16:20:21 +0100 | [diff] [blame] | 220 | { |
Georgios Pinitas | 980a916 | 2020-06-03 20:16:46 +0100 | [diff] [blame] | 221 | // Calculate x bar |
| 222 | const auto numerator = wrapper::vsub(wrapper::vloadq(input_ptr + x), mean_vec); |
| 223 | const auto x_bar = wrapper::vmul(numerator, denominator_vec); |
| 224 | auto res = wrapper::vmla(beta_vec, x_bar, gamma_vec); |
| 225 | |
| 226 | // Perform fused activation |
| 227 | if(fused_activation) |
| 228 | { |
| 229 | activation_functor(res); |
| 230 | } |
| 231 | |
| 232 | // Store results |
| 233 | wrapper::vstore(output_ptr + x, res); |
Georgios Pinitas | aaba4c6 | 2018-08-22 16:20:21 +0100 | [diff] [blame] | 234 | } |
| 235 | |
Georgios Pinitas | 980a916 | 2020-06-03 20:16:46 +0100 | [diff] [blame] | 236 | // Compute left-over elements |
| 237 | for(; x < window_end_x; ++x) |
| 238 | { |
| 239 | const T numerator = input_ptr[x] - mean; |
| 240 | const T x_bar = numerator * denominator; |
| 241 | T res = beta + x_bar * gamma; |
| 242 | |
| 243 | // Perform fused activation |
| 244 | if(fused_activation) |
| 245 | { |
| 246 | activation_functor(res); |
| 247 | } |
| 248 | |
| 249 | // Store results |
| 250 | *(output_ptr + x) = res; |
| 251 | } |
Pablo Tello | 8fda1cb | 2017-07-05 15:20:38 +0100 | [diff] [blame] | 252 | }, |
| 253 | input, output); |
Georgios Pinitas | 57c033b | 2018-02-15 12:29:44 +0000 | [diff] [blame] | 254 | } |
| 255 | |
Georgios Pinitas | 57c033b | 2018-02-15 12:29:44 +0000 | [diff] [blame] | 256 | void NEBatchNormalizationLayerKernel::configure_non_fused() |
| 257 | { |
| 258 | switch(_input->info()->data_type()) |
| 259 | { |
Georgios Pinitas | aaba4c6 | 2018-08-22 16:20:21 +0100 | [diff] [blame] | 260 | #ifdef __ARM_FEATURE_FP16_VECTOR_ARITHMETIC |
Georgios Pinitas | 57c033b | 2018-02-15 12:29:44 +0000 | [diff] [blame] | 261 | case DataType::F16: |
Sheri Zhang | 8d5d78b | 2020-12-15 20:25:31 +0000 | [diff] [blame] | 262 | _func = &NEBatchNormalizationLayerKernel::batch_normalization_nchw<float16_t, false, detail::dummy<float16_t, 8>>; |
Georgios Pinitas | 57c033b | 2018-02-15 12:29:44 +0000 | [diff] [blame] | 263 | break; |
Georgios Pinitas | aaba4c6 | 2018-08-22 16:20:21 +0100 | [diff] [blame] | 264 | #endif // __ARM_FEATURE_FP16_VECTOR_ARITHMETIC |
Georgios Pinitas | 57c033b | 2018-02-15 12:29:44 +0000 | [diff] [blame] | 265 | case DataType::F32: |
Sheri Zhang | 8d5d78b | 2020-12-15 20:25:31 +0000 | [diff] [blame] | 266 | _func = &NEBatchNormalizationLayerKernel::batch_normalization_nchw<float, false, detail::dummy<float, 4>>; |
Georgios Pinitas | 57c033b | 2018-02-15 12:29:44 +0000 | [diff] [blame] | 267 | break; |
| 268 | default: |
| 269 | ARM_COMPUTE_ERROR("Element size not supported"); |
| 270 | break; |
| 271 | } |
| 272 | } |
| 273 | |
| 274 | void NEBatchNormalizationLayerKernel::configure_fused() |
| 275 | { |
Michele Di Giorgio | 0cbb927 | 2018-03-01 16:56:48 +0000 | [diff] [blame] | 276 | // NCHW Fused Batched Normalization with activation functions : FP32 |
| 277 | static std::map<ActivationLayerInfo::ActivationFunction, BatchNormFunctionPtr> bn_fused_map_f32_nchw = |
Georgios Pinitas | 57c033b | 2018-02-15 12:29:44 +0000 | [diff] [blame] | 278 | { |
Georgios Pinitas | 980a916 | 2020-06-03 20:16:46 +0100 | [diff] [blame] | 279 | { ActivationLayerInfo::ActivationFunction::RELU, &NEBatchNormalizationLayerKernel::batch_normalization_nchw<float, true, detail::relu<float, 4>> }, |
| 280 | { ActivationLayerInfo::ActivationFunction::BOUNDED_RELU, &NEBatchNormalizationLayerKernel::batch_normalization_nchw<float, true, detail::brelu<float, 4>> }, |
| 281 | { ActivationLayerInfo::ActivationFunction::LU_BOUNDED_RELU, &NEBatchNormalizationLayerKernel::batch_normalization_nchw<float, true, detail::lubrelu<float, 4>> } |
Michele Di Giorgio | 0cbb927 | 2018-03-01 16:56:48 +0000 | [diff] [blame] | 282 | }; |
Georgios Pinitas | aaba4c6 | 2018-08-22 16:20:21 +0100 | [diff] [blame] | 283 | #ifdef __ARM_FEATURE_FP16_VECTOR_ARITHMETIC |
| 284 | // NCHW Fused Batched Normalization with activation functions : FP16 |
| 285 | static std::map<ActivationLayerInfo::ActivationFunction, BatchNormFunctionPtr> bn_fused_map_f16_nchw = |
| 286 | { |
Georgios Pinitas | 980a916 | 2020-06-03 20:16:46 +0100 | [diff] [blame] | 287 | { ActivationLayerInfo::ActivationFunction::RELU, &NEBatchNormalizationLayerKernel::batch_normalization_nchw<float16_t, true, detail::relu<float16_t, 8>> }, |
| 288 | { ActivationLayerInfo::ActivationFunction::BOUNDED_RELU, &NEBatchNormalizationLayerKernel::batch_normalization_nchw<float16_t, true, detail::brelu<float16_t, 8>> }, |
| 289 | { ActivationLayerInfo::ActivationFunction::LU_BOUNDED_RELU, &NEBatchNormalizationLayerKernel::batch_normalization_nchw<float16_t, true, detail::lubrelu<float16_t, 8>> } |
Georgios Pinitas | aaba4c6 | 2018-08-22 16:20:21 +0100 | [diff] [blame] | 290 | }; |
Georgios Pinitas | aaba4c6 | 2018-08-22 16:20:21 +0100 | [diff] [blame] | 291 | #endif // __ARM_FEATURE_FP16_VECTOR_ARITHMETIC |
Georgios Pinitas | 57c033b | 2018-02-15 12:29:44 +0000 | [diff] [blame] | 292 | |
| 293 | switch(_input->info()->data_type()) |
| 294 | { |
Georgios Pinitas | aaba4c6 | 2018-08-22 16:20:21 +0100 | [diff] [blame] | 295 | #ifdef __ARM_FEATURE_FP16_VECTOR_ARITHMETIC |
| 296 | case DataType::F16: |
Sheri Zhang | 8d5d78b | 2020-12-15 20:25:31 +0000 | [diff] [blame] | 297 | _func = bn_fused_map_f16_nchw[_act_info.activation()]; |
Georgios Pinitas | aaba4c6 | 2018-08-22 16:20:21 +0100 | [diff] [blame] | 298 | break; |
| 299 | #endif // __ARM_FEATURE_FP16_VECTOR_ARITHMETIC |
Georgios Pinitas | 57c033b | 2018-02-15 12:29:44 +0000 | [diff] [blame] | 300 | case DataType::F32: |
Sheri Zhang | 8d5d78b | 2020-12-15 20:25:31 +0000 | [diff] [blame] | 301 | _func = bn_fused_map_f32_nchw[_act_info.activation()]; |
Georgios Pinitas | 57c033b | 2018-02-15 12:29:44 +0000 | [diff] [blame] | 302 | break; |
| 303 | default: |
| 304 | ARM_COMPUTE_ERROR("Element size not supported"); |
| 305 | break; |
| 306 | } |
| 307 | } |
Ioan-Cristian Szabo | 303be90 | 2017-11-27 16:31:10 +0000 | [diff] [blame] | 308 | |
| 309 | NEBatchNormalizationLayerKernel::NEBatchNormalizationLayerKernel() |
Georgios Pinitas | 57c033b | 2018-02-15 12:29:44 +0000 | [diff] [blame] | 310 | : _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] | 311 | { |
| 312 | } |
Pablo Tello | 8fda1cb | 2017-07-05 15:20:38 +0100 | [diff] [blame] | 313 | |
Georgios Pinitas | 57c033b | 2018-02-15 12:29:44 +0000 | [diff] [blame] | 314 | void NEBatchNormalizationLayerKernel::configure(ITensor *input, ITensor *output, |
| 315 | const ITensor *mean, const ITensor *var, |
| 316 | const ITensor *beta, const ITensor *gamma, |
| 317 | float epsilon, ActivationLayerInfo act_info) |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 318 | { |
Michele Di Giorgio | 4d33630 | 2018-03-02 09:43:54 +0000 | [diff] [blame] | 319 | ARM_COMPUTE_ERROR_ON_NULLPTR(input, mean, var); |
Ioan-Cristian Szabo | 303be90 | 2017-11-27 16:31:10 +0000 | [diff] [blame] | 320 | |
Michele Di Giorgio | 4d33630 | 2018-03-02 09:43:54 +0000 | [diff] [blame] | 321 | 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] | 322 | mean->info(), var->info(), |
Michele Di Giorgio | 4d33630 | 2018-03-02 09:43:54 +0000 | [diff] [blame] | 323 | (beta != nullptr) ? beta->info() : nullptr, |
| 324 | (gamma != nullptr) ? gamma->info() : nullptr, |
Georgios Pinitas | 57c033b | 2018-02-15 12:29:44 +0000 | [diff] [blame] | 325 | epsilon, act_info)); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 326 | |
Georgios Pinitas | 57c033b | 2018-02-15 12:29:44 +0000 | [diff] [blame] | 327 | _input = input; |
| 328 | _output = input; |
| 329 | _mean = mean; |
| 330 | _var = var; |
| 331 | _gamma = gamma; |
| 332 | _beta = beta; |
| 333 | _epsilon = epsilon; |
| 334 | _act_info = act_info; |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 335 | |
Michele Di Giorgio | 4d33630 | 2018-03-02 09:43:54 +0000 | [diff] [blame] | 336 | const bool run_in_place = (output == nullptr) || (output == input); |
| 337 | if(!run_in_place) |
Georgios Pinitas | 409ee0a | 2017-08-18 10:16:09 +0100 | [diff] [blame] | 338 | { |
Georgios Pinitas | 409ee0a | 2017-08-18 10:16:09 +0100 | [diff] [blame] | 339 | _output = output; |
| 340 | } |
| 341 | |
Georgios Pinitas | 57c033b | 2018-02-15 12:29:44 +0000 | [diff] [blame] | 342 | // Configure activation function to run |
Sheri Zhang | 8d5d78b | 2020-12-15 20:25:31 +0000 | [diff] [blame] | 343 | const bool is_nchw = _input->info()->data_layout() == DataLayout::NCHW; |
| 344 | if(is_nchw) |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 345 | { |
Sheri Zhang | 8d5d78b | 2020-12-15 20:25:31 +0000 | [diff] [blame] | 346 | if(_act_info.enabled()) |
| 347 | { |
| 348 | configure_fused(); |
| 349 | } |
| 350 | else |
| 351 | { |
| 352 | configure_non_fused(); |
| 353 | } |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 354 | } |
| 355 | |
Ioan-Cristian Szabo | 303be90 | 2017-11-27 16:31:10 +0000 | [diff] [blame] | 356 | // Configure kernel window |
Sheri Zhang | 8d5d78b | 2020-12-15 20:25:31 +0000 | [diff] [blame] | 357 | Window win = calculate_max_window(*input->info(), Steps()); |
| 358 | INEKernel::configure(win); |
| 359 | |
| 360 | if(output != nullptr) |
| 361 | { |
| 362 | // Output auto initialization if not yet initialized |
| 363 | auto_init_if_empty(*output->info(), *input->info()->clone()); |
Sheri Zhang | 8d5d78b | 2020-12-15 20:25:31 +0000 | [diff] [blame] | 364 | } |
Ioan-Cristian Szabo | 303be90 | 2017-11-27 16:31:10 +0000 | [diff] [blame] | 365 | } |
| 366 | |
Georgios Pinitas | 57c033b | 2018-02-15 12:29:44 +0000 | [diff] [blame] | 367 | Status NEBatchNormalizationLayerKernel::validate(const ITensorInfo *input, const ITensorInfo *output, |
| 368 | const ITensorInfo *mean, const ITensorInfo *var, |
| 369 | const ITensorInfo *beta, const ITensorInfo *gamma, |
| 370 | float epsilon, ActivationLayerInfo act_info) |
Ioan-Cristian Szabo | 303be90 | 2017-11-27 16:31:10 +0000 | [diff] [blame] | 371 | { |
Georgios Pinitas | 57c033b | 2018-02-15 12:29:44 +0000 | [diff] [blame] | 372 | 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] | 373 | |
Georgios Pinitas | 631c41a | 2017-12-06 11:53:03 +0000 | [diff] [blame] | 374 | return Status{}; |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 375 | } |
| 376 | |
Moritz Pflanzer | c186b57 | 2017-09-07 09:48:04 +0100 | [diff] [blame] | 377 | void NEBatchNormalizationLayerKernel::run(const Window &window, const ThreadInfo &info) |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 378 | { |
Moritz Pflanzer | c186b57 | 2017-09-07 09:48:04 +0100 | [diff] [blame] | 379 | ARM_COMPUTE_UNUSED(info); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 380 | ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this); |
| 381 | ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(INEKernel::window(), window); |
Sheri Zhang | 8d5d78b | 2020-12-15 20:25:31 +0000 | [diff] [blame] | 382 | ARM_COMPUTE_ERROR_ON(_func == nullptr && _input->info()->data_layout() == DataLayout::NCHW); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 383 | |
Sheri Zhang | 8d5d78b | 2020-12-15 20:25:31 +0000 | [diff] [blame] | 384 | const bool is_nchw = _input->info()->data_layout() == DataLayout::NCHW; |
| 385 | if(is_nchw) |
| 386 | { |
| 387 | (this->*_func)(window); |
| 388 | } |
| 389 | else |
| 390 | { |
Michalis Spyrou | 20fca52 | 2021-06-07 14:23:57 +0100 | [diff] [blame] | 391 | const auto *uk = get_implementation(BatchNormalizationSelectorData{ _input->info()->data_type(), CPUInfo::get() }); |
Sheri Zhang | 8d5d78b | 2020-12-15 20:25:31 +0000 | [diff] [blame] | 392 | uk->ukernel(_input, _output, _mean, _var, _beta, _gamma, _epsilon, _act_info, window); |
| 393 | } |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 394 | } |
Georgios Pinitas | 980a916 | 2020-06-03 20:16:46 +0100 | [diff] [blame] | 395 | } // namespace arm_compute |