Compute Library
 21.02
qsymm16.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  */
28 #include "src/core/NEON/NEMath.h"
29 #include "src/core/NEON/NESymm.h"
32 
33 #include <arm_neon.h>
34 #include <cmath>
35 #include <cstddef>
36 
37 namespace arm_compute
38 {
39 namespace cpu
40 {
41 void qsymm16_neon_activation(const ITensor *src, ITensor *dst, const ActivationLayerInfo &act_info, const Window &window)
42 {
43  constexpr int window_step_x = 8;
44  const auto window_start_x = static_cast<int>(window.x().start());
45  const auto window_end_x = static_cast<int>(window.x().end());
47 
48  Window win_collapsed = window.collapse_if_possible(window, Window::DimZ);
49  win_collapsed.set(Window::DimX, Window::Dimension(0, 1, 1));
50 
51  Iterator input(src, win_collapsed);
52  Iterator output(dst, win_collapsed);
53 
54  const UniformQuantizationInfo qi_in = src->info()->quantization_info().uniform();
55  const UniformQuantizationInfo qi_out = dst->info()->quantization_info().uniform();
56  const auto vconst_1 = vdupq_n_f32(1.f);
57  const float32x4_t va_f32 = vdupq_n_f32(act_info.a());
58  const float32x4_t vb_f32 = vdupq_n_f32(act_info.b());
59  const float a_f32 = act_info.a();
60  const float b_f32 = act_info.b();
61 
62  execute_window_loop(win_collapsed, [&](const Coordinates &)
63  {
64  const auto input_ptr = reinterpret_cast<const qsymm16_t *>(input.ptr());
65  const auto output_ptr = reinterpret_cast<qsymm16_t *>(output.ptr());
66 
68  ARM_COMPUTE_UNUSED(tmp);
69 
70  // Compute S elements per iteration
71  int x = window_start_x;
72  for(; x <= (window_end_x - window_step_x); x += window_step_x)
73  {
74  const auto vin = wrapper::vloadq(input_ptr + x);
76  {
77  // De-quantize
78  const auto vin_deq = vdequantize_int16(vin, qi_in.scale);
79  // Perform activation
80  const float32x4x2_t tmp_dep =
81  {
82  {
83  wrapper::vdiv(vconst_1, wrapper::vadd(vconst_1, wrapper::vexpq(wrapper::vneg(vin_deq.val[0])))),
84  wrapper::vdiv(vconst_1, wrapper::vadd(vconst_1, wrapper::vexpq(wrapper::vneg(vin_deq.val[1])))),
85  }
86  };
87  // Re-quantize to new output space
88  tmp = vquantize_int16(tmp_dep, qi_out.scale);
89  }
91  {
92  // De-quantize
93  const auto vin_deq = vdequantize_int16(vin, qi_in.scale);
94  // Perform activation
95  const float32x4x2_t tmp_dep =
96  {
97  {
98  wrapper::vmul(va_f32, wrapper::vtanh(wrapper::vmul(vin_deq.val[0], vb_f32))),
99  wrapper::vmul(va_f32, wrapper::vtanh(wrapper::vmul(vin_deq.val[1], vb_f32))),
100  }
101  };
102  // Re-quantize to new output space
103  tmp = vquantize_int16(tmp_dep, qi_out.scale);
104  }
105  else
106  {
107  ARM_COMPUTE_ERROR("Unsupported activation function");
108  }
109  wrapper::vstore(output_ptr + x, tmp);
110  }
111 
112  // Compute left-over elements
113  for(; x < window_end_x; ++x)
114  {
115  qsymm16_t in = *(reinterpret_cast<const qsymm16_t *>(input_ptr + x));
116  qsymm16_t tmp = 0;
118  {
119  float tmp_f = dequantize_qsymm16(in, qi_in.scale);
120  tmp_f = 1.f / (1.f + std::exp(-tmp_f));
121  tmp = quantize_qsymm16(tmp_f, qi_out);
122  }
124  {
125  float tmp_f = dequantize_qsymm16(in, qi_in.scale);
126  tmp_f = a_f32 * std::tanh(b_f32 * tmp_f);
127  tmp = quantize_qsymm16(tmp_f, qi_out);
128  }
129  else
130  {
131  ARM_COMPUTE_ERROR("Unsupported activation function");
132  }
133  *(output_ptr + x) = tmp;
134  }
135  },
136  input, output);
137 }
138 } // namespace cpu
139 } // namespace arm_compute
float32x2_t vdiv(const float32x2_t &a, const float32x2_t &b)
Definition: div.h:58
int16_t quantize_qsymm16(float value, const UniformQuantizationInfo &qinfo, RoundingPolicy rounding_policy=RoundingPolicy::TO_NEAREST_UP)
Quantize a value given a 16-bit symmetric quantization scheme.
float32x4_t vtanh(const float32x4_t &a)
Definition: tanh.h:40
#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
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
float32x4x2_t vdequantize_int16(const int16x8_t &qv, float scale)
Dequantize a neon vector holding 8 16-bit quantized values.
Definition: NESymm.h:135
Quantization info when assuming per layer quantization.
Describe one of the image&#39;s dimensions with a start, end and step.
Definition: Window.h:77
int16x8_t vquantize_int16(const float32x4x2_t &qv, float scale)
Quantize a neon vector holding 8 floating point values.
Definition: NESymm.h:155
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
static constexpr size_t DimX
Alias for dimension 0 also known as X dimension.
Definition: Window.h:43
#define ARM_COMPUTE_UNUSED(...)
To avoid unused variables warnings.
Definition: Error.h:152
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
float dequantize_qsymm16(int16_t value, const UniformQuantizationInfo &qinfo)
Dequantize a value given a 16-bit symmetric quantization scheme.
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
void qsymm16_neon_activation(const ITensor *src, ITensor *dst, const ActivationLayerInfo &act_info, const Window &window)
Definition: qsymm16.cpp:41
virtual QuantizationInfo quantization_info() const =0
Get the quantization settings (scale and offset) of the tensor.
int8x8_t vneg(const int8x8_t &a)
Definition: neg.h:39
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
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.
constexpr int end() const
Return the end of the dimension.
Definition: Window.h:99
int16_t qsymm16_t
16 bit quantized symmetric scalar value
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:94
float32x4_t vexpq(const float32x4_t &a)
Definition: exp.h:47
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:145