1// Copyright 2020 Google LLC 2// 3// This source code is licensed under the BSD-style license found in the 4// LICENSE file in the root directory of this source tree. 5 6$ABC = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ" 7$assert REQUANTIZATION == "FP32" 8$assert DATATYPE in ["QC8", "QS8", "QU8"] 9$assert VARIANT in ["LD256", "EXTENDED"] 10$assert MR <= 4 11#include <assert.h> 12 13#include <immintrin.h> 14 15#include <xnnpack/gemm.h> 16#include <xnnpack/intrinsics-polyfill.h> 17#include <xnnpack/math.h> 18 19 20$GEMM_SUFFIX = "_xw" if VARIANT == "EXTENDED" else "" 21$PARAMS_STRUCT = REQUANTIZATION.lower() + "_avx512" 22$PARAMS_UNION = "xnn_%s_conv_minmax_params" % DATATYPE.lower() 23$XINT8_T = "uint8_t" if DATATYPE == "QU8" else "int8_t" 24void xnn_${DATATYPE.lower()}_gemm${GEMM_SUFFIX}_minmax_fp32_ukernel_${MR}x16c8__avx512skx( 25 size_t mr, 26 size_t nc, 27 size_t kc, 28 const ${XINT8_T}* restrict a, 29 size_t a_stride, 30 const void* restrict w, 31 ${XINT8_T}* restrict c, 32 size_t cm_stride, 33 size_t cn_stride, 34 const union ${PARAMS_UNION} params[restrict XNN_MIN_ELEMENTS(1)]) XNN_OOB_READS 35{ 36 assert(mr != 0); 37 assert(mr <= ${MR}); 38 assert(nc != 0); 39 assert(kc != 0); 40 assert(kc % sizeof(${XINT8_T}) == 0); 41 assert(a != NULL); 42 assert(w != NULL); 43 assert(c != NULL); 44 45 kc = round_up_po2(kc, 8); 46 const ${XINT8_T}* a0 = a; 47 ${XINT8_T}* c0 = c; 48 $for M in range(1, MR): 49 const ${XINT8_T}* a${M} = (const ${XINT8_T}*) ((uintptr_t) a${M-1} + a_stride); 50 ${XINT8_T}* c${M} = (${XINT8_T}*) ((uintptr_t) c${M-1} + cm_stride); 51 $if M % 2 == 0: 52 if XNN_UNPREDICTABLE(mr <= ${M}) { 53 a${M} = a${M-1}; 54 c${M} = c${M-1}; 55 } 56 $elif M + 1 == MR: 57 if XNN_UNPREDICTABLE(mr != ${M+1}) { 58 a${M} = a${M-1}; 59 c${M} = c${M-1}; 60 } 61 $else: 62 if XNN_UNPREDICTABLE(mr < ${M+1}) { 63 a${M} = a${M-1}; 64 c${M} = c${M-1}; 65 } 66 67 const __mmask16 vbias_mask = _cvtu32_mask16(0x1111); 68 $if DATATYPE != "QC8": 69 const __m512 vscale = _mm512_load_ps(params->${PARAMS_STRUCT}.scale); 70 const __m512 voutput_max_less_zero_point = _mm512_load_ps(params->${PARAMS_STRUCT}.output_max_less_zero_point); 71 $if MR > 1: 72 const __m512i voutput_zero_point = _mm512_load_si512(params->${PARAMS_STRUCT}.output_zero_point); 73 $else: 74 const __m256i voutput_zero_point = _mm256_load_si256((const __m256i*) params->${PARAMS_STRUCT}.output_zero_point); 75 $if MR > 2: 76 const __m512i voutput_min = _mm512_load_si512(params->${PARAMS_STRUCT}.output_min); 77 $elif MR == 2: 78 const __m256i voutput_min = _mm256_load_si256((const __m256i*) params->${PARAMS_STRUCT}.output_min); 79 $else: 80 const __m128i voutput_min = _mm_load_si128((const __m128i*) params->${PARAMS_STRUCT}.output_min); 81 do { 82 __m512i vacc0x0123 = _mm512_maskz_expandloadu_epi32(vbias_mask, w); 83 $for N in range(4, 16, 4): 84 __m512i vacc0x${ABC[N:N+4]} = _mm512_maskz_expandloadu_epi32(vbias_mask, (const void*) ((const int32_t*) w + ${N})); 85 $for M in range(1, MR): 86 $for N in range(0, 16, 4): 87 __m512i vacc${M}x${ABC[N:N+4]} = vacc0x${ABC[N:N+4]}; 88 w = (const void*) ((const int32_t*) w + 16); 89 90 size_t k = 0; 91 $if DATATYPE == "QU8": 92 const __m512i vb_zero_point = _mm512_load_si512(params->${PARAMS_STRUCT}.kernel_zero_point); 93 while (k < kc) { 94 $for M in range(MR): 95 $if DATATYPE == "QU8": 96 const __m512i va${M} = _mm512_broadcast_i32x4(_mm_cvtepu8_epi16(_mm_loadl_epi64((const __m128i*) a${M}))); 97 $else: 98 const __m512i va${M} = _mm512_broadcast_i32x4(_mm_cvtepi8_epi16(_mm_loadl_epi64((const __m128i*) a${M}))); 99 a${M} += 8; 100 101 $for N in range(0, 16, 4): 102 $if VARIANT == "EXTENDED": 103 $if N == 0: 104 const __m512i vb${ABC[N:N+4]} = _mm512_load_si512(w); 105 $else: 106 const __m512i vb${ABC[N:N+4]} = _mm512_load_si512((const void*) ((const int16_t*) w + ${N * 8})); 107 $else: 108 $if DATATYPE == "QU8": 109 $if N == 0: 110 const __m512i vb${ABC[N:N+4]} = _mm512_sub_epi16(_mm512_cvtepu8_epi16(_mm256_load_si256((const __m256i*) w)), vb_zero_point); 111 $else: 112 const __m512i vb${ABC[N:N+4]} = _mm512_sub_epi16(_mm512_cvtepu8_epi16(_mm256_load_si256((const __m256i*) ((const ${XINT8_T}*) w + ${N * 8}))), vb_zero_point); 113 $else: 114 $if N == 0: 115 const __m512i vb${ABC[N:N+4]} = _mm512_cvtepi8_epi16(_mm256_load_si256((const __m256i*) w)); 116 $else: 117 const __m512i vb${ABC[N:N+4]} = _mm512_cvtepi8_epi16(_mm256_load_si256((const __m256i*) ((const ${XINT8_T}*) w + ${N * 8}))); 118 119 $for M in range(MR): 120 vacc${M}x${ABC[N:N+4]} = _mm512_add_epi32(vacc${M}x${ABC[N:N+4]}, _mm512_madd_epi16(va${M}, vb${ABC[N:N+4]})); 121 122 $if VARIANT == "EXTENDED": 123 w = (const void*) ((const int16_t*) w + 128); 124 $else: 125 w = (const void*) ((const ${XINT8_T}*) w + 128); 126 k += 8 * sizeof(${XINT8_T}); 127 } 128 129 $for M in range(MR): 130 const __m512i vacc${M}x04152637 = _mm512_add_epi32(_mm512_unpacklo_epi32(vacc${M}x0123, vacc${M}x4567), _mm512_unpackhi_epi32(vacc${M}x0123, vacc${M}x4567)); 131 const __m512i vacc${M}x8C9DAEBF = _mm512_add_epi32(_mm512_unpacklo_epi32(vacc${M}x89AB, vacc${M}xCDEF), _mm512_unpackhi_epi32(vacc${M}x89AB, vacc${M}xCDEF)); 132 133 $for M in range(MR): 134 __m512i vacc${M}x084C195D2A6E3B7F = _mm512_add_epi32(_mm512_unpacklo_epi32(vacc${M}x04152637, vacc${M}x8C9DAEBF), _mm512_unpackhi_epi32(vacc${M}x04152637, vacc${M}x8C9DAEBF)); 135 136 $for M in range(MR): 137 __m512 vscaled${M}x084C195D2A6E3B7F = _mm512_cvtepi32_ps(vacc${M}x084C195D2A6E3B7F); 138 139 $if DATATYPE == "QC8": 140 const __m512 vscale012345678ABCDEF = _mm512_load_ps(w); 141 w = (const void*) ((const float*) w + 16); 142 const __m512 vscale084C195D2A6E3B7F = _mm512_permutexvar_ps(_mm512_set_epi32(15, 7, 11, 3, 14, 6, 10, 2, 13, 5, 9, 1, 12, 4, 8, 0), vscale012345678ABCDEF); 143 $for M in range(MR): 144 vscaled${M}x084C195D2A6E3B7F = _mm512_mul_ps(vscaled${M}x084C195D2A6E3B7F, vscale084C195D2A6E3B7F); 145 $else: 146 $for M in range(MR): 147 vscaled${M}x084C195D2A6E3B7F = _mm512_mul_ps(vscaled${M}x084C195D2A6E3B7F, vscale); 148 149 $for M in range(MR): 150 vscaled${M}x084C195D2A6E3B7F = _mm512_min_ps(vscaled${M}x084C195D2A6E3B7F, voutput_max_less_zero_point); 151 152 $for M in range(MR): 153 vacc${M}x084C195D2A6E3B7F = _mm512_cvtps_epi32(vscaled${M}x084C195D2A6E3B7F); 154 155 $if MR == 1: 156 const __m256i vacc0x084C2A6E195D3B7F = _mm256_adds_epi16(_mm256_packs_epi32(_mm512_castsi512_si256(vacc0x084C195D2A6E3B7F), _mm512_extracti32x8_epi32(vacc0x084C195D2A6E3B7F, 1)), voutput_zero_point); 157 $else: 158 $for M in range(0, MR, 2): 159 const __m512i vacc${M}${min(M+1, MR-1)}x084Cx195Dx2A6Ex3B7F = _mm512_adds_epi16(_mm512_packs_epi32(vacc${M}x084C195D2A6E3B7F, vacc${min(M+1, MR-1)}x084C195D2A6E3B7F), voutput_zero_point); 160 161 $if MR > 2: 162 $if DATATYPE == "QU8": 163 __m512i vout012${min(3, MR-1)}x084Cx195Dx2A6Ex3B7F = _mm512_packus_epi16(vacc01x084Cx195Dx2A6Ex3B7F, vacc2${min(3, MR-1)}x084Cx195Dx2A6Ex3B7F); 164 $else: 165 __m512i vout012${min(3, MR-1)}x084Cx195Dx2A6Ex3B7F = _mm512_packs_epi16(vacc01x084Cx195Dx2A6Ex3B7F, vacc2${min(3, MR-1)}x084Cx195Dx2A6Ex3B7F); 166 vout012${min(M+3, MR-1)}x084Cx195Dx2A6Ex3B7F = _mm512_permutexvar_epi32(_mm512_set_epi32(15, 11, 7, 3, 14, 10, 6, 2, 13, 9, 5, 1, 12, 8, 4, 0), vout012${min(3, MR-1)}x084Cx195Dx2A6Ex3B7F); 167 __m512i vout012${min(3, MR-1)}x0123456789ABCDEF = _mm512_shuffle_epi8(vout012${min(3, MR-1)}x084Cx195Dx2A6Ex3B7F, _mm512_set_epi8(15, 11, 7, 3, 13, 9, 5, 1, 14, 10, 6, 2, 12, 8, 4, 0, 15, 11, 7, 3, 13, 9, 5, 1, 14, 10, 6, 2, 12, 8, 4, 0, 15, 11, 7, 3, 13, 9, 5, 1, 14, 10, 6, 2, 12, 8, 4, 0, 15, 11, 7, 3, 13, 9, 5, 1, 14, 10, 6, 2, 12, 8, 4, 0)); 168 $if DATATYPE == "QU8": 169 vout012${min(3, MR-1)}x0123456789ABCDEF = _mm512_max_epu8(vout012${min(3, MR-1)}x0123456789ABCDEF, voutput_min); 170 $else: 171 vout012${min(3, MR-1)}x0123456789ABCDEF = _mm512_max_epi8(vout012${min(3, MR-1)}x0123456789ABCDEF, voutput_min); 172 $elif MR == 2: 173 $if DATATYPE == "QU8": 174 const __m256i vout01x084Cx2A6Ex195Dx3B7F = _mm256_packus_epi16(_mm512_castsi512_si256(vacc01x084Cx195Dx2A6Ex3B7F), _mm512_extracti32x8_epi32(vacc01x084Cx195Dx2A6Ex3B7F, 1)); 175 $else: 176 const __m256i vout01x084Cx2A6Ex195Dx3B7F = _mm256_packs_epi16(_mm512_castsi512_si256(vacc01x084Cx195Dx2A6Ex3B7F), _mm512_extracti32x8_epi32(vacc01x084Cx195Dx2A6Ex3B7F, 1)); 177 const __m256i vout01x084C2A6E195D3B7F = _mm256_permutevar8x32_epi32(vout01x084Cx2A6Ex195Dx3B7F, _mm256_set_epi32(7, 5, 3, 1, 6, 4, 2, 0)); 178 __m256i vout01x0123456789ABCDEF = _mm256_shuffle_epi8(vout01x084C2A6E195D3B7F, _mm256_set_epi8(15, 7, 11, 3, 13, 5, 9, 1, 14, 6, 10, 2, 12, 4, 8, 0, 15, 7, 11, 3, 13, 5, 9, 1, 14, 6, 10, 2, 12, 4, 8, 0)); 179 $if DATATYPE == "QU8": 180 vout01x0123456789ABCDEF = _mm256_max_epu8(vout01x0123456789ABCDEF, voutput_min); 181 $else: 182 vout01x0123456789ABCDEF = _mm256_max_epi8(vout01x0123456789ABCDEF, voutput_min); 183 $elif MR == 1: 184 $if DATATYPE == "QU8": 185 const __m128i vout0x084C2A6E195D3B7F = _mm_packus_epi16(_mm256_castsi256_si128(vacc0x084C2A6E195D3B7F), _mm256_extracti128_si256(vacc0x084C2A6E195D3B7F, 1)); 186 $else: 187 const __m128i vout0x084C2A6E195D3B7F = _mm_packs_epi16(_mm256_castsi256_si128(vacc0x084C2A6E195D3B7F), _mm256_extracti128_si256(vacc0x084C2A6E195D3B7F, 1)); 188 __m128i vout0x0123456789ABCDEF = _mm_shuffle_epi8(vout0x084C2A6E195D3B7F, _mm_set_epi8(15, 7, 11, 3, 13, 5, 9, 1, 14, 6, 10, 2, 12, 4, 8, 0)); 189 $if DATATYPE == "QU8": 190 vout0x0123456789ABCDEF = _mm_max_epu8(vout0x0123456789ABCDEF, voutput_min); 191 $else: 192 vout0x0123456789ABCDEF = _mm_max_epi8(vout0x0123456789ABCDEF, voutput_min); 193 194 $if MR > 2: 195 if (nc >= 16) { 196 _mm_storeu_si128((__m128i*) c0, _mm512_castsi512_si128(vout012${min(M+3, MR-1)}x0123456789ABCDEF)); 197 $for M in range(1, MR): 198 _mm_storeu_si128((__m128i*) c${M}, _mm512_extracti32x4_epi32(vout012${min(M+3, MR-1)}x0123456789ABCDEF, ${M})); 199 200 $for M in range(MR): 201 a${M} = (const ${XINT8_T}*) ((uintptr_t) a${M} - k); 202 203 $for M in range(MR): 204 c${M} = (${XINT8_T}*) ((uintptr_t) c${M} + cn_stride); 205 206 nc -= 16; 207 } else { 208 // Prepare mask for valid 8-bit elements (depends on nc). 209 __mmask64 vmask = _cvtu64_mask64((uint64_t) ((UINT32_C(1) << nc) - UINT32_C(1))); 210 211 _mm512_mask_storeu_epi8(c0, vmask, vout012${min(M+3, MR-1)}x0123456789ABCDEF); 212 $for M in range(1, MR): 213 vmask = _kshiftli_mask64(vmask, 16); 214 _mm512_mask_storeu_epi8(c${M} - ${M * 16}, vmask, vout012${min(3, MR-1)}x0123456789ABCDEF); 215 216 nc = 0; 217 } 218 $elif MR == 2: 219 if (nc >= 16) { 220 _mm_storeu_si128((__m128i*) c0, _mm256_castsi256_si128(vout01x0123456789ABCDEF)); 221 _mm_storeu_si128((__m128i*) c1, _mm256_extracti128_si256(vout01x0123456789ABCDEF, 1)); 222 223 $for M in range(MR): 224 c${M} = (${XINT8_T}*) ((uintptr_t) c${M} + cn_stride); 225 226 $for M in range(MR): 227 a${M} = (const ${XINT8_T}*) ((uintptr_t) a${M} - kc); 228 229 nc -= 16; 230 } else { 231 // Prepare mask for valid 8-bit elements (depends on nc). 232 __mmask64 vmask = _cvtu64_mask64((uint64_t) ((UINT32_C(1) << nc) - UINT32_C(1))); 233 234 _mm256_mask_storeu_epi8(c0, vmask, vout01x0123456789ABCDEF); 235 vmask = _kshiftli_mask64(vmask, 16); 236 _mm256_mask_storeu_epi8(c1 - 16, vmask, vout01x0123456789ABCDEF); 237 238 nc = 0; 239 } 240 $elif MR == 1: 241 if (nc >= 16) { 242 _mm_storeu_si128((__m128i*) c0, vout0x0123456789ABCDEF); 243 244 $for M in range(MR): 245 a${M} = (const ${XINT8_T}*) ((uintptr_t) a${M} - k); 246 247 $for M in range(MR): 248 c${M} = (${XINT8_T}*) ((uintptr_t) c${M} + cn_stride); 249 250 nc -= 16; 251 } else { 252 // Prepare mask for valid 8-bit elements (depends on nc). 253 const __mmask64 vmask = _cvtu64_mask64((uint64_t) ((UINT32_C(1) << nc) - UINT32_C(1))); 254 255 _mm_mask_storeu_epi8(c0, vmask, vout0x0123456789ABCDEF); 256 257 nc = 0; 258 } 259 } while (nc != 0); 260} 261