Debian Codegraph @

src/qd8-f32-qc8w-igemm/gen/qd8-f32-qc8w-igemm-1x16c4-minmax-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_qd8_f32_qc8w_igemm_minmax_ukernel_1x16c4__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 float* restrict c,
28 size_t cm_stride,
29 size_t cn_stride,
30 size_t a_offset,
31 const int8_t* zero,
32 const int8_t* zero_data,
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)])
35 {
36 assert(mr != 0);
37 assert(mr <= 1);
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 // TODO: amxintrin.h only provide intrinsics for __x86_64__
46 // Update if amxintrin changes
47 #if defined(__x86_64__)
48 __attribute__((aligned(64))) int32_t vintile[1 * 16];
49 __attribute__((aligned(64))) int32_t res0[1 * 16];
50
51 kc = round_up_po2(kc, 4 * sizeof(int8_t));
52 const size_t kremainder = (kc & 63) ? (kc & 63) : 64;
53 const __mmask16 kremainder_mask = _cvtu32_mask16((UINT32_C(1) << (kremainder >> 2)) - 1);
54
55 // Define tile config data structure
56 struct __tile_config {
57 uint8_t palette_id;
58 uint8_t start_row;
59 uint8_t reserved_0[14];
60 uint16_t colsb[8];
61 uint16_t reserved_1[8];
62 uint8_t rows[8];
63 uint8_t reserved_2[8];
64 };
65
66 // Load tile configuration
67 __attribute__((aligned(64))) struct __tile_config tile_data = {0};
68 tile_data.palette_id = 1;
69 tile_data.rows[0] = mr; // tmm0 = res 0
70 tile_data.rows[1] = mr; // tmm1 = res 1
71 tile_data.rows[2] = mr; // tmm2 = res 2
72 tile_data.rows[3] = mr; // tmm3 = res 3
73 tile_data.rows[4] = mr; // tmm4 = input
74 tile_data.rows[5] = 16; // tmm5 = weights
75 tile_data.rows[6] = mr; // tmm6 = input remainder
76 tile_data.rows[7] = kremainder >> 2; // tmm7 = weights remainder
77
78 tile_data.colsb[0] = 64; // tmm0 = res 0
79 tile_data.colsb[1] = 64; // tmm1 = res 1
80 tile_data.colsb[2] = 64; // tmm2 = res 1
81 tile_data.colsb[3] = 64; // tmm3 = res 1
82 tile_data.colsb[4] = 64; // tmm4 = input
83 tile_data.colsb[5] = 64; // tmm5 = weights
84 tile_data.colsb[6] = kremainder; // tmm6 = input remainder
85 tile_data.colsb[7] = 64; // tmm7 = weights remainder
86
87 //_tile_loadconfig(&tile_data);
88 __asm__ volatile ("ldtilecfg %0" :: "m" (tile_data));
89
90 float* c0 = c;
91
92 const __m512 voutput_min = _mm512_set1_ps(params->scalar.min);
93 const __m512 voutput_max = _mm512_set1_ps(params->scalar.max);
94 // XNN_FORCE_REALIZATION(voutput_min);
95 // XNN_FORCE_REALIZATION(voutput_max);
96
97 do {
98 const __m512i vksum0123456789ABCDEF = _mm512_loadu_epi32((const int32_t*) w + 0);
99 w = (const int32_t*) w + 16;
100
101 // Zero tile accumulator
102 __asm__ volatile (
103 "tilezero %%tmm0\n"
104 ::);
105
106 size_t p = ks;
107 do {
108 const int8_t* restrict a0 = a[0];
109 if XNN_UNPREDICTABLE(a0 != zero) {
110 a0 = (const int8_t*) ((uintptr_t) a0 + a_offset);
111 } else {
112 a0 = zero_data;
113 }
114 a += 1;
115
116 size_t k = kc;
117 {
118 while (k >= 64 * sizeof(int8_t)) {
119 _tile_loadd(4, a0, 64); // Directly load input for mr=1
120 a0 += 64;
121 _tile_loadd(5, (const int8_t*) w + 0, 64);
122 _tile_dpbssd(0, 4, 5);
123
124 w = (const int8_t*) w + 1024;
125 k -= 64 * sizeof(int8_t);
126 }
127 }
128
129 if XNN_UNLIKELY(k != 0) {
130 const __m512i vin0 = _mm512_maskz_loadu_epi32(kremainder_mask, a0);
131 a0 += kremainder;
132 _mm512_store_epi32(vintile + 0, vin0);
133 _tile_loadd(6, vintile, 64);
134 _tile_loadd(7, (const int8_t*) w + 0, 64);
135 _tile_dpbssd(0, 6, 7);
136
137 w = (const int8_t*) w + kremainder * 16;
138 k -= kremainder * sizeof(int8_t);
139 }
140
141 p -= 1 * sizeof(void*);
142 } while (p != 0);
143
144 // Add tile to bias
145 _tile_stored(0, res0, 64);
146
147 __m512i vacc0x0123456789ABCDEF = _mm512_mullo_epi32(vksum0123456789ABCDEF, _mm512_set1_epi32((int) quantization_params->zero_point));
148 vacc0x0123456789ABCDEF = _mm512_add_epi32(vacc0x0123456789ABCDEF, _mm512_load_epi32(res0 + 0));
149
150 __m512 vscaled0x0123456789ABCDEF = _mm512_cvtepi32_ps(vacc0x0123456789ABCDEF);
151
152 vscaled0x0123456789ABCDEF = _mm512_mul_ps(vscaled0x0123456789ABCDEF, _mm512_set1_ps(quantization_params->inv_scale));
153
154 const __m512 vfilter_output_scale0123456789ABCDEF = _mm512_loadu_ps((const float*) w + 0);
155 w = (const int32_t*) w + 16;
156 const __m512 vbias0123456789ABCDEF = _mm512_loadu_ps((const float*) w + 0);
157 w = (const int32_t*) w + 16;
158
159 vscaled0x0123456789ABCDEF = _mm512_fmadd_ps(vscaled0x0123456789ABCDEF, vfilter_output_scale0123456789ABCDEF, vbias0123456789ABCDEF);
160
161 vscaled0x0123456789ABCDEF = _mm512_max_ps(vscaled0x0123456789ABCDEF, voutput_min);
162
163 vscaled0x0123456789ABCDEF = _mm512_min_ps(vscaled0x0123456789ABCDEF, voutput_max);
164
165 if XNN_LIKELY(nc >= 16) {
166 _mm512_storeu_ps(c0 + 0, vscaled0x0123456789ABCDEF);
167 c0 = (float*) ((uintptr_t) c0 + cn_stride);
168
169 a = (const int8_t**restrict) ((uintptr_t) a - ks);
170 nc -= 16;
171 } else {
172 // Prepare mask for valid 32-bit elements (depends on nc).
173 const __mmask16 vmask0 = _cvtu32_mask16((uint32_t) ((((UINT64_C(1) << nc) - 1) >> 0) & 0xFFFF));
174 _mm512_mask_storeu_ps(c0 + 0, vmask0, vscaled0x0123456789ABCDEF);
175 nc = 0;
176 }
177 } while (nc != 0);
178
179 // Release tile config
180 // _tile_release();
181 __asm__ volatile ("tilerelease" ::);
182 #endif // defined(__x86_64__)
183 }