Debian Codegraph @

src/qd8-f16-qc4w-gemm/gen/qd8-f16-qc4w-gemm-3x8c8-minmax-avx2-madd-prfm.c

raw
1 // Auto-generated file. Do not edit!
2 // Template: src/qs8-gemm/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/gemm.h"
15 #include "xnnpack/intrinsics-polyfill.h"
16 #include "xnnpack/math.h"
17 #include "xnnpack/unaligned.h"
18 #include "xnnpack/prefetch.h"
19
20
21 void xnn_qd8_f16_qc4w_gemm_minmax_ukernel_3x8c8__avx2_madd_prfm(
22 size_t mr,
23 size_t nc,
24 size_t kc,
25 const int8_t* restrict a,
26 size_t a_stride,
27 const void* restrict w,
28 xnn_float16* restrict c,
29 size_t cm_stride,
30 size_t cn_stride,
31 const struct xnn_f16_qc4w_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 <= 3);
36 assert(nc != 0);
37 assert(kc != 0);
38 assert(kc % sizeof(int8_t) == 0);
39 assert(a != NULL);
40 assert(w != NULL);
41 assert(c != NULL);
42
43 kc = round_up_po2(kc, 8 * sizeof(int8_t));
44 const int8_t* a0 = a;
45 uint16_t* c0 = (uint16_t*) c;
46 const int8_t* a1 = (const int8_t*) ((uintptr_t) a0 + a_stride);
47 uint16_t* c1 = (uint16_t*) ((uintptr_t) c0 + cm_stride);
48 if XNN_UNPREDICTABLE(mr < 2) {
49 a1 = a0;
50 c1 = c0;
51 }
52 const int8_t* a2 = (const int8_t*) ((uintptr_t) a1 + a_stride);
53 uint16_t* c2 = (uint16_t*) ((uintptr_t) c1 + cm_stride);
54 if XNN_UNPREDICTABLE(mr <= 2) {
55 a2 = a1;
56 c2 = c1;
57 }
58
59 const __m256i vsign_mask = _mm256_set1_epi8(0x80);
60 XNN_FORCE_REALIZATION(vsign_mask);
61 const __m256i vinput_zero_point0 = _mm256_set1_epi32((int) quantization_params[0].zero_point + 128);
62 const __m256i vinput_zero_point1 = _mm256_set1_epi32((int) quantization_params[1].zero_point + 128);
63 const __m256i vinput_zero_point2 = _mm256_set1_epi32((int) quantization_params[2].zero_point + 128);
64 const __m256 voutput_min = _mm256_cvtph_ps(_mm_set1_epi16(*(const uint16_t*) &params->scalar.min));
65 const __m256 voutput_max = _mm256_cvtph_ps(_mm_set1_epi16(*(const uint16_t*) &params->scalar.max));
66 // XNN_FORCE_REALIZATION(voutput_min);
67 // XNN_FORCE_REALIZATION(voutput_max);
68 const __m256i vmask = _mm256_set1_epi8(0x0F);
69 XNN_FORCE_REALIZATION(vmask);
70 do {
71 const __m256i vksum01234567 = _mm256_load_si256(w);
72 __m256i vsum0x01234567 = _mm256_mullo_epi32(vksum01234567, vinput_zero_point0);
73 __m256i vacc0x0123 = _mm256_cvtepu32_epi64(_mm256_extracti128_si256(vsum0x01234567, 0));
74 __m256i vacc0x4567 = _mm256_cvtepu32_epi64(_mm256_extracti128_si256(vsum0x01234567, 1));
75 __m256i vsum1x01234567 = _mm256_mullo_epi32(vksum01234567, vinput_zero_point1);
76 __m256i vacc1x0123 = _mm256_cvtepu32_epi64(_mm256_extracti128_si256(vsum1x01234567, 0));
77 __m256i vacc1x4567 = _mm256_cvtepu32_epi64(_mm256_extracti128_si256(vsum1x01234567, 1));
78 __m256i vsum2x01234567 = _mm256_mullo_epi32(vksum01234567, vinput_zero_point2);
79 __m256i vacc2x0123 = _mm256_cvtepu32_epi64(_mm256_extracti128_si256(vsum2x01234567, 0));
80 __m256i vacc2x4567 = _mm256_cvtepu32_epi64(_mm256_extracti128_si256(vsum2x01234567, 1));
81 w = (const int32_t*) w + 8;
82
83 size_t k = kc;
84 while (k >= 16 * sizeof(int8_t)) {
85 const __m256i va0x01234567 = _mm256_xor_si256(_mm256_set1_epi64x((int64_t) unaligned_load_u64(a0)), vsign_mask);
86 const __m256i va0x89ABCDEF = _mm256_xor_si256(_mm256_set1_epi64x((int64_t) unaligned_load_u64(a0 + 8)), vsign_mask);
87 a0 += 16;
88 const __m256i va1x01234567 = _mm256_xor_si256(_mm256_set1_epi64x((int64_t) unaligned_load_u64(a1)), vsign_mask);
89 const __m256i va1x89ABCDEF = _mm256_xor_si256(_mm256_set1_epi64x((int64_t) unaligned_load_u64(a1 + 8)), vsign_mask);
90 a1 += 16;
91 const __m256i va2x01234567 = _mm256_xor_si256(_mm256_set1_epi64x((int64_t) unaligned_load_u64(a2)), vsign_mask);
92 const __m256i va2x89ABCDEF = _mm256_xor_si256(_mm256_set1_epi64x((int64_t) unaligned_load_u64(a2 + 8)), vsign_mask);
93 a2 += 16;
94
95 const __m256i vbb01234567x01234567 = _mm256_load_si256(w);
96 const __m256i vbb89ABCDEFx01234567 = _mm256_load_si256((const __m256i*) ((const int8_t*) w + 32));
97 const __m256i vbs01234567x0123 = _mm256_srli_epi32(vbb01234567x01234567, 4);
98 const __m256i vbs89ABCDEFx0123 = _mm256_srli_epi32(vbb89ABCDEFx01234567, 4);
99 const __m256i vb01234567x0123 = _mm256_and_si256(vbb01234567x01234567, vmask);
100 const __m256i vb89ABCDEFx0123 = _mm256_and_si256(vbb89ABCDEFx01234567, vmask);
101 const __m256i vb01234567x4567 = _mm256_and_si256(vbs01234567x0123, vmask);
102 const __m256i vb89ABCDEFx4567 = _mm256_and_si256(vbs89ABCDEFx0123, vmask);
103
104 vacc0x0123 = _mm256_dpbusd_epi32_madd(vacc0x0123, va0x01234567, vb01234567x0123);
105 vacc0x4567 = _mm256_dpbusd_epi32_madd(vacc0x4567, va0x01234567, vb89ABCDEFx0123);
106 vacc1x0123 = _mm256_dpbusd_epi32_madd(vacc1x0123, va1x01234567, vb01234567x0123);
107 vacc1x4567 = _mm256_dpbusd_epi32_madd(vacc1x4567, va1x01234567, vb89ABCDEFx0123);
108 vacc2x0123 = _mm256_dpbusd_epi32_madd(vacc2x0123, va2x01234567, vb01234567x0123);
109 vacc2x4567 = _mm256_dpbusd_epi32_madd(vacc2x4567, va2x01234567, vb89ABCDEFx0123);
110 xnn_prefetch_to_l1((const int8_t*) w + 960);
111 vacc0x0123 = _mm256_dpbusd_epi32_madd(vacc0x0123, va0x89ABCDEF, vb01234567x4567);
112 vacc0x4567 = _mm256_dpbusd_epi32_madd(vacc0x4567, va0x89ABCDEF, vb89ABCDEFx4567);
113 vacc1x0123 = _mm256_dpbusd_epi32_madd(vacc1x0123, va1x89ABCDEF, vb01234567x4567);
114 vacc1x4567 = _mm256_dpbusd_epi32_madd(vacc1x4567, va1x89ABCDEF, vb89ABCDEFx4567);
115 vacc2x0123 = _mm256_dpbusd_epi32_madd(vacc2x0123, va2x89ABCDEF, vb01234567x4567);
116 vacc2x4567 = _mm256_dpbusd_epi32_madd(vacc2x4567, va2x89ABCDEF, vb89ABCDEFx4567);
117
118 w = (const int8_t*) w + 64;
119 k -= 16 * sizeof(int8_t);
120 }
121
122 if (k != 0) {
123 const __m256i va0x01234567 = _mm256_xor_si256(_mm256_set1_epi64x((int64_t) unaligned_load_u64(a0)), vsign_mask);
124 a0 += 8;
125 const __m256i va1x01234567 = _mm256_xor_si256(_mm256_set1_epi64x((int64_t) unaligned_load_u64(a1)), vsign_mask);
126 a1 += 8;
127 const __m256i va2x01234567 = _mm256_xor_si256(_mm256_set1_epi64x((int64_t) unaligned_load_u64(a2)), vsign_mask);
128 a2 += 8;
129
130 const __m256i vbb01234567x01234567 = _mm256_load_si256(w);
131 const __m256i vbb89ABCDEFx01234567 = _mm256_load_si256((const __m256i*) ((const int8_t*) w + 32));
132 const __m256i vb01234567x0123 = _mm256_and_si256(vbb01234567x01234567, vmask);
133 const __m256i vb89ABCDEFx0123 = _mm256_and_si256(vbb89ABCDEFx01234567, vmask);
134
135 vacc0x0123 = _mm256_dpbusd_epi32_madd(vacc0x0123, va0x01234567, vb01234567x0123);
136 vacc0x4567 = _mm256_dpbusd_epi32_madd(vacc0x4567, va0x01234567, vb89ABCDEFx0123);
137 vacc1x0123 = _mm256_dpbusd_epi32_madd(vacc1x0123, va1x01234567, vb01234567x0123);
138 vacc1x4567 = _mm256_dpbusd_epi32_madd(vacc1x4567, va1x01234567, vb89ABCDEFx0123);
139 vacc2x0123 = _mm256_dpbusd_epi32_madd(vacc2x0123, va2x01234567, vb01234567x0123);
140 vacc2x4567 = _mm256_dpbusd_epi32_madd(vacc2x4567, va2x01234567, vb89ABCDEFx0123);
141 xnn_prefetch_to_l1((const int8_t*) w + 960);
142
143 w = (const int8_t*) w + 64;
144 k -= 8 * sizeof(int8_t);
145 }
146
147 // Add adjacent pairs
148 const __m256i vsum0x02134657 = _mm256_hadd_epi32(vacc0x0123, vacc0x4567);
149 __m256i vacc0x01234567 = _mm256_permute4x64_epi64(vsum0x02134657, _MM_SHUFFLE(3, 1, 2, 0));
150 const __m256i vsum1x02134657 = _mm256_hadd_epi32(vacc1x0123, vacc1x4567);
151 __m256i vacc1x01234567 = _mm256_permute4x64_epi64(vsum1x02134657, _MM_SHUFFLE(3, 1, 2, 0));
152 const __m256i vsum2x02134657 = _mm256_hadd_epi32(vacc2x0123, vacc2x4567);
153 __m256i vacc2x01234567 = _mm256_permute4x64_epi64(vsum2x02134657, _MM_SHUFFLE(3, 1, 2, 0));
154
155 __m256 vout0x01234567 = _mm256_cvtepi32_ps(vacc0x01234567);
156 __m256 vout1x01234567 = _mm256_cvtepi32_ps(vacc1x01234567);
157 __m256 vout2x01234567 = _mm256_cvtepi32_ps(vacc2x01234567);
158
159 vout0x01234567 = _mm256_mul_ps(vout0x01234567, _mm256_set1_ps(quantization_params[0].inv_scale));
160 vout1x01234567 = _mm256_mul_ps(vout1x01234567, _mm256_set1_ps(quantization_params[1].inv_scale));
161 vout2x01234567 = _mm256_mul_ps(vout2x01234567, _mm256_set1_ps(quantization_params[2].inv_scale));
162
163 const __m256 vfilter_output_scale01234567 = _mm256_load_ps((const float*) w);
164 const __m256 vbias01234567 = _mm256_load_ps((const float*) w + 8);
165 w = (const float*) w + 16;
166
167 vout0x01234567 = _mm256_fmadd_ps(vout0x01234567, vfilter_output_scale01234567, vbias01234567);
168 vout1x01234567 = _mm256_fmadd_ps(vout1x01234567, vfilter_output_scale01234567, vbias01234567);
169 vout2x01234567 = _mm256_fmadd_ps(vout2x01234567, vfilter_output_scale01234567, vbias01234567);
170
171 vout0x01234567 = _mm256_max_ps(vout0x01234567, voutput_min);
172 vout1x01234567 = _mm256_max_ps(vout1x01234567, voutput_min);
173 vout2x01234567 = _mm256_max_ps(vout2x01234567, voutput_min);
174
175 vout0x01234567 = _mm256_min_ps(vout0x01234567, voutput_max);
176 vout1x01234567 = _mm256_min_ps(vout1x01234567, voutput_max);
177 vout2x01234567 = _mm256_min_ps(vout2x01234567, voutput_max);
178
179 __m128i vfp16out0x01234567 = _mm256_cvtps_ph(vout0x01234567, _MM_FROUND_TO_NEAREST_INT);
180 __m128i vfp16out1x01234567 = _mm256_cvtps_ph(vout1x01234567, _MM_FROUND_TO_NEAREST_INT);
181 __m128i vfp16out2x01234567 = _mm256_cvtps_ph(vout2x01234567, _MM_FROUND_TO_NEAREST_INT);
182 if XNN_LIKELY(nc >= 8) {
183 _mm_storeu_si128((__m128i*) c0, vfp16out0x01234567);
184 a0 = (const int8_t*) ((uintptr_t) a0 - kc);
185 c0 = (uint16_t*) ((uintptr_t) c0 + cn_stride);
186 _mm_storeu_si128((__m128i*) c1, vfp16out1x01234567);
187 a1 = (const int8_t*) ((uintptr_t) a1 - kc);
188 c1 = (uint16_t*) ((uintptr_t) c1 + cn_stride);
189 _mm_storeu_si128((__m128i*) c2, vfp16out2x01234567);
190 a2 = (const int8_t*) ((uintptr_t) a2 - kc);
191 c2 = (uint16_t*) ((uintptr_t) c2 + cn_stride);
192 nc -= 8;
193 } else {
194 if (nc & 4) {
195 _mm_storel_epi64((__m128i*) c0, vfp16out0x01234567);
196 c0 += 4;
197 vfp16out0x01234567 = _mm_unpackhi_epi64(vfp16out0x01234567, vfp16out0x01234567);
198 _mm_storel_epi64((__m128i*) c1, vfp16out1x01234567);
199 c1 += 4;
200 vfp16out1x01234567 = _mm_unpackhi_epi64(vfp16out1x01234567, vfp16out1x01234567);
201 _mm_storel_epi64((__m128i*) c2, vfp16out2x01234567);
202 c2 += 4;
203 vfp16out2x01234567 = _mm_unpackhi_epi64(vfp16out2x01234567, vfp16out2x01234567);
204 }
205 if (nc & 2) {
206 _mm_storeu_si32(c0, vfp16out0x01234567);
207 c0 += 2;
208 vfp16out0x01234567 = _mm_srli_epi64(vfp16out0x01234567, 32);
209 _mm_storeu_si32(c1, vfp16out1x01234567);
210 c1 += 2;
211 vfp16out1x01234567 = _mm_srli_epi64(vfp16out1x01234567, 32);
212 _mm_storeu_si32(c2, vfp16out2x01234567);
213 c2 += 2;
214 vfp16out2x01234567 = _mm_srli_epi64(vfp16out2x01234567, 32);
215 }
216 if (nc & 1) {
217 *c0 = (uint16_t) _mm_extract_epi16(vfp16out0x01234567, 0);
218 *c1 = (uint16_t) _mm_extract_epi16(vfp16out1x01234567, 0);
219 *c2 = (uint16_t) _mm_extract_epi16(vfp16out2x01234567, 0);
220 }
221 nc = 0;
222 }
223 } while (nc != 0);
224 }