blob: 331c30a49646a7e203164d3a1f3530fa8d5644dc [file] [log] [blame]
Michalis Spyrouc4d45552020-10-19 12:41:30 +01001/*
Matthew Benthamf1aeab92023-05-30 13:35:34 +00002 * Copyright (c) 2020-2023 Arm Limited.
Michalis Spyrouc4d45552020-10-19 12:41:30 +01003 *
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 */
Matthew Benthamf1aeab92023-05-30 13:35:34 +000024#include "arm_compute/core/ActivationLayerInfo.h"
Michalis Spyrouc4d45552020-10-19 12:41:30 +010025#include "arm_compute/core/Helpers.h"
26#include "arm_compute/core/ITensorPack.h"
27#include "arm_compute/core/Window.h"
28#include "arm_compute/core/experimental/Types.h"
29#include "src/core/NEON/NEMath.h"
30#include "src/core/NEON/NESymm.h"
31#include "src/core/NEON/wrapper/wrapper.h"
Michalis Spyrouc4d45552020-10-19 12:41:30 +010032
33#include <arm_neon.h>
34#include <cmath>
35#include <cstddef>
36
37namespace arm_compute
38{
39namespace cpu
40{
Dana Zlotnik32291712021-11-25 09:58:27 +020041void neon_qsymm16_activation(const ITensor *src, ITensor *dst, const ActivationLayerInfo &act_info, const Window &window)
Michalis Spyrouc4d45552020-10-19 12:41:30 +010042{
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());
46 const ActivationLayerInfo::ActivationFunction act = act_info.activation();
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
67 wrapper::traits::neon_bitvector_t<qsymm16_t, wrapper::traits::BitWidth::W128> tmp;
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);
75 if(act == ActivationLayerInfo::ActivationFunction::LOGISTIC)
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 }
90 else if(act == ActivationLayerInfo::ActivationFunction::TANH)
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 }
Pablo Marquez Tellof55cca52022-04-06 14:31:25 +0100105
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 =
112 {
113 {
114 wrapper::vmin(va_f32, wrapper::vmax(vb_f32, vin_deq.val[0])),
115 wrapper::vmin(va_f32, wrapper::vmax(vb_f32, vin_deq.val[1]))
116 }
117 };
118 // Re-quantize to new output space
119 tmp = vquantize_int16(tmp_dep, qi_out.scale);
120 }
Michalis Spyrouc4d45552020-10-19 12:41:30 +0100121 else
122 {
123 ARM_COMPUTE_ERROR("Unsupported activation function");
124 }
125 wrapper::vstore(output_ptr + x, tmp);
126 }
127
128 // Compute left-over elements
129 for(; x < window_end_x; ++x)
130 {
131 qsymm16_t in = *(reinterpret_cast<const qsymm16_t *>(input_ptr + x));
132 qsymm16_t tmp = 0;
133 if(act == ActivationLayerInfo::ActivationFunction::LOGISTIC)
134 {
135 float tmp_f = dequantize_qsymm16(in, qi_in.scale);
136 tmp_f = 1.f / (1.f + std::exp(-tmp_f));
137 tmp = quantize_qsymm16(tmp_f, qi_out);
138 }
139 else if(act == ActivationLayerInfo::ActivationFunction::TANH)
140 {
141 float tmp_f = dequantize_qsymm16(in, qi_in.scale);
142 tmp_f = a_f32 * std::tanh(b_f32 * tmp_f);
143 tmp = quantize_qsymm16(tmp_f, qi_out);
144 }
Pablo Marquez Tellof55cca52022-04-06 14:31:25 +0100145 else if(act == ActivationLayerInfo::ActivationFunction::LU_BOUNDED_RELU)
146 {
147 float tmp_f = dequantize_qsymm16(in, qi_in.scale);
148 tmp_f = std::min<float>(a_f32, std::max<float>(b_f32, tmp_f));
149 tmp = quantize_qsymm16(tmp_f, qi_out);
150 }
Michalis Spyrouc4d45552020-10-19 12:41:30 +0100151 else
152 {
153 ARM_COMPUTE_ERROR("Unsupported activation function");
154 }
155 *(output_ptr + x) = tmp;
156 }
157 },
158 input, output);
159}
160} // namespace cpu
161} // namespace arm_compute