Debian Codegraph @

src/x8-packw/gen/x8-packw-x8c8-gemm-goi-avx2-prfm.c

raw
1 // Auto-generated file. Do not edit!
2 // Template: src/x8-packw/kr-avxvnni.c.in
3 // Generator: tools/xngen
4 //
5 // Copyright 2024 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
11 #include <assert.h>
12 #include <stddef.h>
13 #include <stdint.h>
14
15 #include <immintrin.h>
16
17 #include "xnnpack/packw.h"
18 #include "xnnpack/unaligned.h"
19 #include "xnnpack/prefetch.h"
20
21
22 void xnn_x8_packw_gemm_goi_ukernel_x8c8__avx2_prfm(
23 size_t g,
24 size_t nc,
25 size_t kc,
26 size_t nr,
27 size_t kr,
28 size_t sr,
29 const int8_t* weights,
30 const uint32_t* bias,
31 const void* scale,
32 int8_t* packed_weights,
33 size_t extra_bytes,
34 const void* params) XNN_OOB_READS
35 {
36 assert(g != 0);
37 assert(nc != 0);
38 assert(kc != 0);
39 assert(nr == 8);
40 assert(kr == 8);
41 assert(sr == 1);
42 assert(weights != NULL);
43 assert(packed_weights != NULL);
44
45 int8_t* out = (int8_t*) packed_weights;
46 const uint32_t* b = (const uint32_t*) bias;
47
48
49 do {
50 // NC main loop multiple of 8
51 const int8_t* w0 = (const int8_t*) weights;
52 size_t n = nc;
53 for (;n >= 8; n -= 8) {
54 if XNN_LIKELY(b != NULL) {
55 const __m256i vb0 = _mm256_loadu_si256((const __m256i*) (b + 0));
56 _mm256_storeu_si256((__m256i*) (out + 0), vb0);
57 b += 8;
58 } else {
59 _mm256_storeu_si256((__m256i*) (out + 0), _mm256_setzero_si256());
60 }
61 out += 8 * sizeof(uint32_t);
62
63 const int8_t* w1 = w0 + kc;
64 const int8_t* w2 = w1 + kc;
65 const int8_t* w3 = w2 + kc;
66 const int8_t* w4 = w3 + kc;
67 const int8_t* w5 = w4 + kc;
68 const int8_t* w6 = w5 + kc;
69 const int8_t* w7 = w6 + kc;
70 xnn_prefetch_to_l1((const int8_t*) w0 + 0);
71 xnn_prefetch_to_l1((const int8_t*) w0 + 64);
72 xnn_prefetch_to_l1((const int8_t*) w0 + 128);
73 xnn_prefetch_to_l1((const int8_t*) w0 + 192);
74 xnn_prefetch_to_l1((const int8_t*) w0 + 256);
75 xnn_prefetch_to_l1((const int8_t*) w0 + 320);
76 xnn_prefetch_to_l1((const int8_t*) w0 + 384);
77 xnn_prefetch_to_l1((const int8_t*) w1 + 0);
78 xnn_prefetch_to_l1((const int8_t*) w1 + 64);
79 xnn_prefetch_to_l1((const int8_t*) w1 + 128);
80 xnn_prefetch_to_l1((const int8_t*) w1 + 192);
81 xnn_prefetch_to_l1((const int8_t*) w1 + 256);
82 xnn_prefetch_to_l1((const int8_t*) w1 + 320);
83 xnn_prefetch_to_l1((const int8_t*) w1 + 384);
84 xnn_prefetch_to_l1((const int8_t*) w2 + 0);
85 xnn_prefetch_to_l1((const int8_t*) w2 + 64);
86 xnn_prefetch_to_l1((const int8_t*) w2 + 128);
87 xnn_prefetch_to_l1((const int8_t*) w2 + 192);
88 xnn_prefetch_to_l1((const int8_t*) w2 + 256);
89 xnn_prefetch_to_l1((const int8_t*) w2 + 320);
90 xnn_prefetch_to_l1((const int8_t*) w2 + 384);
91 xnn_prefetch_to_l1((const int8_t*) w3 + 0);
92 xnn_prefetch_to_l1((const int8_t*) w3 + 64);
93 xnn_prefetch_to_l1((const int8_t*) w3 + 128);
94 xnn_prefetch_to_l1((const int8_t*) w3 + 192);
95 xnn_prefetch_to_l1((const int8_t*) w3 + 256);
96 xnn_prefetch_to_l1((const int8_t*) w3 + 320);
97 xnn_prefetch_to_l1((const int8_t*) w3 + 384);
98 xnn_prefetch_to_l1((const int8_t*) w4 + 0);
99 xnn_prefetch_to_l1((const int8_t*) w4 + 64);
100 xnn_prefetch_to_l1((const int8_t*) w4 + 128);
101 xnn_prefetch_to_l1((const int8_t*) w4 + 192);
102 xnn_prefetch_to_l1((const int8_t*) w4 + 256);
103 xnn_prefetch_to_l1((const int8_t*) w4 + 320);
104 xnn_prefetch_to_l1((const int8_t*) w4 + 384);
105 xnn_prefetch_to_l1((const int8_t*) w5 + 0);
106 xnn_prefetch_to_l1((const int8_t*) w5 + 64);
107 xnn_prefetch_to_l1((const int8_t*) w5 + 128);
108 xnn_prefetch_to_l1((const int8_t*) w5 + 192);
109 xnn_prefetch_to_l1((const int8_t*) w5 + 256);
110 xnn_prefetch_to_l1((const int8_t*) w5 + 320);
111 xnn_prefetch_to_l1((const int8_t*) w5 + 384);
112 xnn_prefetch_to_l1((const int8_t*) w6 + 0);
113 xnn_prefetch_to_l1((const int8_t*) w6 + 64);
114 xnn_prefetch_to_l1((const int8_t*) w6 + 128);
115 xnn_prefetch_to_l1((const int8_t*) w6 + 192);
116 xnn_prefetch_to_l1((const int8_t*) w6 + 256);
117 xnn_prefetch_to_l1((const int8_t*) w6 + 320);
118 xnn_prefetch_to_l1((const int8_t*) w6 + 384);
119 xnn_prefetch_to_l1((const int8_t*) w7 + 0);
120 xnn_prefetch_to_l1((const int8_t*) w7 + 64);
121 xnn_prefetch_to_l1((const int8_t*) w7 + 128);
122 xnn_prefetch_to_l1((const int8_t*) w7 + 192);
123 xnn_prefetch_to_l1((const int8_t*) w7 + 256);
124 xnn_prefetch_to_l1((const int8_t*) w7 + 320);
125 xnn_prefetch_to_l1((const int8_t*) w7 + 384);
126
127
128 size_t k = kc;
129 // KC main loop multiple of 8x32
130 for (; k >= 32; k -= 32) {
131 const __m256i v0_0123 = _mm256_loadu_si256((const __m256i*) w0);
132 const __m256i v1_0123 = _mm256_loadu_si256((const __m256i*) w1);
133 const __m256i v2_0123 = _mm256_loadu_si256((const __m256i*) w2);
134 const __m256i v3_0123 = _mm256_loadu_si256((const __m256i*) w3);
135 const __m256i v4_0123 = _mm256_loadu_si256((const __m256i*) w4);
136 const __m256i v5_0123 = _mm256_loadu_si256((const __m256i*) w5);
137 const __m256i v6_0123 = _mm256_loadu_si256((const __m256i*) w6);
138 const __m256i v7_0123 = _mm256_loadu_si256((const __m256i*) w7);
139
140 const __m256i v01_02 = _mm256_unpacklo_epi64(v0_0123, v1_0123);
141 const __m256i v01_13 = _mm256_unpackhi_epi64(v0_0123, v1_0123);
142 const __m256i v23_02 = _mm256_unpacklo_epi64(v2_0123, v3_0123);
143 const __m256i v23_13 = _mm256_unpackhi_epi64(v2_0123, v3_0123);
144 const __m256i v45_02 = _mm256_unpacklo_epi64(v4_0123, v5_0123);
145 const __m256i v45_13 = _mm256_unpackhi_epi64(v4_0123, v5_0123);
146 const __m256i v67_02 = _mm256_unpacklo_epi64(v6_0123, v7_0123);
147 const __m256i v67_13 = _mm256_unpackhi_epi64(v6_0123, v7_0123);
148
149 xnn_prefetch_to_l1((const int8_t*) w0 + 448);
150 xnn_prefetch_to_l1((const int8_t*) w1 + 448);
151 xnn_prefetch_to_l1((const int8_t*) w2 + 448);
152 xnn_prefetch_to_l1((const int8_t*) w3 + 448);
153 xnn_prefetch_to_l1((const int8_t*) w4 + 448);
154 xnn_prefetch_to_l1((const int8_t*) w5 + 448);
155 xnn_prefetch_to_l1((const int8_t*) w6 + 448);
156 xnn_prefetch_to_l1((const int8_t*) w7 + 448);
157
158 const __m256i v0_0 = _mm256_permute2f128_si256(v01_02, v23_02, _MM_SHUFFLE(0, 2, 0, 0));
159 const __m256i v0_1 = _mm256_permute2f128_si256(v01_13, v23_13, _MM_SHUFFLE(0, 2, 0, 0));
160 const __m256i v0_2 = _mm256_permute2f128_si256(v01_02, v23_02, _MM_SHUFFLE(0, 3, 0, 1));
161 const __m256i v0_3 = _mm256_permute2f128_si256(v01_13, v23_13, _MM_SHUFFLE(0, 3, 0, 1));
162 const __m256i v4_0 = _mm256_permute2f128_si256(v45_02, v67_02, _MM_SHUFFLE(0, 2, 0, 0));
163 const __m256i v4_1 = _mm256_permute2f128_si256(v45_13, v67_13, _MM_SHUFFLE(0, 2, 0, 0));
164 const __m256i v4_2 = _mm256_permute2f128_si256(v45_02, v67_02, _MM_SHUFFLE(0, 3, 0, 1));
165 const __m256i v4_3 = _mm256_permute2f128_si256(v45_13, v67_13, _MM_SHUFFLE(0, 3, 0, 1));
166
167
168 _mm256_storeu_si256((__m256i *)&out[0], v0_0);
169 _mm256_storeu_si256((__m256i *)&out[32], v4_0);
170 _mm256_storeu_si256((__m256i *)&out[64], v0_1);
171 _mm256_storeu_si256((__m256i *)&out[96], v4_1);
172 _mm256_storeu_si256((__m256i *)&out[128], v0_2);
173 _mm256_storeu_si256((__m256i *)&out[160], v4_2);
174 _mm256_storeu_si256((__m256i *)&out[192], v0_3);
175 _mm256_storeu_si256((__m256i *)&out[224], v4_3);
176
177 w0 += 32;
178 w1 += 32;
179 w2 += 32;
180 w3 += 32;
181 w4 += 32;
182 w5 += 32;
183 w6 += 32;
184 w7 += 32;
185 out += 256;
186 }
187
188 // KC main loop multiple of 8x8
189 for (; k >= 8; k -= 8) {
190 __m256i v0 = _mm256_set1_epi64x((int64_t) unaligned_load_u64(w0));
191 v0 = _mm256_blend_epi32(v0, _mm256_set1_epi64x((int64_t) unaligned_load_u64(w1)), 0x0C);
192 v0 = _mm256_blend_epi32(v0, _mm256_set1_epi64x((int64_t) unaligned_load_u64(w2)), 0x30);
193 v0 = _mm256_blend_epi32(v0, _mm256_set1_epi64x((int64_t) unaligned_load_u64(w3)), 0xC0);
194 __m256i v4 = _mm256_set1_epi64x((int64_t) unaligned_load_u64(w4));
195 v4 = _mm256_blend_epi32(v4, _mm256_set1_epi64x((int64_t) unaligned_load_u64(w5)), 0x0C);
196 v4 = _mm256_blend_epi32(v4, _mm256_set1_epi64x((int64_t) unaligned_load_u64(w6)), 0x30);
197 v4 = _mm256_blend_epi32(v4, _mm256_set1_epi64x((int64_t) unaligned_load_u64(w7)), 0xC0);
198 xnn_prefetch_to_l1((const int8_t*) w0 + 448);
199 xnn_prefetch_to_l1((const int8_t*) w1 + 448);
200 xnn_prefetch_to_l1((const int8_t*) w2 + 448);
201 xnn_prefetch_to_l1((const int8_t*) w3 + 448);
202 xnn_prefetch_to_l1((const int8_t*) w4 + 448);
203 xnn_prefetch_to_l1((const int8_t*) w5 + 448);
204 xnn_prefetch_to_l1((const int8_t*) w6 + 448);
205 xnn_prefetch_to_l1((const int8_t*) w7 + 448);
206
207
208 _mm256_storeu_si256((__m256i *)&out[0], v0);
209 _mm256_storeu_si256((__m256i *)&out[32], v4);
210
211 w0 += 8;
212 w1 += 8;
213 w2 += 8;
214 w3 += 8;
215 w4 += 8;
216 w5 += 8;
217 w6 += 8;
218 w7 += 8;
219 out += 64;
220 }
221
222 // KC remainder of 1..7
223 if (k != 0) {
224 assert(k >= 1 && k <= 7);
225
226 const __m256i vmask = _mm256_srli_epi64(_mm256_set1_epi32(-1), (8 - k) * sizeof(int8_t) * 8);
227
228 __m256i v0 = _mm256_set1_epi64x((int64_t) unaligned_load_u64(w0));
229 v0 = _mm256_blend_epi32(v0, _mm256_set1_epi64x((int64_t) unaligned_load_u64(w1)), 0x0C);
230 v0 = _mm256_blend_epi32(v0, _mm256_set1_epi64x((int64_t) unaligned_load_u64(w2)), 0x30);
231 v0 = _mm256_blend_epi32(v0, _mm256_set1_epi64x((int64_t) unaligned_load_u64(w3)), 0xC0);
232 v0 = _mm256_and_si256(v0, vmask);
233 __m256i v4 = _mm256_set1_epi64x((int64_t) unaligned_load_u64(w4));
234 v4 = _mm256_blend_epi32(v4, _mm256_set1_epi64x((int64_t) unaligned_load_u64(w5)), 0x0C);
235 v4 = _mm256_blend_epi32(v4, _mm256_set1_epi64x((int64_t) unaligned_load_u64(w6)), 0x30);
236 v4 = _mm256_blend_epi32(v4, _mm256_set1_epi64x((int64_t) unaligned_load_u64(w7)), 0xC0);
237 v4 = _mm256_and_si256(v4, vmask);
238
239 w0 += k;
240 w1 += k;
241 w2 += k;
242 w3 += k;
243 w4 += k;
244 w5 += k;
245 w6 += k;
246 w7 += k;
247
248
249 _mm256_storeu_si256((__m256i *)&out[0], v0);
250 _mm256_storeu_si256((__m256i *)&out[32], v4);
251
252 out += 64;
253 }
254
255 out = (int8_t*) ((uintptr_t) out + extra_bytes);
256 w0 = w7;
257 }
258
259 // NC remainder (1..7)
260 if XNN_UNLIKELY(n != 0) {
261 assert(n >= 1 && n <= 7);
262
263 if XNN_LIKELY(b != NULL) {
264 size_t nb = n;
265 do {
266 *((uint32_t*) out) = *b++;
267 out += sizeof(uint32_t);
268 } while (--nb != 0);
269 } else {
270 size_t nb = n;
271 do {
272 *((uint32_t*) out) = 0;
273 out += sizeof(uint32_t);
274 } while (--nb != 0);
275 }
276 out += (8 - n) * sizeof(uint32_t);
277
278 const int8_t* w1 = w0 + kc;
279 if XNN_UNPREDICTABLE(n < 2) {
280 w1 = w0;
281 }
282 const int8_t* w2 = w1 + kc;
283 if XNN_UNPREDICTABLE(n <= 2) {
284 w2 = w1;
285 }
286 const int8_t* w3 = w2 + kc;
287 if XNN_UNPREDICTABLE(n < 4) {
288 w3 = w2;
289 }
290 const int8_t* w4 = w3 + kc;
291 if XNN_UNPREDICTABLE(n <= 4) {
292 w4 = w3;
293 }
294 const int8_t* w5 = w4 + kc;
295 if XNN_UNPREDICTABLE(n < 6) {
296 w5 = w4;
297 }
298 const int8_t* w6 = w5 + kc;
299 if XNN_UNPREDICTABLE(n <= 6) {
300 w6 = w5;
301 }
302 const int8_t* w7 = w6 + kc;
303 if XNN_UNPREDICTABLE(n < 8) {
304 w7 = w6;
305 }
306 xnn_prefetch_to_l1((const int8_t*) w0);
307 xnn_prefetch_to_l1((const int8_t*) w0 + 64);
308 xnn_prefetch_to_l1((const int8_t*) w1);
309 xnn_prefetch_to_l1((const int8_t*) w1 + 64);
310 xnn_prefetch_to_l1((const int8_t*) w2);
311 xnn_prefetch_to_l1((const int8_t*) w2 + 64);
312 xnn_prefetch_to_l1((const int8_t*) w3);
313 xnn_prefetch_to_l1((const int8_t*) w3 + 64);
314 xnn_prefetch_to_l1((const int8_t*) w4);
315 xnn_prefetch_to_l1((const int8_t*) w4 + 64);
316 xnn_prefetch_to_l1((const int8_t*) w5);
317 xnn_prefetch_to_l1((const int8_t*) w5 + 64);
318 xnn_prefetch_to_l1((const int8_t*) w6);
319 xnn_prefetch_to_l1((const int8_t*) w6 + 64);
320 xnn_prefetch_to_l1((const int8_t*) w7);
321 xnn_prefetch_to_l1((const int8_t*) w7 + 64);
322
323
324 // KC main loop multiple of 8x8
325 size_t k = kc;
326 for (; k >= 8; k -= 8) {
327 __m256i v0 = _mm256_set1_epi64x((int64_t) unaligned_load_u64(w0));
328 v0 = _mm256_blend_epi32(v0, _mm256_set1_epi64x((int64_t) unaligned_load_u64(w1)), 0x0C);
329 v0 = _mm256_blend_epi32(v0, _mm256_set1_epi64x((int64_t) unaligned_load_u64(w2)), 0x30);
330 v0 = _mm256_blend_epi32(v0, _mm256_set1_epi64x((int64_t) unaligned_load_u64(w3)), 0xC0);
331 __m256i v4 = _mm256_set1_epi64x((int64_t) unaligned_load_u64(w4));
332 v4 = _mm256_blend_epi32(v4, _mm256_set1_epi64x((int64_t) unaligned_load_u64(w5)), 0x0C);
333 v4 = _mm256_blend_epi32(v4, _mm256_set1_epi64x((int64_t) unaligned_load_u64(w6)), 0x30);
334 v4 = _mm256_blend_epi32(v4, _mm256_set1_epi64x((int64_t) unaligned_load_u64(w7)), 0xC0);
335 xnn_prefetch_to_l1((const int8_t*) w0 + 448);
336 xnn_prefetch_to_l1((const int8_t*) w1 + 448);
337 xnn_prefetch_to_l1((const int8_t*) w2 + 448);
338 xnn_prefetch_to_l1((const int8_t*) w3 + 448);
339 xnn_prefetch_to_l1((const int8_t*) w4 + 448);
340 xnn_prefetch_to_l1((const int8_t*) w5 + 448);
341 xnn_prefetch_to_l1((const int8_t*) w6 + 448);
342 xnn_prefetch_to_l1((const int8_t*) w7 + 448);
343
344
345 _mm256_storeu_si256((__m256i *)&out[0], v0);
346 _mm256_storeu_si256((__m256i *)&out[32], v4);
347
348 w0 += 8;
349 w1 += 8;
350 w2 += 8;
351 w3 += 8;
352 w4 += 8;
353 w5 += 8;
354 w6 += 8;
355 w7 += 8;
356 out += 64;
357 }
358
359 // KC remainder of 1..7
360 if (k != 0) {
361 assert(k >= 1 && k <= 7);
362
363 const __m256i vmask = _mm256_srli_epi64(_mm256_set1_epi32(-1), (8 - k) * sizeof(int8_t) * 8);
364
365 __m256i v0 = _mm256_set1_epi64x((int64_t) unaligned_load_u64(w0));
366 v0 = _mm256_blend_epi32(v0, _mm256_set1_epi64x((int64_t) unaligned_load_u64(w1)), 0x0C);
367 v0 = _mm256_blend_epi32(v0, _mm256_set1_epi64x((int64_t) unaligned_load_u64(w2)), 0x30);
368 v0 = _mm256_blend_epi32(v0, _mm256_set1_epi64x((int64_t) unaligned_load_u64(w3)), 0xC0);
369 v0 = _mm256_and_si256(v0, vmask);
370 __m256i v4 = _mm256_set1_epi64x((int64_t) unaligned_load_u64(w4));
371 v4 = _mm256_blend_epi32(v4, _mm256_set1_epi64x((int64_t) unaligned_load_u64(w5)), 0x0C);
372 v4 = _mm256_blend_epi32(v4, _mm256_set1_epi64x((int64_t) unaligned_load_u64(w6)), 0x30);
373 v4 = _mm256_blend_epi32(v4, _mm256_set1_epi64x((int64_t) unaligned_load_u64(w7)), 0xC0);
374 v4 = _mm256_and_si256(v4, vmask);
375
376 w0 += k;
377 w1 += k;
378 w2 += k;
379 w3 += k;
380 w4 += k;
381 w5 += k;
382 w6 += k;
383 w7 += k;
384
385
386 _mm256_storeu_si256((__m256i *)&out[0], v0);
387 _mm256_storeu_si256((__m256i *)&out[32], v4);
388
389 out += 64;
390 }
391
392 out = (int8_t*) ((uintptr_t) out + extra_bytes);
393 }
394
395 weights += nc * kc;
396 } while (--g != 0);
397 }