okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
54d7f7d7c
main
1// src/kernels/cpu/q2_bitplane_parity.cpp - the opt-in Q2_0 bit-plane kernel (STRATA_Q2_BITPLANE=1, PR #706), 2// on the AVX-2 pair where the CPU has AVX2 and on the AVX1 pair below it (issue #1699) - the bit-plane is an 3// AVX2/AVX1 feature, the AVX-512 kernel does not read the image. 4// 5// 1. against the legacy kernel of the same rung: the integer part is exact, only the float summation order 6// differs, so the two agree to a few ulps of the largest term (a relative bound on the row's magnitude); 7// 2. bitwise: a token's rows are the same alone, in a window of any width, and cut into any row ranges 8// (the engine's pool splits rows between workers, a verify window changes the width); 9// 3. the legacy path is untouched when the switch is off (this test only runs with the switch on: ctest sets it). 10// Synthetic data, CPU only; skipped (pass) on a CPU without AVX. 11#include "strata/kernels/cpu/expert.hpp" 12#include "strata/kernels/cpu/expert_layout.hpp" 13#include <cmath> 14#include <cstdint> 15#include <cstdio> 16#include <cstdlib> 17#include <cstring> 18#include <random> 19#include <vector> 20 21namespace c= strata::kernels::cpu; 22 23int main () { 24if (!c::q2_bitplane_enabled ()) { 25 std::printf ("q2_bitplane_parity: STRATA_Q2_BITPLANE=1 is not set\n" ); 26return 1 ; 27 } 28if (!c::cpu_avx1_ok ()) { std::printf ("q2_bitplane_parity: no AVX on this CPU, skipped\n" );return 0 ; } 29const bool avx2 = c::cpu_avx2_ok ();// the pair under test: AVX-2 where present, AVX1 below it 30auto quant = [& ](const float * x ,int n , c::ActQ & q ) {avx2 ? c::act_quant_q8_1_avx2 (x ,n ,q ) 31 : c::act_quant_q8_1_avx1 (x ,n ,q ); }; 32auto rows_bp_k = [& ](const uint8_t * w ,size_t rb ,int nb ,const c::ActQ * const * a ,int nt ,float * const * o , 33int r0 ,int r1 ) {avx2 ? c::q2_0_gguf_rows_multi_avx2 (w ,rb ,nb ,a ,nt ,o ,r0 ,r1 ) 34 : c::q2_0_gguf_rows_multi_avx1 (w ,rb ,nb ,a ,nt ,o ,r0 ,r1 ); }; 35auto rows_lg = [& ](const uint8_t * w ,size_t rb ,int nb ,const c::ActQ * const * a ,int nt ,float * const * o , 36int r0 ,int r1 ) {avx2 ? c::q2_0_gguf_rows_multi_avx2_legacy (w ,rb ,nb ,a ,nt ,o ,r0 ,r1 ) 37 : c::q2_0_gguf_rows_multi_avx1_legacy (w ,rb ,nb ,a ,nt ,o ,r0 ,r1 ); }; 38 std::mt19937 rng (706 ); 39 std::normal_distribution < float > nd (0.f ,1.f ); 40int fail = 0 ; 41for (const int n : {128 ,256 ,1024 ,2560 }) {// 2560 = 40 blocks: an even count of pairs; 128 = one pair 42const int nblocks = n /64 ; 43const size_t row_bytes = (size_t )nblocks * 18 ; 44const int rows = 37 ;// not a multiple of 4: the 4-row path and the tail 45 std::vector < uint8_t > w ((size_t )rows * row_bytes ); 46for (int r = 0 ;r < rows ;++ r ) 47for (int b = 0 ;b < nblocks ;++ b ) { 48uint8_t * blk = & w [(size_t )r * row_bytes + (size_t )b * 18 ]; 49const uint16_t d = (uint16_t ) (0x1C00 + rng () %0x0800 );// fp16 ~0.004-0.016 50 std::memcpy (blk ,& d ,2 ); 51for (int i = 0 ;i < 16 ;++ i )blk [2 + i ]= (uint8_t )rng (); 52 } 53const int NT = 8 ; 54static c::ActQ acts [8 ]; 55 std::vector < std::vector < float >> xs (NT , std::vector < float > ((size_t )n )); 56for (int t = 0 ;t < NT ;++ t ) { 57for (float & v :xs [(size_t )t ])v = nd (rng )* (t %3 == 0 ?8.f :1.f ); 58quant (xs [(size_t )t ].data (),n ,acts [t ]); 59if (acts [t ].bp_pairs != nblocks /2 ) { std::printf ("FAIL n=%d: no bit-plane image\n" ,n );++ fail ; } 60 } 61const c::ActQ * ap [8 ]; 62for (int t = 0 ;t < NT ;++ t )ap [t ]= & acts [t ]; 63// the whole window at once, with the bit-plane kernel 64 std::vector < std::vector < float >> win (NT , std::vector < float > ((size_t )rows )),ref = win ,alone = win ,cut = win ; 65float * wo [8 ];float * ro [8 ];float * ao [8 ];float * co [8 ]; 66for (int t = 0 ;t < NT ;++ t ) {wo [t ]= win [(size_t )t ].data ();ro [t ]= ref [(size_t )t ].data (); 67ao [t ]= alone [(size_t )t ].data ();co [t ]= cut [(size_t )t ].data (); } 68rows_bp_k (w .data (),row_bytes ,nblocks ,ap ,NT ,wo ,0 ,rows ); 69rows_lg (w .data (),row_bytes ,nblocks ,ap ,NT ,ro ,0 ,rows ); 70for (int t = 0 ;t < NT ;++ t ) {// one token at a time 71rows_bp_k (w .data (),row_bytes ,nblocks ,ap + t ,1 ,ao + t ,0 ,rows ); 72 } 73for (int r0 = 0 ;r0 < rows ;r0 += 5 )// rows cut into ranges of five 74rows_bp_k (w .data (),row_bytes ,nblocks ,ap ,3 ,co ,r0 ,r0 + 5 < rows ?r0 + 5 :rows ); 75for (int t = 0 ;t < NT ;++ t ) 76for (int r = 0 ;r < rows ;++ r ) { 77const float a = win [(size_t )t ][(size_t )r ],b = ref [(size_t )t ][(size_t )r ]; 78if (std::memcmp (& a ,& alone [(size_t )t ][(size_t )r ],4 )!= 0 ) { 79 std::printf ("FAIL n=%d t=%d r=%d: a token alone differs from the window\n" ,n ,t ,r );++ fail ; 80 } 81if (t < 3 && std::memcmp (& a ,& cut [(size_t )t ][(size_t )r ],4 )!= 0 ) { 82 std::printf ("FAIL n=%d t=%d r=%d: a row range differs from the whole\n" ,n ,t ,r );++ fail ; 83 } 84// a few ulps of the row's magnitude (|d| * |q| * n bounds the sum's terms) 85const float tol = 1e-4f * (std::fabs (b )+ 1.f ); 86if (!(std::fabs (a - b ) <=tol )) { 87 std::printf ("FAIL n=%d t=%d r=%d: bit-plane %.7g vs legacy %.7g\n" ,n ,t ,r ,a ,b );++ fail ; 88 } 89 } 90 } 91// a width that is not a multiple of 128 (an odd number of blocks): no image, the legacy kernel answers 92 { 93const int n = 192 ,nblocks = 3 ; 94static c::ActQ a ; 95 std::vector < float > x ((size_t )n ); 96for (float & v :x )v = nd (rng ); 97quant (x .data (),n ,a ); 98if (a .bp_pairs != 0 ) { std::printf ("FAIL: an odd block count got an image\n" );++ fail ; } 99 } 100 std::printf (fail ?"FAIL (%d)\n" :"PASS\n" ,fail ); 101return fail ?1 :0 ; 102}