Debian Codegraph @

src/qd8-f32-qc8w-gemm/gen/qd8-f32-qc8w-gemm-2x8c4-minmax-avxvnni-u4-acc4.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_2x8c4__avxvnni_u4_acc4(
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 <= 2);
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
55 const __m256i vsign_mask =_mm256_set1_epi8(0x80);
56 XNN_FORCE_REALIZATION(vsign_mask);
57 const __m256i vinput_zero_point0 = _mm256_set1_epi32((int) quantization_params[0].zero_point + 128);
58 const __m256i vinput_zero_point1 = _mm256_set1_epi32((int) quantization_params[1].zero_point + 128);
59 const __m256 voutput_min = _mm256_set1_ps(params->scalar.min);
60 const __m256 voutput_max = _mm256_set1_ps(params->scalar.max);
61 // XNN_FORCE_REALIZATION(voutput_min);
62 // XNN_FORCE_REALIZATION(voutput_max);
63 do {
64 const __m256i vksum01234567 = _mm256_load_si256(w);
65 __m256i vacc0x0x01234567 = _mm256_mullo_epi32(vksum01234567, vinput_zero_point0);
66 __m256i vacc0x1x01234567 = _mm256_setzero_si256();
67 __m256i vacc0x2x01234567 = _mm256_setzero_si256();
68 __m256i vacc0x3x01234567 = _mm256_setzero_si256();
69 __m256i vacc1x0x01234567 = _mm256_mullo_epi32(vksum01234567, vinput_zero_point1);
70 __m256i vacc1x1x01234567 = _mm256_setzero_si256();
71 __m256i vacc1x2x01234567 = _mm256_setzero_si256();
72 __m256i vacc1x3x01234567 = _mm256_setzero_si256();
73 w = (const int32_t*) w + 8;
74
75 size_t k = kc;
76 while (k >= 16 * sizeof(int8_t)) {
77 __m256i va0x0x0123 = _mm256_set1_epi32((int) unaligned_load_u32(a0 + 0));
78 __m256i va0x1x0123 = _mm256_set1_epi32((int) unaligned_load_u32(a0 + 4));
79 __m256i va0x2x0123 = _mm256_set1_epi32((int) unaligned_load_u32(a0 + 8));
80 __m256i va0x3x0123 = _mm256_set1_epi32((int) unaligned_load_u32(a0 + 12));
81 a0 += 16;
82 __m256i va1x0x0123 = _mm256_set1_epi32((int) unaligned_load_u32(a1 + 0));
83 __m256i va1x1x0123 = _mm256_set1_epi32((int) unaligned_load_u32(a1 + 4));
84 __m256i va1x2x0123 = _mm256_set1_epi32((int) unaligned_load_u32(a1 + 8));
85 __m256i va1x3x0123 = _mm256_set1_epi32((int) unaligned_load_u32(a1 + 12));
86 a1 += 16;
87
88 va0x0x0123 = _mm256_xor_si256(va0x0x0123, vsign_mask);
89 va0x1x0123 = _mm256_xor_si256(va0x1x0123, vsign_mask);
90 va0x2x0123 = _mm256_xor_si256(va0x2x0123, vsign_mask);
91 va0x3x0123 = _mm256_xor_si256(va0x3x0123, vsign_mask);
92 va1x0x0123 = _mm256_xor_si256(va1x0x0123, vsign_mask);
93 va1x1x0123 = _mm256_xor_si256(va1x1x0123, vsign_mask);
94 va1x2x0123 = _mm256_xor_si256(va1x2x0123, vsign_mask);
95 va1x3x0123 = _mm256_xor_si256(va1x3x0123, vsign_mask);
96
97 const __m256i vb0x01234567 = _mm256_load_si256((const __m256i*) ((const int8_t*) w + 0));
98 const __m256i vb1x01234567 = _mm256_load_si256((const __m256i*) ((const int8_t*) w + 32));
99 const __m256i vb2x01234567 = _mm256_load_si256((const __m256i*) ((const int8_t*) w + 64));
100 const __m256i vb3x01234567 = _mm256_load_si256((const __m256i*) ((const int8_t*) w + 96));
101 w = (const int8_t*) w + 128;
102
103 vacc0x0x01234567 = _mm256_dpbusd_avx_epi32(vacc0x0x01234567, va0x0x0123, vb0x01234567);
104 vacc0x1x01234567 = _mm256_dpbusd_avx_epi32(vacc0x1x01234567, va0x1x0123, vb1x01234567);
105 vacc0x2x01234567 = _mm256_dpbusd_avx_epi32(vacc0x2x01234567, va0x2x0123, vb2x01234567);
106 vacc0x3x01234567 = _mm256_dpbusd_avx_epi32(vacc0x3x01234567, va0x3x0123, vb3x01234567);
107 vacc1x0x01234567 = _mm256_dpbusd_avx_epi32(vacc1x0x01234567, va1x0x0123, vb0x01234567);
108 vacc1x1x01234567 = _mm256_dpbusd_avx_epi32(vacc1x1x01234567, va1x1x0123, vb1x01234567);
109 vacc1x2x01234567 = _mm256_dpbusd_avx_epi32(vacc1x2x01234567, va1x2x0123, vb2x01234567);
110 vacc1x3x01234567 = _mm256_dpbusd_avx_epi32(vacc1x3x01234567, va1x3x0123, vb3x01234567);
111
112 k -= 16 * sizeof(int8_t);
113 }
114 vacc0x0x01234567 = _mm256_add_epi32(vacc0x0x01234567, vacc0x1x01234567);
115 vacc1x0x01234567 = _mm256_add_epi32(vacc1x0x01234567, vacc1x1x01234567);
116 vacc0x2x01234567 = _mm256_add_epi32(vacc0x2x01234567, vacc0x3x01234567);
117 vacc1x2x01234567 = _mm256_add_epi32(vacc1x2x01234567, vacc1x3x01234567);
118 vacc0x0x01234567 = _mm256_add_epi32(vacc0x0x01234567, vacc0x2x01234567);
119 vacc1x0x01234567 = _mm256_add_epi32(vacc1x0x01234567, vacc1x2x01234567);
120 while (k != 0) {
121 __m256i va0x0123 = _mm256_set1_epi32((int) unaligned_load_u32(a0));
122 a0 += 4;
123 __m256i va1x0123 = _mm256_set1_epi32((int) unaligned_load_u32(a1));
124 a1 += 4;
125
126 va0x0123 = _mm256_xor_si256(va0x0123, vsign_mask);
127 va1x0123 = _mm256_xor_si256(va1x0123, vsign_mask);
128
129 const __m256i vb01234567 = _mm256_load_si256(w);
130
131 vacc0x0x01234567 = _mm256_dpbusd_avx_epi32(vacc0x0x01234567, va0x0123, vb01234567);
132 vacc1x0x01234567 = _mm256_dpbusd_avx_epi32(vacc1x0x01234567, va1x0123, vb01234567);
133
134 w = (const int8_t*) w + 32;
135 k -= 4 * sizeof(int8_t);
136 }
137
138 __m256 vout0x01234567 = _mm256_cvtepi32_ps(vacc0x0x01234567);
139 __m256 vout1x01234567 = _mm256_cvtepi32_ps(vacc1x0x01234567);
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
144 const __m256 vfilter_output_scale01234567 = _mm256_load_ps((const float*) w);
145 const __m256 vbias01234567 = _mm256_load_ps((const float*) w + 8);
146 w = (const float*) w + 16;
147
148 vout0x01234567 = _mm256_fmadd_ps(vout0x01234567, vfilter_output_scale01234567, vbias01234567);
149 vout1x01234567 = _mm256_fmadd_ps(vout1x01234567, vfilter_output_scale01234567, vbias01234567);
150
151 vout0x01234567 = _mm256_max_ps(vout0x01234567, voutput_min);
152 vout1x01234567 = _mm256_max_ps(vout1x01234567, voutput_min);
153
154 vout0x01234567 = _mm256_min_ps(vout0x01234567, voutput_max);
155 vout1x01234567 = _mm256_min_ps(vout1x01234567, voutput_max);
156
157 if XNN_LIKELY(nc >= 8) {
158 _mm256_storeu_ps(c0, vout0x01234567);
159 a0 = (const int8_t*) ((uintptr_t) a0 - kc);
160 c0 = (float*) ((uintptr_t) c0 + cn_stride);
161 _mm256_storeu_ps(c1, vout1x01234567);
162 a1 = (const int8_t*) ((uintptr_t) a1 - kc);
163 c1 = (float*) ((uintptr_t) c1 + cn_stride);
164 nc -= 8;
165 } else {
166 __m128 vout0x0123 = _mm256_castps256_ps128(vout0x01234567);
167 __m128 vout1x0123 = _mm256_castps256_ps128(vout1x01234567);
168 if (nc & 4) {
169 _mm_storeu_ps(c0, vout0x0123);
170 c0 += 4;
171 _mm_storeu_ps(c1, vout1x0123);
172 c1 += 4;
173 vout0x0123 = _mm256_extractf128_ps(vout0x01234567, 1);
174 vout1x0123 = _mm256_extractf128_ps(vout1x01234567, 1);
175 }
176 if (nc & 2) {
177 _mm_storel_pi((__m64*) c0, vout0x0123);
178 c0 += 2;
179 _mm_storel_pi((__m64*) c1, vout1x0123);
180 c1 += 2;
181 vout0x0123 = _mm_movehl_ps(vout0x0123, vout0x0123);
182 vout1x0123 = _mm_movehl_ps(vout1x0123, vout1x0123);
183 }
184 if (nc & 1) {
185 _mm_store_ss(c0, vout0x0123);
186 _mm_store_ss(c1, vout1x0123);
187 }
188 nc = 0;
189 }
190 } while (nc != 0);
191 }