Debian Codegraph @

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