okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
e4309aaf3
main
1// src/image/noise/qi21_noise_check_main.cpp - test program strata-noise-check (not shipped): Strata's initial noise 2// (include/strata/image/qi21_noise.hpp) against stable-diffusion.cpp's own noise dumps. 3// 4// strata-noise-check <dump dir> [<dump dir> ...] each dir dump_p<P>_s<SEED> with noise.bin (stage_hooks.py) 5// strata-noise-check --write SEED N OUT our N values for SEED into OUT (float32), for the CPU test 6// 7// Prints per dir byte-identical or the count of differing values and the largest difference in ulps, and 8// NOISE-CHECK PASS / FAIL. 9#include "strata/image/qi21_noise.hpp" 10 11#include <algorithm> 12#include <cstdint> 13#include <cstdio> 14#include <cstdlib> 15#include <cstring> 16#include <filesystem> 17#include <fstream> 18#include <string> 19#include <vector> 20 21namespace fs= std::filesystem; 22using namespace strata::image ; 23 24namespace { 25 26int64_t key (float f ) { 27uint32_t u ; 28 std::memcpy (& u ,& f ,4 ); 29return (u & 0x80000000u ) ?- (int64_t ) (u & 0x7fffffffu ) : (int64_t )u ; 30} 31 32}// namespace 33 34int main (int argc ,char ** argv ) { 35if (argc == 5 && std::strcmp (argv [1 ],"--write" )== 0 ) { 36const std::vector < float > v = qi21_noise (std::strtoull (argv [2 ],nullptr ,10 ), (uint32_t ) std::strtoul (argv [3 ],nullptr ,10 )); 37 std::ofstream (argv [4 ], std::ios::binary ).write (reinterpret_cast < const char *> (v .data ()),v .size ()* 4 ); 38return 0 ; 39 } 40if (argc < 2 ) { 41 std::fprintf (stderr ,"usage: strata-noise-check <dump dir> [...] | --write SEED N OUT\n" ); 42return 2 ; 43 } 44int bad = 0 ; 45for (int i = 1 ;i < argc ;++ i ) { 46const fs::path d (argv [i ]); 47const std::string name = d .filename ().string (),tag = d .parent_path ().filename ().string ()+ "/" + name ; 48const size_t at = name .rfind ("_s" ); 49 std::ifstream f (d /"noise.bin" , std::ios::binary ); 50if (at == std::string::npos || !f ) { 51 std::printf ("%s: no seed in the name or no noise.bin\n" ,tag .c_str ()); 52++ bad ; 53continue ; 54 } 55const uint64_t seed = std::strtoull (name .c_str ()+ at + 2 ,nullptr ,10 ); 56 std::vector < char > raw ((std::istreambuf_iterator < char > (f )), {}); 57 std::vector < float > sd (raw .size () /4 ); 58 std::memcpy (sd .data (),raw .data (),sd .size ()* 4 ); 59const std::vector < float > ours = qi21_noise (seed , (uint32_t )sd .size ()); 60size_t differ = 0 ; 61int64_t max_ulp = 0 ; 62for (size_t k = 0 ;k < sd .size ();++ k ) 63if (std::memcmp (& sd [k ],& ours [k ],4 )!= 0 ) { 64++ differ ; 65max_ulp = std::max < int64_t > (max_ulp , std::llabs (key (sd [k ])- key (ours [k ]))); 66 } 67 std::printf ("%s: seed %llu, %zu values: %s" ,tag .c_str (), (unsigned long long )seed ,sd .size (), 68differ == 0 ?"byte-identical" :"DIFFERS" ); 69if (differ ) std::printf (" (%zu values, largest %lld ulp)" ,differ , (long long )max_ulp ); 70 std::printf ("\n" ); 71bad += differ != 0 ; 72 } 73 std::printf ("NOISE-CHECK %s\n" ,bad ?"FAIL" :"PASS" ); 74return bad ?1 :0 ; 75}