Debian Codegraph @

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