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 % 8 == 0 7$assert CHANNEL_TILE >= 8 8$assert ROW_TILE >= 1 9$ABC = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ" 10#include <assert.h> 11 12#include <immintrin.h> 13 14#include <xnnpack/math.h> 15#include <xnnpack/prelu.h> 16 17 18static const int32_t mask_table[14] = {-1, -1, -1, -1, -1, -1, -1, 0, 0, 0, 0, 0, 0, 0}; 19 20void xnn_f32_prelu_ukernel__avx_${ROW_TILE}x${CHANNEL_TILE}( 21 size_t rows, 22 size_t channels, 23 const float*restrict input, 24 size_t input_stride, 25 const float*restrict weights, 26 float*restrict output, 27 size_t output_stride) 28{ 29 assert(rows != 0); 30 assert(channels != 0); 31 assert(channels % sizeof(float) == 0); 32 33 const float* i0 = input; 34 float* o0 = output; 35 $for M in range(1, ROW_TILE): 36 const float* i${M} = (const float*) ((uintptr_t) i${M-1} + input_stride); 37 float* o${M} = (float*) ((uintptr_t) o${M-1} + output_stride); 38 $if M % 2 == 0: 39 if XNN_UNPREDICTABLE(rows <= ${M}) { 40 i${M} = i${M-1}; 41 o${M} = o${M-1}; 42 } 43 $else: 44 if XNN_UNPREDICTABLE(rows < ${M+1}) { 45 i${M} = i${M-1}; 46 o${M} = o${M-1}; 47 } 48 49 const size_t input_increment = input_stride * ${ROW_TILE} - channels; 50 const size_t output_increment = output_stride * ${ROW_TILE} - channels; 51 52 do { 53 const float* w = weights; 54 size_t c = channels; 55 for (; c >= ${CHANNEL_TILE} * sizeof(float); c -= ${CHANNEL_TILE} * sizeof(float)) { 56 const __m256 vw${ABC[0:8]} = _mm256_load_ps(w); 57 $for C in range(8, CHANNEL_TILE, 8): 58 const __m256 vw${ABC[C:C+8]} = _mm256_load_ps(w + ${C}); 59 w += ${CHANNEL_TILE}; 60 61 $for M in range(ROW_TILE): 62 const __m256 vi${M}x${ABC[0:8]} = _mm256_loadu_ps(i${M}); 63 $for C in range(8, CHANNEL_TILE, 8): 64 const __m256 vi${M}x${ABC[C:C+8]} = _mm256_loadu_ps(i${M} + ${C}); 65 i${M} += ${CHANNEL_TILE}; 66 67 $for M in range(ROW_TILE): 68 $for C in range(0, CHANNEL_TILE, 8): 69 const __m256 vprod${M}x${ABC[C:C+8]} = _mm256_mul_ps(vi${M}x${ABC[C:C+8]}, vw${ABC[C:C+8]}); 70 71 $for M in range(ROW_TILE): 72 $for C in range(0, CHANNEL_TILE, 8): 73 const __m256 vacc${M}x${ABC[C:C+8]} = _mm256_blendv_ps(vi${M}x${ABC[C:C+8]}, vprod${M}x${ABC[C:C+8]}, vi${M}x${ABC[C:C+8]}); 74 75 $for M in range(ROW_TILE): 76 _mm256_storeu_ps(o${M}, vacc${M}x${ABC[0:8]}); 77 $for C in range(8, CHANNEL_TILE, 8): 78 _mm256_storeu_ps(o${M} + ${C}, vacc${M}x${ABC[C:C+8]}); 79 o${M} += ${CHANNEL_TILE}; 80 } 81 $if CHANNEL_TILE > 8: 82 for (; c >= 8 * sizeof(float); c -= 8 * sizeof(float)) { 83 const __m256 vw = _mm256_load_ps(w); 84 w += 8; 85 86 $for M in range(ROW_TILE): 87 const __m256 vi${M} = _mm256_loadu_ps(i${M}); 88 i${M} += 8; 89 90 $for M in range(ROW_TILE): 91 const __m256 vprod${M} = _mm256_mul_ps(vi${M}, vw); 92 93 $for M in range(ROW_TILE): 94 const __m256 vacc${M} = _mm256_blendv_ps(vi${M}, vprod${M}, vi${M}); 95 96 $for M in range(ROW_TILE): 97 _mm256_storeu_ps(o${M}, vacc${M}); 98 o${M} += 8; 99 } 100 if XNN_UNLIKELY(c != 0) { 101 assert(c >= 1 * sizeof(float)); 102 assert(c <= 7 * sizeof(float)); 103 __m256i vmask = _mm256_loadu_si256((const __m256i*) ((uintptr_t) &mask_table[7] - c)); 104 105 const __m256 vw = _mm256_maskload_ps(w, vmask); 106 107 $for M in range(ROW_TILE): 108 const __m256 vi${M} = _mm256_maskload_ps(i${M}, vmask); 109 i${M} = (const float*) ((uintptr_t) i${M} + c); 110 111 $for M in range(ROW_TILE): 112 const __m256 vprod${M} = _mm256_mul_ps(vi${M}, vw); 113 114 $for M in range(ROW_TILE): 115 __m256 vacc${M} = _mm256_blendv_ps(vi${M}, vprod${M}, vi${M}); 116 117 // _mm256_maskstore_ps(o${M}, vmask, vacc${M}) could be used here, but triggers msan failures (probably an msan bug). 118 $for M in range(ROW_TILE): 119 __m128 vacc${M}_lo = _mm256_castps256_ps128(vacc${M}); 120 if (c & (4 * sizeof(float))) { 121 $for M in range(ROW_TILE): 122 _mm_storeu_ps(o${M}, vacc${M}_lo); 123 124 $for M in range(ROW_TILE): 125 vacc${M}_lo = _mm256_extractf128_ps(vacc${M}, 1); 126 127 $for M in range(ROW_TILE): 128 o${M} += 4; 129 } 130 if (c & (2 * sizeof(float))) { 131 $for M in range(ROW_TILE): 132 _mm_storel_pi((__m64*) o${M}, vacc${M}_lo); 133 134 $for M in range(ROW_TILE): 135 vacc${M}_lo = _mm_movehl_ps(vacc${M}_lo, vacc${M}_lo); 136 137 $for M in range(ROW_TILE): 138 o${M} += 2; 139 } 140 if (c & (1 * sizeof(float))) { 141 $for M in range(ROW_TILE): 142 _mm_store_ss(o${M}, vacc${M}_lo); 143 144 $for M in range(ROW_TILE): 145 o${M} += 1; 146 } 147 } 148 $for M in range(ROW_TILE): 149 i${M} = (const float*) ((uintptr_t) i${M} + input_increment); 150 o${M} = (float*) ((uintptr_t) o${M} + output_increment); 151 $if M % 2 == 1: 152 if XNN_UNPREDICTABLE(rows < ${ROW_TILE + M+1}) { 153 i${M} = i${M-1}; 154 o${M} = o${M-1}; 155 } 156 $elif M != 0: 157 if XNN_UNPREDICTABLE(rows <= ${ROW_TILE + M}) { 158 i${M} = i${M-1}; 159 o${M} = o${M-1}; 160 } 161 rows = doz(rows, ${ROW_TILE}); 162 } while (rows != 0); 163} 164