Compute Library
 22.11
fp32.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2020-2022 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  */
24 
28 #include "src/core/NEON/SVEMath.h"
29 
30 #include <cmath>
31 #include <cstddef>
32 
33 #include <arm_sve.h>
34 
35 namespace arm_compute
36 {
37 namespace cpu
38 {
39 void sve_fp32_activation(const ITensor *src, ITensor *dst, const ActivationLayerInfo &act_info, const Window &window)
40 {
41  const auto window_start_x = static_cast<int>(window.x().start());
42  const auto window_end_x = static_cast<int>(window.x().end());
44 
45  Window win_collapsed = window.collapse_if_possible(window, Window::DimZ);
46  win_collapsed.set(Window::DimX, Window::Dimension(0, 1, 1));
47 
48  Iterator input(src, win_collapsed);
49  Iterator output(dst, win_collapsed);
50 
51  const auto const_1 = svdup_n_f32(1.f);
52  const auto const_0 = svdup_n_f32(0.f);
53  const auto const_6 = svdup_n_f32(6.f);
54  const auto const_3 = svdup_n_f32(3.f);
55  const auto const_inv_6 = svdup_n_f32(0.166666667f);
56  const auto soft_relu_thresh = svdup_n_f32(16.63553047f);
57 
58  const auto va = svdup_n_f32(act_info.a());
59  const auto vb = svdup_n_f32(act_info.b());
60  execute_window_loop(win_collapsed, [&](const Coordinates &)
61  {
62  const auto input_ptr = reinterpret_cast<const float *>(input.ptr());
63  const auto output_ptr = reinterpret_cast<float *>(output.ptr());
64 
65  svfloat32_t tmp;
66 
67  // Compute S elements per iteration
68  int x = window_start_x;
69  svbool_t pg = svwhilelt_b32(x, window_end_x);
70  do
71  {
72  const auto vin = svld1_f32(pg, input_ptr + x);
73  switch(act)
74  {
76  tmp = svabs_f32_z(pg, vin);
77  break;
79  tmp = svmla_f32_z(pg, vb, va, vin);
80  break;
82  tmp = svinv_f32_z(pg, svadd_f32_z(pg, const_1, svexp_f32_z(pg, svneg_f32_z(pg, vin))));
83  break;
85  tmp = svmax_f32_z(pg, const_0, vin);
86  break;
88  tmp = svmin_f32_z(pg, va, svmax_f32_z(pg, const_0, vin));
89  break;
91  tmp = svmin_f32_z(pg, va, svmax_f32_z(pg, vb, vin));
92  break;
94  tmp = svadd_f32_z(pg, svmul_f32_z(pg, svmin_f32_z(pg, vin, const_0), va), svmax_f32_z(pg, vin, const_0));
95  break;
97  tmp = svsel_f32(svcmpgt_f32(pg, vin, soft_relu_thresh), vin, svlog_f32_z(pg, svadd_f32_z(pg, const_1, svexp_f32_z(pg, vin))));
98  break;
100  tmp = svsel_f32(svcmpgt_f32(pg, vin, const_0), vin, svmul_f32_z(pg, va, svsub_f32_z(pg, svexp_f32_z(pg, vin), const_1)));
101  break;
103  tmp = svsqrt_f32_z(pg, vin);
104  break;
106  tmp = svmul_f32_z(pg, vin, vin);
107  break;
109  tmp = svmul_f32_z(pg, va, svtanh_f32_z(pg, svmul_f32_z(pg, vb, vin)));
110  break;
112  tmp = vin;
113  break;
115  tmp = svmul_f32_z(pg, vin, svmul_f32_z(pg, const_inv_6, svmin_f32_z(pg, const_6, svmax_f32_z(pg, const_0, svadd_f32_z(pg, vin, const_3)))));
116  break;
118  tmp = svmul_f32_z(pg, vin, svinv_f32_z(pg, svadd_f32_z(pg, const_1, svexp_f32_z(pg, svneg_f32_z(pg, svmul_f32_z(pg, va, vin))))));
119  break;
120  default:
121  ARM_COMPUTE_ERROR("Unsupported activation function");
122  }
123  svst1_f32(pg, output_ptr + x, tmp);
124 
125  x += svcntw();
126  pg = svwhilelt_b32(x, window_end_x);
127 
128  }
129  while(svptest_any(svptrue_b32(), pg));
130  },
131  input, output);
132 }
133 } // namespace cpu
134 } // namespace arm_compute
#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:1684
Describe one of the image&#39;s dimensions with a start, end and step.
Definition: Window.h:79
Activation Layer Information class.
Definition: Types.h:1639
Interface for CPU tensor.
Definition: ITensor.h:36
SimpleTensor< float > src
Definition: DFT.cpp:155
Copyright (c) 2017-2022 Arm Limited.
ActivationFunction
Available activation functions.
Definition: Types.h:1643
static constexpr size_t DimX
Alias for dimension 0 also known as X dimension.
Definition: Window.h:43
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
void sve_fp32_activation(const ITensor *src, ITensor *dst, const ActivationLayerInfo &act_info, const Window &window)
Definition: fp32.cpp:39
Coordinates of an item.
Definition: Coordinates.h:37
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
static constexpr size_t DimZ
Alias for dimension 2 also known as Z dimension.
Definition: Window.h:47
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:1679
float b() const
Get the beta value.
Definition: Types.h:1689
constexpr int end() const
Return the end of the dimension.
Definition: Window.h:102
Iterator updated by execute_window_loop for each window element.
Definition: Helpers.h:46
constexpr int start() const
Return the start of the dimension.
Definition: Window.h:97
Describe a multidimensional execution window.
Definition: Window.h:39
constexpr const Dimension & x() const
Alias to access the first dimension of the window.
Definition: Window.h:159