Compute Library
 22.11
qasymm8.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 
27 
28 #include <cmath>
29 #include <cstddef>
30 
31 #include "src/core/NEON/SVEAsymm.h"
32 #include "src/core/NEON/SVEMath.h"
33 #include <arm_sve.h>
34 
35 namespace arm_compute
36 {
37 namespace cpu
38 {
39 void sve2_qasymm8_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 UniformQuantizationInfo qi_in = src->info()->quantization_info().uniform();
52  const UniformQuantizationInfo qi_out = dst->info()->quantization_info().uniform();
53  const auto va = svdup_n_u8(quantize_qasymm8(act_info.a(), qi_in));
54  const auto vb = svdup_n_u8(quantize_qasymm8(act_info.b(), qi_in));
55  const auto const_0 = quantize_qasymm8(0.f, qi_in);
56  const auto vconst_0 = svdup_n_u8(const_0);
57  const auto vconst_1 = svdup_n_f32(1.f);
58  const auto va_f32 = svdup_n_f32(act_info.a());
59  const auto vb_f32 = svdup_n_f32(act_info.b());
60 
61 
62  // Initialise scale/offset for re-quantization
63  bool requant = true;
64  if(qi_in.scale == qi_out.scale && qi_in.offset == qi_out.offset)
65  {
66  requant = false;
67  }
68  float s = qi_in.scale / qi_out.scale;
69  float o = -qi_in.offset * s + qi_out.offset;
70  auto vs = svdup_n_f32(s);
71  auto vo = svdup_n_f32(o);
72 
73  // Initialise scale/offset for re-quantization with int32_t
74  const auto voffset_in = svdup_n_s32(qi_in.offset);
75  int32_t s_s32 = round(s * (1 << 8), arm_compute::RoundingPolicy::TO_NEAREST_EVEN);
76  int32_t o_s32 = round(o * (1 << 8), arm_compute::RoundingPolicy::TO_NEAREST_EVEN);
77  const auto vs_s32 = svdup_n_s32(s_s32);
78  const auto vo_s32 = svdup_n_s32(o_s32);
79 
80  // Initialise scale/offset for re-quantization for leaky relu
81  int32_t s_leaky_s32 = round(s * act_info.a() * (1 << 8), arm_compute::RoundingPolicy::TO_NEAREST_EVEN);
82  int32_t o_leaky_s32 = round((-qi_in.offset * s * act_info.a() + qi_out.offset) * (1 << 8),
84  const auto vs_leaky_s32 = svdup_n_s32(s_leaky_s32);
85  const auto vo_leaky_s32 = svdup_n_s32(o_leaky_s32);
86 
87  execute_window_loop(win_collapsed, [&](const Coordinates &)
88  {
89  const auto input_ptr = reinterpret_cast<const uint8_t *>(input.ptr());
90  const auto output_ptr = reinterpret_cast<uint8_t *>(output.ptr());
91 
92  svuint8_t tmp;
93 
94  int x = window_start_x;
95  svbool_t pg = svwhilelt_b8(x, window_end_x);
96  do
97  {
98  const auto vin = svld1_u8(pg, input_ptr + x);
100  {
101  // Perform activation
102  tmp = svmax_u8_z(pg, vconst_0, vin);
103  // Re-quantize to new output space
104  tmp = requant ? svmla_qasymm8_z(pg, tmp, vs, vo) : tmp;
105  }
107  {
108  // Perform activation
109  tmp = svmin_u8_z(pg, va, svmax_u8_z(pg, vconst_0, vin));
110  // Re-quantize to new output space
111  tmp = requant ? svmla_qasymm8_z(pg, tmp, vs, vo) : tmp;
112  }
114  {
115  // Perform activation
116  tmp = svmin_u8_z(pg, va, svmax_u8_z(pg, vb, vin));
117  // Re-quantize to new output space
118  tmp = svmla_qasymm8_z(pg, tmp, vs, vo);
119  }
121  {
122  // De-quantize
123  const auto vin_deq = svdequantize_z(pg, vin, qi_in);
124  // Perform activation
125  const svfloat32x4_t tmp_dep = svcreate4_f32(svdiv_f32_z(pg, vconst_1, svadd_f32_z(pg, vconst_1, svexp_f32_z(pg, svneg_f32_z(pg, svget4_f32(vin_deq, 0))))),
126  svdiv_f32_z(pg, vconst_1, svadd_f32_z(pg, vconst_1, svexp_f32_z(pg, svneg_f32_z(pg, svget4_f32(vin_deq, 1))))),
127  svdiv_f32_z(pg, vconst_1, svadd_f32_z(pg, vconst_1, svexp_f32_z(pg, svneg_f32_z(pg, svget4_f32(vin_deq, 2))))),
128  svdiv_f32_z(pg, vconst_1, svadd_f32_z(pg, vconst_1, svexp_f32_z(pg, svneg_f32_z(pg, svget4_f32(vin_deq, 3))))));
129 
130  // Re-quantize to new output space
131  tmp = svquantize_z(pg, tmp_dep, qi_out);
132  }
134  {
135  // De-quantize
136  const auto vin_deq = svdequantize_z(pg, vin, qi_in);
137  // Perform activation
138  const svfloat32x4_t tmp_dep = svcreate4_f32(svmul_f32_z(pg, va_f32, svtanh_f32_z(pg, svmul_f32_z(pg, svget4_f32(vin_deq, 0), vb_f32))),
139  svmul_f32_z(pg, va_f32, svtanh_f32_z(pg, svmul_f32_z(pg, svget4_f32(vin_deq, 1), vb_f32))),
140  svmul_f32_z(pg, va_f32, svtanh_f32_z(pg, svmul_f32_z(pg, svget4_f32(vin_deq, 2), vb_f32))),
141  svmul_f32_z(pg, va_f32, svtanh_f32_z(pg, svmul_f32_z(pg, svget4_f32(vin_deq, 3), vb_f32))));
142 
143  // Re-quantize to new output space
144  tmp = svquantize_z(pg, tmp_dep, qi_out);
145  }
147  {
148  svbool_t p0, p1, p2, p3;
149  svint32x4_t tmp_dep;
150 
151  // Expand to int32
152  const svint32x4_t vin_s32 = svcreate4_s32(
153  svreinterpret_s32_u32(svmovlb_u32(svmovlb_u16(vin))),
154  svreinterpret_s32_u32(svmovlt_u32(svmovlb_u16(vin))),
155  svreinterpret_s32_u32(svmovlb_u32(svmovlt_u16(vin))),
156  svreinterpret_s32_u32(svmovlt_u32(svmovlt_u16(vin))));
157 
158  // Compare elements to input offset
159  if(qi_in.scale >= 0)
160  {
161  p0 = svcmplt_s32(pg, svget4_s32(vin_s32, 0), voffset_in);
162  p1 = svcmplt_s32(pg, svget4_s32(vin_s32, 1), voffset_in);
163  p2 = svcmplt_s32(pg, svget4_s32(vin_s32, 2), voffset_in);
164  p3 = svcmplt_s32(pg, svget4_s32(vin_s32, 3), voffset_in);
165  }
166  else
167  {
168  p0 = svcmpgt_s32(pg, svget4_s32(vin_s32, 0), voffset_in);
169  p1 = svcmpgt_s32(pg, svget4_s32(vin_s32, 1), voffset_in);
170  p2 = svcmpgt_s32(pg, svget4_s32(vin_s32, 2), voffset_in);
171  p3 = svcmpgt_s32(pg, svget4_s32(vin_s32, 3), voffset_in);
172  }
173 
174  // Multiply negative elements and requantize if necessary
175  if(requant)
176  {
177  tmp_dep = svcreate4_s32(
178  svasr_n_s32_m(pg, svmla_s32_m(pg, svsel(p0, vo_leaky_s32, vo_s32), svget4_s32(vin_s32, 0), svsel(p0, vs_leaky_s32, vs_s32)), 8),
179  svasr_n_s32_m(pg, svmla_s32_m(pg, svsel(p1, vo_leaky_s32, vo_s32), svget4_s32(vin_s32, 1), svsel(p1, vs_leaky_s32, vs_s32)), 8),
180  svasr_n_s32_m(pg, svmla_s32_m(pg, svsel(p2, vo_leaky_s32, vo_s32), svget4_s32(vin_s32, 2), svsel(p2, vs_leaky_s32, vs_s32)), 8),
181  svasr_n_s32_m(pg, svmla_s32_m(pg, svsel(p3, vo_leaky_s32, vo_s32), svget4_s32(vin_s32, 3), svsel(p3, vs_leaky_s32, vs_s32)), 8));
182  }
183  else
184  {
185  tmp_dep = svcreate4_s32(
186  svasr_n_s32_m(p0, svmad_s32_m(p0, svget4_s32(vin_s32, 0), vs_leaky_s32, vo_leaky_s32), 8),
187  svasr_n_s32_m(p1, svmad_s32_m(p1, svget4_s32(vin_s32, 1), vs_leaky_s32, vo_leaky_s32), 8),
188  svasr_n_s32_m(p2, svmad_s32_m(p2, svget4_s32(vin_s32, 2), vs_leaky_s32, vo_leaky_s32), 8),
189  svasr_n_s32_m(p3, svmad_s32_m(p3, svget4_s32(vin_s32, 3), vs_leaky_s32, vo_leaky_s32), 8));
190  }
191 
192  // Convert uint32 vectors to uint16 vectors (with saturation)
193  const auto v_low_u16 = svqxtunt_s32(svqxtunb_s32(svget4_s32(tmp_dep, 0)), svget4_s32(tmp_dep, 1));
194  const auto v_high_u16 = svqxtunt_s32(svqxtunb_s32(svget4_s32(tmp_dep, 2)), svget4_s32(tmp_dep, 3));
195 
196  // convert uint16 vectors to uint8 vectors (with saturation)
197  tmp = svqxtnt_u16(svqxtnb_u16(v_low_u16), v_high_u16);
198  }
199  else
200  {
201  ARM_COMPUTE_ERROR("Unsupported activation function");
202  }
203 
204  svst1_u8(pg, output_ptr + x, tmp);
205 
206  x += svcntb();
207  pg = svwhilelt_b8(x, window_end_x);
208 
209  }
210  while(svptest_any(svptrue_b8(), pg));
211 
212  },
213  input, output);
214 }
215 } // namespace cpu
216 } // namespace arm_compute
uint8_t quantize_qasymm8(float value, const INFO_TYPE &qinfo, RoundingPolicy rounding_policy=RoundingPolicy::TO_NEAREST_UP)
Quantize a value given an unsigned 8-bit asymmetric quantization scheme.
#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
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
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
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.
void sve2_qasymm8_activation(const ITensor *src, ITensor *dst, const ActivationLayerInfo &act_info, const Window &window)
Definition: qasymm8.cpp:39
int round(float x, RoundingPolicy rounding_policy)
Return a rounded value of x.
Definition: Rounding.cpp:35
Rounds to nearest value; half rounds to nearest even.
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