okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
94a4a34b0
main
1// iq512_gather_parity: the 512-bit i-quant rows (iq_avx512.cpp) against ggml-cpu's own dot. 2// (a) iq512x_rows, for 1..4 tokens, is ggml-cpu's single-token vec_dot bit for bit (IQ3_XXS, IQ3_S, IQ2_S), whatever the 3// grid lookup is (scalar loads or gathers: STRATA_IQ_GATHER=0 / 1, ctest runs both); 4// (b) iq512_rows (the engine's multi-token rows) agrees with it to float rounding (another summation order). 5// Synthetic blocks (random bytes, a fixed f16 scale), so no quantizer and no imatrix are needed. SKIP without AVX-512. 6#include "strata/kernels/cpu/expert_layout.hpp" 7#include "strata/kernels/cpu/iq_avx512.hpp" 8 9#include "ggml.h" 10#include "ggml-cpu.h" 11 12#include <cmath> 13#include <cstdio> 14#include <cstring> 15#include <vector> 16 17namespace cpu= strata::kernels::cpu; 18 19static int g_fail = 0 ; 20#define CHECK (c ) \ 21 do { \ 22 if (!(c)) { std::fprintf(stderr, "FAIL %s:%d %s\n", __FILE__, __LINE__, #c); ++g_fail; } \ 23 } while (0) 24 25int main () { 26ggml_cpu_init (); 27if (!cpu::cpu_avx512_ok ()) { 28 std::printf ("iq512_gather_parity: SKIP (no AVX-512)\n" ); 29return 0 ; 30 } 31 std::printf ("iq512_gather_parity: gather %d\n" , (int ) cpu::iq512_gather_on ()); 32constexpr int n = 2560 ,rows = 96 ,ntmax = 4 ; 33const ggml_type_traits_cpu * t8 = ggml_get_type_traits_cpu (GGML_TYPE_Q8_K ); 34uint32_t seed = 987654321u ; 35auto rnd = [& seed ] {seed = seed * 1664525u + 1013904223u ;return seed >>8 ; }; 36const size_t ab = ggml_row_size (GGML_TYPE_Q8_K ,n ); 37 std::vector < std::vector < uint8_t >> act (ntmax , std::vector < uint8_t > (ab )); 38const void * ap [ntmax ]; 39for (int t = 0 ;t < ntmax ;++ t ) { 40 std::vector < float > x (n ); 41for (auto & v :x )v = ((float ) (rnd () %2001 )- 1000.f )* 0.001f * (1.f + (float )t ); 42t8 -> from_float (x .data (),act [t ].data (),n ); 43ap [t ]= act [t ].data (); 44 } 45for (int type : {18 ,21 ,22 }) { 46const ggml_type_traits_cpu * tr = ggml_get_type_traits_cpu ((ggml_type )type ); 47CHECK (tr != nullptr && tr -> vec_dot != nullptr ); 48CHECK (cpu::iq512x_supported (type )&& cpu::iq512_supported (type )); 49const size_t rb = ggml_row_size ((ggml_type )type ,n ),nb = (size_t )n /256 ,bb = rb /nb ; 50 std::vector < uint8_t > w ((size_t )rows * rb ); 51for (auto & b :w )b = (uint8_t )rnd (); 52for (int r = 0 ;r < rows ;++ r ) 53for (size_t k = 0 ;k < nb ;++ k ) {uint8_t * d = & w [(size_t )r * rb + k * bb ];d [0 ]= 0x66 ;d [1 ]= 0x2e ; }// f16 ~0.1 54 std::vector < float > want ((size_t )ntmax * rows ),gx ((size_t )ntmax * rows ),ge ((size_t )ntmax * rows ); 55for (int t = 0 ;t < ntmax ;++ t ) 56for (int r = 0 ;r < rows ;++ r ) 57tr -> vec_dot (n ,& want [(size_t )t * rows + r ],0 ,w .data ()+ (size_t )r * rb ,0 ,act [t ].data (),0 ,1 ); 58for (int nt = 1 ;nt <=ntmax ;++ nt ) { 59float * ox [ntmax ]; 60float * oe [ntmax ]; 61for (int t = 0 ;t < ntmax ;++ t ) {ox [t ]= & gx [(size_t )t * rows ];oe [t ]= & ge [(size_t )t * rows ]; } 62 cpu::iq512x_rows (type ,w .data (),rb ,n ,ap ,nt ,ox ,0 ,rows ); 63 cpu::iq512_rows (type ,w .data (),rb ,n ,ap ,nt ,oe ,0 ,rows ); 64for (int t = 0 ;t < nt ;++ t ) 65for (int r = 0 ;r < rows ;++ r ) { 66const float want_v = want [(size_t )t * rows + r ]; 67CHECK (std::memcmp (& gx [(size_t )t * rows + r ],& want_v ,4 )== 0 );// (a) bit for bit 68const float e = ge [(size_t )t * rows + r ]; 69CHECK (std::fabs (e - want_v ) <=1e-3f * std::fabs (want_v )+ 1e-3f );// (b) float-rounding agreement 70 } 71 } 72 std::printf (" type %d: ok\n" ,type ); 73 } 74if (g_fail == 0 ) std::printf ("iq512_gather_parity: OK\n" ); 75return g_fail == 0 ?0 :1 ; 76}