aboutsummaryrefslogtreecommitdiff
path: root/src/core/cpu/kernels/activation/SVE/qasymm8.cpp
blob: 3d9476ac564686066bef2f7098d55514691f70d0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
/*
 * Copyright (c) 2020-2021 Arm Limited.
 *
 * SPDX-License-Identifier: MIT
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to
 * deal in the Software without restriction, including without limitation the
 * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
 * sell copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */

#include "arm_compute/core/Helpers.h"
#include "arm_compute/core/Window.h"
#include "src/core/common/Validate.h"

#include <cmath>
#include <cstddef>

#if defined(__ARM_FEATURE_SVE2)
#include "src/core/NEON/SVEAsymm.h"
#include "src/core/NEON/SVEMath.h"
#include <arm_sve.h>

namespace arm_compute
{
namespace cpu
{
void qasymm8_sve_activation(const ITensor *src, ITensor *dst, const ActivationLayerInfo &act_info, const Window &window)
{
    const auto                                    window_start_x = static_cast<int>(window.x().start());
    const auto                                    window_end_x   = static_cast<int>(window.x().end());
    const ActivationLayerInfo::ActivationFunction act            = act_info.activation();

    Window win_collapsed = window.collapse_if_possible(window, Window::DimZ);
    win_collapsed.set(Window::DimX, Window::Dimension(0, 1, 1));

    Iterator input(src, win_collapsed);
    Iterator output(dst, win_collapsed);

    const UniformQuantizationInfo qi_in           = src->info()->quantization_info().uniform();
    const UniformQuantizationInfo qi_out          = dst->info()->quantization_info().uniform();
    const auto                    va              = svdup_n_u8(quantize_qasymm8(act_info.a(), qi_in));
    const auto                    vb              = svdup_n_u8(quantize_qasymm8(act_info.b(), qi_in));
    const auto                    const_0         = quantize_qasymm8(0.f, qi_in);
    const auto                    vconst_0        = svdup_n_u8(const_0);
    const auto                    vconst_1        = svdup_n_f32(1.f);
    const auto                    va_f32          = svdup_n_f32(act_info.a());
    const auto                    vb_f32          = svdup_n_f32(act_info.b());
    const auto                    const_6_f32     = svdup_n_f32(6.f);
    const auto                    const_0_f32     = svdup_n_f32(0.f);
    const auto                    const_3_f32     = svdup_n_f32(3.f);
    const auto                    const_inv_6_f32 = svdup_n_f32(0.166666667f);

    // Initialise scale/offset for re-quantization
    bool requant = true;
    if(qi_in.scale == qi_out.scale && qi_in.offset == qi_out.offset)
    {
        requant = false;
    }
    float s  = qi_in.scale / qi_out.scale;
    float o  = -qi_in.offset * s + qi_out.offset;
    auto  vs = svdup_n_f32(s);
    auto  vo = svdup_n_f32(o);

    // Initialise scale/offset for re-quantization with int32_t
    const auto voffset_in = svdup_n_s32(qi_in.offset);
    int32_t    s_s32      = round(s * (1 << 8), arm_compute::RoundingPolicy::TO_NEAREST_EVEN);
    int32_t    o_s32      = round(o * (1 << 8), arm_compute::RoundingPolicy::TO_NEAREST_EVEN);
    const auto vs_s32     = svdup_n_s32(s_s32);
    const auto vo_s32     = svdup_n_s32(o_s32);

    // Initialise scale/offset for re-quantization for leaky relu
    int32_t s_leaky_s32 = round(s * act_info.a() * (1 << 8), arm_compute::RoundingPolicy::TO_NEAREST_EVEN);
    int32_t o_leaky_s32 = round((-qi_in.offset * s * act_info.a() + qi_out.offset) * (1 << 8),
                                arm_compute::RoundingPolicy::TO_NEAREST_EVEN);
    const auto vs_leaky_s32 = svdup_n_s32(s_leaky_s32);
    const auto vo_leaky_s32 = svdup_n_s32(o_leaky_s32);

    execute_window_loop(win_collapsed, [&](const Coordinates &)
    {
        const auto input_ptr  = reinterpret_cast<const uint8_t *>(input.ptr());
        const auto output_ptr = reinterpret_cast<uint8_t *>(output.ptr());

        svuint8_t tmp;

        int      x  = window_start_x;
        svbool_t pg = svwhilelt_b8(x, window_end_x);
        do
        {
            const auto vin = svld1_u8(pg, input_ptr + x);
            if(act == ActivationLayerInfo::ActivationFunction::RELU)
            {
                // Perform activation
                tmp = svmax_u8_z(pg, vconst_0, vin);
                // Re-quantize to new output space
                tmp = requant ? svmla_qasymm8_z(pg, tmp, vs, vo) : tmp;
            }
            else if(act == ActivationLayerInfo::ActivationFunction::BOUNDED_RELU)
            {
                // Perform activation
                tmp = svmin_u8_z(pg, va, svmax_u8_z(pg, vconst_0, vin));
                // Re-quantize to new output space
                tmp = requant ? svmla_qasymm8_z(pg, tmp, vs, vo) : tmp;
            }
            else if(act == ActivationLayerInfo::ActivationFunction::LU_BOUNDED_RELU)
            {
                // Perform activation
                tmp = svmin_u8_z(pg, va, svmax_u8_z(pg, vb, vin));
                // Re-quantize to new output space
                tmp = svmla_qasymm8_z(pg, tmp, vs, vo);
            }
            else if(act == ActivationLayerInfo::ActivationFunction::LOGISTIC)
            {
                // De-quantize
                const auto vin_deq = svdequantize_z(pg, vin, qi_in);
                // Perform activation
                const svfloat32x4_t tmp_dep =
                {
                    { {
                            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))))),
                            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))))),
                            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))))),
                            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))))),
                        }
                    }
                };
                // Re-quantize to new output space
                tmp = svquantize_z(pg, tmp_dep, qi_out);
            }
            else if(act == ActivationLayerInfo::ActivationFunction::TANH)
            {
                // De-quantize
                const auto vin_deq = svdequantize_z(pg, vin, qi_in);
                // Perform activation
                const svfloat32x4_t tmp_dep =
                {
                    { {
                            svmul_f32_z(pg, va_f32, svtanh_f32_z(pg, svmul_f32_z(pg, svget4_f32(vin_deq, 0), vb_f32))),
                            svmul_f32_z(pg, va_f32, svtanh_f32_z(pg, svmul_f32_z(pg, svget4_f32(vin_deq, 1), vb_f32))),
                            svmul_f32_z(pg, va_f32, svtanh_f32_z(pg, svmul_f32_z(pg, svget4_f32(vin_deq, 2), vb_f32))),
                            svmul_f32_z(pg, va_f32, svtanh_f32_z(pg, svmul_f32_z(pg, svget4_f32(vin_deq, 3), vb_f32))),
                        }
                    }
                };
                // Re-quantize to new output space
                tmp = svquantize_z(pg, tmp_dep, qi_out);
            }
            else if(act == ActivationLayerInfo::ActivationFunction::HARD_SWISH)
            {
                // De-quantize
                const auto vin_deq = svdequantize_z(pg, vin, qi_in);
                // Perform activation
                const svfloat32x4_t tmp_dep =
                {
                    { {
                            svmul_f32_z(pg, svget4_f32(vin_deq, 0), svmul_f32_z(pg, const_inv_6_f32, svmin_f32_z(pg, const_6_f32, svmax_f32_z(pg, const_0_f32, svadd_f32_z(pg, svget4_f32(vin_deq, 0), const_3_f32))))),
                            svmul_f32_z(pg, svget4_f32(vin_deq, 1), svmul_f32_z(pg, const_inv_6_f32, svmin_f32_z(pg, const_6_f32, svmax_f32_z(pg, const_0_f32, svadd_f32_z(pg, svget4_f32(vin_deq, 1), const_3_f32))))),
                            svmul_f32_z(pg, svget4_f32(vin_deq, 2), svmul_f32_z(pg, const_inv_6_f32, svmin_f32_z(pg, const_6_f32, svmax_f32_z(pg, const_0_f32, svadd_f32_z(pg, svget4_f32(vin_deq, 2), const_3_f32))))),
                            svmul_f32_z(pg, svget4_f32(vin_deq, 3), svmul_f32_z(pg, const_inv_6_f32, svmin_f32_z(pg, const_6_f32, svmax_f32_z(pg, const_0_f32, svadd_f32_z(pg, svget4_f32(vin_deq, 3), const_3_f32))))),
                        }
                    }
                };
                // Re-quantize to new output space
                tmp = svquantize_z(pg, tmp_dep, qi_out);
            }
            else if(act == ActivationLayerInfo::ActivationFunction::LEAKY_RELU)
            {
                svbool_t    p0, p1, p2, p3;
                svint32x4_t tmp_dep;

                // Expand to int32
                const svint32x4_t vin_s32 =
                {
                    { {
                            svreinterpret_s32_u32(svmovlb_u32(svmovlb_u16(vin))),
                            svreinterpret_s32_u32(svmovlt_u32(svmovlb_u16(vin))),
                            svreinterpret_s32_u32(svmovlb_u32(svmovlt_u16(vin))),
                            svreinterpret_s32_u32(svmovlt_u32(svmovlt_u16(vin))),
                        }
                    }
                };

                // Compare elements to input offset
                if(qi_in.scale >= 0)
                {
                    p0 = svcmplt_s32(pg, svget4_s32(vin_s32, 0), voffset_in);
                    p1 = svcmplt_s32(pg, svget4_s32(vin_s32, 1), voffset_in);
                    p2 = svcmplt_s32(pg, svget4_s32(vin_s32, 2), voffset_in);
                    p3 = svcmplt_s32(pg, svget4_s32(vin_s32, 3), voffset_in);
                }
                else
                {
                    p0 = svcmpgt_s32(pg, svget4_s32(vin_s32, 0), voffset_in);
                    p1 = svcmpgt_s32(pg, svget4_s32(vin_s32, 1), voffset_in);
                    p2 = svcmpgt_s32(pg, svget4_s32(vin_s32, 2), voffset_in);
                    p3 = svcmpgt_s32(pg, svget4_s32(vin_s32, 3), voffset_in);
                }

                // Multiply negative elements and requantize if necessary
                if(requant)
                {
                    tmp_dep = svcreate4_s32(
                                  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),
                                  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),
                                  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),
                                  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));
                }
                else
                {
                    tmp_dep = svcreate4_s32(
                                  svasr_n_s32_m(p0, svmad_s32_m(p0, svget4_s32(vin_s32, 0), vs_leaky_s32, vo_leaky_s32), 8),
                                  svasr_n_s32_m(p1, svmad_s32_m(p1, svget4_s32(vin_s32, 1), vs_leaky_s32, vo_leaky_s32), 8),
                                  svasr_n_s32_m(p2, svmad_s32_m(p2, svget4_s32(vin_s32, 2), vs_leaky_s32, vo_leaky_s32), 8),
                                  svasr_n_s32_m(p3, svmad_s32_m(p3, svget4_s32(vin_s32, 3), vs_leaky_s32, vo_leaky_s32), 8));
                }

                // Convert uint32 vectors to uint16 vectors (with saturation)
                const auto v_low_u16  = svqxtunt_s32(svqxtunb_s32(svget4_s32(tmp_dep, 0)), svget4_s32(tmp_dep, 1));
                const auto v_high_u16 = svqxtunt_s32(svqxtunb_s32(svget4_s32(tmp_dep, 2)), svget4_s32(tmp_dep, 3));

                // convert uint16 vectors to uint8 vectors (with saturation)
                tmp = svqxtnt_u16(svqxtnb_u16(v_low_u16), v_high_u16);
            }
            else
            {
                ARM_COMPUTE_ERROR("Unsupported activation function");
            }

            svst1_u8(pg, output_ptr + x, tmp);

            x += svcntb();
            pg = svwhilelt_b8(x, window_end_x);

        }
        while(svptest_any(svptrue_b8(), pg));

    },
    input, output);
}
} // namespace cpu
} // namespace arm_compute
#endif /* defined(__ARM_FEATURE_SVE2) */