okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
0b6fc1cec
main
1// src/kernels/cpu/nvfp4_avx2.cpp - see include/strata/kernels/cpu/nvfp4_avx2.hpp. 2// 3// Per 64-value block, once: its E2M1 values in element order as two 32-byte registers (ggml's reordering: sub-block 4// s keeps value j in the low nibble of qs[8 s + j] and value j + 8 in the high one), their magnitudes, and the four 5// sub-block scales. Per token, as ggml_vec_dot_nvfp4_q8_0's AVX2 path: vpsignb moves the sign onto the activation, 6// vpmaddubsw + vpmaddwd give 8 int32 lanes (four per 16-value sub-block), one FMA per 32 values applies 7// ue4m3(d[s]) * d_q8, and hsum_float_8's order sums the lanes. 8// 9// Needs AVX2, FMA and F16C - what cpu_avx2_ok() checks before anything here is called. 10#include "strata/kernels/cpu/nvfp4_avx2.hpp" 11 12#define GGML_COMMON_DECL_CPP 13#define GGML_COMMON_IMPL_CPP 14#include "ggml-common.h" 15 16#include <immintrin.h> 17 18#include <cmath> 19#include <cstdlib> 20#include <cstring> 21 22namespace strata::kernels::cpu { 23namespace { 24 25// A compile-time table, not a constructor: this file is compiled for a wide ISA, and a namespace-scope object with a 26// constructor runs at program start on EVERY CPU - the compiler vectorises the loop with AVX/FMA, and a CPU without them 27// (Westmere, Sandy Bridge, ...) died with an illegal instruction before main (#2324). Powers of two: exact. 28constexpr float pow2i (int e ) { 29float r = 1.0f ; 30for (;e > 0 ;-- e )r *=2.0f ; 31for (;e < 0 ;++ e )r *=0.5f ; 32return r ; 33} 34struct Ue4m3Table {// ggml_ue4m3_to_fp32 (ggml-impl.h): halved for the doubled kvalues_fp4, 0x7F read as 0 35float v [256 ]; 36constexpr Ue4m3Table () :v () { 37for (int x = 0 ;x < 256 ;++ x ) { 38if (x == 0 || x == 0x7F ) {v [x ]= 0.0f ;continue ; } 39const int e = (x >>3 )& 0xF ,m = x & 0x7 ; 40const float raw = e == 0 ? (float )m * pow2i (-9 ) : (1.0f + (float )m /8.0f )* pow2i (e - 7 ); 41v [x ]= raw * 0.5f ; 42 } 43 } 44}; 45constexpr Ue4m3Table kUe4m3 ; 46 47// software prefetch distance in bytes (STRATA_NVFP4_PREFETCH, 0 = off), as the AVX-512 rows 48// (read on first use, behind the CPU check: a static initialiser here would run on every CPU, see above) 49inline int prefetch_distance () { 50static const int v = [] { 51const char * e = std::getenv ("STRATA_NVFP4_PREFETCH" ); 52return e ? std::atoi (e ) :2048 ; 53 }(); 54return v ; 55} 56 57inline float h2f (uint16_t h ) {return _mm_cvtss_f32 (_mm_cvtph_ps (_mm_cvtsi32_si128 ((int )h ))); } 58 59// sub-blocks 2 h and 2 h + 1 (16 bytes of qs) as their 32 signed doubled E2M1 values, in element order 60inline __m256i values32 (const uint8_t * qs ,const __m128i kv ) { 61const __m128i q = _mm_loadu_si128 ((const __m128i * )qs ); 62const __m128i m4 = _mm_set1_epi8 (0x0F ); 63const __m128i lo = _mm_shuffle_epi8 (kv ,_mm_and_si128 (q ,m4 )); 64const __m128i hi = _mm_shuffle_epi8 (kv ,_mm_and_si128 (_mm_srli_epi16 (q ,4 ),m4 )); 65return _mm256_set_m128i (_mm_unpackhi_epi64 (lo ,hi ),_mm_unpacklo_epi64 (lo ,hi )); 66} 67 68inline float hsum8 (const __m256 x ) {// ggml's hsum_float_8 69__m128 res = _mm256_extractf128_ps (x ,1 ); 70res = _mm_add_ps (res ,_mm256_castps256_ps128 (x )); 71res = _mm_add_ps (res ,_mm_movehl_ps (res ,res )); 72res = _mm_add_ss (res ,_mm_movehdup_ps (res )); 73return _mm_cvtss_f32 (res ); 74} 75 76constexpr int kMaxBlocks = 64 ;// rows of up to 4096 values (n_embd 2560) 77// tokens per pass: the pool's MAXT, so a window is one pass. Spilled accumulators cost less than another pass over 78// the weights: one expert's gate+up, 8 tokens, 0.415 ms in passes of 4 + 4, 0.352 in one of 8 (5 6 7 alike) 79constexpr int kMaxW = 8 ; 80 81struct Acts {// per token and block, the two Q8_0 blocks' scales; built once per call, reused by every row 82int nt = 0 ,nb = 0 ; 83const block_q8_0 * y [kMaxW ]= {}; 84float dy [kMaxW * kMaxBlocks * 2 ]; 85Acts (const void * const * act ,int nt_ ,int nb_ ) :nt (nt_ ),nb (nb_ ) { 86for (int t = 0 ;t < nt ;++ t ) { 87y [t ]= (const block_q8_0 * )act [t ]; 88for (int ib = 0 ;ib < 2 * nb ;++ ib )dy [(size_t )t * 2 * nb + ib ]= h2f (y [t ][ib ].d ); 89 } 90 } 91}; 92 93template < int NT > 94inline void row_dot (const uint8_t * row ,const Acts & a ,float * res ) { 95const __m128i kv = _mm_loadu_si128 ((const __m128i * )kvalues_fp4 ); 96const __m256i ones = _mm256_set1_epi16 (1 ); 97__m256 acc [NT ]; 98for (int t = 0 ;t < NT ;++ t )acc [t ]= _mm256_setzero_ps (); 99const block_nvfp4 * x = (const block_nvfp4 * )row ; 100const int prefetch_ahead = prefetch_distance (); 101for (int ib = 0 ;ib < a .nb ;++ ib ) { 102if (prefetch_ahead > 0 )_mm_prefetch ((const char * ) (x + ib )+ prefetch_ahead ,_MM_HINT_T0 ); 103const __m256i v01 = values32 (x [ib ].qs ,kv ),v23 = values32 (x [ib ].qs + 16 ,kv ); 104const __m256i a01 = _mm256_sign_epi8 (v01 ,v01 ),a23 = _mm256_sign_epi8 (v23 ,v23 ); 105// lanes 0-3 ue4m3(d[0]), 4-7 d[1] (and d[2], d[3]): times a broadcast d_q8, each lane is ggml's scalar 106// ue4m3(d[s]) * d_q8, rounded the same 107const __m256 u01 = _mm256_set_m128 (_mm_set1_ps (kUe4m3 .v [x [ib ].d [1 ]]),_mm_set1_ps (kUe4m3 .v [x [ib ].d [0 ]])); 108const __m256 u23 = _mm256_set_m128 (_mm_set1_ps (kUe4m3 .v [x [ib ].d [3 ]]),_mm_set1_ps (kUe4m3 .v [x [ib ].d [2 ]])); 109for (int t = 0 ;t < NT ;++ t ) { 110const block_q8_0 * yb = a .y [t ]+ 2 * ib ; 111const __m256i q01 = _mm256_loadu_si256 ((const __m256i * )yb [0 ].qs ); 112const __m256i q23 = _mm256_loadu_si256 ((const __m256i * )yb [1 ].qs ); 113const __m256i p1 = _mm256_madd_epi16 (_mm256_maddubs_epi16 (a01 ,_mm256_sign_epi8 (q01 ,v01 )),ones ); 114const __m256i p2 = _mm256_madd_epi16 (_mm256_maddubs_epi16 (a23 ,_mm256_sign_epi8 (q23 ,v23 )),ones ); 115const float * dy = a .dy + (size_t )t * 2 * a .nb + 2 * ib ; 116const __m256 s01 = _mm256_mul_ps (u01 ,_mm256_broadcast_ss (dy )); 117const __m256 s23 = _mm256_mul_ps (u23 ,_mm256_broadcast_ss (dy + 1 )); 118acc [t ]= _mm256_fmadd_ps (s01 ,_mm256_cvtepi32_ps (p1 ),acc [t ]); 119acc [t ]= _mm256_fmadd_ps (s23 ,_mm256_cvtepi32_ps (p2 ),acc [t ]); 120 } 121 } 122for (int t = 0 ;t < NT ;++ t )res [t ]= hsum8 (acc [t ]); 123} 124 125template < int NT > 126void gu_rows (const uint8_t * blob ,size_t gu_row ,size_t up_off ,const Acts & a ,float * const * ff ,int r0 ,int r1 , 127float sg ,float su ) { 128float g [NT ],u [NT ]; 129for (int r = r0 ;r < r1 ;++ r ) { 130row_dot < NT > (blob + (size_t )r * gu_row ,a ,g ); 131row_dot < NT > (blob + up_off + (size_t )r * gu_row ,a ,u ); 132for (int t = 0 ;t < NT ;++ t ) {// native_gu_rows' ggml path, operation for operation 133const float gs = g [t ]* sg ,us = u [t ]* su ; 134ff [t ][r ]= (gs / (1.f + std::exp (- gs )))* us ; 135 } 136 } 137} 138 139template < int NT > 140void dot_rows (const uint8_t * w ,size_t row_bytes ,const Acts & a ,float * const * out ,int r0 ,int r1 ,float scale ) { 141float res [NT ]; 142for (int r = r0 ;r < r1 ;++ r ) { 143row_dot < NT > (w + (size_t )r * row_bytes ,a ,res ); 144for (int t = 0 ;t < NT ;++ t )out [t ][r ]= res [t ]* scale ; 145 } 146} 147 148// windows wider than kMaxW (not from the pool) run in balanced slices of at most kMaxW 149template < typename F > 150inline void slices (int nt ,F && run ) { 151const int k = (nt + kMaxW - 1 ) /kMaxW ; 152for (int i = 0 ,t0 = 0 ;i < k ;++ i ) { 153const int w = nt /k + (i < nt %k ?1 :0 ); 154run (t0 ,w ); 155t0 += w ; 156 } 157} 158 159}// namespace 160 161bool nvfp4_256_fits (int n ) {return n %QK_NVFP4 == 0 && n /QK_NVFP4 <=kMaxBlocks ; } 162 163void nvfp4_256_gu_rows (const uint8_t * blob ,size_t gu_row ,size_t up_off ,int n ,const void * const * act ,int nt , 164float * const * ff ,int r0 ,int r1 ,float s_gate ,float s_up ) { 165slices (nt , [& ](int t0 ,int w ) { 166const Acts a (act + t0 ,w ,n /QK_NVFP4 ); 167float * const * f = ff + t0 ; 168switch (w ) { 169case 1 :gu_rows < 1 > (blob ,gu_row ,up_off ,a ,f ,r0 ,r1 ,s_gate ,s_up );break ; 170case 2 :gu_rows < 2 > (blob ,gu_row ,up_off ,a ,f ,r0 ,r1 ,s_gate ,s_up );break ; 171case 3 :gu_rows < 3 > (blob ,gu_row ,up_off ,a ,f ,r0 ,r1 ,s_gate ,s_up );break ; 172case 4 :gu_rows < 4 > (blob ,gu_row ,up_off ,a ,f ,r0 ,r1 ,s_gate ,s_up );break ; 173case 5 :gu_rows < 5 > (blob ,gu_row ,up_off ,a ,f ,r0 ,r1 ,s_gate ,s_up );break ; 174case 6 :gu_rows < 6 > (blob ,gu_row ,up_off ,a ,f ,r0 ,r1 ,s_gate ,s_up );break ; 175case 7 :gu_rows < 7 > (blob ,gu_row ,up_off ,a ,f ,r0 ,r1 ,s_gate ,s_up );break ; 176default :gu_rows < 8 > (blob ,gu_row ,up_off ,a ,f ,r0 ,r1 ,s_gate ,s_up );break ; 177 } 178 }); 179} 180 181void nvfp4_256_rows (const uint8_t * w ,size_t row_bytes ,int n ,const void * const * act ,int nt ,float * const * out , 182int r0 ,int r1 ,float scale ) { 183slices (nt , [& ](int t0 ,int k ) { 184const Acts a (act + t0 ,k ,n /QK_NVFP4 ); 185float * const * o = out + t0 ; 186switch (k ) { 187case 1 :dot_rows < 1 > (w ,row_bytes ,a ,o ,r0 ,r1 ,scale );break ; 188case 2 :dot_rows < 2 > (w ,row_bytes ,a ,o ,r0 ,r1 ,scale );break ; 189case 3 :dot_rows < 3 > (w ,row_bytes ,a ,o ,r0 ,r1 ,scale );break ; 190case 4 :dot_rows < 4 > (w ,row_bytes ,a ,o ,r0 ,r1 ,scale );break ; 191case 5 :dot_rows < 5 > (w ,row_bytes ,a ,o ,r0 ,r1 ,scale );break ; 192case 6 :dot_rows < 6 > (w ,row_bytes ,a ,o ,r0 ,r1 ,scale );break ; 193case 7 :dot_rows < 7 > (w ,row_bytes ,a ,o ,r0 ,r1 ,scale );break ; 194default :dot_rows < 8 > (w ,row_bytes ,a ,o ,r0 ,r1 ,scale );break ; 195 } 196 }); 197} 198 199}// namespace strata::kernels::cpu