okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
760472559
main
1// src/kernels/cpu/portable.cpp - the CPU kernel symbols on a non-x86 build (aarch64: DGX Spark / GB10, Grace). 2// 3// Replaces expert.cpp, q2_avx2.cpp, iq_avx2.cpp, iq_avx512.cpp, kq_avx2.cpp and kq_avx1.cpp, which are AVX-only. 4// What a native (GGUF) pack needs from this file is the activation quantizer (expert.cpp's scalar loop, bitwise 5// the same as its AVX-512 version) and the BF16 router dot (NEON). The i-quant / k-quant multi-token kernels 6// report themselves unsupported, so native_expert.cpp takes ggml-cpu's vec_dot, which has NEON paths. The 7// canonical Q2_0 pack (its own blob layout) is refused: those kernels are AVX-512 only. A native pack's Q2_0 8// rows are not routed here: q2_native_kernels() is false when cpu_avx2_ok() is, so they use ggml-cpu too. 9// Local port of the approach in https://github.com/Niko1221/Strata/pull/1077 onto main e8ca9af. 10#include "strata/kernels/cpu/expert.hpp" 11#include "strata/kernels/cpu/iq_avx2.hpp" 12#include "strata/kernels/cpu/iq_avx512.hpp" 13#include "strata/kernels/cpu/kq_avx1.hpp" 14#include "strata/kernels/cpu/kq_avx2.hpp" 15#include "strata/kernels/cpu/iq_avx1.hpp" 16#include "strata/kernels/cpu/nvfp4_avx2.hpp" 17#include "strata/kernels/cpu/nvfp4_avx512.hpp" 18 19#include <atomic> 20#include <cmath> 21#include <cstdio> 22#include <cstdlib> 23#include <cstring> 24 25#if defined(__ARM_NEON ) 26#include <arm_neon.h> 27#endif 28 29namespace strata::kernels::cpu { 30namespace { 31 32std::atomic < bool > oracle_q8_0 {false}; 33 34[[noreturn ]]void unsupported (const char * what ) { 35 std::fprintf (stderr , 36"strata: %s is x86-only (AVX2/AVX-512) and this build has no portable version.\n" 37" On this CPU use a native GGUF pack (IQ2_XS, IQ3_S, IQ4_NL, Q4_K, ...).\n" , 38what ); 39 std::abort (); 40} 41 42inline float bf16f (uint16_t h ) { 43const uint32_t b = (uint32_t )h <<16 ; 44float f ; 45 std::memcpy (& f ,& b ,4 ); 46return f ; 47} 48 49}// namespace 50 51 52 53 54 55void act_quant_q8_1 (const float * x ,int n ,ActQ & a ) { 56// The Q8_0 oracle contract (expert.cpp's quantize_oracle_q8_0) is an AVX rounding. The legacy scalar loop 57// below is what the engine uses; the oracle flag would silently quantize differently here. 58if (oracle_q8_0 .load (std::memory_order_relaxed ))unsupported ("the Q8_0 oracle activation quantizer" ); 59// expert.cpp's scalar loop: round half away from zero, clamp to +-127. 60a .nchunks = n /QKA ; 61for (int k = 0 ;k < a .nchunks ;++ k ) { 62const float * xb = x + k * QKA ; 63float amax = 0.f ; 64for (int j = 0 ;j < QKA ;++ j )amax = std::fmax (amax , std::fabs (xb [j ])); 65const float s = amax > 0.f ?amax /127.f :0.f ; 66const float inv = s > 0.f ?1.f /s :0.f ; 67int32_t sum = 0 ; 68int8_t * q = a .q + k * QKA ; 69for (int j = 0 ;j < QKA ;++ j ) { 70const float t = xb [j ]* inv ; 71const float r = t + (t >=0.f ?0.5f :-0.5f ); 72int v = (int )r ; 73v = v < -127 ?-127 : (v > 127 ?127 :v ); 74q [j ]= (int8_t )v ; 75sum += v ; 76 } 77a .scale [k ]= s ; 78a .sum [k ]= sum ; 79a .hx [k ]= s * (float )sum ; 80 } 81} 82 83void act_quant_q8_1_avx2 (const float * x ,int n ,ActQ & a ) {act_quant_q8_1 (x ,n ,a ); } 84 85void s2_expert_vnni (const uint8_t * ,const float * ,float * ,ExpertScratch & ) {unsupported ("the Q2_0 expert kernel" ); } 86void s2_expert_vnni_q (const uint8_t * ,const ActQ & ,float * ,ExpertScratch & ) {unsupported ("the Q2_0 expert kernel" ); } 87void s2_expert_gu_rows (const uint8_t * ,const ActQ & ,float * ,int ,int ) {unsupported ("the Q2_0 expert kernel" ); } 88void s2_expert_down_rows (const uint8_t * ,const ActQ & ,float * ,int ,int ) {unsupported ("the Q2_0 expert kernel" ); } 89void s2_expert_gu_rows_multi (const uint8_t * ,const ActQ * const * ,int ,float * const * ,int ,int ) { 90unsupported ("the Q2_0 expert kernel" ); 91} 92void s2_expert_down_rows_multi (const uint8_t * ,const ActQ * const * ,int ,float * const * ,int ,int ) { 93unsupported ("the Q2_0 expert kernel" ); 94} 95void s2_expert_vnni_multi (const uint8_t * ,const ActQ * const * ,int ,float * const * ,ExpertScratchMulti & ) { 96unsupported ("the Q2_0 expert kernel" ); 97} 98void q2_0_gguf_rows_multi (const uint8_t * ,size_t ,int ,const ActQ * const * ,int ,float * const * ,int ,int ) { 99unsupported ("the Q2_0 GGUF row kernel" ); 100} 101void q2_0_gguf_rows_multi_avx2 (const uint8_t * ,size_t ,int ,const ActQ * const * ,int ,float * const * ,int ,int ) { 102unsupported ("the Q2_0 GGUF row kernel" ); 103} 104bool q2_bitplane_enabled () {return false; } 105void q2_0_gguf_rows_multi_avx2_legacy (const uint8_t * ,size_t ,int ,const ActQ * const * ,int ,float * const * ,int ,int ) { 106unsupported ("the Q2_0 GGUF row kernel" ); 107} 108void q2_0_gguf_rows_multi_avx2_v (bool ,const uint8_t * ,size_t ,int ,const ActQ * const * ,int ,float * const * ,int ,int ) { 109unsupported ("the Q2_0 GGUF row kernel" ); 110} 111void s2_expert_scalar (const uint8_t * ,const float * ,float * ,bool ) {unsupported ("the Q2_0 scalar oracle" ); } 112 113void iq512_gu_rows (int ,const uint8_t * ,size_t ,size_t ,int ,const void * const * ,int ,float * const * ,int ,int ) { 114unsupported ("the AVX-512 i-quant kernel" ); 115} 116void iq512_rows (int ,const uint8_t * ,size_t ,int ,const void * const * ,int ,float * const * ,int ,int ) { 117unsupported ("the AVX-512 i-quant kernel" ); 118} 119bool iq512_gather_on ()noexcept {return false; } 120bool iq512x_supported (int )noexcept {return false; } 121void iq512x_rows (int ,const uint8_t * ,size_t ,int ,const void * const * ,int ,float * const * ,int ,int ) { 122unsupported ("the AVX-512 exact i-quant kernel" ); 123} 124void q50_512_down_rows (const uint8_t * ,size_t ,int ,const void * const * ,int ,float * const * ,int ,int ) { 125unsupported ("the AVX-512 Q5_0 down kernel" ); 126} 127bool iq512f_supported (int )noexcept {return false; } 128bool q2_rows_512_ok () {return false; } 129void iq512_reference_gu_rows (int ,const uint8_t * ,size_t ,size_t ,int ,const void * const * ,int ,float * const * ,int ,int ) { 130unsupported ("the AVX-512 reference gate/up kernel" ); 131} 132void iq512_fused_gu_rows (int ,const uint8_t * ,size_t ,size_t ,int ,const void * const * ,int ,float * const * ,int ,int ) { 133unsupported ("the AVX-512 fused gate/up kernel" ); 134} 135bool iq256x_supported (int )noexcept {return false; }// (the DeepSeek CPU lane's multi-token rows: ggml-cpu's NEON dots instead) 136void iq256x_rows (int ,const uint8_t * ,size_t ,int ,const void * const * ,int ,float * const * ,int ,int ) { 137unsupported ("the AVX2 multi-token i-quant rows" ); 138} 139int iq256_variant ()noexcept {return 0 ; } 140int iq256_variants ()noexcept {return 0 ; } 141void iq256_gu_rows_v (int ,int ,const uint8_t * ,size_t ,size_t ,int ,const void * const * ,int ,float * const * ,int ,int ) { 142unsupported ("the AVX-2 i-quant kernel" ); 143} 144void iq256_rows_v (int ,int ,const uint8_t * ,size_t ,int ,const void * const * ,int ,float * const * ,int ,int ) { 145unsupported ("the AVX-2 i-quant kernel" ); 146} 147void iq256_gu_rows (int ,const uint8_t * ,size_t ,size_t ,int ,const void * const * ,int ,float * const * ,int ,int ) { 148unsupported ("the AVX-2 i-quant kernel" ); 149} 150void iq256_rows (int ,const uint8_t * ,size_t ,int ,const void * const * ,int ,float * const * ,int ,int ) { 151unsupported ("the AVX-2 i-quant kernel" ); 152} 153void iq4nl256_down_rows_v (int ,const uint8_t * ,size_t ,int ,const void * const * ,int ,float * const * ,int ,int ) { 154unsupported ("the AVX-2 IQ4_NL kernel" ); 155} 156void iq4nl256_down_rows (const uint8_t * ,size_t ,int ,const void * const * ,int ,float * const * ,int ,int ) { 157unsupported ("the AVX-2 IQ4_NL kernel" ); 158} 159void q8k_quant_avx2 (const float * ,void * ,int64_t ) {unsupported ("the AVX-2 Q8_K quantizer" ); } 160void kq256_gu_rows (int ,const uint8_t * ,size_t ,size_t ,int ,const void * const * ,int ,float * const * ,int ,int ) { 161unsupported ("the AVX-2 k-quant kernel" ); 162} 163void kq256_rows (int ,const uint8_t * ,size_t ,int ,const void * const * ,int ,float * const * ,int ,int ) { 164unsupported ("the AVX-2 k-quant kernel" ); 165} 166void bf16_rows_dot_multi_avx1 (const uint16_t * ,int ,int ,const float * ,int ,float * ) { 167unsupported ("the AVX1 router dot" ); 168} 169 170void bf16_rows_dot (const uint16_t * w ,int rows ,int cols ,const float * x ,float * out ) { 171bf16_rows_dot_multi (w ,rows ,cols ,x ,1 ,out ); 172} 173 174void bf16_rows_dot_multi (const uint16_t * w ,int rows ,int cols ,const float * x ,int nt ,float * out ) { 175for (int r = 0 ;r < rows ;++ r ) { 176const uint16_t * wr = w + (size_t )r * (size_t )cols ; 177for (int t = 0 ;t < nt ;++ t ) { 178const float * xt = x + (size_t )t * cols ; 179int c = 0 ; 180float s = 0.f ; 181#if defined(__ARM_NEON ) 182float32x4_t acc0 = vdupq_n_f32 (0.f ),acc1 = vdupq_n_f32 (0.f ); 183for (;c + 8 <=cols ;c += 8 ) { 184const uint16x8_t h = vld1q_u16 (wr + c ); 185const float32x4_t w0 = vreinterpretq_f32_u32 (vshll_n_u16 (vget_low_u16 (h ),16 )); 186const float32x4_t w1 = vreinterpretq_f32_u32 (vshll_n_u16 (vget_high_u16 (h ),16 )); 187acc0 = vfmaq_f32 (acc0 ,w0 ,vld1q_f32 (xt + c )); 188acc1 = vfmaq_f32 (acc1 ,w1 ,vld1q_f32 (xt + c + 4 )); 189 } 190s = vaddvq_f32 (vaddq_f32 (acc0 ,acc1 )); 191#endif 192for (;c < cols ;++ c )s += bf16f (wr [c ])* xt [c ]; 193out [(size_t )t * rows + r ]= s ; 194 } 195 } 196} 197 198 199// aarch64 has none of the x86 kernels: these names are referenced by shared code but reached only behind 200// the x86 feature checks (all of them report unsupported here). 201[[noreturn ]]static void cpu_kernel_unreachable (const char * name ) { 202 std::fprintf (stderr ,"strata: %s is an x86 kernel and cannot run on this CPU\n" ,name ); 203 std::abort (); 204} 205void act_quant_q8_1_avx1 (const float * x ,int n ,ActQ & a ) {cpu_kernel_unreachable ("act_quant_q8_1_avx1" ); } 206void iq128_down_rows (int ggml_type ,const uint8_t * w ,size_t row_bytes ,int n ,const void * const * hq ,int nt ,float * const * out ,int r0 ,int r1 ) {cpu_kernel_unreachable ("iq128_down_rows" ); } 207bool iq128_down_supported (int ggml_type )noexcept {return false; } 208void iq128_gu_rows (int ggml_type ,const uint8_t * blob ,size_t gu_row ,size_t up_off ,int n ,const void * const * act ,int nt ,float * const * ff ,int r0 ,int r1 ) {cpu_kernel_unreachable ("iq128_gu_rows" ); } 209bool iq128_supported (int ggml_type )noexcept {return false; } 210void iq256_gu_rows_exact_one (int ggml_type ,const uint8_t * blob ,size_t gu_row ,size_t up_off ,int n ,const void * act ,float * ff ,int r0 ,int r1 ) {cpu_kernel_unreachable ("iq256_gu_rows_exact_one" ); } 211bool nvfp4_256_fits (int n ) {return false; } 212void nvfp4_256_gu_rows (const uint8_t * blob ,size_t gu_row ,size_t up_off ,int n ,const void * const * act ,int nt ,float * const * ff ,int r0 ,int r1 ,float s_gate ,float s_up ) {cpu_kernel_unreachable ("nvfp4_256_gu_rows" ); } 213void nvfp4_256_rows (const uint8_t * w ,size_t row_bytes ,int n ,const void * const * act ,int nt ,float * const * out ,int r0 ,int r1 ,float scale ) {cpu_kernel_unreachable ("nvfp4_256_rows" ); } 214bool nvfp4_512_fits (int n ) {return false; } 215void nvfp4_512_gu_rows (const uint8_t * blob ,size_t gu_row ,size_t up_off ,int n ,const void * const * act ,int nt ,float * const * ff ,int r0 ,int r1 ,float s_gate ,float s_up ) {cpu_kernel_unreachable ("nvfp4_512_gu_rows" ); } 216void nvfp4_512_rows (const uint8_t * w ,size_t row_bytes ,int n ,const void * const * act ,int nt ,float * const * out ,int r0 ,int r1 ,float scale ) {cpu_kernel_unreachable ("nvfp4_512_rows" ); } 217void q2_0_gguf_rows_multi_avx1_legacy (const uint8_t * w ,size_t row_bytes ,int nblocks ,const ActQ * const * a ,int nt ,float * const * out ,int r0 ,int r1 ) {cpu_kernel_unreachable ("q2_0_gguf_rows_multi_avx1_legacy" ); } 218void q2_0_gguf_rows_multi_avx1 (const uint8_t * w ,size_t row_bytes ,int nblocks ,const ActQ * const * a ,int nt ,float * const * out ,int r0 ,int r1 ) {cpu_kernel_unreachable ("q2_0_gguf_rows_multi_avx1" ); } 219void q8k_quant_avx1 (const float * x ,void * y ,int64_t n ) {cpu_kernel_unreachable ("q8k_quant_avx1" ); } 220// (the trellis rows are tq_cpu.cpp's: its scalar kernel runs here, as in the Metal build) 221 222}// namespace strata::kernels::cpu