okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
28ce98bf1
main
1// tools/trellis/tqenc_hip_test.cpp - GPU encoder vs the CPU encoder: correctness (GPU bytes decode, by the CPU decoder, 2// to exactly the weights whose error the GPU reported), quality (SQNR, planted recovery) and speed (weights/s). 3// Run under the box locks: flock -s /data/gpu.lock flock -x ~/locks/gpu3.lock env HIP_VISIBLE_DEVICES=3 ./tqenc_hip_test 4#include "tqenc.hpp" 5 6#include <chrono> 7#include <cmath> 8#include <cstdio> 9#include <cstdlib> 10#include <cstring> 11#include <random> 12#include <vector> 13 14extern "C" double tqh_encode (int type ,const float * w ,const float * h ,int64_t nblocks ,uint8_t * dst ,int iters ,int ncand , 15int scale_tries ,int device ,double * err_out ,int64_t * stats ,double * timing ,int fixed_scale_bits ); 16 17using namespace tqenc ; 18static int g_fail = 0 ; 19#define CHECK (c , ...) do { if (!(c)) { ++g_fail; std::printf(" FAIL line %d: %s ", __LINE__, #c); std::printf(__VA_ARGS__); std::printf("\n"); } } while (0) 20static double now () {return std::chrono::duration < double > (std::chrono::steady_clock::now ().time_since_epoch ()).count (); } 21static const char * tname (int t ) {return t == TQK6 ?"TQK6" :t == TQK7 ?"TQK7" :t == TQK5T ?"TQK5T" :t == TQK4T ?"TQK4T" :t == TQK5X ?"TQK5X" :"TQK4X" ; } 22 23int main (int argc ,char ** argv ) { 24const long big = argc > 1 ? std::atol (argv [1 ]) :131072 ;// blocks for the speed runs 25 std::mt19937_64 rng (77 ); 26 std::normal_distribution < float > nd (0.f ,1.f ); 27const int types [6 ]= {TQK4T ,TQK5T ,TQK4X ,TQK5X ,TQK6 ,TQK7 }; 28for (int type :types ) { 29const int bb = block_bytes (type ); 30 std::printf ("== %s\n" ,tname (type )); 31// 1. correctness + CPU comparison on 2048 Gaussian blocks, without and with importance weights 32 { 33const int nb = 2048 ; 34 std::vector < float > w ((size_t )nb * 128 ),h ((size_t )nb * 128 ); 35for (auto & v :w )v = 0.02f * nd (rng ); 36for (auto & v :h )v = 0.2f + std::fabs (nd (rng )); 37for (int wi = 0 ;wi < 2 ;++ wi ) { 38const float * hp = wi ?h .data () :nullptr ; 39 std::vector < uint8_t > g ((size_t )nb * bb ),c ((size_t )nb * bb ); 40 std::vector < double > ge (nb ),ce (nb ); 41int64_t st [2 ]; 42double tm [2 ]; 43const double gt = tqh_encode (type ,w .data (),hp ,nb ,g .data (),1 ,1 ,1 ,0 ,ge .data (),st ,tm ,0 ); 44Params p ; 45p .iters = 1 ; 46int64_t nf ,zb ; 47const double ct = encode_blocks (type ,w .data (),hp ,nb ,c .data (),p ,8 ,ce .data (),& nf ,& zb ); 48 std::vector < float > dec ((size_t )nb * 128 ); 49decode_blocks (type ,g .data (),nb ,dec .data ()); 50double worst = 0 ; 51for (int b = 0 ;b < nb ;++ b ) { 52double e2 = 0 ; 53for (int i = 0 ;i < 128 ;++ i ) { 54const double d = (double )w [b * 128 + i ]- dec [b * 128 + i ]; 55e2 += (hp ?hp [b * 128 + i ] :1.0 )* d * d ; 56 } 57worst = std::max (worst , std::fabs (e2 - ge [b ]) / std::max (1e-30 ,e2 + ge [b ])); 58 } 59CHECK (worst < 1e-5 ,"GPU reported error differs from the decoded error: %.2e" ,worst ); 60int same = 0 ; 61for (int b = 0 ;b < nb ;++ b )same += std::memcmp (& g [(size_t )b * bb ],& c [(size_t )b * bb ],bb )== 0 ; 62 std::printf (" importance %-3s GPU total error %.6e, CPU %.6e (GPU/CPU %.5f); identical blocks %d/%d; decoded-vs-reported worst %.1e\n" , 63wi ?"yes" :"no" ,gt ,ct ,gt /ct ,same ,nb ,worst ); 64CHECK (gt <=ct * 1.002 ,"GPU error is more than 0.2%% above the CPU's" ); 65 } 66 } 67// 2. planted solutions: weights decoded from random streams must be recovered (the scale is found by the refinement) 68 { 69const int nb = 2048 ; 70 std::vector < uint8_t > blk ((size_t )nb * bb ); 71for (int b = 0 ;b < nb ;++ b ) { 72const float d = std::ldexp (0.5f + (float ) (rng () %1000 ) /1000.f ,-8 + (int ) (rng () %6 )); 73const uint16_t dh = float_to_half (d ); 74blk [(size_t )b * bb ]= dh & 255 ; 75blk [(size_t )b * bb + 1 ]= dh >>8 ; 76for (int i = 2 ;i < bb ;++ i )blk [(size_t )b * bb + i ]= (uint8_t )rng (); 77 } 78 std::vector < float > w ((size_t )nb * 128 ); 79decode_blocks (type ,blk .data (),nb ,w .data ()); 80 std::vector < uint8_t > g ((size_t )nb * bb ); 81 std::vector < double > ge (nb ); 82tqh_encode (type ,w .data (),nullptr ,nb ,g .data (),3 ,1 ,1 ,0 ,ge .data (),nullptr ,nullptr ,0 ); 83int ok = 0 ; 84for (int b = 0 ;b < nb ;++ b )ok += ge [b ]== 0.0 ; 85 std::printf (" planted: %d/%d blocks recovered exactly (scale unknown)\n" ,ok ,nb ); 86CHECK (ok > nb * 0.9 ,"planted recovery low" ); 87 } 88// 3. speed on big Gaussian batches 89 { 90 std::vector < float > w ((size_t )big * 128 ); 91for (auto & v :w )v = nd (rng ); 92 std::vector < uint8_t > g ((size_t )big * bb ); 93double en = 0 ; 94for (float v :w )en += (double )v * v ; 95struct Cfg {const char * n ;int iters ,ncand ,tries ; }cfgs [3 ]= {{"fast (1 pass + 1)" ,0 ,1 ,1 }, {"default (iters 1)" ,1 ,1 ,1 }, {"rot3 iters3" ,3 ,3 ,1 }}; 96for (auto & c :cfgs ) { 97double tm [2 ]; 98int64_t stt [2 ]; 99const double t0 = now (); 100const double e = tqh_encode (type ,w .data (),nullptr ,big ,g .data (),c .iters ,c .ncand ,c .tries ,0 ,nullptr ,stt ,tm ,0 ); 101const double s = now ()- t0 ; 102 { 103 std::vector < float > dec ((size_t )big * 128 ); 104decode_blocks (type ,g .data (),big ,dec .data ()); 105double e2 = 0 ; 106for (size_t i = 0 ;i < dec .size ();++ i )e2 += ((double )w [i ]- dec [i ])* ((double )w [i ]- dec [i ]); 107 std::printf (" [check: energy %.6e, reported error %.6e, decoded error %.6e -> SQNR %.3f dB]\n" ,en ,e ,e2 ,10 * std::log10 (en /e2 )); 108 } 109 std::printf (" %-18s SQNR %.3f dB %.2f M weights/s (kernels only %.2f M/s, %.0f passes) total %.2fs [zero blocks %lld, nonfinite %lld]\n" ,c .n , 11010 * std::log10 (en /e ), (double )big * 128 /s /1e6 , (double )big * 128 /tm [0 ] /1e6 ,tm [1 ],s , 111 (long long )stt [1 ], (long long )stt [0 ]); 112 } 113 } 114 } 115 std::printf ("\n%s (%d failures)\n" ,g_fail ?"FAILED" :"OK" ,g_fail ); 116return g_fail ?1 :0 ; 117}