Debian Codegraph @

src/qs8-qc8w-igemm/gen/qs8-qc8w-igemm-1x64c4-minmax-fp32-avx512amx.c

raw
1 // Auto-generated file. Do not edit!
2 // Template: src/qs8-igemm/c4-avx512amx.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_qs8_qc8w_igemm_minmax_fp32_ukernel_1x64c4__avx512amx(
21 size_t mr,
22 size_t nc,
23 size_t kc,
24 size_t ks,
25 const int8_t** restrict a,
26 const void* restrict w,
27 int8_t* restrict c,
28 size_t cm_stride,
29 size_t cn_stride,
30 size_t a_offset,
31 const int8_t* zero,
32 const union xnn_qs8_qc8w_conv_minmax_params params[restrict XNN_MIN_ELEMENTS(1)])
33 {
34 assert(mr != 0);
35 assert(mr <= 1);
36 assert(nc != 0);
37 assert(kc != 0);
38 assert(kc % sizeof(int8_t) == 0);
39 assert(a != NULL);
40 assert(w != NULL);
41 assert(c != NULL);
42
43 // TODO: amxintrin.h only provide intrinsics for __x86_64__
44 // Update if amxintrin changes
45 #if defined(__x86_64__)
46 __attribute__((aligned(64))) int32_t vintile[1 * 16];
47 __attribute__((aligned(64))) int32_t res0[1 * 16];
48 __attribute__((aligned(64))) int32_t res1[1 * 16];
49 __attribute__((aligned(64))) int32_t res2[1 * 16];
50 __attribute__((aligned(64))) int32_t res3[1 * 16];
51
52 kc = round_up_po2(kc, 4 * sizeof(int8_t));
53 const size_t kremainder = (kc & 63) ? (kc & 63) : 64;
54 const __mmask16 kremainder_mask = _cvtu32_mask16((UINT32_C(1) << (kremainder >> 2)) - 1);
55
56 // Define tile config data structure
57 struct __tile_config {
58 uint8_t palette_id;
59 uint8_t start_row;
60 uint8_t reserved_0[14];
61 uint16_t colsb[8];
62 uint16_t reserved_1[8];
63 uint8_t rows[8];
64 uint8_t reserved_2[8];
65 };
66
67 // Load tile configuration
68 __attribute__((aligned(64))) struct __tile_config tile_data = {0};
69 tile_data.palette_id = 1;
70 tile_data.rows[0] = mr; // tmm0 = res 0
71 tile_data.rows[1] = mr; // tmm1 = res 1
72 tile_data.rows[2] = mr; // tmm2 = res 2
73 tile_data.rows[3] = mr; // tmm3 = res 3
74 tile_data.rows[4] = mr; // tmm4 = input
75 tile_data.rows[5] = 16; // tmm5 = weights
76 tile_data.rows[6] = mr; // tmm6 = input remainder
77 tile_data.rows[7] = kremainder >> 2; // tmm7 = weights remainder
78
79 tile_data.colsb[0] = 64; // tmm0 = res 0
80 tile_data.colsb[1] = 64; // tmm1 = res 1
81 tile_data.colsb[2] = 64; // tmm2 = res 1
82 tile_data.colsb[3] = 64; // tmm3 = res 1
83 tile_data.colsb[4] = 64; // tmm4 = input
84 tile_data.colsb[5] = 64; // tmm5 = weights
85 tile_data.colsb[6] = kremainder; // tmm6 = input remainder
86 tile_data.colsb[7] = 64; // tmm7 = weights remainder
87
88 //_tile_loadconfig(&tile_data);
89 __asm__ volatile ("ldtilecfg %0" :: "m" (tile_data));
90
91 int8_t* c0 = c;
92
93 const __m512 voutput_max_less_zero_point = _mm512_set1_ps((int32_t) params->fp32_scalar.output_max - (int32_t) params->fp32_scalar.output_zero_point);
94 const __m512i voutput_zero_point = _mm512_set1_epi32(params->fp32_scalar.output_zero_point);
95 const __m128i voutput_min = _mm_set1_epi8(params->fp32_scalar.output_min);
96 // XNN_FORCE_REALIZATION(voutput_max_less_zero_point);
97 // XNN_FORCE_REALIZATION(voutput_zero_point);
98 // XNN_FORCE_REALIZATION(voutput_min);
99
100 do {
101 const __m512i vksum0123456789ABCDEF = _mm512_loadu_epi32((const int32_t*) w + 0);
102 const __m512i vksumGHIJKLMNOPQRSTUV = _mm512_loadu_epi32((const int32_t*) w + 16);
103 const __m512i vksumWXYZabcdefghijkl = _mm512_loadu_epi32((const int32_t*) w + 32);
104 const __m512i vksummnopqrstuvwxyz01 = _mm512_loadu_epi32((const int32_t*) w + 48);
105 w = (const int32_t*) w + 64;
106
107 // Zero tile accumulator
108 __asm__ volatile (
109 "tilezero %%tmm0\n"
110 "tilezero %%tmm1\n"
111 "tilezero %%tmm2\n"
112 "tilezero %%tmm3\n"
113 ::);
114
115 size_t p = ks;
116 do {
117 const int8_t* restrict a0 = a[0];
118 if XNN_UNPREDICTABLE(a0 != zero) {
119 a0 = (const int8_t*) ((uintptr_t) a0 + a_offset);
120 }
121 a += 1;
122
123 size_t k = kc;
124 {
125 while (k >= 64 * sizeof(int8_t)) {
126 _tile_loadd(4, a0, 64); // Directly load input for mr=1
127 a0 += 64;
128 _tile_loadd(5, (const int8_t*) w + 0, 256);
129 _tile_dpbssd(0, 4, 5);
130 _tile_loadd(5, (const int8_t*) w + 64, 256);
131 _tile_dpbssd(1, 4, 5);
132 _tile_loadd(5, (const int8_t*) w + 128, 256);
133 _tile_dpbssd(2, 4, 5);
134 _tile_loadd(5, (const int8_t*) w + 192, 256);
135 _tile_dpbssd(3, 4, 5);
136
137 w = (const int8_t*) w + 4096;
138 k -= 64 * sizeof(int8_t);
139 }
140 }
141
142 if XNN_UNLIKELY(k != 0) {
143 const __m512i vin0 = _mm512_maskz_loadu_epi32(kremainder_mask, a0);
144 a0 += kremainder;
145 _mm512_store_epi32(vintile + 0, vin0);
146 _tile_loadd(6, vintile, 64);
147 _tile_loadd(7, (const int8_t*) w + 0, 256);
148 _tile_dpbssd(0, 6, 7);
149 _tile_loadd(7, (const int8_t*) w + 64, 256);
150 _tile_dpbssd(1, 6, 7);
151 _tile_loadd(7, (const int8_t*) w + 128, 256);
152 _tile_dpbssd(2, 6, 7);
153 _tile_loadd(7, (const int8_t*) w + 192, 256);
154 _tile_dpbssd(3, 6, 7);
155
156 w = (const int8_t*) w + kremainder * 64;
157 k -= kremainder * sizeof(int8_t);
158 }
159
160 p -= 1 * sizeof(void*);
161 } while (p != 0);
162
163 // Add tile to bias
164 _tile_stored(0, res0, 64);
165 _tile_stored(1, res1, 64);
166 _tile_stored(2, res2, 64);
167 _tile_stored(3, res3, 64);
168
169 __m512i vacc0x0123456789ABCDEF = _mm512_add_epi32(vksum0123456789ABCDEF, _mm512_load_epi32(res0 + 0));
170 __m512i vacc0xGHIJKLMNOPQRSTUV = _mm512_add_epi32(vksumGHIJKLMNOPQRSTUV, _mm512_load_epi32(res1 + 0));
171 __m512i vacc0xWXYZabcdefghijkl = _mm512_add_epi32(vksumWXYZabcdefghijkl, _mm512_load_epi32(res2 + 0));
172 __m512i vacc0xmnopqrstuvwxyz01 = _mm512_add_epi32(vksummnopqrstuvwxyz01, _mm512_load_epi32(res3 + 0));
173
174 __m512 vscaled0x0123456789ABCDEF = _mm512_cvtepi32_ps(vacc0x0123456789ABCDEF);
175 __m512 vscaled0xGHIJKLMNOPQRSTUV = _mm512_cvtepi32_ps(vacc0xGHIJKLMNOPQRSTUV);
176 __m512 vscaled0xWXYZabcdefghijkl = _mm512_cvtepi32_ps(vacc0xWXYZabcdefghijkl);
177 __m512 vscaled0xmnopqrstuvwxyz01 = _mm512_cvtepi32_ps(vacc0xmnopqrstuvwxyz01);
178
179 const __m512 vscale0123456789ABCDEF = _mm512_loadu_ps((const float*) w + 0);
180 const __m512 vscaleGHIJKLMNOPQRSTUV = _mm512_loadu_ps((const float*) w + 16);
181 const __m512 vscaleWXYZabcdefghijkl = _mm512_loadu_ps((const float*) w + 32);
182 const __m512 vscalemnopqrstuvwxyz01 = _mm512_loadu_ps((const float*) w + 48);
183 w = (const int32_t*) w + 64;
184
185 vscaled0x0123456789ABCDEF = _mm512_mul_ps(vscaled0x0123456789ABCDEF, vscale0123456789ABCDEF);
186 vscaled0xGHIJKLMNOPQRSTUV = _mm512_mul_ps(vscaled0xGHIJKLMNOPQRSTUV, vscaleGHIJKLMNOPQRSTUV);
187 vscaled0xWXYZabcdefghijkl = _mm512_mul_ps(vscaled0xWXYZabcdefghijkl, vscaleWXYZabcdefghijkl);
188 vscaled0xmnopqrstuvwxyz01 = _mm512_mul_ps(vscaled0xmnopqrstuvwxyz01, vscalemnopqrstuvwxyz01);
189
190 vscaled0x0123456789ABCDEF = _mm512_min_ps(vscaled0x0123456789ABCDEF, voutput_max_less_zero_point);
191 vscaled0xGHIJKLMNOPQRSTUV = _mm512_min_ps(vscaled0xGHIJKLMNOPQRSTUV, voutput_max_less_zero_point);
192 vscaled0xWXYZabcdefghijkl = _mm512_min_ps(vscaled0xWXYZabcdefghijkl, voutput_max_less_zero_point);
193 vscaled0xmnopqrstuvwxyz01 = _mm512_min_ps(vscaled0xmnopqrstuvwxyz01, voutput_max_less_zero_point);
194
195 vacc0x0123456789ABCDEF = _mm512_cvtps_epi32(vscaled0x0123456789ABCDEF);
196 vacc0xGHIJKLMNOPQRSTUV = _mm512_cvtps_epi32(vscaled0xGHIJKLMNOPQRSTUV);
197 vacc0xWXYZabcdefghijkl = _mm512_cvtps_epi32(vscaled0xWXYZabcdefghijkl);
198 vacc0xmnopqrstuvwxyz01 = _mm512_cvtps_epi32(vscaled0xmnopqrstuvwxyz01);
199
200 vacc0x0123456789ABCDEF = _mm512_add_epi32(vacc0x0123456789ABCDEF, voutput_zero_point);
201 vacc0xGHIJKLMNOPQRSTUV = _mm512_add_epi32(vacc0xGHIJKLMNOPQRSTUV, voutput_zero_point);
202 vacc0xWXYZabcdefghijkl = _mm512_add_epi32(vacc0xWXYZabcdefghijkl, voutput_zero_point);
203 vacc0xmnopqrstuvwxyz01 = _mm512_add_epi32(vacc0xmnopqrstuvwxyz01, voutput_zero_point);
204
205 __m128i vout0x0123456789ABCDEF = _mm512_cvtsepi32_epi8(vacc0x0123456789ABCDEF);
206 __m128i vout0xGHIJKLMNOPQRSTUV = _mm512_cvtsepi32_epi8(vacc0xGHIJKLMNOPQRSTUV);
207 __m128i vout0xWXYZabcdefghijkl = _mm512_cvtsepi32_epi8(vacc0xWXYZabcdefghijkl);
208 __m128i vout0xmnopqrstuvwxyz01 = _mm512_cvtsepi32_epi8(vacc0xmnopqrstuvwxyz01);
209
210 vout0x0123456789ABCDEF = _mm_max_epi8(vout0x0123456789ABCDEF, voutput_min);
211 vout0xGHIJKLMNOPQRSTUV = _mm_max_epi8(vout0xGHIJKLMNOPQRSTUV, voutput_min);
212 vout0xWXYZabcdefghijkl = _mm_max_epi8(vout0xWXYZabcdefghijkl, voutput_min);
213 vout0xmnopqrstuvwxyz01 = _mm_max_epi8(vout0xmnopqrstuvwxyz01, voutput_min);
214
215 if XNN_LIKELY(nc >= 64) {
216 _mm_storeu_si128((__m128i*) (c0 + 0), vout0x0123456789ABCDEF);
217 _mm_storeu_si128((__m128i*) (c0 + 16), vout0xGHIJKLMNOPQRSTUV);
218 _mm_storeu_si128((__m128i*) (c0 + 32), vout0xWXYZabcdefghijkl);
219 _mm_storeu_si128((__m128i*) (c0 + 48), vout0xmnopqrstuvwxyz01);
220 c0 = (int8_t*) ((uintptr_t) c0 + cn_stride);
221 a = (const int8_t**restrict) ((uintptr_t) a - ks);
222 nc -= 64;
223 } else {
224 // Prepare mask for valid 8-bit elements (depends on nc).
225 const __mmask16 vmask0 = _cvtu32_mask16((uint32_t) ((((UINT64_C(1) << nc) - 1) >> 0) & 0xFFFF));
226 const __mmask16 vmask1 = _cvtu32_mask16((uint32_t) ((((UINT64_C(1) << nc) - 1) >> 16) & 0xFFFF));
227 const __mmask16 vmask2 = _cvtu32_mask16((uint32_t) ((((UINT64_C(1) << nc) - 1) >> 32) & 0xFFFF));
228 const __mmask16 vmask3 = _cvtu32_mask16((uint32_t) ((((UINT64_C(1) << nc) - 1) >> 48) & 0xFFFF));
229
230 _mm_mask_storeu_epi8(c0 + 0, vmask0, vout0x0123456789ABCDEF);
231 _mm_mask_storeu_epi8(c0 + 16, vmask1, vout0xGHIJKLMNOPQRSTUV);
232 _mm_mask_storeu_epi8(c0 + 32, vmask2, vout0xWXYZabcdefghijkl);
233 _mm_mask_storeu_epi8(c0 + 48, vmask3, vout0xmnopqrstuvwxyz01);
234 nc = 0;
235 }
236 } while (nc != 0);
237
238 // Release tile config
239 // _tile_release();
240 __asm__ volatile ("tilerelease" ::);
241 #endif // defined(__x86_64__)
242 }