aboutsummaryrefslogtreecommitdiff
path: root/src/cpu/kernels/elementwise_unary/generic/neon/impl.h
blob: d54d3984cbd9fd5d3c5820ea3f398618fccdb3bc (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
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
/*
 * Copyright (c) 2018-2023 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.
 */
#ifndef SRC_CORE_NEON_KERNELS_ELEMENTWISE_UNARY_LIST_H
#define SRC_CORE_NEON_KERNELS_ELEMENTWISE_UNARY_LIST_H

#include "arm_compute/core/Helpers.h"
#include "arm_compute/core/Types.h"

#include "src/core/NEON/NEAsymm.h"
#include "src/core/NEON/wrapper/intrinsics/intrinsics.h"

namespace arm_compute
{
namespace cpu
{
template <typename ScalarType>
inline ScalarType elementwise_op_scalar_imp(ElementWiseUnary op, const ScalarType &a)
{
    switch (op)
    {
        case ElementWiseUnary::RSQRT:
            return 1 / sqrt(a);
        case ElementWiseUnary::EXP:
            return std::exp(a);
        case ElementWiseUnary::NEG:
            return -a;
        case ElementWiseUnary::LOG:
            return std::log(a);
        case ElementWiseUnary::ABS:
            return std::abs(a);
        case ElementWiseUnary::ROUND:
            return support::cpp11::nearbyint(a);
        case ElementWiseUnary::SIN:
            return std::sin(a);
        default:
            ARM_COMPUTE_ERROR("NOT_SUPPORTED!");
    }
}

template <typename ScalarType, typename VectorType>
inline VectorType elementwise_op_imp(ElementWiseUnary op, const VectorType &a)
{
    switch (op)
    {
        case ElementWiseUnary::RSQRT:
            return wrapper::vinvsqrt(a);
        case ElementWiseUnary::EXP:
            return wrapper::vexpq(a);
        case ElementWiseUnary::NEG:
            return wrapper::vneg(a);
        case ElementWiseUnary::LOG:
            return wrapper::vlog(a);
        case ElementWiseUnary::ABS:
            return wrapper::vabs(a);
        case ElementWiseUnary::ROUND:
            return wrapper::vround(a);
        case ElementWiseUnary::SIN:
            return wrapper::vsin(a);
        default:
            ARM_COMPUTE_ERROR("NOT_SUPPORTED!");
    }
}

template <typename ScalarType>
inline void elementwise_op(const ITensor *in, ITensor *out, const Window &window, ElementWiseUnary op)
{
    const int  window_step_x  = 16 / sizeof(ScalarType);
    const auto window_start_x = static_cast<int>(window.x().start());
    const auto window_end_x   = static_cast<int>(window.x().end());

    Window win = window;
    win.set(Window::DimX, Window::Dimension(0, 1, 1));

    Iterator input(in, win);
    Iterator output(out, win);

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

            int x = window_start_x;
            for (; x <= window_end_x - window_step_x; x += window_step_x)
            {
                wrapper::vstore(output_ptr + x, elementwise_op_imp<ScalarType>(op, wrapper::vloadq(input_ptr + x)));
            }
            for (; x < window_end_x; ++x)
            {
                *(output_ptr + x) = elementwise_op_scalar_imp(op, *(input_ptr + x));
            }
        },
        input, output);
}

template <>
inline void elementwise_op<int8_t>(const ITensor *in, ITensor *out, const Window &window, ElementWiseUnary op)
{
    const int                     window_step_x     = 16;
    const auto                    window_start_x    = static_cast<int>(window.x().start());
    const auto                    window_end_x      = static_cast<int>(window.x().end());
    const UniformQuantizationInfo qi_in             = in->info()->quantization_info().uniform();
    const UniformQuantizationInfo qi_out            = out->info()->quantization_info().uniform();
    const auto                    min_clamped_value = vdupq_n_f32((-128 - qi_out.offset) * qi_out.scale);
    const auto                    max_clamped_value = vdupq_n_f32((127 - qi_out.offset) * qi_out.scale);
    Window                        win               = window;
    win.set(Window::DimX, Window::Dimension(0, 1, 1));

    Iterator input(in, win);
    Iterator output(out, win);

    execute_window_loop(
        win,
        [&](const Coordinates &)
        {
            int8x16_t  vout;
            auto       output_ptr    = reinterpret_cast<int8_t *>(output.ptr());
            const auto input_ptr     = reinterpret_cast<const int8_t *>(input.ptr());
            const auto vconst_0_f32  = vdupq_n_f32(0);
            auto       clamped_value = (op == ElementWiseUnary::LOG) ? min_clamped_value : max_clamped_value;

            int x = window_start_x;
            for (; x <= window_end_x - window_step_x; x += window_step_x)
            {
                const auto vin = wrapper::vloadq(input_ptr + x);

                // De-quantize
                const auto vin_deq = vdequantize(vin, qi_in);

                // Perform activation
                float32x4x4_t vtmp_deq = {{
                    elementwise_op_imp<float>(op, vin_deq.val[0]),
                    elementwise_op_imp<float>(op, vin_deq.val[1]),
                    elementwise_op_imp<float>(op, vin_deq.val[2]),
                    elementwise_op_imp<float>(op, vin_deq.val[3]),
                }};

                if ((op == ElementWiseUnary::LOG) || (op == ElementWiseUnary::RSQRT))
                {
                    vtmp_deq.val[0] =
                        vbslq_f32(vcleq_f32(vin_deq.val[0], vconst_0_f32), clamped_value, vtmp_deq.val[0]);
                    vtmp_deq.val[1] =
                        vbslq_f32(vcleq_f32(vin_deq.val[1], vconst_0_f32), clamped_value, vtmp_deq.val[1]);
                    vtmp_deq.val[2] =
                        vbslq_f32(vcleq_f32(vin_deq.val[2], vconst_0_f32), clamped_value, vtmp_deq.val[2]);
                    vtmp_deq.val[3] =
                        vbslq_f32(vcleq_f32(vin_deq.val[3], vconst_0_f32), clamped_value, vtmp_deq.val[3]);
                }

                // Re-quantize to new output space
                vout = vquantize_signed(vtmp_deq, qi_out);
                wrapper::vstore(output_ptr + x, vout);
            }
            for (; x < window_end_x; ++x)
            {
                qasymm8_signed_t in    = *(reinterpret_cast<const qasymm8_signed_t *>(input_ptr + x));
                qasymm8_signed_t tmp   = 0;
                float            tmp_f = dequantize_qasymm8_signed(in, qi_in);
                if (tmp_f <= 0.0)
                {
                    if (op == ElementWiseUnary::LOG)
                    {
                        tmp_f = (-128 - qi_out.offset) * qi_out.scale;
                    }
                    else if (op == ElementWiseUnary::RSQRT)
                    {
                        tmp_f = (127 - qi_out.offset) * qi_out.scale;
                    }
                    else
                    {
                        tmp_f = elementwise_op_scalar_imp<float>(op, tmp_f);
                    }
                }
                else
                {
                    tmp_f = elementwise_op_scalar_imp<float>(op, tmp_f);
                }
                tmp = quantize_qasymm8_signed(
                    tmp_f, qi_out,
                    RoundingPolicy::
                        TO_ZERO); // Set rounding policy TO_ZERO to be compatible with vquantize_signed() used above that follow same policy for armv7a.
                // For aarch64 LUT is used and rounding to nearest is used
                *(output_ptr + x) = tmp;
            }
        },
        input, output);
}
template <>
inline void elementwise_op<uint8_t>(const ITensor *in, ITensor *out, const Window &window, ElementWiseUnary op)
{
    const int                     window_step_x     = 16;
    const auto                    window_start_x    = static_cast<int>(window.x().start());
    const auto                    window_end_x      = static_cast<int>(window.x().end());
    const UniformQuantizationInfo qi_in             = in->info()->quantization_info().uniform();
    const UniformQuantizationInfo qi_out            = out->info()->quantization_info().uniform();
    const auto                    vconst_0_f32      = vdupq_n_f32(0);
    const auto                    min_clamped_value = vdupq_n_f32((0 - qi_out.offset) * qi_out.scale);
    const auto                    max_clamped_value = vdupq_n_f32((255 - qi_out.offset) * qi_out.scale);
    Window                        win               = window;
    win.set(Window::DimX, Window::Dimension(0, 1, 1));

    Iterator input(in, win);
    Iterator output(out, win);

    execute_window_loop(
        win,
        [&](const Coordinates &)
        {
            uint8x16_t vout;
            auto       clamped_value = (op == ElementWiseUnary::LOG) ? min_clamped_value : max_clamped_value;
            auto       output_ptr    = reinterpret_cast<uint8_t *>(output.ptr());
            const auto input_ptr     = reinterpret_cast<const uint8_t *>(input.ptr());
            int        x             = window_start_x;
            for (; x <= window_end_x - window_step_x; x += window_step_x)
            {
                const auto vin = wrapper::vloadq(input_ptr + x);

                // De-quantize
                const auto vin_deq = vdequantize(vin, qi_in);

                // Perform activation
                float32x4x4_t vtmp_deq = {{
                    elementwise_op_imp<float>(op, vin_deq.val[0]),
                    elementwise_op_imp<float>(op, vin_deq.val[1]),
                    elementwise_op_imp<float>(op, vin_deq.val[2]),
                    elementwise_op_imp<float>(op, vin_deq.val[3]),
                }};
                if ((op == ElementWiseUnary::LOG) || (op == ElementWiseUnary::RSQRT))
                {
                    vtmp_deq.val[0] =
                        vbslq_f32(vcleq_f32(vin_deq.val[0], vconst_0_f32), clamped_value, vtmp_deq.val[0]);
                    vtmp_deq.val[1] =
                        vbslq_f32(vcleq_f32(vin_deq.val[1], vconst_0_f32), clamped_value, vtmp_deq.val[1]);
                    vtmp_deq.val[2] =
                        vbslq_f32(vcleq_f32(vin_deq.val[2], vconst_0_f32), clamped_value, vtmp_deq.val[2]);
                    vtmp_deq.val[3] =
                        vbslq_f32(vcleq_f32(vin_deq.val[3], vconst_0_f32), clamped_value, vtmp_deq.val[3]);
                }

                // Re-quantize to new output space
                vout = vquantize(vtmp_deq, qi_out);
                wrapper::vstore(output_ptr + x, vout);
            }
            for (; x < window_end_x; ++x)
            {
                qasymm8_t in    = *(reinterpret_cast<const qasymm8_t *>(input_ptr + x));
                qasymm8_t tmp   = 0;
                float     tmp_f = dequantize_qasymm8(in, qi_in);
                if (tmp_f <= 0.0)
                {
                    if (op == ElementWiseUnary::LOG)
                    {
                        tmp_f = (0 - qi_out.offset) * qi_out.scale;
                    }
                    else if (op == ElementWiseUnary::RSQRT)
                    {
                        tmp_f = (255 - qi_out.offset) * qi_out.scale;
                    }
                    else
                    {
                        tmp_f = elementwise_op_scalar_imp<float>(op, tmp_f);
                    }
                }
                else
                {
                    tmp_f = elementwise_op_scalar_imp<float>(op, tmp_f);
                }
                tmp               = quantize_qasymm8(tmp_f, qi_out, RoundingPolicy::TO_ZERO);
                *(output_ptr + x) = tmp;
            }
        },
        input, output);
}

} // namespace cpu
} // namespace arm_compute

#endif // SRC_CORE_NEON_KERNELS_ELEMENTWISE_UNARY_LIST_H