okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
fd19246dc
main
1// Native (GGUF-form) experts: ggml activation quantizers and format-specific CPU row kernels. 2// Default keeps the existing dispatch. WindowInvariant preserves that dispatch's single-token arithmetic 3// while retaining shared weight decoding where the grouped kernel gives the same result. 4#include "strata/kernels/cpu/native_expert.hpp" 5#include "strata/kernels/cpu/expert.hpp" 6#include "strata/kernels/cpu/iq_avx512.hpp" 7#include "strata/kernels/cpu/iq_avx2.hpp" 8#include "strata/kernels/cpu/iq_avx1.hpp" 9#include "strata/kernels/cpu/kq_avx2.hpp" 10#include "strata/kernels/cpu/nvfp4_avx2.hpp" 11#include "strata/kernels/cpu/nvfp4_avx512.hpp" 12#include "strata/kernels/cpu/expert_layout.hpp" 13#include "strata/kernels/cpu/hadamard.hpp" 14#include "strata/kernels/cpu/tq_cpu.hpp" 15 16// The weight types' dot products, the activation quantizers and the row geometry: the Strata quant layer (STRATA_OWN_CPU_QUANT, the default build, no 17// ggml-cpu in the pool) or, as before, ggml-cpu's type traits (pool_quant.hpp). 18#include "strata/kernels/cpu/pool_quant.hpp" 19 20#include <algorithm> 21#include <cmath> 22#include <cstdio> 23#include <cstdlib> 24#include <cstring> 25#include <map> 26#include <mutex> 27#include <string> 28#include <vector> 29 30namespace strata::kernels::cpu { 31namespace { 32 33using namespace pq ;// Tr, traits(), row_size_of(), blck_size_of(), flavour_*(), layer_init(), kTQ8_0 ... (pool_quant.hpp) 34 35// Do the trellis rows take Strata's own kernels (tq_cpu.hpp)? Always, unless STRATA_NO_TQ_KERNELS=1 asks for ggml-cpu's 36// vec_dot AND this ggml has the type (the agentionai fork; an upstream ggml does not, and must never be asked about it). 37bool tq_rows_own (int type ) { 38if (!tq_supported (type ))return false; 39if (tq_kernels_enabled ())return true; 40#if defined(STRATA_OWN_CPU_QUANT ) 41return !traits (type ).has_dot ();// the layer has no trellis dot: always the trellis kernels' own rows 42#else 43return type >= (int )GGML_TYPE_COUNT || !traits (type ).has_dot (); 44#endif 45} 46 47// Window-invariant arithmetic (NativeArithmetic::WindowInvariant): the reference rows below reproduce ggml-cpu's one-token dot bit for bit 48// only if that ggml was built to sum the same way (contraction of the scalar tail into an FMA, for one, depends on the build). So each 49// reference is checked once against the linked ggml on synthetic blocks, odd block counts included, and a mismatch sends the invariant 50// path to ggml's own one-token dot per token (slower, never different). 51bool iq4nl_reference_ok () { 52static const bool ok = [] { 53const Tr tr = traits (kTIq4nl ); 54const Tr t8 = traits (kTQ8_0 ); 55if (!tr .has_dot ()|| !t8 .has_from_float ())return false; 56uint32_t seed = 4242u ; 57auto rnd = [& seed ] {seed = seed * 1664525u + 1013904223u ;return seed >>8 ; }; 58for (const int n : {64 ,96 ,160 ,640 }) {// even and odd numbers of blocks: the odd one ends in the scalar tail 59constexpr int rows = 16 ; 60const size_t rb = row_size_of (kTIq4nl , (int64_t )n ),nb = (size_t )n /32 ,bb = rb /nb ; 61 std::vector < uint8_t > w ((size_t )rows * rb ),act (row_size_of (kTQ8_0 , (int64_t )n )); 62for (auto & b :w )b = (uint8_t )rnd (); 63for (int r = 0 ;r < rows ;++ r ) 64for (size_t k = 0 ;k < nb ;++ k ) {uint8_t * d = & w [(size_t )r * rb + k * bb ];d [0 ]= 0x66 ;d [1 ]= 0x2e ; } 65 std::vector < float > x ((size_t )n ); 66for (auto & v :x )v = ((float ) (rnd () %2001 )- 1000.f )* 0.001f ; 67t8 .from_float (x .data (),act .data (),n ); 68 std::vector < float > got ((size_t )rows ),want ((size_t )rows ); 69const void * hp [1 ]= {act .data ()}; 70float * op [1 ]= {got .data ()}; 71iq4nl_reference_rows (w .data (),rb ,n ,hp ,1 ,op ,0 ,rows ); 72for (int r = 0 ;r < rows ;++ r )tr .dot (n ,& want [(size_t )r ],w .data ()+ (size_t )r * rb ,act .data ()); 73if (std::memcmp (got .data (),want .data (),got .size ()* sizeof (float ))!= 0 ) { 74 std::fprintf (stderr ,"strata: the window-invariant IQ4_NL rows differ from this ggml-cpu's dot (n=%d): its dot runs per token instead\n" ,n ); 75return false; 76 } 77 } 78return true; 79 }(); 80return ok ; 81} 82 83// The same check for the AVX-512 accumulation of IQ3_XXS / IQ3_S / IQ2_S gate/up rows (types 18, 21, 22), SwiGLU included. 84bool iq512_reference_ok (int type ) { 85static std::mutex mu ; 86static std::map < int ,bool > memo ; 87 std::lock_guard < std::mutex > lk (mu ); 88if (const auto it = memo .find (type );it != memo .end ())return it -> second ; 89bool ok = false; 90const Tr tr = traits (type ); 91const Tr t8 = traits (kTQ8_K ); 92if (tr .has_dot ()&& t8 .has_from_float ()) { 93constexpr int n = 2560 ,rows = 24 ; 94const size_t rb = row_size_of (type ,n ),nb = (size_t )n /256 ,bb = rb /nb ; 95 std::vector < uint8_t > w ((size_t )2 * rows * rb ),act (row_size_of (kTQ8_K ,n )); 96uint32_t seed = 777u ; 97auto rnd = [& seed ] {seed = seed * 1664525u + 1013904223u ;return seed >>8 ; }; 98for (auto & b :w )b = (uint8_t )rnd (); 99for (int r = 0 ;r < 2 * rows ;++ r ) 100for (size_t k = 0 ;k < nb ;++ k ) {uint8_t * d = & w [(size_t )r * rb + k * bb ];d [0 ]= 0x66 ;d [1 ]= 0x2e ; } 101 std::vector < float > x (n ); 102for (auto & v :x )v = ((float ) (rnd () %2001 )- 1000.f )* 0.001f ; 103t8 .from_float (x .data (),act .data (),n ); 104 std::vector < float > got ((size_t )rows ),want ((size_t )rows ); 105const void * ap [1 ]= {act .data ()}; 106float * fp [1 ]= {got .data ()}; 107iq512_reference_gu_rows (type ,w .data (),rb , (size_t )rows * rb ,n ,ap ,1 ,fp ,0 ,rows ); 108for (int r = 0 ;r < rows ;++ r ) { 109float g = 0.f ,u = 0.f ; 110tr .dot (n ,& g ,w .data ()+ (size_t )r * rb ,act .data ()); 111tr .dot (n ,& u ,w .data ()+ (size_t )rows * rb + (size_t )r * rb ,act .data ()); 112want [(size_t )r ]= (g / (1.f + std::exp (- g )))* u ; 113 } 114ok = std::memcmp (got .data (),want .data (),got .size ()* sizeof (float ))== 0 ; 115if (!ok ) std::fprintf (stderr ,"strata: the window-invariant 512-bit rows of type %d differ from this ggml-cpu's dot: its dot runs per token instead\n" ,type ); 116 } 117memo [type ]= ok ; 118return ok ; 119} 120 121// 0.2.5: the 512-bit exact rows (iq512x_rows) take one token of an IQ3_XXS / IQ3_S expert only if this build's ggml-cpu dot gives 122// the same bits on synthetic blocks of that type (a ggml built for AVX-512 may carry another summation order): checked once a type. 123bool x512_one_ok (int type ) { 124static std::mutex mu ; 125static std::map < int ,bool > memo ; 126 std::lock_guard < std::mutex > lk (mu ); 127if (const auto it = memo .find (type );it != memo .end ())return it -> second ; 128bool ok = false; 129const Tr tr = traits (type ); 130const Tr t8 = traits (kTQ8_K ); 131if (tr .has_dot ()&& t8 .has_from_float ()&& iq512x_supported (type )) { 132constexpr int n = 2560 ,rows = 24 ; 133const size_t rb = row_size_of (type ,n ),nb = (size_t )n /256 ,bb = rb /nb ; 134 std::vector < uint8_t > w ((size_t )rows * rb ),act (row_size_of (kTQ8_K ,n )); 135uint32_t seed = 12345u ; 136auto rnd = [& seed ] {seed = seed * 1664525u + 1013904223u ;return seed >>8 ; }; 137for (auto & b :w )b = (uint8_t )rnd (); 138for (int r = 0 ;r < rows ;++ r ) 139for (size_t k = 0 ;k < nb ;++ k ) {uint8_t * d = & w [(size_t )r * rb + k * bb ];d [0 ]= 0x66 ;d [1 ]= 0x2e ; }// d = ~0.1 (f16) 140 std::vector < float > x (n ); 141for (auto & v :x )v = ((float ) (rnd () %2001 )- 1000.f )* 0.001f ; 142t8 .from_float (x .data (),act .data (),n ); 143 std::vector < float > got ((size_t )rows ),want ((size_t )rows ); 144const void * ap [1 ]= {act .data ()}; 145float * op [1 ]= {got .data ()}; 146iq512x_rows (type ,w .data (),rb ,n ,ap ,1 ,op ,0 ,rows ); 147for (int r = 0 ;r < rows ;++ r )tr .dot (n ,& want [(size_t )r ],w .data ()+ (size_t )r * rb ,act .data ()); 148ok = std::memcmp (got .data (),want .data (),got .size ()* sizeof (float ))== 0 ; 149if (!ok ) std::fprintf (stderr ,"strata: the 512-bit exact one-token rows of type %d differ from this ggml-cpu's dot: not used\n" ,type ); 150 } 151memo [type ]= ok ; 152return ok ; 153} 154 155// 0.2.12: the 512-bit Q5_0 down rows (q50_512_down_rows) take the type only if, on synthetic blocks (even and odd block counts, three tokens), they 156// give the bits of ggml-cpu's dot on this build. 157// The same check for the fused gate/up rows (iq512_fused_gu_rows) of an i-quant type: ggml-cpu's dot, SwiGLU included, bit for bit. 158bool iq512f_ok (int type ) { 159static std::mutex mu ; 160static std::map < int ,bool > memo ; 161 std::lock_guard < std::mutex > lk (mu ); 162if (const auto it = memo .find (type );it != memo .end ())return it -> second ; 163bool ok = false; 164const Tr tr = traits (type ); 165const Tr t8 = traits (kTQ8_K ); 166if (tr .has_dot ()&& t8 .has_from_float ()&& iq512f_supported (type )) { 167constexpr int n = 2560 ,rows = 24 ,nt = 3 ; 168const size_t rb = row_size_of (type ,n ),nb = (size_t )n /256 ,bb = rb /nb ; 169 std::vector < uint8_t > w ((size_t )2 * rows * rb ); 170uint32_t seed = 4711u + (uint32_t )type ; 171auto rnd = [& seed ] {seed = seed * 1664525u + 1013904223u ;return seed >>8 ; }; 172for (auto & b :w )b = (uint8_t )rnd (); 173for (int r = 0 ;r < 2 * rows ;++ r ) 174for (size_t k = 0 ;k < nb ;++ k ) {uint8_t * d = & w [(size_t )r * rb + k * bb ];d [0 ]= 0x66 ;d [1 ]= 0x2e ; } 175 std::vector < std::vector < uint8_t >> act (nt , std::vector < uint8_t > (row_size_of (kTQ8_K ,n ))); 176const void * ap [nt ]; 177for (int t = 0 ;t < nt ;++ t ) { 178 std::vector < float > x (n ); 179for (auto & v :x )v = ((float ) (rnd () %2001 )- 1000.f )* 0.001f ; 180t8 .from_float (x .data (),act [(size_t )t ].data (),n ); 181ap [t ]= act [(size_t )t ].data (); 182 } 183 std::vector < float > got ((size_t )nt * rows ),want ((size_t )nt * rows ); 184float * fp [nt ]; 185for (int t = 0 ;t < nt ;++ t )fp [t ]= & got [(size_t )t * rows ]; 186iq512_fused_gu_rows (type ,w .data (),rb , (size_t )rows * rb ,n ,ap ,nt ,fp ,0 ,rows ); 187for (int t = 0 ;t < nt ;++ t ) 188for (int r = 0 ;r < rows ;++ r ) { 189float g = 0.f ,u = 0.f ; 190tr .dot (n ,& g ,w .data ()+ (size_t )r * rb ,ap [t ]); 191tr .dot (n ,& u ,w .data ()+ (size_t )rows * rb + (size_t )r * rb ,ap [t ]); 192want [(size_t )t * rows + r ]= (g / (1.f + std::exp (- g )))* u ; 193 } 194ok = std::memcmp (got .data (),want .data (),got .size ()* sizeof (float ))== 0 ; 195if (!ok ) std::fprintf (stderr ,"strata: the fused 512-bit gate/up rows of type %d differ from this ggml-cpu's dot: not used\n" ,type ); 196 } 197memo [type ]= ok ; 198return ok ; 199} 200 201bool q50_512_ok () { 202static const bool ok = [] { 203const Tr tr = traits (kTQ5_0 ); 204const Tr t8 = traits (kTQ8_0 ); 205if (!cpu_vnni_ok ()|| !tr .has_dot ()|| !t8 .has_from_float ())return false; 206uint32_t seed = 31337u ; 207auto rnd = [& seed ] {seed = seed * 1664525u + 1013904223u ;return seed >>8 ; }; 208for (const int n : {96 ,640 }) { 209constexpr int rows = 16 ,nt = 3 ; 210const size_t rb = row_size_of (kTQ5_0 , (int64_t )n ),nb = (size_t )n /32 ,bb = rb /nb ; 211 std::vector < uint8_t > w ((size_t )rows * rb ); 212for (auto & b :w )b = (uint8_t )rnd (); 213for (int r = 0 ;r < rows ;++ r ) 214for (size_t k = 0 ;k < nb ;++ k ) {uint8_t * d = & w [(size_t )r * rb + k * bb ];d [0 ]= 0x66 ;d [1 ]= 0x2e ; } 215 std::vector < std::vector < uint8_t >> act (nt , std::vector < uint8_t > (row_size_of (kTQ8_0 ,n ))); 216const void * hp [nt ]; 217for (int t = 0 ;t < nt ;++ t ) { 218 std::vector < float > x ((size_t )n ); 219for (auto & v :x )v = ((float ) (rnd () %2001 )- 1000.f )* 0.001f ; 220t8 .from_float (x .data (),act [(size_t )t ].data (),n ); 221hp [t ]= act [(size_t )t ].data (); 222 } 223 std::vector < float > got ((size_t )nt * rows ),want ((size_t )nt * rows ); 224float * gp [nt ]; 225for (int t = 0 ;t < nt ;++ t )gp [t ]= & got [(size_t )t * rows ]; 226q50_512_down_rows (w .data (),rb ,n ,hp ,nt ,gp ,0 ,rows ); 227for (int r = 0 ;r < rows ;++ r ) 228for (int t = 0 ;t < nt ;++ t )tr .dot (n ,& want [(size_t )t * rows + r ],w .data ()+ (size_t )r * rb ,hp [t ]); 229if (std::memcmp (got .data (),want .data (),got .size ()* sizeof (float ))!= 0 ) { 230 std::fprintf (stderr ,"strata: the 512-bit Q5_0 down rows differ from this ggml-cpu's dot: not used\n" ); 231return false; 232 } 233 } 234return true; 235 }(); 236return ok ; 237} 238 239void init_once () { 240static std::once_flag once ; 241 std::call_once (once , [] {layer_init (); }); 242} 243 244}// namespace 245 246bool native_experts_available ()noexcept {return true; } 247 248// The "is this format handled by that kernel" questions, answered here and not next to the kernels: iq_avx512.cpp is 249// compiled for AVX-512 and iq_avx2.cpp / kq_avx2.cpp for AVX2, and native_gu_rows asks these on every CPU before it 250// has checked what the CPU can run. A function in a wide-ISA file may use that ISA anywhere in its body, so an 251// answer that is only a comparison must not come from one (#795). 252bool iq512_supported (int type )noexcept { 253return type == 16 || type == 17 || type == 18 || type == 21 || type == 22 ; 254} 255 256bool iq256_supported (int type )noexcept { 257return type == 16 || type == 17 || type == 18 || type == 21 || type == 22 || type == 23 ; 258} 259 260bool kq256_supported (int type )noexcept {return type == 12 || type == 7 || type == 8 ; } 261 262bool native_fmt (int gu_type ,int d_type ,int64_t n_embd ,int64_t n_ff ,NativeFmt & f , std::string & err ) { 263init_once (); 264// The trellis types (Gyro, ggml types 144-146) are not in a ggml built from upstream (and an upstream ggml indexes its 265// type table by the id, so asking it about 144 reads past the table): their geometry and activation format (Q8_0) 266// are known here, and their rows run on tq_cpu.hpp. 267const bool tq_g = tq_supported (gu_type ),tq_d = tq_supported (d_type ); 268const Tr tg = tq_g ?Tr {} :traits (gu_type ); 269const Tr td = tq_d ?Tr {} :traits (d_type ); 270if ((!tq_g && !tg .has_dot ())|| (!tq_d && !td .has_dot ())) { 271err = "native experts: " STRATA_POOL_LIB " has no dot product for type " + std::to_string (!tq_g && !tg .has_dot () ?gu_type :d_type ); 272return false; 273 } 274const int vdt_g = tq_g ?kTQ8_0 :tg .vec_dot_type ; 275const int vdt_d = tq_d ?kTQ8_0 :td .vec_dot_type ; 276const Tr ag = traits (vdt_g ); 277const Tr ad = traits (vdt_d ); 278if (!ag .has_from_float ()|| !ad .has_from_float ()) { 279err = "native experts: " STRATA_POOL_LIB " cannot quantize an activation for this layer" ; 280return false; 281 } 282if ((tq_g ?n_embd %kTqBlock :n_embd %blck_size_of (gu_type ))|| 283 (tq_d ?n_ff %kTqBlock :n_ff %blck_size_of (d_type ))|| 284n_embd %blck_size_of (vdt_g )|| n_ff %blck_size_of (vdt_d )) { 285err = "native experts: expert geometry is not whole blocks" ; 286return false; 287 } 288f .gu_type = gu_type ; 289f .d_type = d_type ; 290f .gu_act = (int )vdt_g ; 291f .d_act = (int )vdt_d ; 292f .n_embd = n_embd ; 293f .n_ff = n_ff ; 294f .gu_row = tq_g ? (size_t ) (n_embd /kTqBlock )* tq_block_bytes (gu_type ) :row_size_of (gu_type ,n_embd ); 295f .d_row = tq_d ? (size_t ) (n_ff /kTqBlock )* tq_block_bytes (d_type ) :row_size_of (d_type ,n_ff ); 296f .up_off = f .gu_row * (size_t )n_ff ; 297f .down_off = 2 * f .up_off ; 298f .bytes = f .down_off + f .d_row * (size_t )n_embd ; 299// NVFP4 in any projection: one scale tail per blob; a mixed expert (e.g. NVFP4 gate/up + Q8_0 down) has 1.0 for 300// the other projection's scale (tools/iq_pack.py), so every path below multiplies by the tail unconditionally 301if (gu_type == kNvfp4Type || d_type == kNvfp4Type ) { 302f .tail_off = f .bytes ; 303f .bytes += kNvfp4Tail ; 304 } 305f .act_bytes = row_size_of (vdt_g ,n_embd ); 306f .h_bytes = row_size_of (vdt_d ,n_ff ); 307if (f .act_bytes > kNativeActBytes || f .h_bytes > kNativeHBytes ) { 308err = "native experts: activation larger than the pool's buffers" ; 309return false; 310 } 311return true; 312} 313 314namespace { 315// Q8_K (the activations of every i-quant row): ggml-cpu's x86 quantizer is the scalar reference, ~3 us per token 316// and layer on the host before the pool can start; q8k_quant_avx2 writes the same bytes. cpu_avx2_ok() too: 317// iq_avx2.cpp is compiled for AVX2 (an AVX-only CPU, or STRATA_FORCE_ISA=avx, keeps ggml's). STRATA_NO_Q8K_AVX2=1: 318// ggml's on any CPU. 319bool q8k_avx2 (int type ) { 320static const bool on = cpu_avx2_ok ()&& std::getenv ("STRATA_NO_Q8K_AVX2" )== nullptr ; 321return on && type == (int )kTQ8_K ; 322} 323// the older CPUs' copy (iq_avx1.cpp): byte-identical like the AVX-2 one, and only where AVX2 is absent (same 324// policy as STRATA_NO_IQ128 above; STRATA_NO_Q8K_AVX1=1 keeps ggml's scalar reference on those CPUs). 325bool q8k_avx1 (int type ) { 326static const bool on = !cpu_avx2_ok ()&& cpu_avx1_ok ()&& std::getenv ("STRATA_NO_Q8K_AVX1" )== nullptr ; 327return on && type == (int )kTQ8_K ; 328} 329}// namespace 330 331// STRATA_ACT_DUMP=<prefix> (research, off by default): the second moments of the vectors that enter the CPU experts, in the 332// basis the experts use (after the Hadamard rotation of a folded layer): per layer the gate/up input (the token's hidden 333// state, once per token) and the down input (the SwiGLU output, once per routed expert). STRATA_ACT_DUMP_LAYERS=10,20 picks 334// the layers (default none), STRATA_ACT_DUMP_COV=1 adds the full covariance. Files <prefix>_L<l>_<gu|dn>.bin: int64 n, 335// int64 count, double sum x^2 [n], (double sum x x^T [n*n]); rewritten every 2000 records and at exit. 336namespace { 337struct ActAcc { 338int64_t n = 0 ,count = 0 ; 339 std::vector < double > sq ,cov ; 340}; 341struct ActDump { 342bool on = false,cov = false; 343 std::string prefix ; 344 std::vector < int > layers ; 345 std::map < std::pair < int ,int > ,ActAcc > acc ; 346 std::mutex mu ; 347int64_t records = 0 ; 348ActDump () { 349const char * p = std::getenv ("STRATA_ACT_DUMP" ); 350if (p == nullptr || * p == 0 )return ; 351prefix = p ; 352cov = std::getenv ("STRATA_ACT_DUMP_COV" )!= nullptr && std::atoi (std::getenv ("STRATA_ACT_DUMP_COV" ))!= 0 ; 353if (const char * l = std::getenv ("STRATA_ACT_DUMP_LAYERS" )) { 354 std::string s (l ); 355size_t at = 0 ; 356while (at < s .size ()) { 357layers .push_back (std::atoi (s .c_str ()+ at )); 358const size_t c = s .find (',' ,at ); 359if (c == std::string::npos )break ; 360at = c + 1 ; 361 } 362 } 363on = !layers .empty (); 364if (on ) std::atexit ([] {act_dump_flush (); }); 365 } 366static void act_dump_flush (); 367}; 368ActDump & act_dump () { 369static ActDump * d = new ActDump ;// never destroyed: the atexit flush must still find it 370return * d ; 371} 372void ActDump ::act_dump_flush () { 373ActDump & d = act_dump (); 374 std::lock_guard < std::mutex > lk (d .mu ); 375for (auto & kv :d .acc ) { 376const std::string path = d .prefix + "_L" + std::to_string (kv .first .first )+ (kv .first .second ?"_dn.bin" :"_gu.bin" ); 377FILE * f = std::fopen ((path + ".tmp" ).c_str (),"wb" ); 378if (!f )continue ; 379 std::fwrite (& kv .second .n ,8 ,1 ,f ); 380 std::fwrite (& kv .second .count ,8 ,1 ,f ); 381 std::fwrite (kv .second .sq .data (),8 ,kv .second .sq .size (),f ); 382if (!kv .second .cov .empty ()) std::fwrite (kv .second .cov .data (),8 ,kv .second .cov .size (),f ); 383 std::fclose (f ); 384 std::rename ((path + ".tmp" ).c_str (),path .c_str ()); 385 } 386} 387void act_dump_record (int layer ,int kind ,const float * v ,int64_t n ) { 388ActDump & d = act_dump (); 389if (std::find (d .layers .begin (),d .layers .end (),layer )== d .layers .end ())return ; 390 std::lock_guard < std::mutex > lk (d .mu ); 391ActAcc & a = d .acc [{layer ,kind }]; 392if (a .n == 0 ) { 393a .n = n ; 394a .sq .assign ((size_t )n ,0.0 ); 395if (d .cov )a .cov .assign ((size_t )n * (size_t )n ,0.0 ); 396 } 397for (int64_t i = 0 ;i < n ;++ i )a .sq [(size_t )i ]+= (double )v [i ]* v [i ]; 398if (d .cov ) 399for (int64_t i = 0 ;i < n ;++ i ) { 400double * row = a .cov .data ()+ (size_t )i * (size_t )n ; 401const double vi = v [i ]; 402for (int64_t j = 0 ;j < n ;++ j )row [j ]+= vi * v [j ]; 403 } 404++ a .count ; 405if (++ d .records % (d .cov ?400000 :2000 )== 0 ) { 406d .mu .unlock (); 407ActDump ::act_dump_flush (); 408d .mu .lock (); 409 } 410} 411}// namespace 412 413void native_quant_act (const NativeFmt & f ,const float * x ,void * dst ) { 414if (f .had_gu ) {// Hadamard-folded gate/up: quantize H (s * x), never x itself (hadamard.hpp). 415 thread_local std::vector < float > tmp ;// Column-scaled packs need no multiply here: the D stage's rows 416// arrive already scaled (lat_colscale_rows in verify.cpp / prefill.cpp multiplies x by 1/s at the source). 417tmp .assign (x ,x + f .n_embd ); 418hadamard_rotate (tmp .data (), (int )f .n_embd ,f .had_block ,f .gu_signs ); 419x = tmp .data (); 420 } 421if (act_dump ().on )act_dump_record (f .layer ,0 ,x ,f .n_embd ); 422if (q8k_avx2 (f .gu_act )) {q8k_quant_avx2 (x ,dst ,f .n_embd );return ; } 423if (q8k_avx1 (f .gu_act )) {q8k_quant_avx1 (x ,dst ,f .n_embd );return ; } 424traits (f .gu_act ).from_float (x ,dst ,f .n_embd ); 425} 426 427void native_quant_h (const NativeFmt & f ,const float * h ,void * dst ) { 428if (f .had_d ) { 429 thread_local std::vector < float > tmp ; 430tmp .assign (h ,h + f .n_ff ); 431hadamard_rotate (tmp .data (), (int )f .n_ff ,f .had_block ,f .d_signs ); 432h = tmp .data (); 433 } 434if (act_dump ().on )act_dump_record (f .layer ,1 ,h ,f .n_ff ); 435if (q8k_avx2 (f .d_act )) {q8k_quant_avx2 (h ,dst ,f .n_ff );return ; } 436if (q8k_avx1 (f .d_act )) {q8k_quant_avx1 (h ,dst ,f .n_ff );return ; } 437traits (f .d_act ).from_float (h ,dst ,f .n_ff ); 438} 439 440int native_gu_mt_min (int gu_type ) { 441// #152: from how many tokens the multi-token kernels run (ggml's vec_dot below that). The default 2 is the 442// measured-fastest rule, but a token's expert rows then round differently alone than in a group, so greedy output 443// can depend on how many drafts a verify window held. STRATA_IQ_MT_MIN=1 (opt-in, 0.1.30) uses the multi-token 444// kernels for every group: output independent of the drafting, at a measured -1..-3% decode on IQ3_S (AVX-512). 445static const char * env = std::getenv ("STRATA_IQ_MT_MIN" ); 446static const int mt_min = env ? std::atoi (env ) :2 ; 447// IQ3_S where the AVX-2 kernel gathers its grid (cpu_gather_fast, unless STRATA_IQ256_GATHER=0) on a CPU without 448// AVX-512: there that kernel beats ggml's dot for ONE token too (14900KF P-core, an expert's gate/up rows: 0.34 -> 449// 0.19 ms; its E-cores, which keep the scalar decode: 0.71 -> 0.68), and in decode most CPU experts serve one token 450// of the window. So IQ3_S takes it for every group, and those rows no longer depend on the drafting. A machine- 451// wide rule, not a per-core one: every core rounds an expert the same. Every probe here is in expert_layout.cpp 452// and cpu_avx2_ok() comes first, so no AVX2 code runs before the check. STRATA_IQ_MT_MIN set keeps its rule. 453// Opt-in (STRATA_IQ3S_MT1=1): it changes a lone token's IQ3_S rounding on those CPUs, so the default stays 0.1.39's. 454static const bool iq3s_one = env == nullptr && std::getenv ("STRATA_IQ3S_MT1" )!= nullptr && cpu_avx2_ok ()&& std::getenv ("STRATA_NO_IQ256" )== nullptr && 455 !cpu_avx512_ok ()&& iq256_gather_setting ()!= 0 && cpu_gather_fast (); 456return iq3s_one && gu_type == 21 ?1 :mt_min ; 457} 458 459template < NativeArithmetic Arithmetic > 460void native_gu_rows_impl (const NativeFmt & f ,const uint8_t * blob ,const void * const * act ,int nt ,float * const * ff , 461int r0 ,int r1 ) { 462// the multi-token kernels decode the weights once for all tokens: 2.0-2.4x ggml-cpu at three tokens, no faster 463// at one (all are bound by the codebook lookups, ~5 GB/s per core), measured by native_expert_parity. AVX-512 464// first, then the AVX-2 one (Zen 2/3, Intel 12th-14th gen). STRATA_NO_IQ512 drops an AVX-512 CPU to the 465// AVX-2 kernel, STRATA_NO_IQ256 drops the AVX-2 kernel; ggml-cpu's single-token vec_dot is reached only with 466// both set (and on a CPU without AVX-512, STRATA_NO_IQ512 changes nothing). 467// 0.2.12 (PR 5120): AVX-512 F/DQ/BW/VL + VNNI is what iq_avx512.cpp runs on (its object holds no VBMI instruction); VBMI is not required. 468static const bool avx512 = cpu_vnni_ok ()&& std::getenv ("STRATA_NO_IQ512" )== nullptr ; 469// STRATA_NO_IQ256 drops the AVX-2 kernel. cpu_avx2_ok() is ALSO required: iq_avx2.cpp and kq_avx2.cpp 470// are compiled /arch:AVX2 and use AVX2 and FMA3, so calling them on a CPU without either is an 471// illegal instruction, not a slow path. The gate sat next to `avx512` above, which does test 472// cpu_avx512_ok(), and that asymmetry is what let a pre-Haswell CPU reach iq256_gu_rows. 473// Without it this falls through to ggml-cpu's vec_dot below, which is compiled for whatever 474// baseline the build selected (AVX1 here) and covers the same types - IQ2_XXS and IQ2_S among 475// them. That path loops over tokens itself, so it is correct for any `nt`, not just one. 476static const bool avx2 = cpu_avx2_ok ()&& std::getenv ("STRATA_NO_IQ256" )== nullptr ; 477// The older-CPU tier (STRATA_ISA_FLOOR=avx builds): without it every expert on such a CPU runs on ggml-cpu's 478// scalar _generic dots. Only where AVX2 is absent, so a modern CPU's rounding never moves (and 479// STRATA_NO_IQ256=1 there still means ggml, as before). iq_avx1.cpp is compiled for AVX and gated on 480// cpu_avx1_ok(). Measured on an E5-2470 v2 (iq_avx1_parity --bench): 1.2-3.1x ggml's dot from two tokens on, 481// at one token ggml's auto-vectorized generic loop matches the 128-bit kernel - so the same #152 mt_min rule 482// as the AVX-2 kernels, and STRATA_IQ_MT_MIN=1 takes the kernel for one token too (its rows are then 483// independent of the drafting: iq_avx1_parity checks every width bit for bit). 484static const bool avx1 = !cpu_avx2_ok ()&& cpu_avx1_ok ()&& std::getenv ("STRATA_NO_IQ128" )== nullptr ; 485// The exact rows are ggml-cpu's AVX2 single-token dot bit for bit, so they need ggml-cpu itself built for AVX2 and 486// not AVX-512 (ggml_cpu_has_*): a STRATA_ISA_FLOOR build runs ggml's AVX1/SSE dots, a native build on an AVX-512 CPU 487// run with STRATA_FORCE_AVX2=1 runs its AVX-512 one, and both round differently (#2215's guard). 488static const bool exact_one = [] { 489const char * v = std::getenv ("STRATA_CPU_IQ_ALL_EXACT1" ); 490return v && v [0 ]== '1' && v [1 ]== 0 && flavour_avx2 ()&& !flavour_avx512 (); 491 }(); 492// only where ggml's dot runs today (below the #152 multi-token rule): with STRATA_IQ_MT_MIN=1 a lone token keeps the 493// multi-token kernel, so a token's rows still do not depend on how many tokens share its expert 494if (exact_one && nt == 1 && nt < native_gu_mt_min (f .gu_type )&& avx2 && !cpu_avx512_ok ()&& 495 (f .gu_type == 16 || f .gu_type == 17 || f .gu_type == 18 || f .gu_type == 21 || f .gu_type == 22 )) { 496static std::once_flag announced ; 497 std::call_once (announced , [] { std::fprintf (stderr ,"strata ab: GATE=STRATA_CPU_IQ_ALL_EXACT1 active=1\n" ); }); 498iq256_gu_rows_exact_one (f .gu_type ,blob ,f .gu_row ,f .up_off , (int )f .n_embd ,act [0 ],ff [0 ],r0 ,r1 ); 499return ; 500 } 501// 0.2.5 (AVX-512 CPUs where a gather is cheap: Intel Sapphire Rapids and later): ONE token of an IQ3_XXS / IQ3_S expert on the 502// 512-bit exact rows, ggml-cpu's single-token AVX2 dot bit for bit with the grid words gathered: 1.3x ggml's dot on a Xeon 8468 503// (x512_bench), the same output. Where the multi-token rule (#152) already sends one token to the multi-token kernel 504// (STRATA_IQ_MT_MIN=1) that stays; STRATA_NO_IQ512X1=1 turns this off; the first use checks the bits against this ggml. 505static const bool x512_one = avx512 && iq512_gather_on ()&& std::getenv ("STRATA_NO_IQ512X1" )== nullptr ; 506if (x512_one && nt == 1 && nt < native_gu_mt_min (f .gu_type )&& f .tail_off == 0 && (f .gu_type == 18 || f .gu_type == 21 )&& 507x512_one_ok (f .gu_type )) { 508const int n1 = (int )f .n_embd ; 509float g [64 ],u [64 ]; 510float * gp [1 ]= {g }; 511float * up [1 ]= {u }; 512for (int c0 = r0 ;c0 < r1 ;c0 += 64 ) { 513const int c1 = std::min (r1 ,c0 + 64 ); 514iq512x_rows (f .gu_type ,blob + (size_t )c0 * f .gu_row ,f .gu_row ,n1 ,act ,1 ,gp ,0 ,c1 - c0 ); 515iq512x_rows (f .gu_type ,blob + f .up_off + (size_t )c0 * f .gu_row ,f .gu_row ,n1 ,act ,1 ,up ,0 ,c1 - c0 ); 516for (int r = c0 ;r < c1 ;++ r )ff [0 ][r ]= (g [r - c0 ] / (1.f + std::exp (- g [r - c0 ])))* u [r - c0 ]; 517 } 518return ; 519 } 520// 0.2.12: IQ4_XS on an AVX-512 CPU used to run on ggml-cpu's dot for every token (the 256-bit kernel below is AVX2-only). The 512-bit rows sum 521// as that dot sums (the integer lanes, the f16 scale times y.d, eight float lanes, its hsum), so each token's row is ggml's bit for bit 522// at any number of tokens, checked once against this ggml. STRATA_NO_IQ4XS512=1 keeps ggml's dot; STRATA_IQ4XS512_MIN=N: from N tokens on. 523static const bool iq4xs512 = avx512 && std::getenv ("STRATA_NO_IQ4XS512" )== nullptr ; 524static const int iq4xs512_min = [] {const char * e = std::getenv ("STRATA_IQ4XS512_MIN" );return e ? std::atoi (e ) :1 ; }(); 525if (iq4xs512 && f .gu_type == 23 && nt >=iq4xs512_min && f .tail_off == 0 && iq512_reference_ok (23 )) { 526iq512_reference_gu_rows (23 ,blob ,f .gu_row ,f .up_off , (int )f .n_embd ,act ,nt ,ff ,r0 ,r1 ); 527return ; 528 } 529// 0.2.12, OPT-IN (N): STRATA_IQ512_FUSED_MAX=M (default 0 = off) sums the gate and up rows of an IQ3_XXS / IQ3_S / IQ2_S expert (types 530// STRATA_IQ512_FUSED_TYPES, default 18,21,22) for 2..M tokens in ggml-cpu's order, in one pass over the activations (iq512_fused_gu_rows, checked 531// once against this ggml). Each token's rows are then what ggml's one-token dot gives it, whatever the group, instead of the 16-lane sums of the 532// default rows: different rounding (N), measured 1.27-1.34x the default rows at 2-3 tokens on Zen 4 and a loss at one token (so one token keeps ggml's dot). 533static const int fused_max = [] {const char * e = std::getenv ("STRATA_IQ512_FUSED_MAX" );return e ? std::atoi (e ) :0 ; }(); 534if (fused_max >=2 && avx512 && nt >=2 && nt <=fused_max && f .tail_off == 0 && f .gu_type > 0 && f .gu_type < 32 ) { 535static const unsigned fused_types = [] { 536unsigned m = 0 ; 537const char * e = std::getenv ("STRATA_IQ512_FUSED_TYPES" ); 538const std::string v = e != nullptr && e [0 ]!= '\0' ?e :"18,21,22" ; 539size_t at = 0 ; 540while (at < v .size ()) { 541const int t = std::atoi (v .c_str ()+ at ); 542if (t > 0 && t < 32 )m |=1u <<t ; 543const size_t c = v .find (',' ,at ); 544if (c == std::string::npos )break ; 545at = c + 1 ; 546 } 547return m ; 548 }(); 549if (((fused_types >>f .gu_type )& 1u )&& iq512f_supported (f .gu_type )&& iq512f_ok (f .gu_type )) { 550iq512_fused_gu_rows (f .gu_type ,blob ,f .gu_row ,f .up_off , (int )f .n_embd ,act ,nt ,ff ,r0 ,r1 ); 551return ; 552 } 553 } 554const int mt_min = native_gu_mt_min (f .gu_type );// #152 555const bool singleton_iq = Arithmetic == NativeArithmetic ::WindowInvariant && mt_min > 1 ; 556// Unsloth UD-Q4_K_XL's Q4_K gate/up: the multi-token kernel is bit-exact against ggml's per-token dot (any group 557// size, no #152 rule). Opt-in, STRATA_KQ256=1: measured no faster in the engine (a window's expert groups hold 558// ~1.4 tokens and the weights stay in L1 across ggml's per-token calls; 1.01-1.13x in native_expert_parity). 559static const bool kq = [] {const char * v = std::getenv ("STRATA_KQ256" );return cpu_avx2_ok ()&& v != nullptr && std::atoi (v )!= 0 ; }(); 560// KQ matches the ggml AVX2/FMA dot; other builds keep their own singleton arithmetic. 561if (kq && f .gu_type == 12 && nt >=2 && 562 (Arithmetic == NativeArithmetic ::Default || (flavour_avx2 ()))) {// one token: ggml's own dot below (the same bits, less overhead) 563kq256_gu_rows (f .gu_type ,blob ,f .gu_row ,f .up_off , (int )f .n_embd ,act ,nt ,ff ,r0 ,r1 ); 564return ; 565 } 566// The trellis types (TQ2_T / TQK6 / TQK7): Strata's own kernels (tq_cpu.hpp), for every group size - they decode 567// a block once for all the tokens and give each token the same bits whatever the group (no #152 rule needed). 568if (tq_rows_own (f .gu_type )) { 569tq_gu_rows (tq_isa (),f .gu_type ,blob ,f .gu_row ,f .up_off , (int )f .n_embd ,act ,nt ,ff ,r0 ,r1 ); 570return ; 571 } 572// A format with only an AVX-2 kernel (IQ4_XS, #415) takes it on AVX-2 CPUs only: an AVX-512 CPU keeps ggml-cpu for 573// it, as before (its rows would round differently). Each kernel only for the formats it implements: falling 574// through an empty switch would leave ff unwritten instead of falling back to ggml-cpu. 575static const bool cpu512 = cpu_vnni_ok (); 576if (nt >=mt_min && (iq512_supported (f .gu_type )|| (!cpu512 && iq256_supported (f .gu_type )))) { 577if (singleton_iq ) { 578#if defined(__x86_64__ )|| defined(_M_X64 ) 579if (avx512 && flavour_avx2 ()&& 580 (f .gu_type == 18 || f .gu_type == 21 || f .gu_type == 22 )&& iq512_reference_ok (f .gu_type )) { 581iq512_reference_gu_rows (f .gu_type ,blob ,f .gu_row ,f .up_off , (int )f .n_embd ,act ,nt ,ff ,r0 ,r1 ); 582return ; 583 } 584#endif 585// Otherwise use the singleton dot below, keeping its row-major weight reuse across tokens. 586 }else { 587if (avx512 && iq512_supported (f .gu_type )) { 588iq512_gu_rows (f .gu_type ,blob ,f .gu_row ,f .up_off , (int )f .n_embd ,act ,nt ,ff ,r0 ,r1 ); 589return ; 590 } 591if (avx2 && iq256_supported (f .gu_type )) { 592iq256_gu_rows (f .gu_type ,blob ,f .gu_row ,f .up_off , (int )f .n_embd ,act ,nt ,ff ,r0 ,r1 ); 593return ; 594 } 595 } 596 } 597if (!singleton_iq && avx1 && nt >=mt_min && iq128_supported (f .gu_type )) { 598iq128_gu_rows (f .gu_type ,blob ,f .gu_row ,f .up_off , (int )f .n_embd ,act ,nt ,ff ,r0 ,r1 ); 599return ; 600 } 601const int n = (int )f .n_embd ; 602// NVFP4: the per-expert global scales from the blob's tail (1 and 1 otherwise). s_down goes on the down output: 603// folded into up it left the hidden near 1e-5, whose Q8_0 block scale is an FP16 subnormal (expert error 2-12%) 604float sg = 1.f ,su = 1.f ; 605if (f .tail_off ) { 606float tail [4 ]; 607 std::memcpy (tail ,blob + f .tail_off ,sizeof tail ); 608sg = tail [0 ]; 609su = tail [1 ]; 610 } 611// NVFP4 in 512-bit lanes: ggml's arithmetic, blocks decoded once per window; 1.8x at one token, 3.7x at seven 612// (nvfp4_avx512_parity). STRATA_NO_NVFP4_512 falls back to ggml-cpu. 613static const bool nvfp4_512 = cpu_avx512_ok ()&& std::getenv ("STRATA_NO_NVFP4_512" )== nullptr ; 614if (f .gu_type == kNvfp4Type && nvfp4_512 && nvfp4_512_fits (n )) { 615nvfp4_512_gu_rows (blob ,f .gu_row ,f .up_off ,n ,act ,nt ,ff ,r0 ,r1 ,sg ,su ); 616return ; 617 } 618// Without AVX-512: the same rows in 256-bit lanes, bit-equal to ggml-cpu's AVX2 dot below; 2.0-2.4x it at 4-8 619// tokens, 2x under DRAM load with 6 threads (nvfp4_avx512_parity). STRATA_NO_NVFP4_256 falls back to ggml-cpu. 620static const bool nvfp4_256 = cpu_avx2_ok ()&& std::getenv ("STRATA_NO_NVFP4_256" )== nullptr ; 621if (f .gu_type == kNvfp4Type && nvfp4_256 && nvfp4_256_fits (n )) { 622nvfp4_256_gu_rows (blob ,f .gu_row ,f .up_off ,n ,act ,nt ,ff ,r0 ,r1 ,sg ,su ); 623return ; 624 } 625const Tr dot = traits (f .gu_type ); 626for (int r = r0 ;r < r1 ;++ r ) { 627const uint8_t * gr = blob + (size_t )r * f .gu_row ; 628const uint8_t * ur = blob + f .up_off + (size_t )r * f .gu_row ; 629for (int t = 0 ;t < nt ;++ t ) { 630// g and u are read back after both calls (so the up row's dot starts while the gate row's result is still on its way) 631// and each with its own 4-byte load: GCC on arm64 put them side by side and read both with one 8-byte ldp, a load 632// that the two dots' separate 4-byte stores cannot forward to, so every row pair stalled until the stores drained 633// (OWN-2N2: 2% of the Spark's gate/up time with the quant layer's 4-argument dot). A volatile access is never 634// paired. The same arithmetic, the same bits. 635float g = 0.f ,u = 0.f ; 636dot .dot (n ,& g ,gr ,act [t ]); 637dot .dot (n ,& u ,ur ,act [t ]); 638const float gs = * static_cast < volatile const float *> (& g )* sg ; 639const float us = * static_cast < volatile const float *> (& u )* su ; 640ff [t ][r ]= (gs / (1.f + std::exp (- gs )))* us ; 641 } 642 } 643} 644 645template < NativeArithmetic Arithmetic > 646void native_down_rows_impl (const NativeFmt & f ,const uint8_t * blob ,const void * const * hq ,int nt ,float * const * out , 647int r0 ,int r1 ) { 648// IQ4_NL down rows: the AVX-2 multi-token kernel decodes the nibbles and absolutises the weights once per 649// block instead of once per token; ggml-cpu's dot is single-token. STRATA_NO_IQ4NL falls back to it. 650static const bool iq4nl_mt = std::getenv ("STRATA_NO_IQ4NL" )== nullptr ; 651static const int mt_min = [] {const char * e = std::getenv ("STRATA_IQ_MT_MIN" );return e ? std::atoi (e ) :2 ; }(); 652static const bool kq = [] {const char * v = std::getenv ("STRATA_KQ256" );return v != nullptr && std::atoi (v )!= 0 ; }(); 653// Both multi-token kernels below are /arch:AVX2 translation units (kq_avx2.cpp and iq_avx2.cpp), 654// so a CPU without AVX2 has to reach ggml-cpu's vec_dot instead - same reasoning as the gate/up 655// rows above, where `avx512` tested cpu_avx512_ok() and `avx2` did not. 656if (tq_rows_own (f .d_type )) {// the trellis types, as in native_gu_rows 657tq_rows (tq_isa (),f .d_type ,blob + f .down_off ,f .d_row , (int )f .n_ff ,hq ,nt ,out ,r0 ,r1 ); 658return ; 659 } 660if (cpu_avx2_ok ()&& kq && nt >=2 && (f .d_type == 7 || f .d_type == 8 )&& 661 (Arithmetic == NativeArithmetic ::Default || (flavour_avx2 ()))) {// Q5_1 / Q8_0 down: bit-exact, any group size 662kq256_rows (f .d_type ,blob + f .down_off ,f .d_row , (int )f .n_ff ,hq ,nt ,out ,r0 ,r1 ); 663return ; 664 } 665// 0.2.12: Q5_0 down rows (ggml-cpu's dot, once a token) in 512-bit lanes, the same bits (q50_512_ok checks that on first use): the weights 666// of a block pair are decoded once for all the tokens. STRATA_NO_Q50_512=1 keeps ggml's dot; STRATA_Q50_512_MIN=N: from N tokens on (default 2). 667static const bool q50_512 = cpu_vnni_ok ()&& std::getenv ("STRATA_NO_Q50_512" )== nullptr ; 668static const int q50_min = [] {const char * e = std::getenv ("STRATA_Q50_512_MIN" );return e ? std::atoi (e ) :2 ; }(); 669if (q50_512 && f .d_type == 6 && nt >=q50_min && f .tail_off == 0 && q50_512_ok ()) { 670q50_512_down_rows (blob + f .down_off ,f .d_row , (int )f .n_ff ,hq ,nt ,out ,r0 ,r1 ); 671return ; 672 } 673if (cpu_avx2_ok ()&& nt >=mt_min && f .d_type == 20 && iq4nl_mt ) {// #152: the same rule as the gate/up rows 674if (Arithmetic == NativeArithmetic ::Default || mt_min <=1 ) { 675iq4nl256_down_rows (blob + f .down_off ,f .d_row , (int )f .n_ff ,hq ,nt ,out ,r0 ,r1 ); 676return ; 677 } 678#if defined(__x86_64__ )|| defined(_M_X64 ) 679if (flavour_avx2 ()&& iq4nl_reference_ok ()) { 680iq4nl_reference_rows (blob + f .down_off ,f .d_row , (int )f .n_ff ,hq ,nt ,out ,r0 ,r1 ); 681return ; 682 } 683#endif 684 } 685// the older-CPU tier: IQ4_NL and Q2_0 down rows in 128-bit (ggml-cpu's dots for both are scalar here) 686static const bool avx1 = !cpu_avx2_ok ()&& cpu_avx1_ok ()&& std::getenv ("STRATA_NO_IQ128" )== nullptr ; 687if (avx1 && iq128_down_supported (f .d_type )) { 688iq128_down_rows (f .d_type ,blob + f .down_off ,f .d_row , (int )f .n_ff ,hq ,nt ,out ,r0 ,r1 ); 689return ; 690 } 691const int n = (int )f .n_ff ; 692float sd = 1.f ;// NVFP4: the expert's s_down (see native_gu_rows) 693if (f .tail_off ) std::memcpy (& sd ,blob + f .tail_off + 2 * sizeof (float ),sizeof sd ); 694static const bool nvfp4_512 = cpu_avx512_ok ()&& std::getenv ("STRATA_NO_NVFP4_512" )== nullptr ; 695if (f .d_type == kNvfp4Type && nvfp4_512 && nvfp4_512_fits (n )) { 696nvfp4_512_rows (blob + f .down_off ,f .d_row ,n ,hq ,nt ,out ,r0 ,r1 ,sd ); 697return ; 698 } 699static const bool nvfp4_256 = cpu_avx2_ok ()&& std::getenv ("STRATA_NO_NVFP4_256" )== nullptr ;// see above 700if (f .d_type == kNvfp4Type && nvfp4_256 && nvfp4_256_fits (n )) { 701nvfp4_256_rows (blob + f .down_off ,f .d_row ,n ,hq ,nt ,out ,r0 ,r1 ,sd ); 702return ; 703 } 704const Tr dot = traits (f .d_type ); 705for (int r = r0 ;r < r1 ;++ r ) { 706const uint8_t * dr = blob + f .down_off + (size_t )r * f .d_row ; 707for (int t = 0 ;t < nt ;++ t ) { 708float s = 0.f ; 709dot .dot (n ,& s ,dr ,hq [t ]); 710out [t ][r ]= s * sd ; 711 } 712 } 713} 714 715// Keep one dispatch implementation: only width-sensitive IQ kernels need a different arithmetic path. 716// Already invariant kernels (including NVFP4) retain their shared decoding and scale handling. 717void native_gu_rows (const NativeFmt & f ,const uint8_t * blob ,const void * const * act ,int nt , 718float * const * ff ,int r0 ,int r1 ) { 719native_gu_rows_impl < NativeArithmetic ::Default > (f ,blob ,act ,nt ,ff ,r0 ,r1 ); 720} 721 722void native_down_rows (const NativeFmt & f ,const uint8_t * blob ,const void * const * hq ,int nt , 723float * const * out ,int r0 ,int r1 ) { 724native_down_rows_impl < NativeArithmetic ::Default > (f ,blob ,hq ,nt ,out ,r0 ,r1 ); 725} 726 727void native_gu_rows_invariant (const NativeFmt & f ,const uint8_t * blob ,const void * const * act ,int nt , 728float * const * ff ,int r0 ,int r1 ) { 729native_gu_rows_impl < NativeArithmetic ::WindowInvariant > (f ,blob ,act ,nt ,ff ,r0 ,r1 ); 730} 731 732void native_down_rows_invariant (const NativeFmt & f ,const uint8_t * blob ,const void * const * hq ,int nt , 733float * const * out ,int r0 ,int r1 ) { 734native_down_rows_impl < NativeArithmetic ::WindowInvariant > (f ,blob ,hq ,nt ,out ,r0 ,r1 ); 735} 736 737}// namespace strata::kernels::cpu