Compute Library
 23.11
qsymm16.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2020-2023 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  */
29 
30 #include "src/core/NEON/NEMath.h"
31 #include "src/core/NEON/NESymm.h"
33 
34 #include <arm_neon.h>
35 #include <cmath>
36 #include <cstddef>
37 
38 namespace arm_compute
39 {
40 namespace cpu
41 {
43  ITensor *dst,
45  const Window &window)
46 {
47  constexpr int window_step_x = 8;
48  const auto window_start_x = static_cast<int>(window.x().start());
49  const auto window_end_x = static_cast<int>(window.x().end());
50  const ActivationLayerInfo::ActivationFunction act = act_info.activation();
51 
52  Window win_collapsed = window.collapse_if_possible(window, Window::DimZ);
53  win_collapsed.set(Window::DimX, Window::Dimension(0, 1, 1));
54 
55  Iterator input(src, win_collapsed);
56  Iterator output(dst, win_collapsed);
57 
58  const UniformQuantizationInfo qi_in = src->info()->quantization_info().uniform();
59  const UniformQuantizationInfo qi_out = dst->info()->quantization_info().uniform();
60  const auto vconst_1 = vdupq_n_f32(1.f);
61  const float32x4_t va_f32 = vdupq_n_f32(act_info.a());
62  const float32x4_t vb_f32 = vdupq_n_f32(act_info.b());
63  const float a_f32 = act_info.a();
64  const float b_f32 = act_info.b();
65 
67  win_collapsed,
68  [&](const Coordinates &)
69  {
70  const auto input_ptr = reinterpret_cast<const qsymm16_t *>(input.ptr());
71  const auto output_ptr = reinterpret_cast<qsymm16_t *>(output.ptr());
72 
74  ARM_COMPUTE_UNUSED(tmp);
75 
76  // Compute S elements per iteration
77  int x = window_start_x;
78  for (; x <= (window_end_x - window_step_x); x += window_step_x)
79  {
80  const auto vin = wrapper::vloadq(input_ptr + x);
81  if (act == ActivationLayerInfo::ActivationFunction::LOGISTIC)
82  {
83  // De-quantize
84  const auto vin_deq = vdequantize_int16(vin, qi_in.scale);
85  // Perform activation
86  const float32x4x2_t tmp_dep = {{
87  wrapper::vdiv(vconst_1, wrapper::vadd(vconst_1, wrapper::vexpq(wrapper::vneg(vin_deq.val[0])))),
88  wrapper::vdiv(vconst_1, wrapper::vadd(vconst_1, wrapper::vexpq(wrapper::vneg(vin_deq.val[1])))),
89  }};
90  // Re-quantize to new output space
91  tmp = vquantize_int16(tmp_dep, qi_out.scale);
92  }
93  else if (act == ActivationLayerInfo::ActivationFunction::TANH)
94  {
95  // De-quantize
96  const auto vin_deq = vdequantize_int16(vin, qi_in.scale);
97  // Perform activation
98  const float32x4x2_t tmp_dep = {{
99  wrapper::vmul(va_f32, wrapper::vtanh(wrapper::vmul(vin_deq.val[0], vb_f32))),
100  wrapper::vmul(va_f32, wrapper::vtanh(wrapper::vmul(vin_deq.val[1], vb_f32))),
101  }};
102  // Re-quantize to new output space
103  tmp = vquantize_int16(tmp_dep, qi_out.scale);
104  }
105 
106  else if (act == ActivationLayerInfo::ActivationFunction::LU_BOUNDED_RELU)
107  {
108  // De-quantize
109  const auto vin_deq = vdequantize_int16(vin, qi_in.scale);
110  // Perform activation
111  const float32x4x2_t tmp_dep = {{wrapper::vmin(va_f32, wrapper::vmax(vb_f32, vin_deq.val[0])),
112  wrapper::vmin(va_f32, wrapper::vmax(vb_f32, vin_deq.val[1]))}};
113  // Re-quantize to new output space
114  tmp = vquantize_int16(tmp_dep, qi_out.scale);
115  }
116  else
117  {
118  ARM_COMPUTE_ERROR("Unsupported activation function");
119  }
120  wrapper::vstore(output_ptr + x, tmp);
121  }
122 
123  // Compute left-over elements
124  for (; x < window_end_x; ++x)
125  {
126  qsymm16_t in = *(reinterpret_cast<const qsymm16_t *>(input_ptr + x));
127  qsymm16_t tmp = 0;
128  if (act == ActivationLayerInfo::ActivationFunction::LOGISTIC)
129  {
130  float tmp_f = dequantize_qsymm16(in, qi_in.scale);
131  tmp_f = 1.f / (1.f + std::exp(-tmp_f));
132  tmp = quantize_qsymm16(tmp_f, qi_out);
133  }
134  else if (act == ActivationLayerInfo::ActivationFunction::TANH)
135  {
136  float tmp_f = dequantize_qsymm16(in, qi_in.scale);
137  tmp_f = a_f32 * std::tanh(b_f32 * tmp_f);
138  tmp = quantize_qsymm16(tmp_f, qi_out);
139  }
140  else if (act == ActivationLayerInfo::ActivationFunction::LU_BOUNDED_RELU)
141  {
142  float tmp_f = dequantize_qsymm16(in, qi_in.scale);
143  tmp_f = std::min<float>(a_f32, std::max<float>(b_f32, tmp_f));
144  tmp = quantize_qsymm16(tmp_f, qi_out);
145  }
146  else
147  {
148  ARM_COMPUTE_ERROR("Unsupported activation function");
149  }
150  *(output_ptr + x) = tmp;
151  }
152  },
153  input, output);
154 }
155 } // namespace cpu
156 } // namespace arm_compute
ITensorPack.h
arm_compute::wrapper::vadd
uint8x8_t vadd(const uint8x8_t &a, const uint8x8_t &b)
Definition: add.h:39
arm_compute::Window::Dimension::start
constexpr int start() const
Return the start of the dimension.
Definition: Window.h:96
arm_compute::test::validation::src
SimpleTensor< float > src
Definition: DFT.cpp:155
Helpers.h
arm_compute::quantize_qsymm16
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.
Definition: QuantizationInfo.h:441
arm_compute::test::validation::dst
auto dst
Definition: DFT.cpp:170
ActivationLayerInfo.h
arm_compute::vdequantize_int16
float32x4x2_t vdequantize_int16(const int16x8_t &qv, float scale)
Dequantize a neon vector holding 8 16-bit quantized values.
Definition: NESymm.h:135
arm_compute::wrapper::vexpq
float32x4_t vexpq(const float32x4_t &a)
Definition: exp.h:48
arm_compute::Window::DimX
static constexpr size_t DimX
Alias for dimension 0 also known as X dimension.
Definition: Window.h:43
Window.h
arm_compute::Window::collapse_if_possible
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:72
ARM_COMPUTE_ERROR
#define ARM_COMPUTE_ERROR(msg)
Print the given message then throw an std::runtime_error.
Definition: Error.h:354
arm_compute::wrapper::vtanh
float32x4_t vtanh(const float32x4_t &a)
Definition: tanh.h:41
arm_compute::ITensor
Interface for CPU tensor.
Definition: ITensor.h:36
NESymm.h
arm_compute::cpu::neon_qsymm16_activation
void neon_qsymm16_activation(const ITensor *src, ITensor *dst, const ActivationLayerInfo &act_info, const Window &window)
Definition: qsymm16.cpp:42
arm_compute::wrapper::vloadq
uint8x16_t vloadq(const uint8_t *ptr)
Definition: load.h:58
arm_compute::UniformQuantizationInfo
Quantization info when assuming per layer quantization.
Definition: QuantizationInfo.h:42
wrapper.h
Includes all wrapper headers at once.
arm_compute::vquantize_int16
int16x8_t vquantize_int16(const float32x4x2_t &qv, float scale)
Quantize a neon vector holding 8 floating point values.
Definition: NESymm.h:150
NEMath.h
arm_compute::wrapper::vmin
uint8x8_t vmin(const uint8x8_t &a, const uint8x8_t &b)
Definition: min.h:39
arm_compute::ActivationLayerInfo
Activation Layer Information class.
Definition: ActivationLayerInfo.h:55
arm_compute::test::validation::act_info
act_info
Definition: DirectConvolutionLayer.cpp:547
Types.h
arm_compute::Iterator::ptr
constexpr uint8_t * ptr() const
Return a pointer to the current pixel.
Definition: Helpers.inl:147
arm_compute::wrapper::vmul
uint8x8_t vmul(const uint8x8_t &a, const uint8x8_t &b)
Definition: mul.h:39
arm_compute::execute_window_loop
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:74
arm_compute::Iterator
Iterator updated by execute_window_loop for each window element.
Definition: Helpers.h:46
arm_compute::wrapper::traits::neon_bitvector_t
typename neon_bitvector< T, BW >::type neon_bitvector_t
Helper type template to get the type of a neon vector.
Definition: traits.h:139
arm_compute::ActivationFunction
ActivationFunction
Available activation functions.
Definition: ActivationLayerInfo.h:35
arm_compute::dequantize_qsymm16
float dequantize_qsymm16(int16_t value, const UniformQuantizationInfo &qinfo)
Dequantize a value given a 16-bit symmetric quantization scheme.
Definition: QuantizationInfo.h:457
ARM_COMPUTE_UNUSED
#define ARM_COMPUTE_UNUSED(...)
To avoid unused variables warnings.
Definition: Error.h:151
arm_compute::wrapper::vneg
int8x8_t vneg(const int8x8_t &a)
Definition: neg.h:39
arm_compute::Coordinates
Coordinates of an item.
Definition: Coordinates.h:37
arm_compute::Window::Dimension
Describe one of the image's dimensions with a start, end and step.
Definition: Window.h:79
arm_compute::Window::set
void set(size_t dimension, const Dimension &dim)
Set the values of a given dimension.
Definition: Window.inl:53
arm_compute::wrapper::vmax
uint8x8_t vmax(const uint8x8_t &a, const uint8x8_t &b)
Definition: max.h:39
arm_compute::UniformQuantizationInfo::scale
float scale
Definition: QuantizationInfo.h:62
arm_compute::wrapper::vstore
void vstore(uint8_t *ptr, uint8x8_t val)
Definition: store.h:39
arm_compute::Window
Describe a multidimensional execution window.
Definition: Window.h:39
arm_compute::qsymm16_t
int16_t qsymm16_t
16 bit quantized symmetric scalar value
Definition: QuantizationInfo.h:38
arm_compute
Copyright (c) 2017-2023 Arm Limited.
Definition: introduction.dox:24
arm_compute::Window::DimZ
static constexpr size_t DimZ
Alias for dimension 2 also known as Z dimension.
Definition: Window.h:47
arm_compute::wrapper::vdiv
float32x2_t vdiv(const float32x2_t &a, const float32x2_t &b)
Definition: div.h:59
arm_compute::Window::Dimension::end
constexpr int end() const
Return the end of the dimension.
Definition: Window.h:101
arm_compute::Window::x
constexpr const Dimension & x() const
Alias to access the first dimension of the window.
Definition: Window.h:158
arm_compute::test::validation::input
auto input
Definition: LSTMLayerQuantized.cpp:486