Debian Codegraph @

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