Debian Codegraph @

src/qd8-f32-qc4w-gemm/gen/qd8-f32-qc4w-gemm-3x16c4-minmax-neondot.c

raw
1 // Auto-generated file. Do not edit!
2 // Template: src/qs8-gemm/c4-neondot.c.in
3 // Generator: tools/xngen
4 //
5 // Copyright 2020 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/gemm.h"
15 #include "xnnpack/math.h"
16
17
18 void xnn_qd8_f32_qc4w_gemm_minmax_ukernel_3x16c4__neondot(
19 size_t mr,
20 size_t nc,
21 size_t kc,
22 const int8_t* restrict a,
23 size_t a_stride,
24 const void* restrict w,
25 float* restrict c,
26 size_t cm_stride,
27 size_t cn_stride,
28 const struct xnn_f32_qc4w_minmax_params params[restrict XNN_MIN_ELEMENTS(1)],
29 const struct xnn_qd8_quantization_params quantization_params[restrict XNN_MIN_ELEMENTS(1)]) XNN_OOB_READS
30 {
31 assert(mr != 0);
32 assert(mr <= 3);
33 assert(nc != 0);
34 assert(kc != 0);
35 assert(kc % sizeof(int8_t) == 0);
36 assert(a != NULL);
37 assert(w != NULL);
38 assert(c != NULL);
39
40 kc = round_up_po2(kc, 4 * sizeof(int8_t));
41 const int8_t* a0 = a;
42 float* c0 = c;
43 const int8_t* a1 = (const int8_t*) ((uintptr_t) a0 + a_stride);
44 float* c1 = (float*) ((uintptr_t) c0 + cm_stride);
45 if XNN_UNPREDICTABLE(mr < 2) {
46 a1 = a0;
47 c1 = c0;
48 }
49 const int8_t* a2 = (const int8_t*) ((uintptr_t) a1 + a_stride);
50 float* c2 = (float*) ((uintptr_t) c1 + cm_stride);
51 if XNN_UNPREDICTABLE(mr <= 2) {
52 a2 = a1;
53 c2 = c1;
54 }
55
56 const int8x16_t vmask = vmovq_n_s8(INT8_C(0xF0));
57 // Loop over groups of 16 columns.
58 do {
59 // Initialize accumulators with bias. 16 bias values are loaded from the
60 // weight matrix, at the start of the group of 16 columns.
61 const int32x4_t vinput_zero_point01 = vld1q_s32(&quantization_params[0].zero_point);
62 const int32x4_t vksum0123 = vld1q_s32(w); w = (const int32_t*) w + 4;
63 int32x4_t vacc0x0123 = vmulq_lane_s32(vksum0123, vget_low_s32(vinput_zero_point01), 0);
64 int32x4_t vacc1x0123 = vmulq_lane_s32(vksum0123, vget_high_s32(vinput_zero_point01), 0);
65 const int32x4_t vksum4567 = vld1q_s32(w); w = (const int32_t*) w + 4;
66 int32x4_t vacc0x4567 = vmulq_lane_s32(vksum4567, vget_low_s32(vinput_zero_point01), 0);
67 int32x4_t vacc1x4567 = vmulq_lane_s32(vksum4567, vget_high_s32(vinput_zero_point01), 0);
68 const int32x4_t vksum89AB = vld1q_s32(w); w = (const int32_t*) w + 4;
69 int32x4_t vacc0x89AB = vmulq_lane_s32(vksum89AB, vget_low_s32(vinput_zero_point01), 0);
70 int32x4_t vacc1x89AB = vmulq_lane_s32(vksum89AB, vget_high_s32(vinput_zero_point01), 0);
71 const int32x4_t vksumCDEF = vld1q_s32(w); w = (const int32_t*) w + 4;
72 int32x4_t vacc0xCDEF = vmulq_lane_s32(vksumCDEF, vget_low_s32(vinput_zero_point01), 0);
73 int32x4_t vacc1xCDEF = vmulq_lane_s32(vksumCDEF, vget_high_s32(vinput_zero_point01), 0);
74 const int32x4_t vinput_zero_point2 = vld1q_dup_s32(&quantization_params[2].zero_point);
75 int32x4_t vacc2x0123 = vmulq_s32(vksum0123, vinput_zero_point2);
76 int32x4_t vacc2x4567 = vmulq_s32(vksum4567, vinput_zero_point2);
77 int32x4_t vacc2x89AB = vmulq_s32(vksum89AB, vinput_zero_point2);
78 int32x4_t vacc2xCDEF = vmulq_s32(vksumCDEF, vinput_zero_point2);
79
80 // Inner accumulation loop along the 16 columns.
81 size_t k = kc;
82 // 2x partial unrolled loop to load 8 bytes at a time.
83 while (k >= 8 * sizeof(int8_t)) {
84 // Load a 3x8 block of activations.
85 const int8x8_t va0x01234567 = vld1_s8(a0); a0 += 8;
86 const int8x8_t va1x01234567 = vld1_s8(a1); a1 += 8;
87 const int8x8_t va2x01234567 = vld1_s8(a2); a2 += 8;
88
89 // Load a 8x16 block of weights.
90 const int8x16_t vb01234567x0123 = vld1q_s8(w); w = (const int8_t*) w + 16;
91 const int8x16_t vb01234567x4567 = vld1q_s8(w); w = (const int8_t*) w + 16;
92 const int8x16_t vb01234567x89AB = vld1q_s8(w); w = (const int8_t*) w + 16;
93 const int8x16_t vb01234567xCDEF = vld1q_s8(w); w = (const int8_t*) w + 16;
94 const int8x16_t vb0123x0123 = vshlq_n_s8(vb01234567x0123, 4);
95 const int8x16_t vb0123x4567 = vshlq_n_s8(vb01234567x4567, 4);
96 const int8x16_t vb0123x89AB = vshlq_n_s8(vb01234567x89AB, 4);
97 const int8x16_t vb0123xCDEF = vshlq_n_s8(vb01234567xCDEF, 4);
98 const int8x16_t vb4567x0123 = vandq_s8(vb01234567x0123, vmask);
99 const int8x16_t vb4567x4567 = vandq_s8(vb01234567x4567, vmask);
100 const int8x16_t vb4567x89AB = vandq_s8(vb01234567x89AB, vmask);
101 const int8x16_t vb4567xCDEF = vandq_s8(vb01234567xCDEF, vmask);
102
103 // Multiply-accumulate: 3x8 * 8x16 --> 3x16.
104 vacc0x0123 = vdotq_lane_s32(vacc0x0123, vb0123x0123, va0x01234567, 0);
105 vacc0x4567 = vdotq_lane_s32(vacc0x4567, vb0123x4567, va0x01234567, 0);
106 vacc0x89AB = vdotq_lane_s32(vacc0x89AB, vb0123x89AB, va0x01234567, 0);
107 vacc0xCDEF = vdotq_lane_s32(vacc0xCDEF, vb0123xCDEF, va0x01234567, 0);
108 vacc1x0123 = vdotq_lane_s32(vacc1x0123, vb0123x0123, va1x01234567, 0);
109 vacc1x4567 = vdotq_lane_s32(vacc1x4567, vb0123x4567, va1x01234567, 0);
110 vacc1x89AB = vdotq_lane_s32(vacc1x89AB, vb0123x89AB, va1x01234567, 0);
111 vacc1xCDEF = vdotq_lane_s32(vacc1xCDEF, vb0123xCDEF, va1x01234567, 0);
112 vacc2x0123 = vdotq_lane_s32(vacc2x0123, vb0123x0123, va2x01234567, 0);
113 vacc2x4567 = vdotq_lane_s32(vacc2x4567, vb0123x4567, va2x01234567, 0);
114 vacc2x89AB = vdotq_lane_s32(vacc2x89AB, vb0123x89AB, va2x01234567, 0);
115 vacc2xCDEF = vdotq_lane_s32(vacc2xCDEF, vb0123xCDEF, va2x01234567, 0);
116 vacc0x0123 = vdotq_lane_s32(vacc0x0123, vb4567x0123, va0x01234567, 1);
117 vacc0x4567 = vdotq_lane_s32(vacc0x4567, vb4567x4567, va0x01234567, 1);
118 vacc0x89AB = vdotq_lane_s32(vacc0x89AB, vb4567x89AB, va0x01234567, 1);
119 vacc0xCDEF = vdotq_lane_s32(vacc0xCDEF, vb4567xCDEF, va0x01234567, 1);
120 vacc1x0123 = vdotq_lane_s32(vacc1x0123, vb4567x0123, va1x01234567, 1);
121 vacc1x4567 = vdotq_lane_s32(vacc1x4567, vb4567x4567, va1x01234567, 1);
122 vacc1x89AB = vdotq_lane_s32(vacc1x89AB, vb4567x89AB, va1x01234567, 1);
123 vacc1xCDEF = vdotq_lane_s32(vacc1xCDEF, vb4567xCDEF, va1x01234567, 1);
124 vacc2x0123 = vdotq_lane_s32(vacc2x0123, vb4567x0123, va2x01234567, 1);
125 vacc2x4567 = vdotq_lane_s32(vacc2x4567, vb4567x4567, va2x01234567, 1);
126 vacc2x89AB = vdotq_lane_s32(vacc2x89AB, vb4567x89AB, va2x01234567, 1);
127 vacc2xCDEF = vdotq_lane_s32(vacc2xCDEF, vb4567xCDEF, va2x01234567, 1);
128
129 k -= 8 * sizeof(int8_t);
130 }
131 // Handle up to 4 final positions of `k`
132 if XNN_UNLIKELY(k != 0) {
133 // Load a 3x4 block of activations.
134 const int8x8_t va0x01234567 = vld1_s8(a0); a0 += 4;
135 const int8x8_t va1x01234567 = vld1_s8(a1); a1 += 4;
136 const int8x8_t va2x01234567 = vld1_s8(a2); a2 += 4;
137
138 // Load a 4x16 block of weights.
139 const int8x16_t vb01234567x0123 = vld1q_s8(w); w = (const int8_t*) w + 16;
140 const int8x16_t vb01234567x4567 = vld1q_s8(w); w = (const int8_t*) w + 16;
141 const int8x16_t vb01234567x89AB = vld1q_s8(w); w = (const int8_t*) w + 16;
142 const int8x16_t vb01234567xCDEF = vld1q_s8(w); w = (const int8_t*) w + 16;
143 const int8x16_t vb0123x0123 = vshlq_n_s8(vb01234567x0123, 4);
144 const int8x16_t vb0123x4567 = vshlq_n_s8(vb01234567x4567, 4);
145 const int8x16_t vb0123x89AB = vshlq_n_s8(vb01234567x89AB, 4);
146 const int8x16_t vb0123xCDEF = vshlq_n_s8(vb01234567xCDEF, 4);
147
148 // Multiply-accumulate: 3x4 * 4x16 --> 3x16.
149 vacc0x0123 = vdotq_lane_s32(vacc0x0123, vb0123x0123, va0x01234567, 0);
150 vacc0x4567 = vdotq_lane_s32(vacc0x4567, vb0123x4567, va0x01234567, 0);
151 vacc0x89AB = vdotq_lane_s32(vacc0x89AB, vb0123x89AB, va0x01234567, 0);
152 vacc0xCDEF = vdotq_lane_s32(vacc0xCDEF, vb0123xCDEF, va0x01234567, 0);
153 vacc1x0123 = vdotq_lane_s32(vacc1x0123, vb0123x0123, va1x01234567, 0);
154 vacc1x4567 = vdotq_lane_s32(vacc1x4567, vb0123x4567, va1x01234567, 0);
155 vacc1x89AB = vdotq_lane_s32(vacc1x89AB, vb0123x89AB, va1x01234567, 0);
156 vacc1xCDEF = vdotq_lane_s32(vacc1xCDEF, vb0123xCDEF, va1x01234567, 0);
157 vacc2x0123 = vdotq_lane_s32(vacc2x0123, vb0123x0123, va2x01234567, 0);
158 vacc2x4567 = vdotq_lane_s32(vacc2x4567, vb0123x4567, va2x01234567, 0);
159 vacc2x89AB = vdotq_lane_s32(vacc2x89AB, vb0123x89AB, va2x01234567, 0);
160 vacc2xCDEF = vdotq_lane_s32(vacc2xCDEF, vb0123xCDEF, va2x01234567, 0);
161 }
162
163 float32x4_t vout0x0123 = vcvtq_n_f32_s32(vacc0x0123, 4);
164 float32x4_t vout0x4567 = vcvtq_n_f32_s32(vacc0x4567, 4);
165 float32x4_t vout0x89AB = vcvtq_n_f32_s32(vacc0x89AB, 4);
166 float32x4_t vout0xCDEF = vcvtq_n_f32_s32(vacc0xCDEF, 4);
167 float32x4_t vout1x0123 = vcvtq_n_f32_s32(vacc1x0123, 4);
168 float32x4_t vout1x4567 = vcvtq_n_f32_s32(vacc1x4567, 4);
169 float32x4_t vout1x89AB = vcvtq_n_f32_s32(vacc1x89AB, 4);
170 float32x4_t vout1xCDEF = vcvtq_n_f32_s32(vacc1xCDEF, 4);
171 float32x4_t vout2x0123 = vcvtq_n_f32_s32(vacc2x0123, 4);
172 float32x4_t vout2x4567 = vcvtq_n_f32_s32(vacc2x4567, 4);
173 float32x4_t vout2x89AB = vcvtq_n_f32_s32(vacc2x89AB, 4);
174 float32x4_t vout2xCDEF = vcvtq_n_f32_s32(vacc2xCDEF, 4);
175 const float32x4_t vinput_scale01 = vreinterpretq_f32_s32(vld1q_s32(&quantization_params[0].zero_point));
176 vout0x0123 = vmulq_lane_f32(vout0x0123, vget_low_f32(vinput_scale01), 1);
177 vout1x0123 = vmulq_lane_f32(vout1x0123, vget_high_f32(vinput_scale01), 1);
178 vout0x4567 = vmulq_lane_f32(vout0x4567, vget_low_f32(vinput_scale01), 1);
179 vout1x4567 = vmulq_lane_f32(vout1x4567, vget_high_f32(vinput_scale01), 1);
180 vout0x89AB = vmulq_lane_f32(vout0x89AB, vget_low_f32(vinput_scale01), 1);
181 vout1x89AB = vmulq_lane_f32(vout1x89AB, vget_high_f32(vinput_scale01), 1);
182 vout0xCDEF = vmulq_lane_f32(vout0xCDEF, vget_low_f32(vinput_scale01), 1);
183 vout1xCDEF = vmulq_lane_f32(vout1xCDEF, vget_high_f32(vinput_scale01), 1);
184 const float32x4_t vinput_scale2 = vld1q_dup_f32(&quantization_params[2].inv_scale);
185 vout2x0123 = vmulq_f32(vout2x0123, vinput_scale2);
186 vout2x4567 = vmulq_f32(vout2x4567, vinput_scale2);
187 vout2x89AB = vmulq_f32(vout2x89AB, vinput_scale2);
188 vout2xCDEF = vmulq_f32(vout2xCDEF, vinput_scale2);
189
190 const float32x4_t vfilter_output_scale0123 = vld1q_f32(w); w = (const float*) w + 4;
191 const float32x4_t vfilter_output_scale4567 = vld1q_f32(w); w = (const float*) w + 4;
192 const float32x4_t vfilter_output_scale89AB = vld1q_f32(w); w = (const float*) w + 4;
193 const float32x4_t vfilter_output_scaleCDEF = vld1q_f32(w); w = (const float*) w + 4;
194
195 const float32x4_t vbias0123 = vld1q_f32(w); w = (const float*) w + 4;
196 #if XNN_ARCH_ARM64
197 vout0x0123 = vfmaq_f32(vbias0123, vout0x0123, vfilter_output_scale0123);
198 vout1x0123 = vfmaq_f32(vbias0123, vout1x0123, vfilter_output_scale0123);
199 vout2x0123 = vfmaq_f32(vbias0123, vout2x0123, vfilter_output_scale0123);
200 #else
201 vout0x0123 = vmlaq_f32(vbias0123, vout0x0123, vfilter_output_scale0123);
202 vout1x0123 = vmlaq_f32(vbias0123, vout1x0123, vfilter_output_scale0123);
203 vout2x0123 = vmlaq_f32(vbias0123, vout2x0123, vfilter_output_scale0123);
204 #endif
205 const float32x4_t vbias4567 = vld1q_f32(w); w = (const float*) w + 4;
206 #if XNN_ARCH_ARM64
207 vout0x4567 = vfmaq_f32(vbias4567, vout0x4567, vfilter_output_scale4567);
208 vout1x4567 = vfmaq_f32(vbias4567, vout1x4567, vfilter_output_scale4567);
209 vout2x4567 = vfmaq_f32(vbias4567, vout2x4567, vfilter_output_scale4567);
210 #else
211 vout0x4567 = vmlaq_f32(vbias4567, vout0x4567, vfilter_output_scale4567);
212 vout1x4567 = vmlaq_f32(vbias4567, vout1x4567, vfilter_output_scale4567);
213 vout2x4567 = vmlaq_f32(vbias4567, vout2x4567, vfilter_output_scale4567);
214 #endif
215 const float32x4_t vbias89AB = vld1q_f32(w); w = (const float*) w + 4;
216 #if XNN_ARCH_ARM64
217 vout0x89AB = vfmaq_f32(vbias89AB, vout0x89AB, vfilter_output_scale89AB);
218 vout1x89AB = vfmaq_f32(vbias89AB, vout1x89AB, vfilter_output_scale89AB);
219 vout2x89AB = vfmaq_f32(vbias89AB, vout2x89AB, vfilter_output_scale89AB);
220 #else
221 vout0x89AB = vmlaq_f32(vbias89AB, vout0x89AB, vfilter_output_scale89AB);
222 vout1x89AB = vmlaq_f32(vbias89AB, vout1x89AB, vfilter_output_scale89AB);
223 vout2x89AB = vmlaq_f32(vbias89AB, vout2x89AB, vfilter_output_scale89AB);
224 #endif
225 const float32x4_t vbiasCDEF = vld1q_f32(w); w = (const float*) w + 4;
226 #if XNN_ARCH_ARM64
227 vout0xCDEF = vfmaq_f32(vbiasCDEF, vout0xCDEF, vfilter_output_scaleCDEF);
228 vout1xCDEF = vfmaq_f32(vbiasCDEF, vout1xCDEF, vfilter_output_scaleCDEF);
229 vout2xCDEF = vfmaq_f32(vbiasCDEF, vout2xCDEF, vfilter_output_scaleCDEF);
230 #else
231 vout0xCDEF = vmlaq_f32(vbiasCDEF, vout0xCDEF, vfilter_output_scaleCDEF);
232 vout1xCDEF = vmlaq_f32(vbiasCDEF, vout1xCDEF, vfilter_output_scaleCDEF);
233 vout2xCDEF = vmlaq_f32(vbiasCDEF, vout2xCDEF, vfilter_output_scaleCDEF);
234 #endif
235
236 const float32x4_t voutput_min = vld1q_dup_f32(&params->scalar.min);
237 vout0x0123 = vmaxq_f32(vout0x0123, voutput_min);
238 vout0x4567 = vmaxq_f32(vout0x4567, voutput_min);
239 vout0x89AB = vmaxq_f32(vout0x89AB, voutput_min);
240 vout0xCDEF = vmaxq_f32(vout0xCDEF, voutput_min);
241 vout1x0123 = vmaxq_f32(vout1x0123, voutput_min);
242 vout1x4567 = vmaxq_f32(vout1x4567, voutput_min);
243 vout1x89AB = vmaxq_f32(vout1x89AB, voutput_min);
244 vout1xCDEF = vmaxq_f32(vout1xCDEF, voutput_min);
245 vout2x0123 = vmaxq_f32(vout2x0123, voutput_min);
246 vout2x4567 = vmaxq_f32(vout2x4567, voutput_min);
247 vout2x89AB = vmaxq_f32(vout2x89AB, voutput_min);
248 vout2xCDEF = vmaxq_f32(vout2xCDEF, voutput_min);
249
250 const float32x4_t voutput_max = vld1q_dup_f32(&params->scalar.max);
251 vout0x0123 = vminq_f32(vout0x0123, voutput_max);
252 vout0x4567 = vminq_f32(vout0x4567, voutput_max);
253 vout0x89AB = vminq_f32(vout0x89AB, voutput_max);
254 vout0xCDEF = vminq_f32(vout0xCDEF, voutput_max);
255 vout1x0123 = vminq_f32(vout1x0123, voutput_max);
256 vout1x4567 = vminq_f32(vout1x4567, voutput_max);
257 vout1x89AB = vminq_f32(vout1x89AB, voutput_max);
258 vout1xCDEF = vminq_f32(vout1xCDEF, voutput_max);
259 vout2x0123 = vminq_f32(vout2x0123, voutput_max);
260 vout2x4567 = vminq_f32(vout2x4567, voutput_max);
261 vout2x89AB = vminq_f32(vout2x89AB, voutput_max);
262 vout2xCDEF = vminq_f32(vout2xCDEF, voutput_max);
263
264 if XNN_LIKELY(nc >= 16) {
265 vst1q_f32(c0, vout0x0123);
266 vst1q_f32(c0 + 4, vout0x4567);
267 vst1q_f32(c0 + 8, vout0x89AB);
268 vst1q_f32(c0 + 12, vout0xCDEF);
269 vst1q_f32(c1, vout1x0123);
270 vst1q_f32(c1 + 4, vout1x4567);
271 vst1q_f32(c1 + 8, vout1x89AB);
272 vst1q_f32(c1 + 12, vout1xCDEF);
273 vst1q_f32(c2, vout2x0123);
274 vst1q_f32(c2 + 4, vout2x4567);
275 vst1q_f32(c2 + 8, vout2x89AB);
276 vst1q_f32(c2 + 12, vout2xCDEF);
277
278 a0 = (const int8_t*) ((uintptr_t) a0 - kc);
279 a1 = (const int8_t*) ((uintptr_t) a1 - kc);
280 a2 = (const int8_t*) ((uintptr_t) a2 - kc);
281
282 c0 = (float*) ((uintptr_t) c0 + cn_stride);
283 c1 = (float*) ((uintptr_t) c1 + cn_stride);
284 c2 = (float*) ((uintptr_t) c2 + cn_stride);
285
286 nc -= 16;
287 } else {
288 if (nc & 8) {
289 vst1q_f32(c0, vout0x0123); c0 += 4;
290 vout0x0123 = vout0x89AB;
291 vst1q_f32(c1, vout1x0123); c1 += 4;
292 vout1x0123 = vout1x89AB;
293 vst1q_f32(c2, vout2x0123); c2 += 4;
294 vout2x0123 = vout2x89AB;
295 vst1q_f32(c0, vout0x4567); c0 += 4;
296 vout0x4567 = vout0xCDEF;
297 vst1q_f32(c1, vout1x4567); c1 += 4;
298 vout1x4567 = vout1xCDEF;
299 vst1q_f32(c2, vout2x4567); c2 += 4;
300 vout2x4567 = vout2xCDEF;
301 }
302 if (nc & 4) {
303 vst1q_f32(c0, vout0x0123); c0 += 4;
304 vout0x0123 = vout0x4567;
305 vst1q_f32(c1, vout1x0123); c1 += 4;
306 vout1x0123 = vout1x4567;
307 vst1q_f32(c2, vout2x0123); c2 += 4;
308 vout2x0123 = vout2x4567;
309 }
310 float32x2_t vout0x01 = vget_low_f32(vout0x0123);
311 float32x2_t vout1x01 = vget_low_f32(vout1x0123);
312 float32x2_t vout2x01 = vget_low_f32(vout2x0123);
313 if (nc & 2) {
314 vst1_f32(c0, vout0x01); c0 += 2;
315 vst1_f32(c1, vout1x01); c1 += 2;
316 vst1_f32(c2, vout2x01); c2 += 2;
317 vout0x01 = vget_high_f32(vout0x0123);
318 vout1x01 = vget_high_f32(vout1x0123);
319 vout2x01 = vget_high_f32(vout2x0123);
320 }
321 if (nc & 1) {
322 vst1_lane_f32(c0, vout0x01, 0);
323 vst1_lane_f32(c1, vout1x01, 0);
324 vst1_lane_f32(c2, vout2x01, 0);
325 }
326 nc = 0;
327 }
328 } while (nc != 0);
329 }