okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
d39d20f52
main
1// tools/vision/own/cpu_kernels_avx.cpp - the AVX tier without FMA, F16C or AVX2 (6 x 16 register tile: 12 ymm accumulators), for Sandy Bridge / Ivy 2// Bridge and AMD's AVX CPUs before Excavator. Compiled with -mavx (/arch:AVX on MSVC); only called when quant::isa_ok(Avx). 3// 4// As the SSE4.1 tier: a product term is a multiply then an add in the same k order as every tier (no FMA), F16 weights are converted in integer code 5// (bit exact, no F16C: Sandy Bridge has none), exp is the Cephes polynomial with separate multiply and add. AVX has no 256-bit integer operations, so the 6// integer steps (exponent bits, conversions, rounding) run on 128-bit halves. 7#include "cpu_tier.h" // only this and <immintrin.h>: no standard-library code may be compiled with this file's ISA flags (cpu_tier.h) 8 9#if defined(__x86_64__ )|| defined(_M_X64 ) 10#include <immintrin.h> 11 12namespace strata_vit { 13namespace { 14namespace kn { 15 16template < class T > inline T kmin (T a ,T b ) {return b < a ?b :a ; } 17template < class T > inline T kmax (T a ,T b ) {return a < b ?b :a ; } 18inline void kcopy (float * d ,const float * s ,int n ) {for (int i = 0 ;i < n ;++ i )d [i ]= s [i ]; } 19inline void kzero (float * d ,int n ) {for (int i = 0 ;i < n ;++ i )d [i ]= 0.0f ; } 20 21#define STRATA_KN_RAW 1 22constexpr int VW = 8 ; 23typedef __m256 VR ; 24inline VR vr_load (const float * p ) {return _mm256_loadu_ps (p ); } 25inline void vr_store (float * p ,VR v ) {_mm256_storeu_ps (p ,v ); } 26inline VR vr_set1 (float f ) {return _mm256_set1_ps (f ); } 27inline VR vr_zero () {return _mm256_setzero_ps (); } 28inline VR vr_add (VR a ,VR b ) {return _mm256_add_ps (a ,b ); } 29inline VR vr_sub (VR a ,VR b ) {return _mm256_sub_ps (a ,b ); } 30inline VR vr_mul (VR a ,VR b ) {return _mm256_mul_ps (a ,b ); } 31inline VR vr_div (VR a ,VR b ) {return _mm256_div_ps (a ,b ); } 32inline VR vr_fma (VR a ,VR b ,VR c ) {return _mm256_add_ps (_mm256_mul_ps (a ,b ),c ); }// two roundings (no FMA here) 33inline VR vr_max (VR a ,VR b ) {return _mm256_max_ps (a ,b ); } 34inline float vr_sqrt1 (float x ) {return _mm_cvtss_f32 (_mm_sqrt_ss (_mm_set_ss (x ))); } 35inline float vr_hadd (VR a ) {// the AVX2 tier's order 36__m128 s = _mm_add_ps (_mm256_castps256_ps128 (a ),_mm256_extractf128_ps (a ,1 )); 37s = _mm_add_ps (s ,_mm_movehl_ps (s ,s )); 38s = _mm_add_ss (s ,_mm_shuffle_ps (s ,s ,1 )); 39return _mm_cvtss_f32 (s ); 40} 41inline float vr_hmax (VR a ) { 42__m128 s = _mm_max_ps (_mm256_castps256_ps128 (a ),_mm256_extractf128_ps (a ,1 )); 43s = _mm_max_ps (s ,_mm_movehl_ps (s ,s )); 44s = _mm_max_ss (s ,_mm_shuffle_ps (s ,s ,1 )); 45return _mm_cvtss_f32 (s ); 46} 47 48#define STRATA_KN_HAS_MICRO 1 49template < int MR > 50inline void micro (const float * A ,size_t lda ,const float * B ,size_t ldb ,int K ,float * C ,size_t ldc ,int init ,const float * bias ) { 51__m256 acc [MR ][2 ]; 52for (int r = 0 ;r < MR ;++ r ) 53for (int c = 0 ;c < 2 ;++ c ) { 54__m256 v = _mm256_setzero_ps (); 55if (init != 0 )v = _mm256_loadu_ps (C + (size_t )r * ldc + c * 8 ); 56if (init != 1 && bias )v = _mm256_add_ps (v ,_mm256_loadu_ps (bias + c * 8 )); 57acc [r ][c ]= v ; 58 } 59for (int k = 0 ;k < K ;++ k ) { 60const __m256 b0 = _mm256_loadu_ps (B + (size_t )k * ldb ),b1 = _mm256_loadu_ps (B + (size_t )k * ldb + 8 ); 61for (int r = 0 ;r < MR ;++ r ) { 62const __m256 a = _mm256_set1_ps (A [(size_t )r * lda + k ]); 63acc [r ][0 ]= _mm256_add_ps (_mm256_mul_ps (a ,b0 ),acc [r ][0 ]); 64acc [r ][1 ]= _mm256_add_ps (_mm256_mul_ps (a ,b1 ),acc [r ][1 ]); 65 } 66 } 67for (int r = 0 ;r < MR ;++ r ) 68for (int c = 0 ;c < 2 ;++ c )_mm256_storeu_ps (C + (size_t )r * ldc + c * 8 ,acc [r ][c ]); 69} 70 71inline __m256 combine (__m128 lo ,__m128 hi ) {return _mm256_insertf128_ps (_mm256_castps128_ps256 (lo ),hi ,1 ); } 72 73// expf (Cephes, the AVX2 tier's constants), multiply and add separately; 2^n built on the two 128-bit halves 74inline VR vr_exp (VR xv ) { 75__m256 x = _mm256_min_ps (_mm256_max_ps (xv ,_mm256_set1_ps (-87.3f )),_mm256_set1_ps (88.3f )); 76const __m256 fx = _mm256_round_ps (_mm256_mul_ps (x ,_mm256_set1_ps (1.44269504088896341f )),_MM_FROUND_TO_NEAREST_INT |_MM_FROUND_NO_EXC ); 77x = _mm256_sub_ps (x ,_mm256_mul_ps (fx ,_mm256_set1_ps (0.693359375f ))); 78x = _mm256_sub_ps (x ,_mm256_mul_ps (fx ,_mm256_set1_ps (-2.12194440e-4f ))); 79__m256 y = _mm256_set1_ps (1.9875691500e-4f ); 80y = _mm256_add_ps (_mm256_mul_ps (y ,x ),_mm256_set1_ps (1.3981999507e-3f )); 81y = _mm256_add_ps (_mm256_mul_ps (y ,x ),_mm256_set1_ps (8.3334519073e-3f )); 82y = _mm256_add_ps (_mm256_mul_ps (y ,x ),_mm256_set1_ps (4.1665795894e-2f )); 83y = _mm256_add_ps (_mm256_mul_ps (y ,x ),_mm256_set1_ps (1.6666665459e-1f )); 84y = _mm256_add_ps (_mm256_mul_ps (y ,x ),_mm256_set1_ps (5.0000001201e-1f )); 85y = _mm256_add_ps (_mm256_mul_ps (y ,_mm256_mul_ps (x ,x )),_mm256_add_ps (x ,_mm256_set1_ps (1.0f ))); 86const __m256i n = _mm256_cvtps_epi32 (fx ); 87const __m128i b = _mm_set1_epi32 (127 ); 88const __m128i elo = _mm_slli_epi32 (_mm_add_epi32 (_mm256_castsi256_si128 (n ),b ),23 ); 89const __m128i ehi = _mm_slli_epi32 (_mm_add_epi32 (_mm256_extractf128_si256 (n ,1 ),b ),23 ); 90return _mm256_mul_ps (y ,combine (_mm_castsi128_ps (elo ),_mm_castsi128_ps (ehi ))); 91} 92 93// rounding to 16 bits, bit for bit as the scalar tier (see cpu_kernels_sse41.cpp for the method), on 128-bit halves 94inline __m128 round4 (__m128 x ,int kind ) { 95const __m128i u = _mm_castps_si128 (x ); 96const __m128i absu = _mm_and_si128 (u ,_mm_set1_epi32 (0x7fffffff )); 97if (kind == 1 ) { 98const __m128i lsb = _mm_and_si128 (_mm_srli_epi32 (u ,16 ),_mm_set1_epi32 (1 )); 99const __m128i r = _mm_and_si128 (_mm_add_epi32 (u ,_mm_add_epi32 (_mm_set1_epi32 (0x7fff ),lsb )),_mm_set1_epi32 ((int )0xffff0000u )); 100const __m128i nan = _mm_cmpgt_epi32 (absu ,_mm_set1_epi32 (0x7f800000 )); 101return _mm_castsi128_ps (_mm_blendv_epi8 (r ,u ,nan )); 102 } 103const __m128 a = _mm_castsi128_ps (absu ); 104const __m128i sign = _mm_and_si128 (u ,_mm_set1_epi32 ((int )0x80000000u )); 105const __m128i lsb = _mm_and_si128 (_mm_srli_epi32 (absu ,13 ),_mm_set1_epi32 (1 )); 106const __m128i nrm = _mm_and_si128 (_mm_add_epi32 (absu ,_mm_add_epi32 (_mm_set1_epi32 (0xfff ),lsb )),_mm_set1_epi32 ((int )0xffffe000u )); 107const __m128 sub = _mm_sub_ps (_mm_add_ps (a ,_mm_set1_ps (0.5f )),_mm_set1_ps (0.5f )); 108const __m128 is_sub = _mm_cmplt_ps (a ,_mm_set1_ps (6.103515625e-05f )); 109__m128 q = _mm_blendv_ps (_mm_castsi128_ps (nrm ),sub ,is_sub ); 110q = _mm_or_ps (q ,_mm_castsi128_ps (sign )); 111const __m128 keep = _mm_cmplt_ps (a ,_mm_set1_ps (65520.0f )); 112return _mm_blendv_ps (x ,q ,keep ); 113} 114inline void round_buf (float * p ,size_t n ,int kind ) { 115size_t i = 0 ; 116for (;i + 4 <=n ;i += 4 )_mm_storeu_ps (p + i ,round4 (_mm_loadu_ps (p + i ),kind )); 117if (i < n ) { 118float t [4 ]= {0 }; 119for (size_t j = i ;j < n ;++ j )t [j - i ]= p [j ]; 120_mm_storeu_ps (t ,round4 (_mm_loadu_ps (t ),kind )); 121for (size_t j = i ;j < n ;++ j )p [j ]= t [j - i ]; 122 } 123} 124 125inline __m128 h2f4 (__m128i h ) { 126const __m128i em = _mm_and_si128 (h ,_mm_set1_epi32 (0x7fff )); 127const __m128i sign = _mm_slli_epi32 (_mm_and_si128 (h ,_mm_set1_epi32 (0x8000 )),16 ); 128const __m128i sh = _mm_slli_epi32 (em ,13 ); 129const __m128 fin = _mm_mul_ps (_mm_castsi128_ps (sh ),_mm_castsi128_ps (_mm_set1_epi32 (0x77800000 ))); 130const __m128i inf = _mm_or_si128 (sh ,_mm_set1_epi32 (0x7f800000 )); 131const __m128i big = _mm_cmpgt_epi32 (em ,_mm_set1_epi32 (0x7bff )); 132const __m128i r = _mm_blendv_epi8 (_mm_castps_si128 (fin ),inf ,big ); 133return _mm_castsi128_ps (_mm_or_si128 (r ,sign )); 134} 135inline float f16_one (uint16_t h ) {return _mm_cvtss_f32 (h2f4 (_mm_cvtsi32_si128 ((int )h ))); } 136inline float bf16_one (uint16_t h ) {return _mm_cvtss_f32 (_mm_castsi128_ps (_mm_cvtsi32_si128 ((int ) ((uint32_t )h <<16 )))); } 137 138inline void conv16 (const void * Wm ,int kind ,size_t off ,int n ,float * out ) { 139const uint16_t * p = (const uint16_t * )Wm + off ; 140int i = 0 ; 141if (kind == 2 ) { 142for (;i + 8 <=n ;i += 8 ) { 143const __m128i w = _mm_loadu_si128 ((const __m128i * ) (p + i )); 144_mm256_storeu_ps (out + i ,combine (h2f4 (_mm_cvtepu16_epi32 (w )),h2f4 (_mm_cvtepu16_epi32 (_mm_srli_si128 (w ,8 ))))); 145 } 146for (;i < n ;++ i )out [i ]= f16_one (p [i ]); 147 }else { 148const __m128i z = _mm_setzero_si128 (); 149for (;i + 8 <=n ;i += 8 ) { 150const __m128i w = _mm_loadu_si128 ((const __m128i * ) (p + i )); 151_mm256_storeu_ps (out + i ,combine (_mm_castsi128_ps (_mm_unpacklo_epi16 (z ,w )),_mm_castsi128_ps (_mm_unpackhi_epi16 (z ,w )))); 152 } 153for (;i < n ;++ i )out [i ]= bf16_one (p [i ]); 154 } 155} 156 157}// namespace kn 158}// namespace 159}// namespace strata_vit 160 161#define STRATA_KN_MR 6 162#define KN_MIN (a ,b ) kmin(a, b) 163#define KN_MAX (a ,b ) kmax(a, b) 164#define KN_COPY (d ,s ,n ) kcopy((d), (s), (n)) 165#define KN_ZERO (d ,n ) kzero((d), (n)) 166#define STRATA_KN_NAME "avx" 167#define STRATA_KN_GETTER cpu_ops_avx 168#include "cpu_kernels_impl.h" 169 170#else 171namespace strata_vit { 172const CpuOps & cpu_ops_avx () {return cpu_ops_scalar (); } 173} 174#endif