okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
fd19246dc
main
1// src/kernels/cpu/expert_layout.cpp - plan v0.3 P6: the per-layer expert table. See the header. 2#include "strata/kernels/cpu/expert_layout.hpp" 3#include "strata/kernels/cpu/hadamard.hpp" 4 5#include <atomic> 6#include <cstdio> 7#include <cstdlib> 8#include <cstring> 9#include <string> 10#if defined(__aarch64__ )|| defined(__ARM_ARCH )|| defined(STRATA_CPU_ARM64 ) 11// arm64: no x86 intrinsics to include (the cpuid probes below are compiled out; on Windows ARM64 clang-cl also 12// defines _MSC_VER, which is why the _MSC_VER branch is not enough) 13#elif defined(_MSC_VER ) 14#include <intrin.h> 15#include <immintrin.h> 16#elif defined(__x86_64__ )|| defined(__i386__ ) 17#include <cpuid.h> 18#endif 19#include <fstream> 20#include <sstream> 21 22namespace strata::kernels::cpu { 23namespace { 24ExpertLayout g_layout ; 25} 26 27const ExpertLayout & expert_layout () {return g_layout ; } 28 29int cpu_isa_cap () { 30// STRATA_FORCE_ISA (tests, the experimental older-CPU builds): the engine's own dispatch acts as if this CPU 31// stopped at that level. ggml-cpu is not affected: it runs what the build compiled it for. 32static const int cap = [] { 33const char * f = std::getenv ("STRATA_FORCE_ISA" ); 34if (f == nullptr || f [0 ]== '\0' )return 3 ; 35const std::string v (f ); 36if (v == "avx2" )return 2 ; 37if (v == "avx" )return 1 ; 38if (v == "sse" || v == "sse4.2" || v == "none" )return 0 ; 39 std::fprintf (stderr ,"strata: STRATA_FORCE_ISA=%s is not avx2, avx or sse; ignored\n" ,f ); 40return 3 ; 41 }(); 42return cap ; 43} 44 45#if !defined(__x86_64__ )&& !defined(__i386__ )&& !defined(_M_X64 )&& !defined(_M_IX86 ) 46bool cpu_avx512_ok () {return false; } 47bool cpu_avx512bw_ok () {return false; } 48bool cpu_avx2_ok () {return false; } 49bool cpu_avx1_ok () {return false; } 50bool cpu_sse42_ok () {return false; } 51bool cpu_gather_fast () {return false; } 52bool cpu_gather_fast_here () {return false; } 53bool cpu_avxvnni_ok () {return false; } 54bool cpu_vnni_ok () {return false; } 55std::string cpu_name () { 56#if defined(__APPLE__ ) 57return "unknown" ;// as before: the Metal build's cpuid shim read all zeros 58#else 59 std::ifstream f ("/proc/cpuinfo" ); 60 std::string line ; 61while (std::getline (f ,line )) { 62if (line .rfind ("model name" ,0 )== 0 || line .rfind ("Model" ,0 )== 0 ) { 63const size_t c = line .find (':' ); 64if (c != std::string::npos ) { 65const size_t b = line .find_first_not_of (" \t" ,c + 1 ); 66if (b != std::string::npos )return line .substr (b ); 67 } 68 } 69 } 70#if defined(__aarch64__ ) 71return "aarch64" ; 72#else 73return "unknown" ; 74#endif 75#endif // __APPLE__ 76} 77#else 78bool cpu_isa_free () {// STRATA_FORCE_AVX2 and the ISA cap, shared by the probes 79if (const char * f = std::getenv ("STRATA_FORCE_AVX2" );f != nullptr && f [0 ]== '1' )return false; 80return cpu_isa_cap () >=3 ; 81} 82 83bool cpu_avx512_ok () { 84static const bool ok = [] { 85if (!cpu_isa_free ())return false; 86unsigned r [4 ]= {0 ,0 ,0 ,0 }; 87auto cpuid = [& ](unsigned leaf ,unsigned sub ) { 88#if defined(_MSC_VER ) 89int x [4 ]; 90__cpuidex (x , (int )leaf , (int )sub ); 91for (int i = 0 ;i < 4 ;++ i )r [i ]= (unsigned )x [i ]; 92#else 93__cpuid_count (leaf ,sub ,r [0 ],r [1 ],r [2 ],r [3 ]); 94#endif 95 }; 96cpuid (0 ,0 ); 97if (r [0 ]< 7 )return false; 98cpuid (1 ,0 ); 99if (!((r [2 ] >>27 )& 1u ))return false;// OSXSAVE 100#if defined(_MSC_VER ) 101const unsigned long long xcr0 = _xgetbv (0 ); 102#else 103unsigned lo = 0 ,hi = 0 ; 104#if !defined(__aarch64__ )// arm64 (the Metal build): no XCR0; cpuid reads zero there, so this is never reached 105 __asm__volatile ("xgetbv" :"=a" (lo ),"=d" (hi ) :"c" (0 )); 106#endif 107const unsigned long long xcr0 = ((unsigned long long )hi <<32 ) |lo ; 108#endif 109if ((xcr0 & 0xE6 )!= 0xE6 )return false;// the OS saves the AVX-512 state 110cpuid (7 ,0 ); 111const unsigned ebx = r [1 ],ecx = r [2 ]; 112return ((ebx >>16 )& 1u )&& ((ebx >>30 )& 1u )&& ((ebx >>31 )& 1u )&& ((ecx >>11 )& 1u )&& ((ecx >>1 )& 1u ); 113 }(); 114return ok ; 115} 116 117bool cpu_avx512bw_ok () { 118static const bool ok = [] { 119if (const char * f = std::getenv ("STRATA_FORCE_AVX2" );f != nullptr && f [0 ]== '1' )return false; 120unsigned r [4 ]= {0 ,0 ,0 ,0 }; 121auto cpuid = [& ](unsigned leaf ,unsigned sub ) { 122#if defined(_MSC_VER ) 123int x [4 ]; 124__cpuidex (x , (int )leaf , (int )sub ); 125for (int i = 0 ;i < 4 ;++ i )r [i ]= (unsigned )x [i ]; 126#else 127__cpuid_count (leaf ,sub ,r [0 ],r [1 ],r [2 ],r [3 ]); 128#endif 129 }; 130cpuid (0 ,0 ); 131if (r [0 ]< 7 )return false; 132cpuid (1 ,0 ); 133if (!((r [2 ] >>27 )& 1u ))return false;// OSXSAVE 134#if defined(_MSC_VER ) 135const unsigned long long xcr0 = _xgetbv (0 ); 136#else 137unsigned lo = 0 ,hi = 0 ; 138#if !defined(__aarch64__ )// arm64 (the Metal build): no XCR0; cpuid reads zero there, so this is never reached 139 __asm__volatile ("xgetbv" :"=a" (lo ),"=d" (hi ) :"c" (0 )); 140#endif 141const unsigned long long xcr0 = ((unsigned long long )hi <<32 ) |lo ; 142#endif 143if ((xcr0 & 0xE6 )!= 0xE6 )return false;// the OS saves the AVX-512 state 144cpuid (7 ,0 ); 145const unsigned ebx = r [1 ]; 146return ((ebx >>16 )& 1u )&& ((ebx >>17 )& 1u )&& ((ebx >>30 )& 1u )&& ((ebx >>31 )& 1u ); 147 }(); 148return ok ; 149} 150 151static void isa_probe (unsigned & ebx ,unsigned & ecx ) { 152unsigned r [4 ]= {0 ,0 ,0 ,0 }; 153auto cpuid = [& ](unsigned leaf ,unsigned sub ) { 154#if defined(_MSC_VER ) 155int x [4 ]; 156__cpuidex (x , (int )leaf , (int )sub ); 157for (int i = 0 ;i < 4 ;++ i )r [i ]= (unsigned )x [i ]; 158#else 159__cpuid_count (leaf ,sub ,r [0 ],r [1 ],r [2 ],r [3 ]); 160#endif 161 }; 162cpuid (0 ,0 ); 163if (r [0 ]< 7 )return ; 164cpuid (1 ,0 ); 165if (!((r [2 ] >>27 )& 1u ))return ;// OSXSAVE 166#if defined(_MSC_VER ) 167const unsigned long long xcr0 = _xgetbv (0 ); 168#else 169unsigned lo = 0 ,hi = 0 ; 170 __asm__volatile ("xgetbv" :"=a" (lo ),"=d" (hi ) :"c" (0 )); 171const unsigned long long xcr0 = ((unsigned long long )hi <<32 ) |lo ; 172#endif 173if ((xcr0 & 0xE6 )!= 0xE6 )return ;// the OS saves the AVX-512 state 174cpuid (7 ,0 ); 175ebx = (unsigned )r [1 ]; 176ecx = (unsigned )r [2 ]; 177} 178 179bool cpu_vbmi_ok () {// the byte-permute shuffles (VPERMB, VPMULTISHIFTQB) 180// STRATA_NO_VBMI=1 answers no (a test knob: it runs the substitute q2 unpack on a CPU that has VBMI) 181static const bool ok = cpu_isa_free ()&& std::getenv ("STRATA_NO_VBMI" )== nullptr && [] { 182unsigned ebx = 0 ,ecx = 0 ; 183isa_probe (ebx ,ecx ); 184// AVX-512F + VL + VBMI (ECX bit 1). Cascade Lake has F/BW/VL/VNNI but NOT VBMI: that is the 185// whole reason for the substitute unpack in expert.cpp, so this bit must not be confused with F. 186return bool (((ebx >>16 )& 1u )&& ((ebx >>31 )& 1u )&& ((ecx >>1 )& 1u )); 187 }(); 188return ok ; 189} 190 191bool cpu_vnni_ok () {// VNNI's dpbusd at all, the integer dot at half the lanes 192static const bool ok = cpu_isa_free ()&& [] { 193unsigned ebx = 0 ,ecx = 0 ; 194isa_probe (ebx ,ecx ); 195// AVX-512F + DQ + BW + VL + VNNI (ECX bit 11), VBMI not required: the Cascade Lake Xeon tier (DQ is there on every VNNI CPU; asked for 196// because iq_avx512.cpp is compiled with it). 197const bool vnni = bool (((ebx >>16 )& 1u )&& ((ebx >>17 )& 1u )&& ((ebx >>30 )& 1u )&& ((ebx >>31 )& 1u )&& ((ecx >>11 )& 1u )); 198if (!vnni )return false; 199// The tier below VBMI is OPT-IN (STRATA_VNNI_TIER=1): on such a CPU the 512-bit i-quant and Q2_0 rows round differently from the 200// AVX-2 rows it runs today (measured under SDE Cascade Lake: every multi-token gate/up line and the Q2_0 rows differ), so turning it on 201// changes that CPU's output. Every CPU that has VBMI as well answers as before. 202if (cpu_avx512_ok ())return true; 203const char * e = std::getenv ("STRATA_VNNI_TIER" ); 204return e != nullptr && e [0 ]== '1' ; 205 }(); 206return ok ; 207} 208 209bool cpu_avx2_ok () { 210static const bool ok = [] { 211if (cpu_isa_cap ()< 2 )return false; 212unsigned r [4 ]= {0 ,0 ,0 ,0 }; 213auto cpuid = [& ](unsigned leaf ,unsigned sub ) { 214#if defined(_MSC_VER ) 215int x [4 ]; 216__cpuidex (x , (int )leaf , (int )sub ); 217for (int i = 0 ;i < 4 ;++ i )r [i ]= (unsigned )x [i ]; 218#else 219__cpuid_count (leaf ,sub ,r [0 ],r [1 ],r [2 ],r [3 ]); 220#endif 221 }; 222cpuid (0 ,0 ); 223if (r [0 ]< 7 )return false; 224cpuid (1 ,0 ); 225const unsigned ecx1 = r [2 ]; 226// FMA (12), OSXSAVE (27), AVX (28), F16C (29) 227if (!((ecx1 >>12 )& 1u )|| !((ecx1 >>27 )& 1u )|| !((ecx1 >>28 )& 1u )|| !((ecx1 >>29 )& 1u ))return false; 228#if defined(_MSC_VER ) 229const unsigned long long xcr0 = _xgetbv (0 ); 230#else 231unsigned lo = 0 ,hi = 0 ; 232#if !defined(__aarch64__ )// arm64 (the Metal build): no XCR0; cpuid reads zero there, so this is never reached 233 __asm__volatile ("xgetbv" :"=a" (lo ),"=d" (hi ) :"c" (0 )); 234#endif 235const unsigned long long xcr0 = ((unsigned long long )hi <<32 ) |lo ; 236#endif 237if ((xcr0 & 0x6 )!= 0x6 )return false;// the OS saves the SSE and AVX state 238cpuid (7 ,0 ); 239return ((r [1 ] >>5 )& 1u )!= 0 ;// AVX2 240 }(); 241return ok ; 242} 243 244bool cpu_avx1_ok () { 245// AVX (Sandy Bridge, 2011): AVX + OSXSAVE with the OS saving the YMM state; FMA, F16C and AVX2 not needed. 246// From the Strata_Dirigo fork (rwkeyes). 247static const bool ok = [] { 248if (cpu_isa_cap ()< 1 )return false; 249unsigned r [4 ]= {0 ,0 ,0 ,0 }; 250#if defined(_MSC_VER ) 251int x [4 ]; 252__cpuidex (x ,1 ,0 ); 253for (int i = 0 ;i < 4 ;++ i )r [i ]= (unsigned )x [i ]; 254#else 255__cpuid_count (1 ,0 ,r [0 ],r [1 ],r [2 ],r [3 ]); 256#endif 257if (!((r [2 ] >>27 )& 1u )|| !((r [2 ] >>28 )& 1u ))return false;// OSXSAVE, AVX 258#if defined(_MSC_VER ) 259const unsigned long long xcr0 = _xgetbv (0 ); 260#else 261unsigned lo = 0 ,hi = 0 ; 262#if !defined(__aarch64__ )// arm64 (the Metal build): no XCR0; cpuid reads zero there, so this is never reached 263 __asm__volatile ("xgetbv" :"=a" (lo ),"=d" (hi ) :"c" (0 )); 264#endif 265const unsigned long long xcr0 = ((unsigned long long )hi <<32 ) |lo ; 266#endif 267return (xcr0 & 0x6 )== 0x6 ; 268 }(); 269return ok ; 270} 271 272bool cpu_sse42_ok () { 273static const bool ok = [] { 274unsigned r [4 ]= {0 ,0 ,0 ,0 }; 275#if defined(_MSC_VER ) 276int x [4 ]; 277__cpuidex (x ,1 ,0 ); 278for (int i = 0 ;i < 4 ;++ i )r [i ]= (unsigned )x [i ]; 279#else 280__cpuid_count (1 ,0 ,r [0 ],r [1 ],r [2 ],r [3 ]); 281#endif 282return ((r [2 ] >>20 )& 1u )&& ((r [2 ] >>23 )& 1u );// SSE4.2, POPCNT 283 }(); 284return ok ; 285} 286 287#endif // x86 cpu probes 288 289const char * isa_floor_build () { 290#if defined(STRATA_ISA_FLOOR_AVX ) 291return "avx" ; 292#elif defined(STRATA_ISA_FLOOR_NONE ) 293return "sse4.2" ; 294#else 295return "" ; 296#endif 297} 298 299#if defined(__x86_64__ )|| defined(__i386__ )|| defined(_M_X64 )|| defined(_M_IX86 ) 300namespace { 301void cpuid_regs (unsigned leaf ,unsigned sub ,unsigned r [4 ]) { 302#if defined(_MSC_VER ) 303int x [4 ]; 304__cpuidex (x , (int )leaf , (int )sub ); 305for (int i = 0 ;i < 4 ;++ i )r [i ]= (unsigned )x [i ]; 306#else 307__cpuid_count (leaf ,sub ,r [0 ],r [1 ],r [2 ],r [3 ]); 308#endif 309} 310}// namespace 311#endif // x86 cpuid helper 312 313int iq256_gather_setting () { 314static const int s = [] { 315const char * v = std::getenv ("STRATA_IQ256_GATHER" ); 316if (v == nullptr || v [0 ]== '\0' )return -1 ; 317return std::atoi (v )!= 0 ?1 :0 ; 318 }(); 319return s ; 320} 321 322#if defined(__x86_64__ )|| defined(__i386__ )|| defined(_M_X64 )|| defined(_M_IX86 ) 323bool cpu_gather_fast () { 324static const bool ok = [] { 325if (cpu_isa_cap ()< 3 )return false;// STRATA_FORCE_ISA: as on a CPU that stops at AVX2 326unsigned r [4 ]; 327cpuid_regs (0 ,0 ,r ); 328const unsigned max_leaf = r [0 ]; 329if (!(r [1 ]== 0x756e6547u && r [3 ]== 0x49656e69u && r [2 ]== 0x6c65746eu ))return false;// "GenuineIntel" 330if (max_leaf < 7 )return false; 331cpuid_regs (7 ,0 ,r ); 332if (r [0 ]< 1 )return false;// no sub-leaf 1 333cpuid_regs (7 ,1 ,r ); 334if (!((r [0 ] >>4 )& 1u ))return false;// AVX-VNNI: Alder Lake, Sapphire Rapids and later 335// The E-core-only parts with AVX-VNNI are not hybrid, so the core type below may not tell them apart: Alder 336// Lake-N / Twin Lake (6/BEh), Grand Ridge (6/B6h), Sierra Forest (6/AFh), Clearwater Forest (6/DDh). 337cpuid_regs (1 ,0 ,r ); 338const unsigned family = (r [0 ] >>8 )& 0xf ,model = ((r [0 ] >>4 )& 0xf ) | (((r [0 ] >>16 )& 0xf ) <<4 ); 339if (family == 6 && (model == 0xBE || model == 0xB6 || model == 0xAF || model == 0xDD ))return false; 340return true; 341 }(); 342return ok ; 343} 344 345bool cpu_gather_fast_here () { 346if (!cpu_gather_fast ())return false; 347static const unsigned max_leaf = [] {unsigned r [4 ];cpuid_regs (0 ,0 ,r );return r [0 ]; }(); 348static const bool hybrid = [] {unsigned r [4 ];cpuid_regs (7 ,0 ,r );return ((r [3 ] >>15 )& 1u )!= 0 ; }(); 349// A CPUID can cost a microsecond under a hypervisor (Windows with VBS), so once per thread. 350 thread_localint here = -1 ; 351if (here < 0 ) { 352unsigned type = 0 ; 353if (max_leaf >=0x1A ) { 354unsigned r [4 ]; 355cpuid_regs (0x1A ,0 ,r ); 356type = r [0 ] >>24 ; 357 } 358// 40h a performance core, 20h an E-core; no core type is a non-hybrid part (P-cores, see cpu_gather_fast) 359here = type == 0x40 || (type == 0 && !hybrid ) ?1 :0 ; 360 } 361return here == 1 ; 362} 363 364bool cpu_avxvnni_ok () { 365static const bool ok = [] { 366if (const char * v = std::getenv ("STRATA_NO_AVXVNNI" );v != nullptr && std::atoi (v )!= 0 )return false; 367if (isa_floor_build ()[0 ]!= '\0' || cpu_isa_cap ()< 3 || !cpu_avx2_ok ())return false; 368unsigned r [4 ]; 369cpuid_regs (0 ,0 ,r ); 370if (r [0 ]< 7 )return false; 371cpuid_regs (7 ,0 ,r ); 372if (r [0 ]< 1 )return false;// no sub-leaf 1 373cpuid_regs (7 ,1 ,r ); 374return ((r [0 ] >>4 )& 1u )!= 0 ;// AVX-VNNI; VEX-encoded, so the OS state is AVX's (cpu_avx2_ok) 375 }(); 376return ok ; 377} 378 379std::string cpu_name () { 380unsigned r [12 ]= {}; 381#if defined(_MSC_VER ) 382int x [4 ]; 383__cpuid (x , (int )0x80000000u ); 384if ((unsigned )x [0 ]< 0x80000004u )return "unknown" ; 385for (unsigned i = 0 ;i < 3 ;++ i ) { 386__cpuid (x , (int ) (0x80000002u + i )); 387for (int j = 0 ;j < 4 ;++ j )r [i * 4 + j ]= (unsigned )x [j ]; 388 } 389#else 390unsigned a = 0 ,b = 0 ,c = 0 ,d = 0 ; 391__cpuid (0x80000000u ,a ,b ,c ,d ); 392if (a < 0x80000004u )return "unknown" ; 393for (unsigned i = 0 ;i < 3 ;++ i )__cpuid (0x80000002u + i ,r [i * 4 ],r [i * 4 + 1 ],r [i * 4 + 2 ],r [i * 4 + 3 ]); 394#endif 395char s [49 ]= {}; 396 std::memcpy (s ,r ,48 ); 397 std::string name (s ); 398const size_t b0 = name .find_first_not_of (' ' ),b1 = name .find_last_not_of (' ' ); 399return b0 == std::string::npos ? std::string ("unknown" ) :name .substr (b0 ,b1 - b0 + 1 ); 400} 401#endif // x86 cpuid probes 402 403// ---- the AVX-512 probe and the oracle flag (#795) ---- 404// 405// expert.cpp and iq_avx512.cpp are compiled for AVX-512, and a TU compiled that way may use AVX-512 in ANY of its 406// code. The code that decides whether this CPU has AVX-512 - and the startup flag every CPU, AVX2-only ones 407// included, runs before that decision - therefore lives here, in a file compiled for the x86-64 baseline. 408namespace { 409std::atomic < bool > g_oracle_q8_0 {false}; 410} 411 412void expert_set_oracle_q8_0 (bool enabled ) {g_oracle_q8_0 .store (enabled , std::memory_order_relaxed ); } 413 414bool expert_oracle_q8_0_enabled () {return g_oracle_q8_0 .load (std::memory_order_relaxed ); } 415 416const char * CpuFeatures ::reason ()const { 417if (usable ())return "ok" ; 418// Named individually: "AVX-512 not supported" sends a user looking for a new CPU when the machine may have 419// AVX-512F and be missing only VNNI, which is a much narrower and more explicable gap. 420static char buf [160 ]; 421 std::snprintf (buf ,sizeof buf ,"missing %s%s%s%s%s" ,avx512f ?"" :"AVX512F " , 422avx512bw ?"" :"AVX512BW " ,avx512vl ?"" :"AVX512VL " , 423avx512_vnni ?"" :"AVX512-VNNI " ,avx512_vbmi ?"" :"AVX512-VBMI" ); 424return buf ; 425} 426 427CpuFeatures cpu_features () { 428CpuFeatures f ; 429#if !defined(__x86_64__ )&& !defined(__i386__ )&& !defined(_M_X64 )&& !defined(_M_IX86 ) 430return f ;// the AVX-512 kernels are x86-only: no AVX-512 on this architecture 431#else 432int reg [4 ]= {0 ,0 ,0 ,0 }; 433#if defined(_MSC_VER ) 434__cpuid (reg ,0 ); 435if (reg [0 ]< 7 )return f ; 436__cpuidex (reg ,7 ,0 ); 437#else 438unsigned r [4 ]= {0 ,0 ,0 ,0 }; 439__cpuid_count (0 ,0 ,r [0 ],r [1 ],r [2 ],r [3 ]); 440if (r [0 ]< 7 )return f ; 441__cpuid_count (7 ,0 ,r [0 ],r [1 ],r [2 ],r [3 ]); 442for (int i = 0 ;i < 4 ;++ i )reg [i ]= (int )r [i ]; 443#endif 444const unsigned ebx = (unsigned )reg [1 ],ecx = (unsigned )reg [2 ]; 445f .avx512f = (ebx >>16 )& 1u ; 446f .avx512bw = (ebx >>30 )& 1u ; 447f .avx512vl = (ebx >>31 )& 1u ; 448f .avx512_vnni = (ecx >>11 )& 1u ; 449f .avx512_vbmi = (ecx >>1 )& 1u ; 450return f ; 451#endif 452} 453 454void cpu_require_expert_support () { 455const CpuFeatures f = cpu_features (); 456if (f .usable ())return ; 457 std::fprintf (stderr , 458"strata: this CPU cannot run the expert kernel: %s.\n" 459" The pack in --pack is the AVX-512 Q2_0 pack (its folder has no native_experts.txt): its CPU kernel needs\n" 460" AVX512-VNNI and AVX512-VBMI (Intel Ice Lake / AMD Zen 4 or newer).\n" 461" An i-quant (IQ3_XXS, IQ3_S, ...), NVFP4 or Unsloth pack has native_experts.txt in its folder and runs on an\n" 462" AVX2 CPU as well: build that pack (setup does it on a CPU without AVX-512) and give its folder as --pack.\n" 463" The scalar fallback exists for tests only and is far too slow to decode with.\n" , 464f .reason ()); 465 std::exit (1 ); 466} 467 468void q2_rows_any (const uint8_t * w ,size_t row_bytes ,int nblocks ,const ActQ * const * a ,int nt ,float * const * out , 469int r0 ,int r1 ) { 470// VNNI-512 without VBMI: expert.cpp's zmm row dot has a substitute for the VBMI-only 2-bit unpack there 471// (q2_rows_512_ok also leaves the STRATA_CPU_YMM=1 choice to the AVX-2 kernel, on this CPU on the rung below). 472if (q2_rows_512_ok ())q2_0_gguf_rows_multi (w ,row_bytes ,nblocks ,a ,nt ,out ,r0 ,r1 ); 473else if (cpu_avx2_ok ())q2_0_gguf_rows_multi_avx2 (w ,row_bytes ,nblocks ,a ,nt ,out ,r0 ,r1 ); 474else q2_0_gguf_rows_multi_avx1 (w ,row_bytes ,nblocks ,a ,nt ,out ,r0 ,r1 );// issue #1699: the third rung 475} 476 477void q2_rows_any_legacy (const uint8_t * w ,size_t row_bytes ,int nblocks ,const ActQ * const * a ,int nt ,float * const * out , 478int r0 ,int r1 ) { 479if (cpu_avx512_ok ())q2_0_gguf_rows_multi (w ,row_bytes ,nblocks ,a ,nt ,out ,r0 ,r1 ); 480else if (cpu_avx2_ok ())q2_0_gguf_rows_multi_avx2_legacy (w ,row_bytes ,nblocks ,a ,nt ,out ,r0 ,r1 ); 481else q2_0_gguf_rows_multi_avx1_legacy (w ,row_bytes ,nblocks ,a ,nt ,out ,r0 ,r1 ); 482} 483 484void act_quant_any (const float * x ,int n ,ActQ & a ) { 485// the q8 quantizer is VBMI-free: a VNNI-but-no-VBMI CPU (Cascade Lake Xeon) keeps its 512-bit form 486if (cpu_vnni_ok ())act_quant_q8_1 (x ,n ,a ); 487else if (cpu_avx2_ok ())act_quant_q8_1_avx2 (x ,n ,a ); 488else act_quant_q8_1_avx1 (x ,n ,a );// issue #1699 489} 490 491#if !defined(STRATA_NATIVE_EXPERTS ) 492// Without ggml-cpu no native pack loads (expert_layout_load refuses), so these are never reached. 493bool native_experts_available ()noexcept {return false; } 494bool native_fmt (int ,int ,int64_t ,int64_t ,NativeFmt & , std::string & err ) {err = "built without native experts" ;return false; } 495void native_quant_act (const NativeFmt & ,const float * ,void * ) { std::abort (); } 496void native_quant_h (const NativeFmt & ,const float * ,void * ) { std::abort (); } 497int native_gu_mt_min (int ) {return 2 ; } 498void native_gu_rows (const NativeFmt & ,const uint8_t * ,const void * const * ,int ,float * const * ,int ,int ) { std::abort (); } 499void native_down_rows (const NativeFmt & ,const uint8_t * ,const void * const * ,int ,float * const * ,int ,int ) { std::abort (); } 500void native_gu_rows_invariant (const NativeFmt & ,const uint8_t * ,const void * const * ,int ,float * const * ,int ,int ) { std::abort (); } 501void native_down_rows_invariant (const NativeFmt & ,const uint8_t * ,const void * const * ,int ,float * const * ,int ,int ) { std::abort (); } 502#endif 503 504bool expert_layout_load (const std::string & pack_dir ,int64_t n_layers ,int64_t n_expert , std::string & err ) { 505ExpertLayout L ; 506L .n_layers = n_layers ; 507L .n_expert = n_expert ; 508 std::ifstream in (pack_dir + "/native_experts.txt" ); 509if (!in ) { 510L .total = (uint64_t )n_layers * (uint64_t )n_expert * (uint64_t )BLOB ; 511hadamard_set ({}); 512g_layout = L ; 513return true; 514 } 515#if !defined(STRATA_NATIVE_EXPERTS ) 516err = "this pack has native (IQ) experts but the engine was built without STRATA_NATIVE_EXPERTS" ; 517return false; 518#else 519L .native = true; 520int64_t expert_embd = H ,expert_ff = FF ; 521L .fmt .resize ((size_t )n_layers ); 522L .offset .assign ((size_t )n_layers , ~0ull ); 523L .bytes .assign ((size_t )n_layers ,0 ); 524L .max_blob = 0 ; 525 std::string line ; 526while (std::getline (in ,line )) { 527if (line .empty ()|| line [0 ]== '#' ) { 528if (!line .empty ()&& line [0 ]== '#' ) { 529// "# strata native experts vN: ...". A version this engine does not know may mean columns it 530// would misread, so it is refused rather than parsed as far as the known columns go. 531static const char tag []= "# strata native experts v" ; 532if (line .compare (0 ,sizeof tag - 1 ,tag )== 0 ) { 533L .version = std::atoi (line .c_str ()+ sizeof tag - 1 ); 534if (L .version > kExpertLayoutVersion ) { 535err = "native_experts.txt is v" + std::to_string (L .version )+ "; this engine reads up to v" + 536 std::to_string (kExpertLayoutVersion )+ " (the pack was written by a newer tools/" 537"iq_pack.py: update the engine, or repack with this one)" ; 538return false; 539 } 540 } 541// v3 packs record their expert count in the header; a pruned model (GSQ-RCO Coder) ships 542// fewer experts than the canonical geometry the caller passes, which is a compile-time 543// default, so the header wins. 544const size_t at = line .find ("(n_expert " ); 545if (at != std::string::npos )L .n_expert = std::atoll (line .c_str ()+ at + 10 ); 546const size_t eb = line .find ("expert_embd " ); 547if (eb != std::string::npos )expert_embd = std::atoll (line .c_str ()+ eb + 12 ); 548const size_t ef = line .find ("expert_ff " ); 549if (ef != std::string::npos )expert_ff = std::atoll (line .c_str ()+ ef + 10 ); 550 } 551continue ; 552 } 553 std::istringstream ss (line ); 554long long l = -1 ,gt = -1 ,dt = -1 ; 555unsigned long long off = 0 ,blob = 0 ,go = 0 ,uo = 0 ,dox = 0 ; 556if (!(ss >>l >>gt >>dt >>off >>blob )|| l < 0 || l >=n_layers ) { 557err = "native_experts.txt: a malformed line: " + line ; 558return false; 559 } 560NativeFmt f ; 561if (!native_fmt ((int )gt , (int )dt ,expert_embd ,expert_ff ,f ,err ))return false; 562if (f .bytes != blob ) { 563err = "native_experts.txt: layer " + std::to_string (l )+ " blob is " + std::to_string (blob )+ 564" B but its formats make " + std::to_string (f .bytes ); 565return false; 566 } 567if (ss >>go >>uo >>dox ) {// v2 lines: the GGUF offsets 568if (L .gguf_off .empty ())L .gguf_off .assign ((size_t ) (3 * n_layers ),0 ); 569L .gguf_off [(size_t ) (3 * l )]= go ; 570L .gguf_off [(size_t ) (3 * l + 1 )]= uo ; 571L .gguf_off [(size_t ) (3 * l + 2 )]= dox ; 572 std::string file ;// v3: the shard that holds this layer (a file name beside --native) 573if (ss >>file ) { 574if (L .gguf_file .empty ())L .gguf_file .assign ((size_t ) (3 * n_layers ), std::string ()); 575// One name covers all three roles. When a shard boundary falls inside a layer the packer writes 576// "gate,up,down" instead (v4), an empty field meaning the --native shard. A GGUF file name has no 577// comma, so the split is unambiguous; an engine older than v4 fails to open such a "file" loudly. 578// The comma form is from #255 (gopinath87607). 579 std::vector < std::string > parts ; 580for (size_t from = 0 ;;) { 581const size_t comma = file .find (',' ,from ); 582parts .push_back (file .substr (from ,comma == std::string::npos ?comma :comma - from )); 583if (comma == std::string::npos )break ; 584from = comma + 1 ; 585 } 586if (parts .size ()== 1 ) { 587const std::string one = parts [0 ];// not parts.assign(3, parts[0]): that aliases the element 588parts .assign (3 ,one );// the assignment is about to overwrite 589 } 590 std::string extra ; 591if (parts .size ()!= 3 || (ss >>extra )) { 592err = "native_experts.txt: layer " + std::to_string (l )+ ": the shard column is one name or " 593"gate,up,down, not: " + line ; 594return false; 595 } 596for (size_t r = 0 ;r < 3 ;++ r )L .gguf_file [(size_t ) (3 * l )+ r ]= parts [r ]; 597 } 598 } 599L .fmt [(size_t )l ]= f ; 600L .offset [(size_t )l ]= off ; 601L .bytes [(size_t )l ]= blob ; 602if (blob > L .max_blob )L .max_blob = blob ; 603 } 604uint64_t at = 0 ; 605for (int64_t l = 0 ;l < n_layers ;++ l ) { 606if (L .offset [(size_t )l ]!= at ) { 607err = "native_experts.txt: layer " + std::to_string (l )+ " is missing or not contiguous" ; 608return false; 609 } 610at += L .bytes [(size_t )l ]* (uint64_t )L .n_expert ; 611 } 612L .total = at ; 613// v5: Agention's Hadamard-folded experts (prism.hadamard, hadamard.hpp). The pack must say which layers are 614// rotated and with what signs; a v5 pack without hadamard.txt (or the reverse) is refused, never run unrotated. 615 { 616 std::ifstream probe (pack_dir + "/hadamard.txt" ); 617const bool have = (bool )probe ; 618if (L .version == 5 && !have ) {// v6 latent A-only packs carry no hadamard.txt (their expert rows are latent, not Hadamard-folded) 619err = "native_experts.txt v" + std::to_string (L .version )+ " declares Hadamard-folded experts, but " + pack_dir + "/hadamard.txt is missing" ; 620return false; 621 } 622if (have && L .version < 5 ) { 623err = pack_dir + "/hadamard.txt exists but native_experts.txt is v" + std::to_string (L .version )+ 624": the pack is inconsistent (repack with tools/iq_pack.py)" ; 625return false; 626 } 627for (int64_t l = 0 ;l < n_layers ;++ l )L .fmt [(size_t )l ].layer = (int )l ; 628HadamardSpec h ; 629if (have && !hadamard_parse (pack_dir + "/hadamard.txt" ,n_layers ,h ,err ))return false; 630hadamard_set (std::move (h )); 631const HadamardSpec & hs = hadamard_spec (); 632for (int64_t l = 0 ;hs .any ()&& l < n_layers ;++ l ) { 633NativeFmt & f = L .fmt [(size_t )l ]; 634f .had_block = hs .block ; 635f .had_gu = hs .gu (l ); 636f .had_d = hs .down (l ); 637if ((f .had_gu && f .n_embd %hs .block )|| (f .had_d && f .n_ff %hs .block )) { 638err = "hadamard.txt: block " + std::to_string (hs .block )+ " does not divide layer " + 639 std::to_string (l )+ "'s expert widths" ; 640return false; 641 } 642// the Q2_0 rows quantize their activations with Strata's own act_quant_any, which does not rotate 643if ((f .had_gu && f .gu_type == 42 )|| (f .had_d && f .d_type == 42 )) { 644err = "hadamard.txt: layer " + std::to_string (l )+ " folds a Q2_0 expert, which this engine cannot rotate" ; 645return false; 646 } 647f .gu_signs = f .had_gu ?hadamard_signs ((int )f .n_embd ) :nullptr ; 648f .d_signs = f .had_d ?hadamard_signs ((int )f .n_ff ) :nullptr ; 649// explicit signs are per width: a width without a vector would silently mean identity signs 650if (!hs .signs .empty ()&& ((f .had_gu && !f .gu_signs )|| (f .had_d && !f .d_signs ))) { 651err = "hadamard.txt: no sign vector for layer " + std::to_string (l )+ "'s expert width" ; 652return false; 653 } 654 } 655 } 656// Q3_K + shared column scales (make_latent_a_q3k.py): scales/l{l}.f32 beside the manifest holds s, the gate/up 657// rows are A*diag(s), and the D stage's xl rows are multiplied by 1/s where they are produced (verify.cpp / 658// prefill.cpp, lat_colscale_rows) so every consumer - the GPU experts' q8_1, act0, the CPU pool's f32 659// doorbell - sees scaled rows; native_quant_act does NOT multiply. All or none: a half-scaled pack is refused 660// rather than run wrong. 661 { 662 std::ifstream probe (pack_dir + "/scales/l0.f32" ); 663if (probe ) { 664static std::vector < float > inv ;// outlives g_layout (its copied NativeFmt points into this) 665inv .clear (); 666inv .reserve ((size_t )n_layers * (size_t )expert_embd ); 667for (int64_t l = 0 ;l < n_layers ;++ l ) { 668const int64_t n = L .fmt [(size_t )l ].n_embd ; 669if (n != expert_embd ) {// the reserve above assumes one width; a mixed pack would realloc 670err = "scales: layer " + std::to_string (l )+ " has n_embd " + std::to_string (n )+ 671", the manifest says " + std::to_string (expert_embd ); 672return false; 673 } 674const std::string p = pack_dir + "/scales/l" + std::to_string (l )+ ".f32" ; 675 std::ifstream f (p , std::ios::binary ); 676if (!f ) {err = "scales: " + p + " is missing (all-or-none)" ;return false; } 677 std::vector < float > s ((size_t )n ); 678f .read ((char * )s .data (), (std::streamsize ) (s .size ()* sizeof (float ))); 679if (f .gcount ()!= (std::streamsize ) (s .size ()* sizeof (float ))) { 680err = "scales: " + p + " is short" ; 681return false; 682 } 683L .fmt [(size_t )l ].gu_colscale = inv .data ()+ inv .size (); 684for (float v :s )inv .push_back (v > 0.0f ?1.0f /v :1.0f ); 685 } 686 } 687 } 688g_layout = L ; 689return true; 690#endif 691} 692 693}// namespace strata::kernels::cpu 694 695namespace strata::kernels {// lat_gu_colscale_host: the GPU-side colscale multiplier's data source (iq_kernels.cu) 696 697const float * lat_gu_colscale_host (int layer ,int * width ) { 698const cpu::ExpertLayout & L = cpu::expert_layout (); 699if (width != nullptr )* width = 0 ; 700if (layer < 0 || (size_t )layer >=L .fmt .size ())return nullptr ; 701const float * s = L .fmt [(size_t )layer ].gu_colscale ; 702if (s != nullptr && width != nullptr )* width = (int )L .fmt [(size_t )layer ].n_embd ; 703return s ; 704} 705 706}// namespace strata::kernels