blob: 282b1a6f4d995de0fcec7f995244e6f51c6f4451 [file] [log] [blame]
giuros0115ecc9a2018-12-06 10:47:34 +00001/*
Michele Di Giorgiod9eaf612020-07-08 11:12:57 +01002 * Copyright (c) 2018-2020 Arm Limited.
giuros0115ecc9a2018-12-06 10:47:34 +00003 *
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
Michalis Spyrouaeebe4a2019-01-09 14:21:03 +000017 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
giuros0115ecc9a2018-12-06 10:47:34 +000018 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
Michalis Spyrouaeebe4a2019-01-09 14:21:03 +000019 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
giuros0115ecc9a2018-12-06 10:47:34 +000020 * 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/NEFuseBatchNormalizationKernel.h"
25
Georgios Pinitas8f5802f2019-02-22 11:08:32 +000026#include "arm_compute/core/CPP/Validate.h"
giuros0115ecc9a2018-12-06 10:47:34 +000027#include "arm_compute/core/Helpers.h"
28#include "arm_compute/core/ITensor.h"
Manuel Bottini11091762019-06-17 12:04:40 +010029#include "arm_compute/core/NEON/wrapper/wrapper.h"
giuros0115ecc9a2018-12-06 10:47:34 +000030#include "arm_compute/core/TensorInfo.h"
Georgios Pinitas8f5802f2019-02-22 11:08:32 +000031#include "arm_compute/core/Utils.h"
32#include "arm_compute/core/Validate.h"
33#include "arm_compute/core/Window.h"
giuros0115ecc9a2018-12-06 10:47:34 +000034
Manuel Bottini11091762019-06-17 12:04:40 +010035#include <map>
36
giuros0115ecc9a2018-12-06 10:47:34 +000037namespace arm_compute
38{
39namespace
40{
Manuel Bottini11091762019-06-17 12:04:40 +010041Status validate_arguments(const ITensorInfo *input_weights, const ITensorInfo *bn_mean, const ITensorInfo *bn_var,
giuros0115ecc9a2018-12-06 10:47:34 +000042 const ITensorInfo *fused_weights, const ITensorInfo *fused_bias,
Manuel Bottini11091762019-06-17 12:04:40 +010043 const ITensorInfo *input_bias, const ITensorInfo *bn_beta, const ITensorInfo *bn_gamma,
44 float epsilon, FuseBatchNormalizationType fbn_type)
giuros0115ecc9a2018-12-06 10:47:34 +000045{
46 ARM_COMPUTE_UNUSED(epsilon);
Manuel Bottini11091762019-06-17 12:04:40 +010047 ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(input_weights, bn_mean, bn_var);
48 ARM_COMPUTE_RETURN_ERROR_ON_CPU_F16_UNSUPPORTED(input_weights);
49 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input_weights, 1, DataType::F16, DataType::F32);
giuros0115ecc9a2018-12-06 10:47:34 +000050 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_SHAPES(bn_mean, bn_var);
Manuel Bottini11091762019-06-17 12:04:40 +010051 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input_weights, bn_mean, bn_var);
52 ARM_COMPUTE_RETURN_ERROR_ON(input_bias == nullptr && fused_bias == nullptr);
53 ARM_COMPUTE_RETURN_ERROR_ON(bn_mean->num_dimensions() > 1);
giuros0115ecc9a2018-12-06 10:47:34 +000054
Manuel Bottini11091762019-06-17 12:04:40 +010055 if(fbn_type == FuseBatchNormalizationType::CONVOLUTION)
giuros0115ecc9a2018-12-06 10:47:34 +000056 {
Manuel Bottini11091762019-06-17 12:04:40 +010057 ARM_COMPUTE_RETURN_ERROR_ON(input_weights->dimension(3) != bn_mean->dimension(0));
58 }
59 else
60 {
61 const size_t channel_idx = get_data_layout_dimension_index(input_weights->data_layout(), DataLayoutDimension::CHANNEL);
62 ARM_COMPUTE_RETURN_ERROR_ON(input_weights->dimension(channel_idx) != bn_mean->dimension(0));
63 }
64 // Validate bias
65 if(input_bias != nullptr)
66 {
67 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_SHAPES(bn_mean, input_bias);
68 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input_weights, input_bias);
giuros0115ecc9a2018-12-06 10:47:34 +000069 }
70 // Validate beta
71 if(bn_beta != nullptr)
72 {
73 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_SHAPES(bn_mean, bn_beta);
Manuel Bottini11091762019-06-17 12:04:40 +010074 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input_weights, bn_beta);
giuros0115ecc9a2018-12-06 10:47:34 +000075 }
76 // Validate gamma
77 if(bn_gamma != nullptr)
78 {
79 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_SHAPES(bn_mean, bn_gamma);
Manuel Bottini11091762019-06-17 12:04:40 +010080 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input_weights, bn_gamma);
giuros0115ecc9a2018-12-06 10:47:34 +000081 }
82
83 // Validate output weights
84 if(fused_weights != nullptr && fused_weights->total_size() != 0)
85 {
Manuel Bottini11091762019-06-17 12:04:40 +010086 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_SHAPES(input_weights, fused_weights);
87 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_LAYOUT(input_weights, fused_weights);
88 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input_weights, fused_weights);
giuros0115ecc9a2018-12-06 10:47:34 +000089 }
90 // Validate output bias
91 if(fused_bias != nullptr && fused_bias->total_size() != 0)
92 {
93 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_SHAPES(bn_mean, fused_bias);
Manuel Bottini11091762019-06-17 12:04:40 +010094 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input_weights, fused_bias);
giuros0115ecc9a2018-12-06 10:47:34 +000095 }
96
97 return Status{};
98}
99
Manuel Bottini11091762019-06-17 12:04:40 +0100100template <typename VectorType>
101void fused_batch_normalization_conv(const ITensor *conv_weights, const ITensor *conv_bias, ITensor *fused_weights, ITensor *fused_bias,
102 const ITensor *bn_mean, const ITensor *bn_var, const ITensor *bn_beta, const ITensor *bn_gamma, float epsilon, const Window &window)
giuros0115ecc9a2018-12-06 10:47:34 +0000103{
Manuel Bottini11091762019-06-17 12:04:40 +0100104 using ScalarType = typename VectorType::scalar_type;
105 const int size = 16 / conv_weights->info()->element_size();
106 using ExactTagType = typename VectorType::tag_type;
giuros0115ecc9a2018-12-06 10:47:34 +0000107
108 const bool run_in_place_weights = (fused_weights == nullptr) || (fused_weights == conv_weights);
109 const bool run_in_place_bias = (fused_bias == nullptr) || (conv_bias != nullptr && fused_bias == conv_bias);
110
111 // Set build options
112 Window win = window;
113 win.set(Window::DimX, Window::Dimension(0, 1, 1));
114
115 const int window_step_x = size;
116 const auto window_start_x = static_cast<int>(window.x().start());
117 const auto window_end_x = static_cast<int>(window.x().end());
118
119 Iterator conv_w_in(conv_weights, win);
120 Iterator conv_w_out(run_in_place_weights ? conv_weights : fused_weights, win);
121
122 const auto conv_bias_in = (conv_bias != nullptr ? reinterpret_cast<ScalarType *>(conv_bias->ptr_to_element(Coordinates(0, 0))) : nullptr);
123 auto conv_bias_out = (run_in_place_bias ? conv_bias_in : reinterpret_cast<ScalarType *>(fused_bias->ptr_to_element(Coordinates(0, 0))));
124
giuros0115ecc9a2018-12-06 10:47:34 +0000125 const auto input_mean = reinterpret_cast<const ScalarType *>(bn_mean->ptr_to_element(Coordinates(0, 0)));
126 const auto input_var = reinterpret_cast<const ScalarType *>(bn_var->ptr_to_element(Coordinates(0, 0)));
127 const auto input_gamma = (bn_gamma != nullptr) ? reinterpret_cast<const ScalarType *>(bn_gamma->ptr_to_element(Coordinates(0, 0))) : nullptr;
128 const auto input_beta = (bn_beta != nullptr) ? reinterpret_cast<const ScalarType *>(bn_beta->ptr_to_element(Coordinates(0, 0))) : nullptr;
129
130 auto mean_vec = wrapper::vdup_n(ScalarType(0), ExactTagType{});
131 auto var_vec = wrapper::vdup_n(ScalarType(0), ExactTagType{});
132 auto gamma_vec = wrapper::vdup_n(ScalarType(1), ExactTagType{});
133 auto beta_vec = wrapper::vdup_n(ScalarType(0), ExactTagType{});
134 auto rvar_vec = wrapper::vdup_n(ScalarType(0), ExactTagType{});
135 const auto epsilon_vec = wrapper::vdup_n(ScalarType(epsilon), ExactTagType{});
136
137 auto mean = ScalarType(0.0);
138 auto var = ScalarType(0.0);
139 auto gamma = ScalarType(1.0);
140 auto beta = ScalarType(0.0);
141 auto conv_bias_in_scalar = ScalarType(0.0);
142 execute_window_loop(win, [&](const Coordinates & id)
143 {
Manuel Bottini11091762019-06-17 12:04:40 +0100144 var = input_var[id[3]];
145 if(input_gamma != nullptr)
giuros0115ecc9a2018-12-06 10:47:34 +0000146 {
Manuel Bottini11091762019-06-17 12:04:40 +0100147 gamma = input_gamma[id[3]];
148 }
giuros0115ecc9a2018-12-06 10:47:34 +0000149
Manuel Bottini11091762019-06-17 12:04:40 +0100150 if((id[0] == 0) && (id[1] == 0) && (id[2] == 0))
151 {
giuros0115ecc9a2018-12-06 10:47:34 +0000152 if(input_beta != nullptr)
153 {
Manuel Bottini11091762019-06-17 12:04:40 +0100154 beta = input_beta[id[3]];
giuros0115ecc9a2018-12-06 10:47:34 +0000155 beta_vec = wrapper::vdup_n(beta, ExactTagType{});
156 }
Manuel Bottini11091762019-06-17 12:04:40 +0100157
158 // Construct vectors
159 mean = input_mean[id[3]];
160 mean_vec = wrapper::vdup_n(mean, ExactTagType{});
161
giuros0115ecc9a2018-12-06 10:47:34 +0000162 if(conv_bias_in != nullptr)
163 {
Manuel Bottini11091762019-06-17 12:04:40 +0100164 conv_bias_in_scalar = conv_bias_in[id[3]];
giuros0115ecc9a2018-12-06 10:47:34 +0000165 }
Manuel Bottini11091762019-06-17 12:04:40 +0100166 auto conv_bias_tmp_scalar = (conv_bias_in_scalar - mean) / std::sqrt(var + ScalarType(epsilon));
167 conv_bias_out[id[3]] = (conv_bias_tmp_scalar * gamma) + beta;
giuros0115ecc9a2018-12-06 10:47:34 +0000168 }
169
170 int x = window_start_x;
171 auto conv_w_in_ptr = reinterpret_cast<const ScalarType *>(conv_w_in.ptr());
172 auto conv_w_out_ptr = reinterpret_cast<ScalarType *>(conv_w_out.ptr());
Manuel Bottini11091762019-06-17 12:04:40 +0100173 var_vec = wrapper::vdup_n(var, ExactTagType{});
174 gamma_vec = wrapper::vdup_n(gamma, ExactTagType{});
175 rvar_vec = wrapper::vinvsqrt(wrapper::vadd(var_vec, epsilon_vec));
giuros0115ecc9a2018-12-06 10:47:34 +0000176
177 for(; x <= (window_end_x - window_step_x); x += window_step_x)
178 {
179 auto wn = wrapper::vloadq(conv_w_in_ptr + x);
180 wn = wrapper::vmul(wn, rvar_vec);
181 wn = wrapper::vmul(wn, gamma_vec);
182
183 // Store results
184 wrapper::vstore(conv_w_out_ptr + x, wn);
185 }
186
187 // Compute left-over elements
188 for(; x < window_end_x; ++x)
189 {
Manuel Bottini11091762019-06-17 12:04:40 +0100190 *(conv_w_out_ptr + x) = *(conv_w_in_ptr + x) / std::sqrt(var + ScalarType(epsilon)) * gamma;
giuros0115ecc9a2018-12-06 10:47:34 +0000191 }
192 },
193 conv_w_in, conv_w_out);
194}
Manuel Bottini11091762019-06-17 12:04:40 +0100195
196template <typename VectorType>
197void fused_batch_normalization_dwc_nhwc(const ITensor *dwc_weights, const ITensor *dwc_bias, ITensor *fused_weights, ITensor *fused_bias,
198 const ITensor *bn_mean, const ITensor *bn_var, const ITensor *bn_beta, const ITensor *bn_gamma, float epsilon, const Window &window)
199{
200 using ScalarType = typename VectorType::scalar_type;
201 const int size = 16 / dwc_weights->info()->element_size();
202 using ExactTagType = typename VectorType::tag_type;
203
204 const bool run_in_place_weights = (fused_weights == nullptr) || (fused_weights == dwc_weights);
205 const bool run_in_place_bias = (fused_bias == nullptr) || (dwc_bias != nullptr && fused_bias == dwc_bias);
206
207 // Set build options
208 Window win = window;
209 win.set(Window::DimX, Window::Dimension(0, 1, 1));
210
211 const int window_step_x = size;
212 const auto window_start_x = static_cast<int>(window.x().start());
213 const auto window_end_x = static_cast<int>(window.x().end());
214
215 Iterator dwc_w_in(dwc_weights, win);
216 Iterator dwc_w_out(run_in_place_weights ? dwc_weights : fused_weights, win);
217
218 const auto dwc_bias_in = (dwc_bias != nullptr ? reinterpret_cast<ScalarType *>(dwc_bias->ptr_to_element(Coordinates(0, 0))) : nullptr);
219 auto dwc_bias_out = (run_in_place_bias ? dwc_bias_in : reinterpret_cast<ScalarType *>(fused_bias->ptr_to_element(Coordinates(0, 0))));
220
221 const auto input_mean = reinterpret_cast<const ScalarType *>(bn_mean->ptr_to_element(Coordinates(0, 0)));
222 const auto input_var = reinterpret_cast<const ScalarType *>(bn_var->ptr_to_element(Coordinates(0, 0)));
223 const auto input_gamma = (bn_gamma != nullptr) ? reinterpret_cast<const ScalarType *>(bn_gamma->ptr_to_element(Coordinates(0, 0))) : nullptr;
224 const auto input_beta = (bn_beta != nullptr) ? reinterpret_cast<const ScalarType *>(bn_beta->ptr_to_element(Coordinates(0, 0))) : nullptr;
225
226 auto mean_vec = wrapper::vdup_n(ScalarType(0), ExactTagType{});
227 auto var_vec = wrapper::vdup_n(ScalarType(0), ExactTagType{});
228 auto gamma_vec = wrapper::vdup_n(ScalarType(1), ExactTagType{});
229 auto beta_vec = wrapper::vdup_n(ScalarType(0), ExactTagType{});
230 auto rvar_vec = wrapper::vdup_n(ScalarType(0), ExactTagType{});
231 auto dwc_bias_vec = wrapper::vdup_n(ScalarType(0), ExactTagType{});
232 const auto epsilon_vec = wrapper::vdup_n(ScalarType(epsilon), ExactTagType{});
233
234 auto gamma = ScalarType(1.0);
235 auto beta = ScalarType(0.0);
236 auto dwc_bias_in_scalar = ScalarType(0);
237
238 execute_window_loop(win, [&](const Coordinates & id)
239 {
240 int x = window_start_x;
241 for(; x <= (window_end_x - window_step_x); x += window_step_x)
242 {
243 var_vec = wrapper::vloadq(input_var + x);
244 if(input_gamma != nullptr)
245 {
246 gamma_vec = wrapper::vloadq(input_gamma + x);
247 }
248
249 if((id[2] == 0) && (id[1] == 0))
250 {
251 mean_vec = wrapper::vloadq(input_mean + x);
252
253 // Construct vectors
254 if(input_beta != nullptr)
255 {
256 beta_vec = wrapper::vloadq(input_beta + x);
257 }
258
259 if(dwc_bias_in != nullptr)
260 {
261 dwc_bias_vec = wrapper::vloadq(dwc_bias_in + x);
262 }
263
264 auto dwc_bias_tmp_vec = wrapper::vmul(wrapper::vsub(dwc_bias_vec, mean_vec), wrapper::vinvsqrt(wrapper::vadd(var_vec, epsilon_vec)));
265 dwc_bias_tmp_vec = wrapper::vadd(wrapper::vmul(dwc_bias_tmp_vec, gamma_vec), beta_vec);
266 wrapper::vstore(dwc_bias_out + x, dwc_bias_tmp_vec);
267 }
268
269 auto dwc_w_in_ptr = reinterpret_cast<const ScalarType *>(dwc_w_in.ptr());
270 auto dwc_w_out_ptr = reinterpret_cast<ScalarType *>(dwc_w_out.ptr());
271
272 auto wn = wrapper::vloadq(dwc_w_in_ptr + x);
273 rvar_vec = wrapper::vinvsqrt(wrapper::vadd(var_vec, epsilon_vec));
274 wn = wrapper::vmul(wn, rvar_vec);
275 wn = wrapper::vmul(wn, gamma_vec);
276
277 // Store results
278 wrapper::vstore(dwc_w_out_ptr + x, wn);
279 }
280
281 // Compute left-over elements
282 for(; x < window_end_x; ++x)
283 {
284 auto var = input_var[x];
285 if(input_gamma != nullptr)
286 {
287 gamma = input_gamma[x];
288 }
289
290 if(id[2] == 0 && id[1] == 0)
291 {
292 auto mean = input_mean[x];
293 if(input_beta != nullptr)
294 {
295 beta = input_beta[x];
296 }
297 if(dwc_bias_in != nullptr)
298 {
299 dwc_bias_in_scalar = dwc_bias_in[x];
300 }
301
302 auto dwc_bias_tmp_scalar = (dwc_bias_in_scalar - mean) / std::sqrt(var + ScalarType(epsilon));
303 dwc_bias_out[x] = (dwc_bias_tmp_scalar * gamma) + beta;
304 }
305
306 const auto dwc_w_in_ptr = reinterpret_cast<const ScalarType *>(dwc_w_in.ptr());
307 auto dwc_w_out_ptr = reinterpret_cast<ScalarType *>(dwc_w_out.ptr());
308
309 *(dwc_w_out_ptr + x) = *(dwc_w_in_ptr + x) / std::sqrt(var + ScalarType(epsilon)) * gamma;
310 }
311 },
312 dwc_w_in, dwc_w_out);
313}
314
315template <typename VectorType>
316void fused_batch_normalization_dwc_nchw(const ITensor *dwc_weights, const ITensor *dwc_bias, ITensor *fused_weights, ITensor *fused_bias,
317 const ITensor *bn_mean, const ITensor *bn_var, const ITensor *bn_beta, const ITensor *bn_gamma, float epsilon, const Window &window)
318{
319 using ScalarType = typename VectorType::scalar_type;
320 const int size = 16 / dwc_weights->info()->element_size();
321 using ExactTagType = typename VectorType::tag_type;
322
323 const bool run_in_place_weights = (fused_weights == nullptr) || (fused_weights == dwc_weights);
324 const bool run_in_place_bias = (fused_bias == nullptr) || (dwc_bias != nullptr && fused_bias == dwc_bias);
325
326 // Set build options
327 Window win = window;
328 win.set(Window::DimX, Window::Dimension(0, 1, 1));
329
330 const int window_step_x = size;
331 const auto window_start_x = static_cast<int>(window.x().start());
332 const auto window_end_x = static_cast<int>(window.x().end());
333
334 Iterator dwc_w_in(dwc_weights, win);
335 Iterator dwc_w_out(run_in_place_weights ? dwc_weights : fused_weights, win);
336
337 const auto dwc_bias_in = (dwc_bias != nullptr ? reinterpret_cast<ScalarType *>(dwc_bias->ptr_to_element(Coordinates(0, 0))) : nullptr);
338 auto dwc_bias_out = (run_in_place_bias ? dwc_bias_in : reinterpret_cast<ScalarType *>(fused_bias->ptr_to_element(Coordinates(0, 0))));
339
340 const auto input_mean = reinterpret_cast<const ScalarType *>(bn_mean->ptr_to_element(Coordinates(0, 0)));
341 const auto input_var = reinterpret_cast<const ScalarType *>(bn_var->ptr_to_element(Coordinates(0, 0)));
342 const auto input_gamma = (bn_gamma != nullptr) ? reinterpret_cast<const ScalarType *>(bn_gamma->ptr_to_element(Coordinates(0, 0))) : nullptr;
343 const auto input_beta = (bn_beta != nullptr) ? reinterpret_cast<const ScalarType *>(bn_beta->ptr_to_element(Coordinates(0, 0))) : nullptr;
344
345 auto mean_vec = wrapper::vdup_n(ScalarType(0), ExactTagType{});
346 auto var_vec = wrapper::vdup_n(ScalarType(0), ExactTagType{});
347 auto gamma_vec = wrapper::vdup_n(ScalarType(1), ExactTagType{});
348 auto beta_vec = wrapper::vdup_n(ScalarType(0), ExactTagType{});
349 auto rvar_vec = wrapper::vdup_n(ScalarType(0), ExactTagType{});
350 const auto epsilon_vec = wrapper::vdup_n(ScalarType(epsilon), ExactTagType{});
351
352 auto mean = ScalarType(0.0);
353 auto var = ScalarType(0.0);
354 auto gamma = ScalarType(1.0);
355 auto beta = ScalarType(0.0);
356 auto dwc_bias_in_scalar = ScalarType(0.0);
357 execute_window_loop(win, [&](const Coordinates & id)
358 {
359 var = input_var[id[2]];
360 if(input_gamma != nullptr)
361 {
362 gamma = input_gamma[id[2]];
363 }
364
365 if(id[1] == 0)
366 {
367 mean = input_mean[id[2]];
368
369 // Construct vectors
370 mean_vec = wrapper::vdup_n(mean, ExactTagType{});
371 if(input_beta != nullptr)
372 {
373 beta = input_beta[id[2]];
374 beta_vec = wrapper::vdup_n(beta, ExactTagType{});
375 }
376
377 if(dwc_bias_in != nullptr)
378 {
379 dwc_bias_in_scalar = dwc_bias_in[id[2]];
380 }
381
382 auto dwc_bias_tmp_scalar = (dwc_bias_in_scalar - mean) / std::sqrt(var + ScalarType(epsilon));
383 dwc_bias_out[id[2]] = (dwc_bias_tmp_scalar * gamma) + beta;
384 }
385
386 int x = window_start_x;
387 auto dwc_w_in_ptr = reinterpret_cast<const ScalarType *>(dwc_w_in.ptr());
388 auto dwc_w_out_ptr = reinterpret_cast<ScalarType *>(dwc_w_out.ptr());
389 var_vec = wrapper::vdup_n(var, ExactTagType{});
390 gamma_vec = wrapper::vdup_n(gamma, ExactTagType{});
391 rvar_vec = wrapper::vinvsqrt(wrapper::vadd(var_vec, epsilon_vec));
392
393 for(; x <= (window_end_x - window_step_x); x += window_step_x)
394 {
395 auto wn = wrapper::vloadq(dwc_w_in_ptr + x);
396 wn = wrapper::vmul(wn, rvar_vec);
397 wn = wrapper::vmul(wn, gamma_vec);
398
399 // Store results
400 wrapper::vstore(dwc_w_out_ptr + x, wn);
401 }
402
403 // Compute left-over elements
404 for(; x < window_end_x; ++x)
405 {
406 *(dwc_w_out_ptr + x) = *(dwc_w_in_ptr + x) / std::sqrt(var + ScalarType(epsilon)) * gamma;
407 }
408 },
409 dwc_w_in, dwc_w_out);
410}
411
giuros0115ecc9a2018-12-06 10:47:34 +0000412} // namespace
413
414NEFuseBatchNormalizationKernel::NEFuseBatchNormalizationKernel()
Manuel Bottini11091762019-06-17 12:04:40 +0100415 : _input_weights(nullptr), _input_bias(nullptr), _bn_mean(nullptr), _bn_var(nullptr), _bn_gamma(nullptr), _bn_beta(nullptr), _fused_weights(nullptr), _fused_bias(nullptr), _epsilon(),
giuros0115ecc9a2018-12-06 10:47:34 +0000416 _run_in_place_weights(false), _run_in_place_bias(false), _func(nullptr)
417{
418}
419
Manuel Bottini11091762019-06-17 12:04:40 +0100420void NEFuseBatchNormalizationKernel::configure(const ITensor *input_weights, const ITensor *bn_mean, const ITensor *bn_var,
giuros0115ecc9a2018-12-06 10:47:34 +0000421 ITensor *fused_weights, ITensor *fused_bias,
Manuel Bottini11091762019-06-17 12:04:40 +0100422 const ITensor *input_bias, const ITensor *bn_beta, const ITensor *bn_gamma,
423 float epsilon, FuseBatchNormalizationType fbn_type)
giuros0115ecc9a2018-12-06 10:47:34 +0000424{
Manuel Bottini11091762019-06-17 12:04:40 +0100425 ARM_COMPUTE_ERROR_ON_NULLPTR(input_weights, bn_mean, bn_var);
giuros0115ecc9a2018-12-06 10:47:34 +0000426
Manuel Bottini11091762019-06-17 12:04:40 +0100427 _input_weights = input_weights;
428 _input_bias = input_bias;
giuros0115ecc9a2018-12-06 10:47:34 +0000429 _bn_mean = bn_mean;
430 _bn_var = bn_var;
431 _bn_beta = bn_beta;
432 _bn_gamma = bn_gamma;
433 _fused_weights = fused_weights;
434 _fused_bias = fused_bias;
435 _epsilon = epsilon;
436
Manuel Bottini11091762019-06-17 12:04:40 +0100437 _run_in_place_weights = (fused_weights == nullptr) || (fused_weights == input_weights);
438 _run_in_place_bias = (fused_bias == nullptr) || (input_bias != nullptr && fused_bias == input_bias);
giuros0115ecc9a2018-12-06 10:47:34 +0000439
440 // Auto initialize outputs
441 if(_fused_weights != nullptr)
442 {
443 // Output tensor auto initialization if not yet initialized
Manuel Bottini11091762019-06-17 12:04:40 +0100444 auto_init_if_empty(*_fused_weights->info(), *_input_weights->info()->clone());
445 fused_weights->info()->set_valid_region(input_weights->info()->valid_region());
giuros0115ecc9a2018-12-06 10:47:34 +0000446 }
447 if(_fused_bias != nullptr)
448 {
449 // Output tensor auto initialization if not yet initialized
450 auto_init_if_empty(*_fused_bias->info(), *_bn_mean->info()->clone());
451 _fused_bias->info()->set_valid_region(bn_mean->info()->valid_region());
452 }
453
454 // Validate arguments
Manuel Bottini11091762019-06-17 12:04:40 +0100455 ARM_COMPUTE_ERROR_THROW_ON(validate_arguments(input_weights->info(), bn_mean->info(), bn_var->info(),
giuros0115ecc9a2018-12-06 10:47:34 +0000456 (fused_weights != nullptr) ? fused_weights->info() : nullptr,
457 (fused_bias != nullptr) ? fused_bias->info() : nullptr,
Manuel Bottini11091762019-06-17 12:04:40 +0100458 (input_bias != nullptr) ? input_bias->info() : nullptr,
giuros0115ecc9a2018-12-06 10:47:34 +0000459 (bn_beta != nullptr) ? bn_beta->info() : nullptr,
460 (bn_gamma != nullptr) ? bn_gamma->info() : nullptr,
Manuel Bottini11091762019-06-17 12:04:40 +0100461 epsilon, fbn_type));
giuros0115ecc9a2018-12-06 10:47:34 +0000462
463 // Configure kernel window
Manuel Bottini11091762019-06-17 12:04:40 +0100464 Window win = calculate_max_window(*input_weights->info());
giuros0115ecc9a2018-12-06 10:47:34 +0000465 INEKernel::configure(win);
466
Manuel Bottini11091762019-06-17 12:04:40 +0100467 // Configure function
468 static std::map<std::string, FuseBatchNormFunction *> map_function =
giuros0115ecc9a2018-12-06 10:47:34 +0000469 {
Manuel Bottini11091762019-06-17 12:04:40 +0100470 { "fused_batch_normalization_conv_NHWC_F32", &fused_batch_normalization_conv<wrapper::traits::neon_vector<float, 4>> },
471 { "fused_batch_normalization_conv_NCHW_F32", &fused_batch_normalization_conv<wrapper::traits::neon_vector<float, 4>> },
472 { "fused_batch_normalization_dwc_NHWC_F32", &fused_batch_normalization_dwc_nhwc<wrapper::traits::neon_vector<float, 4>> },
473 { "fused_batch_normalization_dwc_NCHW_F32", &fused_batch_normalization_dwc_nchw<wrapper::traits::neon_vector<float, 4>> },
giuros0115ecc9a2018-12-06 10:47:34 +0000474#ifdef __ARM_FEATURE_FP16_VECTOR_ARITHMETIC
Manuel Bottini11091762019-06-17 12:04:40 +0100475 { "fused_batch_normalization_conv_NHWC_F16", &fused_batch_normalization_conv<wrapper::traits::neon_vector<float16_t, 8>> },
476 { "fused_batch_normalization_conv_NCHW_F16", &fused_batch_normalization_conv<wrapper::traits::neon_vector<float16_t, 8>> },
477 { "fused_batch_normalization_dwc_NHWC_F16", &fused_batch_normalization_dwc_nhwc<wrapper::traits::neon_vector<float16_t, 8>> },
478 { "fused_batch_normalization_dwc_NCHW_F16", &fused_batch_normalization_dwc_nchw<wrapper::traits::neon_vector<float16_t, 8>> },
479#endif /* __ARM_FEATURE_FP16_VECTOR_ARITHMETIC */
480 };
481
482 std::string function_to_call("fused_batch_normalization_");
483 function_to_call += fbn_type == FuseBatchNormalizationType::CONVOLUTION ? "conv_" : "dwc_";
484 function_to_call += string_from_data_layout(_input_weights->info()->data_layout());
485 function_to_call += "_";
486 function_to_call += string_from_data_type(_input_weights->info()->data_type());
487
488 auto it = map_function.find(function_to_call);
489
490 if(it != map_function.end())
491 {
492 _func = it->second;
giuros0115ecc9a2018-12-06 10:47:34 +0000493 }
494}
495
Manuel Bottini11091762019-06-17 12:04:40 +0100496Status NEFuseBatchNormalizationKernel::validate(const ITensorInfo *input_weights, const ITensorInfo *bn_mean, const ITensorInfo *bn_var,
giuros0115ecc9a2018-12-06 10:47:34 +0000497 const ITensorInfo *fused_weights, const ITensorInfo *fused_bias,
Manuel Bottini11091762019-06-17 12:04:40 +0100498 const ITensorInfo *input_bias, const ITensorInfo *bn_beta, const ITensorInfo *bn_gamma,
499 float epsilon, FuseBatchNormalizationType fbn_type)
giuros0115ecc9a2018-12-06 10:47:34 +0000500{
Manuel Bottini11091762019-06-17 12:04:40 +0100501 ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments(input_weights, bn_mean, bn_var, fused_weights, fused_bias, input_bias, bn_beta, bn_gamma, epsilon, fbn_type));
giuros0115ecc9a2018-12-06 10:47:34 +0000502 return Status{};
503}
504
505void NEFuseBatchNormalizationKernel::run(const Window &window, const ThreadInfo &info)
506{
507 ARM_COMPUTE_UNUSED(info);
508 ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this);
509 ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(IKernel::window(), window);
Manuel Bottini11091762019-06-17 12:04:40 +0100510 (*_func)(_input_weights, _input_bias, _fused_weights, _fused_bias, _bn_mean, _bn_var, _bn_beta, _bn_gamma, _epsilon, window);
giuros0115ecc9a2018-12-06 10:47:34 +0000511}
512} // namespace arm_compute