Debian Codegraph @

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