Debian Codegraph @

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