blob: 0bef807db9a15f65dd73970d1fde45dc8e6b5738 [file] [log] [blame]
Michalis Spyrouc4d45552020-10-19 12:41:30 +01001/*
2 * Copyright (c) 2020 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#include "arm_compute/core/Helpers.h"
25#include "arm_compute/core/ITensorPack.h"
26#include "arm_compute/core/Window.h"
27#include "arm_compute/core/experimental/Types.h"
28#include "src/core/NEON/NEMath.h"
29#include "src/core/NEON/NESymm.h"
30#include "src/core/NEON/wrapper/wrapper.h"
31#include "src/core/common/StdTypes.h"
32#include "src/core/common/Validate.h"
33
34#include <arm_neon.h>
35#include <cmath>
36#include <cstddef>
37
38namespace arm_compute
39{
40namespace cpu
41{
42void qsymm16_neon_activation(const ITensor *src, ITensor *dst, const ActivationLayerInfo &act_info, const Window &window)
43{
44 constexpr int window_step_x = 8;
45 const auto window_start_x = static_cast<int>(window.x().start());
46 const auto window_end_x = static_cast<int>(window.x().end());
47 const ActivationLayerInfo::ActivationFunction act = act_info.activation();
48
49 Window win_collapsed = window.collapse_if_possible(window, Window::DimZ);
50 win_collapsed.set(Window::DimX, Window::Dimension(0, 1, 1));
51
52 Iterator input(src, win_collapsed);
53 Iterator output(dst, win_collapsed);
54
55 const UniformQuantizationInfo qi_in = src->info()->quantization_info().uniform();
56 const UniformQuantizationInfo qi_out = dst->info()->quantization_info().uniform();
57 const auto vconst_1 = vdupq_n_f32(1.f);
58 const float32x4_t va_f32 = vdupq_n_f32(act_info.a());
59 const float32x4_t vb_f32 = vdupq_n_f32(act_info.b());
60 const float a_f32 = act_info.a();
61 const float b_f32 = act_info.b();
62
63 execute_window_loop(win_collapsed, [&](const Coordinates &)
64 {
65 const auto input_ptr = reinterpret_cast<const qsymm16_t *>(input.ptr());
66 const auto output_ptr = reinterpret_cast<qsymm16_t *>(output.ptr());
67
68 wrapper::traits::neon_bitvector_t<qsymm16_t, wrapper::traits::BitWidth::W128> tmp;
69 ARM_COMPUTE_UNUSED(tmp);
70
71 // Compute S elements per iteration
72 int x = window_start_x;
73 for(; x <= (window_end_x - window_step_x); x += window_step_x)
74 {
75 const auto vin = wrapper::vloadq(input_ptr + x);
76 if(act == ActivationLayerInfo::ActivationFunction::LOGISTIC)
77 {
78 // De-quantize
79 const auto vin_deq = vdequantize_int16(vin, qi_in.scale);
80 // Perform activation
81 const float32x4x2_t tmp_dep =
82 {
83 {
84 wrapper::vdiv(vconst_1, wrapper::vadd(vconst_1, wrapper::vexpq(wrapper::vneg(vin_deq.val[0])))),
85 wrapper::vdiv(vconst_1, wrapper::vadd(vconst_1, wrapper::vexpq(wrapper::vneg(vin_deq.val[1])))),
86 }
87 };
88 // Re-quantize to new output space
89 tmp = vquantize_int16(tmp_dep, qi_out.scale);
90 }
91 else if(act == ActivationLayerInfo::ActivationFunction::TANH)
92 {
93 // De-quantize
94 const auto vin_deq = vdequantize_int16(vin, qi_in.scale);
95 // Perform activation
96 const float32x4x2_t tmp_dep =
97 {
98 {
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 };
103 // Re-quantize to new output space
104 tmp = vquantize_int16(tmp_dep, qi_out.scale);
105 }
106 else
107 {
108 ARM_COMPUTE_ERROR("Unsupported activation function");
109 }
110 wrapper::vstore(output_ptr + x, tmp);
111 }
112
113 // Compute left-over elements
114 for(; x < window_end_x; ++x)
115 {
116 qsymm16_t in = *(reinterpret_cast<const qsymm16_t *>(input_ptr + x));
117 qsymm16_t tmp = 0;
118 if(act == ActivationLayerInfo::ActivationFunction::LOGISTIC)
119 {
120 float tmp_f = dequantize_qsymm16(in, qi_in.scale);
121 tmp_f = 1.f / (1.f + std::exp(-tmp_f));
122 tmp = quantize_qsymm16(tmp_f, qi_out);
123 }
124 else if(act == ActivationLayerInfo::ActivationFunction::TANH)
125 {
126 float tmp_f = dequantize_qsymm16(in, qi_in.scale);
127 tmp_f = a_f32 * std::tanh(b_f32 * tmp_f);
128 tmp = quantize_qsymm16(tmp_f, qi_out);
129 }
130 else
131 {
132 ARM_COMPUTE_ERROR("Unsupported activation function");
133 }
134 *(output_ptr + x) = tmp;
135 }
136 },
137 input, output);
138}
139} // namespace cpu
140} // namespace arm_compute