// Copyright 2020 Google LLC // // This source code is licensed under the BSD-style license found in the // LICENSE file in the root directory of this source tree. $assert VARIANT in ["LD128", "EXTENDED"] $assert MR <= 4 #include #include #include #include #include $GEMM_SUFFIX = "_xw" if VARIANT == "EXTENDED" else "" void xnn_qs8_gemm${GEMM_SUFFIX}_minmax_ukernel_${MR}x8c8__avx2( size_t mr, size_t nc, size_t kc, const int8_t* restrict a, size_t a_stride, const void* restrict w, int8_t* restrict c, size_t cm_stride, size_t cn_stride, const union xnn_qs8_gemm${GEMM_SUFFIX}_params params[restrict XNN_MIN_ELEMENTS(1)]) XNN_DISABLE_TSAN { assert(mr != 0); assert(mr <= ${MR}); assert(nc != 0); assert(kc != 0); assert(kc % sizeof(int8_t) == 0); assert(a != NULL); assert(w != NULL); assert(c != NULL); kc = round_up_po2(kc, 8); const int8_t* a0 = a; int8_t* c0 = c; $for M in range(1, MR): const int8_t* a${M} = (const int8_t*) ((uintptr_t) a${M-1} + a_stride); int8_t* c${M} = (int8_t*) ((uintptr_t) c${M-1} + cm_stride); $if M % 2 == 0: if XNN_UNPREDICTABLE(mr <= ${M}) { a${M} = a${M-1}; c${M} = c${M-1}; } $elif M + 1 == MR: if XNN_UNPREDICTABLE(mr != ${M+1}) { a${M} = a${M-1}; c${M} = c${M-1}; } $else: if XNN_UNPREDICTABLE(mr < ${M+1}) { a${M} = a${M-1}; c${M} = c${M-1}; } do { const __m128i vbias0x0 = _mm_loadu_si32(w); const __m128i vbias0x1 = _mm_loadu_si32((const void*) ((uintptr_t) w + sizeof(int32_t))); __m256i vacc0x01 = _mm256_inserti128_si256(_mm256_castsi128_si256(vbias0x0), vbias0x1, 1); $for N in range(2, 8, 2): const __m128i vbias0x${N} = _mm_loadu_si32((const void*) ((uintptr_t) w + ${N} * sizeof(int32_t))); const __m128i vbias0x${N+1} = _mm_loadu_si32((const void*) ((uintptr_t) w + ${N+1} * sizeof(int32_t))); __m256i vacc0x${N}${N+1} = _mm256_inserti128_si256(_mm256_castsi128_si256(vbias0x${N}), vbias0x${N+1}, 1); $for M in range(1, MR): $for N in range(0, 8, 2): __m256i vacc${M}x${N}${N+1} = vacc0x${N}${N+1}; w = (const void*) ((uintptr_t) w + 8 * sizeof(int32_t)); size_t k = 0; while (k < kc) { $for M in range(MR): const __m128i va${M} = _mm_broadcastq_epi64(_mm_loadl_epi64((const __m128i*) a${M})); const __m256i vxa${M} = _mm256_cvtepi8_epi16(va${M}); a${M} += 8; $for N in range(0, 8, 2): $if VARIANT == "EXTENDED": $if N == 0: const __m256i vxb${N}${N+1} = _mm256_load_si256((const __m256i*) w); $else: const __m256i vxb${N}${N+1} = _mm256_load_si256((const __m256i*) ((uintptr_t) w + ${N * 8} * sizeof(int16_t))); $else: $if N == 0: const __m128i vb${N}${N+1} = _mm_load_si128((const __m128i*) w); $else: const __m128i vb${N}${N+1} = _mm_load_si128((const __m128i*) ((uintptr_t) w + ${N * 8} * sizeof(int8_t))); const __m256i vxb${N}${N+1} = _mm256_cvtepi8_epi16(vb${N}${N+1}); $for M in range(MR): vacc${M}x${N}${N+1} = _mm256_add_epi32(vacc${M}x${N}${N+1}, _mm256_madd_epi16(vxa${M}, vxb${N}${N+1})); $if VARIANT == "EXTENDED": w = (const void*) ((uintptr_t) w + 64 * sizeof(int16_t)); $else: w = (const void*) ((uintptr_t) w + 64 * sizeof(int8_t)); k += 8 * sizeof(int8_t); } $for M in range(MR): const __m256i vacc${M}x0213 = _mm256_hadd_epi32(vacc${M}x01, vacc${M}x23); const __m256i vacc${M}x4657 = _mm256_hadd_epi32(vacc${M}x45, vacc${M}x67); $for M in range(MR): const __m256i vacc${M}x02461357 = _mm256_hadd_epi32(vacc${M}x0213, vacc${M}x4657); const __m256i vpermute_mask = _mm256_set_epi32(7, 3, 6, 2, 5, 1, 4, 0); $for M in range(MR): __m256i vacc${M}x01234567 = _mm256_permutevar8x32_epi32(vacc${M}x02461357, vpermute_mask); const __m256i vmultiplier = _mm256_broadcastsi128_si256(_mm_load_si128((const __m128i*) params->sse2.multiplier)); const __m256i vrounding = _mm256_broadcastsi128_si256(_mm_load_si128((const __m128i*) params->sse2.rounding)); $for M in range(MR): const __m256i vacc${M}x11335577 = _mm256_shuffle_epi32(vacc${M}x01234567, _MM_SHUFFLE(3, 3, 1, 1)); $for M in range(MR): const __m256i vprod${M}x0246 = _mm256_add_epi64(_mm256_mul_epi32(vacc${M}x01234567, vmultiplier), vrounding); $for M in range(MR): const __m256i vprod${M}x1357 = _mm256_add_epi64(_mm256_mul_epi32(vacc${M}x11335577, vmultiplier), vrounding); $for M in range(MR): const __m256i vq31prod${M}x0246 = _mm256_srli_epi64(vprod${M}x0246, 31); const __m256i vq31prod${M}x1357 = _mm256_add_epi64(vprod${M}x1357, vprod${M}x1357); $for M in range(MR): const __m256i vq31prod${M}x01234567 = _mm256_blend_epi16(vq31prod${M}x0246, vq31prod${M}x1357, 0xCC); const __m256i vremainder_mask = _mm256_broadcastsi128_si256(_mm_load_si128((const __m128i*) params->sse2.remainder_mask)); $for M in range(MR): const __m256i vrem${M}x01234567 = _mm256_add_epi32(_mm256_and_si256(vq31prod${M}x01234567, vremainder_mask), _mm256_cmpgt_epi32(_mm256_setzero_si256(), vq31prod${M}x01234567)); const __m256i vremainder_threshold = _mm256_broadcastsi128_si256(_mm_load_si128((const __m128i*) params->sse2.remainder_threshold)); const __m128i vshift = _mm_load_si128((const __m128i*) params->sse2.shift); $for M in range(MR): vacc${M}x01234567 = _mm256_sub_epi32(_mm256_sra_epi32(vq31prod${M}x01234567, vshift), _mm256_cmpgt_epi32(vrem${M}x01234567, vremainder_threshold)); const __m256i voutput_zero_point = _mm256_broadcastsi128_si256(_mm_load_si128((const __m128i*) params->sse2.output_zero_point)); $for M in range(0, MR, 2): __m256i vacc${M}${min(M+1, MR-1)}x01234567 = _mm256_adds_epi16(_mm256_packs_epi32(vacc${M}x01234567, vacc${min(M+1, MR-1)}x01234567), voutput_zero_point); $for M in range(0, MR, 2): vacc${M}${min(M+1, MR-1)}x01234567 = _mm256_permute4x64_epi64(vacc${M}${min(M+1, MR-1)}x01234567, _MM_SHUFFLE(3, 1, 2, 0)); const __m256i voutput_min = _mm256_broadcastsi128_si256(_mm_load_si128((const __m128i*) params->sse2.output_min)); const __m256i voutput_max = _mm256_broadcastsi128_si256(_mm_load_si128((const __m128i*) params->sse2.output_max)); $for M in range(0, MR, 2): vacc${M}${min(M+1, MR-1)}x01234567 = _mm256_min_epi16(_mm256_max_epi16(vacc${M}${min(M+1, MR-1)}x01234567, voutput_min), voutput_max); $if MR > 2: __m256i vout = _mm256_packs_epi16(vacc0${min(1, MR-1)}x01234567, vacc${min(2, MR-1)}${min(3, MR-1)}x01234567); $else: __m256i vout = _mm256_packs_epi16(vacc0${min(1, MR-1)}x01234567, vacc0${min(1, MR-1)}x01234567); __m128i vout_lo = _mm256_castsi256_si128(vout); __m128i vout_hi = _mm256_extracti128_si256(vout, 1); if (nc >= 8) { _mm_storel_epi64((__m128i*) c0, vout_lo); $if MR > 1: _mm_storel_epi64((__m128i*) c1, vout_hi); $if MR > 2: _mm_storeh_pi((__m64*) c2, _mm_castsi128_ps(vout_lo)); $if MR > 3: _mm_storeh_pi((__m64*) c3, _mm_castsi128_ps(vout_hi)); $for M in range(MR): c${M} = (int8_t*) ((uintptr_t) c${M} + cn_stride); $for M in range(MR): a${M} = (const int8_t*) ((uintptr_t) a${M} - kc); nc -= 8; } else { if (nc & 4) { _mm_storeu_si32(c0, vout_lo); $if MR > 1: _mm_storeu_si32(c1, vout_hi); $if MR > 2: *((uint32_t*) c2) = (uint32_t) _mm_extract_epi32(vout_lo, 2); $if MR > 3: *((uint32_t*) c3) = (uint32_t) _mm_extract_epi32(vout_hi, 2); $for M in range(MR): c${M} += 4; vout_lo = _mm_srli_epi64(vout_lo, 32); vout_hi = _mm_srli_epi64(vout_hi, 32); } if (nc & 2) { *((uint16_t*) c0) = (uint16_t) _mm_extract_epi16(vout_lo, 0); $if MR > 1: *((uint16_t*) c1) = (uint16_t) _mm_extract_epi16(vout_hi, 0); $if MR > 2: *((uint16_t*) c2) = (uint16_t) _mm_extract_epi16(vout_lo, 4); $if MR > 3: *((uint16_t*) c3) = (uint16_t) _mm_extract_epi16(vout_hi, 4); $for M in range(MR): c${M} += 2; vout_lo = _mm_srli_epi32(vout_lo, 16); vout_hi = _mm_srli_epi32(vout_hi, 16); } if (nc & 1) { *c0 = (int8_t) _mm_extract_epi8(vout_lo, 0); $if MR > 1: *c1 = (uint8_t) _mm_extract_epi8(vout_hi, 0); $if MR > 2: *c2 = (uint8_t) _mm_extract_epi8(vout_lo, 8); $if MR > 3: *c3 = (uint8_t) _mm_extract_epi8(vout_hi, 8); } nc = 0; } } while (nc != 0); }