Debian Codegraph @

src/qd8-f16-qc8w-gemm/gen/qd8-f16-qc8w-gemm-3x16c4-minmax-neondotfp16arith.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_f16_qc8w_gemm_minmax_ukernel_3x16c4__neondotfp16arith(
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 xnn_float16* restrict c,
26 size_t cm_stride,
27 size_t cn_stride,
28 const union xnn_f16_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 uint16_t* c0 = (uint16_t*) c;
43 const int8_t* a1 = (const int8_t*) ((uintptr_t) a0 + a_stride);
44 uint16_t* c1 = (uint16_t*) ((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 uint16_t* c2 = (uint16_t*) ((uintptr_t) c1 + cm_stride);
51 if XNN_UNPREDICTABLE(mr <= 2) {
52 a2 = a1;
53 c2 = c1;
54 }
55
56 // Loop over groups of 16 columns.
57 do {
58 // Initialize accumulators with bias. 16 bias values are loaded from the
59 // weight matrix, at the start of the group of 16 columns.
60 const int32x4_t vinput_zero_point01 = vld1q_s32(&quantization_params[0].zero_point);
61 const int32x4_t vksum0123 = vld1q_s32(w); w = (const int32_t*) w + 4;
62 int32x4_t vacc0x0123 = vmulq_lane_s32(vksum0123, vget_low_s32(vinput_zero_point01), 0);
63 int32x4_t vacc1x0123 = vmulq_lane_s32(vksum0123, vget_high_s32(vinput_zero_point01), 0);
64 const int32x4_t vksum4567 = vld1q_s32(w); w = (const int32_t*) w + 4;
65 int32x4_t vacc0x4567 = vmulq_lane_s32(vksum4567, vget_low_s32(vinput_zero_point01), 0);
66 int32x4_t vacc1x4567 = vmulq_lane_s32(vksum4567, vget_high_s32(vinput_zero_point01), 0);
67 const int32x4_t vksum89AB = vld1q_s32(w); w = (const int32_t*) w + 4;
68 int32x4_t vacc0x89AB = vmulq_lane_s32(vksum89AB, vget_low_s32(vinput_zero_point01), 0);
69 int32x4_t vacc1x89AB = vmulq_lane_s32(vksum89AB, vget_high_s32(vinput_zero_point01), 0);
70 const int32x4_t vksumCDEF = vld1q_s32(w); w = (const int32_t*) w + 4;
71 int32x4_t vacc0xCDEF = vmulq_lane_s32(vksumCDEF, vget_low_s32(vinput_zero_point01), 0);
72 int32x4_t vacc1xCDEF = vmulq_lane_s32(vksumCDEF, vget_high_s32(vinput_zero_point01), 0);
73 const int32x4_t vinput_zero_point2 = vld1q_dup_s32(&quantization_params[2].zero_point);
74 int32x4_t vacc2x0123 = vmulq_s32(vksum0123, vinput_zero_point2);
75 int32x4_t vacc2x4567 = vmulq_s32(vksum4567, vinput_zero_point2);
76 int32x4_t vacc2x89AB = vmulq_s32(vksum89AB, vinput_zero_point2);
77 int32x4_t vacc2xCDEF = vmulq_s32(vksumCDEF, vinput_zero_point2);
78
79 // Inner accumulation loop along the 16 columns.
80 size_t k = kc;
81 // 2x partial unrolled loop to load 8 bytes at a time.
82 while (k >= 8 * sizeof(int8_t)) {
83 // Load a 3x8 block of activations.
84 const int8x8_t va0x01234567 = vld1_s8(a0); a0 += 8;
85 const int8x8_t va1x01234567 = vld1_s8(a1); a1 += 8;
86 const int8x8_t va2x01234567 = vld1_s8(a2); a2 += 8;
87
88 // Load a 8x16 block of weights.
89 const int8x16_t vb0123x0123 = vld1q_s8(w); w = (const int8_t*) w + 16;
90 const int8x16_t vb0123x4567 = vld1q_s8(w); w = (const int8_t*) w + 16;
91 const int8x16_t vb0123x89AB = vld1q_s8(w); w = (const int8_t*) w + 16;
92 const int8x16_t vb0123xCDEF = vld1q_s8(w); w = (const int8_t*) w + 16;
93 const int8x16_t vb4567x0123 = vld1q_s8(w); w = (const int8_t*) w + 16;
94 const int8x16_t vb4567x4567 = vld1q_s8(w); w = (const int8_t*) w + 16;
95 const int8x16_t vb4567x89AB = vld1q_s8(w); w = (const int8_t*) w + 16;
96 const int8x16_t vb4567xCDEF = vld1q_s8(w); w = (const int8_t*) w + 16;
97
98 // Multiply-accumulate: 3x8 * 8x16 --> 3x16.
99 vacc0x0123 = vdotq_lane_s32(vacc0x0123, vb0123x0123, va0x01234567, 0);
100 vacc0x4567 = vdotq_lane_s32(vacc0x4567, vb0123x4567, va0x01234567, 0);
101 vacc0x89AB = vdotq_lane_s32(vacc0x89AB, vb0123x89AB, va0x01234567, 0);
102 vacc0xCDEF = vdotq_lane_s32(vacc0xCDEF, vb0123xCDEF, va0x01234567, 0);
103 vacc1x0123 = vdotq_lane_s32(vacc1x0123, vb0123x0123, va1x01234567, 0);
104 vacc1x4567 = vdotq_lane_s32(vacc1x4567, vb0123x4567, va1x01234567, 0);
105 vacc1x89AB = vdotq_lane_s32(vacc1x89AB, vb0123x89AB, va1x01234567, 0);
106 vacc1xCDEF = vdotq_lane_s32(vacc1xCDEF, vb0123xCDEF, va1x01234567, 0);
107 vacc2x0123 = vdotq_lane_s32(vacc2x0123, vb0123x0123, va2x01234567, 0);
108 vacc2x4567 = vdotq_lane_s32(vacc2x4567, vb0123x4567, va2x01234567, 0);
109 vacc2x89AB = vdotq_lane_s32(vacc2x89AB, vb0123x89AB, va2x01234567, 0);
110 vacc2xCDEF = vdotq_lane_s32(vacc2xCDEF, vb0123xCDEF, va2x01234567, 0);
111 vacc0x0123 = vdotq_lane_s32(vacc0x0123, vb4567x0123, va0x01234567, 1);
112 vacc0x4567 = vdotq_lane_s32(vacc0x4567, vb4567x4567, va0x01234567, 1);
113 vacc0x89AB = vdotq_lane_s32(vacc0x89AB, vb4567x89AB, va0x01234567, 1);
114 vacc0xCDEF = vdotq_lane_s32(vacc0xCDEF, vb4567xCDEF, va0x01234567, 1);
115 vacc1x0123 = vdotq_lane_s32(vacc1x0123, vb4567x0123, va1x01234567, 1);
116 vacc1x4567 = vdotq_lane_s32(vacc1x4567, vb4567x4567, va1x01234567, 1);
117 vacc1x89AB = vdotq_lane_s32(vacc1x89AB, vb4567x89AB, va1x01234567, 1);
118 vacc1xCDEF = vdotq_lane_s32(vacc1xCDEF, vb4567xCDEF, va1x01234567, 1);
119 vacc2x0123 = vdotq_lane_s32(vacc2x0123, vb4567x0123, va2x01234567, 1);
120 vacc2x4567 = vdotq_lane_s32(vacc2x4567, vb4567x4567, va2x01234567, 1);
121 vacc2x89AB = vdotq_lane_s32(vacc2x89AB, vb4567x89AB, va2x01234567, 1);
122 vacc2xCDEF = vdotq_lane_s32(vacc2xCDEF, vb4567xCDEF, va2x01234567, 1);
123
124 k -= 8 * sizeof(int8_t);
125 }
126 // Handle up to 4 final positions of `k`
127 if XNN_UNLIKELY(k != 0) {
128 // Load a 3x4 block of activations.
129 const int8x8_t va0x01234567 = vld1_s8(a0); a0 += 4;
130 const int8x8_t va1x01234567 = vld1_s8(a1); a1 += 4;
131 const int8x8_t va2x01234567 = vld1_s8(a2); a2 += 4;
132
133 // Load a 4x16 block of weights.
134 const int8x16_t vb0123x0123 = vld1q_s8(w); w = (const int8_t*) w + 16;
135 const int8x16_t vb0123x4567 = vld1q_s8(w); w = (const int8_t*) w + 16;
136 const int8x16_t vb0123x89AB = vld1q_s8(w); w = (const int8_t*) w + 16;
137 const int8x16_t vb0123xCDEF = vld1q_s8(w); w = (const int8_t*) w + 16;
138
139 // Multiply-accumulate: 3x4 * 4x16 --> 3x16.
140 vacc0x0123 = vdotq_lane_s32(vacc0x0123, vb0123x0123, va0x01234567, 0);
141 vacc0x4567 = vdotq_lane_s32(vacc0x4567, vb0123x4567, va0x01234567, 0);
142 vacc0x89AB = vdotq_lane_s32(vacc0x89AB, vb0123x89AB, va0x01234567, 0);
143 vacc0xCDEF = vdotq_lane_s32(vacc0xCDEF, vb0123xCDEF, va0x01234567, 0);
144 vacc1x0123 = vdotq_lane_s32(vacc1x0123, vb0123x0123, va1x01234567, 0);
145 vacc1x4567 = vdotq_lane_s32(vacc1x4567, vb0123x4567, va1x01234567, 0);
146 vacc1x89AB = vdotq_lane_s32(vacc1x89AB, vb0123x89AB, va1x01234567, 0);
147 vacc1xCDEF = vdotq_lane_s32(vacc1xCDEF, vb0123xCDEF, va1x01234567, 0);
148 vacc2x0123 = vdotq_lane_s32(vacc2x0123, vb0123x0123, va2x01234567, 0);
149 vacc2x4567 = vdotq_lane_s32(vacc2x4567, vb0123x4567, va2x01234567, 0);
150 vacc2x89AB = vdotq_lane_s32(vacc2x89AB, vb0123x89AB, va2x01234567, 0);
151 vacc2xCDEF = vdotq_lane_s32(vacc2xCDEF, vb0123xCDEF, va2x01234567, 0);
152 }
153
154 float32x4_t vout0x0123 = vcvtq_f32_s32(vacc0x0123);
155 float32x4_t vout0x4567 = vcvtq_f32_s32(vacc0x4567);
156 float32x4_t vout0x89AB = vcvtq_f32_s32(vacc0x89AB);
157 float32x4_t vout0xCDEF = vcvtq_f32_s32(vacc0xCDEF);
158 float32x4_t vout1x0123 = vcvtq_f32_s32(vacc1x0123);
159 float32x4_t vout1x4567 = vcvtq_f32_s32(vacc1x4567);
160 float32x4_t vout1x89AB = vcvtq_f32_s32(vacc1x89AB);
161 float32x4_t vout1xCDEF = vcvtq_f32_s32(vacc1xCDEF);
162 float32x4_t vout2x0123 = vcvtq_f32_s32(vacc2x0123);
163 float32x4_t vout2x4567 = vcvtq_f32_s32(vacc2x4567);
164 float32x4_t vout2x89AB = vcvtq_f32_s32(vacc2x89AB);
165 float32x4_t vout2xCDEF = vcvtq_f32_s32(vacc2xCDEF);
166 const float32x4_t vinput_scale01 = vreinterpretq_f32_s32(vld1q_s32(&quantization_params[0].zero_point));
167 vout0x0123 = vmulq_lane_f32(vout0x0123, vget_low_f32(vinput_scale01), 1);
168 vout1x0123 = vmulq_lane_f32(vout1x0123, vget_high_f32(vinput_scale01), 1);
169 vout0x4567 = vmulq_lane_f32(vout0x4567, vget_low_f32(vinput_scale01), 1);
170 vout1x4567 = vmulq_lane_f32(vout1x4567, vget_high_f32(vinput_scale01), 1);
171 vout0x89AB = vmulq_lane_f32(vout0x89AB, vget_low_f32(vinput_scale01), 1);
172 vout1x89AB = vmulq_lane_f32(vout1x89AB, vget_high_f32(vinput_scale01), 1);
173 vout0xCDEF = vmulq_lane_f32(vout0xCDEF, vget_low_f32(vinput_scale01), 1);
174 vout1xCDEF = vmulq_lane_f32(vout1xCDEF, vget_high_f32(vinput_scale01), 1);
175 const float32x4_t vinput_scale2 = vld1q_dup_f32(&quantization_params[2].inv_scale);
176 vout2x0123 = vmulq_f32(vout2x0123, vinput_scale2);
177 vout2x4567 = vmulq_f32(vout2x4567, vinput_scale2);
178 vout2x89AB = vmulq_f32(vout2x89AB, vinput_scale2);
179 vout2xCDEF = vmulq_f32(vout2xCDEF, vinput_scale2);
180
181 const float32x4_t vfilter_output_scale0123 = vld1q_f32(w); w = (const float*) w + 4;
182 const float32x4_t vfilter_output_scale4567 = vld1q_f32(w); w = (const float*) w + 4;
183 const float32x4_t vfilter_output_scale89AB = vld1q_f32(w); w = (const float*) w + 4;
184 const float32x4_t vfilter_output_scaleCDEF = vld1q_f32(w); w = (const float*) w + 4;
185
186 const float32x4_t vbias0123 = vld1q_f32(w); w = (const float*) w + 4;
187 #if XNN_ARCH_ARM64
188 vout0x0123 = vfmaq_f32(vbias0123, vout0x0123, vfilter_output_scale0123);
189 vout1x0123 = vfmaq_f32(vbias0123, vout1x0123, vfilter_output_scale0123);
190 vout2x0123 = vfmaq_f32(vbias0123, vout2x0123, vfilter_output_scale0123);
191 #else
192 vout0x0123 = vmlaq_f32(vbias0123, vout0x0123, vfilter_output_scale0123);
193 vout1x0123 = vmlaq_f32(vbias0123, vout1x0123, vfilter_output_scale0123);
194 vout2x0123 = vmlaq_f32(vbias0123, vout2x0123, vfilter_output_scale0123);
195 #endif
196 const float32x4_t vbias4567 = vld1q_f32(w); w = (const float*) w + 4;
197 #if XNN_ARCH_ARM64
198 vout0x4567 = vfmaq_f32(vbias4567, vout0x4567, vfilter_output_scale4567);
199 vout1x4567 = vfmaq_f32(vbias4567, vout1x4567, vfilter_output_scale4567);
200 vout2x4567 = vfmaq_f32(vbias4567, vout2x4567, vfilter_output_scale4567);
201 #else
202 vout0x4567 = vmlaq_f32(vbias4567, vout0x4567, vfilter_output_scale4567);
203 vout1x4567 = vmlaq_f32(vbias4567, vout1x4567, vfilter_output_scale4567);
204 vout2x4567 = vmlaq_f32(vbias4567, vout2x4567, vfilter_output_scale4567);
205 #endif
206 const float32x4_t vbias89AB = vld1q_f32(w); w = (const float*) w + 4;
207 #if XNN_ARCH_ARM64
208 vout0x89AB = vfmaq_f32(vbias89AB, vout0x89AB, vfilter_output_scale89AB);
209 vout1x89AB = vfmaq_f32(vbias89AB, vout1x89AB, vfilter_output_scale89AB);
210 vout2x89AB = vfmaq_f32(vbias89AB, vout2x89AB, vfilter_output_scale89AB);
211 #else
212 vout0x89AB = vmlaq_f32(vbias89AB, vout0x89AB, vfilter_output_scale89AB);
213 vout1x89AB = vmlaq_f32(vbias89AB, vout1x89AB, vfilter_output_scale89AB);
214 vout2x89AB = vmlaq_f32(vbias89AB, vout2x89AB, vfilter_output_scale89AB);
215 #endif
216 const float32x4_t vbiasCDEF = vld1q_f32(w); w = (const float*) w + 4;
217 #if XNN_ARCH_ARM64
218 vout0xCDEF = vfmaq_f32(vbiasCDEF, vout0xCDEF, vfilter_output_scaleCDEF);
219 vout1xCDEF = vfmaq_f32(vbiasCDEF, vout1xCDEF, vfilter_output_scaleCDEF);
220 vout2xCDEF = vfmaq_f32(vbiasCDEF, vout2xCDEF, vfilter_output_scaleCDEF);
221 #else
222 vout0xCDEF = vmlaq_f32(vbiasCDEF, vout0xCDEF, vfilter_output_scaleCDEF);
223 vout1xCDEF = vmlaq_f32(vbiasCDEF, vout1xCDEF, vfilter_output_scaleCDEF);
224 vout2xCDEF = vmlaq_f32(vbiasCDEF, vout2xCDEF, vfilter_output_scaleCDEF);
225 #endif
226
227 float16x8_t vfp16out0x01234567 = vcombine_f16(vcvt_f16_f32(vout0x0123), vcvt_f16_f32(vout0x4567));
228 float16x8_t vfp16out0x89ABCDEF = vcombine_f16(vcvt_f16_f32(vout0x89AB), vcvt_f16_f32(vout0xCDEF));
229 float16x8_t vfp16out1x01234567 = vcombine_f16(vcvt_f16_f32(vout1x0123), vcvt_f16_f32(vout1x4567));
230 float16x8_t vfp16out1x89ABCDEF = vcombine_f16(vcvt_f16_f32(vout1x89AB), vcvt_f16_f32(vout1xCDEF));
231 float16x8_t vfp16out2x01234567 = vcombine_f16(vcvt_f16_f32(vout2x0123), vcvt_f16_f32(vout2x4567));
232 float16x8_t vfp16out2x89ABCDEF = vcombine_f16(vcvt_f16_f32(vout2x89AB), vcvt_f16_f32(vout2xCDEF));
233
234 const float16x8_t voutput_min = vreinterpretq_f16_u16(vld1q_dup_u16((const uint16_t*) &params->scalar.min));
235 vfp16out0x01234567 = vmaxq_f16(vfp16out0x01234567, voutput_min);
236 vfp16out0x89ABCDEF = vmaxq_f16(vfp16out0x89ABCDEF, voutput_min);
237 vfp16out1x01234567 = vmaxq_f16(vfp16out1x01234567, voutput_min);
238 vfp16out1x89ABCDEF = vmaxq_f16(vfp16out1x89ABCDEF, voutput_min);
239 vfp16out2x01234567 = vmaxq_f16(vfp16out2x01234567, voutput_min);
240 vfp16out2x89ABCDEF = vmaxq_f16(vfp16out2x89ABCDEF, voutput_min);
241 const float16x8_t voutput_max = vreinterpretq_f16_u16(vld1q_dup_u16((const uint16_t*) &params->scalar.max));
242 vfp16out0x01234567 = vminq_f16(vfp16out0x01234567, voutput_max);
243 vfp16out0x89ABCDEF = vminq_f16(vfp16out0x89ABCDEF, voutput_max);
244 vfp16out1x01234567 = vminq_f16(vfp16out1x01234567, voutput_max);
245 vfp16out1x89ABCDEF = vminq_f16(vfp16out1x89ABCDEF, voutput_max);
246 vfp16out2x01234567 = vminq_f16(vfp16out2x01234567, voutput_max);
247 vfp16out2x89ABCDEF = vminq_f16(vfp16out2x89ABCDEF, voutput_max);
248 if XNN_LIKELY(nc >= 16) {
249 vst1q_u16(c0, vreinterpretq_u16_f16(vfp16out0x01234567));
250 vst1q_u16(c0 + 8, vreinterpretq_u16_f16(vfp16out0x89ABCDEF));
251 vst1q_u16(c1, vreinterpretq_u16_f16(vfp16out1x01234567));
252 vst1q_u16(c1 + 8, vreinterpretq_u16_f16(vfp16out1x89ABCDEF));
253 vst1q_u16(c2, vreinterpretq_u16_f16(vfp16out2x01234567));
254 vst1q_u16(c2 + 8, vreinterpretq_u16_f16(vfp16out2x89ABCDEF));
255
256 a0 = (const int8_t*) ((uintptr_t) a0 - kc);
257 a1 = (const int8_t*) ((uintptr_t) a1 - kc);
258 a2 = (const int8_t*) ((uintptr_t) a2 - kc);
259
260 c0 = (uint16_t*) ((uintptr_t) c0 + cn_stride);
261 c1 = (uint16_t*) ((uintptr_t) c1 + cn_stride);
262 c2 = (uint16_t*) ((uintptr_t) c2 + cn_stride);
263
264 nc -= 16;
265 } else {
266 if (nc & 8) {
267 vst1q_u16(c0, vreinterpretq_u16_f16(vfp16out0x01234567)); c0 += 8;
268 vfp16out0x01234567 = vfp16out0x89ABCDEF;
269 vst1q_u16(c1, vreinterpretq_u16_f16(vfp16out1x01234567)); c1 += 8;
270 vfp16out1x01234567 = vfp16out1x89ABCDEF;
271 vst1q_u16(c2, vreinterpretq_u16_f16(vfp16out2x01234567)); c2 += 8;
272 vfp16out2x01234567 = vfp16out2x89ABCDEF;
273 }
274 float16x4_t vfp16out0x0123 = vget_low_f16(vfp16out0x01234567);
275 float16x4_t vfp16out1x0123 = vget_low_f16(vfp16out1x01234567);
276 float16x4_t vfp16out2x0123 = vget_low_f16(vfp16out2x01234567);
277 if (nc & 4) {
278 vst1_u16(c0, vreinterpret_u16_f16(vfp16out0x0123)); c0 += 4;
279 vst1_u16(c1, vreinterpret_u16_f16(vfp16out1x0123)); c1 += 4;
280 vst1_u16(c2, vreinterpret_u16_f16(vfp16out2x0123)); c2 += 4;
281 vfp16out0x0123 = vget_high_f16(vfp16out0x01234567);
282 vfp16out1x0123 = vget_high_f16(vfp16out1x01234567);
283 vfp16out2x0123 = vget_high_f16(vfp16out2x01234567);
284 }
285 if (nc & 2) {
286 vst1_lane_u32((void*) c0, vreinterpret_u32_f16(vfp16out0x0123), 0); c0 += 2;
287 vst1_lane_u32((void*) c1, vreinterpret_u32_f16(vfp16out1x0123), 0); c1 += 2;
288 vst1_lane_u32((void*) c2, vreinterpret_u32_f16(vfp16out2x0123), 0); c2 += 2;
289 vfp16out0x0123 = vext_f16(vfp16out0x0123, vfp16out0x0123, 2);
290 vfp16out1x0123 = vext_f16(vfp16out1x0123, vfp16out1x0123, 2);
291 vfp16out2x0123 = vext_f16(vfp16out2x0123, vfp16out2x0123, 2);
292 }
293 if (nc & 1) {
294 vst1_lane_u16(c0, vreinterpret_u16_f16(vfp16out0x0123), 0);
295 vst1_lane_u16(c1, vreinterpret_u16_f16(vfp16out1x0123), 0);
296 vst1_lane_u16(c2, vreinterpret_u16_f16(vfp16out2x0123), 0);
297 }
298 nc = 0;
299 }
300 } while (nc != 0);
301 }