Debian Codegraph @

src/qd8-f32-qb4w-gemm/gen/qd8-f32-qb4w-gemm-1x16c8-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_f32_qb4w_gemm_minmax_ukernel_1x16c8__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 float* restrict c,
26 size_t cm_stride,
27 size_t cn_stride,
28 const struct xnn_f32_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 <= 1);
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 size_t bl = params->scalar.blocksize;
42 assert(bl <= kc);
43 assert(bl != 0);
44 assert(bl % 32 == 0);
45 const int8_t* a0 = a;
46 float* c0 = c;
47 const int8x16_t vmask = vmovq_n_s8(INT8_C(0xF0));
48
49 // Loop over groups of 16 columns.
50 do {
51 // Initialize accumulators with scaled vksum. 16 scaled vksum values are loaded from the
52 // weight matrix, at the start of the group of 16 columns.
53 const float32x4_t vinput_zero_point01 = vcvtq_f32_s32(vld1q_dup_s32(&quantization_params[0].zero_point));
54 const float32x4_t vksum0123 = vld1q_f32(w); w = (const float*) w + 4;
55 float32x4_t vout0x0123 = vmulq_f32(vksum0123, vinput_zero_point01);
56 const float32x4_t vksum4567 = vld1q_f32(w); w = (const float*) w + 4;
57 float32x4_t vout0x4567 = vmulq_f32(vksum4567, vinput_zero_point01);
58 const float32x4_t vksum89AB = vld1q_f32(w); w = (const float*) w + 4;
59 float32x4_t vout0x89AB = vmulq_f32(vksum89AB, vinput_zero_point01);
60 const float32x4_t vksumCDEF = vld1q_f32(w); w = (const float*) w + 4;
61 float32x4_t vout0xCDEF = vmulq_f32(vksumCDEF, vinput_zero_point01);
62
63
64 for (size_t kb=0; kb < kc; kb += bl) {
65 int32x4_t vacc01x01 = vdupq_n_s32(0);
66 int32x4_t vacc01x23 = vdupq_n_s32(0);
67 int32x4_t vacc01x45 = vdupq_n_s32(0);
68 int32x4_t vacc01x67 = vdupq_n_s32(0);
69 int32x4_t vacc01x89 = vdupq_n_s32(0);
70 int32x4_t vacc01xAB = vdupq_n_s32(0);
71 int32x4_t vacc01xCD = vdupq_n_s32(0);
72 int32x4_t vacc01xEF = vdupq_n_s32(0);
73
74 size_t k = bl;
75 // 2x partial unrolled loop to load 8 bytes at a time.
76
77 uint64x2x2_t va01x0123456789ABCDEF;
78 va01x0123456789ABCDEF.val[0] = vdupq_n_u64(0);
79 va01x0123456789ABCDEF.val[1] = vdupq_n_u64(0);
80
81 while (k >= 16 * sizeof(int8_t)) {
82 // Load a 1x16 block of activations.
83 va01x0123456789ABCDEF = vld2q_lane_u64((const void*) a0, va01x0123456789ABCDEF, 0); a0 += 16;
84
85 const int8x16_t va01x01234567 = vreinterpretq_s8_u64(va01x0123456789ABCDEF.val[0]);
86 const int8x16_t va01x89ABCDEF = vreinterpretq_s8_u64(va01x0123456789ABCDEF.val[1]);
87
88 // Load a 16x16 block of weights.
89 const int8x16_t vb01x0123456789ABCDEF = vld1q_s8(w); w = (const int8_t*) w + 16;
90 const int8x16_t vb23x0123456789ABCDEF = vld1q_s8(w); w = (const int8_t*) w + 16;
91 const int8x16_t vb45x0123456789ABCDEF = vld1q_s8(w); w = (const int8_t*) w + 16;
92 const int8x16_t vb67x0123456789ABCDEF = vld1q_s8(w); w = (const int8_t*) w + 16;
93 const int8x16_t vb89x0123456789ABCDEF = vld1q_s8(w); w = (const int8_t*) w + 16;
94 const int8x16_t vbABx0123456789ABCDEF = vld1q_s8(w); w = (const int8_t*) w + 16;
95 const int8x16_t vbCDx0123456789ABCDEF = vld1q_s8(w); w = (const int8_t*) w + 16;
96 const int8x16_t vbEFx0123456789ABCDEF = vld1q_s8(w); w = (const int8_t*) w + 16;
97 const int8x16_t vb01x01234567 = vshlq_n_s8(vb01x0123456789ABCDEF, 4);
98 const int8x16_t vb23x01234567 = vshlq_n_s8(vb23x0123456789ABCDEF, 4);
99 const int8x16_t vb45x01234567 = vshlq_n_s8(vb45x0123456789ABCDEF, 4);
100 const int8x16_t vb67x01234567 = vshlq_n_s8(vb67x0123456789ABCDEF, 4);
101 const int8x16_t vb89x01234567 = vshlq_n_s8(vb89x0123456789ABCDEF, 4);
102 const int8x16_t vbABx01234567 = vshlq_n_s8(vbABx0123456789ABCDEF, 4);
103 const int8x16_t vbCDx01234567 = vshlq_n_s8(vbCDx0123456789ABCDEF, 4);
104 const int8x16_t vbEFx01234567 = vshlq_n_s8(vbEFx0123456789ABCDEF, 4);
105 const int8x16_t vb01x89ABCDEF = vandq_s8(vb01x0123456789ABCDEF, vmask);
106 const int8x16_t vb23x89ABCDEF = vandq_s8(vb23x0123456789ABCDEF, vmask);
107 const int8x16_t vb45x89ABCDEF = vandq_s8(vb45x0123456789ABCDEF, vmask);
108 const int8x16_t vb67x89ABCDEF = vandq_s8(vb67x0123456789ABCDEF, vmask);
109 const int8x16_t vb89x89ABCDEF = vandq_s8(vb89x0123456789ABCDEF, vmask);
110 const int8x16_t vbABx89ABCDEF = vandq_s8(vbABx0123456789ABCDEF, vmask);
111 const int8x16_t vbCDx89ABCDEF = vandq_s8(vbCDx0123456789ABCDEF, vmask);
112 const int8x16_t vbEFx89ABCDEF = vandq_s8(vbEFx0123456789ABCDEF, vmask);
113
114 // Multiply-accumulate: 1x8 * 8x16 --> 1x16.
115 vacc01x01 = vmmlaq_s32(vacc01x01, va01x01234567, vb01x01234567);
116 vacc01x23 = vmmlaq_s32(vacc01x23, va01x01234567, vb23x01234567);
117 vacc01x45 = vmmlaq_s32(vacc01x45, va01x01234567, vb45x01234567);
118 vacc01x67 = vmmlaq_s32(vacc01x67, va01x01234567, vb67x01234567);
119 vacc01x89 = vmmlaq_s32(vacc01x89, va01x01234567, vb89x01234567);
120 vacc01xAB = vmmlaq_s32(vacc01xAB, va01x01234567, vbABx01234567);
121 vacc01xCD = vmmlaq_s32(vacc01xCD, va01x01234567, vbCDx01234567);
122 vacc01xEF = vmmlaq_s32(vacc01xEF, va01x01234567, vbEFx01234567);
123 vacc01x01 = vmmlaq_s32(vacc01x01, va01x89ABCDEF, vb01x89ABCDEF);
124 vacc01x23 = vmmlaq_s32(vacc01x23, va01x89ABCDEF, vb23x89ABCDEF);
125 vacc01x45 = vmmlaq_s32(vacc01x45, va01x89ABCDEF, vb45x89ABCDEF);
126 vacc01x67 = vmmlaq_s32(vacc01x67, va01x89ABCDEF, vb67x89ABCDEF);
127 vacc01x89 = vmmlaq_s32(vacc01x89, va01x89ABCDEF, vb89x89ABCDEF);
128 vacc01xAB = vmmlaq_s32(vacc01xAB, va01x89ABCDEF, vbABx89ABCDEF);
129 vacc01xCD = vmmlaq_s32(vacc01xCD, va01x89ABCDEF, vbCDx89ABCDEF);
130 vacc01xEF = vmmlaq_s32(vacc01xEF, va01x89ABCDEF, vbEFx89ABCDEF);
131
132 k -= 16 * sizeof(int8_t);
133 }
134 // Handle up to 8 final positions of `k`
135 if XNN_UNLIKELY(k != 0) {
136 // Load a 1x8 block of activations.
137 uint64x2_t va01x01234567 = vld1q_dup_u64((const void*) a0); a0 += 8;
138
139 // Load a 16x16 block of weights.
140 const int8x16_t vb01x0123456789ABCDEF = vld1q_s8(w); w = (const int8_t*) w + 16;
141 const int8x16_t vb23x0123456789ABCDEF = vld1q_s8(w); w = (const int8_t*) w + 16;
142 const int8x16_t vb45x0123456789ABCDEF = vld1q_s8(w); w = (const int8_t*) w + 16;
143 const int8x16_t vb67x0123456789ABCDEF = vld1q_s8(w); w = (const int8_t*) w + 16;
144 const int8x16_t vb89x0123456789ABCDEF = vld1q_s8(w); w = (const int8_t*) w + 16;
145 const int8x16_t vbABx0123456789ABCDEF = vld1q_s8(w); w = (const int8_t*) w + 16;
146 const int8x16_t vbCDx0123456789ABCDEF = vld1q_s8(w); w = (const int8_t*) w + 16;
147 const int8x16_t vbEFx0123456789ABCDEF = vld1q_s8(w); w = (const int8_t*) w + 16;
148 const int8x16_t vb01x01234567 = vshlq_n_s8(vb01x0123456789ABCDEF, 4);
149 const int8x16_t vb23x01234567 = vshlq_n_s8(vb23x0123456789ABCDEF, 4);
150 const int8x16_t vb45x01234567 = vshlq_n_s8(vb45x0123456789ABCDEF, 4);
151 const int8x16_t vb67x01234567 = vshlq_n_s8(vb67x0123456789ABCDEF, 4);
152 const int8x16_t vb89x01234567 = vshlq_n_s8(vb89x0123456789ABCDEF, 4);
153 const int8x16_t vbABx01234567 = vshlq_n_s8(vbABx0123456789ABCDEF, 4);
154 const int8x16_t vbCDx01234567 = vshlq_n_s8(vbCDx0123456789ABCDEF, 4);
155 const int8x16_t vbEFx01234567 = vshlq_n_s8(vbEFx0123456789ABCDEF, 4);
156
157 // Multiply-accumulate: 1x4 * 4x16 --> 1x16.
158 vacc01x01 = vmmlaq_s32(vacc01x01, vreinterpretq_s8_u64(va01x01234567), vb01x01234567);
159 vacc01x23 = vmmlaq_s32(vacc01x23, vreinterpretq_s8_u64(va01x01234567), vb23x01234567);
160 vacc01x45 = vmmlaq_s32(vacc01x45, vreinterpretq_s8_u64(va01x01234567), vb45x01234567);
161 vacc01x67 = vmmlaq_s32(vacc01x67, vreinterpretq_s8_u64(va01x01234567), vb67x01234567);
162 vacc01x89 = vmmlaq_s32(vacc01x89, vreinterpretq_s8_u64(va01x01234567), vb89x01234567);
163 vacc01xAB = vmmlaq_s32(vacc01xAB, vreinterpretq_s8_u64(va01x01234567), vbABx01234567);
164 vacc01xCD = vmmlaq_s32(vacc01xCD, vreinterpretq_s8_u64(va01x01234567), vbCDx01234567);
165 vacc01xEF = vmmlaq_s32(vacc01xEF, vreinterpretq_s8_u64(va01x01234567), vbEFx01234567);
166 }
167
168 int32x4_t vacc0x0123 = vreinterpretq_s32_u64(vtrn1q_u64(vreinterpretq_u64_s32(vacc01x01), vreinterpretq_u64_s32(vacc01x23)));
169 int32x4_t vacc0x4567 = vreinterpretq_s32_u64(vtrn1q_u64(vreinterpretq_u64_s32(vacc01x45), vreinterpretq_u64_s32(vacc01x67)));
170 int32x4_t vacc0x89AB = vreinterpretq_s32_u64(vtrn1q_u64(vreinterpretq_u64_s32(vacc01x89), vreinterpretq_u64_s32(vacc01xAB)));
171 int32x4_t vacc0xCDEF = vreinterpretq_s32_u64(vtrn1q_u64(vreinterpretq_u64_s32(vacc01xCD), vreinterpretq_u64_s32(vacc01xEF)));
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 const float32x4_t vfilter_output_scale89AB = vreinterpretq_f32_u32(vshll_n_u16(vld1_u16(w), 16)); w = (const uint16_t*) w + 4;
175 const float32x4_t vfilter_output_scaleCDEF = vreinterpretq_f32_u32(vshll_n_u16(vld1_u16(w), 16)); w = (const uint16_t*) w + 4;
176 float32x4_t vf0x0123 = vcvtq_f32_s32(vacc0x0123);
177 vout0x0123 = vfmaq_f32(vout0x0123, vf0x0123, vfilter_output_scale0123);
178 float32x4_t vf0x4567 = vcvtq_f32_s32(vacc0x4567);
179 vout0x4567 = vfmaq_f32(vout0x4567, vf0x4567, vfilter_output_scale4567);
180 float32x4_t vf0x89AB = vcvtq_f32_s32(vacc0x89AB);
181 vout0x89AB = vfmaq_f32(vout0x89AB, vf0x89AB, vfilter_output_scale89AB);
182 float32x4_t vf0xCDEF = vcvtq_f32_s32(vacc0xCDEF);
183 vout0xCDEF = vfmaq_f32(vout0xCDEF, vf0xCDEF, vfilter_output_scaleCDEF);
184 }
185
186 const float32x4_t vinput_scale0 = vld1q_dup_f32(&quantization_params[0].inv_scale);
187 vout0x0123 = vmulq_f32(vout0x0123, vinput_scale0);
188 vout0x4567 = vmulq_f32(vout0x4567, vinput_scale0);
189 vout0x89AB = vmulq_f32(vout0x89AB, vinput_scale0);
190 vout0xCDEF = vmulq_f32(vout0xCDEF, vinput_scale0);
191
192
193 const float32x4_t vbias0123 = vld1q_f32(w); w = (const float*) w + 4;
194 vout0x0123 = vaddq_f32(vbias0123, vout0x0123);
195 const float32x4_t vbias4567 = vld1q_f32(w); w = (const float*) w + 4;
196 vout0x4567 = vaddq_f32(vbias4567, vout0x4567);
197 const float32x4_t vbias89AB = vld1q_f32(w); w = (const float*) w + 4;
198 vout0x89AB = vaddq_f32(vbias89AB, vout0x89AB);
199 const float32x4_t vbiasCDEF = vld1q_f32(w); w = (const float*) w + 4;
200 vout0xCDEF = vaddq_f32(vbiasCDEF, vout0xCDEF);
201
202 const float32x4_t voutput_min = vld1q_dup_f32(&params->scalar.min);
203 vout0x0123 = vmaxq_f32(vout0x0123, voutput_min);
204 vout0x4567 = vmaxq_f32(vout0x4567, voutput_min);
205 vout0x89AB = vmaxq_f32(vout0x89AB, voutput_min);
206 vout0xCDEF = vmaxq_f32(vout0xCDEF, voutput_min);
207
208 const float32x4_t voutput_max = vld1q_dup_f32(&params->scalar.max);
209 vout0x0123 = vminq_f32(vout0x0123, voutput_max);
210 vout0x4567 = vminq_f32(vout0x4567, voutput_max);
211 vout0x89AB = vminq_f32(vout0x89AB, voutput_max);
212 vout0xCDEF = vminq_f32(vout0xCDEF, voutput_max);
213
214 if XNN_LIKELY(nc >= 16) {
215 vst1q_f32(c0, vout0x0123);
216 vst1q_f32(c0 + 4, vout0x4567);
217 vst1q_f32(c0 + 8, vout0x89AB);
218 vst1q_f32(c0 + 12, vout0xCDEF);
219
220 a0 = (const int8_t*) ((uintptr_t) a0 - kc);
221
222 c0 = (float*) ((uintptr_t) c0 + cn_stride);
223
224 nc -= 16;
225 } else {
226 if (nc & 8) {
227 vst1q_f32(c0, vout0x0123); c0 += 4;
228 vout0x0123 = vout0x89AB;
229 vst1q_f32(c0, vout0x4567); c0 += 4;
230 vout0x4567 = vout0xCDEF;
231 }
232 if (nc & 4) {
233 vst1q_f32(c0, vout0x0123); c0 += 4;
234 vout0x0123 = vout0x4567;
235 }
236 float32x2_t vout0x01 = vget_low_f32(vout0x0123);
237 if (nc & 2) {
238 vst1_f32(c0, vout0x01); c0 += 2;
239 vout0x01 = vget_high_f32(vout0x0123);
240 }
241 if (nc & 1) {
242 vst1_lane_f32(c0, vout0x01, 0);
243 }
244 nc = 0;
245 }
246 } while (nc != 0);
247 }