okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
8ac8ce32e
main
1// Strata. The CPU lane of the DeepSeek-V4.1 engine path for ggml's block types (IQ2_XXS .. Q4_K, the experts of a requantized model): 2// ggml-cpu's own row dots (type traits: vec_dot over the Q8_K / Q8_0 activation of vec_dot_type), one row at a time. Maya's lane for 3// these types (native_expert split-base rows) is not in Strata's tree; this one is plain and complete, not tuned. 4#include "ds4_gguf_cpu.hpp" 5 6#include <cmath> 7#include <cstdio> 8#include <cstdlib> 9#include <cstring> 10#include <string> 11 12#if defined(STRATA_DSV41_TQ_CPU ) 13#include "ggml-cpu.h" 14#include "ggml.h" 15#include "strata/kernels/cpu/expert_layout.hpp" 16#include "strata/kernels/cpu/iq_avx2.hpp" 17#include "strata/kernels/cpu/iq_avx512.hpp" 18 19#include <mutex> 20#include <vector> 21#if defined(_MSC_VER ) 22#include <intrin.h> 23#elif defined(__x86_64__ ) 24#include <cpuid.h> 25#endif 26 27namespace strata::ds4::gguf { 28namespace { 29void init_once () { 30static std::once_flag f ; 31 std::call_once (f , [] {ggml_cpu_init (); }); 32} 33size_t row_size (int type ,int64_t k ) {return ggml_row_size ((ggml_type )type ,k ); } 34}// namespace 35 36bool gg_cpu_type (int type ) { 37// every type ggml-cpu has a row dot for (ds4_gguf.cu's known types); the i-quants with a device kernel are usually 38// placed in VRAM, but a pack may hand any of them to this lane 39switch (type ) { 40case 10 :case 11 :case 12 :case 13 :case 14 :case 16 :case 17 :case 18 :case 19 :case 20 :case 21 :case 22 : 41case 23 :case 29 :return true; 42default :return false; 43 } 44} 45 46static bool dbg () {static const bool on = std::getenv ("DS4_DBG_GG" )!= nullptr ;return on ; } 47bool gg_cpu_fmt (int tg ,int td ,int n_embd ,int n_ff ,IqCpu & f , std::string & err ) { 48if (dbg ()) std::fprintf (stderr ,"gg_cpu_fmt %d %d\n" ,tg ,td ); 49init_once (); 50if (!gg_cpu_type (tg )|| !gg_cpu_type (td )) { 51err = "not ggml block types" ; 52return false; 53 } 54const ggml_type_traits_cpu * a = ggml_get_type_traits_cpu ((ggml_type )tg ); 55const ggml_type_traits_cpu * b = ggml_get_type_traits_cpu ((ggml_type )td ); 56if (a == nullptr || b == nullptr || a -> vec_dot == nullptr || b -> vec_dot == nullptr ) { 57err = "ggml-cpu has no row dot for these types" ; 58return false; 59 } 60f .tg = tg ; 61f .td = td ; 62f .n_embd = n_embd ; 63f .n_ff = n_ff ; 64f .gu_act = (int )a -> vec_dot_type ; 65f .d_act = (int )b -> vec_dot_type ; 66f .gu_row = row_size (tg ,n_embd ); 67f .d_row = row_size (td ,n_ff ); 68f .act_bytes = row_size (f .gu_act ,n_embd ); 69f .h_bytes = row_size (f .d_act ,n_ff ); 70return true; 71} 72 73// x (k values) -> the activation type (Q8_K, Q8_0 ..) ggml-cpu's dot takes 74void gg_cpu_quant (int act ,const float * x ,int k ,void * dst ) { 75init_once (); 76if (dbg ()) std::fprintf (stderr ,"gg_cpu_quant act %d k %d\n" ,act ,k ); 77const ggml_type_traits_cpu * tc = ggml_get_type_traits_cpu ((ggml_type )act ); 78if (tc != nullptr && tc -> from_float != nullptr ) { 79tc -> from_float (x ,dst ,k ); 80return ; 81 } 82const ggml_type_traits * t = ggml_get_type_traits ((ggml_type )act ); 83if (dbg ()) std::fprintf (stderr ,"gg_cpu_quant: no cpu from_float, ref %p\n" , (void * ) (t ?t -> from_float_ref :nullptr )); 84t -> from_float_ref (x ,dst ,k ); 85} 86 87// Strata's AVX2 multi-token rows for the i-quants (iq256x_rows: each 32-value chunk decoded once, every token applies it). They 88// are ggml-cpu's vec_dot bit for bit (the integer sums are ggml's and the float additions come in ggml's order: ds4_gg_bench --exact, 89// and own_ok() below checks it once per type on this CPU against the ggml-cpu build in use), so taking them or not never changes an 90// answer, at any number of tokens - the choice is speed only. Measured on the rows alone: no faster than ggml's dot at one token 91// (both wait for the codebook lookups), x1.7-2.9 at 2-4 tokens, x1.4 on IQ3_S already at one; so by default ggml's dot below 92// min_tokens(type) and these rows from there. DS4_GG_IQ256=0 never takes them, =1 always (the old opt-in). 93static int g_iq256 = -1 ;// -1: from the environment and the CPU, 0 off, 1 always, 2 from min_tokens(type) 94void gg_cpu_set_iq256 (int mode ) {g_iq256 = mode ; } 95// IQ1_M from one token too: ggml's own IQ1_M dot (and IQ1_S's) uses _pdep_u64 where the build has BMI2, which AMD Zen 1/2 run in 96// microcode - 1.8 GB/s against these rows' 33 GB/s on an EPYC 7452 (a 2.00 set decodes 3x slower on one card), 19-31 GB/s on Intel. 97// The rows are bit-identical, so taking them at every token count only changes the speed. 98static int min_tokens (int type ) {return type == 21 ?1 :type == 29 ?1 :2 ; } 99static bool own_ok (int type ,int impl ) { 100static std::once_flag once [2 ][32 ]; 101static bool ok [2 ][32 ]; 102const int k = type & 31 ; 103 std::call_once (once [impl ][k ], [& ] { 104ok [impl ][k ]= false; 105const ggml_type_traits_cpu * tc = ggml_get_type_traits_cpu ((ggml_type )type ); 106const ggml_type_traits_cpu * q8 = tc ?ggml_get_type_traits_cpu (tc -> vec_dot_type ) :nullptr ; 107if (tc == nullptr || tc -> vec_dot == nullptr || q8 == nullptr || q8 -> from_float == nullptr )return ; 108const int n = 1024 ,rows = 7 ; 109const size_t rb = ggml_row_size ((ggml_type )type ,n ),bb = ggml_type_size ((ggml_type )type ),ab = ggml_row_size (tc -> vec_dot_type ,n ); 110uint64_t st = 0x9E3779B97F4A7C15ull + (uint64_t )type ; 111auto next = [& st ] {st ^=st <<13 ;st ^=st >>7 ;st ^=st <<17 ;return st ; }; 112 std::vector < uint8_t > w (rb * rows ); 113for (auto & b :w )b = (uint8_t )next (); 114for (size_t o = 0 ;o < w .size ();o += bb ) { 115const ggml_fp16_t d = ggml_fp32_to_fp16 (0.002f + 0.001f * (float ) (next () %16 )); 116if (type == 29 ) {// IQ1_M keeps its f16 in the top nibbles of the four scale words 117uint16_t sc [4 ]; 118 std::memcpy (sc ,& w [o + 48 ],8 ); 119for (int j = 0 ;j < 4 ;++ j )sc [j ]= (uint16_t ) ((sc [j ]& 0x0fff ) | ((d >> (4 * j ))& 0xf ) <<12 ); 120 std::memcpy (& w [o + 48 ],sc ,8 ); 121 }else { 122 std::memcpy (& w [o ],& d ,sizeof d ); 123 } 124 } 125for (int nt : {1 ,2 ,3 ,4 ,5 ,6 ,8 ,16 ,33 }) { 126 std::vector < std::vector < uint8_t >> a ((size_t )nt , std::vector < uint8_t > (ab )); 127 std::vector < const void *> ap ((size_t )nt ); 128 std::vector < std::vector < float >> o0 ((size_t )nt , std::vector < float > (rows )),o1 = o0 ; 129 std::vector < float *> p1 ((size_t )nt ); 130for (int t = 0 ;t < nt ;++ t ) { 131 std::vector < float > x ((size_t )n ); 132for (auto & v :x )v = ((float ) (next () %2001 )- 1000.0f )* 1e-3f ; 133q8 -> from_float (x .data (),a [(size_t )t ].data (),n ); 134ap [(size_t )t ]= a [(size_t )t ].data (); 135p1 [(size_t )t ]= o1 [(size_t )t ].data (); 136for (int r = 0 ;r < rows ;++ r )tc -> vec_dot (n ,& o0 [(size_t )t ][(size_t )r ],0 ,w .data ()+ (size_t )r * rb ,0 ,ap [(size_t )t ],0 ,1 ); 137 } 138if (impl == 1 ) strata::kernels::cpu::iq512x_rows (type ,w .data (),rb ,n ,ap .data (),nt ,p1 .data (),0 ,rows ); 139else strata::kernels::cpu::iq256x_rows (type ,w .data (),rb ,n ,ap .data (),nt ,p1 .data (),0 ,rows ); 140for (int t = 0 ;t < nt ;++ t ) 141if (std::memcmp (o0 [(size_t )t ].data (),o1 [(size_t )t ].data (),rows * sizeof (float ))!= 0 ) { 142if (dbg ()|| std::getenv ("DS4_GG_VERBOSE" )) std::fprintf (stderr ,"ds4: CPU lane type %d: AVX2 rows differ from ggml-cpu's dot at %d tokens, not used\n" ,type ,nt ); 143return ; 144 } 145 } 146ok [impl ][k ]= true; 147 }); 148return ok [impl ][k ]; 149} 150// the AMD family (0x19 Zen 3/4, 0x1A Zen 5), 0 on any other vendor or if it cannot be read 151static int amd_family () { 152static const int fam = [] { 153#if defined(_MSC_VER ) 154int r [4 ]; 155__cpuid (r ,0 ); 156char v [13 ]; 157 std::memcpy (v ,& r [1 ],4 ); std::memcpy (v + 4 ,& r [3 ],4 ); std::memcpy (v + 8 ,& r [2 ],4 ); 158v [12 ]= 0 ; 159if (std::strcmp (v ,"AuthenticAMD" )!= 0 )return 0 ; 160__cpuid (r ,1 ); 161const unsigned eax = (unsigned )r [0 ]; 162#elif defined(__x86_64__ ) 163unsigned a ,b ,c ,d ; 164if (!__get_cpuid (0 ,& a ,& b ,& c ,& d ))return 0 ; 165char v [13 ]; 166 std::memcpy (v ,& b ,4 ); std::memcpy (v + 4 ,& d ,4 ); std::memcpy (v + 8 ,& c ,4 ); 167v [12 ]= 0 ; 168if (std::strcmp (v ,"AuthenticAMD" )!= 0 )return 0 ; 169if (!__get_cpuid (1 ,& a ,& b ,& c ,& d ))return 0 ; 170const unsigned eax = a ; 171#else 172return 0 ; 173#endif 174const unsigned base = (eax >>8 )& 0xF ; 175return (int ) (base == 0xF ?base + ((eax >>20 )& 0xFF ) :base ); 176 }(); 177return fam ; 178} 179// DS4_GG_IQ512: 1 takes the AVX-512 rows (bit-identical too) where the CPU has AVX-512 F/BW/VL/DQ/VNNI/VBMI, 0 never; unset: the rule 180// below, from the measurements on Zen 4. 181static int g_iq512 = -1 ; 182static bool want_512 (int type ,int nt ) { 183if (g_iq512 < 0 ) { 184const char * v = std::getenv ("DS4_GG_IQ512" ); 185g_iq512 = !strata::kernels::cpu::cpu_avx512_ok () ?0 :v == nullptr ?2 : std::atoi (v )!= 0 ?1 :0 ; 186 } 187if (g_iq512 == 0 || !strata::kernels::cpu::iq512x_supported (type ))return false; 188if (g_iq512 == 1 )return true; 189// by CPU (measured, ds4_gg_bench --expert; the rows are bit-identical to ggml's dot everywhere): 190// Zen 5 (AMD family 0x1A, Strix Halo, 16 threads, GB/s of weights ggml -> AVX-512 rows): one token IQ2_XXS 35 -> 44, IQ2_XS 37 -> 48, 191// IQ1_M 32 -> 36 (the sign masks by popcount, IQ1_M's indices by pdep: no table loads), IQ3_XXS 43 -> 45; 4 tokens x2.5-3.5; 16 tokens x4.4-4.8; 192// Zen 4 (family 0x19, Ryzen 5 7600, 6 threads): one token IQ2_XXS 15.9 -> 19.7, IQ2_XS 20.6 -> 24.2, IQ1_M 16.7 -> 17.6-18.2, IQ3_XXS 22.4 -> 23.8 193// (noise-level); 2 tokens x1.5-2.2; 4 tokens x2.4-3.3; 16 tokens x2.8-4.6; 194// anything else with AVX-512 (Intel): not measured, so the AVX2 rows 195const int fam = amd_family (); 196if (fam < 0x19 )return false; 197if (type == 16 || type == 17 )return true; 198if (type == 18 )return nt >=2 ; 199if (type == 29 )return nt >=2 || fam >=0x1A ; 200return false; 201} 202// 0: ggml-cpu's dot, 1: the AVX2 rows, 2: the AVX-512 rows 203static int use_iq256 (int type ,int nt ) { 204if (g_iq256 < 0 ) { 205const char * v = std::getenv ("DS4_GG_IQ256" ); 206// (ggml-cpu's own answer: built with AVX2 + FMA + F16C, which is also what makes its dots the AVX2 ones these rows match) 207const bool avx2 = ggml_cpu_has_avx2 ()&& ggml_cpu_has_fma ()&& ggml_cpu_has_f16c (); 208g_iq256 = !avx2 ?0 :v == nullptr ?2 : std::atoi (v )!= 0 ?1 :0 ; 209 } 210if (g_iq256 == 0 )return 0 ; 211if (want_512 (type ,nt )&& own_ok (type ,1 ))return 2 ; 212if (!strata::kernels::cpu::iq256x_supported (type ))return 0 ; 213if (g_iq256 == 2 && nt < min_tokens (type ))return 0 ; 214return own_ok (type ,0 ) ?1 :0 ; 215} 216 217static void dots (int type ,const uint8_t * w ,size_t rb ,int n ,const void * const * act ,int nt ,float * const * out ,int r0 ,int r1 ) { 218if (nt > 0 ) { 219const int impl = use_iq256 (type ,nt ); 220if (impl == 2 ) { 221 strata::kernels::cpu::iq512x_rows (type ,w ,rb ,n ,act ,nt ,out ,r0 ,r1 ); 222return ; 223 } 224if (impl == 1 ) { 225 strata::kernels::cpu::iq256x_rows (type ,w ,rb ,n ,act ,nt ,out ,r0 ,r1 ); 226return ; 227 } 228 } 229const ggml_type_traits_cpu * tc = ggml_get_type_traits_cpu ((ggml_type )type ); 230if (dbg ()) std::fprintf (stderr ,"gg dots type %d n %d rows %d..%d nt %d\n" ,type ,n ,r0 ,r1 ,nt ); 231for (int r = r0 ;r < r1 ;++ r ) 232for (int t = 0 ;t < nt ;++ t )tc -> vec_dot (n ,& out [t ][r ],0 ,w + (size_t )r * rb ,0 ,act [t ],0 ,1 ); 233} 234 235void gg_cpu_gate_up (const IqCpu & f ,const uint8_t * gate ,const uint8_t * up ,const void * const * act ,int nt ,float * const * ff ,int r0 , 236int r1 ,float limit ) { 237constexpr int kMaxTok = 64 ;// (the prompt path: a chunk's tokens an expert) 238static thread_local std::vector < float > tmp [kMaxTok ]; 239float * uo [kMaxTok ]; 240if (nt > kMaxTok ) {// (more than a pass takes: in pieces) 241for (int t0 = 0 ;t0 < nt ;t0 += kMaxTok ) 242gg_cpu_gate_up (f ,gate ,up ,act + t0 ,nt - t0 < kMaxTok ?nt - t0 :kMaxTok ,ff + t0 ,r0 ,r1 ,limit ); 243return ; 244 } 245for (int t = 0 ;t < nt ;++ t ) { 246tmp [t ].resize ((size_t )r1 ); 247uo [t ]= tmp [t ].data (); 248 } 249dots (f .tg ,gate ,f .gu_row ,f .n_embd ,act ,nt ,ff ,r0 ,r1 ); 250dots (f .tg ,up ,f .gu_row ,f .n_embd ,act ,nt ,uo ,r0 ,r1 ); 251for (int t = 0 ;t < nt ;++ t ) 252for (int r = r0 ;r < r1 ;++ r ) { 253float g = ff [t ][r ],u = uo [t ][r ]; 254if (limit > 0.0f ) { 255g = g < limit ?g :limit ; 256u = u < - limit ?- limit : (u > limit ?limit :u ); 257 } 258ff [t ][r ]= g / (1.0f + std::exp (- g ))* u ; 259 } 260} 261 262void gg_cpu_down (const IqCpu & f ,const uint8_t * down ,const void * const * hq ,int nt ,float * const * out ,int r0 ,int r1 ) { 263dots (f .td ,down ,f .d_row ,f .n_ff ,hq ,nt ,out ,r0 ,r1 ); 264} 265 266// a row of any ggml block type -> BF16 on the host (the embedding table's reader; Q5_K / Q4_K / Q6_K / Q8_0 rows of a 267// GGUF whose dense tensors are not BF16): ggml's own to_float, then the same round-to-nearest-even to BF16 the Q8_0 path uses 268bool gg_row_to_bf16 (int type ,const uint8_t * row ,int k ,uint16_t * out ) { 269init_once (); 270const ggml_type_traits * t = ggml_get_type_traits ((ggml_type )type ); 271if (t == nullptr || t -> to_float == nullptr )return false; 272static thread_local std::vector < float > f ; 273f .resize ((size_t )k ); 274t -> to_float (row ,f .data (),k ); 275for (int i = 0 ;i < k ;++ i ) { 276uint32_t u ; 277 std::memcpy (& u ,& f [(size_t )i ],4 ); 278u += 0x7fffu + ((u >>16 )& 1u ); 279out [i ]= (uint16_t ) (u >>16 ); 280 } 281return true; 282} 283}// namespace strata::ds4::gguf 284#else 285namespace strata::ds4::gguf { 286bool gg_cpu_type (int ) {return false; } 287bool gg_cpu_fmt (int ,int ,int ,int ,IqCpu & , std::string & err ) { 288err = "this build has no ggml-cpu lane (STRATA_NATIVE_EXPERTS=OFF)" ; 289return false; 290} 291void gg_cpu_quant (int ,const float * ,int ,void * ) {} 292void gg_cpu_gate_up (const IqCpu & ,const uint8_t * ,const uint8_t * ,const void * const * ,int ,float * const * ,int ,int ,float ) {} 293void gg_cpu_down (const IqCpu & ,const uint8_t * ,const void * const * ,int ,float * const * ,int ,int ) {} 294bool gg_row_to_bf16 (int ,const uint8_t * ,int ,uint16_t * ) {return false; } 295}// namespace strata::ds4::gguf 296#endif