Debian Codegraph @

src/qd8-f32-qc8w-gemm/gen/qd8-f32-qc8w-gemm-3x8c4-minmax-avxvnni-u2-acc2.c

raw
1 // Auto-generated file. Do not edit!
2 // Template: src/qs8-gemm/MRx8c4-avxvnni.c.in
3 // Generator: tools/xngen
4 //
5 // Copyright 2023 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 #include <stdint.h>
12
13 #include <immintrin.h>
14
15 #include "xnnpack/common.h"
16 #include "xnnpack/gemm.h"
17 #include "xnnpack/intrinsics-polyfill.h"
18 #include "xnnpack/math.h"
19 #include "xnnpack/unaligned.h"
20 #include "xnnpack/prefetch.h"
21
22
23 void xnn_qd8_f32_qc8w_gemm_minmax_ukernel_3x8c4__avxvnni_u2_acc2(
24 size_t mr,
25 size_t nc,
26 size_t kc,
27 const int8_t* restrict a,
28 size_t a_stride,
29 const void* restrict w,
30 float* restrict c,
31 size_t cm_stride,
32 size_t cn_stride,
33 const union xnn_f32_minmax_params params[restrict XNN_MIN_ELEMENTS(1)],
34 const struct xnn_qd8_quantization_params quantization_params[restrict XNN_MIN_ELEMENTS(1)]) XNN_OOB_READS
35 {
36 assert(mr != 0);
37 assert(mr <= 3);
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, 4 * sizeof(int8_t));
46 const int8_t* a0 = a;
47 float* c0 = c;
48 const int8_t* a1 = (const int8_t*) ((uintptr_t) a0 + a_stride);
49 float* c1 = (float*) ((uintptr_t) c0 + cm_stride);
50 if XNN_UNPREDICTABLE(mr < 2) {
51 a1 = a0;
52 c1 = c0;
53 }
54 const int8_t* a2 = (const int8_t*) ((uintptr_t) a1 + a_stride);
55 float* c2 = (float*) ((uintptr_t) c1 + cm_stride);
56 if XNN_UNPREDICTABLE(mr <= 2) {
57 a2 = a1;
58 c2 = c1;
59 }
60
61 const __m256i vsign_mask =_mm256_set1_epi8(0x80);
62 XNN_FORCE_REALIZATION(vsign_mask);
63 const __m256i vinput_zero_point0 = _mm256_set1_epi32((int) quantization_params[0].zero_point + 128);
64 const __m256i vinput_zero_point1 = _mm256_set1_epi32((int) quantization_params[1].zero_point + 128);
65 const __m256i vinput_zero_point2 = _mm256_set1_epi32((int) quantization_params[2].zero_point + 128);
66 const __m256 voutput_min = _mm256_set1_ps(params->scalar.min);
67 const __m256 voutput_max = _mm256_set1_ps(params->scalar.max);
68 // XNN_FORCE_REALIZATION(voutput_min);
69 // XNN_FORCE_REALIZATION(voutput_max);
70 do {
71 const __m256i vksum01234567 = _mm256_load_si256(w);
72 __m256i vacc0x0x01234567 = _mm256_mullo_epi32(vksum01234567, vinput_zero_point0);
73 __m256i vacc0x1x01234567 = _mm256_setzero_si256();
74 __m256i vacc1x0x01234567 = _mm256_mullo_epi32(vksum01234567, vinput_zero_point1);
75 __m256i vacc1x1x01234567 = _mm256_setzero_si256();
76 __m256i vacc2x0x01234567 = _mm256_mullo_epi32(vksum01234567, vinput_zero_point2);
77 __m256i vacc2x1x01234567 = _mm256_setzero_si256();
78 w = (const int32_t*) w + 8;
79
80 size_t k = kc;
81 while (k >= 8 * sizeof(int8_t)) {
82 __m256i va0x0x0123 = _mm256_set1_epi32((int) unaligned_load_u32(a0 + 0));
83 __m256i va0x1x0123 = _mm256_set1_epi32((int) unaligned_load_u32(a0 + 4));
84 a0 += 8;
85 __m256i va1x0x0123 = _mm256_set1_epi32((int) unaligned_load_u32(a1 + 0));
86 __m256i va1x1x0123 = _mm256_set1_epi32((int) unaligned_load_u32(a1 + 4));
87 a1 += 8;
88 __m256i va2x0x0123 = _mm256_set1_epi32((int) unaligned_load_u32(a2 + 0));
89 __m256i va2x1x0123 = _mm256_set1_epi32((int) unaligned_load_u32(a2 + 4));
90 a2 += 8;
91
92 va0x0x0123 = _mm256_xor_si256(va0x0x0123, vsign_mask);
93 va0x1x0123 = _mm256_xor_si256(va0x1x0123, vsign_mask);
94 va1x0x0123 = _mm256_xor_si256(va1x0x0123, vsign_mask);
95 va1x1x0123 = _mm256_xor_si256(va1x1x0123, vsign_mask);
96 va2x0x0123 = _mm256_xor_si256(va2x0x0123, vsign_mask);
97 va2x1x0123 = _mm256_xor_si256(va2x1x0123, vsign_mask);
98
99 const __m256i vb0x01234567 = _mm256_load_si256((const __m256i*) ((const int8_t*) w + 0));
100 const __m256i vb1x01234567 = _mm256_load_si256((const __m256i*) ((const int8_t*) w + 32));
101 w = (const int8_t*) w + 64;
102
103 vacc0x0x01234567 = _mm256_dpbusd_avx_epi32(vacc0x0x01234567, va0x0x0123, vb0x01234567);
104 vacc0x1x01234567 = _mm256_dpbusd_avx_epi32(vacc0x1x01234567, va0x1x0123, vb1x01234567);
105 vacc1x0x01234567 = _mm256_dpbusd_avx_epi32(vacc1x0x01234567, va1x0x0123, vb0x01234567);
106 vacc1x1x01234567 = _mm256_dpbusd_avx_epi32(vacc1x1x01234567, va1x1x0123, vb1x01234567);
107 vacc2x0x01234567 = _mm256_dpbusd_avx_epi32(vacc2x0x01234567, va2x0x0123, vb0x01234567);
108 vacc2x1x01234567 = _mm256_dpbusd_avx_epi32(vacc2x1x01234567, va2x1x0123, vb1x01234567);
109
110 k -= 8 * sizeof(int8_t);
111 }
112 vacc0x0x01234567 = _mm256_add_epi32(vacc0x0x01234567, vacc0x1x01234567);
113 vacc1x0x01234567 = _mm256_add_epi32(vacc1x0x01234567, vacc1x1x01234567);
114 vacc2x0x01234567 = _mm256_add_epi32(vacc2x0x01234567, vacc2x1x01234567);
115 while (k != 0) {
116 __m256i va0x0123 = _mm256_set1_epi32((int) unaligned_load_u32(a0));
117 a0 += 4;
118 __m256i va1x0123 = _mm256_set1_epi32((int) unaligned_load_u32(a1));
119 a1 += 4;
120 __m256i va2x0123 = _mm256_set1_epi32((int) unaligned_load_u32(a2));
121 a2 += 4;
122
123 va0x0123 = _mm256_xor_si256(va0x0123, vsign_mask);
124 va1x0123 = _mm256_xor_si256(va1x0123, vsign_mask);
125 va2x0123 = _mm256_xor_si256(va2x0123, vsign_mask);
126
127 const __m256i vb01234567 = _mm256_load_si256(w);
128
129 vacc0x0x01234567 = _mm256_dpbusd_avx_epi32(vacc0x0x01234567, va0x0123, vb01234567);
130 vacc1x0x01234567 = _mm256_dpbusd_avx_epi32(vacc1x0x01234567, va1x0123, vb01234567);
131 vacc2x0x01234567 = _mm256_dpbusd_avx_epi32(vacc2x0x01234567, va2x0123, vb01234567);
132
133 w = (const int8_t*) w + 32;
134 k -= 4 * sizeof(int8_t);
135 }
136
137 __m256 vout0x01234567 = _mm256_cvtepi32_ps(vacc0x0x01234567);
138 __m256 vout1x01234567 = _mm256_cvtepi32_ps(vacc1x0x01234567);
139 __m256 vout2x01234567 = _mm256_cvtepi32_ps(vacc2x0x01234567);
140
141 vout0x01234567 = _mm256_mul_ps(vout0x01234567, _mm256_set1_ps(quantization_params[0].inv_scale));
142 vout1x01234567 = _mm256_mul_ps(vout1x01234567, _mm256_set1_ps(quantization_params[1].inv_scale));
143 vout2x01234567 = _mm256_mul_ps(vout2x01234567, _mm256_set1_ps(quantization_params[2].inv_scale));
144
145 const __m256 vfilter_output_scale01234567 = _mm256_load_ps((const float*) w);
146 const __m256 vbias01234567 = _mm256_load_ps((const float*) w + 8);
147 w = (const float*) w + 16;
148
149 vout0x01234567 = _mm256_fmadd_ps(vout0x01234567, vfilter_output_scale01234567, vbias01234567);
150 vout1x01234567 = _mm256_fmadd_ps(vout1x01234567, vfilter_output_scale01234567, vbias01234567);
151 vout2x01234567 = _mm256_fmadd_ps(vout2x01234567, vfilter_output_scale01234567, vbias01234567);
152
153 vout0x01234567 = _mm256_max_ps(vout0x01234567, voutput_min);
154 vout1x01234567 = _mm256_max_ps(vout1x01234567, voutput_min);
155 vout2x01234567 = _mm256_max_ps(vout2x01234567, voutput_min);
156
157 vout0x01234567 = _mm256_min_ps(vout0x01234567, voutput_max);
158 vout1x01234567 = _mm256_min_ps(vout1x01234567, voutput_max);
159 vout2x01234567 = _mm256_min_ps(vout2x01234567, voutput_max);
160
161 if XNN_LIKELY(nc >= 8) {
162 _mm256_storeu_ps(c0, vout0x01234567);
163 a0 = (const int8_t*) ((uintptr_t) a0 - kc);
164 c0 = (float*) ((uintptr_t) c0 + cn_stride);
165 _mm256_storeu_ps(c1, vout1x01234567);
166 a1 = (const int8_t*) ((uintptr_t) a1 - kc);
167 c1 = (float*) ((uintptr_t) c1 + cn_stride);
168 _mm256_storeu_ps(c2, vout2x01234567);
169 a2 = (const int8_t*) ((uintptr_t) a2 - kc);
170 c2 = (float*) ((uintptr_t) c2 + cn_stride);
171 nc -= 8;
172 } else {
173 __m128 vout0x0123 = _mm256_castps256_ps128(vout0x01234567);
174 __m128 vout1x0123 = _mm256_castps256_ps128(vout1x01234567);
175 __m128 vout2x0123 = _mm256_castps256_ps128(vout2x01234567);
176 if (nc & 4) {
177 _mm_storeu_ps(c0, vout0x0123);
178 c0 += 4;
179 _mm_storeu_ps(c1, vout1x0123);
180 c1 += 4;
181 _mm_storeu_ps(c2, vout2x0123);
182 c2 += 4;
183 vout0x0123 = _mm256_extractf128_ps(vout0x01234567, 1);
184 vout1x0123 = _mm256_extractf128_ps(vout1x01234567, 1);
185 vout2x0123 = _mm256_extractf128_ps(vout2x01234567, 1);
186 }
187 if (nc & 2) {
188 _mm_storel_pi((__m64*) c0, vout0x0123);
189 c0 += 2;
190 _mm_storel_pi((__m64*) c1, vout1x0123);
191 c1 += 2;
192 _mm_storel_pi((__m64*) c2, vout2x0123);
193 c2 += 2;
194 vout0x0123 = _mm_movehl_ps(vout0x0123, vout0x0123);
195 vout1x0123 = _mm_movehl_ps(vout1x0123, vout1x0123);
196 vout2x0123 = _mm_movehl_ps(vout2x0123, vout2x0123);
197 }
198 if (nc & 1) {
199 _mm_store_ss(c0, vout0x0123);
200 _mm_store_ss(c1, vout1x0123);
201 _mm_store_ss(c2, vout2x0123);
202 }
203 nc = 0;
204 }
205 } while (nc != 0);
206 }