Compute Library
 22.05
qsymm16.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  */
28 
29 #include <cmath>
30 #include <cstddef>
31 
32 #include "src/core/NEON/SVEMath.h"
33 #include "src/core/NEON/SVESymm.h"
34 #include <arm_sve.h>
35 
36 namespace arm_compute
37 {
38 namespace cpu
39 {
40 void sve2_qsymm16_activation(const ITensor *src, ITensor *dst, const ActivationLayerInfo &act_info, const Window &window)
41 {
42  const auto window_start_x = static_cast<int>(window.x().start());
43  const auto window_end_x = static_cast<int>(window.x().end());
45 
46  Window win_collapsed = window.collapse_if_possible(window, Window::DimZ);
47  win_collapsed.set(Window::DimX, Window::Dimension(0, 1, 1));
48 
49  Iterator input(src, win_collapsed);
50  Iterator output(dst, win_collapsed);
51 
52  const UniformQuantizationInfo qi_in = src->info()->quantization_info().uniform();
53  const UniformQuantizationInfo qi_out = dst->info()->quantization_info().uniform();
54  const auto vconst_1 = svdup_n_f32(1.f);
55  const auto va_f32 = svdup_n_f32(act_info.a());
56  const auto vb_f32 = svdup_n_f32(act_info.b());
57 
58  execute_window_loop(win_collapsed, [&](const Coordinates &)
59  {
60  const auto input_ptr = reinterpret_cast<const int16_t *>(input.ptr());
61  const auto output_ptr = reinterpret_cast<int16_t *>(output.ptr());
62 
63  svint16_t tmp;
64 
65  int x = window_start_x;
66  svbool_t pg = svwhilelt_b16(x, window_end_x);
67  do
68  {
69  const auto vin = svld1_s16(pg, input_ptr + x);
71  {
72  // De-quantize
73  auto vin_deq = svdequantize_qsymm16_z(pg, vin, qi_in.scale);
74  // Perform activation
75  const svfloat32x2_t tmp_dep = svcreate2_f32(svdiv_f32_z(pg, vconst_1, svadd_f32_z(pg, vconst_1, svexp_f32_z(pg, svneg_f32_z(pg, svget2_f32(vin_deq, 0))))),
76  svdiv_f32_z(pg, vconst_1, svadd_f32_z(pg, vconst_1, svexp_f32_z(pg, svneg_f32_z(pg, svget2_f32(vin_deq, 1))))));
77  // Re-quantize to new output space
78  tmp = svquantize_qsymm16_z(pg, tmp_dep, qi_out.scale);
79  }
81  {
82  // De-quantize
83  auto vin_deq = svdequantize_qsymm16_z(pg, vin, qi_in.scale);
84  // Perform activation
85  const svfloat32x2_t tmp_dep = svcreate2_f32(svmul_f32_z(pg, va_f32, svtanh_f32_z(pg, svmul_f32_z(pg, svget2_f32(vin_deq, 0), vb_f32))),
86  svmul_f32_z(pg, va_f32, svtanh_f32_z(pg, svmul_f32_z(pg, svget2_f32(vin_deq, 1), vb_f32))));
87  // Re-quantize to new output space
88  tmp = svquantize_qsymm16_z(pg, tmp_dep, qi_out.scale);
89  }
91  {
92  // De-quantize
93  auto vin_deq = svdequantize_qsymm16_z(pg, vin, qi_in.scale);
94  // Perform activation
95  const svfloat32x2_t tmp_dep = svcreate2_f32(svmin_f32_z(pg, va_f32, svmax_f32_z(pg, vb_f32, svget2_f32(vin_deq, 0))),
96  svmin_f32_z(pg, va_f32, svmax_f32_z(pg, vb_f32, svget2_f32(vin_deq, 1))));
97  // Re-quantize to new output space
98  tmp = svquantize_qsymm16_z(pg, tmp_dep, qi_out.scale);
99  }
100  else
101  {
102  ARM_COMPUTE_ERROR("Unsupported activation function");
103  }
104 
105  svst1_s16(pg, output_ptr + x, tmp);
106 
107  x += svcnth();
108  pg = svwhilelt_b16(x, window_end_x);
109 
110  }
111  while(svptest_any(svptrue_b16(), pg));
112  },
113  input, output);
114 }
115 } // namespace cpu
116 } // 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:1665
Quantization info when assuming per layer quantization.
Describe one of the image&#39;s dimensions with a start, end and step.
Definition: Window.h:79
void sve2_qsymm16_activation(const ITensor *src, ITensor *dst, const ActivationLayerInfo &act_info, const Window &window)
Definition: qsymm16.cpp:40
Activation Layer Information class.
Definition: Types.h:1625
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:1629
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
Coordinates of an item.
Definition: Coordinates.h:37
UniformQuantizationInfo uniform() const
Return per layer quantization info.
virtual ITensorInfo * info() const =0
Interface to be implemented by the child class to return the tensor&#39;s metadata.
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
virtual QuantizationInfo quantization_info() const =0
Get the quantization settings (scale and offset) of the tensor.
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:1660
float b() const
Get the beta value.
Definition: Types.h:1670
constexpr int end() const
Return the end of the dimension.
Definition: Window.h:101
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:96
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:158