okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
0479b72e9
main
1// tools/vision/own/compare_sve.cpp - compares two strata-vision output files (SVE1) of the same picture. 2// 3// strata-vision-own-compare <a.sve> <b.sve> 4// Prints one line: n=<tokens> nx= ny= dim= global=<cosine of the two whole files as vectors> tokmin=<smallest per-token cosine> 5// tokmean=<mean per-token cosine> maxabs=<largest |a-b|> rel_rms=<rms(a-b)/rms(a)> 6// Exit 0 when both files read and have the same shape; 1 otherwise ("MISMATCH ..." printed). 7#include <algorithm> 8#include <cmath> 9#include <cstdint> 10#include <cstdio> 11#include <cstdlib> 12#include <cstring> 13#include <string> 14#include <vector> 15 16struct Sve { 17int32_t n = 0 ,nx = 0 ,ny = 0 ,dim = 0 ; 18 std::vector < float > v ; 19}; 20 21static bool load (const char * path ,Sve & s ) { 22FILE * f = std::fopen (path ,"rb" ); 23if (!f ) { std::printf ("MISMATCH cannot open %s\n" ,path );return false; } 24int32_t h [5 ]; 25if (std::fread (h ,sizeof h ,1 ,f )!= 1 || h [0 ]!= 0x31455653 || h [1 ] <=0 || h [4 ] <=0 ) { std::printf ("MISMATCH %s is not an SVE1 file\n" ,path ); std::fclose (f );return false; } 26s .n = h [1 ];s .nx = h [2 ];s .ny = h [3 ];s .dim = h [4 ]; 27s .v .resize ((size_t )s .n * s .dim ); 28const bool ok = std::fread (s .v .data (),sizeof (float ),s .v .size (),f )== s .v .size (); 29 std::fclose (f ); 30if (!ok ) std::printf ("MISMATCH %s is cut short\n" ,path ); 31return ok ; 32} 33 34int main (int argc ,char ** argv ) { 35int worst = 0 ; 36if (argc == 5 && std::strcmp (argv [3 ],"--worst" )== 0 )worst = std::atoi (argv [4 ]); 37else if (argc != 3 ) { std::fprintf (stderr ,"usage: strata-vision-own-compare a.sve b.sve [--worst K]\n" );return 2 ; } 38Sve a ,b ; 39if (!load (argv [1 ],a )|| !load (argv [2 ],b ))return 1 ; 40if (a .n != b .n || a .nx != b .nx || a .ny != b .ny || a .dim != b .dim ) { 41 std::printf ("MISMATCH shapes differ: %d tokens (%dx%d, dim %d) vs %d tokens (%dx%d, dim %d)\n" ,a .n ,a .nx ,a .ny ,a .dim ,b .n ,b .nx ,b .ny ,b .dim ); 42return 1 ; 43 } 44double ab = 0 ,aa = 0 ,bb = 0 ,dd = 0 ,maxabs = 0 ,tmin = 2.0 ,tsum = 0 ; 45struct Tok {int t ;double cos ,na ,nb ,maxd ,dn ; }; 46 std::vector < Tok > toks ; 47for (int t = 0 ;t < a .n ;++ t ) { 48double tab = 0 ,taa = 0 ,tbb = 0 ,tdd = 0 ,tmax = 0 ; 49for (int i = 0 ;i < a .dim ;++ i ) { 50const double x = a .v [(size_t )t * a .dim + i ],y = b .v [(size_t )t * a .dim + i ]; 51tab += x * y ;taa += x * x ;tbb += y * y ; 52dd += (x - y )* (x - y ); 53tdd += (x - y )* (x - y ); 54maxabs = std::fmax (maxabs , std::fabs (x - y )); 55tmax = std::fmax (tmax , std::fabs (x - y )); 56 } 57ab += tab ;aa += taa ;bb += tbb ; 58const double c = taa > 0 && tbb > 0 ?tab / std::sqrt (taa * tbb ) :0.0 ; 59tmin = std::fmin (tmin ,c ); 60tsum += c ; 61toks .push_back ({t ,c , std::sqrt (taa ), std::sqrt (tbb ),tmax , std::sqrt (tdd )}); 62 } 63 std::printf ("n=%d nx=%d ny=%d dim=%d global=%.6f tokmin=%.6f tokmean=%.6f maxabs=%.5g rel_rms=%.5g\n" ,a .n ,a .nx ,a .ny ,a .dim , 64aa > 0 && bb > 0 ?ab / std::sqrt (aa * bb ) :0.0 ,tmin ,tsum /a .n ,maxabs ,aa > 0 ? std::sqrt (dd /aa ) :0.0 ); 65if (worst > 0 ) {// the tokens with the lowest cosine: where they sit, how large they are in each file, and how far apart 66 std::sort (toks .begin (),toks .end (), [](const Tok & p ,const Tok & q ) {return p .cos < q .cos ; }); 67double mean_norm = std::sqrt (aa /a .n ); 68 std::printf ("typical token norm in A: %.4g\n" ,mean_norm ); 69for (int k = 0 ;k < worst && k < (int )toks .size ();++ k ) { 70const Tok & w = toks [(size_t )k ]; 71 std::printf ("worst %d: token %d (x=%d y=%d of %dx%d) cos %.6f |A| %.5g |B| %.5g max|A-B| %.5g |A-B| %.5g\n" ,k + 1 ,w .t ,w .t %a .nx ,w .t /a .nx ,a .nx , 72a .ny ,w .cos ,w .na ,w .nb ,w .maxd ,w .dn ); 73 } 74 } 75return 0 ; 76}