Compute Library
 21.02
fp32.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2020-2021 Arm Limited.
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  */
27 #include "src/core/NEON/NEMath.h"
31 
32 #include <arm_neon.h>
33 #include <cmath>
34 #include <cstddef>
35 
36 namespace arm_compute
37 {
38 namespace
39 {
40 using BatchNomalizationPtr = void (*)(ITensor *src, ITensor *dst, const ITensor *mean, const ITensor *var, const ITensor *beta, const ITensor *gamma,
41  float epsilon, ActivationLayerInfo &act_info, const Window &window);
42 
43 template <typename T>
44 void batch_normalization(ITensor *src, ITensor *dst, const ITensor *mean, const ITensor *var, const ITensor *beta, const ITensor *gamma,
45  float epsilon, ActivationLayerInfo &act_info, const Window &window)
46 {
47  /** Neon vector tag type. */
48  using ExactTagType = typename wrapper::traits::neon_bitvector_tag_t<float, wrapper::traits::BitWidth::W128>;
49 
50  const int window_step_x = 4;
51  const auto window_start_x = static_cast<int>(window.x().start());
52  const auto window_end_x = static_cast<int>(window.x().end());
53 
54  Window win_collapsed = window.collapse_if_possible(window, Window::DimZ);
55  win_collapsed.set(Window::DimX, Window::Dimension(0, 1, 1));
56 
57  Iterator input(src, win_collapsed);
58  Iterator output(dst, win_collapsed);
59 
60  const auto input_mean = reinterpret_cast<const float *>(mean->ptr_to_element(Coordinates(0, 0)));
61  const auto input_var = reinterpret_cast<const float *>(var->ptr_to_element(Coordinates(0, 0)));
62  const auto input_gamma = (gamma != nullptr) ? reinterpret_cast<const float *>(gamma->ptr_to_element(Coordinates(0, 0))) : nullptr;
63  const auto input_beta = (beta != nullptr) ? reinterpret_cast<const float *>(beta->ptr_to_element(Coordinates(0, 0))) : nullptr;
64 
65  T activation_functor(act_info);
66 
67  const auto epsilon_vec = wrapper::vdup_n(static_cast<float>(epsilon), ExactTagType{});
68  execute_window_loop(win_collapsed, [&](const Coordinates &)
69  {
70  const auto input_ptr = reinterpret_cast<const float *>(input.ptr());
71  const auto output_ptr = reinterpret_cast<float *>(output.ptr());
72 
73  // Perform core calculations using vector operations
74  int x = window_start_x;
75  for(; x <= (window_end_x - window_step_x); x += window_step_x)
76  {
77  // Conctruct vectors
78  const auto mean_vec = wrapper::vloadq(input_mean + x);
79  const auto var_vec = wrapper::vloadq(input_var + x);
80  const auto gamma_vec = (input_gamma != nullptr) ? wrapper::vloadq(input_gamma + x) : wrapper::vdup_n(static_cast<float>(1.f), ExactTagType{});
81  const auto beta_vec = (input_beta != nullptr) ? wrapper::vloadq(input_beta + x) : wrapper::vdup_n(static_cast<float>(0.f), ExactTagType{});
82 
83  // Calculate denominator
84  const auto denominator = wrapper::vinvsqrt(wrapper::vadd(var_vec, epsilon_vec));
85 
86  // Calculate x bar
87  const auto numerator = wrapper::vsub(wrapper::vloadq(input_ptr + x), mean_vec);
88  const auto x_bar = wrapper::vmul(numerator, denominator);
89  auto res = wrapper::vmla(beta_vec, x_bar, gamma_vec);
90 
91  // Perform fused activation
92  if(act_info.enabled())
93  {
94  activation_functor(res);
95  }
96 
97  // Store results
98  wrapper::vstore(output_ptr + x, res);
99  }
100 
101  // Compute left-over elements
102  for(; x < window_end_x; ++x)
103  {
104  // Conctruct vectors
105  const float gamma = (input_gamma != nullptr) ? input_gamma[x] : 1.f;
106  const float beta = (input_beta != nullptr) ? input_beta[x] : 0.f;
107 
108  const float denominator = sqrt(input_var[x] + epsilon);
109  const float numerator = input_ptr[x] - input_mean[x];
110  const float x_bar = numerator / denominator;
111  float res = beta + x_bar * gamma;
112 
113  // Perform fused activation
114  if(act_info.enabled())
115  {
116  activation_functor(res);
117  }
118 
119  // Store results
120  *reinterpret_cast<float *>(output_ptr + x) = res;
121  }
122  },
123  input, output);
124 }
125 
126 // Fused Batched Normalization with activation functions
127 static std::map<ActivationLayerInfo::ActivationFunction, BatchNomalizationPtr> fused_map =
128 {
129  { ActivationLayerInfo::ActivationFunction::RELU, &batch_normalization<detail::relu<float, 4>> },
130  { ActivationLayerInfo::ActivationFunction::BOUNDED_RELU, &batch_normalization<detail::brelu<float, 4>> },
131  { ActivationLayerInfo::ActivationFunction::LU_BOUNDED_RELU, &batch_normalization<detail::lubrelu<float, 4>> }
132 };
133 }
134 namespace cpu
135 {
136 void fp32_neon_batch_normalization(ITensor *src, ITensor *dst, const ITensor *mean, const ITensor *var, const ITensor *beta, const ITensor *gamma,
137  float epsilon, ActivationLayerInfo &act_info, const Window &window)
138 {
139  if(act_info.enabled())
140  {
141  fused_map[act_info.activation()](src, dst, mean, var, beta, gamma, epsilon, act_info, window);
142  }
143  else
144  {
145  batch_normalization<detail::dummy<float, 4>>(src, dst, mean, var, beta, gamma, epsilon, act_info, window);
146  }
147 }
148 } // namespace cpu
149 } // namespace arm_compute
bool enabled() const
Check if initialised.
Definition: Types.h:1600
float32x2_t vinvsqrt(const float32x2_t &a)
Definition: invsqrt.h:47
uint8x16_t vloadq(const uint8_t *ptr)
Definition: load.h:58
uint8x8_t vadd(const uint8x8_t &a, const uint8x8_t &b)
Definition: add.h:39
uint8x8_t vsub(const uint8x8_t &a, const uint8x8_t &b)
Definition: sub.h:39
Activation Layer Information class.
Definition: Types.h:1550
Interface for Neon tensor.
Definition: ITensor.h:36
SimpleTensor< float > src
Definition: DFT.cpp:155
Copyright (c) 2017-2021 Arm Limited.
static constexpr size_t DimX
Alias for dimension 0 also known as X dimension.
Definition: Window.h:43
void fp32_neon_batch_normalization(ITensor *src, ITensor *dst, const ITensor *mean, const ITensor *var, const ITensor *beta, const ITensor *gamma, float epsilon, ActivationLayerInfo &act_info, const Window &window)
Definition: fp32.cpp:136
uint8x8_t vmul(const uint8x8_t &a, const uint8x8_t &b)
Definition: mul.h:39
static constexpr size_t DimZ
Alias for dimension 2 also known as Z dimension.
Definition: Window.h:47
void vstore(uint8_t *ptr, uint8x8_t val)
Definition: store.h:39
uint8x8_t vdup_n(uint8_t value, traits::vector_64_tag)
Definition: dup_n.h:41
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...
Definition: Helpers.inl:77
ActivationFunction activation() const
Get the type of activation function.
Definition: Types.h:1585
Includes all wrapper headers at once.
uint8x8_t vmla(const uint8x8_t &a, const uint8x8_t &b, const uint8x8_t &c)
Definition: mla.h:46
Describe a multidimensional execution window.
Definition: Window.h:39