okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
0b6fc1cec
main
1// src/kernels/cpu/nvfp4_avx512.cpp - see include/strata/kernels/cpu/nvfp4_avx512.hpp. 2// 3// Per 64-value block, once: the 64 E2M1 codes in element order, their magnitudes from |kvalues_fp4| (0..12, the 4// doubled E2M1 values), a sign mask (code bit 3), and the four sub-block scales. Per token: the sign moves onto the 5// activation (a masked subtract - Q8_0 is quantized to [-127, 127], so negation never overflows), one vpdpbusd gives 6// 16 int32 lanes = four per 16-value sub-block, and one FMA applies ue4m3(d[s]) * d_q8. The UE4M3 decode is ggml's 7// CPU one (ggml-impl.h ggml_ue4m3_to_fp32): halved for the doubled table, 0x7F read as 0. 8// 9// Needs AVX512F/BW/VL and VNNI - what cpu_avx512_ok() checks before anything here is called. 10#include "strata/kernels/cpu/nvfp4_avx512.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): the rows stream from DRAM through 4 KB 48// pages, where the hardware prefetchers stop at every page boundary; the IQ kernel's E-2 knob, same default 49// (read on first use, behind the CPU check: a static initialiser here would run on every CPU, see above) 50inline int prefetch_distance () { 51static const int v = [] { 52const char * e = std::getenv ("STRATA_NVFP4_PREFETCH" ); 53return e ? std::atoi (e ) :2048 ; 54 }(); 55return v ; 56} 57 58inline float h2f (uint16_t h ) {return _mm_cvtss_f32 (_mm_cvtph_ps (_mm_cvtsi32_si128 ((int )h ))); } 59 60// The block's 64 codes in element order. Sub-block s keeps its codes in qs[8 s .. 8 s + 7]: byte j holds value j 61// in its low nibble and value j + 8 in its high one, so a sub-block is [low nibbles, high nibbles] of its 8 bytes. 62inline __m512i codes64 (const uint8_t * qs ) { 63const __m256i q = _mm256_loadu_si256 ((const __m256i * )qs ); 64const __m256i m4 = _mm256_set1_epi8 (0x0F ); 65const __m256i lo = _mm256_and_si256 (q ,m4 ); 66const __m256i hi = _mm256_and_si256 (_mm256_srli_epi16 (q ,4 ),m4 ); 67const __m256i ul = _mm256_unpacklo_epi64 (lo ,hi );// [lo 0-7, hi 0-7 | lo 16-23, hi 16-23] 68const __m256i uh = _mm256_unpackhi_epi64 (lo ,hi );// [lo 8-15, hi 8-15 | lo 24-31, hi 24-31] 69const __m256i s01 = _mm256_permute2x128_si256 (ul ,uh ,0x20 );// sub-blocks 0, 1 70const __m256i s23 = _mm256_permute2x128_si256 (ul ,uh ,0x31 );// sub-blocks 2, 3 71return _mm512_inserti64x4 (_mm512_castsi256_si512 (s01 ),s23 ,1 ); 72} 73 74// lanes 4 s .. 4 s + 3 = ue4m3(d[s]) 75inline __m512 sub_scales (const uint8_t * d ) { 76const __m128 s4 = _mm_setr_ps (kUe4m3 .v [d [0 ]],kUe4m3 .v [d [1 ]],kUe4m3 .v [d [2 ]],kUe4m3 .v [d [3 ]]); 77const __m512i idx = _mm512_setr_epi32 (0 ,0 ,0 ,0 ,1 ,1 ,1 ,1 ,2 ,2 ,2 ,2 ,3 ,3 ,3 ,3 ); 78return _mm512_permutexvar_ps (idx ,_mm512_castps128_ps512 (s4 )); 79} 80 81// Per token and block, the activation's scales as lanes: 0-7 -> the first Q8_0 block's d (sub-blocks 0, 1), 82// 8-15 -> the second's (sub-blocks 2, 3). Built once per call, reused by every row. 83constexpr int kMaxBlocks = 64 ;// rows of up to 4096 values (n_embd 2560) 84struct Acts { 85int nt = 0 ,nb = 0 ; 86const block_q8_0 * y [7 ]= {}; 87__m512 dy [7 * kMaxBlocks ];// [t * nb + ib]; on the stack, no allocation 88Acts (const void * const * act ,int nt_ ,int nb_ ) :nt (nt_ ),nb (nb_ ) { 89for (int t = 0 ;t < nt ;++ t ) { 90y [t ]= (const block_q8_0 * )act [t ]; 91for (int ib = 0 ;ib < nb ;++ ib ) { 92const block_q8_0 * yb = y [t ]+ 2 * ib ; 93dy [(size_t )t * nb + ib ]= _mm512_mask_blend_ps ((__mmask16 )0xFF00 ,_mm512_set1_ps (h2f (yb [0 ].d )), 94_mm512_set1_ps (h2f (yb [1 ].d ))); 95 } 96 } 97 } 98}; 99 100template < int NT > 101inline void row_dot (const uint8_t * row ,const Acts & a ,float * res ) { 102const __m512i mag = _mm512_broadcast_i32x4 (_mm_setr_epi8 (0 ,1 ,2 ,3 ,4 ,6 ,8 ,12 ,0 ,1 ,2 ,3 ,4 ,6 ,8 ,12 )); 103const __m512i eight = _mm512_set1_epi8 (8 ); 104const __m512i zero = _mm512_setzero_si512 (); 105__m512 acc [NT ]; 106for (int t = 0 ;t < NT ;++ t )acc [t ]= _mm512_setzero_ps (); 107const block_nvfp4 * x = (const block_nvfp4 * )row ; 108const int prefetch_ahead = prefetch_distance (); 109for (int ib = 0 ;ib < a .nb ;++ ib ) { 110if (prefetch_ahead > 0 )_mm_prefetch ((const char * ) (x + ib )+ prefetch_ahead ,_MM_HINT_T0 ); 111const __m512i codes = codes64 (x [ib ].qs ); 112const __m512i g = _mm512_shuffle_epi8 (mag ,codes ); 113const __mmask64 neg = _mm512_test_epi8_mask (codes ,eight ); 114const __m512 sw = sub_scales (x [ib ].d ); 115for (int t = 0 ;t < NT ;++ t ) { 116const block_q8_0 * yb = a .y [t ]+ 2 * ib ; 117const __m512i yv = _mm512_inserti64x4 (_mm512_castsi256_si512 (_mm256_loadu_si256 ((const __m256i * )yb [0 ].qs )), 118_mm256_loadu_si256 ((const __m256i * )yb [1 ].qs ),1 ); 119const __m512i ys = _mm512_mask_sub_epi8 (yv ,neg ,zero ,yv ); 120const __m512i p = _mm512_dpbusd_epi32 (zero ,g ,ys ); 121acc [t ]= _mm512_fmadd_ps (_mm512_mul_ps (sw ,a .dy [(size_t )t * a .nb + ib ]),_mm512_cvtepi32_ps (p ),acc [t ]); 122 } 123 } 124for (int t = 0 ;t < NT ;++ t )res [t ]= _mm512_reduce_add_ps (acc [t ]); 125} 126 127template < int NT > 128void gu_rows (const uint8_t * blob ,size_t gu_row ,size_t up_off ,const Acts & a ,float * const * ff ,int r0 ,int r1 , 129float sg ,float su ) { 130float g [NT ],u [NT ]; 131for (int r = r0 ;r < r1 ;++ r ) { 132row_dot < NT > (blob + (size_t )r * gu_row ,a ,g ); 133row_dot < NT > (blob + up_off + (size_t )r * gu_row ,a ,u ); 134for (int t = 0 ;t < NT ;++ t ) { 135const float gs = g [t ]* sg ; 136ff [t ][r ]= (gs / (1.f + std::exp (- gs )))* (u [t ]* su ); 137 } 138 } 139} 140 141template < int NT > 142void dot_rows (const uint8_t * w ,size_t row_bytes ,const Acts & a ,float * const * out ,int r0 ,int r1 ,float scale ) { 143float res [NT ]; 144for (int r = r0 ;r < r1 ;++ r ) { 145row_dot < NT > (w + (size_t )r * row_bytes ,a ,res ); 146for (int t = 0 ;t < NT ;++ t )out [t ][r ]= res [t ]* scale ; 147 } 148} 149 150}// namespace 151 152bool nvfp4_512_fits (int n ) {return n %QK_NVFP4 == 0 && n /QK_NVFP4 <=kMaxBlocks ; } 153 154// Windows wider than 7 run in balanced slices of at most 7: at 8 the accumulators no longer fit beside the loads and 155// MSVC spills (measured 0.32 ms for 8 tokens against 2 x 0.13 for 4 + 4). 156template < typename F > 157inline void slices (int nt ,F && run ) { 158const int k = (nt + 6 ) /7 ; 159for (int i = 0 ,t0 = 0 ;i < k ;++ i ) { 160const int w = nt /k + (i < nt %k ?1 :0 ); 161run (t0 ,w ); 162t0 += w ; 163 } 164} 165 166void nvfp4_512_gu_rows (const uint8_t * blob ,size_t gu_row ,size_t up_off ,int n ,const void * const * act ,int nt , 167float * const * ff ,int r0 ,int r1 ,float s_gate ,float s_up ) { 168slices (nt , [& ](int t0 ,int w ) { 169const Acts a (act + t0 ,w ,n /QK_NVFP4 ); 170float * const * f = ff + t0 ; 171switch (w ) { 172case 1 :gu_rows < 1 > (blob ,gu_row ,up_off ,a ,f ,r0 ,r1 ,s_gate ,s_up );break ; 173case 2 :gu_rows < 2 > (blob ,gu_row ,up_off ,a ,f ,r0 ,r1 ,s_gate ,s_up );break ; 174case 3 :gu_rows < 3 > (blob ,gu_row ,up_off ,a ,f ,r0 ,r1 ,s_gate ,s_up );break ; 175case 4 :gu_rows < 4 > (blob ,gu_row ,up_off ,a ,f ,r0 ,r1 ,s_gate ,s_up );break ; 176case 5 :gu_rows < 5 > (blob ,gu_row ,up_off ,a ,f ,r0 ,r1 ,s_gate ,s_up );break ; 177case 6 :gu_rows < 6 > (blob ,gu_row ,up_off ,a ,f ,r0 ,r1 ,s_gate ,s_up );break ; 178default :gu_rows < 7 > (blob ,gu_row ,up_off ,a ,f ,r0 ,r1 ,s_gate ,s_up );break ; 179 } 180 }); 181} 182 183void nvfp4_512_rows (const uint8_t * w ,size_t row_bytes ,int n ,const void * const * act ,int nt ,float * const * out , 184int r0 ,int r1 ,float scale ) { 185slices (nt , [& ](int t0 ,int k ) { 186const Acts a (act + t0 ,k ,n /QK_NVFP4 ); 187float * const * o = out + t0 ; 188switch (k ) { 189case 1 :dot_rows < 1 > (w ,row_bytes ,a ,o ,r0 ,r1 ,scale );break ; 190case 2 :dot_rows < 2 > (w ,row_bytes ,a ,o ,r0 ,r1 ,scale );break ; 191case 3 :dot_rows < 3 > (w ,row_bytes ,a ,o ,r0 ,r1 ,scale );break ; 192case 4 :dot_rows < 4 > (w ,row_bytes ,a ,o ,r0 ,r1 ,scale );break ; 193case 5 :dot_rows < 5 > (w ,row_bytes ,a ,o ,r0 ,r1 ,scale );break ; 194case 6 :dot_rows < 6 > (w ,row_bytes ,a ,o ,r0 ,r1 ,scale );break ; 195default :dot_rows < 7 > (w ,row_bytes ,a ,o ,r0 ,r1 ,scale );break ; 196 } 197 }); 198} 199 200}// namespace strata::kernels::cpu