Marat Dukhan | 1bbf96b | 2020-06-15 23:01:20 -0700 | [diff] [blame] | 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 | $assert NR % 4 == 0 |
| 7 | $ABC = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ" |
| 8 | #include <assert.h> |
| 9 | |
| 10 | #include <wasm_simd128.h> |
| 11 | |
| 12 | #include <xnnpack/gemm.h> |
| 13 | |
| 14 | |
Marat Dukhan | 802808c | 2020-06-16 11:01:17 -0700 | [diff] [blame^] | 15 | $assert ACTIVATION in ["LINEAR", "RELU", "MINMAX"] |
| 16 | $ACTIVATION_SUFFIX = {"LINEAR": ""}.get(ACTIVATION, "_" + ACTIVATION.lower()) |
| 17 | $ARCH_SUFFIX = "" if ACTIVATION == "LINEAR" else "_x86" if X86 else "_arm" |
| 18 | $PARAMS = {"LINEAR": "xnn_f32_default_params", "RELU": "xnn_f32_relu_params", "MINMAX": "xnn_f32_minmax_params"}[ACTIVATION] |
| 19 | void xnn_f32_gemm${"inc" if INC else ""}${ACTIVATION_SUFFIX}_ukernel_${MR}x${NR}s4__wasmsimd${ARCH_SUFFIX}( |
Marat Dukhan | 1bbf96b | 2020-06-15 23:01:20 -0700 | [diff] [blame] | 20 | size_t mr, |
| 21 | size_t nc, |
| 22 | size_t kc, |
| 23 | const float*restrict a, |
| 24 | size_t a_stride, |
| 25 | const float*restrict w, |
| 26 | float*restrict c, |
| 27 | size_t cm_stride, |
| 28 | size_t cn_stride, |
| 29 | $if INC: |
| 30 | const float*restrict acc, |
Marat Dukhan | 802808c | 2020-06-16 11:01:17 -0700 | [diff] [blame^] | 31 | const union ${PARAMS} params[restrict XNN_MIN_ELEMENTS(1)]) |
Marat Dukhan | 1bbf96b | 2020-06-15 23:01:20 -0700 | [diff] [blame] | 32 | { |
| 33 | assert(mr != 0); |
| 34 | assert(mr <= ${MR}); |
| 35 | assert(nc != 0); |
| 36 | assert(kc != 0); |
| 37 | assert(kc % sizeof(float) == 0); |
| 38 | assert(a != NULL); |
| 39 | assert(w != NULL); |
| 40 | assert(c != NULL); |
| 41 | $if INC: |
| 42 | assert(acc != NULL); |
| 43 | |
| 44 | const float* a0 = a; |
| 45 | float* c0 = c; |
| 46 | $for M in range(1, MR): |
| 47 | const float* a${M} = (const float*) ((uintptr_t) a${M-1} + a_stride); |
| 48 | float* c${M} = (float*) ((uintptr_t) c${M-1} + cm_stride); |
| 49 | $if M % 2 == 0: |
| 50 | if XNN_UNPREDICTABLE(mr <= ${M}) { |
| 51 | a${M} = a${M-1}; |
| 52 | c${M} = c${M-1}; |
| 53 | } |
| 54 | $elif M + 1 == MR: |
| 55 | if XNN_UNPREDICTABLE(mr != ${M+1}) { |
| 56 | a${M} = a${M-1}; |
| 57 | c${M} = c${M-1}; |
| 58 | } |
| 59 | $else: |
| 60 | if XNN_UNPREDICTABLE(mr < ${M+1}) { |
| 61 | a${M} = a${M-1}; |
| 62 | c${M} = c${M-1}; |
| 63 | } |
| 64 | |
Marat Dukhan | 802808c | 2020-06-16 11:01:17 -0700 | [diff] [blame^] | 65 | $if ACTIVATION == "MINMAX" and not X86: |
| 66 | const v128_t vmin = wasm_v32x4_load_splat(¶ms->scalar.min); |
| 67 | const v128_t vmax = wasm_v32x4_load_splat(¶ms->scalar.max); |
Marat Dukhan | 1bbf96b | 2020-06-15 23:01:20 -0700 | [diff] [blame] | 68 | do { |
| 69 | $if INC: |
| 70 | $for M in range(MR): |
| 71 | $for N in range(0, NR, 4): |
| 72 | v128_t vacc${M}x${ABC[N:N+4]} = wasm_v128_load(acc + ${M*NR+N}); |
| 73 | acc += ${MR*NR}; |
| 74 | $else: |
| 75 | $for N in range(0, NR, 4): |
| 76 | v128_t vacc0x${ABC[N:N+4]} = wasm_v128_load(w + ${N}); |
| 77 | $for M in range(1, MR): |
| 78 | $for N in range(0, NR, 4): |
| 79 | v128_t vacc${M}x${ABC[N:N+4]} = vacc0x${ABC[N:N+4]}; |
| 80 | w += ${NR}; |
| 81 | |
| 82 | size_t k = kc; |
| 83 | while (k >= 4 * sizeof(float)) { |
| 84 | $for M in range(MR): |
| 85 | v128_t va${M} = wasm_v128_load(a${M}); |
| 86 | a${M} += 4; |
| 87 | |
| 88 | $for L in range(4): |
| 89 | |
| 90 | $for N in range(0, NR, 4): |
| 91 | const v128_t vb${ABC[N:N+4]}c${L} = wasm_v128_load(w + ${L * NR + N}); |
| 92 | |
| 93 | $for N in range(0, NR, 4): |
| 94 | $for M in range(MR): |
| 95 | vacc${M}x${ABC[N:N+4]} = wasm_f32x4_add(vacc${M}x${ABC[N:N+4]}, wasm_f32x4_mul(va${M}, vb${ABC[N:N+4]}c${L})); |
| 96 | |
| 97 | $if L + 1 != 4: |
| 98 | $for M in range(MR): |
| 99 | va${M} = wasm_v32x4_shuffle(va${M}, va${M}, 1, 2, 3, 0); |
| 100 | |
| 101 | w += ${4 * NR}; |
| 102 | k -= 4 * sizeof(float); |
| 103 | } |
| 104 | if XNN_UNLIKELY(k != 0) { |
| 105 | do { |
| 106 | $for M in range(MR): |
| 107 | const v128_t va${M} = wasm_v32x4_load_splat(a${M}); |
| 108 | a${M} += 1; |
| 109 | |
| 110 | const v128_t vb${ABC[0:4]} = wasm_v128_load(w); |
| 111 | $for N in range(4, NR, 4): |
| 112 | const v128_t vb${ABC[N:N+4]} = wasm_v128_load(w + ${N}); |
| 113 | w += ${NR}; |
| 114 | |
| 115 | $for N in range(0, NR, 4): |
| 116 | $for M in range(MR): |
| 117 | vacc${M}x${ABC[N:N+4]} = wasm_f32x4_add(vacc${M}x${ABC[N:N+4]}, wasm_f32x4_mul(va${M}, vb${ABC[N:N+4]})); |
| 118 | |
| 119 | k -= sizeof(float); |
| 120 | } while (k != 0); |
| 121 | } |
| 122 | |
Marat Dukhan | 802808c | 2020-06-16 11:01:17 -0700 | [diff] [blame^] | 123 | $if ACTIVATION == "MINMAX": |
| 124 | $if X86: |
| 125 | const v128_t vmin = wasm_v32x4_load_splat(¶ms->scalar.min); |
| 126 | $for N in range(0, NR, 4): |
| 127 | $for M in range(MR): |
| 128 | vacc${M}x${ABC[N:N+4]} = wasm_v128_bitselect(vmin, vacc${M}x${ABC[N:N+4]}, wasm_f32x4_lt(vacc${M}x${ABC[N:N+4]}, vmin)); |
Marat Dukhan | 1bbf96b | 2020-06-15 23:01:20 -0700 | [diff] [blame] | 129 | |
Marat Dukhan | 802808c | 2020-06-16 11:01:17 -0700 | [diff] [blame^] | 130 | const v128_t vmax = wasm_v32x4_load_splat(¶ms->scalar.max); |
| 131 | $for N in range(0, NR, 4): |
| 132 | $for M in range(MR): |
| 133 | vacc${M}x${ABC[N:N+4]} = wasm_v128_bitselect(vacc${M}x${ABC[N:N+4]}, vmax, wasm_f32x4_le(vacc${M}x${ABC[N:N+4]}, vmax)); |
| 134 | $else: |
| 135 | $for N in range(0, NR, 4): |
| 136 | $for M in range(MR): |
| 137 | vacc${M}x${ABC[N:N+4]} = wasm_f32x4_max(vacc${M}x${ABC[N:N+4]}, vmin); |
| 138 | |
| 139 | $for N in range(0, NR, 4): |
| 140 | $for M in range(MR): |
| 141 | vacc${M}x${ABC[N:N+4]} = wasm_f32x4_min(vacc${M}x${ABC[N:N+4]}, vmax); |
| 142 | $elif ACTIVATION == "RELU": |
| 143 | const v128_t vzero = wasm_f32x4_splat(0.0f); |
| 144 | $if X86: |
| 145 | $for N in range(0, NR, 4): |
| 146 | $for M in range(MR): |
| 147 | vacc${M}x${ABC[N:N+4]} = wasm_v128_andnot(vacc${M}x${ABC[N:N+4]}, wasm_f32x4_le(vacc${M}x${ABC[N:N+4]}, vzero)); |
| 148 | $else: |
| 149 | $for N in range(0, NR, 4): |
| 150 | $for M in range(MR): |
| 151 | vacc${M}x${ABC[N:N+4]} = wasm_f32x4_max(vacc${M}x${ABC[N:N+4]}, vzero); |
Marat Dukhan | ac014d7 | 2020-06-16 08:36:47 -0700 | [diff] [blame] | 152 | |
Marat Dukhan | 1bbf96b | 2020-06-15 23:01:20 -0700 | [diff] [blame] | 153 | if XNN_LIKELY(nc >= ${NR}) { |
| 154 | $for M in reversed(range(MR)): |
| 155 | wasm_v128_store(c${M}, vacc${M}x${ABC[0:4]}); |
| 156 | $for N in range(4, NR, 4): |
| 157 | wasm_v128_store(c${M} + ${N}, vacc${M}x${ABC[N:N+4]}); |
| 158 | c${M} = (float*) ((uintptr_t) c${M} + cn_stride); |
| 159 | |
| 160 | $for M in reversed(range(MR)): |
| 161 | a${M} = (const float*) ((uintptr_t) a${M} - kc); |
| 162 | |
| 163 | nc -= ${NR}; |
| 164 | } else { |
| 165 | $for LOG2N in reversed(range(NR.bit_length())): |
| 166 | $if NR != 1 << LOG2N: |
| 167 | if (nc & ${1 << LOG2N}) { |
| 168 | $if LOG2N >= 2: |
| 169 | $for M in reversed(range(MR)): |
| 170 | wasm_v128_store(c${M}, vacc${M}x${ABC[0:4]}); |
| 171 | $for N in range(4, 1 << LOG2N, 4): |
| 172 | wasm_v128_store(c${M} + ${N}, vacc${M}x${ABC[N:N+4]}); |
| 173 | |
| 174 | $for M in reversed(range(MR)): |
| 175 | $for N in range(0, 1 << (LOG2N - 1), 4): |
| 176 | vacc${M}x${ABC[N:N+4]} = vacc${M}x${ABC[N + (1 << LOG2N):N + (1 << LOG2N)+4]}; |
| 177 | |
| 178 | $for M in reversed(range(MR)): |
| 179 | c${M} += ${1 << LOG2N}; |
| 180 | $elif LOG2N == 1: |
| 181 | $for M in reversed(range(MR)): |
| 182 | *((double*) c${M}) = wasm_f64x2_extract_lane(vacc${M}x${ABC[0:4]}, 0); |
| 183 | |
| 184 | $for M in reversed(range(MR)): |
| 185 | vacc${M}x${ABC[0:4]} = wasm_v32x4_shuffle(vacc${M}x${ABC[0:4]}, vacc${M}x${ABC[0:4]}, 2, 3, 2, 3); |
| 186 | |
| 187 | $for M in reversed(range(MR)): |
| 188 | c${M} += 2; |
| 189 | $elif LOG2N == 0: |
| 190 | $for M in reversed(range(MR)): |
| 191 | *c${M} = wasm_f32x4_extract_lane(vacc${M}x${ABC[0:4]}, 0); |
| 192 | } |
| 193 | |
| 194 | nc = 0; |
| 195 | } |
| 196 | } while (nc != 0); |
| 197 | } |