okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
d365372c8
main
1// tools/vision/own/cpu_kernels_avx2.cpp - the AVX2 + FMA + F16C tier (6 x 16 register tile). Compiled with -mavx2 -mfma -mf16c (/arch:AVX2 on MSVC). 2#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) 3 4#if defined(__x86_64__ )|| defined(_M_X64 ) 5#include <immintrin.h> 6 7 8namespace strata_vit { 9namespace { 10namespace kn { 11 12// one 16-bit value to float, bit exact (the hardware F16 conversion equals the software one for every finite value; BF16 is a shift) 13inline float f16_one (uint16_t h ) {return _mm_cvtss_f32 (_mm_cvtph_ps (_mm_cvtsi32_si128 ((int )h ))); } 14inline float bf16_one (uint16_t h ) {return _mm_cvtss_f32 (_mm_castsi128_ps (_mm_cvtsi32_si128 ((int ) ((uint32_t )h <<16 )))); } 15// the helpers cpu_kernels_impl.h asks for: no library call is compiled with this file's flags 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// The vector of the elementwise code (cpu_kernels_impl.h, STRATA_KN_RAW): raw __m256 and free inline functions, no wrapper struct (MSVC kept the wrapper in 22// memory; RESULTS-OWN-3V.md 12.14 and part C item 4). 23#define STRATA_KN_RAW 1 24constexpr int VW = 8 ; 25typedef __m256 VR ; 26inline VR vr_load (const float * p ) {return _mm256_loadu_ps (p ); } 27inline void vr_store (float * p ,VR v ) {_mm256_storeu_ps (p ,v ); } 28inline VR vr_set1 (float f ) {return _mm256_set1_ps (f ); } 29inline VR vr_zero () {return _mm256_setzero_ps (); } 30inline VR vr_add (VR a ,VR b ) {return _mm256_add_ps (a ,b ); } 31inline VR vr_sub (VR a ,VR b ) {return _mm256_sub_ps (a ,b ); } 32inline VR vr_mul (VR a ,VR b ) {return _mm256_mul_ps (a ,b ); } 33inline VR vr_div (VR a ,VR b ) {return _mm256_div_ps (a ,b ); } 34inline VR vr_fma (VR a ,VR b ,VR c ) {return _mm256_fmadd_ps (a ,b ,c ); } 35inline VR vr_max (VR a ,VR b ) {return _mm256_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 ) { 38__m128 s = _mm_add_ps (_mm256_castps256_ps128 (a ),_mm256_extractf128_ps (a ,1 )); 39s = _mm_add_ps (s ,_mm_movehl_ps (s ,s )); 40s = _mm_add_ss (s ,_mm_shuffle_ps (s ,s ,1 )); 41return _mm_cvtss_f32 (s ); 42} 43inline float vr_hmax (VR a ) { 44__m128 s = _mm_max_ps (_mm256_castps256_ps128 (a ),_mm256_extractf128_ps (a ,1 )); 45s = _mm_max_ps (s ,_mm_movehl_ps (s ,s )); 46s = _mm_max_ss (s ,_mm_shuffle_ps (s ,s ,1 )); 47return _mm_cvtss_f32 (s ); 48} 49 50// The product's micro-kernel on raw __m256 (no wrapper struct in the hot loop: MSVC kept the wrapper in memory, 1.46 to 1.82 of mtmd-CPU's time on Windows; 51// RESULTS-OWN-3V.md 12.14). The same operations in the same order as the generic micro of cpu_kernels_impl.h, so the results are bit-identical. 52#define STRATA_KN_HAS_MICRO 1 53template < int MR > 54inline 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 ) { 55__m256 acc [MR ][2 ]; 56for (int r = 0 ;r < MR ;++ r ) 57for (int c = 0 ;c < 2 ;++ c ) { 58__m256 v = _mm256_setzero_ps (); 59if (init != 0 )v = _mm256_loadu_ps (C + (size_t )r * ldc + c * 8 ); 60if (init != 1 && bias )v = _mm256_add_ps (v ,_mm256_loadu_ps (bias + c * 8 )); 61acc [r ][c ]= v ; 62 } 63for (int k = 0 ;k < K ;++ k ) { 64const __m256 b0 = _mm256_loadu_ps (B + (size_t )k * ldb ),b1 = _mm256_loadu_ps (B + (size_t )k * ldb + 8 ); 65for (int r = 0 ;r < MR ;++ r ) { 66const __m256 a = _mm256_set1_ps (A [(size_t )r * lda + k ]); 67acc [r ][0 ]= _mm256_fmadd_ps (a ,b0 ,acc [r ][0 ]); 68acc [r ][1 ]= _mm256_fmadd_ps (a ,b1 ,acc [r ][1 ]); 69 } 70 } 71for (int r = 0 ;r < MR ;++ r ) 72for (int c = 0 ;c < 2 ;++ c )_mm256_storeu_ps (C + (size_t )r * ldc + c * 8 ,acc [r ][c ]); 73} 74 75// expf (Cephes): x = n ln2 + r, exp(x) = 2^n * p(r) 76inline VR vr_exp (VR xv ) { 77__m256 x = _mm256_min_ps (_mm256_max_ps (xv ,_mm256_set1_ps (-87.3f )),_mm256_set1_ps (88.3f )); 78__m256 fx = _mm256_round_ps (_mm256_mul_ps (x ,_mm256_set1_ps (1.44269504088896341f )),_MM_FROUND_TO_NEAREST_INT |_MM_FROUND_NO_EXC ); 79x = _mm256_fnmadd_ps (fx ,_mm256_set1_ps (0.693359375f ),x ); 80x = _mm256_fnmadd_ps (fx ,_mm256_set1_ps (-2.12194440e-4f ),x ); 81__m256 y = _mm256_set1_ps (1.9875691500e-4f ); 82y = _mm256_fmadd_ps (y ,x ,_mm256_set1_ps (1.3981999507e-3f )); 83y = _mm256_fmadd_ps (y ,x ,_mm256_set1_ps (8.3334519073e-3f )); 84y = _mm256_fmadd_ps (y ,x ,_mm256_set1_ps (4.1665795894e-2f )); 85y = _mm256_fmadd_ps (y ,x ,_mm256_set1_ps (1.6666665459e-1f )); 86y = _mm256_fmadd_ps (y ,x ,_mm256_set1_ps (5.0000001201e-1f )); 87y = _mm256_fmadd_ps (y ,_mm256_mul_ps (x ,x ),_mm256_add_ps (x ,_mm256_set1_ps (1.0f ))); 88const __m256i e = _mm256_slli_epi32 (_mm256_add_epi32 (_mm256_cvtps_epi32 (fx ),_mm256_set1_epi32 (127 )),23 ); 89return _mm256_mul_ps (y ,_mm256_castsi256_ps (e )); 90} 91 92// 8 floats rounded to the 16-bit type and back (BF16: integer round to nearest even; F16: the hardware conversion pair, also round to nearest even) 93inline __m256 round8 (__m256 x ,int kind ) { 94if (kind == 2 )return _mm256_cvtph_ps (_mm256_cvtps_ph (x ,_MM_FROUND_TO_NEAREST_INT )); 95__m256i u = _mm256_castps_si256 (x ); 96const __m256i lsb = _mm256_and_si256 (_mm256_srli_epi32 (u ,16 ),_mm256_set1_epi32 (1 )); 97u = _mm256_add_epi32 (u ,_mm256_add_epi32 (_mm256_set1_epi32 (0x7fff ),lsb )); 98return _mm256_castsi256_ps (_mm256_and_si256 (u ,_mm256_set1_epi32 ((int )0xffff0000u ))); 99} 100inline void round_buf (float * p ,size_t n ,int kind ) { 101size_t i = 0 ; 102for (;i + 8 <=n ;i += 8 )_mm256_storeu_ps (p + i ,round8 (_mm256_loadu_ps (p + i ),kind )); 103if (i < n ) { 104float t [8 ]= {0 }; 105for (size_t j = i ;j < n ;++ j )t [j - i ]= p [j ]; 106_mm256_storeu_ps (t ,round8 (_mm256_loadu_ps (t ),kind )); 107for (size_t j = i ;j < n ;++ j )p [j ]= t [j - i ]; 108 } 109} 110 111inline void conv16 (const void * Wm ,int kind ,size_t off ,int n ,float * out ) { 112const uint16_t * p = (const uint16_t * )Wm + off ; 113int i = 0 ; 114if (kind == 2 ) { 115for (;i + 8 <=n ;i += 8 )_mm256_storeu_ps (out + i ,_mm256_cvtph_ps (_mm_loadu_si128 ((const __m128i * ) (p + i )))); 116for (;i < n ;++ i )out [i ]= f16_one (p [i ]); 117 }else { 118for (;i + 8 <=n ;i += 8 ) { 119const __m256i w = _mm256_slli_epi32 (_mm256_cvtepu16_epi32 (_mm_loadu_si128 ((const __m128i * ) (p + i ))),16 ); 120_mm256_storeu_ps (out + i ,_mm256_castsi256_ps (w )); 121 } 122for (;i < n ;++ i )out [i ]= bf16_one (p [i ]); 123 } 124} 125 126}// namespace kn 127}// namespace 128}// namespace strata_vit 129 130#define STRATA_KN_MR 6 131#define KN_MIN (a ,b ) kmin(a, b) 132#define KN_MAX (a ,b ) kmax(a, b) 133#define KN_COPY (d ,s ,n ) kcopy((d), (s), (n)) 134#define KN_ZERO (d ,n ) kzero((d), (n)) 135#define STRATA_KN_NAME "avx2" 136#define STRATA_KN_GETTER cpu_ops_avx2 137#include "cpu_kernels_impl.h" 138 139#else // not x86-64: the tier does not exist, the dispatcher never asks for it 140namespace strata_vit { 141const CpuOps & cpu_ops_avx2 () {return cpu_ops_scalar (); } 142} 143#endif