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