blob: 73e8b3863d94dfdc16c70d6535b19b8eb295e81d [file] [log] [blame]
// 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 SSE in [2, 3, 4]
$assert not XOP or AVX
$assert not AVX or SSE == 4
$assert REQUANTIZATION in ["GEMMLOWP", "FP32"]
$assert DATATYPE in ["QC8", "QS8", "QU8"]
$assert DATATYPE != "QC8" or REQUANTIZATION == "FP32"
$assert SSE != 3 or REQUANTIZATION != "FP32"
$assert VARIANT in ["LD64", "LD128"]
$assert MR <= 4
#include <assert.h>
$if XOP:
#if defined(__GNUC__) || defined(__clang__)
#include <x86intrin.h>
#else
#include <immintrin.h>
#include <ammintrin.h>
#endif
$else:
$SSE_HEADER = {2: "emmintrin.h", 3: "tmmintrin.h", 4: "smmintrin.h"}[SSE]
#include <${SSE_HEADER}>
#include <xnnpack/igemm.h>
#include <xnnpack/math.h>
$PARAMS_UNION = "xnn_qs8_minmax_params" if DATATYPE == "QC8" else "xnn_%s_conv_minmax_params" % DATATYPE.lower()
$PARAMS_STRUCT = ("" if DATATYPE == "QC8" else REQUANTIZATION.lower() + "_") + ("sse4" if SSE >= 4 and DATATYPE != "QU8" else "sse2")
$XINT8_T = "uint8_t" if DATATYPE == "QU8" else "int8_t"
$ISA = "xop" if XOP else "avx" if AVX else {2: "sse2", 3: "ssse3", 4: "sse41"}[SSE]
void xnn_${DATATYPE.lower()}_igemm_minmax_${REQUANTIZATION.lower()}_ukernel_${MR}x4c2__${ISA}_${VARIANT.lower()}(
size_t mr,
size_t nc,
size_t kc,
size_t ks,
const ${XINT8_T}** restrict a,
const void* restrict w,
${XINT8_T}* restrict c,
size_t cm_stride,
size_t cn_stride,
size_t a_offset,
const ${XINT8_T}* zero,
const union ${PARAMS_UNION} params[restrict XNN_MIN_ELEMENTS(1)]) XNN_DISABLE_TSAN XNN_DISABLE_MSAN
{
assert(mr != 0);
assert(mr <= ${MR});
assert(nc != 0);
assert(kc != 0);
assert(ks != 0);
assert(ks % (${MR} * sizeof(void*)) == 0);
assert(a_offset % sizeof(${XINT8_T}) == 0);
assert(a != NULL);
assert(w != NULL);
assert(c != NULL);
kc = round_up_po2(kc, 2);
${XINT8_T}* c0 = c;
$for M in range(1, MR):
${XINT8_T}* c${M} = (${XINT8_T}*) ((uintptr_t) c${M-1} + cm_stride);
$if M % 2 == 0:
if XNN_UNPREDICTABLE(mr <= ${M}) {
c${M} = c${M-1};
}
$elif M + 1 == MR:
if XNN_UNPREDICTABLE(mr != ${M+1}) {
c${M} = c${M-1};
}
$else:
if XNN_UNPREDICTABLE(mr < ${M+1}) {
c${M} = c${M-1};
}
do {
__m128i vacc0x0123 = _mm_loadu_si128((const __m128i*) w);
$for M in range(1, MR):
__m128i vacc${M}x0123 = vacc0x0123;
w = (const void*) ((const int32_t*) w + 4);
size_t p = ks;
do {
$for M in range(MR):
const ${XINT8_T}* restrict a${M} = a[${M}];
if XNN_UNPREDICTABLE(a${M} != zero) {
a${M} = (const ${XINT8_T}*) ((uintptr_t) a${M} + a_offset);
}
a += ${MR};
size_t k = kc;
$if DATATYPE == "QU8":
const __m128i vb_zero_point = _mm_load_si128((const __m128i*) params->${PARAMS_STRUCT}.kernel_zero_point);
$if SSE < 4 or VARIANT == "LD128":
const __m128i vzero = _mm_setzero_si128();
while (k >= 8 * sizeof(${XINT8_T})) {
$for M in range(MR):
const __m128i va${M} = _mm_loadl_epi64((const __m128i*) a${M});
$if DATATYPE == "QU8":
$if SSE == 4:
const __m128i vxa${M} = _mm_cvtepu8_epi16(va${M});
$else:
const __m128i vxa${M} = _mm_unpacklo_epi8(va${M}, vzero);
$else:
$if SSE == 4:
const __m128i vxa${M} = _mm_cvtepi8_epi16(va${M});
$else:
const __m128i vxa${M} = _mm_srai_epi16(_mm_unpacklo_epi8(va${M}, va${M}), 8);
a${M} += 8;
$if VARIANT == "LD128":
$for K in range(0, 4, 2):
$if K == 0:
const __m128i vb${K}${K+1} = _mm_loadu_si128((const __m128i*) w);
$else:
const __m128i vb${K}${K+1} = _mm_loadu_si128((const __m128i*) ((const ${XINT8_T}*) w + ${K * 8}));
$if DATATYPE == "QU8":
const __m128i vxb${K} = _mm_sub_epi16(_mm_unpacklo_epi8(vb${K}${K+1}, vzero), vb_zero_point);
const __m128i vxb${K+1} = _mm_sub_epi16(_mm_unpackhi_epi8(vb${K}${K+1}, vzero), vb_zero_point);
$elif SSE == 4:
const __m128i vxb${K} = _mm_cvtepi8_epi16(vb${K}${K+1});
const __m128i vxb${K+1} = _mm_srai_epi16(_mm_unpackhi_epi8(vb${K}${K+1}, vb${K}${K+1}), 8);
$else:
const __m128i vsb${K}${K+1} = _mm_cmpgt_epi8(_mm_setzero_si128(), vb${K}${K+1});
const __m128i vxb${K} = _mm_unpacklo_epi8(vb${K}${K+1}, vsb${K}${K+1});
const __m128i vxb${K+1} = _mm_unpackhi_epi8(vb${K}${K+1}, vsb${K}${K+1});
$for M in range(MR):
$if XOP:
vacc${M}x0123 = _mm_maddd_epi16(
_mm_shuffle_epi32(vxa${M}, _MM_SHUFFLE(${K}, ${K}, ${K}, ${K})), vxb${K}, vacc${M}x0123);
$else:
vacc${M}x0123 = _mm_add_epi32(vacc${M}x0123,
_mm_madd_epi16(_mm_shuffle_epi32(vxa${M}, _MM_SHUFFLE(${K}, ${K}, ${K}, ${K})), vxb${K}));
$for M in range(MR):
$if XOP:
vacc${M}x0123 = _mm_maddd_epi16(
_mm_shuffle_epi32(vxa${M}, _MM_SHUFFLE(${K+1}, ${K+1}, ${K+1}, ${K+1})), vxb${K+1}, vacc${M}x0123);
$else:
vacc${M}x0123 = _mm_add_epi32(vacc${M}x0123,
_mm_madd_epi16(_mm_shuffle_epi32(vxa${M}, _MM_SHUFFLE(${K+1}, ${K+1}, ${K+1}, ${K+1})), vxb${K+1}));
$else:
$for K in range(4):
$if K == 0:
const __m128i vb${K} = _mm_loadl_epi64((const __m128i*) w);
$else:
const __m128i vb${K} = _mm_loadl_epi64((const __m128i*) ((const ${XINT8_T}*) w + ${K * 8}));
$if DATATYPE == "QU8":
$if SSE == 4:
const __m128i vxb${K} = _mm_sub_epi16(_mm_cvtepu8_epi16(vb${K}), vb_zero_point);
$else:
const __m128i vxb${K} = _mm_sub_epi16(_mm_unpacklo_epi8(vb${K}, vzero), vb_zero_point);
$else:
$if SSE == 4:
const __m128i vxb${K} = _mm_cvtepi8_epi16(vb${K});
$else:
const __m128i vxb${K} = _mm_srai_epi16(_mm_unpacklo_epi8(vb${K}, vb${K}), 8);
$for M in range(MR):
$if XOP:
vacc${M}x0123 = _mm_maddd_epi16(
_mm_shuffle_epi32(vxa${M}, _MM_SHUFFLE(${K}, ${K}, ${K}, ${K})), vxb${K}, vacc${M}x0123);
$else:
vacc${M}x0123 = _mm_add_epi32(vacc${M}x0123,
_mm_madd_epi16(_mm_shuffle_epi32(vxa${M}, _MM_SHUFFLE(${K}, ${K}, ${K}, ${K})), vxb${K}));
w = (const void*) ((const ${XINT8_T}*) w + 32);
k -= 8 * sizeof(${XINT8_T});
}
if (k != 0) {
$for M in range(MR):
const __m128i va${M} = _mm_loadl_epi64((const __m128i*) a${M});
$if DATATYPE == "QU8":
$if SSE == 4:
const __m128i vxa${M} = _mm_cvtepu8_epi16(va${M});
$else:
const __m128i vxa${M} = _mm_unpacklo_epi8(va${M}, vzero);
$else:
$if SSE == 4:
const __m128i vxa${M} = _mm_cvtepi8_epi16(va${M});
$else:
const __m128i vxa${M} = _mm_srai_epi16(_mm_unpacklo_epi8(va${M}, va${M}), 8);
a${M} = (const ${XINT8_T}*) ((uintptr_t) a${M} + k);
const __m128i vb0 = _mm_loadl_epi64((const __m128i*) w);
w = (const void*) ((const ${XINT8_T}*) w + 8);
$if DATATYPE == "QU8":
$if SSE == 4:
const __m128i vxb0 = _mm_sub_epi16(_mm_cvtepu8_epi16(vb0), vb_zero_point);
$else:
const __m128i vxb0 = _mm_sub_epi16(_mm_unpacklo_epi8(vb0, vzero), vb_zero_point);
$else:
$if SSE == 4:
const __m128i vxb0 = _mm_cvtepi8_epi16(vb0);
$else:
const __m128i vxb0 = _mm_srai_epi16(_mm_unpacklo_epi8(vb0, vb0), 8);
$for M in range(MR):
$if XOP:
vacc${M}x0123 = _mm_maddd_epi16(
_mm_shuffle_epi32(vxa${M}, _MM_SHUFFLE(0, 0, 0, 0)), vxb0, vacc${M}x0123);
$else:
vacc${M}x0123 = _mm_add_epi32(vacc${M}x0123,
_mm_madd_epi16(_mm_shuffle_epi32(vxa${M}, _MM_SHUFFLE(0, 0, 0, 0)), vxb0));
if (k > 2 * sizeof(${XINT8_T})) {
const __m128i vb1 = _mm_loadl_epi64((const __m128i*) w);
w = (const void*) ((const ${XINT8_T}*) w + 8);
$if DATATYPE == "QU8":
$if SSE == 4:
const __m128i vxb1 = _mm_sub_epi16(_mm_cvtepu8_epi16(vb1), vb_zero_point);
$else:
const __m128i vxb1 = _mm_sub_epi16(_mm_unpacklo_epi8(vb1, vzero), vb_zero_point);
$else:
$if SSE == 4:
const __m128i vxb1 = _mm_cvtepi8_epi16(vb1);
$else:
const __m128i vxb1 = _mm_srai_epi16(_mm_unpacklo_epi8(vb1, vb1), 8);
$for M in range(MR):
$if XOP:
vacc${M}x0123 = _mm_maddd_epi16(
_mm_shuffle_epi32(vxa${M}, _MM_SHUFFLE(1, 1, 1, 1)), vxb1, vacc${M}x0123);
$else:
vacc${M}x0123 = _mm_add_epi32(vacc${M}x0123,
_mm_madd_epi16(_mm_shuffle_epi32(vxa${M}, _MM_SHUFFLE(1, 1, 1, 1)), vxb1));
if (k > 4 * sizeof(${XINT8_T})) {
const __m128i vb2 = _mm_loadl_epi64((const __m128i*) w);
w = (const void*) ((const ${XINT8_T}*) w + 8);
$if DATATYPE == "QU8":
$if SSE == 4:
const __m128i vxb2 = _mm_sub_epi16(_mm_cvtepu8_epi16(vb2), vb_zero_point);
$else:
const __m128i vxb2 = _mm_sub_epi16(_mm_unpacklo_epi8(vb2, vzero), vb_zero_point);
$else:
$if SSE == 4:
const __m128i vxb2 = _mm_cvtepi8_epi16(vb2);
$else:
const __m128i vxb2 = _mm_srai_epi16(_mm_unpacklo_epi8(vb2, vb2), 8);
$for M in range(MR):
$if XOP:
vacc${M}x0123 = _mm_maddd_epi16(
_mm_shuffle_epi32(vxa${M}, _MM_SHUFFLE(2, 2, 2, 2)), vxb2, vacc${M}x0123);
$else:
vacc${M}x0123 = _mm_add_epi32(vacc${M}x0123,
_mm_madd_epi16(_mm_shuffle_epi32(vxa${M}, _MM_SHUFFLE(2, 2, 2, 2)), vxb2));
}
}
}
p -= ${MR} * sizeof(void*);
} while (p != 0);
$if REQUANTIZATION == "GEMMLOWP":
const __m128i vmultiplier = _mm_load_si128((const __m128i*) params->${PARAMS_STRUCT}.multiplier);
const __m128i vrounding = _mm_load_si128((const __m128i*) params->${PARAMS_STRUCT}.rounding);
$if SSE == 4:
$for M in range(MR):
const __m128i vacc${M}x1133 = _mm_shuffle_epi32(vacc${M}x0123, _MM_SHUFFLE(3, 3, 1, 1));
$for M in range(MR):
const __m128i vprod${M}x02 = _mm_add_epi64(_mm_mul_epi32(vacc${M}x0123, vmultiplier), vrounding);
$for M in range(MR):
const __m128i vprod${M}x13 = _mm_add_epi64(_mm_mul_epi32(vacc${M}x1133, vmultiplier), vrounding);
$for M in range(MR):
const __m128i vq31prod${M}x02 = _mm_srli_epi64(vprod${M}x02, 31);
const __m128i vq31prod${M}x13 = _mm_add_epi64(vprod${M}x13, vprod${M}x13);
$for M in range(MR):
const __m128i vq31prod${M}x0123 = _mm_blend_epi16(vq31prod${M}x02, vq31prod${M}x13, 0xCC);
$else:
$for M in range(MR):
const __m128i vnmask${M}x0123 = _mm_cmpgt_epi32(_mm_setzero_si128(), vacc${M}x0123);
$for M in range(MR):
$if SSE >= 3:
const __m128i vabsacc${M}x0123 = _mm_abs_epi32(vacc${M}x0123);
$else:
const __m128i vabsacc${M}x0123 = _mm_sub_epi32(_mm_xor_si128(vacc${M}x0123, vnmask${M}x0123), vnmask${M}x0123);
$for M in range(MR):
const __m128i vabsacc${M}x1133 = _mm_shuffle_epi32(vabsacc${M}x0123, _MM_SHUFFLE(3, 3, 1, 1));
$for M in range(MR):
const __m128i vabsprod${M}x02 = _mm_mul_epu32(vabsacc${M}x0123, vmultiplier);
$for M in range(MR):
const __m128i vnmask${M}x02 = _mm_shuffle_epi32(vnmask${M}x0123, _MM_SHUFFLE(2, 2, 0, 0));
$for M in range(MR):
const __m128i vprod${M}x02 = _mm_sub_epi64(_mm_xor_si128(vabsprod${M}x02, vnmask${M}x02), vnmask${M}x02);
$for M in range(MR):
const __m128i vq31prod${M}x02 = _mm_srli_epi64(_mm_add_epi64(vprod${M}x02, vrounding), 31);
$for M in range(MR):
const __m128i vabsprod${M}x13 = _mm_mul_epu32(vabsacc${M}x1133, vmultiplier);
$for M in range(MR):
const __m128i vnmask${M}x13 = _mm_shuffle_epi32(vnmask${M}x0123, _MM_SHUFFLE(3, 3, 1, 1));
$for M in range(MR):
const __m128i vprod${M}x13 = _mm_sub_epi64(_mm_xor_si128(vabsprod${M}x13, vnmask${M}x13), vnmask${M}x13);
$for M in range(MR):
const __m128i vq31prod${M}x13 = _mm_srli_epi64(_mm_add_epi64(vprod${M}x13, vrounding), 31);
$for M in range(MR):
const __m128i vq31prod${M}x0213 = _mm_castps_si128(_mm_shuffle_ps(
_mm_castsi128_ps(vq31prod${M}x02), _mm_castsi128_ps(vq31prod${M}x13), _MM_SHUFFLE(2, 0, 2, 0)));
$for M in range(MR):
const __m128i vq31prod${M}x0123 = _mm_shuffle_epi32(vq31prod${M}x0213, _MM_SHUFFLE(3, 1, 2, 0));
const __m128i vremainder_mask = _mm_load_si128((const __m128i*) params->${PARAMS_STRUCT}.remainder_mask);
$for M in range(MR):
const __m128i vrem${M}x0123 =
_mm_add_epi32(_mm_and_si128(vq31prod${M}x0123, vremainder_mask), _mm_cmpgt_epi32(_mm_setzero_si128(), vq31prod${M}x0123));
const __m128i vremainder_threshold = _mm_load_si128((const __m128i*) params->${PARAMS_STRUCT}.remainder_threshold);
$if M > 1:
const __m128i vshift = _mm_loadl_epi64((const __m128i*) params->${PARAMS_STRUCT}.shift);
$else:
const __m128i vshift = _mm_load_si128((const __m128i*) params->${PARAMS_STRUCT}.shift);
$for M in range(MR):
vacc${M}x0123 =
_mm_sub_epi32(_mm_sra_epi32(vq31prod${M}x0123, vshift), _mm_cmpgt_epi32(vrem${M}x0123, vremainder_threshold));
$elif REQUANTIZATION == "FP32":
$for M in range(MR):
__m128 vscaled${M}x0123 = _mm_cvtepi32_ps(vacc${M}x0123);
$if DATATYPE == "QC8":
const __m128 vscale0123 = _mm_loadu_ps((const float*) w);
w = (const void*) ((const float*) w + 4);
$for M in range(MR):
vscaled${M}x0123 = _mm_mul_ps(vscaled${M}x0123, vscale0123);
$else:
const __m128 vscale = _mm_load_ps(params->${PARAMS_STRUCT}.scale);
$for M in range(MR):
vscaled${M}x0123 = _mm_mul_ps(vscaled${M}x0123, vscale);
$for M in range(MR):
vacc${M}x0123 = _mm_cvtps_epi32(vscaled${M}x0123);
const __m128i voutput_zero_point = _mm_load_si128((const __m128i*) params->${PARAMS_STRUCT}.output_zero_point);
$for M in range(0, MR, 2):
__m128i vacc${M}${min(M+1, MR-1)}x0123 = _mm_adds_epi16(_mm_packs_epi32(vacc${M}x0123, vacc${min(M+1, MR-1)}x0123), voutput_zero_point);
$if DATATYPE == "QU8":
$if MR > 2:
__m128i vout = _mm_packus_epi16(vacc0${min(1, MR-1)}x0123, vacc${min(2, MR-1)}${min(3, MR-1)}x0123);
$else:
__m128i vout = _mm_packus_epi16(vacc0${min(1, MR-1)}x0123, vacc0${min(1, MR-1)}x0123);
vout = _mm_max_epu8(vout, _mm_load_si128((const __m128i*) params->${PARAMS_STRUCT}.output_min));
vout = _mm_min_epu8(vout, _mm_load_si128((const __m128i*) params->${PARAMS_STRUCT}.output_max));
$else:
$if SSE < 4:
const __m128i voutput_min = _mm_load_si128((const __m128i*) params->${PARAMS_STRUCT}.output_min);
const __m128i voutput_max = _mm_load_si128((const __m128i*) params->${PARAMS_STRUCT}.output_max);
$for M in range(0, MR, 2):
vacc${M}${min(M+1, MR-1)}x0123 = _mm_min_epi16(_mm_max_epi16(vacc${M}${min(M+1, MR-1)}x0123, voutput_min), voutput_max);
$if MR > 2:
__m128i vout = _mm_packs_epi16(vacc0${min(1, MR-1)}x0123, vacc${min(2, MR-1)}${min(3, MR-1)}x0123);
$else:
__m128i vout = _mm_packs_epi16(vacc0${min(1, MR-1)}x0123, vacc0${min(1, MR-1)}x0123);
$if SSE == 4:
vout = _mm_max_epi8(vout, _mm_load_si128((const __m128i*) params->${PARAMS_STRUCT}.output_min));
vout = _mm_min_epi8(vout, _mm_load_si128((const __m128i*) params->${PARAMS_STRUCT}.output_max));
if (nc >= 4) {
$for M in reversed(range(1, MR)):
$if SSE == 4:
*((uint32_t*) c${M}) = (uint32_t) _mm_extract_epi32(vout, ${M});
$else:
*((uint32_t*) c${M}) = (uint32_t) _mm_cvtsi128_si32(_mm_shuffle_epi32(vout, _MM_SHUFFLE(${M}, ${M}, ${M}, ${M})));
c${M} = (${XINT8_T}*) ((uintptr_t) c${M} + cn_stride);
*((uint32_t*) c0) = (uint32_t) _mm_cvtsi128_si32(vout);
c0 = (${XINT8_T}*) ((uintptr_t) c0 + cn_stride);
a = (const ${XINT8_T}**restrict) ((uintptr_t) a - ks);
nc -= 4;
} else {
if (nc & 2) {
$for M in reversed(range(MR)):
*((uint16_t*) c${M}) = (uint16_t) _mm_extract_epi16(vout, ${M * 2});
c${M} += 2;
vout = _mm_srli_epi32(vout, 16);
}
if (nc & 1) {
$if SSE == 4:
$for M in reversed(range(MR)):
*c${M} = (${XINT8_T}) _mm_extract_epi8(vout, ${M * 4});
$else:
$for M in reversed(range(1, MR)):
*c${M} = (${XINT8_T}) _mm_extract_epi16(vout, ${M * 2});
*c0 = (${XINT8_T}) _mm_cvtsi128_si32(vout);
}
nc = 0;
}
} while (nc != 0);
}