Debian Codegraph @

src/qd8-f32-qc8w-gemm/gen/qd8-f32-qc8w-gemm-1x64c4-minmax-avx512amx.c

raw
1 // Auto-generated file. Do not edit!
2 // Template: src/qs8-gemm/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/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_f32_qc8w_gemm_minmax_ukernel_1x64c4__avx512amx(
22 size_t mr,
23 size_t nc,
24 size_t kc,
25 const int8_t* restrict a,
26 size_t a_stride,
27 const void* restrict w,
28 float* restrict c,
29 size_t cm_stride,
30 size_t cn_stride,
31 const union xnn_f32_minmax_params params[restrict XNN_MIN_ELEMENTS(1)],
32 const struct xnn_qd8_quantization_params quantization_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 res0[1 * 16];
47 __attribute__((aligned(64))) int32_t res1[1 * 16];
48 __attribute__((aligned(64))) int32_t res2[1 * 16];
49 __attribute__((aligned(64))) int32_t res3[1 * 16];
50
51 kc = round_up_po2(kc, 4 * sizeof(int8_t));
52 const size_t kremainder = (kc & 63) ? (kc & 63) : 64;
53
54 // Define tile config data structure
55 struct __tile_config {
56 uint8_t palette_id;
57 uint8_t start_row;
58 uint8_t reserved_0[14];
59 uint16_t colsb[8];
60 uint16_t reserved_1[8];
61 uint8_t rows[8];
62 uint8_t reserved_2[8];
63 };
64
65 // Load tile configuration
66 __attribute__((aligned(64))) struct __tile_config tile_data = {0};
67 tile_data.palette_id = 1;
68 tile_data.rows[0] = mr; // tmm0 = res 0
69 tile_data.rows[1] = mr; // tmm1 = res 1
70 tile_data.rows[2] = mr; // tmm2 = res 2
71 tile_data.rows[3] = mr; // tmm3 = res 3
72 tile_data.rows[4] = mr; // tmm4 = input
73 tile_data.rows[5] = 16; // tmm5 = weights
74 tile_data.rows[6] = mr; // tmm6 = input remainder
75 tile_data.rows[7] = kremainder >> 2; // tmm7 = weights remainder
76
77 tile_data.colsb[0] = 64; // tmm0 = res 0
78 tile_data.colsb[1] = 64; // tmm1 = res 1
79 tile_data.colsb[2] = 64; // tmm2 = res 1
80 tile_data.colsb[3] = 64; // tmm3 = res 1
81 tile_data.colsb[4] = 64; // tmm4 = input
82 tile_data.colsb[5] = 64; // tmm5 = weights
83 tile_data.colsb[6] = kremainder; // tmm6 = input remainder
84 tile_data.colsb[7] = 64; // tmm7 = weights remainder
85
86 //_tile_loadconfig(&tile_data);
87 __asm__ volatile ("ldtilecfg %0" :: "m" (tile_data));
88
89 float* c0 = c;
90
91 const __m512 voutput_min = _mm512_set1_ps(params->scalar.min);
92 const __m512 voutput_max = _mm512_set1_ps(params->scalar.max);
93 // XNN_FORCE_REALIZATION(voutput_min);
94 // XNN_FORCE_REALIZATION(voutput_max);
95
96 do {
97 const __m512i vksum0123456789ABCDEF = _mm512_load_epi32((const int32_t*) w + 0);
98 const __m512i vksumGHIJKLMNOPQRSTUV = _mm512_load_epi32((const int32_t*) w + 16);
99 const __m512i vksumWXYZabcdefghijkl = _mm512_load_epi32((const int32_t*) w + 32);
100 const __m512i vksummnopqrstuvwxyz01 = _mm512_load_epi32((const int32_t*) w + 48);
101 w = (const int32_t*) w + 64;
102
103 // Zero tile accumulator
104 __asm__ volatile (
105 "tilezero %%tmm0\n"
106 "tilezero %%tmm1\n"
107 "tilezero %%tmm2\n"
108 "tilezero %%tmm3\n"
109 ::);
110
111 size_t k = kc;
112 while (k >= 64 * sizeof(int8_t)) {
113 _tile_loadd(4, a, a_stride);
114 a += 64;
115 _tile_loadd(5, (const int8_t*) w + 0, 256);
116 _tile_dpbssd(0, 4, 5);
117 _tile_loadd(5, (const int8_t*) w + 64, 256);
118 _tile_dpbssd(1, 4, 5);
119 _tile_loadd(5, (const int8_t*) w + 128, 256);
120 _tile_dpbssd(2, 4, 5);
121 _tile_loadd(5, (const int8_t*) w + 192, 256);
122 _tile_dpbssd(3, 4, 5);
123
124 w = (const int8_t*) w + 4096;
125 k -= 64 * sizeof(int8_t);
126 }
127
128 if XNN_UNLIKELY(k != 0) {
129 _tile_loadd(6, a, a_stride);
130 a += kremainder;
131 _tile_loadd(7, (const int8_t*) w + 0, 256);
132 _tile_dpbssd(0, 6, 7);
133 _tile_loadd(7, (const int8_t*) w + 64, 256);
134 _tile_dpbssd(1, 6, 7);
135 _tile_loadd(7, (const int8_t*) w + 128, 256);
136 _tile_dpbssd(2, 6, 7);
137 _tile_loadd(7, (const int8_t*) w + 192, 256);
138 _tile_dpbssd(3, 6, 7);
139
140 w = (const int8_t*) w + kremainder * 64;
141 k -= kremainder * sizeof(int8_t);
142 }
143
144 // Add tile to bias
145 _tile_stored(0, res0, 64);
146 _tile_stored(1, res1, 64);
147 _tile_stored(2, res2, 64);
148 _tile_stored(3, res3, 64);
149
150 __m512i vacc0x0123456789ABCDEF = _mm512_mullo_epi32(vksum0123456789ABCDEF, _mm512_set1_epi32((int) quantization_params[0].zero_point));
151 __m512i vacc0xGHIJKLMNOPQRSTUV = _mm512_mullo_epi32(vksumGHIJKLMNOPQRSTUV, _mm512_set1_epi32((int) quantization_params[0].zero_point));
152 __m512i vacc0xWXYZabcdefghijkl = _mm512_mullo_epi32(vksumWXYZabcdefghijkl, _mm512_set1_epi32((int) quantization_params[0].zero_point));
153 __m512i vacc0xmnopqrstuvwxyz01 = _mm512_mullo_epi32(vksummnopqrstuvwxyz01, _mm512_set1_epi32((int) quantization_params[0].zero_point));
154 vacc0x0123456789ABCDEF = _mm512_add_epi32(vacc0x0123456789ABCDEF, _mm512_load_epi32(res0 + 0));
155 vacc0xGHIJKLMNOPQRSTUV = _mm512_add_epi32(vacc0xGHIJKLMNOPQRSTUV, _mm512_load_epi32(res1 + 0));
156 vacc0xWXYZabcdefghijkl = _mm512_add_epi32(vacc0xWXYZabcdefghijkl, _mm512_load_epi32(res2 + 0));
157 vacc0xmnopqrstuvwxyz01 = _mm512_add_epi32(vacc0xmnopqrstuvwxyz01, _mm512_load_epi32(res3 + 0));
158
159 __m512 vscaled0x0123456789ABCDEF = _mm512_cvtepi32_ps(vacc0x0123456789ABCDEF);
160 __m512 vscaled0xGHIJKLMNOPQRSTUV = _mm512_cvtepi32_ps(vacc0xGHIJKLMNOPQRSTUV);
161 __m512 vscaled0xWXYZabcdefghijkl = _mm512_cvtepi32_ps(vacc0xWXYZabcdefghijkl);
162 __m512 vscaled0xmnopqrstuvwxyz01 = _mm512_cvtepi32_ps(vacc0xmnopqrstuvwxyz01);
163
164 vscaled0x0123456789ABCDEF = _mm512_mul_ps(vscaled0x0123456789ABCDEF, _mm512_set1_ps(quantization_params[0].inv_scale));
165 vscaled0xGHIJKLMNOPQRSTUV = _mm512_mul_ps(vscaled0xGHIJKLMNOPQRSTUV, _mm512_set1_ps(quantization_params[0].inv_scale));
166 vscaled0xWXYZabcdefghijkl = _mm512_mul_ps(vscaled0xWXYZabcdefghijkl, _mm512_set1_ps(quantization_params[0].inv_scale));
167 vscaled0xmnopqrstuvwxyz01 = _mm512_mul_ps(vscaled0xmnopqrstuvwxyz01, _mm512_set1_ps(quantization_params[0].inv_scale));
168
169 const __m512 vfilter_output_scale0123456789ABCDEF = _mm512_load_ps((const float*) w + 0);
170 const __m512 vfilter_output_scaleGHIJKLMNOPQRSTUV = _mm512_load_ps((const float*) w + 16);
171 const __m512 vfilter_output_scaleWXYZabcdefghijkl = _mm512_load_ps((const float*) w + 32);
172 const __m512 vfilter_output_scalemnopqrstuvwxyz01 = _mm512_load_ps((const float*) w + 48);
173 w = (const int32_t*) w + 64;
174 const __m512 vbias0123456789ABCDEF = _mm512_load_ps((const float*) w + 0);
175 const __m512 vbiasGHIJKLMNOPQRSTUV = _mm512_load_ps((const float*) w + 16);
176 const __m512 vbiasWXYZabcdefghijkl = _mm512_load_ps((const float*) w + 32);
177 const __m512 vbiasmnopqrstuvwxyz01 = _mm512_load_ps((const float*) w + 48);
178 w = (const int32_t*) w + 64;
179
180 vscaled0x0123456789ABCDEF = _mm512_fmadd_ps(vscaled0x0123456789ABCDEF, vfilter_output_scale0123456789ABCDEF, vbias0123456789ABCDEF);
181 vscaled0xGHIJKLMNOPQRSTUV = _mm512_fmadd_ps(vscaled0xGHIJKLMNOPQRSTUV, vfilter_output_scaleGHIJKLMNOPQRSTUV, vbiasGHIJKLMNOPQRSTUV);
182 vscaled0xWXYZabcdefghijkl = _mm512_fmadd_ps(vscaled0xWXYZabcdefghijkl, vfilter_output_scaleWXYZabcdefghijkl, vbiasWXYZabcdefghijkl);
183 vscaled0xmnopqrstuvwxyz01 = _mm512_fmadd_ps(vscaled0xmnopqrstuvwxyz01, vfilter_output_scalemnopqrstuvwxyz01, vbiasmnopqrstuvwxyz01);
184
185 vscaled0x0123456789ABCDEF = _mm512_max_ps(vscaled0x0123456789ABCDEF, voutput_min);
186 vscaled0xGHIJKLMNOPQRSTUV = _mm512_max_ps(vscaled0xGHIJKLMNOPQRSTUV, voutput_min);
187 vscaled0xWXYZabcdefghijkl = _mm512_max_ps(vscaled0xWXYZabcdefghijkl, voutput_min);
188 vscaled0xmnopqrstuvwxyz01 = _mm512_max_ps(vscaled0xmnopqrstuvwxyz01, voutput_min);
189
190 vscaled0x0123456789ABCDEF = _mm512_min_ps(vscaled0x0123456789ABCDEF, voutput_max);
191 vscaled0xGHIJKLMNOPQRSTUV = _mm512_min_ps(vscaled0xGHIJKLMNOPQRSTUV, voutput_max);
192 vscaled0xWXYZabcdefghijkl = _mm512_min_ps(vscaled0xWXYZabcdefghijkl, voutput_max);
193 vscaled0xmnopqrstuvwxyz01 = _mm512_min_ps(vscaled0xmnopqrstuvwxyz01, voutput_max);
194
195 if XNN_LIKELY(nc >= 64) {
196 _mm512_storeu_ps(c0 + 0, vscaled0x0123456789ABCDEF);
197 _mm512_storeu_ps(c0 + 16, vscaled0xGHIJKLMNOPQRSTUV);
198 _mm512_storeu_ps(c0 + 32, vscaled0xWXYZabcdefghijkl);
199 _mm512_storeu_ps(c0 + 48, vscaled0xmnopqrstuvwxyz01);
200 c0 = (float*) ((uintptr_t) c0 + cn_stride);
201
202 a -= kc;
203 nc -= 64;
204 } else {
205 // Prepare mask for valid 32-bit elements (depends on nc).
206 const __mmask16 vmask0 = _cvtu32_mask16((uint32_t) ((((UINT64_C(1) << nc) - 1) >> 0) & 0xFFFF));
207 const __mmask16 vmask1 = _cvtu32_mask16((uint32_t) ((((UINT64_C(1) << nc) - 1) >> 16) & 0xFFFF));
208 const __mmask16 vmask2 = _cvtu32_mask16((uint32_t) ((((UINT64_C(1) << nc) - 1) >> 32) & 0xFFFF));
209 const __mmask16 vmask3 = _cvtu32_mask16((uint32_t) ((((UINT64_C(1) << nc) - 1) >> 48) & 0xFFFF));
210 _mm512_mask_storeu_ps(c0 + 0, vmask0, vscaled0x0123456789ABCDEF);
211 _mm512_mask_storeu_ps(c0 + 16, vmask1, vscaled0xGHIJKLMNOPQRSTUV);
212 _mm512_mask_storeu_ps(c0 + 32, vmask2, vscaled0xWXYZabcdefghijkl);
213 _mm512_mask_storeu_ps(c0 + 48, vmask3, vscaled0xmnopqrstuvwxyz01);
214 nc = 0;
215 }
216 } while (nc != 0);
217 // Release tile config
218 // _tile_release();
219 __asm__ volatile ("tilerelease" ::);
220 #endif // defined(__x86_64__)
221 }