okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
0d2ee8def
main
1// src/kernels/hc_persist_parity.cpp - the STRATA_HC_PERSIST=1 hyper-connection read (one persistent kernel) against the 2// default multi read (fused_gr_read_multi). 3// 4// Real geometry (n_embd 2560, hc 4, hc_lr 320), every T = 1..8, the pending write applied and the inject rows on, as the 5// verifier calls it. The persistent read sums in another order, so it is bounded, not bitwise: every output (the mixed 6// row, the inject outputs, the written R) must be finite and within 1e-4 of the default read relative to its largest 7// magnitude, and two runs of the persistent read must agree bit for bit. Where the card does not hold the 80 blocks at 8// once (or on CUDA, where the read is not built) the read reports that it did not run and the test passes as a skip. 9// `--off`: with STRATA_HC_PERSIST unset the persistent read must never run (the default path is untouched). 10// GPU, synthetic, no model. 11#include "strata/kernels/fused_gr.hpp" 12 13#include <cuda_runtime.h> 14 15#include <cmath> 16#include <cstdint> 17#include <cstdio> 18#include <cstdlib> 19#include <cstring> 20#include <random> 21#include <vector> 22 23#if defined(_WIN32 )// MSVC has no setenv / unsetenv 24#define setenv (k ,v ,o ) _putenv_s((k), (v)) 25#define unsetenv (k ) _putenv_s((k), "") 26#endif 27 28namespace k= strata::kernels; 29 30namespace { 31constexpr int N = 2560 ,HC = 4 ,D = N * HC ,LR = 320 ,TM = 8 ; 32void ck (cudaError_t e ,const char * w ) { 33if (e != cudaSuccess ) { 34 std::fprintf (stderr ,"%s: %s\n" ,w ,cudaGetErrorString (e )); 35 std::exit (2 ); 36 } 37} 38uint16_t bf16 (float x ) { 39uint32_t u ; 40 std::memcpy (& u ,& x ,4 ); 41return (uint16_t ) ((u + 0x7fff + ((u >>16 )& 1 )) >>16 ); 42} 43template < typename T > 44T * up (const std::vector < T >& h ) { 45void * p = nullptr ; 46ck (cudaMalloc (& p ,h .size ()* sizeof (T )),"malloc" ); 47ck (cudaMemcpy (p ,h .data (),h .size ()* sizeof (T ),cudaMemcpyHostToDevice ),"h2d" ); 48return (T * )p ; 49} 50template < typename T > 51T * dev (size_t n ) { 52void * p = nullptr ; 53ck (cudaMalloc (& p ,n * sizeof (T )),"malloc" ); 54return (T * )p ; 55} 56}// namespace 57 58int main (int argc ,char ** argv ) { 59const bool off = argc > 1 && std::strcmp (argv [1 ],"--off" )== 0 ; 60if (off )unsetenv ("STRATA_HC_PERSIST" ); 61else setenv ("STRATA_HC_PERSIST" ,"1" ,1 ); 62 k::fused_gr_check (); 63 k::fused_gr_persist_init (); 64 65 std::mt19937 rng (7 ); 66 std::normal_distribution < float > nd (0.f ,1.f ); 67 std::vector < float > R ((size_t )TM * D ),bo ((size_t )TM * N ),inj ((size_t )TM * HC ),wn (D ); 68for (auto & x :R )x = nd (rng ); 69for (auto & x :bo )x = 0.5f * nd (rng ); 70for (auto & x :inj )x = nd (rng ); 71for (auto & x :wn )x = 1.f + 0.3f * nd (rng ); 72 std::vector < uint16_t > wd ((size_t )LR * D ),wu ((size_t )D * LR ),wi ((size_t )HC * D ); 73for (auto & x :wd )x = bf16 (0.02f * nd (rng )); 74for (auto & x :wu )x = bf16 (0.05f * nd (rng )); 75for (auto & x :wi )x = bf16 (0.02f * nd (rng )); 76float * dR = dev < float > (R .size ()); 77float * dbo = up (bo ); 78float * dinj = up (inj ); 79float * dwn = up (wn ); 80uint16_t * dwd = up (wd ); 81uint16_t * dwu = up (wu ); 82uint16_t * dwi = up (wi ); 83float * dxn = dev < float > ((size_t )TM * D ); 84float * dlo = dev < float > ((size_t )TM * LR ); 85float * drs = dev < float > ((size_t )TM * HC ); 86float * dio = dev < float > ((size_t )TM * HC ); 87float * dmix = dev < float > ((size_t )TM * N ); 88 89int bad = 0 ,ran = 0 ; 90for (int T = 1 ;T <=TM ;++ T ) { 91 k::FusedGrArgs a [TM ]; 92for (int t = 0 ;t < T ;++ t ) { 93a [t ]= k::FusedGrArgs {}; 94a [t ].R = dR + (size_t )t * D ; 95a [t ].R_out = dR + (size_t )t * D ; 96a [t ].apply = true; 97a [t ].bo_prev = dbo + (size_t )t * N ; 98a [t ].inj_prev = dinj + (size_t )t * HC ; 99a [t ].w_norm = dwn ; 100a [t ].w_down = dwd ; 101a [t ].w_up = dwu ; 102a [t ].w_inject = dwi ; 103a [t ].eps = 1e-6f ; 104a [t ].lo = dlo + (size_t )t * LR ; 105a [t ].rs = drs + (size_t )t * HC ; 106a [t ].inject_out = dio + (size_t )t * HC ; 107a [t ].mixed = dmix + (size_t )t * N ; 108 } 109// 0: the default read, 1 and 2: the persistent read twice; each output is mixed | inject | R 110 std::vector < float > out [3 ]; 111bool used = true; 112for (int w = 0 ;w < 3 ;++ w ) { 113ck (cudaMemcpy (dR ,R .data (),R .size ()* 4 ,cudaMemcpyHostToDevice ),"R" ); 114ck (cudaMemset (dmix ,0xff , (size_t )TM * N * 4 ),"mix" ); 115ck (cudaMemset (dio ,0xff , (size_t )TM * HC * 4 ),"io" ); 116ck (cudaDeviceSynchronize (),"sync" ); 117if (w == 0 ) k::fused_gr_read_multi (a ,T ,dxn ,nullptr ); 118else used = k::fused_gr_read_persist (a ,T ,nullptr )&& used ; 119ck (cudaDeviceSynchronize (),"read" ); 120out [w ].resize ((size_t )T * (N + HC + D )); 121ck (cudaMemcpy (out [w ].data (),dmix , (size_t )T * N * 4 ,cudaMemcpyDeviceToHost ),"d2h mix" ); 122ck (cudaMemcpy (out [w ].data ()+ (size_t )T * N ,dio , (size_t )T * HC * 4 ,cudaMemcpyDeviceToHost ),"d2h io" ); 123ck (cudaMemcpy (out [w ].data ()+ (size_t )T * (N + HC ),dR , (size_t )T * D * 4 ,cudaMemcpyDeviceToHost ),"d2h R" ); 124if (w == 1 && !used )break ; 125 } 126if (off ) { 127if (used ) { 128 std::printf ("T=%d: the persistent read ran with STRATA_HC_PERSIST unset FAIL\n" ,T ); 129++ bad ; 130 } 131continue ; 132 } 133if (!used ) { 134 std::printf ("T=%d: the persistent read did not run on this card (skip)\n" ,T ); 135continue ; 136 } 137++ ran ; 138double mref = 0.0 ,md = 0.0 ; 139bool finite = true; 140for (size_t i = 0 ;i < out [0 ].size ();++ i ) { 141finite = finite && std::isfinite (out [1 ][i ]); 142mref = std::fmax (mref , std::fabs ((double )out [0 ][i ])); 143md = std::fmax (md , std::fabs ((double )out [1 ][i ]- out [0 ][i ])); 144 } 145const double rel = md / (mref > 0 ?mref :1.0 ); 146const bool same = std::memcmp (out [1 ].data (),out [2 ].data (),out [1 ].size ()* 4 )== 0 ; 147const bool ok = finite && rel < 1e-4 && same ; 148 std::printf ("T=%d: max relative difference %.3g against the default read, %s, two runs %s %s\n" ,T ,rel , 149finite ?"finite" :"NON-FINITE" ,same ?"bitwise equal" :"DIFFER" ,ok ?"ok" :"FAIL" ); 150if (!ok )++ bad ; 151 } 152if (off ) std::printf ("%s: STRATA_HC_PERSIST unset, the persistent read never ran\n" ,bad ?"FAIL" :"PASS" ); 153else if (ran == 0 ) std::printf ("SKIP: the persistent read is not used on this card\n" ); 154else std::printf ("%s: %d token counts checked\n" ,bad ?"FAIL" :"PASS" ,ran ); 155return bad ?1 :0 ; 156}