okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
d365372c8
main
1// tools/vision/own/cpu_kernels_avx512.cpp - the AVX-512 tier (12 x 32 register tile: 24 accumulators). Compiled with -mavx512f -mavx512bw -mavx512dq -mavx512vl 2// -mavx2 -mfma -mf16c (/arch:AVX512 on MSVC); only called when quant::isa_ok(Avx512). 3#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) 4 5#if defined(__x86_64__ )|| defined(_M_X64 ) 6#include <immintrin.h> 7 8 9namespace strata_vit { 10namespace { 11namespace kn { 12 13// one 16-bit value to float, bit exact (the hardware F16 conversion equals the software one for every finite value; BF16 is a shift) 14inline float f16_one (uint16_t h ) {return _mm_cvtss_f32 (_mm_cvtph_ps (_mm_cvtsi32_si128 ((int )h ))); } 15inline float bf16_one (uint16_t h ) {return _mm_cvtss_f32 (_mm_castsi128_ps (_mm_cvtsi32_si128 ((int ) ((uint32_t )h <<16 )))); } 16// the helpers cpu_kernels_impl.h asks for: no library call is compiled with this file's flags 17template < class T > inline T kmin (T a ,T b ) {return b < a ?b :a ; } 18template < class T > inline T kmax (T a ,T b ) {return a < b ?b :a ; } 19inline void kcopy (float * d ,const float * s ,int n ) {for (int i = 0 ;i < n ;++ i )d [i ]= s [i ]; } 20inline void kzero (float * d ,int n ) {for (int i = 0 ;i < n ;++ i )d [i ]= 0.0f ; } 21 22// The vector of the elementwise code (cpu_kernels_impl.h, STRATA_KN_RAW): raw __m512 and free inline functions, no wrapper struct (as the AVX2 tier). 23#define STRATA_KN_RAW 1 24constexpr int VW = 16 ; 25typedef __m512 VR ; 26inline VR vr_load (const float * p ) {return _mm512_loadu_ps (p ); } 27inline void vr_store (float * p ,VR v ) {_mm512_storeu_ps (p ,v ); } 28inline VR vr_set1 (float f ) {return _mm512_set1_ps (f ); } 29inline VR vr_zero () {return _mm512_setzero_ps (); } 30inline VR vr_add (VR a ,VR b ) {return _mm512_add_ps (a ,b ); } 31inline VR vr_sub (VR a ,VR b ) {return _mm512_sub_ps (a ,b ); } 32inline VR vr_mul (VR a ,VR b ) {return _mm512_mul_ps (a ,b ); } 33inline VR vr_div (VR a ,VR b ) {return _mm512_div_ps (a ,b ); } 34inline VR vr_fma (VR a ,VR b ,VR c ) {return _mm512_fmadd_ps (a ,b ,c ); } 35inline VR vr_max (VR a ,VR b ) {return _mm512_max_ps (a ,b ); } 36inline float vr_sqrt1 (float x ) {return _mm_cvtss_f32 (_mm_sqrt_ss (_mm_set_ss (x ))); }// sqrtss: the correctly rounded root, as std::sqrt 37inline float vr_hadd (VR a ) {return _mm512_reduce_add_ps (a ); } 38inline float vr_hmax (VR a ) {return _mm512_reduce_max_ps (a ); } 39 40// The product's micro-kernel on raw __m512 (as the AVX2 tier's; same operations, same order, bit-identical to the generic micro of cpu_kernels_impl.h). 41#define STRATA_KN_HAS_MICRO 1 42template < int MR > 43inline 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 ) { 44__m512 acc [MR ][2 ]; 45for (int r = 0 ;r < MR ;++ r ) 46for (int c = 0 ;c < 2 ;++ c ) { 47__m512 v = _mm512_setzero_ps (); 48if (init != 0 )v = _mm512_loadu_ps (C + (size_t )r * ldc + c * 16 ); 49if (init != 1 && bias )v = _mm512_add_ps (v ,_mm512_loadu_ps (bias + c * 16 )); 50acc [r ][c ]= v ; 51 } 52for (int k = 0 ;k < K ;++ k ) { 53const __m512 b0 = _mm512_loadu_ps (B + (size_t )k * ldb ),b1 = _mm512_loadu_ps (B + (size_t )k * ldb + 16 ); 54for (int r = 0 ;r < MR ;++ r ) { 55const __m512 a = _mm512_set1_ps (A [(size_t )r * lda + k ]); 56acc [r ][0 ]= _mm512_fmadd_ps (a ,b0 ,acc [r ][0 ]); 57acc [r ][1 ]= _mm512_fmadd_ps (a ,b1 ,acc [r ][1 ]); 58 } 59 } 60for (int r = 0 ;r < MR ;++ r ) 61for (int c = 0 ;c < 2 ;++ c )_mm512_storeu_ps (C + (size_t )r * ldc + c * 16 ,acc [r ][c ]); 62} 63 64inline VR vr_exp (VR xv ) { 65__m512 x = _mm512_min_ps (_mm512_max_ps (xv ,_mm512_set1_ps (-87.3f )),_mm512_set1_ps (88.3f )); 66__m512 fx = _mm512_roundscale_ps (_mm512_mul_ps (x ,_mm512_set1_ps (1.44269504088896341f )),_MM_FROUND_TO_NEAREST_INT |_MM_FROUND_NO_EXC ); 67x = _mm512_fnmadd_ps (fx ,_mm512_set1_ps (0.693359375f ),x ); 68x = _mm512_fnmadd_ps (fx ,_mm512_set1_ps (-2.12194440e-4f ),x ); 69__m512 y = _mm512_set1_ps (1.9875691500e-4f ); 70y = _mm512_fmadd_ps (y ,x ,_mm512_set1_ps (1.3981999507e-3f )); 71y = _mm512_fmadd_ps (y ,x ,_mm512_set1_ps (8.3334519073e-3f )); 72y = _mm512_fmadd_ps (y ,x ,_mm512_set1_ps (4.1665795894e-2f )); 73y = _mm512_fmadd_ps (y ,x ,_mm512_set1_ps (1.6666665459e-1f )); 74y = _mm512_fmadd_ps (y ,x ,_mm512_set1_ps (5.0000001201e-1f )); 75y = _mm512_fmadd_ps (y ,_mm512_mul_ps (x ,x ),_mm512_add_ps (x ,_mm512_set1_ps (1.0f ))); 76return _mm512_scalef_ps (y ,fx ); 77} 78 79inline __m512 round16v (__m512 x ,int kind ) { 80if (kind == 2 )return _mm512_cvtph_ps (_mm512_cvtps_ph (x ,_MM_FROUND_TO_NEAREST_INT |_MM_FROUND_NO_EXC )); 81__m512i u = _mm512_castps_si512 (x ); 82const __m512i lsb = _mm512_and_si512 (_mm512_srli_epi32 (u ,16 ),_mm512_set1_epi32 (1 )); 83u = _mm512_add_epi32 (u ,_mm512_add_epi32 (_mm512_set1_epi32 (0x7fff ),lsb )); 84return _mm512_castsi512_ps (_mm512_and_si512 (u ,_mm512_set1_epi32 ((int )0xffff0000u ))); 85} 86inline void round_buf (float * p ,size_t n ,int kind ) { 87size_t i = 0 ; 88for (;i + 16 <=n ;i += 16 )_mm512_storeu_ps (p + i ,round16v (_mm512_loadu_ps (p + i ),kind )); 89if (i < n ) { 90float t [16 ]= {0 }; 91for (size_t j = i ;j < n ;++ j )t [j - i ]= p [j ]; 92_mm512_storeu_ps (t ,round16v (_mm512_loadu_ps (t ),kind )); 93for (size_t j = i ;j < n ;++ j )p [j ]= t [j - i ]; 94 } 95} 96 97inline void conv16 (const void * Wm ,int kind ,size_t off ,int n ,float * out ) { 98const uint16_t * p = (const uint16_t * )Wm + off ; 99int i = 0 ; 100if (kind == 2 ) { 101for (;i + 16 <=n ;i += 16 )_mm512_storeu_ps (out + i ,_mm512_cvtph_ps (_mm256_loadu_si256 ((const __m256i * ) (p + i )))); 102for (;i < n ;++ i )out [i ]= f16_one (p [i ]); 103 }else { 104for (;i + 16 <=n ;i += 16 ) { 105const __m512i w = _mm512_slli_epi32 (_mm512_cvtepu16_epi32 (_mm256_loadu_si256 ((const __m256i * ) (p + i ))),16 ); 106_mm512_storeu_ps (out + i ,_mm512_castsi512_ps (w )); 107 } 108for (;i < n ;++ i )out [i ]= bf16_one (p [i ]); 109 } 110} 111 112}// namespace kn 113}// namespace 114}// namespace strata_vit 115 116#define STRATA_KN_MR 12 117#define KN_MIN (a ,b ) kmin(a, b) 118#define KN_MAX (a ,b ) kmax(a, b) 119#define KN_COPY (d ,s ,n ) kcopy((d), (s), (n)) 120#define KN_ZERO (d ,n ) kzero((d), (n)) 121#define STRATA_KN_NAME "avx512" 122#define STRATA_KN_GETTER cpu_ops_avx512 123#include "cpu_kernels_impl.h" 124 125#else 126namespace strata_vit { 127const CpuOps & cpu_ops_avx512 () {return cpu_ops_scalar (); } 128} 129#endif