• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1// Copyright 2020 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/igemm.h>
14#include <xnnpack/math.h>
15
16
17void xnn_qs8_igemm_minmax_ukernel_${MR}x${NR}c4__neondot(
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, 4);
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      // Inner accumulation loop along the ${NR} columns.
76      size_t k = kc;
77      // 2x partial unrolled loop to load 8 bytes at a time.
78      while (k >= 8 * sizeof(int8_t)) {
79        // Load a ${MR}x8 block of activations.
80        $for M in range(MR):
81          const int8x8_t va${M}x01234567 = vld1_s8(a${M}); a${M} += 8;
82
83        // Load a 8x${NR} block of weights.
84        $for K in range(0, 8, 4):
85          $for N in range(0, NR, 4):
86            const int8x16_t vb${ABC[K:K+4]}x${ABC[N:N+4]} = vld1q_s8(w); w = (const void*) ((const int8_t*) w + 16);
87
88        // Multiply-accumulate: ${MR}x8 * 8x${NR} --> ${MR}x${NR}.
89        $for K in range(0, 8, 4):
90          $for M in range(MR):
91            $for N in range(0, NR, 4):
92              vacc${M}x${ABC[N:N+4]} = vdotq_lane_s32(vacc${M}x${ABC[N:N+4]}, vb${ABC[K:K+4]}x${ABC[N:N+4]}, va${M}x01234567, ${K/4});
93
94        k -= 8 * sizeof(int8_t);
95      }
96      // Handle up to 4 final positions of `k`
97      if XNN_UNLIKELY(k != 0) {
98        // Load a ${MR}x4 block of activations.
99        $for M in range(MR):
100          const int8x8_t va${M}x01234567 = vld1_s8(a${M});
101
102        // Load a 4x${NR} block of weights.
103        $for N in range(0, NR, 4):
104          const int8x16_t vb0123x${ABC[N:N+4]} = vld1q_s8(w); w = (const void*) ((const int8_t*) w + 16);
105
106        // Multiply-accumulate: ${MR}x4 * 4x${NR} --> ${MR}x${NR}.
107        $for M in range(MR):
108          $for N in range(0, NR, 4):
109              vacc${M}x${ABC[N:N+4]} = vdotq_lane_s32(vacc${M}x${ABC[N:N+4]}, vb0123x${ABC[N:N+4]}, va${M}x01234567, 0);
110      }
111      p -= ${MR} * sizeof(void*);
112    } while (p != 0);
113
114    const int32x4_t vmultiplier = vld1q_dup_s32(&params->neon.multiplier);
115    $for M in range(MR):
116      $for N in range(0, NR, 4):
117        vacc${M}x${ABC[N:N+4]} = vqrdmulhq_s32(vacc${M}x${ABC[N:N+4]}, vmultiplier);
118
119    const int32x4_t vright_shift = vld1q_dup_s32(&params->neon.right_shift);
120    const int32x4_t vzero_shift_mask = vreinterpretq_s32_u32(vceqq_s32(vright_shift, vmovq_n_s32(0)));
121    $for M in range(MR):
122      $for N in range(0, NR, 4):
123        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);
124
125    $for M in range(MR):
126      $for N in range(0, NR, 4):
127        vacc${M}x${ABC[N:N+4]} = vrshlq_s32(vacc${M}x${ABC[N:N+4]}, vright_shift);
128
129    const int16x8_t voutput_zero_point = vld1q_dup_s16(&params->neon.output_zero_point);
130#if XNN_ARCH_ARM64
131    $for M in range(MR):
132      $for N in range(0, NR, 8):
133        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);
134
135    $for M in range(MR):
136      $for N in range(0, NR, 16):
137        $if N + 8 < NR:
138          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]});
139        $elif M % 2 == 1:
140          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]});
141        $elif M + 1 == MR:
142          int8x8_t vout${M}x${ABC[N:N+8]} = vqmovn_s16(vacc${M}x${ABC[N:N+8]});
143#else
144    $for M in range(MR):
145      $for N in range(0, NR, 8):
146        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);
147
148    $for M in range(MR):
149      $for N in range(0, NR, 16):
150        $if N + 8 < NR:
151          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]}));
152        $elif M % 2 == 1:
153          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]}));
154        $elif M + 1 == MR:
155          int8x8_t vout${M}x${ABC[N:N+8]} = vqmovn_s16(vacc${M}x${ABC[N:N+8]});
156#endif
157    $if NR == 8 and MR == 1:
158      const int8x8_t voutput_min = vld1_dup_s8(&params->neon.output_min);
159      const int8x8_t voutput_max = vld1_dup_s8(&params->neon.output_max);
160    $else:
161      const int8x16_t voutput_min = vld1q_dup_s8(&params->neon.output_min);
162      const int8x16_t voutput_max = vld1q_dup_s8(&params->neon.output_max);
163
164    $for M in reversed(range(MR)):
165      $for N in range(0, NR, 16):
166        $if N + 8 < NR:
167          vout${M}x${ABC[N:N+16]} = vmaxq_s8(vout${M}x${ABC[N:N+16]}, voutput_min);
168        $elif M % 2 == 1:
169          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);
170        $elif M + 1 == MR:
171          $if NR == 8 and MR == 1:
172            vout${M}x${ABC[N:N+8]} = vmax_s8(vout${M}x${ABC[N:N+8]}, voutput_min);
173          $else:
174            vout${M}x${ABC[N:N+8]} = vmax_s8(vout${M}x${ABC[N:N+8]}, vget_low_s8(voutput_min));
175
176    $for M in reversed(range(MR)):
177      $for N in range(0, NR, 16):
178        $if N + 8 < NR:
179          vout${M}x${ABC[N:N+16]} = vminq_s8(vout${M}x${ABC[N:N+16]}, voutput_max);
180        $elif M % 2 == 1:
181          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);
182        $elif M + 1 == MR:
183          $if NR == 8 and MR == 1:
184            vout${M}x${ABC[N:N+8]} = vmin_s8(vout${M}x${ABC[N:N+8]}, voutput_max);
185          $else:
186            vout${M}x${ABC[N:N+8]} = vmin_s8(vout${M}x${ABC[N:N+8]}, vget_low_s8(voutput_max));
187
188    if (nc >= ${NR}) {
189      $for M in reversed(range(MR)):
190        $for N in range(0, NR, 16):
191          $if N + 8 < NR:
192            vst1q_s8(c${M} + ${N}, vout${M}x${ABC[N:N+16]});
193          $elif M % 2 == 1:
194            vst1_s8(c${M} + ${N}, vget_high_s8(vout${M-1}x${ABC[N:N+8]}_${M}x${ABC[N:N+8]}));
195            vst1_s8(c${M-1} + ${N}, vget_low_s8(vout${M-1}x${ABC[N:N+8]}_${M}x${ABC[N:N+8]}));
196          $elif M + 1 == MR:
197            vst1_s8(c${M} + ${N}, vout${M}x${ABC[N:N+8]});
198
199      $for M in reversed(range(MR)):
200        c${M} = (int8_t*) ((uintptr_t) c${M} + cn_stride);
201
202      a = (const int8_t**restrict) ((uintptr_t) a - ks);
203
204      nc -= ${NR};
205    } else {
206      $if NR == 16:
207        $for M in range(MR):
208          $if M % 2 == 1:
209            int8x16_t vout${M-1}x01234567_${M}x01234567 = vcombine_s8(vget_low_s8(vout${M-1}x0123456789ABCDEF), vget_low_s8(vout${M}x0123456789ABCDEF));
210          $elif M + 1 == MR:
211            int8x8_t vout${M}x01234567 = vget_low_s8(vout${M}x0123456789ABCDEF);
212        if (nc & 8) {
213          $for M in reversed(range(MR)):
214            $if M % 2 == 1:
215              vst1_s8(c${M}, vget_high_s8(vout${M-1}x${ABC[N:N+8]}_${M}x${ABC[N:N+8]})); c${M} += 8;
216              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;
217            $elif M + 1 == MR:
218              vst1_s8(c${M}, vout${M}x${ABC[N:N+8]}); c${M} += 8;
219          $for M in reversed(range(MR)):
220            $if M % 2 == 1:
221              vout${M-1}x01234567_${M}x01234567 = vcombine_s8(vget_high_s8(vout${M-1}x0123456789ABCDEF), vget_high_s8(vout${M}x0123456789ABCDEF));
222            $elif M + 1 == MR:
223              vout${M}x01234567 = vget_high_s8(vout${M}x0123456789ABCDEF);
224        }
225      if (nc & 4) {
226        $for M in reversed(range(MR)):
227          $if M % 2 == 1:
228            vst1q_lane_u32(__builtin_assume_aligned(c${M}, 1), vreinterpretq_u32_s8(vout${M-1}x01234567_${M}x01234567), 2); c${M} += 4;
229            vst1q_lane_u32(__builtin_assume_aligned(c${M-1}, 1), vreinterpretq_u32_s8(vout${M-1}x01234567_${M}x01234567), 0); c${M-1} += 4;
230          $elif M + 1 == MR:
231            vst1_lane_u32(__builtin_assume_aligned(c${M}, 1), vreinterpret_u32_s8(vout${M}x01234567), 0); c${M} += 4;
232        $for M in reversed(range(MR)):
233          $if M % 2 == 1:
234            vout${M-1}x01234567_${M}x01234567 = vextq_s8(vout${M-1}x01234567_${M}x01234567, vout${M-1}x01234567_${M}x01234567, 4);
235          $elif M + 1 == MR:
236            vout${M}x01234567 = vext_s8(vout${M}x01234567, vout${M}x01234567, 4);
237      }
238      if (nc & 2) {
239        $for M in reversed(range(MR)):
240          $if M % 2 == 1:
241            vst1q_lane_u16(__builtin_assume_aligned(c${M}, 1), vreinterpretq_u16_s8(vout${M-1}x01234567_${M}x01234567), 4); c${M} += 2;
242            vst1q_lane_u16(__builtin_assume_aligned(c${M-1}, 1), vreinterpretq_u16_s8(vout${M-1}x01234567_${M}x01234567), 0); c${M-1} += 2;
243          $elif M + 1 == MR:
244            vst1_lane_u16(__builtin_assume_aligned(c${M}, 1), vreinterpret_u16_s8(vout${M}x01234567), 0); c${M} += 2;
245        $for M in reversed(range(MR)):
246          $if M % 2 == 1:
247            vout${M-1}x01234567_${M}x01234567 = vextq_s8(vout${M-1}x01234567_${M}x01234567, vout${M-1}x01234567_${M}x01234567, 2);
248          $elif M + 1 == MR:
249            vout${M}x01234567 = vext_s8(vout${M}x01234567, vout${M}x01234567, 2);
250      }
251      if (nc & 1) {
252        $for M in reversed(range(MR)):
253          $if M % 2 == 1:
254            vst1q_lane_s8(c${M}, vout${M-1}x01234567_${M}x01234567, 8);
255            vst1q_lane_s8(c${M-1}, vout${M-1}x01234567_${M}x01234567, 0);
256          $elif M + 1 == MR:
257            vst1_lane_s8(c${M}, vout${M}x01234567, 0);
258      }
259
260      nc = 0;
261    }
262  } while (nc != 0);
263}
264