Debian Codegraph @

src/qd8-f32-qc8w-igemm/gen/qd8-f32-qc8w-igemm-2x16c8-minmax-neoni8mm.c

raw
1 // Auto-generated file. Do not edit!
2 // Template: src/qs8-igemm/c8-neoni8mm.c.in
3 // Generator: tools/xngen
4 //
5 // Copyright 2023 Google LLC
6 //
7 // This source code is licensed under the BSD-style license found in the
8 // LICENSE file in the root directory of this source tree.
9
10 #include <assert.h>
11
12 #include <arm_neon.h>
13
14 #include "xnnpack/igemm.h"
15 #include "xnnpack/math.h"
16
17
18 void xnn_qd8_f32_qc8w_igemm_minmax_ukernel_2x16c8__neoni8mm(
19 size_t mr,
20 size_t nc,
21 size_t kc,
22 size_t ks,
23 const int8_t** restrict a,
24 const void* restrict w,
25 float* restrict c,
26 size_t cm_stride,
27 size_t cn_stride,
28 size_t a_offset,
29 const int8_t* zero,
30 const int8_t* zero_data,
31 const union xnn_f32_minmax_params params[restrict XNN_MIN_ELEMENTS(1)],
32 const struct xnn_qd8_quantization_params quantization_params[restrict XNN_MIN_ELEMENTS(1)]) XNN_OOB_READS
33 {
34 assert(mr != 0);
35 assert(mr <= 2);
36 assert(nc != 0);
37 assert(kc != 0);
38 assert(ks != 0);
39 assert(ks % (2 * sizeof(void*)) == 0);
40 assert(a_offset % sizeof(int8_t) == 0);
41 assert(a != NULL);
42 assert(w != NULL);
43 assert(c != NULL);
44
45 kc = round_up_po2(kc, 8 * sizeof(int8_t));
46 float* c0 = c;
47 float* c1 = (float*) ((uintptr_t) c0 + cm_stride);
48 if XNN_UNPREDICTABLE(mr != 2) {
49 c1 = c0;
50 }
51
52 do {
53 const int32x4_t vinput_zero_point = vld1q_dup_s32(&quantization_params->zero_point);
54 const int32x4_t vksum0123 = vld1q_s32(w); w = (const int32_t*) w + 4;
55 const int32x4_t vksumzp0x0123 = vmulq_s32(vksum0123, vinput_zero_point);
56 const int32x4_t vksum4567 = vld1q_s32(w); w = (const int32_t*) w + 4;
57 const int32x4_t vksumzp0x4567 = vmulq_s32(vksum4567, vinput_zero_point);
58 const int32x4_t vksum89AB = vld1q_s32(w); w = (const int32_t*) w + 4;
59 const int32x4_t vksumzp0x89AB = vmulq_s32(vksum89AB, vinput_zero_point);
60 const int32x4_t vksumCDEF = vld1q_s32(w); w = (const int32_t*) w + 4;
61 const int32x4_t vksumzp0xCDEF = vmulq_s32(vksumCDEF, vinput_zero_point);
62 const int32x4_t vksumzp1x0123 = vksumzp0x0123;
63 const int32x4_t vksumzp1x4567 = vksumzp0x4567;
64 const int32x4_t vksumzp1x89AB = vksumzp0x89AB;
65 const int32x4_t vksumzp1xCDEF = vksumzp0xCDEF;
66
67 int32x4_t vacc01x01 = vreinterpretq_s32_u64(vtrn1q_u64(vreinterpretq_u64_s32(vksumzp0x0123), vreinterpretq_u64_s32(vksumzp1x0123)));
68 int32x4_t vacc01x23 = vreinterpretq_s32_u64(vtrn2q_u64(vreinterpretq_u64_s32(vksumzp0x0123), vreinterpretq_u64_s32(vksumzp1x0123)));
69 int32x4_t vacc01x45 = vreinterpretq_s32_u64(vtrn1q_u64(vreinterpretq_u64_s32(vksumzp0x4567), vreinterpretq_u64_s32(vksumzp1x4567)));
70 int32x4_t vacc01x67 = vreinterpretq_s32_u64(vtrn2q_u64(vreinterpretq_u64_s32(vksumzp0x4567), vreinterpretq_u64_s32(vksumzp1x4567)));
71 int32x4_t vacc01x89 = vreinterpretq_s32_u64(vtrn1q_u64(vreinterpretq_u64_s32(vksumzp0x89AB), vreinterpretq_u64_s32(vksumzp1x89AB)));
72 int32x4_t vacc01xAB = vreinterpretq_s32_u64(vtrn2q_u64(vreinterpretq_u64_s32(vksumzp0x89AB), vreinterpretq_u64_s32(vksumzp1x89AB)));
73 int32x4_t vacc01xCD = vreinterpretq_s32_u64(vtrn1q_u64(vreinterpretq_u64_s32(vksumzp0xCDEF), vreinterpretq_u64_s32(vksumzp1xCDEF)));
74 int32x4_t vacc01xEF = vreinterpretq_s32_u64(vtrn2q_u64(vreinterpretq_u64_s32(vksumzp0xCDEF), vreinterpretq_u64_s32(vksumzp1xCDEF)));
75
76 size_t p = ks;
77 do {
78 const int8_t* restrict a0 = a[0];
79 if XNN_UNPREDICTABLE(a0 != zero) {
80 a0 = (const int8_t*) ((uintptr_t) a0 + a_offset);
81 } else {
82 a0 = zero_data;
83 }
84 const int8_t* restrict a1 = a[1];
85 if XNN_UNPREDICTABLE(a1 != zero) {
86 a1 = (const int8_t*) ((uintptr_t) a1 + a_offset);
87 } else {
88 a1 = zero_data;
89 }
90 a += 2;
91
92 uint64x2x2_t va01x0123456789ABCDEF;
93 va01x0123456789ABCDEF.val[0] = vdupq_n_u64(0);
94 va01x0123456789ABCDEF.val[1] = vdupq_n_u64(0);
95
96 // Inner accumulation loop along the 16 columns.
97 size_t k = kc;
98 // 2x partial unrolled loop to load 8 bytes at a time.
99 while (k >= 16 * sizeof(int8_t)) {
100 // Load a 2x16 block of activations.
101 va01x0123456789ABCDEF = vld2q_lane_u64((const void*) a0, va01x0123456789ABCDEF, 0); a0 += 16;
102 va01x0123456789ABCDEF = vld2q_lane_u64((const void*) a1, va01x0123456789ABCDEF, 1); a1 += 16;
103
104 // Load a 16x16 block of weights.
105 const int8x16_t vb01x01234567 = vld1q_s8(w); w = (const int8_t*) w + 16;
106 const int8x16_t vb23x01234567 = vld1q_s8(w); w = (const int8_t*) w + 16;
107 const int8x16_t vb45x01234567 = vld1q_s8(w); w = (const int8_t*) w + 16;
108 const int8x16_t vb67x01234567 = vld1q_s8(w); w = (const int8_t*) w + 16;
109 const int8x16_t vb89x01234567 = vld1q_s8(w); w = (const int8_t*) w + 16;
110 const int8x16_t vbABx01234567 = vld1q_s8(w); w = (const int8_t*) w + 16;
111 const int8x16_t vbCDx01234567 = vld1q_s8(w); w = (const int8_t*) w + 16;
112 const int8x16_t vbEFx01234567 = vld1q_s8(w); w = (const int8_t*) w + 16;
113 const int8x16_t vb01x89ABCDEF = vld1q_s8(w); w = (const int8_t*) w + 16;
114 const int8x16_t vb23x89ABCDEF = vld1q_s8(w); w = (const int8_t*) w + 16;
115 const int8x16_t vb45x89ABCDEF = vld1q_s8(w); w = (const int8_t*) w + 16;
116 const int8x16_t vb67x89ABCDEF = vld1q_s8(w); w = (const int8_t*) w + 16;
117 const int8x16_t vb89x89ABCDEF = vld1q_s8(w); w = (const int8_t*) w + 16;
118 const int8x16_t vbABx89ABCDEF = vld1q_s8(w); w = (const int8_t*) w + 16;
119 const int8x16_t vbCDx89ABCDEF = vld1q_s8(w); w = (const int8_t*) w + 16;
120 const int8x16_t vbEFx89ABCDEF = vld1q_s8(w); w = (const int8_t*) w + 16;
121
122 // Multiply-accumulate: 2x16 * 16x16 --> 2x16.
123 vacc01x01 = vmmlaq_s32(vacc01x01, vreinterpretq_s8_u64(va01x0123456789ABCDEF.val[0]), vb01x01234567);
124 vacc01x23 = vmmlaq_s32(vacc01x23, vreinterpretq_s8_u64(va01x0123456789ABCDEF.val[0]), vb23x01234567);
125 vacc01x45 = vmmlaq_s32(vacc01x45, vreinterpretq_s8_u64(va01x0123456789ABCDEF.val[0]), vb45x01234567);
126 vacc01x67 = vmmlaq_s32(vacc01x67, vreinterpretq_s8_u64(va01x0123456789ABCDEF.val[0]), vb67x01234567);
127 vacc01x89 = vmmlaq_s32(vacc01x89, vreinterpretq_s8_u64(va01x0123456789ABCDEF.val[0]), vb89x01234567);
128 vacc01xAB = vmmlaq_s32(vacc01xAB, vreinterpretq_s8_u64(va01x0123456789ABCDEF.val[0]), vbABx01234567);
129 vacc01xCD = vmmlaq_s32(vacc01xCD, vreinterpretq_s8_u64(va01x0123456789ABCDEF.val[0]), vbCDx01234567);
130 vacc01xEF = vmmlaq_s32(vacc01xEF, vreinterpretq_s8_u64(va01x0123456789ABCDEF.val[0]), vbEFx01234567);
131 vacc01x01 = vmmlaq_s32(vacc01x01, vreinterpretq_s8_u64(va01x0123456789ABCDEF.val[1]), vb01x89ABCDEF);
132 vacc01x23 = vmmlaq_s32(vacc01x23, vreinterpretq_s8_u64(va01x0123456789ABCDEF.val[1]), vb23x89ABCDEF);
133 vacc01x45 = vmmlaq_s32(vacc01x45, vreinterpretq_s8_u64(va01x0123456789ABCDEF.val[1]), vb45x89ABCDEF);
134 vacc01x67 = vmmlaq_s32(vacc01x67, vreinterpretq_s8_u64(va01x0123456789ABCDEF.val[1]), vb67x89ABCDEF);
135 vacc01x89 = vmmlaq_s32(vacc01x89, vreinterpretq_s8_u64(va01x0123456789ABCDEF.val[1]), vb89x89ABCDEF);
136 vacc01xAB = vmmlaq_s32(vacc01xAB, vreinterpretq_s8_u64(va01x0123456789ABCDEF.val[1]), vbABx89ABCDEF);
137 vacc01xCD = vmmlaq_s32(vacc01xCD, vreinterpretq_s8_u64(va01x0123456789ABCDEF.val[1]), vbCDx89ABCDEF);
138 vacc01xEF = vmmlaq_s32(vacc01xEF, vreinterpretq_s8_u64(va01x0123456789ABCDEF.val[1]), vbEFx89ABCDEF);
139
140 k -= 16 * sizeof(int8_t);
141 }
142 // Handle up to 8 final positions of `k`
143 if XNN_UNLIKELY(k != 0) {
144 // Load a 2x8 block of activations.
145 uint64x2_t va01x01234567 = vld1q_dup_u64((const void*) a0); a0 += 8;
146 va01x01234567 = vld1q_lane_u64((const void*) a1, va01x01234567, 1); a1 += 8;
147
148 // Load a 16x16 block of weights.
149 const int8x16_t vb01x01234567 = vld1q_s8(w); w = (const int8_t*) w + 16;
150 const int8x16_t vb23x01234567 = vld1q_s8(w); w = (const int8_t*) w + 16;
151 const int8x16_t vb45x01234567 = vld1q_s8(w); w = (const int8_t*) w + 16;
152 const int8x16_t vb67x01234567 = vld1q_s8(w); w = (const int8_t*) w + 16;
153 const int8x16_t vb89x01234567 = vld1q_s8(w); w = (const int8_t*) w + 16;
154 const int8x16_t vbABx01234567 = vld1q_s8(w); w = (const int8_t*) w + 16;
155 const int8x16_t vbCDx01234567 = vld1q_s8(w); w = (const int8_t*) w + 16;
156 const int8x16_t vbEFx01234567 = vld1q_s8(w); w = (const int8_t*) w + 16;
157
158 // Multiply-accumulate: 2x4 * 4x16 --> 2x16.
159 vacc01x01 = vmmlaq_s32(vacc01x01, vreinterpretq_s8_u64(va01x01234567), vb01x01234567);
160 vacc01x23 = vmmlaq_s32(vacc01x23, vreinterpretq_s8_u64(va01x01234567), vb23x01234567);
161 vacc01x45 = vmmlaq_s32(vacc01x45, vreinterpretq_s8_u64(va01x01234567), vb45x01234567);
162 vacc01x67 = vmmlaq_s32(vacc01x67, vreinterpretq_s8_u64(va01x01234567), vb67x01234567);
163 vacc01x89 = vmmlaq_s32(vacc01x89, vreinterpretq_s8_u64(va01x01234567), vb89x01234567);
164 vacc01xAB = vmmlaq_s32(vacc01xAB, vreinterpretq_s8_u64(va01x01234567), vbABx01234567);
165 vacc01xCD = vmmlaq_s32(vacc01xCD, vreinterpretq_s8_u64(va01x01234567), vbCDx01234567);
166 vacc01xEF = vmmlaq_s32(vacc01xEF, vreinterpretq_s8_u64(va01x01234567), vbEFx01234567);
167 }
168
169 p -= 2 * sizeof(void*);
170 } while (p != 0);
171
172 int32x4_t vacc0x0123 = vcombine_s32(vget_low_s32(vacc01x01), vget_low_s32(vacc01x23));
173 int32x4_t vacc1x0123 = vcombine_s32(vget_high_s32(vacc01x01), vget_high_s32(vacc01x23));
174 int32x4_t vacc0x4567 = vcombine_s32(vget_low_s32(vacc01x45), vget_low_s32(vacc01x67));
175 int32x4_t vacc1x4567 = vcombine_s32(vget_high_s32(vacc01x45), vget_high_s32(vacc01x67));
176 int32x4_t vacc0x89AB = vcombine_s32(vget_low_s32(vacc01x89), vget_low_s32(vacc01xAB));
177 int32x4_t vacc1x89AB = vcombine_s32(vget_high_s32(vacc01x89), vget_high_s32(vacc01xAB));
178 int32x4_t vacc0xCDEF = vcombine_s32(vget_low_s32(vacc01xCD), vget_low_s32(vacc01xEF));
179 int32x4_t vacc1xCDEF = vcombine_s32(vget_high_s32(vacc01xCD), vget_high_s32(vacc01xEF));
180
181 const float32x4_t vinput_scale = vld1q_dup_f32(&quantization_params->inv_scale);
182 float32x4_t vout0x0123 = vcvtq_f32_s32(vacc0x0123);
183 float32x4_t vout0x4567 = vcvtq_f32_s32(vacc0x4567);
184 float32x4_t vout0x89AB = vcvtq_f32_s32(vacc0x89AB);
185 float32x4_t vout0xCDEF = vcvtq_f32_s32(vacc0xCDEF);
186 float32x4_t vout1x0123 = vcvtq_f32_s32(vacc1x0123);
187 float32x4_t vout1x4567 = vcvtq_f32_s32(vacc1x4567);
188 float32x4_t vout1x89AB = vcvtq_f32_s32(vacc1x89AB);
189 float32x4_t vout1xCDEF = vcvtq_f32_s32(vacc1xCDEF);
190 vout0x0123 = vmulq_f32(vout0x0123, vinput_scale);
191 vout1x0123 = vmulq_f32(vout1x0123, vinput_scale);
192 vout0x4567 = vmulq_f32(vout0x4567, vinput_scale);
193 vout1x4567 = vmulq_f32(vout1x4567, vinput_scale);
194 vout0x89AB = vmulq_f32(vout0x89AB, vinput_scale);
195 vout1x89AB = vmulq_f32(vout1x89AB, vinput_scale);
196 vout0xCDEF = vmulq_f32(vout0xCDEF, vinput_scale);
197 vout1xCDEF = vmulq_f32(vout1xCDEF, vinput_scale);
198
199 const float32x4_t vfilter_output_scale0123 = vld1q_f32(w); w = (const float*) w + 4;
200 const float32x4_t vfilter_output_scale4567 = vld1q_f32(w); w = (const float*) w + 4;
201 const float32x4_t vfilter_output_scale89AB = vld1q_f32(w); w = (const float*) w + 4;
202 const float32x4_t vfilter_output_scaleCDEF = vld1q_f32(w); w = (const float*) w + 4;
203
204 const float32x4_t vbias0123 = vld1q_f32(w); w = (const float*) w + 4;
205 vout0x0123 = vfmaq_f32(vbias0123, vout0x0123, vfilter_output_scale0123);
206 vout1x0123 = vfmaq_f32(vbias0123, vout1x0123, vfilter_output_scale0123);
207 const float32x4_t vbias4567 = vld1q_f32(w); w = (const float*) w + 4;
208 vout0x4567 = vfmaq_f32(vbias4567, vout0x4567, vfilter_output_scale4567);
209 vout1x4567 = vfmaq_f32(vbias4567, vout1x4567, vfilter_output_scale4567);
210 const float32x4_t vbias89AB = vld1q_f32(w); w = (const float*) w + 4;
211 vout0x89AB = vfmaq_f32(vbias89AB, vout0x89AB, vfilter_output_scale89AB);
212 vout1x89AB = vfmaq_f32(vbias89AB, vout1x89AB, vfilter_output_scale89AB);
213 const float32x4_t vbiasCDEF = vld1q_f32(w); w = (const float*) w + 4;
214 vout0xCDEF = vfmaq_f32(vbiasCDEF, vout0xCDEF, vfilter_output_scaleCDEF);
215 vout1xCDEF = vfmaq_f32(vbiasCDEF, vout1xCDEF, vfilter_output_scaleCDEF);
216
217 const float32x4_t voutput_min = vld1q_dup_f32(&params->scalar.min);
218 vout0x0123 = vmaxq_f32(vout0x0123, voutput_min);
219 vout0x4567 = vmaxq_f32(vout0x4567, voutput_min);
220 vout0x89AB = vmaxq_f32(vout0x89AB, voutput_min);
221 vout0xCDEF = vmaxq_f32(vout0xCDEF, voutput_min);
222 vout1x0123 = vmaxq_f32(vout1x0123, voutput_min);
223 vout1x4567 = vmaxq_f32(vout1x4567, voutput_min);
224 vout1x89AB = vmaxq_f32(vout1x89AB, voutput_min);
225 vout1xCDEF = vmaxq_f32(vout1xCDEF, voutput_min);
226
227 const float32x4_t voutput_max = vld1q_dup_f32(&params->scalar.max);
228 vout0x0123 = vminq_f32(vout0x0123, voutput_max);
229 vout0x4567 = vminq_f32(vout0x4567, voutput_max);
230 vout0x89AB = vminq_f32(vout0x89AB, voutput_max);
231 vout0xCDEF = vminq_f32(vout0xCDEF, voutput_max);
232 vout1x0123 = vminq_f32(vout1x0123, voutput_max);
233 vout1x4567 = vminq_f32(vout1x4567, voutput_max);
234 vout1x89AB = vminq_f32(vout1x89AB, voutput_max);
235 vout1xCDEF = vminq_f32(vout1xCDEF, voutput_max);
236
237 if XNN_LIKELY(nc >= 16) {
238 vst1q_f32(c1, vout1x0123);
239 vst1q_f32(c1 + 4, vout1x4567);
240 vst1q_f32(c1 + 8, vout1x89AB);
241 vst1q_f32(c1 + 12, vout1xCDEF);
242 vst1q_f32(c0, vout0x0123);
243 vst1q_f32(c0 + 4, vout0x4567);
244 vst1q_f32(c0 + 8, vout0x89AB);
245 vst1q_f32(c0 + 12, vout0xCDEF);
246
247 c1 = (float*) ((uintptr_t) c1 + cn_stride);
248 c0 = (float*) ((uintptr_t) c0 + cn_stride);
249
250 a = (const int8_t**restrict) ((uintptr_t) a - ks);
251
252 nc -= 16;
253 } else {
254 if (nc & 8) {
255 vst1q_f32(c1, vout1x0123); c1 += 4;
256 vout1x0123 = vout1x89AB;
257 vst1q_f32(c0, vout0x0123); c0 += 4;
258 vout0x0123 = vout0x89AB;
259 vst1q_f32(c1, vout1x4567); c1 += 4;
260 vout1x4567 = vout1xCDEF;
261 vst1q_f32(c0, vout0x4567); c0 += 4;
262 vout0x4567 = vout0xCDEF;
263 }
264 if (nc & 4) {
265 vst1q_f32(c1, vout1x0123); c1 += 4;
266 vout1x0123 = vout1x4567;
267 vst1q_f32(c0, vout0x0123); c0 += 4;
268 vout0x0123 = vout0x4567;
269 }
270 float32x2_t vout1x01 = vget_low_f32(vout1x0123);
271 float32x2_t vout0x01 = vget_low_f32(vout0x0123);
272 if (nc & 2) {
273 vst1_f32(c1, vout1x01); c1 += 2;
274 vst1_f32(c0, vout0x01); c0 += 2;
275 vout1x01 = vget_high_f32(vout1x0123);
276 vout0x01 = vget_high_f32(vout0x0123);
277 }
278 if (nc & 1) {
279 vst1_lane_f32(c1, vout1x01, 0);
280 vst1_lane_f32(c0, vout0x01, 0);
281 }
282 nc = 0;
283 }
284 } while (nc != 0);
285 }