okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
a37491ede
main
1// src/kernels/iq_parity.cpp - plan v0.3 P6: the i-quant kernels against gguf-py on generated rows. 2// 3// python tools/iq_fixture.py --out <dir> && build/iq_parity <dir> 4// 5// The fixtures are GENERATED (deterministic; TODO 24) - tools/iq_fixture.py writes each type's raw block 6// bytes and the gguf-py dequantized reference (Q2_0, this repository's type 42, is dequantized by 7// tools/gguf_writer.py's codec instead - gguf-py has no such type). Dependencies: python3 with numpy and 8// the vendored gguf-py that setup downloads (the pinned llama.cpp zip; third_party/ is gitignored). 9// 10// What is compared, and how: the dequant parity is a MEASURED relative error (sum|got-ref| / sum|ref|) - 11// bitwise equality is not claimed and not required; on the generated fixtures it measures 0.00e+00. The 12// MMVQ dot (q8_1 activations) must match the float matrix-vector product over that reference within the 13// activation rounding (a few 1e-3 relative), for 1 to 8 columns, every column of a multi-column call bitwise 14// equal to a one-column call on it. 15#include "strata/kernels/iq_kernels.hpp" 16#include "strata/kernels/native_mmvq.hpp" 17 18#include <cuda_runtime.h> 19 20#include <cmath> 21#include <cstdio> 22#include <cstring> 23#include <random> 24#include <string> 25#include <vector> 26 27int main (int argc ,char ** argv ) { 28const std::string dir = argc > 1 ?argv [1 ] :"logs/iq_fixture" ; 29const char * names []= {"IQ2_XXS" ,"IQ2_XS" ,"IQ2_S" ,"IQ3_XXS" ,"IQ3_S" ,"IQ1_M" ,"IQ4_NL" ,"IQ4_XS" ,"Q2_0" ,"Q3_K" }; 30int failures = 0 ,missing = 0 ; 31cudaStream_t s ; 32cudaStreamCreate (& s ); 33for (const char * nm :names ) { 34 std::FILE * f = std::fopen ((dir + "/" + nm + ".bin" ).c_str (),"rb" ); 35 std::FILE * g = std::fopen ((dir + "/" + nm + ".f32" ).c_str (),"rb" ); 36if (!f || !g ) { 37 std::printf ("%-8s missing fixture: %s/%s.bin or %s/%s.f32 - generate the fixtures first with " 38"python tools/iq_fixture.py --out %s (deterministic; see the tool's --help)\n" , 39nm ,dir .c_str (),nm ,dir .c_str (),nm ,dir .c_str ()); 40if (f ) std::fclose (f ); 41if (g ) std::fclose (g ); 42++ failures ; 43++ missing ; 44continue ; 45 } 46int hdr [3 ]; 47 std::fread (hdr ,4 ,3 ,f ); 48const int type = hdr [0 ],rows = hdr [1 ],cols = hdr [2 ]; 49 std::vector < uint8_t > raw ; 50 {uint8_t buf [1 <<16 ];size_t n ;while ((n = std::fread (buf ,1 ,sizeof buf ,f ))> 0 )raw .insert (raw .end (),buf ,buf + n ); } 51 std::vector < float > ref ((size_t )rows * cols ); 52 std::fread (ref .data (),4 ,ref .size (),g ); 53 std::fclose (f ); 54 std::fclose (g ); 55void * dw = nullptr ; 56float * dq = nullptr ; 57cudaMalloc (& dw ,raw .size ()); 58cudaMalloc (& dq ,ref .size ()* 4 ); 59cudaMemcpy (dw ,raw .data (),raw .size (),cudaMemcpyHostToDevice ); 60double dq_err = 0.0 ; 61if (strata::kernels::iq_supported (type )&& ((size_t )rows * cols ) %256 == 0 ) { 62 strata::kernels::iq_dequant_f32 (type ,dw , (int64_t )rows * cols ,dq ,s ); 63 std::vector < float > got (ref .size ()); 64cudaMemcpy (got .data (),dq ,got .size ()* 4 ,cudaMemcpyDeviceToHost ); 65double num = 0 ,den = 0 ; 66for (size_t i = 0 ;i < ref .size ();++ i ) {num += std::fabs (got [i ]- ref [i ]);den += std::fabs (ref [i ]); } 67dq_err = num / (den + 1e-30 ); 68 } 69// the dot, ncols 1..8: each column of an ncols call bitwise equal to a one-column call on it (the 70// exact multi-column layout, the default), all of them within the activation rounding of the float product 71const int MC = 8 ; 72 std::mt19937 rng (7 ); 73 std::normal_distribution < float > nd (0.f ,1.f ); 74 std::vector < float > x ((size_t )MC * cols ); 75for (auto & v :x )v = nd (rng ); 76float * dx = nullptr ; 77void * xq = nullptr ; 78float * dy = nullptr ; 79cudaMalloc (& dx ,x .size ()* 4 ); 80cudaMalloc (& xq , (size_t )MC * cols /32 * 36 ); 81cudaMalloc (& dy , (size_t )MC * rows * 4 ); 82cudaMemcpy (dx ,x .data (),x .size ()* 4 ,cudaMemcpyHostToDevice ); 83 strata::kernels::quantize_q8_1_rows (dx ,MC ,cols ,xq ,s ); 84 std::vector < float > y ((size_t )MC * rows ),yn (y .size ()); 85int multi_bad = 0 ; 86try { 87for (int c = 0 ;c < MC ;++ c ) 88 strata::kernels::native_mmvq (type ,dw , (const uint8_t * )xq + (size_t )c * cols /32 * 36 , 89dy + (size_t )c * rows ,cols ,rows ,1 ,s ); 90cudaStreamSynchronize (s ); 91cudaMemcpy (y .data (),dy ,y .size ()* 4 ,cudaMemcpyDeviceToHost ); 92for (int nc = 2 ;nc <=MC ;++ nc ) { 93 strata::kernels::native_mmvq (type ,dw ,xq ,dy ,cols ,rows ,nc ,s ); 94cudaStreamSynchronize (s ); 95cudaMemcpy (yn .data (),dy ,yn .size ()* 4 ,cudaMemcpyDeviceToHost ); 96if (strata::kernels::native_mmvq_multi_exact ()&& 97 std::memcmp (yn .data (),y .data (), (size_t )nc * rows * 4 )!= 0 ) { 98 std::printf ("%-8s mmvq ncols %d: not bitwise equal to the one-column calls\n" ,nm ,nc ); 99++ multi_bad ; 100 } 101 } 102 }catch (const std::exception & e ) { std::printf ("%-8s mmvq: %s\n" ,nm ,e .what ());++ failures ;continue ; } 103double num = 0 ,den = 0 ; 104for (int c = 0 ;c < MC ;++ c ) 105for (int r = 0 ;r < rows ;++ r ) { 106double acc = 0 ; 107for (int k = 0 ;k < cols ;++ k )acc += (double )ref [(size_t )r * cols + k ]* x [(size_t )c * cols + k ]; 108num += std::fabs (y [(size_t )c * rows + r ]- acc ); 109den += std::fabs (acc ); 110 } 111const double mm_err = num / (den + 1e-30 ); 112const bool ok = dq_err < 1e-6 && mm_err < 2e-2 && multi_bad == 0 ; 113 std::printf ("%-8s type %2d %4d x %5d dequant rel %.2e mmvq rel %.2e (ncols 1..%d) %s\n" ,nm ,type ,rows ,cols , 114dq_err ,mm_err ,MC ,ok ?"ok" :"FAIL" ); 115if (!ok )++ failures ; 116cudaFree (dw );cudaFree (dq );cudaFree (dx );cudaFree (xq );cudaFree (dy ); 117 } 118 std::printf ("iq_parity: %d failures\n" ,failures ); 119// no fixture at all: the generator was skipped (no numpy / vendored gguf-py) - a skip (3), not a failure 120if (missing == (int ) (sizeof names /sizeof names [0 ])) { 121 std::printf ("iq_parity: no fixtures in %s - skipped\n" ,dir .c_str ()); 122return 3 ; 123 } 124return failures ?1 :0 ; 125}