43 Status
validate_arguments(
const ITensorInfo *input_weights,
const ITensorInfo *bn_mean,
const ITensorInfo *bn_var,
44 const ITensorInfo *fused_weights,
const ITensorInfo *fused_bias,
45 const ITensorInfo *input_bias,
const ITensorInfo *bn_beta,
const ITensorInfo *bn_gamma,
67 if(input_bias !=
nullptr)
73 if(bn_beta !=
nullptr)
79 if(bn_gamma !=
nullptr)
86 if(fused_weights !=
nullptr && fused_weights->total_size() != 0)
93 if(fused_bias !=
nullptr && fused_bias->total_size() != 0)
102 template <
typename VectorType>
103 void fused_batch_normalization_conv(
const ITensor *conv_weights,
const ITensor *conv_bias, ITensor *fused_weights, ITensor *fused_bias,
104 const ITensor *bn_mean,
const ITensor *bn_var,
const ITensor *bn_beta,
const ITensor *bn_gamma,
float epsilon,
const Window &window)
106 using ScalarType =
typename VectorType::scalar_type;
107 const int size = 16 / conv_weights->info()->element_size();
108 using ExactTagType =
typename VectorType::tag_type;
110 const bool run_in_place_weights = (fused_weights ==
nullptr) || (fused_weights == conv_weights);
111 const bool run_in_place_bias = (fused_bias ==
nullptr) || (conv_bias !=
nullptr && fused_bias == conv_bias);
117 const int window_step_x = size;
118 const auto window_start_x =
static_cast<int>(window.x().start());
119 const auto window_end_x =
static_cast<int>(window.x().end());
121 Iterator conv_w_in(conv_weights, win);
122 Iterator conv_w_out(run_in_place_weights ? conv_weights : fused_weights, win);
124 const auto conv_bias_in = (conv_bias !=
nullptr ?
reinterpret_cast<ScalarType *
>(conv_bias->ptr_to_element(Coordinates(0, 0))) :
nullptr);
125 auto conv_bias_out = (run_in_place_bias ? conv_bias_in :
reinterpret_cast<ScalarType *
>(fused_bias->ptr_to_element(Coordinates(0, 0))));
127 const auto input_mean =
reinterpret_cast<const ScalarType *
>(bn_mean->ptr_to_element(Coordinates(0, 0)));
128 const auto input_var =
reinterpret_cast<const ScalarType *
>(bn_var->ptr_to_element(Coordinates(0, 0)));
129 const auto input_gamma = (bn_gamma !=
nullptr) ? reinterpret_cast<const ScalarType *>(bn_gamma->ptr_to_element(Coordinates(0, 0))) :
nullptr;
130 const auto input_beta = (bn_beta !=
nullptr) ? reinterpret_cast<const ScalarType *>(bn_beta->ptr_to_element(Coordinates(0, 0))) :
nullptr;
137 const auto epsilon_vec =
wrapper::vdup_n(ScalarType(epsilon), ExactTagType{});
139 auto mean = ScalarType(0.0);
140 auto var = ScalarType(0.0);
141 auto gamma = ScalarType(1.0);
142 auto beta = ScalarType(0.0);
143 auto conv_bias_in_scalar = ScalarType(0.0);
146 var = input_var[
id[3]];
147 if(input_gamma !=
nullptr)
149 gamma = input_gamma[
id[3]];
152 if((
id[0] == 0) && (
id[1] == 0) && (
id[2] == 0))
154 if(input_beta !=
nullptr)
156 beta = input_beta[
id[3]];
161 mean = input_mean[
id[3]];
164 if(conv_bias_in !=
nullptr)
166 conv_bias_in_scalar = conv_bias_in[
id[3]];
168 auto conv_bias_tmp_scalar = (conv_bias_in_scalar - mean) / std::sqrt(var + ScalarType(epsilon));
169 conv_bias_out[
id[3]] = (conv_bias_tmp_scalar * gamma) + beta;
172 int x = window_start_x;
173 auto conv_w_in_ptr =
reinterpret_cast<const ScalarType *
>(conv_w_in.ptr());
174 auto conv_w_out_ptr =
reinterpret_cast<ScalarType *
>(conv_w_out.ptr());
179 for(; x <= (window_end_x - window_step_x); x += window_step_x)
190 for(; x < window_end_x; ++x)
192 *(conv_w_out_ptr + x) = *(conv_w_in_ptr + x) / std::sqrt(var + ScalarType(epsilon)) * gamma;
195 conv_w_in, conv_w_out);
198 template <
typename VectorType>
199 void fused_batch_normalization_dwc_nhwc(
const ITensor *dwc_weights,
const ITensor *dwc_bias, ITensor *fused_weights, ITensor *fused_bias,
200 const ITensor *bn_mean,
const ITensor *bn_var,
const ITensor *bn_beta,
const ITensor *bn_gamma,
float epsilon,
const Window &window)
202 using ScalarType =
typename VectorType::scalar_type;
203 const int size = 16 / dwc_weights->info()->element_size();
204 using ExactTagType =
typename VectorType::tag_type;
206 const bool run_in_place_weights = (fused_weights ==
nullptr) || (fused_weights == dwc_weights);
207 const bool run_in_place_bias = (fused_bias ==
nullptr) || (dwc_bias !=
nullptr && fused_bias == dwc_bias);
213 const int window_step_x = size;
214 const auto window_start_x =
static_cast<int>(window.x().start());
215 const auto window_end_x =
static_cast<int>(window.x().end());
217 Iterator dwc_w_in(dwc_weights, win);
218 Iterator dwc_w_out(run_in_place_weights ? dwc_weights : fused_weights, win);
220 const auto dwc_bias_in = (dwc_bias !=
nullptr ?
reinterpret_cast<ScalarType *
>(dwc_bias->ptr_to_element(Coordinates(0, 0))) :
nullptr);
221 auto dwc_bias_out = (run_in_place_bias ? dwc_bias_in :
reinterpret_cast<ScalarType *
>(fused_bias->ptr_to_element(Coordinates(0, 0))));
223 const auto input_mean =
reinterpret_cast<const ScalarType *
>(bn_mean->ptr_to_element(Coordinates(0, 0)));
224 const auto input_var =
reinterpret_cast<const ScalarType *
>(bn_var->ptr_to_element(Coordinates(0, 0)));
225 const auto input_gamma = (bn_gamma !=
nullptr) ? reinterpret_cast<const ScalarType *>(bn_gamma->ptr_to_element(Coordinates(0, 0))) :
nullptr;
226 const auto input_beta = (bn_beta !=
nullptr) ? reinterpret_cast<const ScalarType *>(bn_beta->ptr_to_element(Coordinates(0, 0))) :
nullptr;
234 const auto epsilon_vec =
wrapper::vdup_n(ScalarType(epsilon), ExactTagType{});
236 auto gamma = ScalarType(1.0);
237 auto beta = ScalarType(0.0);
238 auto dwc_bias_in_scalar = ScalarType(0);
242 int x = window_start_x;
243 for(; x <= (window_end_x - window_step_x); x += window_step_x)
246 if(input_gamma !=
nullptr)
251 if((
id[2] == 0) && (
id[1] == 0))
256 if(input_beta !=
nullptr)
261 if(dwc_bias_in !=
nullptr)
271 auto dwc_w_in_ptr =
reinterpret_cast<const ScalarType *
>(dwc_w_in.ptr());
272 auto dwc_w_out_ptr =
reinterpret_cast<ScalarType *
>(dwc_w_out.ptr());
284 for(; x < window_end_x; ++x)
286 auto var = input_var[x];
287 if(input_gamma !=
nullptr)
289 gamma = input_gamma[x];
292 if(
id[2] == 0 &&
id[1] == 0)
294 auto mean = input_mean[x];
295 if(input_beta !=
nullptr)
297 beta = input_beta[x];
299 if(dwc_bias_in !=
nullptr)
301 dwc_bias_in_scalar = dwc_bias_in[x];
304 auto dwc_bias_tmp_scalar = (dwc_bias_in_scalar - mean) / std::sqrt(var + ScalarType(epsilon));
305 dwc_bias_out[x] = (dwc_bias_tmp_scalar * gamma) + beta;
308 const auto dwc_w_in_ptr =
reinterpret_cast<const ScalarType *
>(dwc_w_in.ptr());
309 auto dwc_w_out_ptr =
reinterpret_cast<ScalarType *
>(dwc_w_out.ptr());
311 *(dwc_w_out_ptr + x) = *(dwc_w_in_ptr + x) / std::sqrt(var + ScalarType(epsilon)) * gamma;
314 dwc_w_in, dwc_w_out);
317 template <
typename VectorType>
318 void fused_batch_normalization_dwc_nchw(
const ITensor *dwc_weights,
const ITensor *dwc_bias, ITensor *fused_weights, ITensor *fused_bias,
319 const ITensor *bn_mean,
const ITensor *bn_var,
const ITensor *bn_beta,
const ITensor *bn_gamma,
float epsilon,
const Window &window)
321 using ScalarType =
typename VectorType::scalar_type;
322 const int size = 16 / dwc_weights->info()->element_size();
323 using ExactTagType =
typename VectorType::tag_type;
325 const bool run_in_place_weights = (fused_weights ==
nullptr) || (fused_weights == dwc_weights);
326 const bool run_in_place_bias = (fused_bias ==
nullptr) || (dwc_bias !=
nullptr && fused_bias == dwc_bias);
332 const int window_step_x = size;
333 const auto window_start_x =
static_cast<int>(window.x().start());
334 const auto window_end_x =
static_cast<int>(window.x().end());
336 Iterator dwc_w_in(dwc_weights, win);
337 Iterator dwc_w_out(run_in_place_weights ? dwc_weights : fused_weights, win);
339 const auto dwc_bias_in = (dwc_bias !=
nullptr ?
reinterpret_cast<ScalarType *
>(dwc_bias->ptr_to_element(Coordinates(0, 0))) :
nullptr);
340 auto dwc_bias_out = (run_in_place_bias ? dwc_bias_in :
reinterpret_cast<ScalarType *
>(fused_bias->ptr_to_element(Coordinates(0, 0))));
342 const auto input_mean =
reinterpret_cast<const ScalarType *
>(bn_mean->ptr_to_element(Coordinates(0, 0)));
343 const auto input_var =
reinterpret_cast<const ScalarType *
>(bn_var->ptr_to_element(Coordinates(0, 0)));
344 const auto input_gamma = (bn_gamma !=
nullptr) ? reinterpret_cast<const ScalarType *>(bn_gamma->ptr_to_element(Coordinates(0, 0))) :
nullptr;
345 const auto input_beta = (bn_beta !=
nullptr) ? reinterpret_cast<const ScalarType *>(bn_beta->ptr_to_element(Coordinates(0, 0))) :
nullptr;
352 const auto epsilon_vec =
wrapper::vdup_n(ScalarType(epsilon), ExactTagType{});
354 auto mean = ScalarType(0.0);
355 auto var = ScalarType(0.0);
356 auto gamma = ScalarType(1.0);
357 auto beta = ScalarType(0.0);
358 auto dwc_bias_in_scalar = ScalarType(0.0);
361 var = input_var[
id[2]];
362 if(input_gamma !=
nullptr)
364 gamma = input_gamma[
id[2]];
369 mean = input_mean[
id[2]];
373 if(input_beta !=
nullptr)
375 beta = input_beta[
id[2]];
379 if(dwc_bias_in !=
nullptr)
381 dwc_bias_in_scalar = dwc_bias_in[
id[2]];
384 auto dwc_bias_tmp_scalar = (dwc_bias_in_scalar - mean) / std::sqrt(var + ScalarType(epsilon));
385 dwc_bias_out[
id[2]] = (dwc_bias_tmp_scalar * gamma) + beta;
388 int x = window_start_x;
389 auto dwc_w_in_ptr =
reinterpret_cast<const ScalarType *
>(dwc_w_in.ptr());
390 auto dwc_w_out_ptr =
reinterpret_cast<ScalarType *
>(dwc_w_out.ptr());
395 for(; x <= (window_end_x - window_step_x); x += window_step_x)
406 for(; x < window_end_x; ++x)
408 *(dwc_w_out_ptr + x) = *(dwc_w_in_ptr + x) / std::sqrt(var + ScalarType(epsilon)) * gamma;
411 dwc_w_in, dwc_w_out);
417 : _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(),
418 _run_in_place_weights(false), _run_in_place_bias(false), _func(nullptr)
429 _input_weights = input_weights;
430 _input_bias = input_bias;
434 _bn_gamma = bn_gamma;
435 _fused_weights = fused_weights;
436 _fused_bias = fused_bias;
439 _run_in_place_weights = (fused_weights ==
nullptr) || (fused_weights == input_weights);
440 _run_in_place_bias = (fused_bias ==
nullptr) || (input_bias !=
nullptr && fused_bias == input_bias);
443 if(_fused_weights !=
nullptr)
449 if(_fused_bias !=
nullptr)
458 (fused_weights !=
nullptr) ? fused_weights->
info() :
nullptr,
459 (fused_bias !=
nullptr) ? fused_bias->
info() :
nullptr,
460 (input_bias !=
nullptr) ? input_bias->
info() :
nullptr,
461 (bn_beta !=
nullptr) ? bn_beta->
info() :
nullptr,
462 (bn_gamma !=
nullptr) ? bn_gamma->
info() :
nullptr,
467 INEKernel::configure(win);
470 static std::map<std::string, FuseBatchNormFunction *> map_function =
472 {
"fused_batch_normalization_conv_NHWC_F32", &fused_batch_normalization_conv<wrapper::traits::neon_vector<float, 4>> },
473 {
"fused_batch_normalization_conv_NCHW_F32", &fused_batch_normalization_conv<wrapper::traits::neon_vector<float, 4>> },
474 {
"fused_batch_normalization_dwc_NHWC_F32", &fused_batch_normalization_dwc_nhwc<wrapper::traits::neon_vector<float, 4>> },
475 {
"fused_batch_normalization_dwc_NCHW_F32", &fused_batch_normalization_dwc_nchw<wrapper::traits::neon_vector<float, 4>> },
476 #ifdef __ARM_FEATURE_FP16_VECTOR_ARITHMETIC 477 {
"fused_batch_normalization_conv_NHWC_F16", &fused_batch_normalization_conv<wrapper::traits::neon_vector<float16_t, 8>> },
478 {
"fused_batch_normalization_conv_NCHW_F16", &fused_batch_normalization_conv<wrapper::traits::neon_vector<float16_t, 8>> },
479 {
"fused_batch_normalization_dwc_NHWC_F16", &fused_batch_normalization_dwc_nhwc<wrapper::traits::neon_vector<float16_t, 8>> },
480 {
"fused_batch_normalization_dwc_NCHW_F16", &fused_batch_normalization_dwc_nchw<wrapper::traits::neon_vector<float16_t, 8>> },
484 std::string function_to_call(
"fused_batch_normalization_");
487 function_to_call +=
"_";
490 auto it = map_function.find(function_to_call);
492 if(it != map_function.end())
512 (*_func)(_input_weights, _input_bias, _fused_weights, _fused_bias, _bn_mean, _bn_var, _bn_beta, _bn_gamma, _epsilon,
window);
static Status validate(const ITensorInfo *input_weights, const ITensorInfo *bn_mean, const ITensorInfo *bn_var, const ITensorInfo *fused_weights, const ITensorInfo *fused_bias, const ITensorInfo *input_bias=nullptr, const ITensorInfo *bn_beta=nullptr, const ITensorInfo *bn_gamma=nullptr, float epsilon=0.001f, FuseBatchNormalizationType fbn_type=FuseBatchNormalizationType::CONVOLUTION)
Static function to check if given info will lead to a valid configuration of NEFuseBatchNormalization...
Window calculate_max_window(const ValidRegion &valid_region, const Steps &steps, bool skip_border, BorderSize border_size)
const Window & window() const
The maximum window the kernel can be executed on.
#define ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_LAYOUT(...)
#define ARM_COMPUTE_RETURN_ERROR_ON_CPU_F16_UNSUPPORTED(tensor)
float32x2_t vinvsqrt(const float32x2_t &a)
uint8x16_t vloadq(const uint8_t *ptr)
#define ARM_COMPUTE_RETURN_ON_ERROR(status)
Checks if a status contains an error and returns it.
virtual DataType data_type() const =0
Data type used for each element of the tensor.
uint8x8_t vadd(const uint8x8_t &a, const uint8x8_t &b)
1 channel, 1 F32 per channel
Store the tensor's metadata.
#define ARM_COMPUTE_ERROR_THROW_ON(status)
uint8x8_t vsub(const uint8x8_t &a, const uint8x8_t &b)
#define ARM_COMPUTE_RETURN_ERROR_ON(cond)
If the condition is true, an error is returned.
Interface for Neon tensor.
Copyright (c) 2017-2021 Arm Limited.
virtual void set_valid_region(const ValidRegion &valid_region)=0
Set the valid region of the tensor.
1 channel, 1 F16 per channel
virtual ValidRegion valid_region() const =0
Valid region of the tensor.
void run(const Window &window, const ThreadInfo &info) override
Execute the kernel on the passed window.
#define ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(...)
FuseBatchNormalizationType
Available FuseBatchNormalizationType.
const std::string & string_from_data_type(DataType dt)
Convert a data type identity into a string.
static constexpr size_t DimX
Alias for dimension 0 also known as X dimension.
#define ARM_COMPUTE_UNUSED(...)
To avoid unused variables warnings.
void configure(const ITensor *input_weights, const ITensor *bn_mean, const ITensor *bn_var, ITensor *fused_weights, ITensor *fused_bias, const ITensor *input_bias=nullptr, const ITensor *bn_beta=nullptr, const ITensor *bn_gamma=nullptr, float epsilon=0.001f, FuseBatchNormalizationType fbn_type=FuseBatchNormalizationType::CONVOLUTION)
Set the source, destination of the kernel.
bool auto_init_if_empty(ITensorInfo &info, const TensorShape &shape, int num_channels, DataType data_type, QuantizationInfo quantization_info=QuantizationInfo())
Auto initialize the tensor info (shape, number of channels and data type) if the current assignment i...
virtual std::unique_ptr< T > clone() const =0
Provide a clone of the current object of class T.
virtual ITensorInfo * info() const =0
Interface to be implemented by the child class to return the tensor's metadata.
#define ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(k)
ScaleKernelInfo info(interpolation_policy, default_border_mode, PixelValue(), sampling_policy, false)
uint8x8_t vmul(const uint8x8_t &a, const uint8x8_t &b)
const std::string & string_from_data_layout(DataLayout dl)
Convert a data layout identity into a string.
Information about executing thread and CPU.
#define ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_SHAPES(...)
NEFuseBatchNormalizationKernel()
Default constructor.
#define ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(...)
#define ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(t, c,...)
Status validate_arguments(const ITensorInfo *input, const ITensorInfo *bias, const ITensorInfo *output, const GEMMLowpOutputStageInfo *output_stage)
void vstore(uint8_t *ptr, uint8x8_t val)
#define ARM_COMPUTE_ERROR_ON_NULLPTR(...)
uint8x8_t vdup_n(uint8_t value, traits::vector_64_tag)
void execute_window_loop(const Window &w, L &&lambda_function, Ts &&... iterators)
Iterate through the passed window, automatically adjusting the iterators and calling the lambda_funct...
Includes all wrapper headers at once.
size_t get_data_layout_dimension_index(const DataLayout data_layout, const DataLayoutDimension data_layout_dimension)
Get the index of the given dimension.
Describe a multidimensional execution window.
#define ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(f, s)
virtual DataLayout data_layout() const =0
Get the data layout of the tensor.