• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1// Copyright 2021 Google LLC
2//
3// This source code is licensed under the BSD-style license found in the
4// LICENSE file in the root directory of this source tree.
5
6$ABC = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"
7$assert NR % 8 == 0
8$assert 8 <= NR <= 16
9#include <assert.h>
10
11#include <arm_neon.h>
12
13#include <xnnpack/gemm.h>
14#include <xnnpack/math.h>
15
16
17void xnn_qs8_igemm_minmax_ukernel_${MR}x${NR}c2__neon_${"mlal" if MLA else "mull"}_padal_dup(
18    size_t mr,
19    size_t nc,
20    size_t kc,
21    size_t ks,
22    const int8_t** restrict a,
23    const void* restrict w,
24    int8_t* restrict c,
25    size_t cm_stride,
26    size_t cn_stride,
27    size_t a_offset,
28    const int8_t* zero,
29    const union xnn_qs8_gemm_params params[restrict XNN_MIN_ELEMENTS(1)]) XNN_DISABLE_TSAN
30{
31  assert(mr != 0);
32  assert(mr <= ${MR});
33  assert(nc != 0);
34  assert(kc != 0);
35  assert(ks != 0);
36  assert(ks % (${MR} * sizeof(void*)) == 0);
37  assert(a_offset % sizeof(int8_t) == 0);
38  assert(a != NULL);
39  assert(w != NULL);
40  assert(c != NULL);
41
42  kc = round_up_po2(kc, 2);
43  int8_t* c0 = c;
44  $for M in range(1, MR):
45    int8_t* c${M} = (int8_t*) ((uintptr_t) c${M-1} + cm_stride);
46    $if M % 2 == 0:
47      if XNN_UNPREDICTABLE(mr <= ${M}) {
48        c${M} = c${M-1};
49      }
50    $elif M + 1 == MR:
51      if XNN_UNPREDICTABLE(mr != ${M+1}) {
52        c${M} = c${M-1};
53      }
54    $else:
55      if XNN_UNPREDICTABLE(mr < ${M+1}) {
56        c${M} = c${M-1};
57      }
58
59  do {
60    $for N in range(0, NR, 4):
61      int32x4_t vacc0x${ABC[N:N+4]} = vld1q_s32(w); w = (const void*) ((uintptr_t) w + 4 * sizeof(int32_t));
62    $for M in range(1, MR):
63      $for N in range(0, NR, 4):
64        int32x4_t vacc${M}x${ABC[N:N+4]} = vacc0x${ABC[N:N+4]};
65
66    size_t p = ks;
67    do {
68      $for M in range(MR):
69        const int8_t* restrict a${M} = a[${M}];
70        if XNN_UNPREDICTABLE(a${M} != zero) {
71          a${M} = (const int8_t*) ((uintptr_t) a${M} + a_offset);
72        }
73      a += ${MR};
74
75      size_t k = kc;
76
77      $if MLA:
78        while (k >= 16 * sizeof(int8_t)) {
79          $for M in range(MR):
80            const int8x8_t va${M}x0 = vld1_s8(a${M}); a${M} += 8;
81            const int8x8_t va${M}x1 = vld1_s8(a${M}); a${M} += 8;
82
83          $for K in range(4):
84            $for N in range(0, NR, 4):
85              const int8x8_t vb${ABC[N:N+4]}c${K}x0 = vld1_s8(w); w = (const void*) ((uintptr_t) w + 8 * sizeof(int8_t));
86
87          $for K in range(4):
88            $for N in range(0, NR, 4):
89              $for M in range(MR):
90                int16x8_t vprod${M}x${ABC[N:N+4]}c${K} = vmull_s8(vb${ABC[N:N+4]}c${K}x0, vreinterpret_s8_s16(vdup_lane_s16(vreinterpret_s16_s8(va${M}x0), ${K})));
91              const int8x8_t vb${ABC[N:N+4]}c${K}x1 = vld1_s8(w); w = (const void*) ((uintptr_t) w + 8 * sizeof(int8_t));
92              $for M in range(MR):
93                vprod${M}x${ABC[N:N+4]}c${K} = vmlal_s8(vprod${M}x${ABC[N:N+4]}c${K}, vb${ABC[N:N+4]}c${K}x1, vreinterpret_s8_s16(vdup_lane_s16(vreinterpret_s16_s8(va${M}x1), ${K})));
94              $for M in range(MR):
95                vacc${M}x${ABC[N:N+4]} = vpadalq_s16(vacc${M}x${ABC[N:N+4]}, vprod${M}x${ABC[N:N+4]}c${K});
96
97          k -= 16 * sizeof(int8_t);
98        }
99
100      ${"if" if MLA else "while"} (k >= 8 * sizeof(int8_t)) {
101        $for M in range(MR):
102          const int8x8_t va${M} = vld1_s8(a${M}); a${M} += 8;
103
104        $for K in range(4):
105          $for N in range(0, NR, 4):
106            const int8x8_t vb${ABC[N:N+4]}c${K} = vld1_s8(w); w = (const void*) ((uintptr_t) w + 8 * sizeof(int8_t));
107
108        $for M in range(MR):
109          $for N in range(0, NR, 4):
110            $for K in range(4):
111              const int16x8_t vprod${M}x${ABC[N:N+4]}c${K} = vmull_s8(vb${ABC[N:N+4]}c${K}, vreinterpret_s8_s16(vdup_lane_s16(vreinterpret_s16_s8(va${M}), ${K})));
112            $for K in range(4):
113              vacc${M}x${ABC[N:N+4]} = vpadalq_s16(vacc${M}x${ABC[N:N+4]}, vprod${M}x${ABC[N:N+4]}c${K});
114
115        k -= 8 * sizeof(int8_t);
116      }
117
118      if XNN_UNLIKELY(k != 0) {
119        $for M in range(MR):
120          const int8x8_t va${M} = vld1_s8(a${M}); a${M} = (const int8_t*) ((uintptr_t) a${M} + k);
121
122        $for N in range(0, NR, 4):
123          const int8x8_t vb${ABC[N:N+4]}c0 = vld1_s8(w); w = (const void*) ((uintptr_t) w + 8 * sizeof(int8_t));
124
125        $for M in range(MR):
126          $for N in range(0, NR, 4):
127            const int16x8_t vprod${M}x${ABC[N:N+4]}c0 = vmull_s8(vb${ABC[N:N+4]}c0, vreinterpret_s8_s16(vdup_lane_s16(vreinterpret_s16_s8(va${M}), 0)));
128            vacc${M}x${ABC[N:N+4]} = vpadalq_s16(vacc${M}x${ABC[N:N+4]}, vprod${M}x${ABC[N:N+4]}c0);
129
130        if (k > 2 * sizeof(int8_t)) {
131          $for N in range(0, NR, 4):
132            const int8x8_t vb${ABC[N:N+4]}c1 = vld1_s8(w); w = (const void*) ((uintptr_t) w + 8 * sizeof(int8_t));
133
134          $for M in range(MR):
135            $for N in range(0, NR, 4):
136              const int16x8_t vprod${M}x${ABC[N:N+4]}c1 = vmull_s8(vb${ABC[N:N+4]}c1, vreinterpret_s8_s16(vdup_lane_s16(vreinterpret_s16_s8(va${M}), 1)));
137              vacc${M}x${ABC[N:N+4]} = vpadalq_s16(vacc${M}x${ABC[N:N+4]}, vprod${M}x${ABC[N:N+4]}c1);
138
139          if (k > 4 * sizeof(int8_t)) {
140            $for N in range(0, NR, 4):
141              const int8x8_t vb${ABC[N:N+4]}c2 = vld1_s8(w); w = (const void*) ((uintptr_t) w + 8 * sizeof(int8_t));
142
143            $for M in range(MR):
144              $for N in range(0, NR, 4):
145                const int16x8_t vprod${M}x${ABC[N:N+4]}c2 = vmull_s8(vb${ABC[N:N+4]}c2, vreinterpret_s8_s16(vdup_lane_s16(vreinterpret_s16_s8(va${M}), 2)));
146                vacc${M}x${ABC[N:N+4]} = vpadalq_s16(vacc${M}x${ABC[N:N+4]}, vprod${M}x${ABC[N:N+4]}c2);
147          }
148        }
149      }
150      p -= ${MR} * sizeof(void*);
151    } while (p != 0);
152
153    const int32x4_t vmultiplier = vld1q_dup_s32(&params->neon.multiplier);
154    $for M in range(MR):
155      $for N in range(0, NR, 4):
156        vacc${M}x${ABC[N:N+4]} = vqrdmulhq_s32(vacc${M}x${ABC[N:N+4]}, vmultiplier);
157
158    const int32x4_t vright_shift = vld1q_dup_s32(&params->neon.right_shift);
159    const int32x4_t vzero_shift_mask = vreinterpretq_s32_u32(vceqq_s32(vright_shift, vmovq_n_s32(0)));
160    $for M in range(MR):
161      $for N in range(0, NR, 4):
162        vacc${M}x${ABC[N:N+4]} = vsraq_n_s32(vacc${M}x${ABC[N:N+4]}, vbicq_s32(vacc${M}x${ABC[N:N+4]}, vzero_shift_mask), 31);
163
164    $for M in range(MR):
165      $for N in range(0, NR, 4):
166        vacc${M}x${ABC[N:N+4]} = vrshlq_s32(vacc${M}x${ABC[N:N+4]}, vright_shift);
167
168    const int16x8_t voutput_zero_point = vld1q_dup_s16(&params->neon.output_zero_point);
169#if XNN_ARCH_ARM64
170    $for M in range(MR):
171      $for N in range(0, NR, 8):
172        const int16x8_t vacc${M}x${ABC[N:N+8]} = vqaddq_s16(vqmovn_high_s32(vqmovn_s32(vacc${M}x${ABC[N:N+4]}), vacc${M}x${ABC[N+4:N+8]}), voutput_zero_point);
173
174    $for M in range(MR):
175      $for N in range(0, NR, 16):
176        $if N + 8 < NR:
177          int8x16_t vout${M}x${ABC[N:N+16]} = vqmovn_high_s16(vqmovn_s16(vacc${M}x${ABC[N:N+8]}), vacc${M}x${ABC[N+8:N+16]});
178        $elif M % 2 == 1:
179          int8x16_t vout${M-1}x${ABC[N:N+8]}_${M}x${ABC[N:N+8]} = vqmovn_high_s16(vqmovn_s16(vacc${M-1}x${ABC[N:N+8]}), vacc${M}x${ABC[N:N+8]});
180        $elif M + 1 == MR:
181          int8x8_t vout${M}x${ABC[N:N+8]} = vqmovn_s16(vacc${M}x${ABC[N:N+8]});
182#else
183    $for M in range(MR):
184      $for N in range(0, NR, 8):
185        const int16x8_t vacc${M}x${ABC[N:N+8]} = vqaddq_s16(vcombine_s16(vqmovn_s32(vacc${M}x${ABC[N:N+4]}), vqmovn_s32(vacc${M}x${ABC[N+4:N+8]})), voutput_zero_point);
186
187    $for M in range(MR):
188      $for N in range(0, NR, 16):
189        $if N + 8 < NR:
190          int8x16_t vout${M}x${ABC[N:N+16]} = vcombine_s8(vqmovn_s16(vacc${M}x${ABC[N:N+8]}), vqmovn_s16(vacc${M}x${ABC[N+8:N+16]}));
191        $elif M % 2 == 1:
192          int8x16_t vout${M-1}x${ABC[N:N+8]}_${M}x${ABC[N:N+8]} = vcombine_s8(vqmovn_s16(vacc${M-1}x${ABC[N:N+8]}), vqmovn_s16(vacc${M}x${ABC[N:N+8]}));
193        $elif M + 1 == MR:
194          int8x8_t vout${M}x${ABC[N:N+8]} = vqmovn_s16(vacc${M}x${ABC[N:N+8]});
195#endif
196    $if NR == 8 and MR == 1:
197      const int8x8_t voutput_min = vld1_dup_s8(&params->neon.output_min);
198      const int8x8_t voutput_max = vld1_dup_s8(&params->neon.output_max);
199    $else:
200      const int8x16_t voutput_min = vld1q_dup_s8(&params->neon.output_min);
201      const int8x16_t voutput_max = vld1q_dup_s8(&params->neon.output_max);
202
203    $for M in reversed(range(MR)):
204      $for N in range(0, NR, 16):
205        $if N + 8 < NR:
206          vout${M}x${ABC[N:N+16]} = vmaxq_s8(vout${M}x${ABC[N:N+16]}, voutput_min);
207        $elif M % 2 == 1:
208          vout${M-1}x${ABC[N:N+8]}_${M}x${ABC[N:N+8]} = vmaxq_s8(vout${M-1}x${ABC[N:N+8]}_${M}x${ABC[N:N+8]}, voutput_min);
209        $elif M + 1 == MR:
210          $if NR == 8 and MR == 1:
211            vout${M}x${ABC[N:N+8]} = vmax_s8(vout${M}x${ABC[N:N+8]}, voutput_min);
212          $else:
213            vout${M}x${ABC[N:N+8]} = vmax_s8(vout${M}x${ABC[N:N+8]}, vget_low_s8(voutput_min));
214
215    $for M in reversed(range(MR)):
216      $for N in range(0, NR, 16):
217        $if N + 8 < NR:
218          vout${M}x${ABC[N:N+16]} = vminq_s8(vout${M}x${ABC[N:N+16]}, voutput_max);
219        $elif M % 2 == 1:
220          vout${M-1}x${ABC[N:N+8]}_${M}x${ABC[N:N+8]} = vminq_s8(vout${M-1}x${ABC[N:N+8]}_${M}x${ABC[N:N+8]}, voutput_max);
221        $elif M + 1 == MR:
222          $if NR == 8 and MR == 1:
223            vout${M}x${ABC[N:N+8]} = vmin_s8(vout${M}x${ABC[N:N+8]}, voutput_max);
224          $else:
225            vout${M}x${ABC[N:N+8]} = vmin_s8(vout${M}x${ABC[N:N+8]}, vget_low_s8(voutput_max));
226
227    if (nc >= ${NR}) {
228      $for M in reversed(range(MR)):
229        $for N in range(0, NR, 16):
230          $if N + 8 < NR:
231            vst1q_s8(c${M} + ${N}, vout${M}x${ABC[N:N+16]});
232          $elif M % 2 == 1:
233            vst1_s8(c${M} + ${N}, vget_high_s8(vout${M-1}x${ABC[N:N+8]}_${M}x${ABC[N:N+8]}));
234            vst1_s8(c${M-1} + ${N}, vget_low_s8(vout${M-1}x${ABC[N:N+8]}_${M}x${ABC[N:N+8]}));
235          $elif M + 1 == MR:
236            vst1_s8(c${M} + ${N}, vout${M}x${ABC[N:N+8]});
237
238      $for M in reversed(range(MR)):
239        c${M} = (int8_t*) ((uintptr_t) c${M} + cn_stride);
240
241      a = (const int8_t**restrict) ((uintptr_t) a - ks);
242
243      nc -= ${NR};
244    } else {
245      $if NR == 16:
246        $for M in range(MR):
247          $if M % 2 == 1:
248            int8x16_t vout${M-1}x01234567_${M}x01234567 = vcombine_s8(vget_low_s8(vout${M-1}x0123456789ABCDEF), vget_low_s8(vout${M}x0123456789ABCDEF));
249          $elif M + 1 == MR:
250            int8x8_t vout${M}x01234567 = vget_low_s8(vout${M}x0123456789ABCDEF);
251        if (nc & 8) {
252          $for M in reversed(range(MR)):
253            $if M % 2 == 1:
254              vst1_s8(c${M}, vget_high_s8(vout${M-1}x${ABC[N:N+8]}_${M}x${ABC[N:N+8]})); c${M} += 8;
255              vst1_s8(c${M-1}, vget_low_s8(vout${M-1}x${ABC[N:N+8]}_${M}x${ABC[N:N+8]})); c${M-1} += 8;
256            $elif M + 1 == MR:
257              vst1_s8(c${M}, vout${M}x${ABC[N:N+8]}); c${M} += 8;
258          $for M in reversed(range(MR)):
259            $if M % 2 == 1:
260              vout${M-1}x01234567_${M}x01234567 = vcombine_s8(vget_high_s8(vout${M-1}x0123456789ABCDEF), vget_high_s8(vout${M}x0123456789ABCDEF));
261            $elif M + 1 == MR:
262              vout${M}x01234567 = vget_high_s8(vout${M}x0123456789ABCDEF);
263        }
264      if (nc & 4) {
265        $for M in reversed(range(MR)):
266          $if M % 2 == 1:
267            vst1q_lane_u32(__builtin_assume_aligned(c${M}, 1), vreinterpretq_u32_s8(vout${M-1}x01234567_${M}x01234567), 2); c${M} += 4;
268            vst1q_lane_u32(__builtin_assume_aligned(c${M-1}, 1), vreinterpretq_u32_s8(vout${M-1}x01234567_${M}x01234567), 0); c${M-1} += 4;
269          $elif M + 1 == MR:
270            vst1_lane_u32(__builtin_assume_aligned(c${M}, 1), vreinterpret_u32_s8(vout${M}x01234567), 0); c${M} += 4;
271        $for M in reversed(range(MR)):
272          $if M % 2 == 1:
273            vout${M-1}x01234567_${M}x01234567 = vextq_s8(vout${M-1}x01234567_${M}x01234567, vout${M-1}x01234567_${M}x01234567, 4);
274          $elif M + 1 == MR:
275            vout${M}x01234567 = vext_s8(vout${M}x01234567, vout${M}x01234567, 4);
276      }
277      if (nc & 2) {
278        $for M in reversed(range(MR)):
279          $if M % 2 == 1:
280            vst1q_lane_u16(__builtin_assume_aligned(c${M}, 1), vreinterpretq_u16_s8(vout${M-1}x01234567_${M}x01234567), 4); c${M} += 2;
281            vst1q_lane_u16(__builtin_assume_aligned(c${M-1}, 1), vreinterpretq_u16_s8(vout${M-1}x01234567_${M}x01234567), 0); c${M-1} += 2;
282          $elif M + 1 == MR:
283            vst1_lane_u16(__builtin_assume_aligned(c${M}, 1), vreinterpret_u16_s8(vout${M}x01234567), 0); c${M} += 2;
284        $for M in reversed(range(MR)):
285          $if M % 2 == 1:
286            vout${M-1}x01234567_${M}x01234567 = vextq_s8(vout${M-1}x01234567_${M}x01234567, vout${M-1}x01234567_${M}x01234567, 2);
287          $elif M + 1 == MR:
288            vout${M}x01234567 = vext_s8(vout${M}x01234567, vout${M}x01234567, 2);
289      }
290      if (nc & 1) {
291        $for M in reversed(range(MR)):
292          $if M % 2 == 1:
293            vst1q_lane_s8(c${M}, vout${M-1}x01234567_${M}x01234567, 8);
294            vst1q_lane_s8(c${M-1}, vout${M-1}x01234567_${M}x01234567, 0);
295          $elif M + 1 == MR:
296            vst1_lane_s8(c${M}, vout${M}x01234567, 0);
297      }
298
299      nc = 0;
300    }
301  } while (nc != 0);
302}
303