Debian Codegraph @

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