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 CHANNEL_TILE % 16 == 0 8$assert CHANNEL_TILE >= 16 9$assert KERNEL_TILE >= 2 10#include <assert.h> 11 12#include <immintrin.h> 13 14#include <xnnpack/dwconv.h> 15#include <xnnpack/intrinsics-polyfill.h> 16 17 18void xnn_qs8_dwconv_minmax_ukernel_up${CHANNEL_TILE}x${KERNEL_TILE}__avx512skx_mul32( 19 size_t channels, 20 size_t output_width, 21 const int8_t** input, 22 const void* weights, 23 int8_t* output, 24 size_t input_stride, 25 size_t output_increment, 26 size_t input_offset, 27 const int8_t* zero, 28 const union xnn_qs8_gemm_params params[restrict XNN_MIN_ELEMENTS(1)]) XNN_DISABLE_TSAN 29{ 30 assert(channels != 0); 31 assert(output_width != 0); 32 33 const __mmask16 vblend_mask = _cvtu32_mask16(0xAAAA); 34 const __m512i vmultiplier = _mm512_broadcast_i32x4(_mm_load_si128((const __m128i*) params->sse2.multiplier)); 35 const __m512i vrounding = _mm512_broadcast_i32x4(_mm_load_si128((const __m128i*) params->sse2.rounding)); 36 const __m512i vremainder_mask = _mm512_broadcast_i32x4(_mm_load_si128((const __m128i*) params->sse2.remainder_mask)); 37 const __m512i vremainder_threshold = _mm512_broadcast_i32x4(_mm_load_si128((const __m128i*) params->sse2.remainder_threshold)); 38 const __m128i vshift = _mm_load_si128((const __m128i*) params->sse2.shift); 39 $if CHANNEL_TILE > 16: 40 const __m512i voutput_zero_point = _mm512_broadcast_i32x4(_mm_load_si128((const __m128i*) params->sse2.output_zero_point)); 41 const __m512i voutput_min = _mm512_broadcast_i32x4(_mm_load_si128((const __m128i*) params->sse2.output_min)); 42 const __m512i voutput_max = _mm512_broadcast_i32x4(_mm_load_si128((const __m128i*) params->sse2.output_max)); 43 const __m256i vpermute_mask = _mm256_set_epi32(7, 3, 5, 1, 6, 2, 4, 0); 44 $else: 45 const __m256i voutput_zero_point = _mm256_broadcastsi128_si256(_mm_load_si128((const __m128i*) params->sse2.output_zero_point)); 46 const __m256i voutput_min = _mm256_broadcastsi128_si256(_mm_load_si128((const __m128i*) params->sse2.output_min)); 47 const __m256i voutput_max = _mm256_broadcastsi128_si256(_mm_load_si128((const __m128i*) params->sse2.output_max)); 48 49 do { 50 $for K in range(KERNEL_TILE): 51 const int8_t* i${K} = input[${K}]; 52 assert(i${K} != NULL); 53 if XNN_UNPREDICTABLE(i${K} != zero) { 54 i${K} = (const int8_t*) ((uintptr_t) i${K} + input_offset); 55 } 56 input = (const int8_t**) ((uintptr_t) input + input_stride); 57 58 size_t c = channels; 59 const void* w = weights; 60 for (; c >= ${CHANNEL_TILE}; c -= ${CHANNEL_TILE}) { 61 __m512i vacc${ABC[0:16]} = _mm512_loadu_si512(w); 62 $for C in range(16, CHANNEL_TILE, 16): 63 __m512i vacc${ABC[C:C+16]} = _mm512_loadu_si512((const void*) ((uintptr_t) w + ${C} * sizeof(int32_t))); 64 65 $for K in range(KERNEL_TILE): 66 67 $for C in range(0, CHANNEL_TILE, 16): 68 $if C == 0: 69 const __m512i vi${K}x${ABC[0:16]} = _mm512_cvtepi8_epi32(_mm_loadu_si128((const __m128i*) i${K})); 70 $else: 71 const __m512i vi${K}x${ABC[C:C+16]} = _mm512_cvtepi8_epi32(_mm_loadu_si128((const __m128i*) (i${K} + ${C}))); 72 const __m512i vk${K}x${ABC[C:C+16]} = _mm512_cvtepi8_epi32(_mm_load_si128((const __m128i*) ((uintptr_t) w + ${CHANNEL_TILE} * sizeof(int32_t) + ${K * CHANNEL_TILE + C} * sizeof(int8_t)))); 73 i${K} += ${CHANNEL_TILE}; 74 75 $for C in range(0, CHANNEL_TILE, 16): 76 vacc${ABC[C:C+16]} = _mm512_add_epi32(vacc${ABC[C:C+16]}, _mm512_mullo_epi32(vi${K}x${ABC[C:C+16]}, vk${K}x${ABC[C:C+16]})); 77 78 w = (const void*) ((uintptr_t) w + ${CHANNEL_TILE} * sizeof(int32_t) + ${KERNEL_TILE * CHANNEL_TILE} * sizeof(int8_t)); 79 80 $for C in range(0, CHANNEL_TILE, 16): 81 const __m512i vacc${ABC[C+1:C+16:2]} = _mm512_shuffle_epi32(vacc${ABC[C:C+16]}, _MM_SHUFFLE(3, 3, 1, 1)); 82 83 $for C in range(0, CHANNEL_TILE, 16): 84 const __m512i vprod${ABC[C:C+16:2]} = _mm512_add_epi64(_mm512_mul_epi32(vacc${ABC[C:C+16]}, vmultiplier), vrounding); 85 const __m512i vprod${ABC[C+1:C+16:2]} = _mm512_add_epi64(_mm512_mul_epi32(vacc${ABC[C+1:C+16:2]}, vmultiplier), vrounding); 86 87 $for C in range(0, CHANNEL_TILE, 16): 88 const __m512i vq31prod${ABC[C:C+16:2]} = _mm512_srli_epi64(vprod${ABC[C:C+16:2]}, 31); 89 const __m512i vq31prod${ABC[C+1:C+16:2]} = _mm512_add_epi64(vprod${ABC[C+1:C+16:2]}, vprod${ABC[C+1:C+16:2]}); 90 91 $for C in range(0, CHANNEL_TILE, 16): 92 const __m512i vq31prod${ABC[C:C+16]} = _mm512_mask_blend_epi32(vblend_mask, vq31prod${ABC[C:C+16:2]}, vq31prod${ABC[C+1:C+16:2]}); 93 94 $for C in range(0, CHANNEL_TILE, 16): 95 const __m512i vrem${ABC[C:C+16]} = 96 _mm512_add_epi32(_mm512_and_epi32(vq31prod${ABC[C:C+16]}, vremainder_mask), _mm512_srai_epi32(vq31prod${ABC[C:C+16]}, 31)); 97 98 $for C in range(0, CHANNEL_TILE, 16): 99 vacc${ABC[C:C+16]} = _mm512_sra_epi32(vq31prod${ABC[C:C+16]}, vshift); 100 101 const __m512i vminus_one = _mm512_set1_epi32(-1); 102 $for C in range(0, CHANNEL_TILE, 16): 103 vacc${ABC[C:C+16]} = _mm512_mask_sub_epi32(vacc${ABC[C:C+16]}, _mm512_cmpgt_epi32_mask(vrem${ABC[C:C+16]}, vremainder_threshold), vacc${ABC[C:C+16]}, vminus_one); 104 105 $for C in range(0, CHANNEL_TILE, 16): 106 $if C + 16 < CHANNEL_TILE: 107 __m512i vout${ABC[C:C+4]}${ABC[C+16:C+20]}${ABC[C+4:C+8]}${ABC[C+20:C+24]}${ABC[C+8:C+12]}${ABC[C+24:C+28]}${ABC[C+12:C+16]}${ABC[C+28:C+32]} = _mm512_adds_epi16(_mm512_packs_epi32(vacc${ABC[C:C+16]}, vacc${ABC[C+16:C+32]}), voutput_zero_point); 108 $elif CHANNEL_TILE > 16: 109 __m256i vout${ABC[C:C+4]}${ABC[C+8:C+12]}${ABC[C+4:C+8]}${ABC[C+12:C+16]} = _mm256_adds_epi16(_mm256_packs_epi32(_mm512_castsi512_si256(vacc${ABC[C:C+16]}), _mm512_extracti32x8_epi32(vacc${ABC[C:C+16]}, 1)), _mm512_castsi512_si256(voutput_zero_point)); 110 $else: 111 __m256i vout${ABC[C:C+4]}${ABC[C+8:C+12]}${ABC[C+4:C+8]}${ABC[C+12:C+16]} = _mm256_adds_epi16(_mm256_packs_epi32(_mm512_castsi512_si256(vacc${ABC[C:C+16]}), _mm512_extracti32x8_epi32(vacc${ABC[C:C+16]}, 1)), voutput_zero_point); 112 113 $for C in range(0, CHANNEL_TILE, 16): 114 $if C + 16 < CHANNEL_TILE: 115 vout${ABC[C:C+4]}${ABC[C+16:C+20]}${ABC[C+4:C+8]}${ABC[C+20:C+24]}${ABC[C+8:C+12]}${ABC[C+24:C+28]}${ABC[C+12:C+16]}${ABC[C+28:C+32]} = _mm512_min_epi16(_mm512_max_epi16(vout${ABC[C:C+4]}${ABC[C+16:C+20]}${ABC[C+4:C+8]}${ABC[C+20:C+24]}${ABC[C+8:C+12]}${ABC[C+24:C+28]}${ABC[C+12:C+16]}${ABC[C+28:C+32]}, voutput_min), voutput_max); 116 $elif CHANNEL_TILE > 16: 117 vout${ABC[C:C+4]}${ABC[C+8:C+12]}${ABC[C+4:C+8]}${ABC[C+12:C+16]} = _mm256_min_epi16(_mm256_max_epi16(vout${ABC[C:C+4]}${ABC[C+8:C+12]}${ABC[C+4:C+8]}${ABC[C+12:C+16]}, _mm512_castsi512_si256(voutput_min)), _mm512_castsi512_si256(voutput_max)); 118 $else: 119 vout${ABC[C:C+4]}${ABC[C+8:C+12]}${ABC[C+4:C+8]}${ABC[C+12:C+16]} = _mm256_min_epi16(_mm256_max_epi16(vout${ABC[C:C+4]}${ABC[C+8:C+12]}${ABC[C+4:C+8]}${ABC[C+12:C+16]}, voutput_min), voutput_max); 120 121 $for C in range(0, CHANNEL_TILE, 16): 122 $if C + 16 < CHANNEL_TILE: 123 const __m256i vout${ABC[C:C+4]}${ABC[C+16:C+20]}${ABC[C+4:C+8]}${ABC[C+20:C+24]} = _mm512_castsi512_si256(vout${ABC[C:C+4]}${ABC[C+16:C+20]}${ABC[C+4:C+8]}${ABC[C+20:C+24]}${ABC[C+8:C+12]}${ABC[C+24:C+28]}${ABC[C+12:C+16]}${ABC[C+28:C+32]}); 124 const __m256i vout${ABC[C+8:C+12]}${ABC[C+24:C+28]}${ABC[C+12:C+16]}${ABC[C+28:C+32]} = _mm512_extracti32x8_epi32(vout${ABC[C:C+4]}${ABC[C+16:C+20]}${ABC[C+4:C+8]}${ABC[C+20:C+24]}${ABC[C+8:C+12]}${ABC[C+24:C+28]}${ABC[C+12:C+16]}${ABC[C+28:C+32]}, 1); 125 const __m256i vout${ABC[C:C+4]}${ABC[C+16:C+20]}${ABC[C+8:C+12]}${ABC[C+24:C+28]}${ABC[C+4:C+8]}${ABC[C+20:C+24]}${ABC[C+12:C+16]}${ABC[C+28:C+32]} = _mm256_packs_epi16(vout${ABC[C:C+4]}${ABC[C+16:C+20]}${ABC[C+4:C+8]}${ABC[C+20:C+24]}, vout${ABC[C+8:C+12]}${ABC[C+24:C+28]}${ABC[C+12:C+16]}${ABC[C+28:C+32]}); 126 __m256i vout${ABC[C:C+32]} = _mm256_permutevar8x32_epi32(vout${ABC[C:C+4]}${ABC[C+16:C+20]}${ABC[C+8:C+12]}${ABC[C+24:C+28]}${ABC[C+4:C+8]}${ABC[C+20:C+24]}${ABC[C+12:C+16]}${ABC[C+28:C+32]}, vpermute_mask); 127 $else: 128 const __m128i vout${ABC[C:C+4]}${ABC[C+8:C+12]} = _mm256_castsi256_si128(vout${ABC[C:C+4]}${ABC[C+8:C+12]}${ABC[C+4:C+8]}${ABC[C+12:C+16]}); 129 const __m128i vout${ABC[C+4:C+8]}${ABC[C+12:C+16]} = _mm256_extracti128_si256(vout${ABC[C:C+4]}${ABC[C+8:C+12]}${ABC[C+4:C+8]}${ABC[C+12:C+16]}, 1); 130 __m128i vout${ABC[C:C+16]} = _mm_shuffle_epi32(_mm_packs_epi16(vout${ABC[C:C+4]}${ABC[C+8:C+12]}, vout${ABC[C+4:C+8]}${ABC[C+12:C+16]}), _MM_SHUFFLE(3, 1, 2, 0)); 131 132 $if CHANNEL_TILE > 16: 133 _mm256_storeu_si256((__m256i*) output, vout${ABC[0:32]}); 134 $else: 135 _mm_storeu_si128((__m128i*) output, vout${ABC[0:16]}); 136 $for C in range(16, CHANNEL_TILE, 16): 137 $if C + 16 < CHANNEL_TILE: 138 _mm256_storeu_si256((__m256i*) (output + ${C}), vout${ABC[C:C+32]}); 139 $else: 140 _mm_storeu_si128((__m128i*) (output + ${C}), vout${ABC[C:C+16]}); 141 output += ${CHANNEL_TILE}; 142 } 143 if XNN_UNLIKELY(c != 0) { 144 // Prepare mask for valid 8-bit elements (depends on nc). 145 const __mmask16 vmask = _cvtu32_mask16((uint32_t) ((UINT32_C(1) << (c & 15)) - UINT32_C(1))); 146 $if CHANNEL_TILE > 16: 147 const int8_t* k = (const int8_t*) ((uintptr_t) w + ${CHANNEL_TILE} * sizeof(int32_t)); 148 ${"do " if CHANNEL_TILE > 16 else ""}{ 149 __m512i vacc${ABC[0:16]} = _mm512_loadu_si512(w); 150 151 $for K in range(KERNEL_TILE): 152 153 const __m512i vi${K}x${ABC[0:16]} = _mm512_cvtepi8_epi32(_mm_loadu_si128((const __m128i*) i${K})); 154 $if CHANNEL_TILE > 16: 155 $if K == 0: 156 const __m512i vk${K}x${ABC[0:16]} = _mm512_cvtepi8_epi32(_mm_loadu_si128((const __m128i*) k)); 157 $else: 158 const __m512i vk${K}x${ABC[0:16]} = _mm512_cvtepi8_epi32(_mm_loadu_si128((const __m128i*) (k + ${K * CHANNEL_TILE}))); 159 $else: 160 const __m512i vk${K}x${ABC[0:16]} = _mm512_cvtepi8_epi32(_mm_loadu_si128((const __m128i*) ((uintptr_t) w + ${CHANNEL_TILE} * sizeof(int32_t) + ${K * CHANNEL_TILE} * sizeof(int8_t)))); 161 $if CHANNEL_TILE > 16: 162 i${K} += 16; 163 164 vacc${ABC[0:16]} = _mm512_add_epi32(vacc${ABC[0:16]}, _mm512_mullo_epi32(vi${K}x${ABC[0:16]}, vk${K}x${ABC[0:16]})); 165 166 $if CHANNEL_TILE > 16: 167 w = (const void*) ((uintptr_t) w + 16 * sizeof(int32_t)); 168 k += 16; 169 170 const __m512i vacc${ABC[1:16:2]} = _mm512_shuffle_epi32(vacc${ABC[0:16]}, _MM_SHUFFLE(3, 3, 1, 1)); 171 172 const __m512i vprod${ABC[0:16:2]} = _mm512_add_epi64(_mm512_mul_epi32(vacc${ABC[0:16]}, vmultiplier), vrounding); 173 const __m512i vprod${ABC[1:16:2]} = _mm512_add_epi64(_mm512_mul_epi32(vacc${ABC[1:16:2]}, vmultiplier), vrounding); 174 175 const __m512i vq31prod${ABC[0:16:2]} = _mm512_srli_epi64(vprod${ABC[0:16:2]}, 31); 176 const __m512i vq31prod${ABC[1:16:2]} = _mm512_add_epi64(vprod${ABC[1:16:2]}, vprod${ABC[1:16:2]}); 177 178 const __m512i vq31prod${ABC[0:16]} = _mm512_mask_blend_epi32(vblend_mask, vq31prod${ABC[0:16:2]}, vq31prod${ABC[1:16:2]}); 179 180 const __m512i vrem${ABC[0:16]} = _mm512_add_epi32(_mm512_and_epi32(vq31prod${ABC[0:16]}, vremainder_mask), _mm512_srai_epi32(vq31prod${ABC[0:16]}, 31)); 181 182 vacc${ABC[0:16]} = _mm512_sra_epi32(vq31prod${ABC[0:16]}, vshift); 183 vacc${ABC[0:16]} = _mm512_mask_sub_epi32(vacc${ABC[0:16]}, _mm512_cmpgt_epi32_mask(vrem${ABC[0:16]}, vremainder_threshold), vacc${ABC[0:16]}, _mm512_set1_epi32(-1)); 184 185 $if CHANNEL_TILE > 16: 186 __m256i vout${ABC[0:4]}${ABC[8:12]}${ABC[4:8]}${ABC[12:16]} = _mm256_adds_epi16(_mm256_packs_epi32(_mm512_castsi512_si256(vacc${ABC[0:16]}), _mm512_extracti32x8_epi32(vacc${ABC[0:16]}, 1)), _mm512_castsi512_si256(voutput_zero_point)); 187 $else: 188 __m256i vout${ABC[0:4]}${ABC[8:12]}${ABC[4:8]}${ABC[12:16]} = _mm256_adds_epi16(_mm256_packs_epi32(_mm512_castsi512_si256(vacc${ABC[0:16]}), _mm512_extracti32x8_epi32(vacc${ABC[0:16]}, 1)), voutput_zero_point); 189 190 $if CHANNEL_TILE > 16: 191 vout${ABC[0:4]}${ABC[8:12]}${ABC[4:8]}${ABC[12:16]} = _mm256_min_epi16(_mm256_max_epi16(vout${ABC[0:4]}${ABC[8:12]}${ABC[4:8]}${ABC[12:16]}, _mm512_castsi512_si256(voutput_min)), _mm512_castsi512_si256(voutput_max)); 192 $else: 193 vout${ABC[0:4]}${ABC[8:12]}${ABC[4:8]}${ABC[12:16]} = _mm256_min_epi16(_mm256_max_epi16(vout${ABC[0:4]}${ABC[8:12]}${ABC[4:8]}${ABC[12:16]}, voutput_min), voutput_max); 194 195 const __m128i vout${ABC[0:4]}${ABC[8:12]} = _mm256_castsi256_si128(vout${ABC[0:4]}${ABC[8:12]}${ABC[4:8]}${ABC[12:16]}); 196 const __m128i vout${ABC[4:8]}${ABC[12:16]} = _mm256_extracti128_si256(vout${ABC[0:4]}${ABC[8:12]}${ABC[4:8]}${ABC[12:16]}, 1); 197 __m128i vout${ABC[0:16]} = _mm_shuffle_epi32(_mm_packs_epi16(vout${ABC[0:4]}${ABC[8:12]}, vout${ABC[4:8]}${ABC[12:16]}), _MM_SHUFFLE(3, 1, 2, 0)); 198 199 $if CHANNEL_TILE > 16: 200 if XNN_LIKELY(c >= 16) { 201 _mm_storeu_si128((__m128i*) output, vout${ABC[0:16]}); 202 output += 16; 203 c -= 16; 204 } else { 205 _mm_mask_storeu_epi8(output, vmask, vout${ABC[0:16]}); 206 output = (int8_t*) ((uintptr_t) output + c); 207 c = 0; 208 } 209 $else: 210 _mm_mask_storeu_epi8(output, vmask, vout${ABC[0:16]}); 211 output = (int8_t*) ((uintptr_t) output + c); 212 }${" while (c != 0);" if CHANNEL_TILE > 16 else ""} 213 } 214 215 output = (int8_t*) ((uintptr_t) output + output_increment); 216 } while (--output_width != 0); 217} 218