okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
6abee7f39
main
1// src/kernels/cvec_parity.cpp - the control vector kernel (strata/kernels/cvec.hpp) against a host reference. 2// 1. project: h - s (h.v) v per stream and token, against double precision; the steered component is gone. 3// 2. add: h + d. 4// 3. switched off: R unchanged without a pending write; with one, R is BITWISE the write the fused read folds 5// (fused_gr_read with apply), which is what keeps a loaded-but-off vector identical to the stock engine. 6// 4. a layer without a direction is untouched. 7 8#include "strata/kernels/cvec.hpp" 9#include "strata/kernels/fused_gr.hpp" 10 11#include <cuda_runtime.h> 12 13#include <cmath> 14#include <cstdio> 15#include <cstdlib> 16#include <cstring> 17#include <random> 18#include <vector> 19 20namespace k= strata::kernels; 21 22namespace { 23 24int g_fail = 0 ; 25 26void ck (cudaError_t e ,const char * w ) { 27if (e != cudaSuccess ) { 28 std::fprintf (stderr ,"CUDA error in %s: %s\n" ,w ,cudaGetErrorString (e )); 29 std::exit (2 ); 30 } 31} 32 33template < typename T > 34T * dalloc (size_t n ) { 35T * p = nullptr ; 36ck (cudaMalloc (& p ,n * sizeof (T )),"malloc" ); 37return p ; 38} 39 40template < typename T > 41void up (T * d ,const std::vector < T >& h ) {ck (cudaMemcpy (d ,h .data (),h .size ()* sizeof (T ),cudaMemcpyHostToDevice ),"up" ); } 42template < typename T > 43std::vector < T > down (const T * d ,size_t n ) { 44 std::vector < T > h (n ); 45ck (cudaMemcpy (h .data (),d ,n * sizeof (T ),cudaMemcpyDeviceToHost ),"down" ); 46return h ; 47} 48 49void check (bool ok ,const char * what ) { 50 std::printf (" %-66s %s\n" ,what ,ok ?"ok" :"FAIL" ); 51if (!ok )++ g_fail ; 52} 53 54}// namespace 55 56int main () { 57constexpr int64_t L = 48 ,N = 2560 ,HC = 4 ,LR = 320 ,T = 5 ,D = HC * N ; 58constexpr int64_t kLayer = 7 ,kOff = 3 ;// steered / not steered 59 std::mt19937 rng (1234 ); 60 std::normal_distribution < float > nd (0.0f ,1.0f ); 61 62// a unit direction at kLayer with a norm-2 scaled one's s = 2; nothing at kOff 63 std::vector < float > dir ((size_t ) (L * N ),0.0f ),s ((size_t )L ,0.0f ); 64 { 65double nrm = 0.0 ; 66 std::vector < float > v ((size_t )N ); 67for (auto & x :v ) {x = nd (rng );nrm += (double )x * x ; } 68nrm = std::sqrt (nrm ); 69for (int64_t j = 0 ;j < N ;++ j )dir [(size_t ) (kLayer * N + j )]= (float ) (v [(size_t )j ] /nrm ); 70s [(size_t )kLayer ]= 2.0f ; 71 } 72 std::string err ; 73if (!k::cvec_upload (dir ,s ,/*project*/ 0 ,4 ,44 ,N ,HC ,err )) { std::fprintf (stderr ,"%s\n" ,err .c_str ());return 2 ; } 74check (k::cvec ().covers (kLayer )&& !k::cvec ().covers (kOff )&& !k::cvec ().covers (45 ),"covers(): steered layers only" ); 75 76 std::vector < float > R ((size_t ) (T * D )),bo ((size_t ) (T * N )),inj ((size_t ) (T * HC )); 77for (auto & x :R )x = nd (rng )* 3.0f ; 78for (auto & x :bo )x = nd (rng ); 79for (auto & x :inj )x = nd (rng ); 80float * dR = dalloc < float > (R .size ()); 81float * dbo = dalloc < float > (bo .size ()); 82float * dinj = dalloc < float > (inj .size ()); 83up (dbo ,bo ); 84up (dinj ,inj ); 85 86// ---- 1. project, no pending write 87 std::printf ("project\n" ); 88up (dR ,R ); 89 k::cvec_apply (dR ,kLayer ,T ,D ,nullptr ,0 ,nullptr ,0 , false,nullptr ); 90ck (cudaDeviceSynchronize (),"project" ); 91 { 92const auto got = down (dR ,R .size ()); 93double worst = 0.0 ,worst_dot = 0.0 ; 94for (int64_t t = 0 ;t < T ;++ t ) 95for (int64_t c = 0 ;c < HC ;++ c ) { 96const float * h = R .data ()+ t * D + c * N ; 97const float * v = dir .data ()+ kLayer * N ; 98double dot = 0.0 ; 99for (int64_t j = 0 ;j < N ;++ j )dot += (double )h [j ]* v [j ]; 100double after = 0.0 ; 101for (int64_t j = 0 ;j < N ;++ j ) { 102const double want = h [j ]- 2.0 * dot * v [j ]; 103const double g = got [(size_t ) (t * D + c * N + j )]; 104worst = std::fmax (worst , std::fabs (g - want )); 105after += g * v [j ]; 106 } 107// s = 2 reflects the component: h.v -> -h.v 108worst_dot = std::fmax (worst_dot , std::fabs (after + dot )); 109 } 110 std::printf (" max |err| %.3g, max |h'.v + h.v| %.3g\n" ,worst ,worst_dot ); 111check (worst < 1e-4 ,"project matches h - s (h.v) v (s = 2)" ); 112check (worst_dot < 1e-3 ,"the component along v is reflected (s = 2)" ); 113 } 114// s = 1 removes it 115s [(size_t )kLayer ]= 1.0f ; 116if (!k::cvec_upload (dir ,s ,0 ,4 ,44 ,N ,HC ,err ))return 2 ; 117up (dR ,R ); 118 k::cvec_apply (dR ,kLayer ,T ,D ,nullptr ,0 ,nullptr ,0 , false,nullptr ); 119ck (cudaDeviceSynchronize (),"project s=1" ); 120 { 121const auto got = down (dR ,R .size ()); 122double worst = 0.0 ; 123for (int64_t t = 0 ;t < T ;++ t ) 124for (int64_t c = 0 ;c < HC ;++ c ) { 125double after = 0.0 ; 126for (int64_t j = 0 ;j < N ;++ j )after += (double )got [(size_t ) (t * D + c * N + j )]* dir [(size_t ) (kLayer * N + j )]; 127worst = std::fmax (worst , std::fabs (after )); 128 } 129 std::printf (" max |h'.v| %.3g\n" ,worst ); 130check (worst < 1e-3 ,"s = 1 projects the direction out" ); 131 } 132 133// ---- 4. a layer without a direction 134up (dR ,R ); 135 k::cvec_apply (dR ,kOff ,T ,D ,nullptr ,0 ,nullptr ,0 , false,nullptr ); 136ck (cudaDeviceSynchronize (),"off layer" ); 137check (std::memcmp (down (dR ,R .size ()).data (),R .data (),R .size ()* 4 )== 0 ,"a layer without a direction is untouched" ); 138 139// ---- 3. switched off 140 std::printf ("switched off\n" ); 141 k::cvec_set_enabled (false); 142check (!k::cvec_enabled (),"cvec_enabled() follows the switch" ); 143up (dR ,R ); 144 k::cvec_apply (dR ,kLayer ,T ,D ,nullptr ,0 ,nullptr ,0 , false,nullptr ); 145ck (cudaDeviceSynchronize (),"off" ); 146check (std::memcmp (down (dR ,R .size ()).data (),R .data (),R .size ()* 4 )== 0 ,"off, no pending write: R unchanged" ); 147 { 148// the fused read's folded write, one token at a time, against the kernel's write-only pass 149 std::vector < uint16_t > wd ((size_t ) (LR * D )),wu ((size_t ) (D * LR )),wi ((size_t ) (HC * D )); 150 std::vector < float > wn ((size_t )D ); 151auto bf = [& ](float x ) {uint32_t u ; std::memcpy (& u ,& x ,4 );return (uint16_t ) (u >>16 ); }; 152for (auto & x :wd )x = bf (nd (rng )* 0.02f ); 153for (auto & x :wu )x = bf (nd (rng )* 0.02f ); 154for (auto & x :wi )x = bf (nd (rng )* 0.02f ); 155for (auto & x :wn )x = 1.0f + 0.1f * nd (rng ); 156uint16_t * dwd = dalloc < uint16_t > (wd .size ()); 157uint16_t * dwu = dalloc < uint16_t > (wu .size ()); 158uint16_t * dwi = dalloc < uint16_t > (wi .size ()); 159float * dwn = dalloc < float > (wn .size ()); 160up (dwd ,wd );up (dwu ,wu );up (dwi ,wi );up (dwn ,wn ); 161float * lo = dalloc < float > (LR ); 162float * rs = dalloc < float > (HC ); 163float * inj_out = dalloc < float > (HC ); 164float * mixed = dalloc < float > (N ); 165float * dF = dalloc < float > (R .size ()); 166up (dF ,R ); 167for (int64_t t = 0 ;t < T ;++ t ) { 168 k::FusedGrArgs a ; 169a .R = dF + t * D ;a .R_out = dF + t * D ;a .apply = true; 170a .bo_prev = dbo + t * N ;a .inj_prev = dinj + t * HC ; 171a .w_norm = dwn ;a .w_down = dwd ;a .w_up = dwu ;a .w_inject = dwi ; 172a .lo = lo ;a .rs = rs ;a .inject_out = inj_out ;a .mixed = mixed ; 173 k::fused_gr_read (a ,nullptr ); 174 } 175up (dR ,R ); 176 k::cvec_apply (dR ,kLayer ,T ,D ,dbo ,N ,dinj ,HC , true,nullptr ); 177ck (cudaDeviceSynchronize (),"write only" ); 178const auto fused = down (dF ,R .size ()),mine = down (dR ,R .size ()); 179check (std::memcmp (fused .data (),mine .data (),R .size ()* 4 )== 0 , 180"off, pending write: bitwise the fused read's folded write" ); 181 k::cvec_set_enabled (true); 182// on, with the write: the write, then the projection (reference from the fused result) 183up (dR ,R ); 184 k::cvec_apply (dR ,kLayer ,T ,D ,dbo ,N ,dinj ,HC , true,nullptr ); 185ck (cudaDeviceSynchronize (),"write+project" ); 186const auto both = down (dR ,R .size ()); 187double worst = 0.0 ; 188for (int64_t t = 0 ;t < T ;++ t ) 189for (int64_t c = 0 ;c < HC ;++ c ) { 190const float * h = fused .data ()+ t * D + c * N ; 191const float * v = dir .data ()+ kLayer * N ; 192double dot = 0.0 ; 193for (int64_t j = 0 ;j < N ;++ j )dot += (double )h [j ]* v [j ]; 194for (int64_t j = 0 ;j < N ;++ j ) 195worst = std::fmax (worst , std::fabs (both [(size_t ) (t * D + c * N + j )]- (h [j ]- dot * v [j ]))); 196 } 197 std::printf (" write + project: max |err| %.3g\n" ,worst ); 198check (worst < 1e-4 ,"on, pending write: the write, then the projection" ); 199 } 200 201// ---- 2. add 202 std::printf ("add\n" ); 203for (int64_t j = 0 ;j < N ;++ j )dir [(size_t ) (kLayer * N + j )] *=0.1f ;// d = 0.1 v, s = 1 204s [(size_t )kLayer ]= 1.0f ; 205if (!k::cvec_upload (dir ,s ,/*add*/ 1 ,4 ,44 ,N ,HC ,err ))return 2 ; 206up (dR ,R ); 207 k::cvec_apply (dR ,kLayer ,T ,D ,nullptr ,0 ,nullptr ,0 , false,nullptr ); 208ck (cudaDeviceSynchronize (),"add" ); 209 { 210const auto got = down (dR ,R .size ()); 211bool same = true; 212for (int64_t t = 0 ;t < T ;++ t ) 213for (int64_t c = 0 ;c < HC ;++ c ) 214for (int64_t j = 0 ;j < N ;++ j ) { 215const size_t i = (size_t ) (t * D + c * N + j ); 216same = same && got [i ]== R [i ]+ dir [(size_t ) (kLayer * N + j )]; 217 } 218check (same ,"add is h + d in every stream (bitwise)" ); 219 } 220 221 std::printf (g_fail ?"cvec_parity: %d FAILED\n" :"cvec_parity: all passed\n" ,g_fail ); 222return g_fail ?1 :0 ; 223}