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"
30 
31 #include <arm_neon.h>
32 #include <cmath>
33 #include <cstddef>
34 
35 namespace arm_compute
36 {
37 namespace cpu
38 {
39 namespace
40 {
41 #ifndef __aarch64__
42 inline float32x4_t mask_float_vector(const float32x4_t &in, const uint32x4_t &mask)
43 {
44  auto int_in = vreinterpretq_u32_f32(in);
45  return vreinterpretq_f32_u32(wrapper::vand(int_in, mask));
46 }
47 #endif /* __aarch64__ */
48 } // namespace
49 
50 void fp32_neon_activation(const ITensor *src, ITensor *dst, const ActivationLayerInfo &act_info, const Window &window)
51 {
52  /** Neon vector tag type. */
54 
55  constexpr int window_step_x = 4;
56  const auto window_start_x = static_cast<int>(window.x().start());
57  const auto window_end_x = static_cast<int>(window.x().end());
59 
60  Window win_collapsed = window.collapse_if_possible(window, Window::DimZ);
61  win_collapsed.set(Window::DimX, Window::Dimension(0, 1, 1));
62 
63  Iterator input(src, win_collapsed);
64  Iterator output(dst, win_collapsed);
65 
66  // In case of non-aarch64, a small delta value is added to the input
67  // to prevent NAN values caused by zeros in inputs to SQRT.
68  // In case of aarh64, we call vsqrt directly, so we don't use delta.
69 #ifndef __aarch64__
70  const auto delta = wrapper::vdup_n(static_cast<float>(1e-24), ExactTagType {});
71 #endif /* __aarch64__ */
72  const auto const_1 = wrapper::vdup_n(static_cast<float>(1.f), ExactTagType {});
73  const auto const_0 = wrapper::vdup_n(static_cast<float>(0.f), ExactTagType{});
74  const auto const_6 = wrapper::vdup_n(static_cast<float>(6.f), ExactTagType{});
75  const auto const_3 = wrapper::vdup_n(static_cast<float>(3.f), ExactTagType{});
76  const auto const_inv_6 = wrapper::vdup_n(static_cast<float>(0.166666667f), ExactTagType{});
77 
78  constexpr float soft_relu_thresh = 12.f;
79  const auto vsoft_relu_thresh = wrapper::vdup_n(static_cast<float>(soft_relu_thresh), ExactTagType{});
80 
81  const auto va = wrapper::vdup_n(static_cast<float>(act_info.a()), ExactTagType{});
82  const auto vb = wrapper::vdup_n(static_cast<float>(act_info.b()), ExactTagType{});
83  const auto a = static_cast<float>(act_info.a());
84  const auto b = static_cast<float>(act_info.b());
85  execute_window_loop(win_collapsed, [&](const Coordinates &)
86  {
87  const auto input_ptr = reinterpret_cast<const float *>(input.ptr());
88  const auto output_ptr = reinterpret_cast<float *>(output.ptr());
89 
91 
92  // Compute S elements per iteration
93  int x = window_start_x;
94  for(; x <= (window_end_x - window_step_x); x += window_step_x)
95  {
96  const auto vin = wrapper::vloadq(input_ptr + x);
97  switch(act)
98  {
100  tmp = wrapper::vabs(vin);
101  break;
103  tmp = wrapper::vmla(vb, va, vin);
104  break;
107  break;
109  tmp = wrapper::vmax(const_0, vin);
110  break;
112  tmp = wrapper::vmin(va, wrapper::vmax(const_0, vin));
113  break;
115  tmp = wrapper::vmin(va, wrapper::vmax(vb, vin));
116  break;
118  tmp = wrapper::vbsl(wrapper::vcgt(vin, const_0), vin, wrapper::vmul(va, vin));
119  break;
121  tmp = wrapper::vbsl(wrapper::vcgt(vin, vsoft_relu_thresh), vin, wrapper::vlog(wrapper::vadd(const_1, wrapper::vexpq(vin))));
122  break;
124  tmp = wrapper::vbsl(wrapper::vcge(vin, const_0), vin, wrapper::vmul(va, wrapper::vsub(wrapper::vexpq(vin), const_1)));
125  break;
127 #ifdef __aarch64__
128  tmp = wrapper::vsqrt(vin);
129 #else /* __aarch64__ */
130  {
131  const auto bitmask = wrapper::vceq(vin, wrapper::vdup_n(0.f, ExactTagType{}));
132  tmp = wrapper::vinv(wrapper::vinvsqrt(wrapper::vadd(vin, mask_float_vector(delta, bitmask))));
133  tmp = mask_float_vector(tmp, wrapper::vnot(bitmask));
134  }
135 #endif /* __aarch64__ */
136  break;
138  tmp = wrapper::vmul(vin, vin);
139  break;
141  tmp = wrapper::vmul(va, wrapper::vtanh(wrapper::vmul(vb, vin)));
142  break;
144  tmp = vin;
145  break;
147  tmp = wrapper::vmul(vin, wrapper::vmul(const_inv_6, wrapper::vmin(const_6, wrapper::vmax(const_0, wrapper::vadd(vin, const_3)))));
148  break;
149  default:
150  ARM_COMPUTE_ERROR("Unsupported activation function");
151  }
152  wrapper::vstore(output_ptr + x, tmp);
153  }
154 
155  // Compute left-over elements
156  for(; x < window_end_x; ++x)
157  {
158  const float in = *(reinterpret_cast<const float *>(input_ptr + x));
159  float tmp;
160  switch(act)
161  {
163  tmp = std::abs(in);
164  break;
166  tmp = a * in + b;
167  break;
169  tmp = static_cast<float>(1) / (static_cast<float>(1) + std::exp(-in));
170  break;
172  tmp = std::max<float>(static_cast<float>(0), in);
173  break;
175  tmp = std::min<float>(a, std::max(static_cast<float>(0), in));
176  break;
178  tmp = std::min<float>(a, std::max<float>(b, in));
179  break;
181  tmp = (in > 0) ? in : a * in;
182  break;
184  tmp = (in > soft_relu_thresh) ? in : std::log(static_cast<float>(1) + std::exp(in));
185  break;
187  tmp = (in >= 0) ? in : a * (std::exp(in) - 1);
188  break;
190  tmp = std::sqrt(in);
191  break;
193  tmp = in * in;
194  break;
196  tmp = a * std::tanh(b * in);
197  break;
199  tmp = in;
200  break;
202  tmp = in * ((std::min(std::max((in + 3), 0.0f), 6.0f)) * 0.166666667f);
203  break;
204  default:
205  ARM_COMPUTE_ERROR("Unsupported activation function");
206  }
207  *(output_ptr + x) = tmp;
208  }
209  },
210  input, output);
211 }
212 } // namespace cpu
213 } // namespace arm_compute
float32x4_t vlog(const float32x4_t &a)
Definition: log.h:47
float32x4_t vtanh(const float32x4_t &a)
Definition: tanh.h:40
SimpleTensor< float > b
Definition: DFT.cpp:157
#define ARM_COMPUTE_ERROR(msg)
Print the given message then throw an std::runtime_error.
Definition: Error.h:352
float a() const
Get the alpha value.
Definition: Types.h:1590
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
float32x2_t vinv(const float32x2_t &a)
Definition: inv.h:47
int8x8_t vabs(const int8x8_t &a)
Definition: abs.h:46
uint8x8_t vsub(const uint8x8_t &a, const uint8x8_t &b)
Definition: sub.h:39
Describe one of the image&#39;s dimensions with a start, end and step.
Definition: Window.h:77
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.
ActivationFunction
Available activation functions.
Definition: Types.h:1554
typename neon_bitvector< T, BW >::tag_type neon_bitvector_tag_t
Helper type template to get the tag type of a neon vector.
Definition: traits.h:132
static constexpr size_t DimX
Alias for dimension 0 also known as X dimension.
Definition: Window.h:43
uint8x8_t vnot(const uint8x8_t &a)
Definition: not.h:39
Window collapse_if_possible(const Window &full_window, size_t first, size_t last, bool *has_collapsed=nullptr) const
Collapse the dimensions between first and last if possible.
Definition: Window.inl:68
uint8x8_t vmin(const uint8x8_t &a, const uint8x8_t &b)
Definition: min.h:39
Coordinates of an item.
Definition: Coordinates.h:37
uint8x8_t vand(const uint8x8_t &a, const uint8x8_t &b)
Definition: and.h:39
void fp32_neon_activation(const ITensor *src, ITensor *dst, const ActivationLayerInfo &act_info, const Window &window)
Definition: fp32.cpp:50
constexpr uint8_t * ptr() const
Return a pointer to the current pixel.
Definition: Helpers.inl:139
void set(size_t dimension, const Dimension &dim)
Set the values of a given dimension.
Definition: Window.inl:49
int8x8_t vneg(const int8x8_t &a)
Definition: neg.h:39
uint8x8_t vcgt(const uint8x8_t &a, const uint8x8_t &b)
Definition: cgt.h:39
uint8x8_t vmul(const uint8x8_t &a, const uint8x8_t &b)
Definition: mul.h:39
uint8x8_t vbsl(const uint8x8_t &a, const uint8x8_t &b, const uint8x8_t &c)
Definition: bsl.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
typename neon_bitvector< T, BW >::type neon_bitvector_t
Helper type template to get the type of a neon vector.
Definition: traits.h:130
ActivationFunction activation() const
Get the type of activation function.
Definition: Types.h:1585
float b() const
Get the beta value.
Definition: Types.h:1595
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
constexpr int end() const
Return the end of the dimension.
Definition: Window.h:99
uint8x8_t vcge(const uint8x8_t &a, const uint8x8_t &b)
Definition: cge.h:39
Iterator updated by execute_window_loop for each window element.
Definition: Helpers.h:46
uint8x8_t vmax(const uint8x8_t &a, const uint8x8_t &b)
Definition: max.h:39
constexpr int start() const
Return the start of the dimension.
Definition: Window.h:94
float32x4_t vexpq(const float32x4_t &a)
Definition: exp.h:47
Describe a multidimensional execution window.
Definition: Window.h:39
uint8x8_t vceq(const uint8x8_t &a, const uint8x8_t &b)
Definition: ceq.h:39
constexpr const Dimension & x() const
Alias to access the first dimension of the window.
Definition: Window.h:145