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 CHANNEL_TILE % 16 == 0 7$assert CHANNEL_TILE >= 16 8$assert ROW_TILE >= 1 9$ABC = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ" 10#include <assert.h> 11 12#include <immintrin.h> 13 14#include <xnnpack/intrinsics-polyfill.h> 15#include <xnnpack/math.h> 16#include <xnnpack/prelu.h> 17 18 19void xnn_f32_prelu_ukernel__avx512f_${ROW_TILE}x${CHANNEL_TILE}( 20 size_t rows, 21 size_t channels, 22 const float*restrict input, 23 size_t input_stride, 24 const float*restrict weights, 25 float*restrict output, 26 size_t output_stride) 27{ 28 assert(rows != 0); 29 assert(channels != 0); 30 assert(channels % sizeof(float) == 0); 31 32 const float* i0 = input; 33 float* o0 = output; 34 $for M in range(1, ROW_TILE): 35 const float* i${M} = (const float*) ((uintptr_t) i${M-1} + input_stride); 36 float* o${M} = (float*) ((uintptr_t) o${M-1} + output_stride); 37 38 const size_t input_increment = input_stride * ${ROW_TILE} - channels; 39 const size_t output_increment = output_stride * ${ROW_TILE} - channels; 40 41 const __m512 vzero = _mm512_setzero_ps(); 42 do { 43 $for M in range(1, ROW_TILE): 44 $if M % 2 == 0: 45 if XNN_UNPREDICTABLE(rows <= ${M}) { 46 i${M} = i${M-1}; 47 o${M} = o${M-1}; 48 } 49 $else: 50 if XNN_UNPREDICTABLE(rows < ${M+1}) { 51 i${M} = i${M-1}; 52 o${M} = o${M-1}; 53 } 54 55 const float* w = weights; 56 size_t c = channels; 57 for (; c >= ${CHANNEL_TILE} * sizeof(float); c -= ${CHANNEL_TILE} * sizeof(float)) { 58 const __m512 vw${ABC[0:16]} = _mm512_load_ps(w); 59 $for C in range(16, CHANNEL_TILE, 16): 60 const __m512 vw${ABC[C:C+16]} = _mm512_load_ps(w + ${C}); 61 w += ${CHANNEL_TILE}; 62 63 $for M in range(ROW_TILE): 64 const __m512 vi${M}x${ABC[0:16]} = _mm512_loadu_ps(i${M}); 65 $for C in range(16, CHANNEL_TILE, 16): 66 const __m512 vi${M}x${ABC[C:C+16]} = _mm512_loadu_ps(i${M} + ${C}); 67 i${M} += ${CHANNEL_TILE}; 68 69 $for M in range(ROW_TILE): 70 $for C in range(0, CHANNEL_TILE, 16): 71 const __mmask16 vsign${M}x${ABC[C:C+16]} = _mm512_cmp_ps_mask(vi${M}x${ABC[C:C+16]}, vzero, _CMP_LT_OQ); 72 const __m512 vacc${M}x${ABC[C:C+16]} = _mm512_mask_mul_ps(vi${M}x${ABC[C:C+16]}, vsign${M}x${ABC[C:C+16]}, vi${M}x${ABC[C:C+16]}, vw${ABC[C:C+16]}); 73 74 $for M in range(ROW_TILE): 75 _mm512_storeu_ps(o${M}, vacc${M}x${ABC[0:16]}); 76 $for C in range(16, CHANNEL_TILE, 16): 77 _mm512_storeu_ps(o${M} + ${C}, vacc${M}x${ABC[C:C+16]}); 78 o${M} += ${CHANNEL_TILE}; 79 } 80 $if CHANNEL_TILE > 16: 81 for (; c >= 16 * sizeof(float); c -= 16 * sizeof(float)) { 82 const __m512 vw = _mm512_load_ps(w); 83 w += 16; 84 85 $for M in range(ROW_TILE): 86 const __m512 vi${M} = _mm512_loadu_ps(i${M}); 87 i${M} += 16; 88 89 $for M in range(ROW_TILE): 90 const __mmask16 vsign${M} = _mm512_cmp_ps_mask(vi${M}, vzero, _CMP_LT_OQ); 91 const __m512 vacc${M} = _mm512_mask_mul_ps(vi${M}, vsign${M}, vi${M}, vw); 92 93 $for M in range(ROW_TILE): 94 _mm512_storeu_ps(o${M}, vacc${M}); 95 o${M} += 16; 96 } 97 if XNN_UNLIKELY(c != 0) { 98 assert(c >= 1 * sizeof(float)); 99 assert(c <= 15 * sizeof(float)); 100 // Prepare mask for valid 32-bit elements (depends on c). 101 const __mmask16 vmask = _cvtu32_mask16((uint16_t) ((uint32_t) (UINT32_C(1) << (c >> 2 /* log2(sizeof(float))*/)) - UINT32_C(1))); 102 103 const __m512 vw = _mm512_maskz_loadu_ps(vmask, w); 104 105 $for M in range(ROW_TILE): 106 const __m512 vi${M} = _mm512_maskz_loadu_ps(vmask, i${M}); 107 i${M} = (const float*) ((uintptr_t) i${M} + c); 108 109 $for M in range(ROW_TILE): 110 const __mmask16 vsign${M} = _mm512_cmp_ps_mask(vi${M}, vzero, _CMP_LT_OQ); 111 const __m512 vacc${M} = _mm512_mask_mul_ps(vi${M}, vsign${M}, vi${M}, vw); 112 113 $for M in range(ROW_TILE): 114 _mm512_mask_storeu_ps(o${M}, vmask, vacc${M}); 115 o${M} = (float*) ((uintptr_t) o${M} + c); 116 } 117 $for M in range(ROW_TILE): 118 i${M} = (const float*) ((uintptr_t) i${M} + input_increment); 119 o${M} = (float*) ((uintptr_t) o${M} + output_increment); 120 rows = doz(rows, ${ROW_TILE}); 121 } while (rows != 0); 122} 123