| 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_1x32c4__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 |
|
| 50 |
kc = round_up_po2(kc, 4 * sizeof(int8_t)); |
| 51 |
const size_t kremainder = (kc & 63) ? (kc & 63) : 64; |
| 52 |
const __mmask16 kremainder_mask = _cvtu32_mask16((UINT32_C(1) << (kremainder >> 2)) - 1); |
| 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 |
int8_t* c0 = c; |
| 90 |
|
| 91 |
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); |
| 92 |
const __m512i voutput_zero_point = _mm512_set1_epi32(params->fp32_scalar.output_zero_point); |
| 93 |
const __m128i voutput_min = _mm_set1_epi8(params->fp32_scalar.output_min); |
| 94 |
// XNN_FORCE_REALIZATION(voutput_max_less_zero_point); |
| 95 |
// XNN_FORCE_REALIZATION(voutput_zero_point); |
| 96 |
// XNN_FORCE_REALIZATION(voutput_min); |
| 97 |
|
| 98 |
do { |
| 99 |
const __m512i vksum0123456789ABCDEF = _mm512_loadu_epi32((const int32_t*) w + 0); |
| 100 |
const __m512i vksumGHIJKLMNOPQRSTUV = _mm512_loadu_epi32((const int32_t*) w + 16); |
| 101 |
w = (const int32_t*) w + 32; |
| 102 |
|
| 103 |
// Zero tile accumulator |
| 104 |
__asm__ volatile ( |
| 105 |
"tilezero %%tmm0\n" |
| 106 |
"tilezero %%tmm1\n" |
| 107 |
::); |
| 108 |
|
| 109 |
size_t p = ks; |
| 110 |
do { |
| 111 |
const int8_t* restrict a0 = a[0]; |
| 112 |
if XNN_UNPREDICTABLE(a0 != zero) { |
| 113 |
a0 = (const int8_t*) ((uintptr_t) a0 + a_offset); |
| 114 |
} |
| 115 |
a += 1; |
| 116 |
|
| 117 |
size_t k = kc; |
| 118 |
{ |
| 119 |
while (k >= 64 * sizeof(int8_t)) { |
| 120 |
_tile_loadd(4, a0, 64); // Directly load input for mr=1 |
| 121 |
a0 += 64; |
| 122 |
_tile_loadd(5, (const int8_t*) w + 0, 128); |
| 123 |
_tile_dpbssd(0, 4, 5); |
| 124 |
_tile_loadd(5, (const int8_t*) w + 64, 128); |
| 125 |
_tile_dpbssd(1, 4, 5); |
| 126 |
|
| 127 |
w = (const int8_t*) w + 2048; |
| 128 |
k -= 64 * sizeof(int8_t); |
| 129 |
} |
| 130 |
} |
| 131 |
|
| 132 |
if XNN_UNLIKELY(k != 0) { |
| 133 |
const __m512i vin0 = _mm512_maskz_loadu_epi32(kremainder_mask, a0); |
| 134 |
a0 += kremainder; |
| 135 |
_mm512_store_epi32(vintile + 0, vin0); |
| 136 |
_tile_loadd(6, vintile, 64); |
| 137 |
_tile_loadd(7, (const int8_t*) w + 0, 128); |
| 138 |
_tile_dpbssd(0, 6, 7); |
| 139 |
_tile_loadd(7, (const int8_t*) w + 64, 128); |
| 140 |
_tile_dpbssd(1, 6, 7); |
| 141 |
|
| 142 |
w = (const int8_t*) w + kremainder * 32; |
| 143 |
k -= kremainder * sizeof(int8_t); |
| 144 |
} |
| 145 |
|
| 146 |
p -= 1 * sizeof(void*); |
| 147 |
} while (p != 0); |
| 148 |
|
| 149 |
// Add tile to bias |
| 150 |
_tile_stored(0, res0, 64); |
| 151 |
_tile_stored(1, res1, 64); |
| 152 |
|
| 153 |
__m512i vacc0x0123456789ABCDEF = _mm512_add_epi32(vksum0123456789ABCDEF, _mm512_load_epi32(res0 + 0)); |
| 154 |
__m512i vacc0xGHIJKLMNOPQRSTUV = _mm512_add_epi32(vksumGHIJKLMNOPQRSTUV, _mm512_load_epi32(res1 + 0)); |
| 155 |
|
| 156 |
__m512 vscaled0x0123456789ABCDEF = _mm512_cvtepi32_ps(vacc0x0123456789ABCDEF); |
| 157 |
__m512 vscaled0xGHIJKLMNOPQRSTUV = _mm512_cvtepi32_ps(vacc0xGHIJKLMNOPQRSTUV); |
| 158 |
|
| 159 |
const __m512 vscale0123456789ABCDEF = _mm512_loadu_ps((const float*) w + 0); |
| 160 |
const __m512 vscaleGHIJKLMNOPQRSTUV = _mm512_loadu_ps((const float*) w + 16); |
| 161 |
w = (const int32_t*) w + 32; |
| 162 |
|
| 163 |
vscaled0x0123456789ABCDEF = _mm512_mul_ps(vscaled0x0123456789ABCDEF, vscale0123456789ABCDEF); |
| 164 |
vscaled0xGHIJKLMNOPQRSTUV = _mm512_mul_ps(vscaled0xGHIJKLMNOPQRSTUV, vscaleGHIJKLMNOPQRSTUV); |
| 165 |
|
| 166 |
vscaled0x0123456789ABCDEF = _mm512_min_ps(vscaled0x0123456789ABCDEF, voutput_max_less_zero_point); |
| 167 |
vscaled0xGHIJKLMNOPQRSTUV = _mm512_min_ps(vscaled0xGHIJKLMNOPQRSTUV, voutput_max_less_zero_point); |
| 168 |
|
| 169 |
vacc0x0123456789ABCDEF = _mm512_cvtps_epi32(vscaled0x0123456789ABCDEF); |
| 170 |
vacc0xGHIJKLMNOPQRSTUV = _mm512_cvtps_epi32(vscaled0xGHIJKLMNOPQRSTUV); |
| 171 |
|
| 172 |
vacc0x0123456789ABCDEF = _mm512_add_epi32(vacc0x0123456789ABCDEF, voutput_zero_point); |
| 173 |
vacc0xGHIJKLMNOPQRSTUV = _mm512_add_epi32(vacc0xGHIJKLMNOPQRSTUV, voutput_zero_point); |
| 174 |
|
| 175 |
__m128i vout0x0123456789ABCDEF = _mm512_cvtsepi32_epi8(vacc0x0123456789ABCDEF); |
| 176 |
__m128i vout0xGHIJKLMNOPQRSTUV = _mm512_cvtsepi32_epi8(vacc0xGHIJKLMNOPQRSTUV); |
| 177 |
|
| 178 |
vout0x0123456789ABCDEF = _mm_max_epi8(vout0x0123456789ABCDEF, voutput_min); |
| 179 |
vout0xGHIJKLMNOPQRSTUV = _mm_max_epi8(vout0xGHIJKLMNOPQRSTUV, voutput_min); |
| 180 |
|
| 181 |
if XNN_LIKELY(nc >= 32) { |
| 182 |
_mm_storeu_si128((__m128i*) (c0 + 0), vout0x0123456789ABCDEF); |
| 183 |
_mm_storeu_si128((__m128i*) (c0 + 16), vout0xGHIJKLMNOPQRSTUV); |
| 184 |
c0 = (int8_t*) ((uintptr_t) c0 + cn_stride); |
| 185 |
a = (const int8_t**restrict) ((uintptr_t) a - ks); |
| 186 |
nc -= 32; |
| 187 |
} else { |
| 188 |
// Prepare mask for valid 8-bit elements (depends on nc). |
| 189 |
const __mmask16 vmask0 = _cvtu32_mask16((uint32_t) ((((UINT64_C(1) << nc) - 1) >> 0) & 0xFFFF)); |
| 190 |
const __mmask16 vmask1 = _cvtu32_mask16((uint32_t) ((((UINT64_C(1) << nc) - 1) >> 16) & 0xFFFF)); |
| 191 |
|
| 192 |
_mm_mask_storeu_epi8(c0 + 0, vmask0, vout0x0123456789ABCDEF); |
| 193 |
_mm_mask_storeu_epi8(c0 + 16, vmask1, vout0xGHIJKLMNOPQRSTUV); |
| 194 |
nc = 0; |
| 195 |
} |
| 196 |
} while (nc != 0); |
| 197 |
|
| 198 |
// Release tile config |
| 199 |
// _tile_release(); |
| 200 |
__asm__ volatile ("tilerelease" ::); |
| 201 |
#endif // defined(__x86_64__) |
| 202 |
} |