Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 1 | /* |
Pablo Marquez Tello | 8d4cdd4 | 2023-11-21 10:10:01 +0000 | [diff] [blame] | 2 | * Copyright (c) 2017-2021, 2023 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" |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 31 | |
| 32 | #include "src/core/common/Registrars.h" |
Sang-Hoon Park | 68dd25f | 2020-10-19 16:00:11 +0100 | [diff] [blame] | 33 | #include "src/core/CPP/Validate.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" |
Sheri Zhang | 8d5d78b | 2020-12-15 20:25:31 +0000 | [diff] [blame] | 36 | #include "src/core/NEON/kernels/batchnormalization/impl/list.h" |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 37 | #include "src/core/NEON/kernels/detail/NEActivationFunctionDetail.h" |
| 38 | #include "src/core/NEON/NEFixedPoint.h" |
| 39 | #include "src/core/NEON/NEMath.h" |
| 40 | #include "src/core/NEON/wrapper/wrapper.h" |
Sheri Zhang | 8d5d78b | 2020-12-15 20:25:31 +0000 | [diff] [blame] | 41 | |
Georgios Pinitas | 57c033b | 2018-02-15 12:29:44 +0000 | [diff] [blame] | 42 | #include <map> |
| 43 | |
Georgios Pinitas | 980a916 | 2020-06-03 20:16:46 +0100 | [diff] [blame] | 44 | namespace arm_compute |
| 45 | { |
Ioan-Cristian Szabo | 303be90 | 2017-11-27 16:31:10 +0000 | [diff] [blame] | 46 | namespace |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 47 | { |
Sheri Zhang | 8d5d78b | 2020-12-15 20:25:31 +0000 | [diff] [blame] | 48 | struct BatchNormalizationSelectorData |
| 49 | { |
Michalis Spyrou | 20fca52 | 2021-06-07 14:23:57 +0100 | [diff] [blame] | 50 | DataType dt; |
| 51 | const CPUInfo &ci; |
Sheri Zhang | 8d5d78b | 2020-12-15 20:25:31 +0000 | [diff] [blame] | 52 | }; |
| 53 | using BatchNormalizationSelectorPtr = std::add_pointer<bool(const BatchNormalizationSelectorData &data)>::type; |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 54 | using BatchNormalizationKernelPtr = std::add_pointer<void(ITensor *, |
| 55 | ITensor *, |
| 56 | const ITensor *, |
| 57 | const ITensor *, |
| 58 | const ITensor *, |
| 59 | const ITensor *, |
| 60 | float, |
| 61 | ActivationLayerInfo &, |
| 62 | const Window &)>::type; |
Sheri Zhang | 8d5d78b | 2020-12-15 20:25:31 +0000 | [diff] [blame] | 63 | |
| 64 | struct BatchNormalizationKernel |
| 65 | { |
| 66 | const char *name; |
| 67 | const BatchNormalizationSelectorPtr is_selected; |
| 68 | BatchNormalizationKernelPtr ukernel; |
| 69 | }; |
| 70 | |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 71 | static const BatchNormalizationKernel available_kernels[] = { |
Michalis Spyrou | 20fca52 | 2021-06-07 14:23:57 +0100 | [diff] [blame] | 72 | #if defined(ARM_COMPUTE_ENABLE_SVE) |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 73 | {"sve_fp16_batch_normalization", |
| 74 | [](const BatchNormalizationSelectorData &data) { return data.dt == DataType::F16 && data.ci.has_sve(); }, |
| 75 | REGISTER_FP16_SVE(arm_compute::cpu::fp16_sve_batch_normalization)}, |
| 76 | {"sve_fp32_batch_normalization", |
| 77 | [](const BatchNormalizationSelectorData &data) { return data.dt == DataType::F32 && data.ci.has_sve(); }, |
| 78 | REGISTER_FP32_SVE(arm_compute::cpu::fp32_sve_batch_normalization)}, |
Michalis Spyrou | 20fca52 | 2021-06-07 14:23:57 +0100 | [diff] [blame] | 79 | #endif /* !defined(ARM_COMPUTE_ENABLE_SVE) */ |
| 80 | #if defined(ARM_COMPUTE_ENABLE_NEON) |
Sheri Zhang | 97b3f11 | 2021-01-04 17:14:23 +0000 | [diff] [blame] | 81 | #if defined(__ARM_FEATURE_FP16_VECTOR_ARITHMETIC) |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 82 | {"neon_fp16_batch_normalization", |
| 83 | [](const BatchNormalizationSelectorData &data) { return data.dt == DataType::F16; }, |
| 84 | REGISTER_FP16_NEON(arm_compute::cpu::fp16_neon_batch_normalization)}, |
Sheri Zhang | 97b3f11 | 2021-01-04 17:14:23 +0000 | [diff] [blame] | 85 | #endif /* __ARM_FEATURE_FP16_VECTOR_ARITHMETIC */ |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 86 | {"neon_fp32_batch_normalization", |
| 87 | [](const BatchNormalizationSelectorData &data) { return data.dt == DataType::F32; }, |
| 88 | REGISTER_FP32_NEON(arm_compute::cpu::fp32_neon_batch_normalization)}, |
Michalis Spyrou | 20fca52 | 2021-06-07 14:23:57 +0100 | [diff] [blame] | 89 | #endif /* !defined(ARM_COMPUTE_ENABLE_NEON) */ |
Sheri Zhang | 8d5d78b | 2020-12-15 20:25:31 +0000 | [diff] [blame] | 90 | }; |
| 91 | |
| 92 | const BatchNormalizationKernel *get_implementation(const BatchNormalizationSelectorData &data) |
| 93 | { |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 94 | for (const auto &uk : available_kernels) |
Sheri Zhang | 8d5d78b | 2020-12-15 20:25:31 +0000 | [diff] [blame] | 95 | { |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 96 | if (uk.is_selected(data)) |
Sheri Zhang | 8d5d78b | 2020-12-15 20:25:31 +0000 | [diff] [blame] | 97 | { |
| 98 | return &uk; |
| 99 | } |
| 100 | } |
| 101 | return nullptr; |
| 102 | } |
| 103 | |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 104 | Status validate_arguments(const ITensorInfo *input, |
| 105 | const ITensorInfo *output, |
| 106 | const ITensorInfo *mean, |
| 107 | const ITensorInfo *var, |
| 108 | const ITensorInfo *beta, |
| 109 | const ITensorInfo *gamma, |
| 110 | float epsilon, |
| 111 | ActivationLayerInfo act_info) |
Ioan-Cristian Szabo | 303be90 | 2017-11-27 16:31:10 +0000 | [diff] [blame] | 112 | { |
| 113 | ARM_COMPUTE_UNUSED(epsilon); |
Sheri Zhang | 8d5d78b | 2020-12-15 20:25:31 +0000 | [diff] [blame] | 114 | |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 115 | const auto *uk = get_implementation(BatchNormalizationSelectorData{input->data_type(), CPUInfo::get()}); |
Sheri Zhang | 8d5d78b | 2020-12-15 20:25:31 +0000 | [diff] [blame] | 116 | ARM_COMPUTE_RETURN_ERROR_ON(uk == nullptr || uk->ukernel == nullptr); |
Georgios Pinitas | 57c033b | 2018-02-15 12:29:44 +0000 | [diff] [blame] | 117 | |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 118 | if (act_info.enabled()) |
Georgios Pinitas | 57c033b | 2018-02-15 12:29:44 +0000 | [diff] [blame] | 119 | { |
| 120 | ActivationLayerInfo::ActivationFunction act = act_info.activation(); |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 121 | ARM_COMPUTE_RETURN_ERROR_ON(act != ActivationLayerInfo::ActivationLayerInfo::ActivationFunction::RELU && |
| 122 | act != ActivationLayerInfo::ActivationLayerInfo::ActivationFunction::BOUNDED_RELU && |
| 123 | act != |
| 124 | ActivationLayerInfo::ActivationLayerInfo::ActivationFunction::LU_BOUNDED_RELU); |
Georgios Pinitas | 57c033b | 2018-02-15 12:29:44 +0000 | [diff] [blame] | 125 | ARM_COMPUTE_RETURN_ERROR_ON(act_info.b() > act_info.a()); |
| 126 | } |
Ioan-Cristian Szabo | 303be90 | 2017-11-27 16:31:10 +0000 | [diff] [blame] | 127 | |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 128 | if (nullptr != output) |
Ioan-Cristian Szabo | 303be90 | 2017-11-27 16:31:10 +0000 | [diff] [blame] | 129 | { |
| 130 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_SHAPES(input, output); |
Michele Di Giorgio | 0cbb927 | 2018-03-01 16:56:48 +0000 | [diff] [blame] | 131 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_LAYOUT(input, output); |
Ioan-Cristian Szabo | 303be90 | 2017-11-27 16:31:10 +0000 | [diff] [blame] | 132 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input, output); |
Ioan-Cristian Szabo | 303be90 | 2017-11-27 16:31:10 +0000 | [diff] [blame] | 133 | } |
| 134 | |
Michele Di Giorgio | 4d33630 | 2018-03-02 09:43:54 +0000 | [diff] [blame] | 135 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input, mean, var); |
Michele Di Giorgio | 4d33630 | 2018-03-02 09:43:54 +0000 | [diff] [blame] | 136 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_SHAPES(mean, var); |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 137 | if (beta != nullptr) |
Michele Di Giorgio | 4d33630 | 2018-03-02 09:43:54 +0000 | [diff] [blame] | 138 | { |
| 139 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input, beta); |
Michele Di Giorgio | 4d33630 | 2018-03-02 09:43:54 +0000 | [diff] [blame] | 140 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_SHAPES(mean, beta); |
| 141 | } |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 142 | if (gamma != nullptr) |
Michele Di Giorgio | 4d33630 | 2018-03-02 09:43:54 +0000 | [diff] [blame] | 143 | { |
| 144 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input, gamma); |
Michele Di Giorgio | 4d33630 | 2018-03-02 09:43:54 +0000 | [diff] [blame] | 145 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_SHAPES(mean, gamma); |
| 146 | } |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 147 | ARM_COMPUTE_RETURN_ERROR_ON(input->dimension(get_data_layout_dimension_index( |
| 148 | input->data_layout(), DataLayoutDimension::CHANNEL)) != mean->dimension(0)); |
Ioan-Cristian Szabo | 303be90 | 2017-11-27 16:31:10 +0000 | [diff] [blame] | 149 | |
Georgios Pinitas | 631c41a | 2017-12-06 11:53:03 +0000 | [diff] [blame] | 150 | return Status{}; |
Ioan-Cristian Szabo | 303be90 | 2017-11-27 16:31:10 +0000 | [diff] [blame] | 151 | } |
Georgios Pinitas | 57c033b | 2018-02-15 12:29:44 +0000 | [diff] [blame] | 152 | } //namespace |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 153 | |
Georgios Pinitas | 57c033b | 2018-02-15 12:29:44 +0000 | [diff] [blame] | 154 | void NEBatchNormalizationLayerKernel::configure_non_fused() |
| 155 | { |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 156 | switch (_input->info()->data_type()) |
Georgios Pinitas | 57c033b | 2018-02-15 12:29:44 +0000 | [diff] [blame] | 157 | { |
Georgios Pinitas | 57c033b | 2018-02-15 12:29:44 +0000 | [diff] [blame] | 158 | case DataType::F16: |
Pablo Marquez Tello | 8d4cdd4 | 2023-11-21 10:10:01 +0000 | [diff] [blame] | 159 | _func = REGISTER_FP16_NEON(cpu::fp16_batch_normalization_nchw_non_fused); |
Georgios Pinitas | 57c033b | 2018-02-15 12:29:44 +0000 | [diff] [blame] | 160 | break; |
| 161 | case DataType::F32: |
Pablo Marquez Tello | 8d4cdd4 | 2023-11-21 10:10:01 +0000 | [diff] [blame] | 162 | _func = REGISTER_FP32_NEON(cpu::fp32_batch_normalization_nchw_non_fused); |
Georgios Pinitas | 57c033b | 2018-02-15 12:29:44 +0000 | [diff] [blame] | 163 | break; |
| 164 | default: |
| 165 | ARM_COMPUTE_ERROR("Element size not supported"); |
| 166 | break; |
| 167 | } |
| 168 | } |
| 169 | |
| 170 | void NEBatchNormalizationLayerKernel::configure_fused() |
| 171 | { |
Michele Di Giorgio | 0cbb927 | 2018-03-01 16:56:48 +0000 | [diff] [blame] | 172 | // NCHW Fused Batched Normalization with activation functions : FP32 |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 173 | static std::map<ActivationLayerInfo::ActivationFunction, BatchNormFunctionPtr> bn_fused_map_f32_nchw = { |
| 174 | {ActivationLayerInfo::ActivationFunction::RELU, |
Pablo Marquez Tello | 8d4cdd4 | 2023-11-21 10:10:01 +0000 | [diff] [blame] | 175 | REGISTER_FP32_NEON(cpu::fp32_batch_normalization_nchw_non_fused_relu)}, |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 176 | {ActivationLayerInfo::ActivationFunction::BOUNDED_RELU, |
Pablo Marquez Tello | 8d4cdd4 | 2023-11-21 10:10:01 +0000 | [diff] [blame] | 177 | REGISTER_FP32_NEON(cpu::fp32_batch_normalization_nchw_non_fused_brelu)}, |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 178 | {ActivationLayerInfo::ActivationFunction::LU_BOUNDED_RELU, |
Pablo Marquez Tello | 8d4cdd4 | 2023-11-21 10:10:01 +0000 | [diff] [blame] | 179 | REGISTER_FP32_NEON(cpu::fp32_batch_normalization_nchw_non_fused_lubrelu)}}; |
| 180 | |
Georgios Pinitas | aaba4c6 | 2018-08-22 16:20:21 +0100 | [diff] [blame] | 181 | // NCHW Fused Batched Normalization with activation functions : FP16 |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 182 | static std::map<ActivationLayerInfo::ActivationFunction, BatchNormFunctionPtr> bn_fused_map_f16_nchw = { |
| 183 | {ActivationLayerInfo::ActivationFunction::RELU, |
Pablo Marquez Tello | 8d4cdd4 | 2023-11-21 10:10:01 +0000 | [diff] [blame] | 184 | REGISTER_FP16_NEON(cpu::fp16_batch_normalization_nchw_non_fused_relu)}, |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 185 | {ActivationLayerInfo::ActivationFunction::BOUNDED_RELU, |
Pablo Marquez Tello | 8d4cdd4 | 2023-11-21 10:10:01 +0000 | [diff] [blame] | 186 | REGISTER_FP16_NEON(cpu::fp16_batch_normalization_nchw_non_fused_brelu)}, |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 187 | {ActivationLayerInfo::ActivationFunction::LU_BOUNDED_RELU, |
Pablo Marquez Tello | 8d4cdd4 | 2023-11-21 10:10:01 +0000 | [diff] [blame] | 188 | REGISTER_FP16_NEON(cpu::fp16_batch_normalization_nchw_non_fused_lubrelu)}}; |
Georgios Pinitas | 57c033b | 2018-02-15 12:29:44 +0000 | [diff] [blame] | 189 | |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 190 | switch (_input->info()->data_type()) |
Georgios Pinitas | 57c033b | 2018-02-15 12:29:44 +0000 | [diff] [blame] | 191 | { |
Georgios Pinitas | aaba4c6 | 2018-08-22 16:20:21 +0100 | [diff] [blame] | 192 | case DataType::F16: |
Sheri Zhang | 8d5d78b | 2020-12-15 20:25:31 +0000 | [diff] [blame] | 193 | _func = bn_fused_map_f16_nchw[_act_info.activation()]; |
Georgios Pinitas | aaba4c6 | 2018-08-22 16:20:21 +0100 | [diff] [blame] | 194 | break; |
Georgios Pinitas | 57c033b | 2018-02-15 12:29:44 +0000 | [diff] [blame] | 195 | case DataType::F32: |
Sheri Zhang | 8d5d78b | 2020-12-15 20:25:31 +0000 | [diff] [blame] | 196 | _func = bn_fused_map_f32_nchw[_act_info.activation()]; |
Georgios Pinitas | 57c033b | 2018-02-15 12:29:44 +0000 | [diff] [blame] | 197 | break; |
| 198 | default: |
| 199 | ARM_COMPUTE_ERROR("Element size not supported"); |
| 200 | break; |
| 201 | } |
| 202 | } |
Ioan-Cristian Szabo | 303be90 | 2017-11-27 16:31:10 +0000 | [diff] [blame] | 203 | |
| 204 | NEBatchNormalizationLayerKernel::NEBatchNormalizationLayerKernel() |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 205 | : _func(nullptr), |
| 206 | _input(nullptr), |
| 207 | _output(nullptr), |
| 208 | _mean(nullptr), |
| 209 | _var(nullptr), |
| 210 | _gamma(nullptr), |
| 211 | _beta(nullptr), |
| 212 | _epsilon(), |
| 213 | _act_info() |
Ioan-Cristian Szabo | 303be90 | 2017-11-27 16:31:10 +0000 | [diff] [blame] | 214 | { |
| 215 | } |
Pablo Tello | 8fda1cb | 2017-07-05 15:20:38 +0100 | [diff] [blame] | 216 | |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 217 | void NEBatchNormalizationLayerKernel::configure(ITensor *input, |
| 218 | ITensor *output, |
| 219 | const ITensor *mean, |
| 220 | const ITensor *var, |
| 221 | const ITensor *beta, |
| 222 | const ITensor *gamma, |
| 223 | float epsilon, |
| 224 | ActivationLayerInfo act_info) |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 225 | { |
Michele Di Giorgio | 4d33630 | 2018-03-02 09:43:54 +0000 | [diff] [blame] | 226 | ARM_COMPUTE_ERROR_ON_NULLPTR(input, mean, var); |
Ioan-Cristian Szabo | 303be90 | 2017-11-27 16:31:10 +0000 | [diff] [blame] | 227 | |
Michele Di Giorgio | 4d33630 | 2018-03-02 09:43:54 +0000 | [diff] [blame] | 228 | ARM_COMPUTE_ERROR_THROW_ON(validate_arguments(input->info(), (output != nullptr) ? output->info() : nullptr, |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 229 | mean->info(), var->info(), (beta != nullptr) ? beta->info() : nullptr, |
| 230 | (gamma != nullptr) ? gamma->info() : nullptr, epsilon, act_info)); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 231 | |
Georgios Pinitas | 57c033b | 2018-02-15 12:29:44 +0000 | [diff] [blame] | 232 | _input = input; |
| 233 | _output = input; |
| 234 | _mean = mean; |
| 235 | _var = var; |
| 236 | _gamma = gamma; |
| 237 | _beta = beta; |
| 238 | _epsilon = epsilon; |
| 239 | _act_info = act_info; |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 240 | |
Michele Di Giorgio | 4d33630 | 2018-03-02 09:43:54 +0000 | [diff] [blame] | 241 | const bool run_in_place = (output == nullptr) || (output == input); |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 242 | if (!run_in_place) |
Georgios Pinitas | 409ee0a | 2017-08-18 10:16:09 +0100 | [diff] [blame] | 243 | { |
Georgios Pinitas | 409ee0a | 2017-08-18 10:16:09 +0100 | [diff] [blame] | 244 | _output = output; |
| 245 | } |
| 246 | |
Georgios Pinitas | 57c033b | 2018-02-15 12:29:44 +0000 | [diff] [blame] | 247 | // Configure activation function to run |
Sheri Zhang | 8d5d78b | 2020-12-15 20:25:31 +0000 | [diff] [blame] | 248 | const bool is_nchw = _input->info()->data_layout() == DataLayout::NCHW; |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 249 | if (is_nchw) |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 250 | { |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 251 | if (_act_info.enabled()) |
Sheri Zhang | 8d5d78b | 2020-12-15 20:25:31 +0000 | [diff] [blame] | 252 | { |
| 253 | configure_fused(); |
| 254 | } |
| 255 | else |
| 256 | { |
| 257 | configure_non_fused(); |
| 258 | } |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 259 | } |
| 260 | |
Ioan-Cristian Szabo | 303be90 | 2017-11-27 16:31:10 +0000 | [diff] [blame] | 261 | // Configure kernel window |
Sheri Zhang | 8d5d78b | 2020-12-15 20:25:31 +0000 | [diff] [blame] | 262 | Window win = calculate_max_window(*input->info(), Steps()); |
| 263 | INEKernel::configure(win); |
| 264 | |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 265 | if (output != nullptr) |
Sheri Zhang | 8d5d78b | 2020-12-15 20:25:31 +0000 | [diff] [blame] | 266 | { |
| 267 | // Output auto initialization if not yet initialized |
| 268 | auto_init_if_empty(*output->info(), *input->info()->clone()); |
Sheri Zhang | 8d5d78b | 2020-12-15 20:25:31 +0000 | [diff] [blame] | 269 | } |
Ioan-Cristian Szabo | 303be90 | 2017-11-27 16:31:10 +0000 | [diff] [blame] | 270 | } |
| 271 | |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 272 | Status NEBatchNormalizationLayerKernel::validate(const ITensorInfo *input, |
| 273 | const ITensorInfo *output, |
| 274 | const ITensorInfo *mean, |
| 275 | const ITensorInfo *var, |
| 276 | const ITensorInfo *beta, |
| 277 | const ITensorInfo *gamma, |
| 278 | float epsilon, |
| 279 | ActivationLayerInfo act_info) |
Ioan-Cristian Szabo | 303be90 | 2017-11-27 16:31:10 +0000 | [diff] [blame] | 280 | { |
Georgios Pinitas | 57c033b | 2018-02-15 12:29:44 +0000 | [diff] [blame] | 281 | 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] | 282 | |
Georgios Pinitas | 631c41a | 2017-12-06 11:53:03 +0000 | [diff] [blame] | 283 | return Status{}; |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 284 | } |
| 285 | |
Moritz Pflanzer | c186b57 | 2017-09-07 09:48:04 +0100 | [diff] [blame] | 286 | void NEBatchNormalizationLayerKernel::run(const Window &window, const ThreadInfo &info) |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 287 | { |
Moritz Pflanzer | c186b57 | 2017-09-07 09:48:04 +0100 | [diff] [blame] | 288 | ARM_COMPUTE_UNUSED(info); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 289 | ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this); |
| 290 | ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(INEKernel::window(), window); |
Sheri Zhang | 8d5d78b | 2020-12-15 20:25:31 +0000 | [diff] [blame] | 291 | ARM_COMPUTE_ERROR_ON(_func == nullptr && _input->info()->data_layout() == DataLayout::NCHW); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 292 | |
Sheri Zhang | 8d5d78b | 2020-12-15 20:25:31 +0000 | [diff] [blame] | 293 | const bool is_nchw = _input->info()->data_layout() == DataLayout::NCHW; |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 294 | if (is_nchw) |
Sheri Zhang | 8d5d78b | 2020-12-15 20:25:31 +0000 | [diff] [blame] | 295 | { |
Pablo Marquez Tello | 8d4cdd4 | 2023-11-21 10:10:01 +0000 | [diff] [blame] | 296 | (*_func)(window, _input, _output, _mean, _var, _beta, _gamma, _epsilon, _act_info); |
Sheri Zhang | 8d5d78b | 2020-12-15 20:25:31 +0000 | [diff] [blame] | 297 | } |
| 298 | else |
| 299 | { |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 300 | const auto *uk = |
| 301 | get_implementation(BatchNormalizationSelectorData{_input->info()->data_type(), CPUInfo::get()}); |
Sheri Zhang | 8d5d78b | 2020-12-15 20:25:31 +0000 | [diff] [blame] | 302 | uk->ukernel(_input, _output, _mean, _var, _beta, _gamma, _epsilon, _act_info, window); |
| 303 | } |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 304 | } |
Georgios Pinitas | 980a916 | 2020-06-03 20:16:46 +0100 | [diff] [blame] | 305 | } // namespace arm_compute |