okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
0fff115df
main
1// Real-artifact Q5_0 / Q5_1 / Q8_0 PLE rows against ggml's reference dequantizer, through both readers (the mapped 2// one and the direct SSD reader). 3#define NOMINMAX 4#include "strata/artifact/gguf_reader.hpp" 5#include "strata/kernels/ngram.hpp" 6#include "ggml.h" 7 8#include <algorithm> 9#include <cmath> 10#include <cstdio> 11#include <string> 12#include <vector> 13 14namespace { 15 16// Largest absolute difference between the table's rows and ggml's dequantization of the same bytes, single rows 17// and one 16-row batch; -1 when the table cannot be opened or read. 18double check (const char * path , strata::kernels::PleIo mode ,const uint8_t * bytes ,size_t row_bytes , 19const ggml_type_traits * traits , std::string & format ,size_t & n_rows ) { 20 strata::kernels::PleIoOptions options ; 21options .mode = mode ; 22 strata::kernels::PleTable table ; 23 std::string err ; 24if (!table .open (path ,err ,options )) { 25 std::fprintf (stderr ,"open: %s\n" ,err .c_str ()); 26return -1.0 ; 27 } 28format = table .format (); 29n_rows = (size_t )table .rows (); 30const uint32_t probes []= {0 ,1 ,12345 ,20000003 , (uint32_t ) (table .rows ()- 1 )}; 31double max_abs = 0.0 ; 32for (uint32_t row :probes ) { 33float got [160 ],want [160 ]; 34table .read_row (row ,got ); 35traits -> to_float (bytes + (size_t )row * row_bytes ,want ,160 ); 36for (int i = 0 ;i < 160 ;++ i ) 37max_abs = std::max (max_abs , (double ) std::fabs (got [i ]- want [i ])); 38 } 39uint32_t rows [16 ]; 40for (int i = 0 ;i < 16 ;++ i )rows [i ]= probes [i %5 ]; 41float batch [16 * 160 ]; 42if (!table .issue (rows )|| !table .collect (batch ,err )) { 43 std::fprintf (stderr ,"collect: %s\n" ,err .c_str ()); 44return -1.0 ; 45 } 46for (int h = 0 ;h < 16 ;++ h ) { 47float want [160 ]; 48traits -> to_float (bytes + (size_t )rows [h ]* row_bytes ,want ,160 ); 49for (int i = 0 ;i < 160 ;++ i ) 50max_abs = std::max (max_abs , (double ) std::fabs (batch [h * 160 + i ]- want [i ])); 51 } 52return max_abs ; 53} 54 55}// namespace 56 57int main (int argc ,char ** argv ) { 58if (argc != 2 ) { 59 std::fprintf (stderr ,"usage: ple_q5_parity <gguf-containing-ple>\n" ); 60return 2 ; 61 } 62 strata::GgufFile gguf (argv [1 ]); 63const auto * tensor = gguf .find ("per_layer_token_embd.weight" ); 64// Q5_0 (#296: OrcaRouter's Q4_K_S), Q5_1 (a Q5_K_M GGUF), Q8_0 (UD-Q6_K_XL, Swift-1.5 Q4_K_L): 5 blocks of 65// 32 per row 66if (!tensor || (tensor -> type != GGML_TYPE_Q5_0 && tensor -> type != GGML_TYPE_Q5_1 && tensor -> type != GGML_TYPE_Q8_0 )|| 67tensor -> shape .size ()!= 2 || tensor -> shape [0 ]!= 160 ) { 68 std::fprintf (stderr ,"expected a Q5_0, Q5_1 or Q8_0 PLE [160, N]\n" ); 69return 2 ; 70 } 71const auto type = (ggml_type )tensor -> type ; 72const size_t row_bytes = type == GGML_TYPE_Q5_0 ?110 :type == GGML_TYPE_Q5_1 ?120 :170 ; 73const auto * traits = ggml_get_type_traits (type ); 74const uint8_t * bytes = gguf .tensor_data (* tensor ); 75bool ok = true; 76for (auto mode : {strata::kernels::PleIo ::Mmap , strata::kernels::PleIo ::Direct }) { 77 std::string format ; 78size_t n_rows = 0 ; 79const double max_abs = check (argv [1 ],mode ,bytes ,row_bytes ,traits ,format ,n_rows ); 80const bool pass = max_abs >=0.0 && max_abs <=1e-6 ; 81 std::printf ("%s PLE, %s reader: %zu rows, max_abs %.3e %s\n" ,format .c_str (), 82mode == strata::kernels::PleIo ::Mmap ?"mapped" :"direct" ,n_rows ,max_abs ,pass ?"PASS" :"FAIL" ); 83ok = ok && pass ; 84 } 85return ok ?0 :1 ; 86}