// 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 CHANNEL_TILE > 0 $assert ROW_TILE >= 1 $ABC = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ" #include #include #include void xnn_f32_prelu_ukernel__wasm_${ROW_TILE}x${CHANNEL_TILE}( size_t rows, size_t channels, const float*restrict input, size_t input_stride, const float*restrict weights, float*restrict output, size_t output_stride) { assert(rows != 0); assert(channels != 0); assert(channels % sizeof(float) == 0); const float* i0 = input; float* o0 = output; $for M in range(1, ROW_TILE): const float* i${M} = (const float*) ((uintptr_t) i${M-1} + input_stride); float* o${M} = (float*) ((uintptr_t) o${M-1} + output_stride); $if M % 2 == 0: if XNN_UNPREDICTABLE(rows <= ${M}) { i${M} = i${M-1}; o${M} = o${M-1}; } $else: if XNN_UNPREDICTABLE(rows < ${M+1}) { i${M} = i${M-1}; o${M} = o${M-1}; } const size_t input_increment = input_stride * ${ROW_TILE} - channels; const size_t output_increment = output_stride * ${ROW_TILE} - channels; const float vzero = 0.0f; do { const float* w = weights; size_t c = channels; $if CHANNEL_TILE > 1: for (; c >= ${CHANNEL_TILE} * sizeof(float); c -= ${CHANNEL_TILE} * sizeof(float)) { $for C in range(CHANNEL_TILE): const float vw${ABC[C]} = w[${C}]; $for M in range(ROW_TILE): $for C in range(CHANNEL_TILE): float vi${M}x${ABC[C]} = i${M}[${C}]; i${M} += ${CHANNEL_TILE}; $for M in range(ROW_TILE): $for C in range(CHANNEL_TILE): float vacc${M}x${ABC[C]} = __builtin_wasm_max_f32(vi${M}x${ABC[C]}, vzero); vi${M}x${ABC[C]} = __builtin_wasm_min_f32(vi${M}x${ABC[C]}, vzero); $for M in range(ROW_TILE): $for C in range(CHANNEL_TILE): vacc${M}x${ABC[C]} += vi${M}x${ABC[C]} * vw${ABC[C]}; $for M in range(ROW_TILE): $for C in range(CHANNEL_TILE): o${M}[${C}] = vacc${M}x${ABC[C]}; o${M} += ${CHANNEL_TILE}; w += ${CHANNEL_TILE}; } for (; c != 0; c -= sizeof(float)) { const float vw = *w++; $for M in range(ROW_TILE): float vi${M} = *i${M}++; $for M in range(ROW_TILE): float vacc${M} = __builtin_wasm_max_f32(vi${M}, vzero); vi${M} = __builtin_wasm_min_f32(vi${M}, vzero); $for M in range(ROW_TILE): vacc${M} += vi${M} * vw; $for M in range(ROW_TILE): *o${M}++ = vacc${M}; } $else: do { const float vw = *w++; $for M in range(ROW_TILE): float vi${M} = *i${M}++; $for M in range(ROW_TILE): float vacc${M} = __builtin_wasm_max_f32(vi${M}, vzero); vi${M} = __builtin_wasm_min_f32(vi${M}, vzero); $for M in range(ROW_TILE): vacc${M} += vi${M} * vw; $for M in range(ROW_TILE): *o${M}++ = vacc${M}; c -= sizeof(float); } while (c != 0); $for M in range(ROW_TILE): i${M} = (const float*) ((uintptr_t) i${M} + input_increment); o${M} = (float*) ((uintptr_t) o${M} + output_increment); $if M % 2 == 1: if XNN_UNPREDICTABLE(rows < ${ROW_TILE + M+1}) { i${M} = i${M-1}; o${M} = o${M-1}; } $elif M != 0: if XNN_UNPREDICTABLE(rows <= ${ROW_TILE + M}) { i${M} = i${M-1}; o${M} = o${M-1}; } rows = doz(rows, ${ROW_TILE}); } while (rows != 0); }