Debian Codegraph @

src/qd8-f16-qc8w-igemm/gen/qd8-f16-qc8w-igemm-4x8c4-minmax-neondotfp16arith.c

raw
1 // Auto-generated file. Do not edit!
2 // Template: src/qs8-igemm/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/igemm.h"
15 #include "xnnpack/math.h"
16
17
18 void xnn_qd8_f16_qc8w_igemm_minmax_ukernel_4x8c4__neondotfp16arith(
19 size_t mr,
20 size_t nc,
21 size_t kc,
22 size_t ks,
23 const int8_t** restrict a,
24 const void* restrict w,
25 xnn_float16* restrict c,
26 size_t cm_stride,
27 size_t cn_stride,
28 size_t a_offset,
29 const int8_t* zero,
30 const int8_t* zero_data,
31 const union xnn_f16_minmax_params params[restrict XNN_MIN_ELEMENTS(1)],
32 const struct xnn_qd8_quantization_params quantization_params[restrict XNN_MIN_ELEMENTS(1)]) XNN_OOB_READS
33 {
34 assert(mr != 0);
35 assert(mr <= 4);
36 assert(nc != 0);
37 assert(kc != 0);
38 assert(ks != 0);
39 assert(ks % (4 * sizeof(void*)) == 0);
40 assert(a_offset % sizeof(int8_t) == 0);
41 assert(a != NULL);
42 assert(w != NULL);
43 assert(c != NULL);
44
45 kc = round_up_po2(kc, 4 * sizeof(int8_t));
46 uint16_t* c0 = (uint16_t*) c;
47 uint16_t* c1 = (uint16_t*) ((uintptr_t) c0 + cm_stride);
48 if XNN_UNPREDICTABLE(mr < 2) {
49 c1 = c0;
50 }
51 uint16_t* c2 = (uint16_t*) ((uintptr_t) c1 + cm_stride);
52 if XNN_UNPREDICTABLE(mr <= 2) {
53 c2 = c1;
54 }
55 uint16_t* c3 = (uint16_t*) ((uintptr_t) c2 + cm_stride);
56 if XNN_UNPREDICTABLE(mr != 4) {
57 c3 = c2;
58 }
59
60 do {
61 const int32x4_t vinput_zero_point = vld1q_dup_s32(&quantization_params->zero_point);
62 const int32x4_t vksum0123 = vld1q_s32(w); w = (const int32_t*) w + 4;
63 int32x4_t vacc0x0123 = vmulq_s32(vksum0123, vinput_zero_point);
64 const int32x4_t vksum4567 = vld1q_s32(w); w = (const int32_t*) w + 4;
65 int32x4_t vacc0x4567 = vmulq_s32(vksum4567, vinput_zero_point);
66 int32x4_t vacc1x0123 = vacc0x0123;
67 int32x4_t vacc1x4567 = vacc0x4567;
68 int32x4_t vacc2x0123 = vacc0x0123;
69 int32x4_t vacc2x4567 = vacc0x4567;
70 int32x4_t vacc3x0123 = vacc0x0123;
71 int32x4_t vacc3x4567 = vacc0x4567;
72
73 size_t p = ks;
74 do {
75 const int8_t* restrict a0 = a[0];
76 if XNN_UNPREDICTABLE(a0 != zero) {
77 a0 = (const int8_t*) ((uintptr_t) a0 + a_offset);
78 } else {
79 a0 = zero_data;
80 }
81 const int8_t* restrict a1 = a[1];
82 if XNN_UNPREDICTABLE(a1 != zero) {
83 a1 = (const int8_t*) ((uintptr_t) a1 + a_offset);
84 } else {
85 a1 = zero_data;
86 }
87 const int8_t* restrict a2 = a[2];
88 if XNN_UNPREDICTABLE(a2 != zero) {
89 a2 = (const int8_t*) ((uintptr_t) a2 + a_offset);
90 } else {
91 a2 = zero_data;
92 }
93 const int8_t* restrict a3 = a[3];
94 if XNN_UNPREDICTABLE(a3 != zero) {
95 a3 = (const int8_t*) ((uintptr_t) a3 + a_offset);
96 } else {
97 a3 = zero_data;
98 }
99 a += 4;
100
101 // Inner accumulation loop along the 8 columns.
102 size_t k = kc;
103 // 2x partial unrolled loop to load 8 bytes at a time.
104 while (k >= 8 * sizeof(int8_t)) {
105 // Load a 4x8 block of activations.
106 const int8x8_t va0x01234567 = vld1_s8(a0); a0 += 8;
107 const int8x8_t va1x01234567 = vld1_s8(a1); a1 += 8;
108 const int8x8_t va2x01234567 = vld1_s8(a2); a2 += 8;
109 const int8x8_t va3x01234567 = vld1_s8(a3); a3 += 8;
110
111 // Load a 8x8 block of weights.
112 const int8x16_t vb0123x0123 = vld1q_s8(w); w = (const int8_t*) w + 16;
113 const int8x16_t vb0123x4567 = vld1q_s8(w); w = (const int8_t*) w + 16;
114 const int8x16_t vb4567x0123 = vld1q_s8(w); w = (const int8_t*) w + 16;
115 const int8x16_t vb4567x4567 = vld1q_s8(w); w = (const int8_t*) w + 16;
116
117 // Multiply-accumulate: 4x8 * 8x8 --> 4x8.
118 vacc0x0123 = vdotq_lane_s32(vacc0x0123, vb0123x0123, va0x01234567, 0);
119 vacc0x4567 = vdotq_lane_s32(vacc0x4567, vb0123x4567, va0x01234567, 0);
120 vacc1x0123 = vdotq_lane_s32(vacc1x0123, vb0123x0123, va1x01234567, 0);
121 vacc1x4567 = vdotq_lane_s32(vacc1x4567, vb0123x4567, va1x01234567, 0);
122 vacc2x0123 = vdotq_lane_s32(vacc2x0123, vb0123x0123, va2x01234567, 0);
123 vacc2x4567 = vdotq_lane_s32(vacc2x4567, vb0123x4567, va2x01234567, 0);
124 vacc3x0123 = vdotq_lane_s32(vacc3x0123, vb0123x0123, va3x01234567, 0);
125 vacc3x4567 = vdotq_lane_s32(vacc3x4567, vb0123x4567, va3x01234567, 0);
126 vacc0x0123 = vdotq_lane_s32(vacc0x0123, vb4567x0123, va0x01234567, 1);
127 vacc0x4567 = vdotq_lane_s32(vacc0x4567, vb4567x4567, va0x01234567, 1);
128 vacc1x0123 = vdotq_lane_s32(vacc1x0123, vb4567x0123, va1x01234567, 1);
129 vacc1x4567 = vdotq_lane_s32(vacc1x4567, vb4567x4567, va1x01234567, 1);
130 vacc2x0123 = vdotq_lane_s32(vacc2x0123, vb4567x0123, va2x01234567, 1);
131 vacc2x4567 = vdotq_lane_s32(vacc2x4567, vb4567x4567, va2x01234567, 1);
132 vacc3x0123 = vdotq_lane_s32(vacc3x0123, vb4567x0123, va3x01234567, 1);
133 vacc3x4567 = vdotq_lane_s32(vacc3x4567, vb4567x4567, va3x01234567, 1);
134
135 k -= 8 * sizeof(int8_t);
136 }
137 // Handle up to 4 final positions of `k`
138 if XNN_UNLIKELY(k != 0) {
139 // Load a 4x4 block of activations.
140 const int8x8_t va0x01234567 = vld1_s8(a0);
141 const int8x8_t va1x01234567 = vld1_s8(a1);
142 const int8x8_t va2x01234567 = vld1_s8(a2);
143 const int8x8_t va3x01234567 = vld1_s8(a3);
144
145 // Load a 4x8 block of weights.
146 const int8x16_t vb0123x0123 = vld1q_s8(w); w = (const int8_t*) w + 16;
147 const int8x16_t vb0123x4567 = vld1q_s8(w); w = (const int8_t*) w + 16;
148
149 // Multiply-accumulate: 4x4 * 4x8 --> 4x8.
150 vacc0x0123 = vdotq_lane_s32(vacc0x0123, vb0123x0123, va0x01234567, 0);
151 vacc0x4567 = vdotq_lane_s32(vacc0x4567, vb0123x4567, va0x01234567, 0);
152 vacc1x0123 = vdotq_lane_s32(vacc1x0123, vb0123x0123, va1x01234567, 0);
153 vacc1x4567 = vdotq_lane_s32(vacc1x4567, vb0123x4567, va1x01234567, 0);
154 vacc2x0123 = vdotq_lane_s32(vacc2x0123, vb0123x0123, va2x01234567, 0);
155 vacc2x4567 = vdotq_lane_s32(vacc2x4567, vb0123x4567, va2x01234567, 0);
156 vacc3x0123 = vdotq_lane_s32(vacc3x0123, vb0123x0123, va3x01234567, 0);
157 vacc3x4567 = vdotq_lane_s32(vacc3x4567, vb0123x4567, va3x01234567, 0);
158 }
159 p -= 4 * sizeof(void*);
160 } while (p != 0);
161
162 float32x4_t vout0x0123 = vcvtq_f32_s32(vacc0x0123);
163 float32x4_t vout0x4567 = vcvtq_f32_s32(vacc0x4567);
164 float32x4_t vout1x0123 = vcvtq_f32_s32(vacc1x0123);
165 float32x4_t vout1x4567 = vcvtq_f32_s32(vacc1x4567);
166 float32x4_t vout2x0123 = vcvtq_f32_s32(vacc2x0123);
167 float32x4_t vout2x4567 = vcvtq_f32_s32(vacc2x4567);
168 float32x4_t vout3x0123 = vcvtq_f32_s32(vacc3x0123);
169 float32x4_t vout3x4567 = vcvtq_f32_s32(vacc3x4567);
170 const float32x4_t vinput_scale = vld1q_dup_f32(&quantization_params->inv_scale);
171 vout0x0123 = vmulq_f32(vout0x0123, vinput_scale);
172 vout0x4567 = vmulq_f32(vout0x4567, vinput_scale);
173 vout1x0123 = vmulq_f32(vout1x0123, vinput_scale);
174 vout1x4567 = vmulq_f32(vout1x4567, vinput_scale);
175 vout2x0123 = vmulq_f32(vout2x0123, vinput_scale);
176 vout2x4567 = vmulq_f32(vout2x4567, vinput_scale);
177 vout3x0123 = vmulq_f32(vout3x0123, vinput_scale);
178 vout3x4567 = vmulq_f32(vout3x4567, vinput_scale);
179
180 const float32x4_t vfilter_output_scale0123 = vld1q_f32(w); w = (const float*) w + 4;
181 const float32x4_t vfilter_output_scale4567 = vld1q_f32(w); w = (const float*) w + 4;
182
183 const float32x4_t vbias0123 = vld1q_f32(w); w = (const float*) w + 4;
184 #if XNN_ARCH_ARM64
185 vout0x0123 = vfmaq_f32(vbias0123, vout0x0123, vfilter_output_scale0123);
186 vout1x0123 = vfmaq_f32(vbias0123, vout1x0123, vfilter_output_scale0123);
187 vout2x0123 = vfmaq_f32(vbias0123, vout2x0123, vfilter_output_scale0123);
188 vout3x0123 = vfmaq_f32(vbias0123, vout3x0123, 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 #endif
195 const float32x4_t vbias4567 = vld1q_f32(w); w = (const float*) w + 4;
196 #if XNN_ARCH_ARM64
197 vout0x4567 = vfmaq_f32(vbias4567, vout0x4567, vfilter_output_scale4567);
198 vout1x4567 = vfmaq_f32(vbias4567, vout1x4567, vfilter_output_scale4567);
199 vout2x4567 = vfmaq_f32(vbias4567, vout2x4567, vfilter_output_scale4567);
200 vout3x4567 = vfmaq_f32(vbias4567, vout3x4567, 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 vout3x4567 = vmlaq_f32(vbias4567, vout3x4567, vfilter_output_scale4567);
206 #endif
207
208 float16x8_t vfp16out0x01234567 = vcombine_f16(vcvt_f16_f32(vout0x0123), vcvt_f16_f32(vout0x4567));
209 float16x8_t vfp16out1x01234567 = vcombine_f16(vcvt_f16_f32(vout1x0123), vcvt_f16_f32(vout1x4567));
210 float16x8_t vfp16out2x01234567 = vcombine_f16(vcvt_f16_f32(vout2x0123), vcvt_f16_f32(vout2x4567));
211 float16x8_t vfp16out3x01234567 = vcombine_f16(vcvt_f16_f32(vout3x0123), vcvt_f16_f32(vout3x4567));
212
213 const float16x8_t voutput_min = vreinterpretq_f16_u16(vld1q_dup_u16((const uint16_t*) &params->scalar.min));
214 vfp16out0x01234567 = vmaxq_f16(vfp16out0x01234567, voutput_min);
215 vfp16out1x01234567 = vmaxq_f16(vfp16out1x01234567, voutput_min);
216 vfp16out2x01234567 = vmaxq_f16(vfp16out2x01234567, voutput_min);
217 vfp16out3x01234567 = vmaxq_f16(vfp16out3x01234567, voutput_min);
218 const float16x8_t voutput_max = vreinterpretq_f16_u16(vld1q_dup_u16((const uint16_t*) &params->scalar.max));
219 vfp16out0x01234567 = vminq_f16(vfp16out0x01234567, voutput_max);
220 vfp16out1x01234567 = vminq_f16(vfp16out1x01234567, voutput_max);
221 vfp16out2x01234567 = vminq_f16(vfp16out2x01234567, voutput_max);
222 vfp16out3x01234567 = vminq_f16(vfp16out3x01234567, voutput_max);
223 if XNN_LIKELY(nc >= 8) {
224 vst1q_u16(c3, vreinterpretq_u16_f16(vfp16out3x01234567));
225 vst1q_u16(c2, vreinterpretq_u16_f16(vfp16out2x01234567));
226 vst1q_u16(c1, vreinterpretq_u16_f16(vfp16out1x01234567));
227 vst1q_u16(c0, vreinterpretq_u16_f16(vfp16out0x01234567));
228
229 a = (const int8_t**restrict) ((uintptr_t) a - ks);
230
231 c0 = (uint16_t*) ((uintptr_t) c0 + cn_stride);
232 c1 = (uint16_t*) ((uintptr_t) c1 + cn_stride);
233 c2 = (uint16_t*) ((uintptr_t) c2 + cn_stride);
234 c3 = (uint16_t*) ((uintptr_t) c3 + cn_stride);
235
236 nc -= 8;
237 } else {
238 float16x4_t vfp16out3x0123 = vget_low_f16(vfp16out3x01234567);
239 float16x4_t vfp16out2x0123 = vget_low_f16(vfp16out2x01234567);
240 float16x4_t vfp16out1x0123 = vget_low_f16(vfp16out1x01234567);
241 float16x4_t vfp16out0x0123 = vget_low_f16(vfp16out0x01234567);
242 if (nc & 4) {
243 vst1_u16(c3, vreinterpret_u16_f16(vfp16out3x0123)); c3 += 4;
244 vst1_u16(c2, vreinterpret_u16_f16(vfp16out2x0123)); c2 += 4;
245 vst1_u16(c1, vreinterpret_u16_f16(vfp16out1x0123)); c1 += 4;
246 vst1_u16(c0, vreinterpret_u16_f16(vfp16out0x0123)); c0 += 4;
247 vfp16out3x0123 = vget_high_f16(vfp16out3x01234567);
248 vfp16out2x0123 = vget_high_f16(vfp16out2x01234567);
249 vfp16out1x0123 = vget_high_f16(vfp16out1x01234567);
250 vfp16out0x0123 = vget_high_f16(vfp16out0x01234567);
251 }
252 if (nc & 2) {
253 vst1_lane_u32((void*) c3, vreinterpret_u32_f16(vfp16out3x0123), 0); c3 += 2;
254 vst1_lane_u32((void*) c2, vreinterpret_u32_f16(vfp16out2x0123), 0); c2 += 2;
255 vst1_lane_u32((void*) c1, vreinterpret_u32_f16(vfp16out1x0123), 0); c1 += 2;
256 vst1_lane_u32((void*) c0, vreinterpret_u32_f16(vfp16out0x0123), 0); c0 += 2;
257 vfp16out3x0123 = vext_f16(vfp16out3x0123, vfp16out3x0123, 2);
258 vfp16out2x0123 = vext_f16(vfp16out2x0123, vfp16out2x0123, 2);
259 vfp16out1x0123 = vext_f16(vfp16out1x0123, vfp16out1x0123, 2);
260 vfp16out0x0123 = vext_f16(vfp16out0x0123, vfp16out0x0123, 2);
261 }
262 if (nc & 1) {
263 vst1_lane_u16(c3, vreinterpret_u16_f16(vfp16out3x0123), 0);
264 vst1_lane_u16(c2, vreinterpret_u16_f16(vfp16out2x0123), 0);
265 vst1_lane_u16(c1, vreinterpret_u16_f16(vfp16out1x0123), 0);
266 vst1_lane_u16(c0, vreinterpret_u16_f16(vfp16out0x0123), 0);
267 }
268 nc = 0;
269 }
270 } while (nc != 0);
271 }