blob: 108b199df717a936c92420b556675a57d3712112 [file] [log] [blame]
Anthony Barbier6ff3b192017-09-04 18:44:23 +01001/*
Sheri Zhang97b3f112021-01-04 17:14:23 +00002 * Copyright (c) 2017-2021 Arm Limited.
Anthony Barbier6ff3b192017-09-04 18:44:23 +01003 *
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 Spyrouebcebf12020-10-21 00:04:14 +010024#include "src/core/NEON/kernels/NEBatchNormalizationLayerKernel.h"
Anthony Barbier6ff3b192017-09-04 18:44:23 +010025
26#include "arm_compute/core/Helpers.h"
Anthony Barbier6ff3b192017-09-04 18:44:23 +010027#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 Park68dd25f2020-10-19 16:00:11 +010031#include "src/core/CPP/Validate.h"
Georgios Pinitasddb93bb2020-10-02 16:38:59 +010032#include "src/core/NEON/NEFixedPoint.h"
33#include "src/core/NEON/NEMath.h"
Sang-Hoon Park68dd25f2020-10-19 16:00:11 +010034#include "src/core/helpers/AutoConfiguration.h"
35#include "src/core/helpers/WindowHelpers.h"
Anthony Barbier6ff3b192017-09-04 18:44:23 +010036
Georgios Pinitasddb93bb2020-10-02 16:38:59 +010037#include "src/core/NEON/kernels/detail/NEActivationFunctionDetail.h"
38#include "src/core/NEON/wrapper/wrapper.h"
Georgios Pinitas980a9162020-06-03 20:16:46 +010039
Sheri Zhang8d5d78b2020-12-15 20:25:31 +000040#include "src/core/NEON/kernels/batchnormalization/impl/list.h"
41#include "src/core/common/Registrars.h"
42
Georgios Pinitas57c033b2018-02-15 12:29:44 +000043#include <map>
44
Georgios Pinitas980a9162020-06-03 20:16:46 +010045namespace arm_compute
46{
Ioan-Cristian Szabo303be902017-11-27 16:31:10 +000047namespace
Anthony Barbier6ff3b192017-09-04 18:44:23 +010048{
Sheri Zhang8d5d78b2020-12-15 20:25:31 +000049struct BatchNormalizationSelectorData
50{
Michalis Spyrou20fca522021-06-07 14:23:57 +010051 DataType dt;
52 const CPUInfo &ci;
Sheri Zhang8d5d78b2020-12-15 20:25:31 +000053};
54using BatchNormalizationSelectorPtr = std::add_pointer<bool(const BatchNormalizationSelectorData &data)>::type;
55using BatchNormalizationKernelPtr = std::add_pointer<void(ITensor *, ITensor *, const ITensor *, const ITensor *, const ITensor *, const ITensor *,
56 float, ActivationLayerInfo &, const Window &)>::type;
57
58struct BatchNormalizationKernel
59{
60 const char *name;
61 const BatchNormalizationSelectorPtr is_selected;
62 BatchNormalizationKernelPtr ukernel;
63};
64
65static const BatchNormalizationKernel available_kernels[] =
66{
Michalis Spyrou20fca522021-06-07 14:23:57 +010067#if defined(ARM_COMPUTE_ENABLE_SVE)
Sheri Zhang8d5d78b2020-12-15 20:25:31 +000068 {
Georgios Pinitas5fdde992021-06-25 05:42:57 +010069 "sve_fp16_batch_normalization",
Michalis Spyrou20fca522021-06-07 14:23:57 +010070 [](const BatchNormalizationSelectorData & data) { return data.dt == DataType::F16 && data.ci.has_sve(); },
Sheri Zhang8d5d78b2020-12-15 20:25:31 +000071 REGISTER_FP16_SVE(arm_compute::cpu::fp16_sve_batch_normalization)
72 },
73 {
Georgios Pinitas5fdde992021-06-25 05:42:57 +010074 "sve_fp32_batch_normalization",
Michalis Spyrou20fca522021-06-07 14:23:57 +010075 [](const BatchNormalizationSelectorData & data) { return data.dt == DataType::F32 && data.ci.has_sve(); },
Sheri Zhang8d5d78b2020-12-15 20:25:31 +000076 REGISTER_FP32_SVE(arm_compute::cpu::fp32_sve_batch_normalization)
77 },
Michalis Spyrou20fca522021-06-07 14:23:57 +010078#endif /* !defined(ARM_COMPUTE_ENABLE_SVE) */
79#if defined(ARM_COMPUTE_ENABLE_NEON)
Sheri Zhang97b3f112021-01-04 17:14:23 +000080#if defined(__ARM_FEATURE_FP16_VECTOR_ARITHMETIC)
Sheri Zhang8d5d78b2020-12-15 20:25:31 +000081 {
Georgios Pinitas5fdde992021-06-25 05:42:57 +010082 "neon_fp16_batch_normalization",
Sheri Zhang8d5d78b2020-12-15 20:25:31 +000083 [](const BatchNormalizationSelectorData & data) { return data.dt == DataType::F16; },
84 REGISTER_FP16_NEON(arm_compute::cpu::fp16_neon_batch_normalization)
85 },
Sheri Zhang97b3f112021-01-04 17:14:23 +000086#endif /* __ARM_FEATURE_FP16_VECTOR_ARITHMETIC */
Sheri Zhang8d5d78b2020-12-15 20:25:31 +000087 {
Georgios Pinitas5fdde992021-06-25 05:42:57 +010088 "neon_fp32_batch_normalization",
Sheri Zhang8d5d78b2020-12-15 20:25:31 +000089 [](const BatchNormalizationSelectorData & data) { return data.dt == DataType::F32; },
90 REGISTER_FP32_NEON(arm_compute::cpu::fp32_neon_batch_normalization)
91 },
Michalis Spyrou20fca522021-06-07 14:23:57 +010092#endif /* !defined(ARM_COMPUTE_ENABLE_NEON) */
Sheri Zhang8d5d78b2020-12-15 20:25:31 +000093};
94
95const 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 Pinitas57c033b2018-02-15 12:29:44 +0000107Status
108validate_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 Szabo303be902017-11-27 16:31:10 +0000110{
111 ARM_COMPUTE_UNUSED(epsilon);
Sheri Zhang8d5d78b2020-12-15 20:25:31 +0000112
Michalis Spyrou20fca522021-06-07 14:23:57 +0100113 const auto *uk = get_implementation(BatchNormalizationSelectorData{ input->data_type(), CPUInfo::get() });
Sheri Zhang8d5d78b2020-12-15 20:25:31 +0000114 ARM_COMPUTE_RETURN_ERROR_ON(uk == nullptr || uk->ukernel == nullptr);
Georgios Pinitas57c033b2018-02-15 12:29:44 +0000115
116 if(act_info.enabled())
117 {
118 ActivationLayerInfo::ActivationFunction act = act_info.activation();
Georgios Pinitas6f109bd2018-07-16 12:57:42 +0100119 ARM_COMPUTE_RETURN_ERROR_ON(act != ActivationLayerInfo::ActivationLayerInfo::ActivationFunction::RELU
120 && act != ActivationLayerInfo::ActivationLayerInfo::ActivationFunction::BOUNDED_RELU
Georgios Pinitas57c033b2018-02-15 12:29:44 +0000121 && act != ActivationLayerInfo::ActivationLayerInfo::ActivationFunction::LU_BOUNDED_RELU);
122 ARM_COMPUTE_RETURN_ERROR_ON(act_info.b() > act_info.a());
123 }
Ioan-Cristian Szabo303be902017-11-27 16:31:10 +0000124
125 if(nullptr != output)
126 {
127 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_SHAPES(input, output);
Michele Di Giorgio0cbb9272018-03-01 16:56:48 +0000128 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_LAYOUT(input, output);
Ioan-Cristian Szabo303be902017-11-27 16:31:10 +0000129 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input, output);
Ioan-Cristian Szabo303be902017-11-27 16:31:10 +0000130 }
131
Michele Di Giorgio4d336302018-03-02 09:43:54 +0000132 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input, mean, var);
Michele Di Giorgio4d336302018-03-02 09:43:54 +0000133 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 Giorgio4d336302018-03-02 09:43:54 +0000137 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 Giorgio4d336302018-03-02 09:43:54 +0000142 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_SHAPES(mean, gamma);
143 }
Michele Di Giorgio0cbb9272018-03-01 16:56:48 +0000144 ARM_COMPUTE_RETURN_ERROR_ON(input->dimension(get_data_layout_dimension_index(input->data_layout(), DataLayoutDimension::CHANNEL)) != mean->dimension(0));
Ioan-Cristian Szabo303be902017-11-27 16:31:10 +0000145
Georgios Pinitas631c41a2017-12-06 11:53:03 +0000146 return Status{};
Ioan-Cristian Szabo303be902017-11-27 16:31:10 +0000147}
Georgios Pinitas57c033b2018-02-15 12:29:44 +0000148} //namespace
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100149
Georgios Pinitas980a9162020-06-03 20:16:46 +0100150template <typename T, bool fused_activation, typename F>
151void NEBatchNormalizationLayerKernel::batch_normalization_nchw(const Window &window)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100152{
Michele Di Giorgio33f41fa2021-03-09 14:09:08 +0000153 /** SIMD vector tag type. */
Georgios Pinitas980a9162020-06-03 20:16:46 +0100154 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 Tello8fda1cb2017-07-05 15:20:38 +0100165
Georgios Pinitasaaba4c62018-08-22 16:20:21 +0100166 F activation_functor(_act_info);
167
Pablo Tello8fda1cb2017-07-05 15:20:38 +0100168 // Hold information about the current feature map we are iterating.
Michele Di Giorgio33f41fa2021-03-09 14:09:08 +0000169 // Only compute denominator and constants once per feature map.
Pablo Tello8fda1cb2017-07-05 15:20:38 +0100170 int slice = -1;
171
Georgios Pinitas980a9162020-06-03 20:16:46 +0100172 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 Tello8fda1cb2017-07-05 15:20:38 +0100176
Georgios Pinitas980a9162020-06-03 20:16:46 +0100177 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 Tello8fda1cb2017-07-05 15:20:38 +0100190 {
Georgios Pinitas980a9162020-06-03 20:16:46 +0100191 const auto input_ptr = reinterpret_cast<const T *>(input.ptr());
192 const auto output_ptr = reinterpret_cast<T *>(output.ptr());
193
Pablo Tello8fda1cb2017-07-05 15:20:38 +0100194 if(slice != id.z())
195 {
Georgios Pinitas980a9162020-06-03 20:16:46 +0100196 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 Giorgio4d336302018-03-02 09:43:54 +0000200 if(input_gamma != nullptr)
201 {
Georgios Pinitas980a9162020-06-03 20:16:46 +0100202 gamma = input_gamma[id.z()];
203 gamma_vec = wrapper::vdup_n(gamma, ExactTagType{});
Michele Di Giorgio4d336302018-03-02 09:43:54 +0000204 }
205 if(input_beta != nullptr)
206 {
Georgios Pinitas980a9162020-06-03 20:16:46 +0100207 beta = input_beta[id.z()];
208 beta_vec = wrapper::vdup_n(beta, ExactTagType{});
Michele Di Giorgio4d336302018-03-02 09:43:54 +0000209 }
Pablo Tello8fda1cb2017-07-05 15:20:38 +0100210
211 // Calculate denominator
Georgios Pinitas980a9162020-06-03 20:16:46 +0100212 denominator_vec = wrapper::vinvsqrt(wrapper::vadd(var_vec, epsilon_vec));
213 denominator = wrapper::vgetlane(denominator_vec, 0);
214 slice = id.z();
Pablo Tello8fda1cb2017-07-05 15:20:38 +0100215 }
216
Georgios Pinitas980a9162020-06-03 20:16:46 +0100217 // 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 Pinitasaaba4c62018-08-22 16:20:21 +0100220 {
Georgios Pinitas980a9162020-06-03 20:16:46 +0100221 // 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 Pinitasaaba4c62018-08-22 16:20:21 +0100234 }
235
Georgios Pinitas980a9162020-06-03 20:16:46 +0100236 // 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 Tello8fda1cb2017-07-05 15:20:38 +0100252 },
253 input, output);
Georgios Pinitas57c033b2018-02-15 12:29:44 +0000254}
255
Georgios Pinitas57c033b2018-02-15 12:29:44 +0000256void NEBatchNormalizationLayerKernel::configure_non_fused()
257{
258 switch(_input->info()->data_type())
259 {
Georgios Pinitasaaba4c62018-08-22 16:20:21 +0100260#ifdef __ARM_FEATURE_FP16_VECTOR_ARITHMETIC
Georgios Pinitas57c033b2018-02-15 12:29:44 +0000261 case DataType::F16:
Sheri Zhang8d5d78b2020-12-15 20:25:31 +0000262 _func = &NEBatchNormalizationLayerKernel::batch_normalization_nchw<float16_t, false, detail::dummy<float16_t, 8>>;
Georgios Pinitas57c033b2018-02-15 12:29:44 +0000263 break;
Georgios Pinitasaaba4c62018-08-22 16:20:21 +0100264#endif // __ARM_FEATURE_FP16_VECTOR_ARITHMETIC
Georgios Pinitas57c033b2018-02-15 12:29:44 +0000265 case DataType::F32:
Sheri Zhang8d5d78b2020-12-15 20:25:31 +0000266 _func = &NEBatchNormalizationLayerKernel::batch_normalization_nchw<float, false, detail::dummy<float, 4>>;
Georgios Pinitas57c033b2018-02-15 12:29:44 +0000267 break;
268 default:
269 ARM_COMPUTE_ERROR("Element size not supported");
270 break;
271 }
272}
273
274void NEBatchNormalizationLayerKernel::configure_fused()
275{
Michele Di Giorgio0cbb9272018-03-01 16:56:48 +0000276 // NCHW Fused Batched Normalization with activation functions : FP32
277 static std::map<ActivationLayerInfo::ActivationFunction, BatchNormFunctionPtr> bn_fused_map_f32_nchw =
Georgios Pinitas57c033b2018-02-15 12:29:44 +0000278 {
Georgios Pinitas980a9162020-06-03 20:16:46 +0100279 { 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 Giorgio0cbb9272018-03-01 16:56:48 +0000282 };
Georgios Pinitasaaba4c62018-08-22 16:20:21 +0100283#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 Pinitas980a9162020-06-03 20:16:46 +0100287 { 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 Pinitasaaba4c62018-08-22 16:20:21 +0100290 };
Georgios Pinitasaaba4c62018-08-22 16:20:21 +0100291#endif // __ARM_FEATURE_FP16_VECTOR_ARITHMETIC
Georgios Pinitas57c033b2018-02-15 12:29:44 +0000292
293 switch(_input->info()->data_type())
294 {
Georgios Pinitasaaba4c62018-08-22 16:20:21 +0100295#ifdef __ARM_FEATURE_FP16_VECTOR_ARITHMETIC
296 case DataType::F16:
Sheri Zhang8d5d78b2020-12-15 20:25:31 +0000297 _func = bn_fused_map_f16_nchw[_act_info.activation()];
Georgios Pinitasaaba4c62018-08-22 16:20:21 +0100298 break;
299#endif // __ARM_FEATURE_FP16_VECTOR_ARITHMETIC
Georgios Pinitas57c033b2018-02-15 12:29:44 +0000300 case DataType::F32:
Sheri Zhang8d5d78b2020-12-15 20:25:31 +0000301 _func = bn_fused_map_f32_nchw[_act_info.activation()];
Georgios Pinitas57c033b2018-02-15 12:29:44 +0000302 break;
303 default:
304 ARM_COMPUTE_ERROR("Element size not supported");
305 break;
306 }
307}
Ioan-Cristian Szabo303be902017-11-27 16:31:10 +0000308
309NEBatchNormalizationLayerKernel::NEBatchNormalizationLayerKernel()
Georgios Pinitas57c033b2018-02-15 12:29:44 +0000310 : _func(nullptr), _input(nullptr), _output(nullptr), _mean(nullptr), _var(nullptr), _gamma(nullptr), _beta(nullptr), _epsilon(), _act_info()
Ioan-Cristian Szabo303be902017-11-27 16:31:10 +0000311{
312}
Pablo Tello8fda1cb2017-07-05 15:20:38 +0100313
Georgios Pinitas57c033b2018-02-15 12:29:44 +0000314void 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 Barbier6ff3b192017-09-04 18:44:23 +0100318{
Michele Di Giorgio4d336302018-03-02 09:43:54 +0000319 ARM_COMPUTE_ERROR_ON_NULLPTR(input, mean, var);
Ioan-Cristian Szabo303be902017-11-27 16:31:10 +0000320
Michele Di Giorgio4d336302018-03-02 09:43:54 +0000321 ARM_COMPUTE_ERROR_THROW_ON(validate_arguments(input->info(), (output != nullptr) ? output->info() : nullptr,
Georgios Pinitas57c033b2018-02-15 12:29:44 +0000322 mean->info(), var->info(),
Michele Di Giorgio4d336302018-03-02 09:43:54 +0000323 (beta != nullptr) ? beta->info() : nullptr,
324 (gamma != nullptr) ? gamma->info() : nullptr,
Georgios Pinitas57c033b2018-02-15 12:29:44 +0000325 epsilon, act_info));
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100326
Georgios Pinitas57c033b2018-02-15 12:29:44 +0000327 _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 Barbier6ff3b192017-09-04 18:44:23 +0100335
Michele Di Giorgio4d336302018-03-02 09:43:54 +0000336 const bool run_in_place = (output == nullptr) || (output == input);
337 if(!run_in_place)
Georgios Pinitas409ee0a2017-08-18 10:16:09 +0100338 {
Georgios Pinitas409ee0a2017-08-18 10:16:09 +0100339 _output = output;
340 }
341
Georgios Pinitas57c033b2018-02-15 12:29:44 +0000342 // Configure activation function to run
Sheri Zhang8d5d78b2020-12-15 20:25:31 +0000343 const bool is_nchw = _input->info()->data_layout() == DataLayout::NCHW;
344 if(is_nchw)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100345 {
Sheri Zhang8d5d78b2020-12-15 20:25:31 +0000346 if(_act_info.enabled())
347 {
348 configure_fused();
349 }
350 else
351 {
352 configure_non_fused();
353 }
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100354 }
355
Ioan-Cristian Szabo303be902017-11-27 16:31:10 +0000356 // Configure kernel window
Sheri Zhang8d5d78b2020-12-15 20:25:31 +0000357 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 Zhang8d5d78b2020-12-15 20:25:31 +0000364 }
Ioan-Cristian Szabo303be902017-11-27 16:31:10 +0000365}
366
Georgios Pinitas57c033b2018-02-15 12:29:44 +0000367Status 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 Szabo303be902017-11-27 16:31:10 +0000371{
Georgios Pinitas57c033b2018-02-15 12:29:44 +0000372 ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments(input, output, mean, var, beta, gamma, epsilon, act_info));
Ioan-Cristian Szabo303be902017-11-27 16:31:10 +0000373
Georgios Pinitas631c41a2017-12-06 11:53:03 +0000374 return Status{};
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100375}
376
Moritz Pflanzerc186b572017-09-07 09:48:04 +0100377void NEBatchNormalizationLayerKernel::run(const Window &window, const ThreadInfo &info)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100378{
Moritz Pflanzerc186b572017-09-07 09:48:04 +0100379 ARM_COMPUTE_UNUSED(info);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100380 ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this);
381 ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(INEKernel::window(), window);
Sheri Zhang8d5d78b2020-12-15 20:25:31 +0000382 ARM_COMPUTE_ERROR_ON(_func == nullptr && _input->info()->data_layout() == DataLayout::NCHW);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100383
Sheri Zhang8d5d78b2020-12-15 20:25:31 +0000384 const bool is_nchw = _input->info()->data_layout() == DataLayout::NCHW;
385 if(is_nchw)
386 {
387 (this->*_func)(window);
388 }
389 else
390 {
Michalis Spyrou20fca522021-06-07 14:23:57 +0100391 const auto *uk = get_implementation(BatchNormalizationSelectorData{ _input->info()->data_type(), CPUInfo::get() });
Sheri Zhang8d5d78b2020-12-15 20:25:31 +0000392 uk->ukernel(_input, _output, _mean, _var, _beta, _gamma, _epsilon, _act_info, window);
393 }
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100394}
Georgios Pinitas980a9162020-06-03 20:16:46 +0100395} // namespace arm_compute