Debian Codegraph @

src/qd8-f16-qc4w-gemm/gen/qd8-f16-qc4w-gemm-4x8c8-minmax-neoni8mm.c

raw
1 // Auto-generated file. Do not edit!
2 // Template: src/qs8-gemm/c8-neoni8mm.c.in
3 // Generator: tools/xngen
4 //
5 // Copyright 2023 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_4x8c8__neoni8mm(
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, 8 * 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 int8x16_t vmask = vmovq_n_s8(INT8_C(0xF0));
62
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 const int32x4_t vksumzp0x0123 = vmulq_lane_s32(vksum0123, vget_low_s32(vinput_zero_point01), 0);
70 const int32x4_t vksumzp1x0123 = 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 const int32x4_t vksumzp0x4567 = vmulq_lane_s32(vksum4567, vget_low_s32(vinput_zero_point01), 0);
73 const int32x4_t vksumzp1x4567 = 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 const int32x4_t vksumzp2x0123 = vmulq_lane_s32(vksum0123, vget_low_s32(vinput_zero_point23), 0);
76 const int32x4_t vksumzp3x0123 = vmulq_lane_s32(vksum0123, vget_high_s32(vinput_zero_point23), 0);
77 const int32x4_t vksumzp2x4567 = vmulq_lane_s32(vksum4567, vget_low_s32(vinput_zero_point23), 0);
78 const int32x4_t vksumzp3x4567 = vmulq_lane_s32(vksum4567, vget_high_s32(vinput_zero_point23), 0);
79
80 int32x4_t vacc01x01 = vreinterpretq_s32_u64(vtrn1q_u64(vreinterpretq_u64_s32(vksumzp0x0123), vreinterpretq_u64_s32(vksumzp1x0123)));
81 int32x4_t vacc01x23 = vreinterpretq_s32_u64(vtrn2q_u64(vreinterpretq_u64_s32(vksumzp0x0123), vreinterpretq_u64_s32(vksumzp1x0123)));
82 int32x4_t vacc01x45 = vreinterpretq_s32_u64(vtrn1q_u64(vreinterpretq_u64_s32(vksumzp0x4567), vreinterpretq_u64_s32(vksumzp1x4567)));
83 int32x4_t vacc01x67 = vreinterpretq_s32_u64(vtrn2q_u64(vreinterpretq_u64_s32(vksumzp0x4567), vreinterpretq_u64_s32(vksumzp1x4567)));
84 int32x4_t vacc23x01 = vreinterpretq_s32_u64(vtrn1q_u64(vreinterpretq_u64_s32(vksumzp2x0123), vreinterpretq_u64_s32(vksumzp3x0123)));
85 int32x4_t vacc23x23 = vreinterpretq_s32_u64(vtrn2q_u64(vreinterpretq_u64_s32(vksumzp2x0123), vreinterpretq_u64_s32(vksumzp3x0123)));
86 int32x4_t vacc23x45 = vreinterpretq_s32_u64(vtrn1q_u64(vreinterpretq_u64_s32(vksumzp2x4567), vreinterpretq_u64_s32(vksumzp3x4567)));
87 int32x4_t vacc23x67 = vreinterpretq_s32_u64(vtrn2q_u64(vreinterpretq_u64_s32(vksumzp2x4567), vreinterpretq_u64_s32(vksumzp3x4567)));
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
93 uint64x2x2_t va01x0123456789ABCDEF;
94 va01x0123456789ABCDEF.val[0] = vdupq_n_u64(0);
95 va01x0123456789ABCDEF.val[1] = vdupq_n_u64(0);
96 uint64x2x2_t va23x0123456789ABCDEF;
97 va23x0123456789ABCDEF.val[0] = vdupq_n_u64(0);
98 va23x0123456789ABCDEF.val[1] = vdupq_n_u64(0);
99
100 while (k >= 16 * sizeof(int8_t)) {
101 // Load a 4x16 block of activations.
102 va01x0123456789ABCDEF = vld2q_lane_u64((const void*) a0, va01x0123456789ABCDEF, 0); a0 += 16;
103 va23x0123456789ABCDEF = vld2q_lane_u64((const void*) a2, va23x0123456789ABCDEF, 0); a2 += 16;
104 va01x0123456789ABCDEF = vld2q_lane_u64((const void*) a1, va01x0123456789ABCDEF, 1); a1 += 16;
105 va23x0123456789ABCDEF = vld2q_lane_u64((const void*) a3, va23x0123456789ABCDEF, 1); a3 += 16;
106
107 const int8x16_t va01x01234567 = vreinterpretq_s8_u64(va01x0123456789ABCDEF.val[0]);
108 const int8x16_t va01x89ABCDEF = vreinterpretq_s8_u64(va01x0123456789ABCDEF.val[1]);
109 const int8x16_t va23x01234567 = vreinterpretq_s8_u64(va23x0123456789ABCDEF.val[0]);
110 const int8x16_t va23x89ABCDEF = vreinterpretq_s8_u64(va23x0123456789ABCDEF.val[1]);
111
112 // Load a 16x8 block of weights.
113 const int8x16_t vb01x0123456789ABCDEF = vld1q_s8(w); w = (const int8_t*) w + 16;
114 const int8x16_t vb23x0123456789ABCDEF = vld1q_s8(w); w = (const int8_t*) w + 16;
115 const int8x16_t vb45x0123456789ABCDEF = vld1q_s8(w); w = (const int8_t*) w + 16;
116 const int8x16_t vb67x0123456789ABCDEF = vld1q_s8(w); w = (const int8_t*) w + 16;
117 const int8x16_t vb01x01234567 = vshlq_n_s8(vb01x0123456789ABCDEF, 4);
118 const int8x16_t vb23x01234567 = vshlq_n_s8(vb23x0123456789ABCDEF, 4);
119 const int8x16_t vb45x01234567 = vshlq_n_s8(vb45x0123456789ABCDEF, 4);
120 const int8x16_t vb67x01234567 = vshlq_n_s8(vb67x0123456789ABCDEF, 4);
121 const int8x16_t vb01x89ABCDEF = vandq_s8(vb01x0123456789ABCDEF, vmask);
122 const int8x16_t vb23x89ABCDEF = vandq_s8(vb23x0123456789ABCDEF, vmask);
123 const int8x16_t vb45x89ABCDEF = vandq_s8(vb45x0123456789ABCDEF, vmask);
124 const int8x16_t vb67x89ABCDEF = vandq_s8(vb67x0123456789ABCDEF, vmask);
125
126 // Multiply-accumulate: 4x8 * 8x8 --> 4x8.
127 vacc01x01 = vmmlaq_s32(vacc01x01, va01x01234567, vb01x01234567);
128 vacc01x23 = vmmlaq_s32(vacc01x23, va01x01234567, vb23x01234567);
129 vacc01x45 = vmmlaq_s32(vacc01x45, va01x01234567, vb45x01234567);
130 vacc01x67 = vmmlaq_s32(vacc01x67, va01x01234567, vb67x01234567);
131 vacc23x01 = vmmlaq_s32(vacc23x01, va23x01234567, vb01x01234567);
132 vacc23x23 = vmmlaq_s32(vacc23x23, va23x01234567, vb23x01234567);
133 vacc23x45 = vmmlaq_s32(vacc23x45, va23x01234567, vb45x01234567);
134 vacc23x67 = vmmlaq_s32(vacc23x67, va23x01234567, vb67x01234567);
135 vacc01x01 = vmmlaq_s32(vacc01x01, va01x89ABCDEF, vb01x89ABCDEF);
136 vacc01x23 = vmmlaq_s32(vacc01x23, va01x89ABCDEF, vb23x89ABCDEF);
137 vacc01x45 = vmmlaq_s32(vacc01x45, va01x89ABCDEF, vb45x89ABCDEF);
138 vacc01x67 = vmmlaq_s32(vacc01x67, va01x89ABCDEF, vb67x89ABCDEF);
139 vacc23x01 = vmmlaq_s32(vacc23x01, va23x89ABCDEF, vb01x89ABCDEF);
140 vacc23x23 = vmmlaq_s32(vacc23x23, va23x89ABCDEF, vb23x89ABCDEF);
141 vacc23x45 = vmmlaq_s32(vacc23x45, va23x89ABCDEF, vb45x89ABCDEF);
142 vacc23x67 = vmmlaq_s32(vacc23x67, va23x89ABCDEF, vb67x89ABCDEF);
143
144 k -= 16 * sizeof(int8_t);
145 }
146 // Handle up to 8 final positions of `k`
147 if XNN_UNLIKELY(k != 0) {
148 // Load a 4x8 block of activations.
149 uint64x2_t va01x01234567 = vld1q_dup_u64((const void*) a0); a0 += 8;
150 uint64x2_t va23x01234567 = vld1q_dup_u64((const void*) a2); a2 += 8;
151 va01x01234567 = vld1q_lane_u64((const void*) a1, va01x01234567, 1); a1 += 8;
152 va23x01234567 = vld1q_lane_u64((const void*) a3, va23x01234567, 1); a3 += 8;
153
154 // Load a 16x8 block of weights.
155 const int8x16_t vb01x0123456789ABCDEF = vld1q_s8(w); w = (const int8_t*) w + 16;
156 const int8x16_t vb23x0123456789ABCDEF = vld1q_s8(w); w = (const int8_t*) w + 16;
157 const int8x16_t vb45x0123456789ABCDEF = vld1q_s8(w); w = (const int8_t*) w + 16;
158 const int8x16_t vb67x0123456789ABCDEF = vld1q_s8(w); w = (const int8_t*) w + 16;
159 const int8x16_t vb01x01234567 = vshlq_n_s8(vb01x0123456789ABCDEF, 4);
160 const int8x16_t vb23x01234567 = vshlq_n_s8(vb23x0123456789ABCDEF, 4);
161 const int8x16_t vb45x01234567 = vshlq_n_s8(vb45x0123456789ABCDEF, 4);
162 const int8x16_t vb67x01234567 = vshlq_n_s8(vb67x0123456789ABCDEF, 4);
163
164 // Multiply-accumulate: 4x4 * 4x8 --> 4x8.
165 vacc01x01 = vmmlaq_s32(vacc01x01, vreinterpretq_s8_u64(va01x01234567), vb01x01234567);
166 vacc01x23 = vmmlaq_s32(vacc01x23, vreinterpretq_s8_u64(va01x01234567), vb23x01234567);
167 vacc01x45 = vmmlaq_s32(vacc01x45, vreinterpretq_s8_u64(va01x01234567), vb45x01234567);
168 vacc01x67 = vmmlaq_s32(vacc01x67, vreinterpretq_s8_u64(va01x01234567), vb67x01234567);
169 vacc23x01 = vmmlaq_s32(vacc23x01, vreinterpretq_s8_u64(va23x01234567), vb01x01234567);
170 vacc23x23 = vmmlaq_s32(vacc23x23, vreinterpretq_s8_u64(va23x01234567), vb23x01234567);
171 vacc23x45 = vmmlaq_s32(vacc23x45, vreinterpretq_s8_u64(va23x01234567), vb45x01234567);
172 vacc23x67 = vmmlaq_s32(vacc23x67, vreinterpretq_s8_u64(va23x01234567), vb67x01234567);
173 }
174
175 int32x4_t vacc0x0123 = vreinterpretq_s32_u64(vtrn1q_u64(vreinterpretq_u64_s32(vacc01x01), vreinterpretq_u64_s32(vacc01x23)));
176 int32x4_t vacc1x0123 = vreinterpretq_s32_u64(vtrn2q_u64(vreinterpretq_u64_s32(vacc01x01), vreinterpretq_u64_s32(vacc01x23)));
177 int32x4_t vacc0x4567 = vreinterpretq_s32_u64(vtrn1q_u64(vreinterpretq_u64_s32(vacc01x45), vreinterpretq_u64_s32(vacc01x67)));
178 int32x4_t vacc1x4567 = vreinterpretq_s32_u64(vtrn2q_u64(vreinterpretq_u64_s32(vacc01x45), vreinterpretq_u64_s32(vacc01x67)));
179 int32x4_t vacc2x0123 = vreinterpretq_s32_u64(vtrn1q_u64(vreinterpretq_u64_s32(vacc23x01), vreinterpretq_u64_s32(vacc23x23)));
180 int32x4_t vacc3x0123 = vreinterpretq_s32_u64(vtrn2q_u64(vreinterpretq_u64_s32(vacc23x01), vreinterpretq_u64_s32(vacc23x23)));
181 int32x4_t vacc2x4567 = vreinterpretq_s32_u64(vtrn1q_u64(vreinterpretq_u64_s32(vacc23x45), vreinterpretq_u64_s32(vacc23x67)));
182 int32x4_t vacc3x4567 = vreinterpretq_s32_u64(vtrn2q_u64(vreinterpretq_u64_s32(vacc23x45), vreinterpretq_u64_s32(vacc23x67)));
183 float32x4_t vout0x0123 = vcvtq_n_f32_s32(vacc0x0123, 4);
184 float32x4_t vout0x4567 = vcvtq_n_f32_s32(vacc0x4567, 4);
185 float32x4_t vout1x0123 = vcvtq_n_f32_s32(vacc1x0123, 4);
186 float32x4_t vout1x4567 = vcvtq_n_f32_s32(vacc1x4567, 4);
187 float32x4_t vout2x0123 = vcvtq_n_f32_s32(vacc2x0123, 4);
188 float32x4_t vout2x4567 = vcvtq_n_f32_s32(vacc2x4567, 4);
189 float32x4_t vout3x0123 = vcvtq_n_f32_s32(vacc3x0123, 4);
190 float32x4_t vout3x4567 = vcvtq_n_f32_s32(vacc3x4567, 4);
191
192 const float32x4_t vinput_scale01 = vreinterpretq_f32_s32(vld1q_s32(&quantization_params[0].zero_point));
193 vout0x0123 = vmulq_lane_f32(vout0x0123, vget_low_f32(vinput_scale01), 1);
194 vout1x0123 = vmulq_lane_f32(vout1x0123, vget_high_f32(vinput_scale01), 1);
195 vout0x4567 = vmulq_lane_f32(vout0x4567, vget_low_f32(vinput_scale01), 1);
196 vout1x4567 = vmulq_lane_f32(vout1x4567, vget_high_f32(vinput_scale01), 1);
197 const float32x4_t vinput_scale23 = vreinterpretq_f32_s32(vld1q_s32(&quantization_params[2].zero_point));
198 vout2x0123 = vmulq_lane_f32(vout2x0123, vget_low_f32(vinput_scale23), 1);
199 vout3x0123 = vmulq_lane_f32(vout3x0123, vget_high_f32(vinput_scale23), 1);
200 vout2x4567 = vmulq_lane_f32(vout2x4567, vget_low_f32(vinput_scale23), 1);
201 vout3x4567 = vmulq_lane_f32(vout3x4567, vget_high_f32(vinput_scale23), 1);
202
203 const float32x4_t vfilter_output_scale0123 = vld1q_f32(w); w = (const float*) w + 4;
204 const float32x4_t vfilter_output_scale4567 = vld1q_f32(w); w = (const float*) w + 4;
205
206 const float32x4_t vbias0123 = vld1q_f32(w); w = (const float*) w + 4;
207 vout0x0123 = vfmaq_f32(vbias0123, vout0x0123, vfilter_output_scale0123);
208 vout1x0123 = vfmaq_f32(vbias0123, vout1x0123, vfilter_output_scale0123);
209 vout2x0123 = vfmaq_f32(vbias0123, vout2x0123, vfilter_output_scale0123);
210 vout3x0123 = vfmaq_f32(vbias0123, vout3x0123, vfilter_output_scale0123);
211 const float32x4_t vbias4567 = vld1q_f32(w); w = (const float*) w + 4;
212 vout0x4567 = vfmaq_f32(vbias4567, vout0x4567, vfilter_output_scale4567);
213 vout1x4567 = vfmaq_f32(vbias4567, vout1x4567, vfilter_output_scale4567);
214 vout2x4567 = vfmaq_f32(vbias4567, vout2x4567, vfilter_output_scale4567);
215 vout3x4567 = vfmaq_f32(vbias4567, vout3x4567, vfilter_output_scale4567);
216
217 float16x8_t vfp16out0x01234567 = vcombine_f16(vcvt_f16_f32(vout0x0123), vcvt_f16_f32(vout0x4567));
218 float16x8_t vfp16out1x01234567 = vcombine_f16(vcvt_f16_f32(vout1x0123), vcvt_f16_f32(vout1x4567));
219 float16x8_t vfp16out2x01234567 = vcombine_f16(vcvt_f16_f32(vout2x0123), vcvt_f16_f32(vout2x4567));
220 float16x8_t vfp16out3x01234567 = vcombine_f16(vcvt_f16_f32(vout3x0123), vcvt_f16_f32(vout3x4567));
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 const float16x8_t voutput_max = vreinterpretq_f16_u16(vld1q_dup_u16((const uint16_t*) &params->scalar.max));
228 vfp16out0x01234567 = vminq_f16(vfp16out0x01234567, voutput_max);
229 vfp16out1x01234567 = vminq_f16(vfp16out1x01234567, voutput_max);
230 vfp16out2x01234567 = vminq_f16(vfp16out2x01234567, voutput_max);
231 vfp16out3x01234567 = vminq_f16(vfp16out3x01234567, voutput_max);
232 if XNN_LIKELY(nc >= 8) {
233 vst1q_u16(c0, vreinterpretq_u16_f16(vfp16out0x01234567));
234 vst1q_u16(c1, vreinterpretq_u16_f16(vfp16out1x01234567));
235 vst1q_u16(c2, vreinterpretq_u16_f16(vfp16out2x01234567));
236 vst1q_u16(c3, vreinterpretq_u16_f16(vfp16out3x01234567));
237
238 a0 = (const int8_t*) ((uintptr_t) a0 - kc);
239 a1 = (const int8_t*) ((uintptr_t) a1 - kc);
240 a2 = (const int8_t*) ((uintptr_t) a2 - kc);
241 a3 = (const int8_t*) ((uintptr_t) a3 - kc);
242
243 c0 = (uint16_t*) ((uintptr_t) c0 + cn_stride);
244 c1 = (uint16_t*) ((uintptr_t) c1 + cn_stride);
245 c2 = (uint16_t*) ((uintptr_t) c2 + cn_stride);
246 c3 = (uint16_t*) ((uintptr_t) c3 + 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 if (nc & 4) {
255 vst1_u16(c0, vreinterpret_u16_f16(vfp16out0x0123)); c0 += 4;
256 vst1_u16(c1, vreinterpret_u16_f16(vfp16out1x0123)); c1 += 4;
257 vst1_u16(c2, vreinterpret_u16_f16(vfp16out2x0123)); c2 += 4;
258 vst1_u16(c3, vreinterpret_u16_f16(vfp16out3x0123)); c3 += 4;
259 vfp16out0x0123 = vget_high_f16(vfp16out0x01234567);
260 vfp16out1x0123 = vget_high_f16(vfp16out1x01234567);
261 vfp16out2x0123 = vget_high_f16(vfp16out2x01234567);
262 vfp16out3x0123 = vget_high_f16(vfp16out3x01234567);
263 }
264 if (nc & 2) {
265 vst1_lane_u32((void*) c0, vreinterpret_u32_f16(vfp16out0x0123), 0); c0 += 2;
266 vst1_lane_u32((void*) c1, vreinterpret_u32_f16(vfp16out1x0123), 0); c1 += 2;
267 vst1_lane_u32((void*) c2, vreinterpret_u32_f16(vfp16out2x0123), 0); c2 += 2;
268 vst1_lane_u32((void*) c3, vreinterpret_u32_f16(vfp16out3x0123), 0); c3 += 2;
269 vfp16out0x0123 = vext_f16(vfp16out0x0123, vfp16out0x0123, 2);
270 vfp16out1x0123 = vext_f16(vfp16out1x0123, vfp16out1x0123, 2);
271 vfp16out2x0123 = vext_f16(vfp16out2x0123, vfp16out2x0123, 2);
272 vfp16out3x0123 = vext_f16(vfp16out3x0123, vfp16out3x0123, 2);
273 }
274 if (nc & 1) {
275 vst1_lane_u16(c0, vreinterpret_u16_f16(vfp16out0x0123), 0);
276 vst1_lane_u16(c1, vreinterpret_u16_f16(vfp16out1x0123), 0);
277 vst1_lane_u16(c2, vreinterpret_u16_f16(vfp16out2x0123), 0);
278 vst1_lane_u16(c3, vreinterpret_u16_f16(vfp16out3x0123), 0);
279 }
280 nc = 0;
281 }
282 } while (nc != 0);
283 }