| // 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 == "FP32" |
| $assert DATATYPE in ["QC8", "QS8", "QU8"] |
| $assert VARIANT in ["LD64", "LD128"] |
| $assert MR <= 4 |
| #include <assert.h> |
| |
| #include <wasm_simd128.h> |
| |
| #include <xnnpack/igemm.h> |
| #include <xnnpack/math.h> |
| |
| |
| $PARAMS_STRUCT = REQUANTIZATION.lower() + "_wasmsimd" |
| $PARAMS_UNION = "xnn_%s_conv_minmax_params" % DATATYPE.lower() |
| $XINT8_T = "uint8_t" if DATATYPE == "QU8" else "int8_t" |
| $WASM_X16X8_LOAD8X8 = "wasm_u16x8_load8x8" if DATATYPE == "QU8" else "wasm_i16x8_load8x8" |
| $WASM_X8X16_NARROW_I16X8 = "wasm_u8x16_narrow_i16x8" if DATATYPE == "QU8" else "wasm_i8x16_narrow_i16x8" |
| $WASM_X8X16_MIN = "wasm_u8x16_min" if DATATYPE == "QU8" else "wasm_i8x16_min" |
| void xnn_${DATATYPE.lower()}_igemm_minmax_fp32_ukernel_${MR}x4c2s4__wasmsimd_dot16x2_${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_OOB_READS |
| { |
| 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); |
| |
| ${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}; |
| } |
| |
| kc = round_up_po2(kc, 8 * sizeof(${XINT8_T})); |
| do { |
| v128_t vacc0x0123 = wasm_v128_load(w); |
| $for M in range(1, MR): |
| v128_t 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}; |
| |
| $if DATATYPE == "QU8": |
| const v128_t vb_zero_point = wasm_v128_load64_splat(params->${PARAMS_STRUCT}.kernel_zero_point); |
| size_t k = kc; |
| do { |
| $for M in range(MR): |
| v128_t vxa${M} = ${WASM_X16X8_LOAD8X8}(a${M}); |
| a${M} += 8; |
| |
| $if VARIANT == "LD128": |
| $for K in range(0, 4, 2): |
| $if K == 0: |
| const v128_t vb${K}${K+1} = wasm_v128_load(w); |
| $else: |
| const v128_t vb${K}${K+1} = wasm_v128_load((const ${XINT8_T}*) w + ${K * 8}); |
| $if DATATYPE == "QU8": |
| const v128_t vxb${K} = wasm_i16x8_sub(wasm_u16x8_extend_low_u8x16(vb${K}${K+1}), vb_zero_point); |
| const v128_t vxb${K+1} = wasm_i16x8_sub(wasm_u16x8_extend_high_u8x16(vb${K}${K+1}), vb_zero_point); |
| $else: |
| const v128_t vxb${K} = wasm_i16x8_extend_low_i8x16(vb${K}${K+1}); |
| const v128_t vxb${K+1} = wasm_i16x8_extend_high_i8x16(vb${K}${K+1}); |
| |
| $for M in range(MR): |
| vacc${M}x0123 = wasm_i32x4_add(vacc${M}x0123, wasm_i32x4_dot_i16x8(vxa${M}, vxb${K})); |
| vxa${M} = wasm_v32x4_shuffle(vxa${M}, vxa${M}, 1, 2, 3, 4); |
| |
| $for M in range(MR): |
| vacc${M}x0123 = wasm_i32x4_add(vacc${M}x0123, wasm_i32x4_dot_i16x8(vxa${M}, vxb${K+1})); |
| $if K + 2 != 4: |
| vxa${M} = wasm_v32x4_shuffle(vxa${M}, vxa${M}, 1, 2, 3, 4); |
| $else: |
| $for K in range(4): |
| $if K == 0: |
| $if DATATYPE == "QU8": |
| const v128_t vxb${K} = wasm_i16x8_sub(wasm_u16x8_load8x8(w), vb_zero_point); |
| $else: |
| const v128_t vxb${K} = wasm_i16x8_load8x8(w); |
| $else: |
| $if DATATYPE == "QU8": |
| const v128_t vxb${K} = wasm_i16x8_sub(wasm_u16x8_load8x8((const ${XINT8_T}*) w + ${K * 8}), vb_zero_point); |
| $else: |
| const v128_t vxb${K} = wasm_i16x8_load8x8((const ${XINT8_T}*) w + ${K * 8}); |
| |
| $for M in range(MR): |
| vacc${M}x0123 = wasm_i32x4_add(vacc${M}x0123, wasm_i32x4_dot_i16x8(vxa${M}, vxb${K})); |
| $if K + 1 != 4: |
| vxa${M} = wasm_v32x4_shuffle(vxa${M}, vxa${M}, 1, 2, 3, 4); |
| |
| w = (const ${XINT8_T}*) w + 32; |
| k -= 8 * sizeof(${XINT8_T}); |
| } while (k != 0); |
| p -= ${MR} * sizeof(void*); |
| } while (p != 0); |
| |
| $for M in range(MR): |
| vacc${M}x0123 = wasm_f32x4_convert_i32x4(vacc${M}x0123); |
| |
| $if DATATYPE == "QC8": |
| const v128_t vscale0123 = wasm_v128_load(w); |
| w = (const void*) ((const float*) w + 4); |
| $for M in range(MR): |
| vacc${M}x0123 = wasm_f32x4_mul(vacc${M}x0123, vscale0123); |
| $else: |
| const v128_t vscale = wasm_v128_load64_splat(params->${PARAMS_STRUCT}.scale); |
| $for M in range(MR): |
| vacc${M}x0123 = wasm_f32x4_mul(vacc${M}x0123, vscale); |
| |
| const v128_t vmagic_bias = wasm_v128_load64_splat(params->${PARAMS_STRUCT}.magic_bias); |
| $for M in range(MR): |
| vacc${M}x0123 = wasm_f32x4_add(vacc${M}x0123, vmagic_bias); |
| |
| const v128_t vmagic_min = wasm_v128_load64_splat(params->${PARAMS_STRUCT}.magic_min); |
| $for M in range(MR): |
| vacc${M}x0123 = wasm_i32x4_max(vacc${M}x0123, vmagic_min); |
| |
| const v128_t vmagic_bias_less_output_zero_point = wasm_v128_load64_splat(params->${PARAMS_STRUCT}.magic_bias_less_output_zero_point); |
| $for M in range(MR): |
| vacc${M}x0123 = wasm_i32x4_sub(vacc${M}x0123, vmagic_bias_less_output_zero_point); |
| |
| $for M in range(0, MR, 2): |
| v128_t vacc${M}${min(M+1, MR-1)}x0123 = wasm_i16x8_narrow_i32x4(vacc${M}x0123, vacc${min(M+1, MR-1)}x0123); |
| |
| $if MR > 2: |
| v128_t vout = ${WASM_X8X16_NARROW_I16X8}(vacc0${min(1, MR-1)}x0123, vacc${min(2, MR-1)}${min(3, MR-1)}x0123); |
| $else: |
| v128_t vout = ${WASM_X8X16_NARROW_I16X8}(vacc0${min(1, MR-1)}x0123, vacc0${min(1, MR-1)}x0123); |
| |
| const v128_t voutput_max = wasm_v128_load64_splat(params->${PARAMS_STRUCT}.output_max); |
| vout = ${WASM_X8X16_MIN}(vout, voutput_max); |
| |
| if (nc >= 4) { |
| $for M in reversed(range(MR)): |
| *((float*) c${M}) = (float) wasm_f32x4_extract_lane(vout, ${M}); |
| |
| $for M in reversed(range(MR)): |
| c${M} = (${XINT8_T}*) ((uintptr_t) c${M} + cn_stride); |
| |
| a = (const ${XINT8_T}**restrict) ((uintptr_t) a - ks); |
| |
| nc -= 4; |
| } else { |
| $for M in reversed(range(MR)): |
| uint32_t vout${M} = wasm_i32x4_extract_lane(vout, ${M}); |
| if (nc & 2) { |
| $for M in reversed(range(MR)): |
| *((uint16_t*) c${M}) = (uint16_t) vout${M}; |
| vout${M} >>= 16; |
| c${M} += 2; |
| } |
| if (nc & 1) { |
| $for M in reversed(range(MR)): |
| *c${M} = (${XINT8_T}) vout${M}; |
| } |
| |
| nc = 0; |
| } |
| } while (nc != 0); |
| } |