1 /*
2 * Copyright (c) 2020-2022 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
25 #include "arm_compute/core/Helpers.h"
26 #include "arm_compute/core/ITensor.h"
27 #include "arm_compute/core/Types.h"
28 #include "arm_compute/core/utils/misc/Traits.h"
29 #include "src/core/NEON/SVEMath.h"
30 #include "src/core/NEON/wrapper/intrinsics/intrinsics.h"
31 #include <arm_sve.h>
32
33 namespace arm_compute
34 {
35 namespace cpu
36 {
add_qsymm16_sve2(const ITensor * src0,const ITensor * src1,ITensor * dst,const ConvertPolicy & policy,const Window & window)37 void add_qsymm16_sve2(const ITensor *src0, const ITensor *src1, ITensor *dst, const ConvertPolicy &policy, const Window &window)
38 {
39 ARM_COMPUTE_UNUSED(policy);
40
41 // Create input windows
42 Window input1_win = window.broadcast_if_dimension_le_one(src0->info()->tensor_shape());
43 Window input2_win = window.broadcast_if_dimension_le_one(src1->info()->tensor_shape());
44
45 // Clear X Dimension on execution window as we handle manually
46 Window win = window;
47 win.set(Window::DimX, Window::Dimension(0, 1, 1));
48
49 const auto window_start_x = static_cast<int>(window.x().start());
50 const auto window_end_x = static_cast<int>(window.x().end());
51 const bool is_broadcast_across_x = src0->info()->tensor_shape().x() != src1->info()->tensor_shape().x();
52
53 const UniformQuantizationInfo iq1_info = src0->info()->quantization_info().uniform();
54 const UniformQuantizationInfo iq2_info = src1->info()->quantization_info().uniform();
55 const UniformQuantizationInfo oq_info = dst->info()->quantization_info().uniform();
56
57 const auto vscale1 = svdup_n_f32(iq1_info.scale);
58 const auto vscale2 = svdup_n_f32(iq2_info.scale);
59 const auto invvscaleo = svdup_n_f32(1.f / oq_info.scale);
60 const auto all_true_pg = svptrue_b16();
61
62 if(is_broadcast_across_x)
63 {
64 const bool is_broadcast_input_2 = input2_win.x().step() == 0;
65 Window broadcast_win = is_broadcast_input_2 ? input2_win : input1_win;
66 Window non_broadcast_win = !is_broadcast_input_2 ? input2_win : input1_win;
67 const ITensor *broadcast_tensor = is_broadcast_input_2 ? src1 : src0;
68 const ITensor *non_broadcast_tensor = !is_broadcast_input_2 ? src1 : src0;
69
70 // Clear X Dimension on execution window as we handle manually
71 non_broadcast_win.set(Window::DimX, Window::Dimension(0, 1, 1));
72
73 Iterator broadcast_input(broadcast_tensor, broadcast_win);
74 Iterator non_broadcast_input(non_broadcast_tensor, non_broadcast_win);
75 Iterator output(dst, win);
76
77 execute_window_loop(win, [&](const Coordinates &)
78 {
79 const auto non_broadcast_input_ptr = reinterpret_cast<const int16_t *>(non_broadcast_input.ptr());
80 const auto output_ptr = reinterpret_cast<int16_t *>(output.ptr());
81
82 const int16_t broadcast_value = *reinterpret_cast<const int16_t *>(broadcast_input.ptr());
83 const auto broadcast_value_vec = svdup_n_s16(broadcast_value);
84
85 int x = window_start_x;
86 svbool_t pg = svwhilelt_b16(x, window_end_x);
87
88 const auto bf_0 = svmul_f32_z(pg, svcvt_f32_s32_z(pg, svmovlb_s32(broadcast_value_vec)), vscale2);
89 const auto bf_1 = svmul_f32_z(pg, svcvt_f32_s32_z(pg, svmovlt_s32(broadcast_value_vec)), vscale2);
90
91 do
92 {
93 const auto a = svld1_s16(pg, non_broadcast_input_ptr + x);
94 const auto af_0 = svmul_f32_z(pg, svcvt_f32_s32_z(pg, svmovlb_s32(a)), vscale1);
95 const auto af_1 = svmul_f32_z(pg, svcvt_f32_s32_z(pg, svmovlt_s32(a)), vscale1);
96
97 const auto rf_0 = svcvt_s32_f32_z(pg, svmul_f32_z(pg, svadd_f32_z(pg, af_0, bf_0), invvscaleo));
98 const auto rf_1 = svcvt_s32_f32_z(pg, svmul_f32_z(pg, svadd_f32_z(pg, af_1, bf_1), invvscaleo));
99
100 const auto res = svqxtnt_s32(svqxtnb_s32(rf_0), rf_1);
101
102 svst1_s16(pg, output_ptr + x, res);
103
104 x += svcnth();
105 pg = svwhilelt_b16(x, window_end_x);
106 }
107 while(svptest_any(all_true_pg, pg));
108 },
109 broadcast_input, non_broadcast_input, output);
110 }
111 else
112 {
113 // Clear X Dimension on execution window as we handle manually
114 input1_win.set(Window::DimX, Window::Dimension(0, 1, 1));
115 input2_win.set(Window::DimX, Window::Dimension(0, 1, 1));
116
117 Iterator input1(src0, input1_win);
118 Iterator input2(src1, input2_win);
119 Iterator output(dst, win);
120
121 execute_window_loop(win, [&](const Coordinates &)
122 {
123 const auto input1_ptr = reinterpret_cast<const int16_t *>(input1.ptr());
124 const auto input2_ptr = reinterpret_cast<const int16_t *>(input2.ptr());
125 const auto output_ptr = reinterpret_cast<int16_t *>(output.ptr());
126
127 int x = window_start_x;
128 svbool_t pg = svwhilelt_b16(x, window_end_x);
129 do
130 {
131 auto a = svld1_s16(pg, input1_ptr + x);
132 auto b = svld1_s16(pg, input2_ptr + x);
133
134 const auto af_0 = svmul_f32_z(pg, svcvt_f32_s32_z(pg, svmovlb_s32(a)), vscale1);
135 const auto af_1 = svmul_f32_z(pg, svcvt_f32_s32_z(pg, svmovlt_s32(a)), vscale1);
136
137 const auto bf_0 = svmul_f32_z(pg, svcvt_f32_s32_z(pg, svmovlb_s32(b)), vscale2);
138 const auto bf_1 = svmul_f32_z(pg, svcvt_f32_s32_z(pg, svmovlt_s32(b)), vscale2);
139
140 const auto rf_0 = svcvt_s32_f32_z(pg, svmul_f32_z(pg, svadd_f32_z(pg, af_0, bf_0), invvscaleo));
141 const auto rf_1 = svcvt_s32_f32_z(pg, svmul_f32_z(pg, svadd_f32_z(pg, af_1, bf_1), invvscaleo));
142
143 const auto res = svqxtnt_s32(svqxtnb_s32(rf_0), rf_1);
144 svst1_s16(pg, output_ptr + x, res);
145
146 x += svcnth();
147 pg = svwhilelt_b16(x, window_end_x);
148 }
149 while(svptest_any(all_true_pg, pg));
150 },
151 input1, input2, output);
152 }
153 }
154 } // namespace cpu
155 } // namespace arm_compute
156