Debian Codegraph @

src/qd8-f16-qc8w-igemm/gen/qd8-f16-qc8w-igemm-2x8c8-minmax-avxvnni-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_qd8_f16_qc8w_igemm_minmax_ukernel_2x8c8__avxvnni_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 xnn_float16* restrict c,
30 size_t cm_stride,
31 size_t cn_stride,
32 size_t a_offset,
33 const int8_t* zero,
34 const int8_t* zero_data,
35 const union xnn_f16_minmax_params params[restrict XNN_MIN_ELEMENTS(1)],
36 const struct xnn_qd8_quantization_params quantization_params[restrict XNN_MIN_ELEMENTS(1)]) XNN_OOB_READS
37 {
38 assert(mr != 0);
39 assert(mr <= 2);
40 assert(nc != 0);
41 assert(kc != 0);
42 assert(kc % sizeof(int8_t) == 0);
43 assert(a != NULL);
44 assert(w != NULL);
45 assert(c != NULL);
46
47 kc = round_up_po2(kc, 8 * sizeof(int8_t));
48 uint16_t* c0 = (uint16_t*) c;
49 uint16_t* c1 = (uint16_t*) ((uintptr_t) c0 + cm_stride);
50 if XNN_UNPREDICTABLE(mr != 2) {
51 c1 = c0;
52 }
53
54 const __m256i vsign_mask = _mm256_set1_epi8(0x80);
55 XNN_FORCE_REALIZATION(vsign_mask);
56 const __m256i vinput_zero_point = _mm256_set1_epi32((int) quantization_params->zero_point + 128);
57 const __m256 vinput_inv_scale = _mm256_set1_ps(quantization_params->inv_scale);
58 const __m256 voutput_min = _mm256_cvtph_ps(_mm_set1_epi16(*(const uint16_t*) &params->scalar.min));
59 const __m256 voutput_max = _mm256_cvtph_ps(_mm_set1_epi16(*(const uint16_t*) &params->scalar.max));
60 // XNN_FORCE_REALIZATION(vinput_zero_point);
61 // XNN_FORCE_REALIZATION(vinput_inv_scale);
62 // XNN_FORCE_REALIZATION(voutput_min);
63 // XNN_FORCE_REALIZATION(voutput_max);
64 do {
65 const __m256i vksum01234567 = _mm256_load_si256(w);
66 const __m256i vsum0x01234567 = _mm256_mullo_epi32(vksum01234567, vinput_zero_point);
67 __m256i vacc0x0123 = _mm256_cvtepu32_epi64(_mm256_extracti128_si256(vsum0x01234567, 0));
68 __m256i vacc0x4567 = _mm256_cvtepu32_epi64(_mm256_extracti128_si256(vsum0x01234567, 1));
69 __m256i vacc1x0123 = vacc0x0123;
70 __m256i vacc1x4567 = vacc0x4567;
71 __m256i vacc1x0x0123 = _mm256_setzero_si256();
72 __m256i vacc1x0x4567 = _mm256_setzero_si256();
73 __m256i vacc1x1x0123 = _mm256_setzero_si256();
74 __m256i vacc1x1x4567 = _mm256_setzero_si256();
75 w = (const int32_t*) w + 8;
76
77 size_t p = ks;
78 do {
79 const int8_t* restrict a0 = a[0];
80 if XNN_UNPREDICTABLE(a0 != zero) {
81 a0 = (const int8_t*) ((uintptr_t) a0 + a_offset);
82 } else {
83 a0 = zero_data;
84 }
85 const int8_t* restrict a1 = a[1];
86 if XNN_UNPREDICTABLE(a1 != zero) {
87 a1 = (const int8_t*) ((uintptr_t) a1 + a_offset);
88 } else {
89 a1 = zero_data;
90 }
91 a += 2;
92
93 size_t k = kc;
94 while (k >= 16 * sizeof(int8_t)) {
95 const __m256i va0x01234567 = _mm256_xor_si256(_mm256_set1_epi64x((int64_t) unaligned_load_u64(a0)), vsign_mask);
96 const __m256i va0x89ABCDEF = _mm256_xor_si256(_mm256_set1_epi64x((int64_t) unaligned_load_u64(a0 + 8)), vsign_mask);
97 a0 += 16;
98 const __m256i va1x01234567 = _mm256_xor_si256(_mm256_set1_epi64x((int64_t) unaligned_load_u64(a1)), vsign_mask);
99 const __m256i va1x89ABCDEF = _mm256_xor_si256(_mm256_set1_epi64x((int64_t) unaligned_load_u64(a1 + 8)), vsign_mask);
100 a1 += 16;
101
102 const __m256i vb01234567x0123 = _mm256_load_si256(w);
103 const __m256i vb89ABCDEFx0123 = _mm256_load_si256((const __m256i*) ((const int8_t*) w + 32));
104 const __m256i vb01234567x4567 = _mm256_load_si256((const __m256i*) ((const int8_t*) w + 64));
105 const __m256i vb89ABCDEFx4567 = _mm256_load_si256((const __m256i*) ((const int8_t*) w + 96));
106 xnn_prefetch_to_l1((const int8_t*) w + 896);
107
108 vacc0x0123 = _mm256_dpbusd_avx_epi32(vacc0x0123, va0x01234567, vb01234567x0123);
109 vacc0x4567 = _mm256_dpbusd_avx_epi32(vacc0x4567, va0x01234567, vb89ABCDEFx0123);
110 vacc1x0123 = _mm256_dpbusd_avx_epi32(vacc1x0123, va1x01234567, vb01234567x0123);
111 vacc1x4567 = _mm256_dpbusd_avx_epi32(vacc1x4567, va1x01234567, vb89ABCDEFx0123);
112 xnn_prefetch_to_l1((const int8_t*) w + 960);
113 vacc1x0x0123 = _mm256_dpbusd_avx_epi32(vacc1x0x0123, va0x89ABCDEF, vb01234567x4567);
114 vacc1x0x4567 = _mm256_dpbusd_avx_epi32(vacc1x0x4567, va0x89ABCDEF, vb89ABCDEFx4567);
115 vacc1x1x0123 = _mm256_dpbusd_avx_epi32(vacc1x1x0123, va1x89ABCDEF, vb01234567x4567);
116 vacc1x1x4567 = _mm256_dpbusd_avx_epi32(vacc1x1x4567, va1x89ABCDEF, vb89ABCDEFx4567);
117
118 w = (const int8_t*) w + 128;
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
128 const __m256i vb01234567x0123 = _mm256_load_si256(w);
129 const __m256i vb89ABCDEFx0123 = _mm256_load_si256((const __m256i*) ((const int8_t*) w + 32));
130
131 vacc0x0123 = _mm256_dpbusd_avx_epi32(vacc0x0123, va0x01234567, vb01234567x0123);
132 vacc0x4567 = _mm256_dpbusd_avx_epi32(vacc0x4567, va0x01234567, vb89ABCDEFx0123);
133 vacc1x0123 = _mm256_dpbusd_avx_epi32(vacc1x0123, va1x01234567, vb01234567x0123);
134 vacc1x4567 = _mm256_dpbusd_avx_epi32(vacc1x4567, va1x01234567, vb89ABCDEFx0123);
135 xnn_prefetch_to_l1((const int8_t*) w + 960);
136
137 w = (const int8_t*) w + 64;
138 k -= 8 * sizeof(int8_t);
139 }
140
141 p -= 2 * sizeof(void*);
142 } while (p != 0);
143
144 vacc0x0123 = _mm256_add_epi32(vacc0x0123, vacc1x0x0123);
145 vacc0x4567 = _mm256_add_epi32(vacc0x4567, vacc1x0x4567);
146 vacc1x0123 = _mm256_add_epi32(vacc1x0123, vacc1x1x0123);
147 vacc1x4567 = _mm256_add_epi32(vacc1x4567, vacc1x1x4567);
148
149 // Add adjacent pairs
150 const __m256i vsum0x02134657 = _mm256_hadd_epi32(vacc0x0123, vacc0x4567);
151 __m256i vacc0x01234567 = _mm256_permute4x64_epi64(vsum0x02134657, _MM_SHUFFLE(3, 1, 2, 0));
152 const __m256i vsum1x02134657 = _mm256_hadd_epi32(vacc1x0123, vacc1x4567);
153 __m256i vacc1x01234567 = _mm256_permute4x64_epi64(vsum1x02134657, _MM_SHUFFLE(3, 1, 2, 0));
154
155 __m256 vout0x01234567 = _mm256_cvtepi32_ps(vacc0x01234567);
156 __m256 vout1x01234567 = _mm256_cvtepi32_ps(vacc1x01234567);
157
158 vout0x01234567 = _mm256_mul_ps(vout0x01234567, vinput_inv_scale);
159 vout1x01234567 = _mm256_mul_ps(vout1x01234567, vinput_inv_scale);
160
161 const __m256 vfilter_output_scale01234567 = _mm256_load_ps((const float*) w);
162 const __m256 vbias01234567 = _mm256_load_ps((const float*) w + 8);
163 w = (const float*) w + 16;
164 vout0x01234567 = _mm256_fmadd_ps(vout0x01234567, vfilter_output_scale01234567, vbias01234567);
165 vout1x01234567 = _mm256_fmadd_ps(vout1x01234567, vfilter_output_scale01234567, vbias01234567);
166
167 vout0x01234567 = _mm256_max_ps(vout0x01234567, voutput_min);
168 vout1x01234567 = _mm256_max_ps(vout1x01234567, voutput_min);
169
170 vout0x01234567 = _mm256_min_ps(vout0x01234567, voutput_max);
171 vout1x01234567 = _mm256_min_ps(vout1x01234567, voutput_max);
172
173 __m128i vfp16out1x01234567 = _mm256_cvtps_ph(vout1x01234567, _MM_FROUND_TO_NEAREST_INT);
174 __m128i vfp16out0x01234567 = _mm256_cvtps_ph(vout0x01234567, _MM_FROUND_TO_NEAREST_INT);
175 if XNN_LIKELY(nc >= 8) {
176 _mm_storeu_si128((__m128i*) c1, vfp16out1x01234567);
177 c1 = (uint16_t*) ((uintptr_t) c1 + cn_stride);
178 _mm_storeu_si128((__m128i*) c0, vfp16out0x01234567);
179 c0 = (uint16_t*) ((uintptr_t) c0 + cn_stride);
180
181 a = (const int8_t**restrict) ((uintptr_t) a - ks);
182 nc -= 8;
183 } else {
184 if (nc & 4) {
185 _mm_storel_epi64((__m128i*) c1, vfp16out1x01234567);
186 c1 += 4;
187 vfp16out1x01234567 = _mm_unpackhi_epi64(vfp16out1x01234567, vfp16out1x01234567);
188 _mm_storel_epi64((__m128i*) c0, vfp16out0x01234567);
189 c0 += 4;
190 vfp16out0x01234567 = _mm_unpackhi_epi64(vfp16out0x01234567, vfp16out0x01234567);
191 }
192 if (nc & 2) {
193 _mm_storeu_si32(c1, vfp16out1x01234567);
194 vfp16out1x01234567 = _mm_srli_epi64(vfp16out1x01234567, 32);
195 c1 += 2;
196 _mm_storeu_si32(c0, vfp16out0x01234567);
197 vfp16out0x01234567 = _mm_srli_epi64(vfp16out0x01234567, 32);
198 c0 += 2;
199 }
200 if (nc & 1) {
201 *c1 = (uint16_t) _mm_extract_epi16(vfp16out1x01234567, 0);
202 *c0 = (uint16_t) _mm_extract_epi16(vfp16out0x01234567, 0);
203 }
204 nc = 0;
205 }
206 } while (nc != 0);
207 }