okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
da4a3a476
main
1// src/kernels/ple_fp8_parity.cpp - the FP8 PLE table (tools/ple_fp8_pack.py) read through PleTable, against values 2// decoded from the checkpoint itself, and against the IQ4_NL table it replaces. 3// 4// ple_fp8_parity <ple-fp8.gguf> <reference.bin> [<iq4nl shard.gguf>] 5// 6// reference.bin: int32 n, uint32 rows[n], float32 values[n][160] - the rows as torch decodes the checkpoint's 7// F8_E4M3 bytes, times its weight_scale (n a multiple of 16 for the gather_batch check). Every row must match BIT FOR BIT through 8// both I/O modes (Direct and Mmap) and through the prompt path's gather_batch. With the IQ4_NL shard the same rows 9// are compared too: same table, so a correlation near 0.997 - a shifted row order would be ~0. 10#include "strata/kernels/ngram.hpp" 11 12#include <cmath> 13#include <cstdio> 14#include <cstdlib> 15#include <cstring> 16#include <fstream> 17#include <string> 18#include <vector> 19 20namespace k= strata::kernels; 21 22int main (int argc ,char ** argv ) { 23if (argc < 3 ) { 24 std::fprintf (stderr ,"usage: ple_fp8_parity <ple-fp8.gguf> <reference.bin> [<iq4nl shard.gguf>]\n" ); 25return 2 ; 26 } 27 std::ifstream f (argv [2 ], std::ios::binary ); 28int32_t n = 0 ; 29f .read ((char * )& n ,4 ); 30 std::vector < uint32_t > rows ((size_t )n ); 31 std::vector < float > ref ((size_t )n * k::PLE_HEAD_DIM ); 32f .read ((char * )rows .data (), (std::streamsize )rows .size ()* 4 ); 33f .read ((char * )ref .data (), (std::streamsize )ref .size ()* 4 ); 34if (!f || n <=0 ) { std::fprintf (stderr ,"cannot read %s\n" ,argv [2 ]);return 2 ; } 35 36int bad = 0 ; 37for (const k::PleIo mode : {k::PleIo ::Direct , k::PleIo ::Mmap }) { 38 k::PleTable t ; 39 std::string err ; 40 k::PleIoOptions io ; 41io .mode = mode ; 42if (!t .open (argv [1 ],err ,io )) { std::fprintf (stderr ,"open: %s\n" ,err .c_str ());return 1 ; } 43 std::vector < float > got ((size_t )n * k::PLE_HEAD_DIM ); 44for (int i = 0 ;i < n ;++ i )t .read_row (rows [(size_t )i ],got .data ()+ (size_t )i * k::PLE_HEAD_DIM ); 45int diff = 0 ; 46for (size_t j = 0 ;j < got .size ();++ j )diff += std::memcmp (& got [j ],& ref [j ],4 )!= 0 ; 47 std::vector < float > batch ((size_t )n * k::PLE_HEAD_DIM );// the prompt path: all rows as one request 48const bool ok = t .gather_batch (rows .data (), (size_t )n / k::PLE_N_HEADS ,batch .data (),err ); 49int bdiff = 0 ; 50const size_t nb = (size_t )n / k::PLE_N_HEADS * k::PLE_N_HEADS * k::PLE_HEAD_DIM ; 51for (size_t j = 0 ;ok && j < nb ;++ j )bdiff += std::memcmp (& batch [j ],& ref [j ],4 )!= 0 ; 52 std::printf ("%-6s %s, %llu rows: read_row %d/%zu values differ, gather_batch %s %d differ\n" , 53mode == k::PleIo ::Direct ?"Direct" :"Mmap" ,t .format (), (unsigned long long )t .rows (),diff , 54got .size (),ok ?"ok," :"FAILED," ,bdiff ); 55bad += diff + bdiff + (ok ?0 :1 )+ (std::strcmp (t .format (), std::getenv ("PLE_EXPECT_FORMAT" ) ? std::getenv ("PLE_EXPECT_FORMAT" ) :"F8_E4M3" )!= 0 ); 56 } 57if (argc > 3 ) { 58 k::PleTable q ; 59 std::string err ; 60if (!q .open (argv [3 ],err )) { std::fprintf (stderr ,"open %s: %s\n" ,argv [3 ],err .c_str ());return 1 ; } 61double cmin = 1 ,rsum = 0 ; 62int counted = 0 ; 63 std::vector < float > v (k::PLE_HEAD_DIM ); 64for (int i = 0 ;i < n ;++ i ) { 65q .read_row (rows [(size_t )i ],v .data ()); 66const float * r = ref .data ()+ (size_t )i * k::PLE_HEAD_DIM ; 67double sa = 0 ,sb = 0 ,sab = 0 ,sa2 = 0 ,sb2 = 0 ,e2 = 0 ,r2 = 0 ; 68for (int j = 0 ;j < k::PLE_HEAD_DIM ;++ j ) { 69sa += r [j ];sb += v [j ];sab += (double )r [j ]* v [j ];sa2 += (double )r [j ]* r [j ];sb2 += (double )v [j ]* v [j ]; 70e2 += ((double )v [j ]- r [j ])* ((double )v [j ]- r [j ]);r2 += (double )r [j ]* r [j ]; 71 } 72if (r2 == 0 )continue ;// an all-zero row (the table has some) 73const double m = k::PLE_HEAD_DIM ; 74const double c = (sab - sa * sb /m ) / std::sqrt ((sa2 - sa * sa /m )* (sb2 - sb * sb /m )); 75cmin = std::fmin (cmin ,c ); 76rsum += std::sqrt (e2 /r2 ); 77++ counted ; 78 } 79 std::printf ("IQ4_NL table, same rows: correlation min %.4f, error vs FP8 %.1f%% mean (%d non-zero rows)\n" , 80cmin ,100 * rsum / (counted ?counted :1 ),counted ); 81if (cmin < 0.98 )++ bad ; 82 } 83 std::printf ("RESULT: %s\n" ,bad ?"MISMATCH" :"FP8 rows exact" ); 84return bad ?1 :0 ; 85}