| // Copyright 2021 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 REQUANTIZATION in ["FP32", "RNDNU"] |
| $assert DATATYPE in ["QC8", "QS8", "QU8"] |
| $assert DATATYPE != "QC8" or REQUANTIZATION == "FP32" |
| #include <assert.h> |
| $if REQUANTIZATION == "FP32": |
| $if VARIANT == "LRINT": |
| #include <math.h> |
| $elif VARIANT == "MAGIC": |
| |
| #include <fp16.h> |
| |
| #include <xnnpack/math.h> |
| #include <xnnpack/gemm.h> |
| |
| |
| $FUNCTION_SUFFIX = "scalar_" + VARIANT.lower() if VARIANT else "scalar" |
| $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() + "_") + "scalar" + ("_" + VARIANT.lower() if VARIANT else "") |
| $XINT8_T = "uint8_t" if DATATYPE == "QU8" else "int8_t" |
| void xnn_${DATATYPE.lower()}_gemm_minmax_${REQUANTIZATION.lower()}_ukernel_${MR}x${NR}__${FUNCTION_SUFFIX}( |
| size_t mr, |
| size_t nc, |
| size_t kc, |
| const ${XINT8_T}* restrict a, |
| size_t a_stride, |
| const void* restrict w, |
| ${XINT8_T}* restrict c, |
| size_t cm_stride, |
| size_t cn_stride, |
| const union ${PARAMS_UNION} params[restrict XNN_MIN_ELEMENTS(1)]) |
| { |
| assert(mr != 0); |
| assert(mr <= ${MR}); |
| assert(nc != 0); |
| assert(kc != 0); |
| |
| const ${XINT8_T}* a0 = a; |
| ${XINT8_T}* c0 = c; |
| $for M in range(1, MR): |
| const ${XINT8_T}* a${M} = (const ${XINT8_T}*) ((uintptr_t) a${M-1} + a_stride); |
| ${XINT8_T}* c${M} = (${XINT8_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}; |
| } |
| |
| $if DATATYPE == "QU8": |
| const int32_t vb_zero_point = params->${PARAMS_STRUCT}.kernel_zero_point; |
| do { |
| $for N in range(NR): |
| int32_t vacc0x${N} = ((const int32_t*) w)[${N}]; |
| $for M in range(1, MR): |
| $for N in range(NR): |
| int32_t vacc${M}x${N} = vacc0x${N}; |
| w = (const void*) ((const int32_t*) w + ${NR}); |
| |
| size_t k = kc; |
| do { |
| $for M in range(MR): |
| $if DATATYPE == "QU8": |
| const int32_t va${M} = (int32_t) (uint32_t) *a${M}++; |
| $else: |
| const int32_t va${M} = (int32_t) *a${M}++; |
| |
| $for N in range(NR): |
| $if DATATYPE == "QU8": |
| const int32_t vb${N} = (int32_t) (uint32_t) ((const uint8_t*) w)[${N}] - vb_zero_point; |
| $else: |
| const int32_t vb${N} = (int32_t) ((const int8_t*) w)[${N}]; |
| w = (const void*) ((const ${XINT8_T}*) w + ${NR}); |
| |
| $for M in range(MR): |
| $for N in range(NR): |
| vacc${M}x${N} += va${M} * vb${N}; |
| |
| k -= sizeof(${XINT8_T}); |
| } while (k != 0); |
| |
| $if REQUANTIZATION == "RNDNU": |
| const int32_t vmultiplier = params->${PARAMS_STRUCT}.multiplier; |
| $for M in range(MR): |
| $for N in range(NR): |
| const int64_t vproduct${M}x${N} = (int64_t) vacc${M}x${N} * (int64_t) vmultiplier; |
| |
| const uint32_t shift = params->${PARAMS_STRUCT}.shift; |
| const int64_t rounding = params->${PARAMS_STRUCT}.rounding; |
| $for M in range(MR): |
| $for N in range(NR): |
| int32_t vout${M}x${N} = (int32_t) asr_s64(vproduct${M}x${N} + rounding, shift); |
| |
| const int32_t voutput_min_less_zero_point = params->${PARAMS_STRUCT}.output_min_less_zero_point; |
| $for M in range(MR): |
| $for N in range(NR): |
| vout${M}x${N} = math_max_s32(vout${M}x${N}, voutput_min_less_zero_point); |
| |
| const int32_t voutput_max_less_zero_point = params->${PARAMS_STRUCT}.output_max_less_zero_point; |
| $for M in range(MR): |
| $for N in range(NR): |
| vout${M}x${N} = math_min_s32(vout${M}x${N}, voutput_max_less_zero_point); |
| |
| const int32_t voutput_zero_point = params->${PARAMS_STRUCT}.output_zero_point; |
| $for M in range(MR): |
| $for N in range(NR): |
| vout${M}x${N} += voutput_zero_point; |
| $elif REQUANTIZATION == "FP32": |
| $for M in range(MR): |
| $for N in range(NR): |
| float vfpacc${M}x${N} = (float) vacc${M}x${N}; |
| |
| $if DATATYPE == "QC8": |
| $if NR % 4 != 0: |
| typedef XNN_UNALIGNED float unaligned_float; |
| $for N in range(NR): |
| const float vscale${N} = ((const unaligned_float*) w)[${N}]; |
| $for M in range(MR): |
| vfpacc${M}x${N} *= vscale${N}; |
| $else: |
| $for N in range(NR): |
| const float vscale${N} = ((const float*) w)[${N}]; |
| $for M in range(MR): |
| vfpacc${M}x${N} *= vscale${N}; |
| w = (const void*) ((const float*) w + ${NR}); |
| $else: |
| const float vscale = params->${PARAMS_STRUCT}.scale; |
| $for M in range(MR): |
| $for N in range(NR): |
| vfpacc${M}x${N} *= vscale; |
| |
| $if VARIANT == "MAGIC": |
| const float voutput_min_less_zero_point = params->${PARAMS_STRUCT}.output_min_less_zero_point; |
| $for M in range(MR): |
| $for N in range(NR): |
| vfpacc${M}x${N} = math_max_f32(vfpacc${M}x${N}, voutput_min_less_zero_point); |
| |
| const float voutput_max_less_zero_point = params->${PARAMS_STRUCT}.output_max_less_zero_point; |
| $for M in range(MR): |
| $for N in range(NR): |
| vfpacc${M}x${N} = math_min_f32(vfpacc${M}x${N}, voutput_max_less_zero_point); |
| |
| const float vmagic_bias = params->${PARAMS_STRUCT}.magic_bias; |
| $for M in range(MR): |
| $for N in range(NR): |
| vfpacc${M}x${N} += vmagic_bias; |
| |
| const int32_t vmagic_bias_less_output_zero_point = params->${PARAMS_STRUCT}.magic_bias_less_output_zero_point; |
| $for M in range(MR): |
| $for N in range(NR): |
| int32_t vout${M}x${N} = (int32_t) fp32_to_bits(vfpacc${M}x${N}) - vmagic_bias_less_output_zero_point; |
| $elif VARIANT == "LRINT": |
| $for M in range(MR): |
| $for N in range(NR): |
| long vrndacc${M}x${N} = lrintf(vfpacc${M}x${N}); |
| |
| const long voutput_min_less_zero_point = params->${PARAMS_STRUCT}.output_min_less_zero_point; |
| $for M in range(MR): |
| $for N in range(NR): |
| vrndacc${M}x${N} = XNN_UNPREDICTABLE(vrndacc${M}x${N} < voutput_min_less_zero_point) ? voutput_min_less_zero_point : vrndacc${M}x${N}; |
| |
| const long voutput_max_less_zero_point = params->${PARAMS_STRUCT}.output_max_less_zero_point; |
| $for M in range(MR): |
| $for N in range(NR): |
| vrndacc${M}x${N} = XNN_UNPREDICTABLE(vrndacc${M}x${N} > voutput_max_less_zero_point) ? voutput_max_less_zero_point : vrndacc${M}x${N}; |
| |
| const int32_t voutput_zero_point = params->${PARAMS_STRUCT}.output_zero_point; |
| $for M in range(MR): |
| $for N in range(NR): |
| int32_t vout${M}x${N} = (int32_t) vrndacc${M}x${N} + voutput_zero_point; |
| |
| if XNN_LIKELY(nc >= ${NR}) { |
| $for M in range(MR): |
| $for N in range(NR): |
| c${M}[${N}] = (${XINT8_T}) vout${M}x${N}; |
| |
| $for M in range(MR): |
| a${M} = (const ${XINT8_T}*) ((uintptr_t) a${M} - kc); |
| |
| $for M in range(MR): |
| c${M} = (${XINT8_T}*) ((uintptr_t) c${M} + cn_stride); |
| |
| nc -= ${NR}; |
| } else { |
| $for LOG2N in reversed(range(NR.bit_length() - 1)): |
| if (nc & ${1 << LOG2N}) { |
| $for M in range(MR): |
| $for N in range(1 << LOG2N): |
| c${M}[${N}] = (${XINT8_T}) vout${M}x${N}; |
| $if LOG2N != 0: |
| $for N in range(1 << (LOG2N - 1)): |
| vout${M}x${N} = vout${M}x${N + (1 << LOG2N)}; |
| c${M} += ${1 << LOG2N}; |
| } |
| |
| nc = 0; |
| } |
| } while (nc != 0); |
| } |