Debian Codegraph @

src/qs8-qc8w-igemm/gen/qs8-qc8w-igemm-1x8c8-minmax-fp32-avx256vnni-prfm.c

raw
1 // Auto-generated file. Do not edit!
2 // Template: src/qs8-igemm/MRx8c8-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 #include <assert.h>
11
12 #include <immintrin.h>
13
14 #include "xnnpack/common.h"
15 #include "xnnpack/gemm.h"
16 #include "xnnpack/intrinsics-polyfill.h"
17 #include "xnnpack/math.h"
18 #include "xnnpack/unaligned.h"
19 #include "xnnpack/prefetch.h"
20
21
22 void xnn_qs8_qc8w_igemm_minmax_fp32_ukernel_1x8c8__avx256vnni_prfm(
23 size_t mr,
24 size_t nc,
25 size_t kc,
26 size_t ks,
27 const int8_t** restrict a,
28 const void* restrict w,
29 int8_t* restrict c,
30 size_t cm_stride,
31 size_t cn_stride,
32 size_t a_offset,
33 const int8_t* zero,
34 const union xnn_qs8_qc8w_conv_minmax_params params[restrict XNN_MIN_ELEMENTS(1)]) XNN_OOB_READS
35 {
36 assert(mr != 0);
37 assert(mr <= 1);
38 assert(nc != 0);
39 assert(kc != 0);
40 assert(kc % sizeof(int8_t) == 0);
41 assert(a != NULL);
42 assert(w != NULL);
43 assert(c != NULL);
44
45 kc = round_up_po2(kc, 8 * sizeof(int8_t));
46 int8_t* c0 = c;
47
48 const __m256i vsign_mask = _mm256_set1_epi8(0x80);
49 XNN_FORCE_REALIZATION(vsign_mask);
50 const __m256 voutput_max_less_zero_point = _mm256_set1_ps((int32_t) params->fp32_scalar.output_max - (int32_t) params->fp32_scalar.output_zero_point);
51 const __m256i voutput_zero_point = _mm256_set1_epi32(params->fp32_scalar.output_zero_point);
52 const __m128i voutput_min = _mm_set1_epi8(params->fp32_scalar.output_min);
53 // XNN_FORCE_REALIZATION(voutput_max_less_zero_point);
54 // XNN_FORCE_REALIZATION(voutput_zero_point);
55 // XNN_FORCE_REALIZATION(voutput_min);
56 do {
57 __m256i vacc0x0123 = _mm256_cvtepu32_epi64(_mm_load_si128((const __m128i*) w));
58 __m256i vacc0x4567 = _mm256_cvtepu32_epi64(_mm_load_si128((const __m128i*) ((const int32_t*) w + 4)));
59 __m256i vacc1x0x0123 = _mm256_setzero_si256();
60 __m256i vacc1x0x4567 = _mm256_setzero_si256();
61 w = (const int32_t*) w + 8;
62
63 size_t p = ks;
64 do {
65 const int8_t* restrict a0 = a[0];
66 if XNN_UNPREDICTABLE(a0 != zero) {
67 a0 = (const int8_t*) ((uintptr_t) a0 + a_offset);
68 }
69 a += 1;
70
71 size_t k = kc;
72 while (k >= 16 * sizeof(int8_t)) {
73 const __m256i va0x01234567 = _mm256_xor_si256(_mm256_set1_epi64x((int64_t) unaligned_load_u64(a0)), vsign_mask);
74 const __m256i va0x89ABCDEF = _mm256_xor_si256(_mm256_set1_epi64x((int64_t) unaligned_load_u64(a0 + 8)), vsign_mask);
75 a0 += 16;
76
77 const __m256i vb01234567x0123 = _mm256_load_si256(w);
78 const __m256i vb89ABCDEFx0123 = _mm256_load_si256((const __m256i*) ((const int8_t*) w + 32));
79 const __m256i vb01234567x4567 = _mm256_load_si256((const __m256i*) ((const int8_t*) w + 64));
80 const __m256i vb89ABCDEFx4567 = _mm256_load_si256((const __m256i*) ((const int8_t*) w + 96));
81 xnn_prefetch_to_l1((const int8_t*) w + 896);
82
83 vacc0x0123 = _mm256_dpbusd_epi32(vacc0x0123, va0x01234567, vb01234567x0123);
84 vacc0x4567 = _mm256_dpbusd_epi32(vacc0x4567, va0x01234567, vb89ABCDEFx0123);
85 xnn_prefetch_to_l1((const int8_t*) w + 960);
86 vacc1x0x0123 = _mm256_dpbusd_epi32(vacc1x0x0123, va0x89ABCDEF, vb01234567x4567);
87 vacc1x0x4567 = _mm256_dpbusd_epi32(vacc1x0x4567, va0x89ABCDEF, vb89ABCDEFx4567);
88
89 w = (const int8_t*) w + 128;
90 k -= 16 * sizeof(int8_t);
91 }
92
93 if (k != 0) {
94 const __m256i va0x01234567 = _mm256_xor_si256(_mm256_set1_epi64x((int64_t) unaligned_load_u64(a0)), vsign_mask);
95 a0 += 8;
96
97 const __m256i vb01234567x0123 = _mm256_load_si256(w);
98 const __m256i vb89ABCDEFx0123 = _mm256_load_si256((const __m256i*) ((const int8_t*) w + 32));
99
100 vacc0x0123 = _mm256_dpbusd_epi32(vacc0x0123, va0x01234567, vb01234567x0123);
101 vacc0x4567 = _mm256_dpbusd_epi32(vacc0x4567, va0x01234567, vb89ABCDEFx0123);
102 xnn_prefetch_to_l1((const int8_t*) w + 960);
103
104 w = (const int8_t*) w + 64;
105 k -= 8 * sizeof(int8_t);
106 }
107
108 p -= 1 * sizeof(void*);
109 } while (p != 0);
110
111 vacc0x0123 = _mm256_add_epi32(vacc0x0123, vacc1x0x0123);
112 vacc0x4567 = _mm256_add_epi32(vacc0x4567, vacc1x0x4567);
113
114 // Add adjacent pairs
115 const __m256i vsum0x02134657 = _mm256_hadd_epi32(vacc0x0123, vacc0x4567);
116 __m256i vacc0x01234567 = _mm256_permute4x64_epi64(vsum0x02134657, _MM_SHUFFLE(3, 1, 2, 0));
117
118 __m256 vout0x01234567 = _mm256_cvtepi32_ps(vacc0x01234567);
119
120 const __m256 vscale01234567 = _mm256_load_ps(w);
121 w = (const float*) w + 8;
122 vout0x01234567 = _mm256_mul_ps(vout0x01234567, vscale01234567);
123
124 vout0x01234567 = _mm256_min_ps(vout0x01234567, voutput_max_less_zero_point);
125
126 vacc0x01234567 = _mm256_cvtps_epi32(vout0x01234567);
127
128 vacc0x01234567 = _mm256_add_epi32(vacc0x01234567, voutput_zero_point);
129
130 __m128i voutb0x01234567 = _mm256_cvtsepi32_epi8(vacc0x01234567);
131
132 voutb0x01234567 = _mm_max_epi8(voutb0x01234567, voutput_min);
133
134 if (nc >= 8) {
135 _mm_storel_epi64((__m128i*) c0, voutb0x01234567);
136 c0 = (int8_t*) ((uintptr_t) c0 + cn_stride);
137 a = (const int8_t**restrict) ((uintptr_t) a - ks);
138 nc -= 8;
139 } else {
140 // Prepare mask for valid 8-bit elements (depends on nc).
141 const __mmask16 vmask = _cvtu32_mask16((UINT32_C(1) << nc) - UINT32_C(1));
142 _mm_mask_storeu_epi8(c0, vmask, voutb0x01234567);
143 nc = 0;
144 }
145 } while (nc != 0);
146 }