blob: 2c9e411743d97936150262717d39943e869cb737 [file] [log] [blame]
Sheri Zhangfc6744a2021-01-13 15:54:05 +00001/*
2 * Copyright (c) 2021 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/ITensor.h"
26#include "arm_compute/core/Types.h"
27#include "arm_compute/core/utils/misc/Traits.h"
28#include "src/core/NEON/wrapper/intrinsics/intrinsics.h"
29#include "src/core/helpers/WindowHelpers.h"
30
31namespace arm_compute
32{
33namespace cpu
34{
35void sub_qasymm8_signed_neon(const ITensor *src0, const ITensor *src1, ITensor *dst, const ConvertPolicy &policy, const Window &window)
36{
37 ARM_COMPUTE_UNUSED(policy);
38
39 // Create input windows
40 Window input1_win = window.broadcast_if_dimension_le_one(src0->info()->tensor_shape());
41 Window input2_win = window.broadcast_if_dimension_le_one(src1->info()->tensor_shape());
42
43 // Clear X Dimension on execution window as we handle manually
44 Window win = window;
45 win.set(Window::DimX, Window::Dimension(0, 1, 1));
46
47 const int window_step_x = 16;
48 const auto window_start_x = static_cast<int>(window.x().start());
49 const auto window_end_x = static_cast<int>(window.x().end());
50 const bool is_broadcast_across_x = src0->info()->tensor_shape().x() != src1->info()->tensor_shape().x();
51
52 const UniformQuantizationInfo iq1_info = src0->info()->quantization_info().uniform();
53 const UniformQuantizationInfo iq2_info = src1->info()->quantization_info().uniform();
54 const UniformQuantizationInfo oq_info = dst->info()->quantization_info().uniform();
55
56 const float32x4_t invvscaleo = vdupq_n_f32(1.f / oq_info.scale);
57 const float32x4_t voffseto = vdupq_n_f32(oq_info.offset);
58
59 if(is_broadcast_across_x)
60 {
61 const bool is_broadcast_input_2 = input2_win.x().step() == 0;
62 Window broadcast_win = is_broadcast_input_2 ? input2_win : input1_win;
63 Window non_broadcast_win = !is_broadcast_input_2 ? input2_win : input1_win;
64 const ITensor *broadcast_tensor = is_broadcast_input_2 ? src1 : src0;
65 const ITensor *non_broadcast_tensor = !is_broadcast_input_2 ? src1 : src0;
66 const UniformQuantizationInfo broadcast_qinfo = broadcast_tensor->info()->quantization_info().uniform();
67 const UniformQuantizationInfo non_broadcast_qinfo = non_broadcast_tensor->info()->quantization_info().uniform();
68 const float32x4_t vscale1 = is_broadcast_input_2 ? vdupq_n_f32(iq1_info.scale) : vdupq_n_f32(iq2_info.scale);
69 const float32x4_t vscale2 = is_broadcast_input_2 ? vdupq_n_f32(iq2_info.scale) : vdupq_n_f32(iq1_info.scale);
70 const int32x4_t voffset1 = is_broadcast_input_2 ? vdupq_n_s32(iq1_info.offset) : vdupq_n_s32(iq2_info.offset);
71 const int32x4_t voffset2 = is_broadcast_input_2 ? vdupq_n_s32(iq2_info.offset) : vdupq_n_s32(iq1_info.offset);
72
73 // Clear X Dimension on execution window as we handle manually
74 non_broadcast_win.set(Window::DimX, Window::Dimension(0, 1, 1));
75
76 Iterator broadcast_input(broadcast_tensor, broadcast_win);
77 Iterator non_broadcast_input(non_broadcast_tensor, non_broadcast_win);
78 Iterator output(dst, win);
79
80 execute_window_loop(win, [&](const Coordinates &)
81 {
82 const auto non_broadcast_input_ptr = reinterpret_cast<const int8_t *>(non_broadcast_input.ptr());
83 const auto output_ptr = reinterpret_cast<int8_t *>(output.ptr());
84
85 const auto broadcast_value = *reinterpret_cast<const int8_t *>(broadcast_input.ptr());
86 const auto broadcast_value_vec = wrapper::vdup_n(static_cast<int8_t>(broadcast_value), wrapper::traits::vector_128_tag{});
87
88 const float32x4x4_t bf =
89 {
90 {
91 vmulq_f32(vcvtq_f32_s32(vsubq_s32(wrapper::vreinterpret(wrapper::vmovl(wrapper::vgetlow(wrapper::vmovl(wrapper::vgetlow(broadcast_value_vec))))), voffset2)), vscale2),
92 vmulq_f32(vcvtq_f32_s32(vsubq_s32(wrapper::vreinterpret(wrapper::vmovl(wrapper::vgethigh(wrapper::vmovl(wrapper::vgetlow(broadcast_value_vec))))), voffset2)), vscale2),
93 vmulq_f32(vcvtq_f32_s32(vsubq_s32(wrapper::vreinterpret(wrapper::vmovl(wrapper::vgetlow(wrapper::vmovl(wrapper::vgethigh(broadcast_value_vec))))), voffset2)), vscale2),
94 vmulq_f32(vcvtq_f32_s32(vsubq_s32(wrapper::vreinterpret(wrapper::vmovl(wrapper::vgethigh(wrapper::vmovl(wrapper::vgethigh(broadcast_value_vec))))), voffset2)), vscale2),
95 }
96 };
97
98 // Compute S elements per iteration
99 int x = window_start_x;
100 for(; x <= (window_end_x - window_step_x); x += window_step_x)
101 {
102 const auto a = wrapper::vloadq(non_broadcast_input_ptr + x);
103
104 const float32x4x4_t af =
105 {
106 {
107 vmulq_f32(vcvtq_f32_s32(vsubq_s32(wrapper::vreinterpret(wrapper::vmovl(wrapper::vgetlow(wrapper::vmovl(wrapper::vgetlow(a))))), voffset1)), vscale1),
108 vmulq_f32(vcvtq_f32_s32(vsubq_s32(wrapper::vreinterpret(wrapper::vmovl(wrapper::vgethigh(wrapper::vmovl(wrapper::vgetlow(a))))), voffset1)), vscale1),
109 vmulq_f32(vcvtq_f32_s32(vsubq_s32(wrapper::vreinterpret(wrapper::vmovl(wrapper::vgetlow(wrapper::vmovl(wrapper::vgethigh(a))))), voffset1)), vscale1),
110 vmulq_f32(vcvtq_f32_s32(vsubq_s32(wrapper::vreinterpret(wrapper::vmovl(wrapper::vgethigh(wrapper::vmovl(wrapper::vgethigh(a))))), voffset1)), vscale1),
111 }
112 };
113
114 const int32x4x4_t rf =
115 {
116 {
117#ifdef __aarch64_
118 vcvtnq_s32_f32(vmlaq_f32(voffseto, !is_broadcast_input_2 ? vsubq_f32(bf.val[0], af.val[0]) : vsubq_f32(af.val[0], bf.val[0]), invvscaleo)),
119 vcvtnq_s32_f32(vmlaq_f32(voffseto, !is_broadcast_input_2 ? vsubq_f32(bf.val[1], af.val[1]) : vsubq_f32(af.val[1], bf.val[1]), invvscaleo)),
120 vcvtnq_s32_f32(vmlaq_f32(voffseto, !is_broadcast_input_2 ? vsubq_f32(bf.val[2], af.val[2]) : vsubq_f32(af.val[2], bf.val[2]), invvscaleo)),
121 vcvtnq_s32_f32(vmlaq_f32(voffseto, !is_broadcast_input_2 ? vsubq_f32(bf.val[3], af.val[3]) : vsubq_f32(af.val[3], bf.val[3]), invvscaleo)),
122#else //__aarch64__
123 vcvtq_s32_f32(vmlaq_f32(voffseto, !is_broadcast_input_2 ? vsubq_f32(bf.val[0], af.val[0]) : vsubq_f32(af.val[0], bf.val[0]), invvscaleo)),
124 vcvtq_s32_f32(vmlaq_f32(voffseto, !is_broadcast_input_2 ? vsubq_f32(bf.val[1], af.val[1]) : vsubq_f32(af.val[1], bf.val[1]), invvscaleo)),
125 vcvtq_s32_f32(vmlaq_f32(voffseto, !is_broadcast_input_2 ? vsubq_f32(bf.val[2], af.val[2]) : vsubq_f32(af.val[2], bf.val[2]), invvscaleo)),
126 vcvtq_s32_f32(vmlaq_f32(voffseto, !is_broadcast_input_2 ? vsubq_f32(bf.val[3], af.val[3]) : vsubq_f32(af.val[3], bf.val[3]), invvscaleo)),
127#endif //__aarch64__
128 }
129 };
130
131 const auto pa = vqmovn_s16(vcombine_s16(vqmovn_s32(rf.val[0]), vqmovn_s32(rf.val[1])));
132 const auto pb = vqmovn_s16(vcombine_s16(vqmovn_s32(rf.val[2]), vqmovn_s32(rf.val[3])));
133 wrapper::vstore(output_ptr + x, wrapper::vcombine(pa, pb));
134 }
135
136 // Compute left-over elements
137 for(; x < window_end_x; ++x)
138 {
139 const float afs = static_cast<int32_t>(*(non_broadcast_input_ptr + x) - non_broadcast_qinfo.offset) * non_broadcast_qinfo.scale;
140 const float bfs = static_cast<int32_t>(broadcast_value - broadcast_qinfo.offset) * broadcast_qinfo.scale;
141 *(output_ptr + x) = quantize_qasymm8_signed(is_broadcast_input_2 ? afs - bfs : bfs - afs, dst->info()->quantization_info());
142 }
143 },
144 broadcast_input, non_broadcast_input, output);
145 }
146 else
147 {
148 const float32x4_t vscale1 = vdupq_n_f32(iq1_info.scale);
149 const float32x4_t vscale2 = vdupq_n_f32(iq2_info.scale);
150 const int32x4_t voffset1 = vdupq_n_s32(iq1_info.offset);
151 const int32x4_t voffset2 = vdupq_n_s32(iq2_info.offset);
152
153 // Clear X Dimension on execution window as we handle manually
154 input1_win.set(Window::DimX, Window::Dimension(0, 1, 1));
155 input2_win.set(Window::DimX, Window::Dimension(0, 1, 1));
156
157 Iterator input1(src0, input1_win);
158 Iterator input2(src1, input2_win);
159 Iterator output(dst, win);
160
161 execute_window_loop(win, [&](const Coordinates &)
162 {
163 const auto input1_ptr = reinterpret_cast<const int8_t *>(input1.ptr());
164 const auto input2_ptr = reinterpret_cast<const int8_t *>(input2.ptr());
165 const auto output_ptr = reinterpret_cast<int8_t *>(output.ptr());
166
167 // Compute S elements per iteration
168 int x = window_start_x;
169 for(; x <= (window_end_x - window_step_x); x += window_step_x)
170 {
171 const auto a = wrapper::vloadq(input1_ptr + x);
172 const auto b = wrapper::vloadq(input2_ptr + x);
173
174 const float32x4x4_t af =
175 {
176 {
177 vmulq_f32(vcvtq_f32_s32(vsubq_s32(wrapper::vreinterpret(wrapper::vmovl(wrapper::vgetlow(wrapper::vmovl(wrapper::vgetlow(a))))), voffset1)), vscale1),
178 vmulq_f32(vcvtq_f32_s32(vsubq_s32(wrapper::vreinterpret(wrapper::vmovl(wrapper::vgethigh(wrapper::vmovl(wrapper::vgetlow(a))))), voffset1)), vscale1),
179 vmulq_f32(vcvtq_f32_s32(vsubq_s32(wrapper::vreinterpret(wrapper::vmovl(wrapper::vgetlow(wrapper::vmovl(wrapper::vgethigh(a))))), voffset1)), vscale1),
180 vmulq_f32(vcvtq_f32_s32(vsubq_s32(wrapper::vreinterpret(wrapper::vmovl(wrapper::vgethigh(wrapper::vmovl(wrapper::vgethigh(a))))), voffset1)), vscale1),
181 }
182 };
183
184 const float32x4x4_t bf =
185 {
186 {
187 vmulq_f32(vcvtq_f32_s32(vsubq_s32(wrapper::vreinterpret(wrapper::vmovl(wrapper::vgetlow(wrapper::vmovl(wrapper::vgetlow(b))))), voffset2)), vscale2),
188 vmulq_f32(vcvtq_f32_s32(vsubq_s32(wrapper::vreinterpret(wrapper::vmovl(wrapper::vgethigh(wrapper::vmovl(wrapper::vgetlow(b))))), voffset2)), vscale2),
189 vmulq_f32(vcvtq_f32_s32(vsubq_s32(wrapper::vreinterpret(wrapper::vmovl(wrapper::vgetlow(wrapper::vmovl(wrapper::vgethigh(b))))), voffset2)), vscale2),
190 vmulq_f32(vcvtq_f32_s32(vsubq_s32(wrapper::vreinterpret(wrapper::vmovl(wrapper::vgethigh(wrapper::vmovl(wrapper::vgethigh(b))))), voffset2)), vscale2),
191 }
192 };
193
194 const int32x4x4_t rf =
195 {
196 {
197#ifdef __aarch64__
198 vcvtnq_s32_f32(vmlaq_f32(voffseto, vsubq_f32(af.val[0], bf.val[0]), invvscaleo)),
199 vcvtnq_s32_f32(vmlaq_f32(voffseto, vsubq_f32(af.val[1], bf.val[1]), invvscaleo)),
200 vcvtnq_s32_f32(vmlaq_f32(voffseto, vsubq_f32(af.val[2], bf.val[2]), invvscaleo)),
201 vcvtnq_s32_f32(vmlaq_f32(voffseto, vsubq_f32(af.val[3], bf.val[3]), invvscaleo)),
202#else //__aarch64__
203 vcvtq_s32_f32(vmlaq_f32(voffseto, vsubq_f32(af.val[0], bf.val[0]), invvscaleo)),
204 vcvtq_s32_f32(vmlaq_f32(voffseto, vsubq_f32(af.val[1], bf.val[1]), invvscaleo)),
205 vcvtq_s32_f32(vmlaq_f32(voffseto, vsubq_f32(af.val[2], bf.val[2]), invvscaleo)),
206 vcvtq_s32_f32(vmlaq_f32(voffseto, vsubq_f32(af.val[3], bf.val[3]), invvscaleo)),
207#endif //__aarch64__
208 }
209 };
210
211 const auto pa = vqmovn_s16(vcombine_s16(vqmovn_s32(rf.val[0]), vqmovn_s32(rf.val[1])));
212 const auto pb = vqmovn_s16(vcombine_s16(vqmovn_s32(rf.val[2]), vqmovn_s32(rf.val[3])));
213 wrapper::vstore(output_ptr + x, wrapper::vcombine(pa, pb));
214 }
215
216 // Compute left-over elements
217 for(; x < window_end_x; ++x)
218 {
219 const float afs = static_cast<int32_t>((*(input1_ptr + x)) - iq1_info.offset) * iq1_info.scale;
220 const float bfs = static_cast<int32_t>((*(input2_ptr + x)) - iq2_info.offset) * iq2_info.scale;
221
222 *(output_ptr + x) = quantize_qasymm8_signed((afs - bfs), dst->info()->quantization_info());
223 }
224 },
225 input1, input2, output);
226 }
227}
228} // namespace cpu
229} // namespace arm_compute