blob: 6e7e5ab23fabcda1a1c5e55fa06fa70868b34158 [file] [log] [blame]
giuros0115ecc9a2018-12-06 10:47:34 +00001/*
Matthew Bentham92046462020-03-07 22:15:55 +00002 * 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
giuros0115ecc9a2018-12-06 10:47:34 +000035#include "utils/TypePrinter.h"
Manuel Bottini11091762019-06-17 12:04:40 +010036#include <map>
37
giuros0115ecc9a2018-12-06 10:47:34 +000038namespace arm_compute
39{
40namespace
41{
Manuel Bottini11091762019-06-17 12:04:40 +010042Status validate_arguments(const ITensorInfo *input_weights, const ITensorInfo *bn_mean, const ITensorInfo *bn_var,
giuros0115ecc9a2018-12-06 10:47:34 +000043 const ITensorInfo *fused_weights, const ITensorInfo *fused_bias,
Manuel Bottini11091762019-06-17 12:04:40 +010044 const ITensorInfo *input_bias, const ITensorInfo *bn_beta, const ITensorInfo *bn_gamma,
45 float epsilon, FuseBatchNormalizationType fbn_type)
giuros0115ecc9a2018-12-06 10:47:34 +000046{
47 ARM_COMPUTE_UNUSED(epsilon);
Manuel Bottini11091762019-06-17 12:04:40 +010048 ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(input_weights, bn_mean, bn_var);
49 ARM_COMPUTE_RETURN_ERROR_ON_CPU_F16_UNSUPPORTED(input_weights);
50 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input_weights, 1, DataType::F16, DataType::F32);
giuros0115ecc9a2018-12-06 10:47:34 +000051 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_SHAPES(bn_mean, bn_var);
Manuel Bottini11091762019-06-17 12:04:40 +010052 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input_weights, bn_mean, bn_var);
53 ARM_COMPUTE_RETURN_ERROR_ON(input_bias == nullptr && fused_bias == nullptr);
54 ARM_COMPUTE_RETURN_ERROR_ON(bn_mean->num_dimensions() > 1);
giuros0115ecc9a2018-12-06 10:47:34 +000055
Manuel Bottini11091762019-06-17 12:04:40 +010056 if(fbn_type == FuseBatchNormalizationType::CONVOLUTION)
giuros0115ecc9a2018-12-06 10:47:34 +000057 {
Manuel Bottini11091762019-06-17 12:04:40 +010058 ARM_COMPUTE_RETURN_ERROR_ON(input_weights->dimension(3) != bn_mean->dimension(0));
59 }
60 else
61 {
62 const size_t channel_idx = get_data_layout_dimension_index(input_weights->data_layout(), DataLayoutDimension::CHANNEL);
63 ARM_COMPUTE_RETURN_ERROR_ON(input_weights->dimension(channel_idx) != bn_mean->dimension(0));
64 }
65 // Validate bias
66 if(input_bias != nullptr)
67 {
68 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_SHAPES(bn_mean, input_bias);
69 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input_weights, input_bias);
giuros0115ecc9a2018-12-06 10:47:34 +000070 }
71 // Validate beta
72 if(bn_beta != nullptr)
73 {
74 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_SHAPES(bn_mean, bn_beta);
Manuel Bottini11091762019-06-17 12:04:40 +010075 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input_weights, bn_beta);
giuros0115ecc9a2018-12-06 10:47:34 +000076 }
77 // Validate gamma
78 if(bn_gamma != nullptr)
79 {
80 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_SHAPES(bn_mean, bn_gamma);
Manuel Bottini11091762019-06-17 12:04:40 +010081 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input_weights, bn_gamma);
giuros0115ecc9a2018-12-06 10:47:34 +000082 }
83
84 // Validate output weights
85 if(fused_weights != nullptr && fused_weights->total_size() != 0)
86 {
Manuel Bottini11091762019-06-17 12:04:40 +010087 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_SHAPES(input_weights, fused_weights);
88 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_LAYOUT(input_weights, fused_weights);
89 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input_weights, fused_weights);
giuros0115ecc9a2018-12-06 10:47:34 +000090 }
91 // Validate output bias
92 if(fused_bias != nullptr && fused_bias->total_size() != 0)
93 {
94 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_SHAPES(bn_mean, fused_bias);
Manuel Bottini11091762019-06-17 12:04:40 +010095 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input_weights, fused_bias);
giuros0115ecc9a2018-12-06 10:47:34 +000096 }
97
98 return Status{};
99}
100
Manuel Bottini11091762019-06-17 12:04:40 +0100101template <typename VectorType>
102void fused_batch_normalization_conv(const ITensor *conv_weights, const ITensor *conv_bias, ITensor *fused_weights, ITensor *fused_bias,
103 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 +0000104{
Manuel Bottini11091762019-06-17 12:04:40 +0100105 using ScalarType = typename VectorType::scalar_type;
106 const int size = 16 / conv_weights->info()->element_size();
107 using ExactTagType = typename VectorType::tag_type;
giuros0115ecc9a2018-12-06 10:47:34 +0000108
109 const bool run_in_place_weights = (fused_weights == nullptr) || (fused_weights == conv_weights);
110 const bool run_in_place_bias = (fused_bias == nullptr) || (conv_bias != nullptr && fused_bias == conv_bias);
111
112 // Set build options
113 Window win = window;
114 win.set(Window::DimX, Window::Dimension(0, 1, 1));
115
116 const int window_step_x = size;
117 const auto window_start_x = static_cast<int>(window.x().start());
118 const auto window_end_x = static_cast<int>(window.x().end());
119
120 Iterator conv_w_in(conv_weights, win);
121 Iterator conv_w_out(run_in_place_weights ? conv_weights : fused_weights, win);
122
123 const auto conv_bias_in = (conv_bias != nullptr ? reinterpret_cast<ScalarType *>(conv_bias->ptr_to_element(Coordinates(0, 0))) : nullptr);
124 auto conv_bias_out = (run_in_place_bias ? conv_bias_in : reinterpret_cast<ScalarType *>(fused_bias->ptr_to_element(Coordinates(0, 0))));
125
giuros0115ecc9a2018-12-06 10:47:34 +0000126 const auto input_mean = reinterpret_cast<const ScalarType *>(bn_mean->ptr_to_element(Coordinates(0, 0)));
127 const auto input_var = reinterpret_cast<const ScalarType *>(bn_var->ptr_to_element(Coordinates(0, 0)));
128 const auto input_gamma = (bn_gamma != nullptr) ? reinterpret_cast<const ScalarType *>(bn_gamma->ptr_to_element(Coordinates(0, 0))) : nullptr;
129 const auto input_beta = (bn_beta != nullptr) ? reinterpret_cast<const ScalarType *>(bn_beta->ptr_to_element(Coordinates(0, 0))) : nullptr;
130
131 auto mean_vec = wrapper::vdup_n(ScalarType(0), ExactTagType{});
132 auto var_vec = wrapper::vdup_n(ScalarType(0), ExactTagType{});
133 auto gamma_vec = wrapper::vdup_n(ScalarType(1), ExactTagType{});
134 auto beta_vec = wrapper::vdup_n(ScalarType(0), ExactTagType{});
135 auto rvar_vec = wrapper::vdup_n(ScalarType(0), ExactTagType{});
136 const auto epsilon_vec = wrapper::vdup_n(ScalarType(epsilon), ExactTagType{});
137
138 auto mean = ScalarType(0.0);
139 auto var = ScalarType(0.0);
140 auto gamma = ScalarType(1.0);
141 auto beta = ScalarType(0.0);
142 auto conv_bias_in_scalar = ScalarType(0.0);
143 execute_window_loop(win, [&](const Coordinates & id)
144 {
Manuel Bottini11091762019-06-17 12:04:40 +0100145 var = input_var[id[3]];
146 if(input_gamma != nullptr)
giuros0115ecc9a2018-12-06 10:47:34 +0000147 {
Manuel Bottini11091762019-06-17 12:04:40 +0100148 gamma = input_gamma[id[3]];
149 }
giuros0115ecc9a2018-12-06 10:47:34 +0000150
Manuel Bottini11091762019-06-17 12:04:40 +0100151 if((id[0] == 0) && (id[1] == 0) && (id[2] == 0))
152 {
giuros0115ecc9a2018-12-06 10:47:34 +0000153 if(input_beta != nullptr)
154 {
Manuel Bottini11091762019-06-17 12:04:40 +0100155 beta = input_beta[id[3]];
giuros0115ecc9a2018-12-06 10:47:34 +0000156 beta_vec = wrapper::vdup_n(beta, ExactTagType{});
157 }
Manuel Bottini11091762019-06-17 12:04:40 +0100158
159 // Construct vectors
160 mean = input_mean[id[3]];
161 mean_vec = wrapper::vdup_n(mean, ExactTagType{});
162
giuros0115ecc9a2018-12-06 10:47:34 +0000163 if(conv_bias_in != nullptr)
164 {
Manuel Bottini11091762019-06-17 12:04:40 +0100165 conv_bias_in_scalar = conv_bias_in[id[3]];
giuros0115ecc9a2018-12-06 10:47:34 +0000166 }
Manuel Bottini11091762019-06-17 12:04:40 +0100167 auto conv_bias_tmp_scalar = (conv_bias_in_scalar - mean) / std::sqrt(var + ScalarType(epsilon));
168 conv_bias_out[id[3]] = (conv_bias_tmp_scalar * gamma) + beta;
giuros0115ecc9a2018-12-06 10:47:34 +0000169 }
170
171 int x = window_start_x;
172 auto conv_w_in_ptr = reinterpret_cast<const ScalarType *>(conv_w_in.ptr());
173 auto conv_w_out_ptr = reinterpret_cast<ScalarType *>(conv_w_out.ptr());
Manuel Bottini11091762019-06-17 12:04:40 +0100174 var_vec = wrapper::vdup_n(var, ExactTagType{});
175 gamma_vec = wrapper::vdup_n(gamma, ExactTagType{});
176 rvar_vec = wrapper::vinvsqrt(wrapper::vadd(var_vec, epsilon_vec));
giuros0115ecc9a2018-12-06 10:47:34 +0000177
178 for(; x <= (window_end_x - window_step_x); x += window_step_x)
179 {
180 auto wn = wrapper::vloadq(conv_w_in_ptr + x);
181 wn = wrapper::vmul(wn, rvar_vec);
182 wn = wrapper::vmul(wn, gamma_vec);
183
184 // Store results
185 wrapper::vstore(conv_w_out_ptr + x, wn);
186 }
187
188 // Compute left-over elements
189 for(; x < window_end_x; ++x)
190 {
Manuel Bottini11091762019-06-17 12:04:40 +0100191 *(conv_w_out_ptr + x) = *(conv_w_in_ptr + x) / std::sqrt(var + ScalarType(epsilon)) * gamma;
giuros0115ecc9a2018-12-06 10:47:34 +0000192 }
193 },
194 conv_w_in, conv_w_out);
195}
Manuel Bottini11091762019-06-17 12:04:40 +0100196
197template <typename VectorType>
198void fused_batch_normalization_dwc_nhwc(const ITensor *dwc_weights, const ITensor *dwc_bias, ITensor *fused_weights, ITensor *fused_bias,
199 const ITensor *bn_mean, const ITensor *bn_var, const ITensor *bn_beta, const ITensor *bn_gamma, float epsilon, const Window &window)
200{
201 using ScalarType = typename VectorType::scalar_type;
202 const int size = 16 / dwc_weights->info()->element_size();
203 using ExactTagType = typename VectorType::tag_type;
204
205 const bool run_in_place_weights = (fused_weights == nullptr) || (fused_weights == dwc_weights);
206 const bool run_in_place_bias = (fused_bias == nullptr) || (dwc_bias != nullptr && fused_bias == dwc_bias);
207
208 // Set build options
209 Window win = window;
210 win.set(Window::DimX, Window::Dimension(0, 1, 1));
211
212 const int window_step_x = size;
213 const auto window_start_x = static_cast<int>(window.x().start());
214 const auto window_end_x = static_cast<int>(window.x().end());
215
216 Iterator dwc_w_in(dwc_weights, win);
217 Iterator dwc_w_out(run_in_place_weights ? dwc_weights : fused_weights, win);
218
219 const auto dwc_bias_in = (dwc_bias != nullptr ? reinterpret_cast<ScalarType *>(dwc_bias->ptr_to_element(Coordinates(0, 0))) : nullptr);
220 auto dwc_bias_out = (run_in_place_bias ? dwc_bias_in : reinterpret_cast<ScalarType *>(fused_bias->ptr_to_element(Coordinates(0, 0))));
221
222 const auto input_mean = reinterpret_cast<const ScalarType *>(bn_mean->ptr_to_element(Coordinates(0, 0)));
223 const auto input_var = reinterpret_cast<const ScalarType *>(bn_var->ptr_to_element(Coordinates(0, 0)));
224 const auto input_gamma = (bn_gamma != nullptr) ? reinterpret_cast<const ScalarType *>(bn_gamma->ptr_to_element(Coordinates(0, 0))) : nullptr;
225 const auto input_beta = (bn_beta != nullptr) ? reinterpret_cast<const ScalarType *>(bn_beta->ptr_to_element(Coordinates(0, 0))) : nullptr;
226
227 auto mean_vec = wrapper::vdup_n(ScalarType(0), ExactTagType{});
228 auto var_vec = wrapper::vdup_n(ScalarType(0), ExactTagType{});
229 auto gamma_vec = wrapper::vdup_n(ScalarType(1), ExactTagType{});
230 auto beta_vec = wrapper::vdup_n(ScalarType(0), ExactTagType{});
231 auto rvar_vec = wrapper::vdup_n(ScalarType(0), ExactTagType{});
232 auto dwc_bias_vec = wrapper::vdup_n(ScalarType(0), ExactTagType{});
233 const auto epsilon_vec = wrapper::vdup_n(ScalarType(epsilon), ExactTagType{});
234
235 auto gamma = ScalarType(1.0);
236 auto beta = ScalarType(0.0);
237 auto dwc_bias_in_scalar = ScalarType(0);
238
239 execute_window_loop(win, [&](const Coordinates & id)
240 {
241 int x = window_start_x;
242 for(; x <= (window_end_x - window_step_x); x += window_step_x)
243 {
244 var_vec = wrapper::vloadq(input_var + x);
245 if(input_gamma != nullptr)
246 {
247 gamma_vec = wrapper::vloadq(input_gamma + x);
248 }
249
250 if((id[2] == 0) && (id[1] == 0))
251 {
252 mean_vec = wrapper::vloadq(input_mean + x);
253
254 // Construct vectors
255 if(input_beta != nullptr)
256 {
257 beta_vec = wrapper::vloadq(input_beta + x);
258 }
259
260 if(dwc_bias_in != nullptr)
261 {
262 dwc_bias_vec = wrapper::vloadq(dwc_bias_in + x);
263 }
264
265 auto dwc_bias_tmp_vec = wrapper::vmul(wrapper::vsub(dwc_bias_vec, mean_vec), wrapper::vinvsqrt(wrapper::vadd(var_vec, epsilon_vec)));
266 dwc_bias_tmp_vec = wrapper::vadd(wrapper::vmul(dwc_bias_tmp_vec, gamma_vec), beta_vec);
267 wrapper::vstore(dwc_bias_out + x, dwc_bias_tmp_vec);
268 }
269
270 auto dwc_w_in_ptr = reinterpret_cast<const ScalarType *>(dwc_w_in.ptr());
271 auto dwc_w_out_ptr = reinterpret_cast<ScalarType *>(dwc_w_out.ptr());
272
273 auto wn = wrapper::vloadq(dwc_w_in_ptr + x);
274 rvar_vec = wrapper::vinvsqrt(wrapper::vadd(var_vec, epsilon_vec));
275 wn = wrapper::vmul(wn, rvar_vec);
276 wn = wrapper::vmul(wn, gamma_vec);
277
278 // Store results
279 wrapper::vstore(dwc_w_out_ptr + x, wn);
280 }
281
282 // Compute left-over elements
283 for(; x < window_end_x; ++x)
284 {
285 auto var = input_var[x];
286 if(input_gamma != nullptr)
287 {
288 gamma = input_gamma[x];
289 }
290
291 if(id[2] == 0 && id[1] == 0)
292 {
293 auto mean = input_mean[x];
294 if(input_beta != nullptr)
295 {
296 beta = input_beta[x];
297 }
298 if(dwc_bias_in != nullptr)
299 {
300 dwc_bias_in_scalar = dwc_bias_in[x];
301 }
302
303 auto dwc_bias_tmp_scalar = (dwc_bias_in_scalar - mean) / std::sqrt(var + ScalarType(epsilon));
304 dwc_bias_out[x] = (dwc_bias_tmp_scalar * gamma) + beta;
305 }
306
307 const auto dwc_w_in_ptr = reinterpret_cast<const ScalarType *>(dwc_w_in.ptr());
308 auto dwc_w_out_ptr = reinterpret_cast<ScalarType *>(dwc_w_out.ptr());
309
310 *(dwc_w_out_ptr + x) = *(dwc_w_in_ptr + x) / std::sqrt(var + ScalarType(epsilon)) * gamma;
311 }
312 },
313 dwc_w_in, dwc_w_out);
314}
315
316template <typename VectorType>
317void fused_batch_normalization_dwc_nchw(const ITensor *dwc_weights, const ITensor *dwc_bias, ITensor *fused_weights, ITensor *fused_bias,
318 const ITensor *bn_mean, const ITensor *bn_var, const ITensor *bn_beta, const ITensor *bn_gamma, float epsilon, const Window &window)
319{
320 using ScalarType = typename VectorType::scalar_type;
321 const int size = 16 / dwc_weights->info()->element_size();
322 using ExactTagType = typename VectorType::tag_type;
323
324 const bool run_in_place_weights = (fused_weights == nullptr) || (fused_weights == dwc_weights);
325 const bool run_in_place_bias = (fused_bias == nullptr) || (dwc_bias != nullptr && fused_bias == dwc_bias);
326
327 // Set build options
328 Window win = window;
329 win.set(Window::DimX, Window::Dimension(0, 1, 1));
330
331 const int window_step_x = size;
332 const auto window_start_x = static_cast<int>(window.x().start());
333 const auto window_end_x = static_cast<int>(window.x().end());
334
335 Iterator dwc_w_in(dwc_weights, win);
336 Iterator dwc_w_out(run_in_place_weights ? dwc_weights : fused_weights, win);
337
338 const auto dwc_bias_in = (dwc_bias != nullptr ? reinterpret_cast<ScalarType *>(dwc_bias->ptr_to_element(Coordinates(0, 0))) : nullptr);
339 auto dwc_bias_out = (run_in_place_bias ? dwc_bias_in : reinterpret_cast<ScalarType *>(fused_bias->ptr_to_element(Coordinates(0, 0))));
340
341 const auto input_mean = reinterpret_cast<const ScalarType *>(bn_mean->ptr_to_element(Coordinates(0, 0)));
342 const auto input_var = reinterpret_cast<const ScalarType *>(bn_var->ptr_to_element(Coordinates(0, 0)));
343 const auto input_gamma = (bn_gamma != nullptr) ? reinterpret_cast<const ScalarType *>(bn_gamma->ptr_to_element(Coordinates(0, 0))) : nullptr;
344 const auto input_beta = (bn_beta != nullptr) ? reinterpret_cast<const ScalarType *>(bn_beta->ptr_to_element(Coordinates(0, 0))) : nullptr;
345
346 auto mean_vec = wrapper::vdup_n(ScalarType(0), ExactTagType{});
347 auto var_vec = wrapper::vdup_n(ScalarType(0), ExactTagType{});
348 auto gamma_vec = wrapper::vdup_n(ScalarType(1), ExactTagType{});
349 auto beta_vec = wrapper::vdup_n(ScalarType(0), ExactTagType{});
350 auto rvar_vec = wrapper::vdup_n(ScalarType(0), ExactTagType{});
351 const auto epsilon_vec = wrapper::vdup_n(ScalarType(epsilon), ExactTagType{});
352
353 auto mean = ScalarType(0.0);
354 auto var = ScalarType(0.0);
355 auto gamma = ScalarType(1.0);
356 auto beta = ScalarType(0.0);
357 auto dwc_bias_in_scalar = ScalarType(0.0);
358 execute_window_loop(win, [&](const Coordinates & id)
359 {
360 var = input_var[id[2]];
361 if(input_gamma != nullptr)
362 {
363 gamma = input_gamma[id[2]];
364 }
365
366 if(id[1] == 0)
367 {
368 mean = input_mean[id[2]];
369
370 // Construct vectors
371 mean_vec = wrapper::vdup_n(mean, ExactTagType{});
372 if(input_beta != nullptr)
373 {
374 beta = input_beta[id[2]];
375 beta_vec = wrapper::vdup_n(beta, ExactTagType{});
376 }
377
378 if(dwc_bias_in != nullptr)
379 {
380 dwc_bias_in_scalar = dwc_bias_in[id[2]];
381 }
382
383 auto dwc_bias_tmp_scalar = (dwc_bias_in_scalar - mean) / std::sqrt(var + ScalarType(epsilon));
384 dwc_bias_out[id[2]] = (dwc_bias_tmp_scalar * gamma) + beta;
385 }
386
387 int x = window_start_x;
388 auto dwc_w_in_ptr = reinterpret_cast<const ScalarType *>(dwc_w_in.ptr());
389 auto dwc_w_out_ptr = reinterpret_cast<ScalarType *>(dwc_w_out.ptr());
390 var_vec = wrapper::vdup_n(var, ExactTagType{});
391 gamma_vec = wrapper::vdup_n(gamma, ExactTagType{});
392 rvar_vec = wrapper::vinvsqrt(wrapper::vadd(var_vec, epsilon_vec));
393
394 for(; x <= (window_end_x - window_step_x); x += window_step_x)
395 {
396 auto wn = wrapper::vloadq(dwc_w_in_ptr + x);
397 wn = wrapper::vmul(wn, rvar_vec);
398 wn = wrapper::vmul(wn, gamma_vec);
399
400 // Store results
401 wrapper::vstore(dwc_w_out_ptr + x, wn);
402 }
403
404 // Compute left-over elements
405 for(; x < window_end_x; ++x)
406 {
407 *(dwc_w_out_ptr + x) = *(dwc_w_in_ptr + x) / std::sqrt(var + ScalarType(epsilon)) * gamma;
408 }
409 },
410 dwc_w_in, dwc_w_out);
411}
412
giuros0115ecc9a2018-12-06 10:47:34 +0000413} // namespace
414
415NEFuseBatchNormalizationKernel::NEFuseBatchNormalizationKernel()
Manuel Bottini11091762019-06-17 12:04:40 +0100416 : _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 +0000417 _run_in_place_weights(false), _run_in_place_bias(false), _func(nullptr)
418{
419}
420
Manuel Bottini11091762019-06-17 12:04:40 +0100421void NEFuseBatchNormalizationKernel::configure(const ITensor *input_weights, const ITensor *bn_mean, const ITensor *bn_var,
giuros0115ecc9a2018-12-06 10:47:34 +0000422 ITensor *fused_weights, ITensor *fused_bias,
Manuel Bottini11091762019-06-17 12:04:40 +0100423 const ITensor *input_bias, const ITensor *bn_beta, const ITensor *bn_gamma,
424 float epsilon, FuseBatchNormalizationType fbn_type)
giuros0115ecc9a2018-12-06 10:47:34 +0000425{
Manuel Bottini11091762019-06-17 12:04:40 +0100426 ARM_COMPUTE_ERROR_ON_NULLPTR(input_weights, bn_mean, bn_var);
giuros0115ecc9a2018-12-06 10:47:34 +0000427
Manuel Bottini11091762019-06-17 12:04:40 +0100428 _input_weights = input_weights;
429 _input_bias = input_bias;
giuros0115ecc9a2018-12-06 10:47:34 +0000430 _bn_mean = bn_mean;
431 _bn_var = bn_var;
432 _bn_beta = bn_beta;
433 _bn_gamma = bn_gamma;
434 _fused_weights = fused_weights;
435 _fused_bias = fused_bias;
436 _epsilon = epsilon;
437
Manuel Bottini11091762019-06-17 12:04:40 +0100438 _run_in_place_weights = (fused_weights == nullptr) || (fused_weights == input_weights);
439 _run_in_place_bias = (fused_bias == nullptr) || (input_bias != nullptr && fused_bias == input_bias);
giuros0115ecc9a2018-12-06 10:47:34 +0000440
441 // Auto initialize outputs
442 if(_fused_weights != nullptr)
443 {
444 // Output tensor auto initialization if not yet initialized
Manuel Bottini11091762019-06-17 12:04:40 +0100445 auto_init_if_empty(*_fused_weights->info(), *_input_weights->info()->clone());
446 fused_weights->info()->set_valid_region(input_weights->info()->valid_region());
giuros0115ecc9a2018-12-06 10:47:34 +0000447 }
448 if(_fused_bias != nullptr)
449 {
450 // Output tensor auto initialization if not yet initialized
451 auto_init_if_empty(*_fused_bias->info(), *_bn_mean->info()->clone());
452 _fused_bias->info()->set_valid_region(bn_mean->info()->valid_region());
453 }
454
455 // Validate arguments
Manuel Bottini11091762019-06-17 12:04:40 +0100456 ARM_COMPUTE_ERROR_THROW_ON(validate_arguments(input_weights->info(), bn_mean->info(), bn_var->info(),
giuros0115ecc9a2018-12-06 10:47:34 +0000457 (fused_weights != nullptr) ? fused_weights->info() : nullptr,
458 (fused_bias != nullptr) ? fused_bias->info() : nullptr,
Manuel Bottini11091762019-06-17 12:04:40 +0100459 (input_bias != nullptr) ? input_bias->info() : nullptr,
giuros0115ecc9a2018-12-06 10:47:34 +0000460 (bn_beta != nullptr) ? bn_beta->info() : nullptr,
461 (bn_gamma != nullptr) ? bn_gamma->info() : nullptr,
Manuel Bottini11091762019-06-17 12:04:40 +0100462 epsilon, fbn_type));
giuros0115ecc9a2018-12-06 10:47:34 +0000463
464 // Configure kernel window
Manuel Bottini11091762019-06-17 12:04:40 +0100465 Window win = calculate_max_window(*input_weights->info());
giuros0115ecc9a2018-12-06 10:47:34 +0000466 INEKernel::configure(win);
467
Manuel Bottini11091762019-06-17 12:04:40 +0100468 // Configure function
469 static std::map<std::string, FuseBatchNormFunction *> map_function =
giuros0115ecc9a2018-12-06 10:47:34 +0000470 {
Manuel Bottini11091762019-06-17 12:04:40 +0100471 { "fused_batch_normalization_conv_NHWC_F32", &fused_batch_normalization_conv<wrapper::traits::neon_vector<float, 4>> },
472 { "fused_batch_normalization_conv_NCHW_F32", &fused_batch_normalization_conv<wrapper::traits::neon_vector<float, 4>> },
473 { "fused_batch_normalization_dwc_NHWC_F32", &fused_batch_normalization_dwc_nhwc<wrapper::traits::neon_vector<float, 4>> },
474 { "fused_batch_normalization_dwc_NCHW_F32", &fused_batch_normalization_dwc_nchw<wrapper::traits::neon_vector<float, 4>> },
giuros0115ecc9a2018-12-06 10:47:34 +0000475#ifdef __ARM_FEATURE_FP16_VECTOR_ARITHMETIC
Manuel Bottini11091762019-06-17 12:04:40 +0100476 { "fused_batch_normalization_conv_NHWC_F16", &fused_batch_normalization_conv<wrapper::traits::neon_vector<float16_t, 8>> },
477 { "fused_batch_normalization_conv_NCHW_F16", &fused_batch_normalization_conv<wrapper::traits::neon_vector<float16_t, 8>> },
478 { "fused_batch_normalization_dwc_NHWC_F16", &fused_batch_normalization_dwc_nhwc<wrapper::traits::neon_vector<float16_t, 8>> },
479 { "fused_batch_normalization_dwc_NCHW_F16", &fused_batch_normalization_dwc_nchw<wrapper::traits::neon_vector<float16_t, 8>> },
480#endif /* __ARM_FEATURE_FP16_VECTOR_ARITHMETIC */
481 };
482
483 std::string function_to_call("fused_batch_normalization_");
484 function_to_call += fbn_type == FuseBatchNormalizationType::CONVOLUTION ? "conv_" : "dwc_";
485 function_to_call += string_from_data_layout(_input_weights->info()->data_layout());
486 function_to_call += "_";
487 function_to_call += string_from_data_type(_input_weights->info()->data_type());
488
489 auto it = map_function.find(function_to_call);
490
491 if(it != map_function.end())
492 {
493 _func = it->second;
giuros0115ecc9a2018-12-06 10:47:34 +0000494 }
495}
496
Manuel Bottini11091762019-06-17 12:04:40 +0100497Status NEFuseBatchNormalizationKernel::validate(const ITensorInfo *input_weights, const ITensorInfo *bn_mean, const ITensorInfo *bn_var,
giuros0115ecc9a2018-12-06 10:47:34 +0000498 const ITensorInfo *fused_weights, const ITensorInfo *fused_bias,
Manuel Bottini11091762019-06-17 12:04:40 +0100499 const ITensorInfo *input_bias, const ITensorInfo *bn_beta, const ITensorInfo *bn_gamma,
500 float epsilon, FuseBatchNormalizationType fbn_type)
giuros0115ecc9a2018-12-06 10:47:34 +0000501{
Manuel Bottini11091762019-06-17 12:04:40 +0100502 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 +0000503 return Status{};
504}
505
506void NEFuseBatchNormalizationKernel::run(const Window &window, const ThreadInfo &info)
507{
508 ARM_COMPUTE_UNUSED(info);
509 ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this);
510 ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(IKernel::window(), window);
Manuel Bottini11091762019-06-17 12:04:40 +0100511 (*_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 +0000512}
513} // namespace arm_compute