okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
62db0bf10
main
1// src/kernels/gr_parity.cpp - P2.S2's test for the gated residual / hyper-connection. 2// 3// `ref/gr.py` opens by listing the details "a prose reading gets wrong", and every one of them is a reading 4// that has the RIGHT SHAPES and produces a PLAUSIBLE number. So each is computed here the wrong way round and 5// required to differ materially BEFORE the kernel is judged against the right one. A test that only does the 6// second half passes against either reading: 7// 8// 1. PER-STREAM RMSNorm. The source comment reads ambiguously; the code reduces over ne[0] = n_embd, i.e. 9// one RMS per stream. The rival reading is one RMS over the whole hc*n_embd stack. 10// 2. `/ hc` INSIDE the silu: `lo = silu(proj / hc)`. Moving it outside is a one-line change that keeps 11// every shape. 12// 3. SiLU on `lo` and SIGMOID on the gate, a few lines apart in the same function. Swapping them is the 13// obvious slip, and both are monotone saturating functions so the magnitudes stay comparable. 14// 4. `mean` over the stream axis, not `sum`. A factor of hc = 4, which is exactly the kind of error that 15// looks like a scale problem rather than a structural one. 16// 5. ACTIVATION PRECISION. The historical BF16-rounded CPU contract and the pinned CUDA single-token 17// FP32-activation contract must be distinguishable. Both are tested against explicit references; 18// neither is selected merely because the weight is BF16. The scalar fixture isolates xn and lo 19// rounding from reduction order, and graph replay must preserve the precision chosen at capture. 20// 6. `gr_write`'s `2*sigmoid`, which centres the gate on 1 so a ZERO injection is a plain residual add. 21// Asserted as a property, not as a value, because that is what the source comment claims. 22#include "strata/kernels/gr.hpp" 23#include "strata/kernels/fused_gr.hpp" 24#include "strata/kernels/native_mmvq.hpp" 25 26#include <cuda_runtime.h> 27 28#include <cmath> 29#include <cstdio> 30#include <algorithm> 31#include <cstdlib> 32#include <cstring> 33#include <random> 34#include <string> 35#include <vector> 36 37namespace { 38 39void check (cudaError_t e ,const char * what ) { 40if (e != cudaSuccess ) { 41 std::fprintf (stderr ,"%s: %s\n" ,what ,cudaGetErrorString (e )); 42 std::exit (1 ); 43 } 44} 45 46/// The same rule the kernel uses, on the host - `ref/quant.py::bf16`. 47float to_bf16 (float f ) { 48uint32_t i ; 49 std::memcpy (& i ,& f ,4 ); 50i = (i + ((i >>16 )& 1u )+ 0x7FFFu )& 0xFFFF0000u ; 51float o ; 52 std::memcpy (& o ,& i ,4 ); 53return o ; 54} 55 56uint16_t bf16_bits (float f ) { 57uint32_t i ; 58 std::memcpy (& i ,& f ,4 ); 59return (uint16_t ) (i >>16 ); 60} 61 62struct Opts { 63bool per_stream_norm = true;///< false: one RMS over the whole stack (WRONG) 64bool scale_inside_silu = true;///< false: silu(proj)/hc instead of silu(proj/hc) (WRONG) 65bool sigmoid_on_gate = true;///< false: silu on the gate and sigmoid on lo (WRONG) 66bool mean_over_streams = true;///< false: sum over streams instead of mean (WRONG) 67bool round_activation = true;///< false: pinned CUDA single-token BF16 MMVF contract 68}; 69 70/// `ref/gr.py::gr_read`, transcribed with switches for the rival readings. 71void reference (const std::vector < float >& R ,const std::vector < float >& w_norm , 72const std::vector < float >& w_down ,const std::vector < float >& w_up , 73const std::vector < float >& w_inject ,float eps ,long long n_embd ,long long hc ,long long hc_lr , 74const Opts & o , std::vector < float >& mixed , std::vector < float >& inject ) { 75const long long hc_dim = hc * n_embd ; 76 std::vector < float > xn ((size_t )hc_dim ); 77if (o .per_stream_norm ) { 78for (long long c = 0 ;c < hc ;++ c ) { 79double ms = 0 ; 80for (long long d = 0 ;d < n_embd ;++ d )ms += (double )R [(size_t ) (c * n_embd + d )]* R [(size_t ) (c * n_embd + d )]; 81ms /= (double )n_embd ; 82const float rs = (float ) (1.0 / std::sqrt (ms + (double )eps )); 83for (long long d = 0 ;d < n_embd ;++ d ) { 84const size_t i = (size_t ) (c * n_embd + d ); 85xn [i ]= R [i ]* rs * w_norm [i ]; 86 } 87 } 88 }else { 89double ms = 0 ; 90for (long long i = 0 ;i < hc_dim ;++ i )ms += (double )R [(size_t )i ]* R [(size_t )i ]; 91ms /= (double )hc_dim ; 92const float rs = (float ) (1.0 / std::sqrt (ms + (double )eps )); 93for (long long i = 0 ;i < hc_dim ;++ i )xn [(size_t )i ]= R [(size_t )i ]* rs * w_norm [(size_t )i ]; 94 } 95 96 std::vector < float > act ((size_t )hc_dim ); 97for (long long i = 0 ;i < hc_dim ;++ i )act [(size_t )i ]= o .round_activation ?to_bf16 (xn [(size_t )i ]) :xn [(size_t )i ]; 98 99// lo 100 std::vector < float > lo ((size_t )hc_lr ); 101for (long long k = 0 ;k < hc_lr ;++ k ) { 102double a = 0 ; 103for (long long i = 0 ;i < hc_dim ;++ i )a += (double )act [(size_t )i ]* (double )w_down [(size_t ) (k * hc_dim + i )]; 104const float p = (float )a ; 105if (o .sigmoid_on_gate )lo [(size_t )k ]= o .scale_inside_silu ? (p / (float )hc ) / (1.0f + std::exp (- (p / (float )hc ))) 106 : (p / (1.0f + std::exp (- p ))) / (float )hc ; 107else lo [(size_t )k ]= o .scale_inside_silu ?1.0f / (1.0f + std::exp (- (p / (float )hc ))) 108 : (1.0f / (1.0f + std::exp (- p ))) / (float )hc ; 109 } 110 std::vector < float > lq ((size_t )hc_lr ); 111for (long long k = 0 ;k < hc_lr ;++ k ) 112lq [(size_t )k ]= o .round_activation ?to_bf16 (lo [(size_t )k ]) :lo [(size_t )k ]; 113 114// gate and the gated mean 115mixed .assign ((size_t )n_embd ,0.0f ); 116for (long long d = 0 ;d < n_embd ;++ d ) { 117float m = 0.0f ; 118for (long long c = 0 ;c < hc ;++ c ) { 119const long long i = c * n_embd + d ; 120double a = 0 ; 121for (long long k = 0 ;k < hc_lr ;++ k )a += (double )lq [(size_t )k ]* (double )w_up [(size_t ) (i * hc_lr + k )]; 122const float g = (float )a ; 123const float s = o .sigmoid_on_gate ?1.0f / (1.0f + std::exp (- g )) :g / (1.0f + std::exp (- g )); 124m += xn [(size_t )i ]* s ; 125 } 126mixed [(size_t )d ]= o .mean_over_streams ?m / (float )hc :m ; 127 } 128 129inject .assign ((size_t )hc ,0.0f ); 130for (long long c = 0 ;c < hc ;++ c ) { 131double a = 0 ; 132for (long long i = 0 ;i < hc_dim ;++ i )a += (double )act [(size_t )i ]* (double )w_inject [(size_t ) (c * hc_dim + i )]; 133inject [(size_t )c ]= (float )a ; 134 } 135} 136 137/// Normalised L1 difference. `mag_out` returns the mean |a|, for reporting. 138double rel_diff (const std::vector < float >& a ,const std::vector < float >& b ,double * mag_out = nullptr ) { 139double d = 0 ,mag = 0 ; 140for (size_t i = 0 ;i < a .size ();++ i ) { 141d += std::fabs ((double )a [i ]- (double )b [i ]); 142mag += std::fabs ((double )a [i ]); 143 } 144if (mag_out )* mag_out = mag / (double ) (a .empty () ?1 :a .size ()); 145return d / (mag > 1e-30 ?mag :1e-30 ); 146} 147 148/// Relative error against the magnitude of the TERMS, not of the result. 149/// 150/// `R + block_out * w` cancels wherever block_out*w is close to -R, and a plain |want-got|/|want| then 151/// reports the CONDITION NUMBER instead of the arithmetic. This is the same metric mistake the project has 152/// now made four times: rounds 169 (Q4_K), 189 (RoPE), 194 (shared_expert) and this one. For a sum, the 153/// denominator is the sum of the term magnitudes. 154double rel_terms (double want ,double got ,double term_a ,double term_b ) { 155const double den = std::fabs (term_a )+ std::fabs (term_b ); 156return std::fabs (want - got ) / (den > 1e-30 ?den :1e-30 ); 157} 158 159const char * activation_mode_name (int mode ) { 160return mode == 2 ?"native pinned GR" : (mode == 1 ?"FP32" :"BF16" ); 161} 162void select_activation_mode (int mode ) { 163 strata::kernels::gr_set_native_mmvf (mode == 2 ); 164// In mode 2 the precision-only switch is deliberately false: native MMVF must imply FP32 by itself. 165 strata::kernels::gr_set_fp32_activations (mode == 1 ); 166} 167 168// A diagonal GR fixture makes activation precision observable without summation-order ambiguity. 169// Two-wide matrices satisfy native MMVF's pair ABI, while each nonzero dot has only one nonzero product. 170// At R=1 and eps=0, xn=gamma exactly; any rounding in xn or lo comes from the selected activation contract. 171int scalar_activation_contract () { 172using namespace strata::kernels ; 173const GrShapes sh {2 ,1 ,2 }; 174float * d_R = nullptr ,* d_norm = nullptr ,* d_mixed = nullptr ,* d_inject = nullptr ; 175uint16_t * d_weights = nullptr ; 176void * d_scratch = nullptr ; 177check (cudaMalloc (& d_R ,2 * sizeof (float )),"scalar R" ); 178check (cudaMalloc (& d_norm ,2 * sizeof (float )),"scalar norm" ); 179check (cudaMalloc (& d_mixed ,2 * sizeof (float )),"scalar mixed" ); 180check (cudaMalloc (& d_inject ,sizeof (float )),"scalar inject" ); 181check (cudaMalloc (& d_weights ,10 * sizeof (uint16_t )),"scalar weights" ); 182check (cudaMalloc (& d_scratch ,gr_workspace_bytes (sh )),"scalar scratch" ); 183GrWorkspace ws ; 184gr_workspace_init (sh ,d_scratch ,ws ); 185const float ones []= {1.0f ,1.0f }; 186const uint16_t weights []= { 187bf16_bits (0.5f ),0 ,0 ,0 ,// down: lo[0]=silu(xn[0]/2), lo[1]=0 188bf16_bits (1.5f ),0 ,bf16_bits (1.5f ),0 ,// both gate rows consume lo[0] 189bf16_bits (2.0f ),0 // injection: 2*xn[0] 190 }; 191check (cudaMemcpy (d_R ,ones ,sizeof (ones ),cudaMemcpyHostToDevice ),"scalar upload R" ); 192check (cudaMemcpy (d_weights ,weights ,sizeof (weights ),cudaMemcpyHostToDevice ),"scalar upload weights" ); 193// the uploads run on the legacy stream from pageable memory, which the non-blocking `stream` does not wait for 194check (cudaDeviceSynchronize (),"scalar setup" ); 195cudaStream_t stream ; 196check (cudaStreamCreateWithFlags (& stream ,cudaStreamNonBlocking ),"scalar stream" ); 197int bad = 0 ; 198for (float gamma : {1.0f ,1.00390625f }) { 199const float gammas []= {gamma ,gamma }; 200check (cudaMemcpy (d_norm ,gammas ,sizeof (gammas ),cudaMemcpyHostToDevice ),"scalar upload norm" ); 201check (cudaDeviceSynchronize (),"scalar norm sync" ); 202float mixed_by_mode [3 ][2 ]= {},inject_by_mode [3 ]= {}; 203for (int mode = 0 ;mode < 3 ;++ mode ) { 204select_activation_mode (mode ); 205gr_read (d_R ,d_norm ,d_weights ,d_weights + 4 ,d_weights + 8 ,0.0f , 206sh ,ws ,d_mixed ,d_inject ,stream ); 207check (cudaStreamSynchronize (stream ),"scalar warmup" ); 208cudaGraph_t graph ; 209cudaGraphExec_t executable ; 210check (cudaStreamBeginCapture (stream ,cudaStreamCaptureModeThreadLocal ),"scalar capture begin" ); 211gr_read (d_R ,d_norm ,d_weights ,d_weights + 4 ,d_weights + 8 ,0.0f , 212sh ,ws ,d_mixed ,d_inject ,stream ); 213check (cudaStreamEndCapture (stream ,& graph ),"scalar capture end" ); 214check (cudaGraphInstantiate (& executable ,graph ,nullptr ,nullptr ,0 ),"scalar instantiate" ); 215// Changing both host options after capture cannot change the graph's selected kernels. 216select_activation_mode ((mode + 1 ) %3 ); 217// Warmup results must not make a missing/no-op captured launch pass. 218// Poison the outputs and intermediate workspace before replay. 219check (cudaMemsetAsync (d_mixed ,0xa5 ,2 * sizeof (float ),stream ),"scalar poison mixed" ); 220check (cudaMemsetAsync (d_inject ,0xa5 ,sizeof (float ),stream ),"scalar poison inject" ); 221check (cudaMemsetAsync (d_scratch ,0xa5 ,ws .bytes ,stream ),"scalar poison workspace" ); 222check (cudaGraphLaunch (executable ,stream ),"scalar replay" ); 223check (cudaStreamSynchronize (stream ),"scalar replay sync" ); 224check (cudaMemcpy (mixed_by_mode [mode ],d_mixed ,2 * sizeof (float ),cudaMemcpyDeviceToHost ),"scalar mixed copy" ); 225check (cudaMemcpy (& inject_by_mode [mode ],d_inject ,sizeof (float ),cudaMemcpyDeviceToHost ),"scalar inject copy" ); 226const float activation = mode ?gamma :to_bf16 (gamma ); 227const float projection = activation * 0.5f ; 228const float lo = projection / (1.0f + std::exp (- projection )); 229const float gate = (mode ?lo :to_bf16 (lo ))* 1.5f ; 230const float expected_mixed = gamma / (1.0f + std::exp (- gate )); 231const float expected_inject = activation * 2.0f ; 232// Each mode has its own activation contract and bounded scalar-formula 233// check. Native fast exp/div need not match ordinary FP32 postops bit 234// for bit: native_gr_postops_parity establishes that arithmetic with 235// actual pinned CUDA graphs. The nonzero projection/injection here 236// has only one exactly representable product and remains exact. 237const bool ok = std::fabs (mixed_by_mode [mode ][0 ]- expected_mixed ) <=2e-7f && 238mixed_by_mode [mode ][0 ]== mixed_by_mode [mode ][1 ]&& 239inject_by_mode [mode ]== expected_inject ; 240 std::printf (" scalar gamma %.8f %s activation/capture %s (mixed %.9f, ref %.9f)\n" , 241gamma ,activation_mode_name (mode ),ok ?"pass" :"FAIL" , 242mixed_by_mode [mode ][0 ],expected_mixed ); 243if (!ok )++ bad ; 244check (cudaGraphExecDestroy (executable ),"scalar graph exec destroy" ); 245check (cudaGraphDestroy (graph ),"scalar graph destroy" ); 246 247const float sentinel = -73.25f ; 248check (cudaMemcpy (d_inject ,& sentinel ,sizeof (float ),cudaMemcpyHostToDevice ),"scalar sentinel" ); 249check (cudaDeviceSynchronize (),"scalar sentinel sync" );// a late sentinel would hide a write to d_inject 250select_activation_mode (mode ); 251gr_read (d_R ,d_norm ,d_weights ,d_weights + 4 ,nullptr ,0.0f , 252sh ,ws ,d_mixed ,d_inject ,stream ); 253check (cudaStreamSynchronize (stream ),"scalar final mixer sync" ); 254float final_mixed [2 ]= {},untouched = 0.0f ; 255check (cudaMemcpy (final_mixed ,d_mixed ,sizeof (final_mixed ),cudaMemcpyDeviceToHost ),"scalar final mixed" ); 256check (cudaMemcpy (& untouched ,d_inject ,sizeof (float ),cudaMemcpyDeviceToHost ),"scalar sentinel copy" ); 257const bool final_ok = std::memcmp (final_mixed ,mixed_by_mode [mode ],sizeof (final_mixed ))== 0 && untouched == sentinel ; 258 std::printf (" scalar final mixer %s %s\n" ,activation_mode_name (mode ),final_ok ?"pass" :"FAIL" ); 259if (!final_ok )++ bad ; 260 } 261if (std::fabs (mixed_by_mode [1 ][0 ]- mixed_by_mode [0 ][0 ]) <=1e-5f || 262 (gamma != 1.0f && inject_by_mode [0 ]== inject_by_mode [1 ])) { 263 std::printf (" scalar precision fixture is not observable\n" ); 264++ bad ; 265 } 266if (inject_by_mode [1 ]!= inject_by_mode [2 ]) { 267 std::printf (" native MMVF changed the exact scalar FP32 injection projection\n" ); 268++ bad ; 269 } 270 } 271select_activation_mode (0 ); 272check (cudaStreamDestroy (stream ),"scalar stream destroy" ); 273cudaFree (d_R );cudaFree (d_norm );cudaFree (d_mixed );cudaFree (d_inject ); 274cudaFree (d_weights );cudaFree (d_scratch ); 275return bad ; 276} 277 278int fused_multi_lds_parity (const float * d_norm ,const uint16_t * d_down ,const uint16_t * d_up , 279const uint16_t * d_inject ,float eps ) { 280using namespace strata::kernels ; 281constexpr int N = 2560 ,HC = 4 ,LR = 320 ,D = N * HC ,T = kFusedGrMaxT ; 282 std::mt19937 rng (0x6f8a ); 283 std::normal_distribution < float > normal (0.0f ,0.3f ); 284 std::vector < float > r ((size_t )T * D ),bo ((size_t )T * N ),inj ((size_t )T * HC ); 285for (auto & x :r )x = normal (rng ); 286for (auto & x :bo )x = normal (rng ); 287for (auto & x :inj )x = normal (rng ); 288 289float * d_r = nullptr ,* d_r_out = nullptr ,* d_bo = nullptr ,* d_inj = nullptr ; 290float * d_lo = nullptr ,* d_rs = nullptr ,* d_inj_out = nullptr ,* d_mixed = nullptr ,* d_xn = nullptr ; 291check (cudaMalloc (& d_r ,r .size ()* sizeof (float )),"multi R" ); 292check (cudaMalloc (& d_r_out ,r .size ()* sizeof (float )),"multi R_out" ); 293check (cudaMalloc (& d_bo ,bo .size ()* sizeof (float )),"multi bo" ); 294check (cudaMalloc (& d_inj ,inj .size ()* sizeof (float )),"multi inj" ); 295check (cudaMalloc (& d_lo , (size_t )T * LR * sizeof (float )),"multi lo" ); 296check (cudaMalloc (& d_rs , (size_t )T * HC * sizeof (float )),"multi rs" ); 297check (cudaMalloc (& d_inj_out , (size_t )T * HC * sizeof (float )),"multi injection" ); 298check (cudaMalloc (& d_mixed , (size_t )T * N * sizeof (float )),"multi mixed" ); 299check (cudaMalloc (& d_xn , (size_t )T * D * sizeof (float )),"multi xn" ); 300check (cudaMemcpy (d_r ,r .data (),r .size ()* sizeof (float ),cudaMemcpyHostToDevice ),"multi copy R" ); 301check (cudaMemcpy (d_bo ,bo .data (),bo .size ()* sizeof (float ),cudaMemcpyHostToDevice ),"multi copy bo" ); 302check (cudaMemcpy (d_inj ,inj .data (),inj .size ()* sizeof (float ),cudaMemcpyHostToDevice ),"multi copy inj" ); 303 304 std::vector < FusedGrArgs > args (T ); 305for (int t = 0 ;t < T ;++ t ) { 306auto & a = args [t ]; 307a .R = d_r + (size_t )t * D ; 308a .R_out = d_r_out + (size_t )t * D ; 309a .apply = true; 310a .bo_prev = d_bo + (size_t )t * N ; 311a .inj_prev = d_inj + (size_t )t * HC ; 312a .w_norm = d_norm ; 313a .w_down = d_down ; 314a .w_up = d_up ; 315a .w_inject = d_inject ; 316a .eps = eps ; 317a .lo = d_lo + (size_t )t * LR ; 318a .rs = d_rs + (size_t )t * HC ; 319a .inject_out = d_inj_out + (size_t )t * HC ; 320a .mixed = d_mixed + (size_t )t * N ; 321 } 322 323struct Snapshot { 324 std::vector < float > r_out ,lo ,rs ,inject ,mixed ; 325 }; 326auto snapshot = [& ]() { 327Snapshot s ; 328s .r_out .resize ((size_t )T * D ); 329s .lo .resize ((size_t )T * LR ); 330s .rs .resize ((size_t )T * HC ); 331s .inject .resize ((size_t )T * HC ); 332s .mixed .resize ((size_t )T * N ); 333check (cudaMemcpy (s .r_out .data (),d_r_out ,s .r_out .size ()* sizeof (float ),cudaMemcpyDeviceToHost ),"multi read R_out" ); 334check (cudaMemcpy (s .lo .data (),d_lo ,s .lo .size ()* sizeof (float ),cudaMemcpyDeviceToHost ),"multi read lo" ); 335check (cudaMemcpy (s .rs .data (),d_rs ,s .rs .size ()* sizeof (float ),cudaMemcpyDeviceToHost ),"multi read rs" ); 336check (cudaMemcpy (s .inject .data (),d_inj_out ,s .inject .size ()* sizeof (float ),cudaMemcpyDeviceToHost ),"multi read inject" ); 337check (cudaMemcpy (s .mixed .data (),d_mixed ,s .mixed .size ()* sizeof (float ),cudaMemcpyDeviceToHost ),"multi read mixed" ); 338return s ; 339 }; 340// the split read (STRATA_GR_V3=1) sums in another order than the single-token kernel: equal within float 341// rounding, not to the bit, so it is compared with a relative tolerance; the default kernels bit for bit 342static const bool v3 = [] {const char * v = std::getenv ("STRATA_GR_V3" );return v != nullptr && std::atoi (v )!= 0 ; }(); 343auto close = [](const std::vector < float >& x ,const std::vector < float >& y ) { 344double worst = 0.0 ,mag = 1e-30 ; 345for (size_t i = 0 ;i < x .size ();++ i ) { 346worst = std::max (worst , (double ) std::fabs (x [i ]- y [i ])); 347mag = std::max (mag , (double ) std::fabs (y [i ])); 348 } 349if (worst > 2e-6 * mag ) 350 std::printf (" tolerance: worst %.3e of max |ref| %.3e (rel %.3e), n %zu\n" ,worst ,mag ,worst /mag ,x .size ()); 351return worst <=2e-6 * mag ; 352 }; 353auto same = [& ](const Snapshot & a ,const Snapshot & b ) { 354if (v3 )// `lo` is the default kernels' workspace between down and up; the split read keeps it in shared memory 355return close (a .r_out ,b .r_out )&& close (a .rs ,b .rs )&& close (a .inject ,b .inject )&& close (a .mixed ,b .mixed ); 356return std::memcmp (a .r_out .data (),b .r_out .data (),a .r_out .size ()* sizeof (float ))== 0 && 357 std::memcmp (a .lo .data (),b .lo .data (),a .lo .size ()* sizeof (float ))== 0 && 358 std::memcmp (a .rs .data (),b .rs .data (),a .rs .size ()* sizeof (float ))== 0 && 359 std::memcmp (a .inject .data (),b .inject .data (),a .inject .size ()* sizeof (float ))== 0 && 360 std::memcmp (a .mixed .data (),b .mixed .data (),a .mixed .size ()* sizeof (float ))== 0 ; 361 }; 362 363cudaStream_t stream = nullptr ; 364check (cudaStreamCreateWithFlags (& stream ,cudaStreamNonBlocking ),"multi stream" ); 365// the uploads above (and the caller's weights) run on the legacy stream, which the non-blocking `stream` does not wait for 366check (cudaDeviceSynchronize (),"multi setup" ); 367// Max T forces the HIP kernel's full dynamic-LDS request: 8 * 1280 * sizeof(float) = 40 KiB. 368fused_gr_read_multi (args .data (),T ,d_xn ,stream ); 369check (cudaStreamSynchronize (stream ),"multi max-T sync" ); 370const Snapshot multi = snapshot (); 371for (int t = 0 ;t < T ;++ t )fused_gr_read (args [t ],stream ); 372check (cudaStreamSynchronize (stream ),"single reference sync" ); 373const Snapshot single = snapshot (); 374int bad = 0 ; 375if (!same (multi ,single )) { 376 std::printf (" fused GR multi max-T differs from single-token calls\n" ); 377++ bad ; 378 } 379 380cudaGraph_t graph = nullptr ; 381cudaGraphExec_t graph_exec = nullptr ; 382check (cudaStreamBeginCapture (stream ,cudaStreamCaptureModeThreadLocal ),"multi graph begin" ); 383fused_gr_read_multi (args .data (),T ,d_xn ,stream ); 384check (cudaStreamEndCapture (stream ,& graph ),"multi graph end" ); 385check (cudaGraphInstantiate (& graph_exec ,graph ,nullptr ,nullptr ,0 ),"multi graph instantiate" ); 386check (cudaGraphLaunch (graph_exec ,stream ),"multi graph initial replay" ); 387check (cudaStreamSynchronize (stream ),"multi graph initial sync" ); 388const Snapshot captured = snapshot (); 389if (!same (multi ,captured )) { 390 std::printf (" fused GR multi captured graph differs from direct max-T call\n" ); 391++ bad ; 392 } 393 394// Reuse the same captured pointers with new payloads; then independently run the single-token path again. 395for (size_t i = 0 ;i < r .size ();++ i )r [i ]= -0.7f * r [i ]+ 0.001f * (float ) (i %17 ); 396for (size_t i = 0 ;i < bo .size ();++ i )bo [i ]= -0.4f * bo [i ]+ 0.02f ; 397for (size_t i = 0 ;i < inj .size ();++ i )inj [i ]+= 0.3f ; 398check (cudaMemcpyAsync (d_r ,r .data (),r .size ()* sizeof (float ),cudaMemcpyHostToDevice ,stream ),"multi replay R" ); 399check (cudaMemcpyAsync (d_bo ,bo .data (),bo .size ()* sizeof (float ),cudaMemcpyHostToDevice ,stream ),"multi replay bo" ); 400check (cudaMemcpyAsync (d_inj ,inj .data (),inj .size ()* sizeof (float ),cudaMemcpyHostToDevice ,stream ),"multi replay inj" ); 401check (cudaGraphLaunch (graph_exec ,stream ),"multi graph changed replay" ); 402check (cudaStreamSynchronize (stream ),"multi graph changed sync" ); 403const Snapshot replay = snapshot (); 404if (std::memcmp (multi .mixed .data (),replay .mixed .data (),multi .mixed .size ()* sizeof (float ))== 0 ) { 405 std::printf (" fused GR graph replay ignored changed inputs\n" ); 406++ bad ; 407 } 408for (int t = 0 ;t < T ;++ t )fused_gr_read (args [t ],stream ); 409check (cudaStreamSynchronize (stream ),"changed single reference sync" ); 410if (!same (replay ,snapshot ())) { 411 std::printf (" fused GR changed graph replay differs from single-token calls\n" ); 412++ bad ; 413 } 414 415 416// S26 STRATA_QFUSE: the read's own q8_1 image of `mixed` must be the bytes native_quantize_q8_1 writes from it - 417// for every T (1..8), directly and through a captured graph replayed twice (the group counters must reset) 418 { 419uint8_t * d_q = nullptr ,* d_ref = nullptr ; 420unsigned * d_cnt = nullptr ; 421const size_t qbytes = (size_t )T * (N /32 )* 36 ; 422check (cudaMalloc (& d_q ,qbytes ),"qfuse q8" ); 423check (cudaMalloc (& d_ref ,qbytes ),"qfuse ref" ); 424check (cudaMalloc (& d_cnt , (N /32 )* sizeof (unsigned )),"qfuse counters" ); 425check (cudaMemset (d_cnt ,0 , (N /32 )* sizeof (unsigned )),"qfuse counters zero" ); 426int qbad = 0 ; 427 std::vector < uint8_t > hq (qbytes ),hr (qbytes ); 428for (int tt = 1 ;tt <=T && !qbad ;++ tt ) { 429 std::vector < FusedGrArgs > qa (args .begin (),args .begin ()+ tt ); 430for (int t = 0 ;t < tt ;++ t ) {qa [t ].q8_mixed = d_q + (size_t )t * (N /32 )* 36 ;qa [t ].q8_cnt = d_cnt ; } 431for (int rep = 0 ;rep < 3 && !qbad ;++ rep ) { 432check (cudaMemsetAsync (d_q ,0x5a ,qbytes ,stream ),"qfuse poison" ); 433bool wrote = false; 434cudaGraph_t qg = nullptr ; 435cudaGraphExec_t qx = nullptr ; 436if (rep == 0 ) { 437wrote = fused_gr_read_multi (qa .data (),tt ,d_xn ,stream ); 438 }else {// a captured read, replayed (twice: rep 1 and 2 use fresh captures, each replayed twice) 439check (cudaStreamBeginCapture (stream ,cudaStreamCaptureModeThreadLocal ),"qfuse begin" ); 440wrote = fused_gr_read_multi (qa .data (),tt ,d_xn ,stream ); 441check (cudaStreamEndCapture (stream ,& qg ),"qfuse end" ); 442check (cudaGraphInstantiate (& qx ,qg ,nullptr ,nullptr ,0 ),"qfuse instantiate" ); 443check (cudaGraphLaunch (qx ,stream ),"qfuse replay 1" ); 444check (cudaMemsetAsync (d_q ,0x5a ,qbytes ,stream ),"qfuse poison 2" ); 445check (cudaGraphLaunch (qx ,stream ),"qfuse replay 2" ); 446 } 447 strata::kernels::native_quantize_q8_1 (d_mixed ,d_ref ,N ,tt ,stream ); 448check (cudaStreamSynchronize (stream ),"qfuse sync" ); 449if (qx ) {cudaGraphExecDestroy (qx );cudaGraphDestroy (qg ); } 450if (v3 ) { std::printf (" QFUSE: the v3 read writes no q8_1 (%s)\n" ,wrote ?"WRONG: it says it did" :"ok" );qbad += wrote ;break ; } 451check (cudaMemcpy (hq .data (),d_q , (size_t )tt * (N /32 )* 36 ,cudaMemcpyDeviceToHost ),"qfuse read" ); 452check (cudaMemcpy (hr .data (),d_ref , (size_t )tt * (N /32 )* 36 ,cudaMemcpyDeviceToHost ),"qfuse ref read" ); 453if (!wrote || std::memcmp (hq .data (),hr .data (), (size_t )tt * (N /32 )* 36 )!= 0 ) { 454 std::printf (" QFUSE: T=%d rep %d: %s\n" ,tt ,rep ,wrote ?"q8_1 bytes differ" :"not written" ); 455++ qbad ; 456 } 457 } 458 } 459 std::printf (" fused GR read + q8_1 (STRATA_QFUSE), T 1..8, direct and graph replays: %s\n" ,qbad ?"FAIL" :"pass" ); 460bad += qbad ; 461#if (defined(STRATA_USE_HIP )|| defined(__HIP_PLATFORM_AMD__ ))&& !defined(STRATA_HIP_GFX906 ) 462// Counter-free q8_mixed path (q8_cnt == nullptr, used by MTP when STRATA_QFUSE_HC_LOCAL=1 on wave32 HIP where 463// STRATA_GR_FAST_BUILD == 0): must return true and write identical q8_1 bytes across T=1..8 and graph replays, 464// and must return false without writing when STRATA_QFUSE_HC_LOCAL=0 and q8_cnt == nullptr. 465if (!v3 ) { 466int cf_bad = 0 ; 467const char * prev_loc = std::getenv ("STRATA_QFUSE_HC_LOCAL" ); 468const std::string saved_loc = prev_loc ?prev_loc :"" ; 469#if defined(_WIN32 ) 470_putenv_s ("STRATA_QFUSE_HC_LOCAL" ,"1" ); 471#else 472setenv ("STRATA_QFUSE_HC_LOCAL" ,"1" ,1 ); 473#endif 474for (int tt = 1 ;tt <=T && !cf_bad ;++ tt ) { 475 std::vector < FusedGrArgs > qa (args .begin (),args .begin ()+ tt ); 476for (int t = 0 ;t < tt ;++ t ) {qa [t ].q8_mixed = d_q + (size_t )t * (N /32 )* 36 ;qa [t ].q8_cnt = nullptr ; } 477for (int rep = 0 ;rep < 2 && !cf_bad ;++ rep ) { 478check (cudaMemcpyAsync (d_r ,r .data (),r .size ()* sizeof (float ),cudaMemcpyHostToDevice ,stream ),"cf R" ); 479check (cudaMemsetAsync (d_q ,0xff ,qbytes ,stream ),"cf poison" ); 480bool wrote = false; 481cudaGraph_t qg = nullptr ; 482cudaGraphExec_t qx = nullptr ; 483if (rep == 0 ) { 484wrote = fused_gr_read_multi (qa .data (),tt ,d_xn ,stream ); 485 }else { 486check (cudaStreamBeginCapture (stream ,cudaStreamCaptureModeThreadLocal ),"cf begin" ); 487wrote = fused_gr_read_multi (qa .data (),tt ,d_xn ,stream ); 488check (cudaStreamEndCapture (stream ,& qg ),"cf end" ); 489check (cudaGraphInstantiate (& qx ,qg ,nullptr ,nullptr ,0 ),"cf inst" ); 490check (cudaGraphLaunch (qx ,stream ),"cf replay 1" ); 491check (cudaMemsetAsync (d_q ,0x5a ,qbytes ,stream ),"cf poison 2" ); 492check (cudaMemcpyAsync (d_r ,r .data (),r .size ()* sizeof (float ),cudaMemcpyHostToDevice ,stream ),"cf R2" ); 493check (cudaGraphLaunch (qx ,stream ),"cf replay 2" ); 494 } 495 strata::kernels::native_quantize_q8_1 (d_mixed ,d_ref ,N ,tt ,stream ); 496check (cudaStreamSynchronize (stream ),"cf sync" ); 497if (qx ) {cudaGraphExecDestroy (qx );cudaGraphDestroy (qg ); } 498check (cudaMemcpy (hq .data (),d_q , (size_t )tt * (N /32 )* 36 ,cudaMemcpyDeviceToHost ),"cf read" ); 499check (cudaMemcpy (hr .data (),d_ref , (size_t )tt * (N /32 )* 36 ,cudaMemcpyDeviceToHost ),"cf ref" ); 500if (!wrote || std::memcmp (hq .data (),hr .data (), (size_t )tt * (N /32 )* 36 )!= 0 )++ cf_bad ; 501 } 502 } 503#if defined(_WIN32 ) 504_putenv_s ("STRATA_QFUSE_HC_LOCAL" ,"0" ); 505#else 506setenv ("STRATA_QFUSE_HC_LOCAL" ,"0" ,1 ); 507#endif 508 { 509 std::vector < FusedGrArgs > qa (args .begin (),args .begin ()+ 2 ); 510for (int t = 0 ;t < 2 ;++ t ) {qa [t ].q8_mixed = d_q + (size_t )t * (N /32 )* 36 ;qa [t ].q8_cnt = nullptr ; } 511check (cudaMemsetAsync (d_q ,0xa5 ,qbytes ,stream ),"cf off poison" ); 512const bool wrote = fused_gr_read_multi (qa .data (),2 ,d_xn ,stream ); 513check (cudaStreamSynchronize (stream ),"cf off sync" ); 514check (cudaMemcpy (hq .data (),d_q , (size_t )2 * (N /32 )* 36 ,cudaMemcpyDeviceToHost ),"cf off read" ); 515bool untouched = true; 516for (size_t i = 0 ;i < (size_t )2 * (N /32 )* 36 ;++ i )untouched = untouched && (hq [i ]== 0xa5 ); 517if (wrote || !untouched )++ cf_bad ; 518 } 519#if defined(_WIN32 ) 520_putenv_s ("STRATA_QFUSE_HC_LOCAL" ,prev_loc ?saved_loc .c_str () :"" ); 521#else 522if (prev_loc )setenv ("STRATA_QFUSE_HC_LOCAL" ,saved_loc .c_str (),1 ); 523else unsetenv ("STRATA_QFUSE_HC_LOCAL" ); 524#endif 525 std::printf (" fused GR counter-free q8_1 (q8_cnt == nullptr), T 1..8, direct/graph + LOCAL=0 guard: %s\n" , 526cf_bad ?"FAIL" :"pass" ); 527bad += cf_bad ; 528 } 529#endif 530cudaFree (d_q );cudaFree (d_ref );cudaFree (d_cnt ); 531 } 532 533// #3948: STRATA_QFUSE in a verify window of TWO GROUPS (verify.cpp gr_read_group with G = 2): the window's tokens are 534// read in two calls one after the other, the same group counters, each call with its own tokens' destinations - the 535// attention half's xq_ is indexed from the group's first token, the expert half's nat_xq_ by the window's token. 536// Fresh random inputs every repetition, 200 repetitions for each T = 2..8 (a race that fires one launch in a hundred 537// shows), direct and as a graph replay. After every repetition: both groups' q8_1 bytes equal what 538// native_quantize_q8_1 makes of `mixed`, nothing outside the written ranges changed, and the counters are back at zero. 539if (!v3 ) { 540// (STRATA_GRP_REPS=N more repetitions, STRATA_GRP_KEEP_GOING=1 counts every failure instead of stopping at the first: to measure a rare one) 541const int REPS = std::getenv ("STRATA_GRP_REPS" ) ? std::max (1 , std::atoi (std::getenv ("STRATA_GRP_REPS" ))) :200 ; 542const bool keep_going = std::getenv ("STRATA_GRP_KEEP_GOING" )!= nullptr ; 543const size_t B = (size_t ) (N /32 )* 36 ;// one token's q8_1 image 544uint8_t * d_qa = nullptr ,* d_qb = nullptr ,* d_qabs = nullptr ,* d_ref2 = nullptr ; 545unsigned * d_cnt2 = nullptr ; 546check (cudaMalloc (& d_qa , (size_t )T * B ),"qfuse2 qa" ); 547check (cudaMalloc (& d_qb , (size_t )T * B ),"qfuse2 qb" ); 548check (cudaMalloc (& d_qabs , (size_t )T * B ),"qfuse2 qabs" ); 549check (cudaMalloc (& d_ref2 , (size_t )T * B ),"qfuse2 ref" ); 550check (cudaMalloc (& d_cnt2 , (N /32 )* sizeof (unsigned )),"qfuse2 counters" ); 551check (cudaMemset (d_cnt2 ,0 , (N /32 )* sizeof (unsigned )),"qfuse2 counters zero" ); 552 std::mt19937 prng (0x3948u ); 553 std::normal_distribution < float > pn (0.0f ,0.3f ); 554 std::vector < uint8_t > g_all ((size_t )T * B ),g_ref ((size_t )T * B ),g_a ((size_t )T * B ),g_b ((size_t )T * B ); 555 std::vector < unsigned > h_cnt (N /32 ); 556int g2bad = 0 ; 557long runs = 0 ; 558for (int tt = 2 ;tt <=T && (!g2bad || keep_going );++ tt ) { 559const int h = (tt + 1 ) /2 ;// verify.cpp: tb_/te_ = {0, (T + 1) / 2}, {(T + 1) / 2, T} 560 std::vector < FusedGrArgs > rel1 (args .begin (),args .begin ()+ h ),rel2 (args .begin ()+ h ,args .begin ()+ tt ); 561 std::vector < FusedGrArgs > abs1 = rel1 ,abs2 = rel2 ; 562for (int t = 0 ;t < h ;++ t ) { 563rel1 [t ].q8_mixed = d_qa + (size_t )t * B ;rel1 [t ].q8_cnt = d_cnt2 ; 564abs1 [t ].q8_mixed = d_qabs + (size_t )t * B ;abs1 [t ].q8_cnt = d_cnt2 ; 565 } 566for (int t = h ;t < tt ;++ t ) { 567rel2 [t - h ].q8_mixed = d_qb + (size_t ) (t - h )* B ;rel2 [t - h ].q8_cnt = d_cnt2 ; 568abs2 [t - h ].q8_mixed = d_qabs + (size_t )t * B ;abs2 [t - h ].q8_cnt = d_cnt2 ; 569 } 570cudaGraph_t gg = nullptr ; 571cudaGraphExec_t gx = nullptr ; 572check (cudaStreamBeginCapture (stream ,cudaStreamCaptureModeThreadLocal ),"qfuse2 begin" ); 573const bool w1 = fused_gr_read_multi (rel1 .data (),h ,d_xn ,stream ); 574const bool w2 = fused_gr_read_multi (rel2 .data (),tt - h ,d_xn + (size_t )h * D ,stream ); 575check (cudaStreamEndCapture (stream ,& gg ),"qfuse2 end" ); 576check (cudaGraphInstantiate (& gx ,gg ,nullptr ,nullptr ,0 ),"qfuse2 instantiate" ); 577if (!w1 || !w2 ) { std::printf (" QFUSE two groups: T=%d: a group did not write its q8_1\n" ,tt );++ g2bad ; } 578for (int rep = 0 ;rep < REPS && (!g2bad || keep_going );++ rep ) { 579for (auto & x :r )x = pn (prng ); 580for (auto & x :bo )x = pn (prng ); 581for (auto & x :inj )x = pn (prng ); 582check (cudaMemcpy (d_r ,r .data (),r .size ()* sizeof (float ),cudaMemcpyHostToDevice ),"qfuse2 R" ); 583check (cudaMemcpy (d_bo ,bo .data (),bo .size ()* sizeof (float ),cudaMemcpyHostToDevice ),"qfuse2 bo" ); 584check (cudaMemcpy (d_inj ,inj .data (),inj .size ()* sizeof (float ),cudaMemcpyHostToDevice ),"qfuse2 inj" ); 585check (cudaMemsetAsync (d_qa ,0x5a , (size_t )T * B ,stream ),"qfuse2 poison a" ); 586check (cudaMemsetAsync (d_qb ,0x5a , (size_t )T * B ,stream ),"qfuse2 poison b" ); 587check (cudaMemsetAsync (d_qabs ,0x5a , (size_t )T * B ,stream ),"qfuse2 poison abs" ); 588const int mode = rep %3 ;// 0: group-relative, direct; 1: window-indexed, direct; 2: group-relative, graph replay 589if (mode == 0 ) { 590fused_gr_read_multi (rel1 .data (),h ,d_xn ,stream ); 591fused_gr_read_multi (rel2 .data (),tt - h ,d_xn + (size_t )h * D ,stream ); 592 }else if (mode == 1 ) { 593fused_gr_read_multi (abs1 .data (),h ,d_xn ,stream ); 594fused_gr_read_multi (abs2 .data (),tt - h ,d_xn + (size_t )h * D ,stream ); 595 }else { 596check (cudaGraphLaunch (gx ,stream ),"qfuse2 replay" ); 597 } 598 strata::kernels::native_quantize_q8_1 (d_mixed ,d_ref2 ,N ,tt ,stream ); 599check (cudaStreamSynchronize (stream ),"qfuse2 sync" ); 600++ runs ; 601check (cudaMemcpy (g_ref .data (),d_ref2 , (size_t )tt * B ,cudaMemcpyDeviceToHost ),"qfuse2 read ref" ); 602check (cudaMemcpy (h_cnt .data (),d_cnt2 ,h_cnt .size ()* sizeof (unsigned ),cudaMemcpyDeviceToHost ),"qfuse2 read counters" ); 603for (size_t i = 0 ;i < h_cnt .size ();++ i ) 604if (h_cnt [i ]!= 0 ) { 605 std::printf (" QFUSE two groups: T=%d rep %d: counter %zu left at %u\n" ,tt ,rep ,i ,h_cnt [i ]); 606++ g2bad ; 607break ; 608 } 609if (mode == 1 ) { 610check (cudaMemcpy (g_all .data (),d_qabs , (size_t )T * B ,cudaMemcpyDeviceToHost ),"qfuse2 read abs" ); 611if (std::memcmp (g_all .data (),g_ref .data (), (size_t )tt * B )!= 0 ) { 612for (int t = 0 ;t < tt ;++ t ) 613if (std::memcmp (g_all .data ()+ (size_t )t * B ,g_ref .data ()+ (size_t )t * B ,B )!= 0 ) { 614 std::printf (" QFUSE two groups (window-indexed): T=%d rep %d: token %d (group %d) differs\n" ,tt ,rep ,t ,t < h ?0 :1 ); 615int nd = 0 ;// where: block = 32 columns, bytes 0-3 the (scale, sum) pair, 4-35 the codes 616for (size_t o = 0 ;o < B && nd < 6 ;++ o ) 617if (g_all [(size_t )t * B + o ]!= g_ref [(size_t )t * B + o ]) { 618 std::printf (" block %zu byte %zu (%s): got 0x%02x, separate quantise 0x%02x\n" ,o /36 ,o %36 , 619o %36 < 4 ?"scale/sum" :"code" ,g_all [(size_t )t * B + o ],g_ref [(size_t )t * B + o ]); 620++ nd ; 621 } 622break ; 623 } 624++ g2bad ; 625 } 626for (size_t i = (size_t )tt * B ;i < (size_t )T * B ;++ i ) 627if (g_all [i ]!= 0x5a ) { std::printf (" QFUSE two groups: T=%d rep %d: wrote past the window at byte %zu\n" ,tt ,rep ,i );++ g2bad ;break ; } 628 }else { 629check (cudaMemcpy (g_a .data (),d_qa , (size_t )T * B ,cudaMemcpyDeviceToHost ),"qfuse2 read a" ); 630check (cudaMemcpy (g_b .data (),d_qb , (size_t )T * B ,cudaMemcpyDeviceToHost ),"qfuse2 read b" ); 631if (std::memcmp (g_a .data (),g_ref .data (), (size_t )h * B )!= 0 ) { 632 std::printf (" QFUSE two groups (group-relative, %s): T=%d rep %d: group 0 differs\n" ,mode == 2 ?"graph" :"direct" ,tt ,rep ); 633for (size_t o = 0 ,nd = 0 ;o < (size_t )h * B && nd < 6 ;++ o ) 634if (g_a [o ]!= g_ref [o ]) { 635 std::printf (" token %zu block %zu byte %zu (%s): got 0x%02x, separate quantise 0x%02x\n" ,o /B , (o %B ) /36 , 636o %36 ,o %36 < 4 ?"scale/sum" :"code" ,g_a [o ],g_ref [o ]); 637++ nd ; 638 } 639++ g2bad ; 640 } 641if (std::memcmp (g_b .data (),g_ref .data ()+ (size_t )h * B , (size_t ) (tt - h )* B )!= 0 ) { 642 std::printf (" QFUSE two groups (group-relative, %s): T=%d rep %d: group 1 differs\n" ,mode == 2 ?"graph" :"direct" ,tt ,rep ); 643++ g2bad ; 644 } 645for (size_t i = (size_t )h * B ;i < (size_t )T * B ;++ i ) 646if (g_a [i ]!= 0x5a ) { std::printf (" QFUSE two groups: T=%d rep %d: group 0 wrote past its tokens at byte %zu\n" ,tt ,rep ,i );++ g2bad ;break ; } 647for (size_t i = (size_t ) (tt - h )* B ;i < (size_t )T * B ;++ i ) 648if (g_b [i ]!= 0x5a ) { std::printf (" QFUSE two groups: T=%d rep %d: group 1 wrote past its tokens at byte %zu\n" ,tt ,rep ,i );++ g2bad ;break ; } 649 } 650 } 651cudaGraphExecDestroy (gx ); 652cudaGraphDestroy (gg ); 653 } 654// the inputs the rest of the test (and its caller) expect again 655check (cudaDeviceSynchronize (),"qfuse2 done" ); 656 std::printf (" fused GR read + q8_1 (STRATA_QFUSE), a window of two groups, T 2..8, %ld runs (%d random inputs each T), " 657"group-relative and window-indexed, direct and graph: %s\n" ,runs ,REPS ,g2bad ?"FAIL" :"pass" ); 658bad += g2bad ; 659cudaFree (d_qa );cudaFree (d_qb );cudaFree (d_qabs );cudaFree (d_ref2 );cudaFree (d_cnt2 ); 660 } 661 std::printf (" fused GR multi max-T=8 LDS launch and changing graph replay %s\n" , 662bad == 0 ?"pass" :"FAIL" ); 663check (cudaGraphExecDestroy (graph_exec ),"multi graph exec destroy" ); 664check (cudaGraphDestroy (graph ),"multi graph destroy" ); 665check (cudaStreamDestroy (stream ),"multi stream destroy" ); 666check (cudaFree (d_xn ),"free multi xn" ); 667check (cudaFree (d_mixed ),"free multi mixed" ); 668check (cudaFree (d_inj_out ),"free multi injection" ); 669check (cudaFree (d_rs ),"free multi rs" ); 670check (cudaFree (d_lo ),"free multi lo" ); 671check (cudaFree (d_inj ),"free multi inj" ); 672check (cudaFree (d_bo ),"free multi bo" ); 673check (cudaFree (d_r_out ),"free multi R_out" ); 674check (cudaFree (d_r ),"free multi R" ); 675return bad ; 676} 677 678}// namespace 679 680int main (int argc ,char ** argv ) { 681// this test compares the default kernels bit for bit; the split read (a default on a 12.0 card since 0.2.7) is STRATA_GR_V3=1 below 682#if defined(_WIN32 ) 683if (std::getenv ("STRATA_GR_V3" )== nullptr )_putenv_s ("STRATA_GR_V3" ,"0" ); 684#else 685if (std::getenv ("STRATA_GR_V3" )== nullptr )setenv ("STRATA_GR_V3" ,"0" ,1 ); 686#endif 687bool selftest = false; 688for (int i = 1 ;i < argc ;++ i ) { 689if (std::string (argv [i ])== "--selftest" )selftest = true; 690else { std::fprintf (stderr ,"usage: gr_parity [--selftest]\n" );return 2 ; } 691 } 692 693const long long n_embd = 256 ,hc = 4 ,hc_lr = 32 ; 694const long long hc_dim = hc * n_embd ; 695const float eps = 1e-6f ; 696 std::mt19937 rng (1234 ); 697 std::normal_distribution < float > gauss (0.0f ,1.0f ); 698 699// Weights are BF16-VALUED f32, which is what the pack holds for a BF16 source type. Generating plain f32 700// weights would make the kernel (which stores 16 bits) differ from the reference by the rounding itself, 701// and the test would then be measuring the fixture. 702auto bf16_weight = [& ](size_t n ,float sigma ) { 703 std::vector < float > v (n ); 704for (auto & x :v )x = to_bf16 (gauss (rng )* sigma ); 705return v ; 706 }; 707 std::vector < float > R ((size_t )hc_dim ); 708for (auto & x :R )x = gauss (rng ); 709// STREAM SCALES 1, 4, 16, 64. Without them trap 1 is only ~5% observable, because four streams of i.i.d. 710// normal noise have nearly the same RMS by accident - so a whole-stack RMS and a per-stream RMS agree to 711// within the noise, and the fixture would be asserting a distinction it cannot see. Giving the streams 712// different magnitudes is what makes "per-stream" a claim with content. 713for (long long c = 0 ;c < hc ;++ c ) 714for (long long d = 0 ;d < n_embd ;++ d )R [(size_t ) (c * n_embd + d )] *= std::pow (4.0f , (float )c ); 715// gamma stored as (1 + w), so values near 1 - a reader expecting a plain scale is the one this catches 716 std::vector < float > w_norm ((size_t )hc_dim ); 717for (auto & x :w_norm )x = 1.0f + 0.1f * gauss (rng ); 718 std::vector < float > w_down = bf16_weight ((size_t ) (hc_lr * hc_dim ),0.2f ); 719 std::vector < float > w_up = bf16_weight ((size_t ) (hc_dim * hc_lr ),0.2f ); 720 std::vector < float > w_inject = bf16_weight ((size_t ) (hc * hc_dim ),0.05f ); 721 722// NO PERMUTATION: the kernel takes both matrices exactly as the manifest (and `ref/gr.py`) store them. 723// `w_down` is (hc_lr, hc_dim) row-major and `w_up` is (hc_dim, hc_lr) row-major, so the bit-copy below is 724// the whole conversion. An earlier version of this kernel wanted both TRANSPOSED - the test built them 725// that way and asserted the orientation observable - and that is gone because the warp-per-row mapping 726// makes it unnecessary. What replaces it is the general orientation trap further down: a weight array 727// read with the wrong index expression must still produce a visibly different answer. 728 std::vector < uint16_t > q_down (w_down .size ()),q_up (w_up .size ()),q_inject (w_inject .size ()); 729for (size_t i = 0 ;i < w_down .size ();++ i )q_down [i ]= bf16_bits (w_down [i ]); 730for (size_t i = 0 ;i < w_up .size ();++ i )q_up [i ]= bf16_bits (w_up [i ]); 731for (size_t i = 0 ;i < w_inject .size ();++ i )q_inject [i ]= bf16_bits (w_inject [i ]); 732 733// ---- device side 734float * d_R = nullptr ,* d_norm = nullptr ,* d_mixed = nullptr ,* d_inject = nullptr ; 735uint16_t * d_down = nullptr ,* d_up = nullptr ,* d_inj = nullptr ; 736check (cudaMalloc (& d_R ,R .size ()* sizeof (float )),"m R" ); 737check (cudaMalloc (& d_norm ,w_norm .size ()* sizeof (float )),"m norm" ); 738check (cudaMalloc (& d_down ,q_down .size ()* sizeof (uint16_t )),"m down" ); 739check (cudaMalloc (& d_up ,q_up .size ()* sizeof (uint16_t )),"m up" ); 740check (cudaMalloc (& d_inj ,q_inject .size ()* sizeof (uint16_t )),"m inj" ); 741check (cudaMalloc (& d_mixed , (size_t )n_embd * sizeof (float )),"m mixed" ); 742check (cudaMalloc (& d_inject , (size_t )hc * sizeof (float )),"m inject" ); 743check (cudaMemcpy (d_R ,R .data (),R .size ()* sizeof (float ),cudaMemcpyHostToDevice ),"c R" ); 744check (cudaMemcpy (d_norm ,w_norm .data (),w_norm .size ()* sizeof (float ),cudaMemcpyHostToDevice ),"c norm" ); 745check (cudaMemcpy (d_down ,q_down .data (),q_down .size ()* sizeof (uint16_t ),cudaMemcpyHostToDevice ),"c down" ); 746check (cudaMemcpy (d_up ,q_up .data (),q_up .size ()* sizeof (uint16_t ),cudaMemcpyHostToDevice ),"c up" ); 747check (cudaMemcpy (d_inj ,q_inject .data (),q_inject .size ()* sizeof (uint16_t ),cudaMemcpyHostToDevice ),"c inj" ); 748 749const strata::kernels::GrShapes sh {n_embd ,hc ,hc_lr }; 750// DEVICE memory: the workspace is written by the kernel. 751void * d_ws_raw = nullptr ; 752check (cudaMalloc (& d_ws_raw , strata::kernels::gr_workspace_bytes (sh )),"m ws" ); 753 strata::kernels::GrWorkspace ws ; 754 strata::kernels::gr_workspace_init (sh ,d_ws_raw ,ws ); 755 strata::kernels::gr_read (d_R ,d_norm ,d_down ,d_up ,d_inj ,eps ,sh ,ws ,d_mixed ,d_inject ,nullptr ); 756 std::vector < float > got_mixed ((size_t )n_embd ),got_inject ((size_t )hc ); 757check (cudaMemcpy (got_mixed .data (),d_mixed ,got_mixed .size ()* sizeof (float ),cudaMemcpyDeviceToHost ),"c mixed" ); 758check (cudaMemcpy (got_inject .data (),d_inject ,got_inject .size ()* sizeof (float ),cudaMemcpyDeviceToHost ),"c inject" ); 759 760int bad = 0 ; 761 762// ---- the traps, asserted OBSERVABLE before the kernel is judged against the right reading 763Opts right ; 764 std::vector < float > want_mixed ,want_inject ; 765reference (R ,w_norm ,w_down ,w_up ,w_inject ,eps ,n_embd ,hc ,hc_lr ,right ,want_mixed ,want_inject ); 766 767struct Trap {const char * name ;Opts o ;double floor ; }; 768const Trap traps []= { 769 {"per-stream RMSNorm vs whole-stack" , {false, true, true, true, true},0.05 }, 770 {"/hc inside the silu vs outside" , {true, false, true, true, true},0.05 }, 771 {"SiLU on lo / sigmoid on gate, swapped" , {true, true, false, true, true},0.05 }, 772 {"mean over streams vs sum" , {true, true, true, false, true},0.50 }, 773 {"BF16 vs FP32 activation contracts" , {true, true, true, true, false},1e-4 }, 774 }; 775for (const Trap & t :traps ) { 776 std::vector < float > m ,i ; 777reference (R ,w_norm ,w_down ,w_up ,w_inject ,eps ,n_embd ,hc ,hc_lr ,t .o ,m ,i ); 778const double rel = rel_diff (want_mixed ,m ); 779const bool observable = rel > t .floor ; 780 std::printf (" %-40s %-4s (%.4f%% apart, floor %.3f%%)\n" ,t .name ,observable ?"yes" :"*** NO ***" , 781rel * 100 ,t .floor * 100 ); 782if (!observable ) { 783 std::printf (" *** the fixture cannot see this trap, so passing proves nothing about it ***\n" ); 784++ bad ; 785 } 786 } 787 788// ---- the kernel against the correct reference 789double mag = 0 ; 790const double rel_mixed = rel_diff (want_mixed ,got_mixed ,& mag ); 791 std::printf ("\n %-40s worst %.3e (mean |ref| %.4f)\n" ,"mixed vs reference" ,rel_mixed ,mag ); 792 793// The tolerance is set by SUMMATION ORDER, not by the structure: both sides round the activation to bf16 794// and both weights are bf16-valued, so every product is exact in f32 and only the order differs. A 795// structural error is several orders of magnitude larger - the trap floors above say so. 796if (!(rel_mixed <=1e-4 )) { 797 std::printf (" *** over 1e-4, which summation order does not explain - look for a structural bug ***\n" ); 798++ bad ; 799 } 800 801const double rel_inject = rel_diff (want_inject ,got_inject ); 802 std::printf (" %-40s rel %.3e\n" ,"inject vs reference" ,rel_inject ); 803if (!(rel_inject <=1e-4 )) { 804// Say WHICH elements and by how much: a whole-array L1 hides whether this is one stream wrong (an 805// indexing bug) or all of them slightly wrong (a precision bug), and those need different responses. 806 std::printf (" want:" ); 807for (long long c = 0 ;c < hc ;++ c ) std::printf (" %12.6f" , (double )want_inject [(size_t )c ]); 808 std::printf ("\n got :" ); 809for (long long c = 0 ;c < hc ;++ c ) std::printf (" %12.6f" , (double )got_inject [(size_t )c ]); 810 std::printf ("\n" ); 811++ bad ; 812 } 813 814// Same geometry and weights with the CUDA single-token activation contract. The scalar fixture 815// below is the independent precision check; this case exercises multi-stream reductions and indexing. 816 { 817Opts fp32 ; 818fp32 .round_activation = false; 819 std::vector < float > wm ,wi ,gm ((size_t )n_embd ),gi ((size_t )hc ); 820reference (R ,w_norm ,w_down ,w_up ,w_inject ,eps ,n_embd ,hc ,hc_lr ,fp32 ,wm ,wi ); 821 strata::kernels::gr_set_fp32_activations (true); 822 strata::kernels::gr_read (d_R ,d_norm ,d_down ,d_up ,d_inj ,eps ,sh ,ws ,d_mixed ,d_inject ,nullptr ); 823check (cudaMemcpy (gm .data (),d_mixed ,gm .size ()* sizeof (float ),cudaMemcpyDeviceToHost ),"FP32 mixed" ); 824check (cudaMemcpy (gi .data (),d_inject ,gi .size ()* sizeof (float ),cudaMemcpyDeviceToHost ),"FP32 inject" ); 825 strata::kernels::gr_set_fp32_activations (false); 826const double rm = rel_diff (wm ,gm ),ri = rel_diff (wi ,gi ); 827const bool ok = rm <=1e-4 && ri <=1e-4 ; 828 std::printf (" FP32 activations vs reference: %s (mixed %.3e, inject %.3e)\n" , 829ok ?"pass" :"FAIL" ,rm ,ri ); 830if (!ok )++ bad ; 831 strata::kernels::gr_read (d_R ,d_norm ,d_down ,d_up ,d_inj ,eps ,sh ,ws ,d_mixed ,d_inject ,nullptr ); 832check (cudaMemcpy (gm .data (),d_mixed ,gm .size ()* sizeof (float ),cudaMemcpyDeviceToHost ),"restored mixed" ); 833check (cudaMemcpy (gi .data (),d_inject ,gi .size ()* sizeof (float ),cudaMemcpyDeviceToHost ),"restored inject" ); 834const bool restored = std::memcmp (gm .data (),got_mixed .data (),gm .size ()* sizeof (float ))== 0 && 835 std::memcmp (gi .data (),got_inject .data (),gi .size ()* sizeof (float ))== 0 ; 836 std::printf (" restoring default activation contract: %s\n" ,restored ?"byte-identical" :"FAIL" ); 837if (!restored )++ bad ; 838 } 839 840// ---- THE WEIGHT ORIENTATION MUST BE OBSERVABLE. The kernel now takes both matrices exactly as the 841// manifest stores them, so there is no permutation for a loader to forget - but the INDEX EXPRESSIONS 842// inside the kernel are still a place to be wrong, and a wrong one gives right-shaped, wrong-valued 843// output. Feed transposed copies through the same code path and require the answer to differ. 844 { 845uint16_t * d_down_bad = nullptr ,* d_up_bad = nullptr ; 846check (cudaMalloc (& d_down_bad ,q_down .size ()* sizeof (uint16_t )),"m down_bad" ); 847check (cudaMalloc (& d_up_bad ,q_up .size ()* sizeof (uint16_t )),"m up_bad" ); 848 std::vector < uint16_t > tr_down (q_down .size ()),tr_up (q_up .size ()); 849for (long long k = 0 ;k < hc_lr ;++ k ) 850for (long long i = 0 ;i < hc_dim ;++ i ) 851tr_down [(size_t ) (i * hc_lr + k )]= q_down [(size_t ) (k * hc_dim + i )]; 852for (long long i = 0 ;i < hc_dim ;++ i ) 853for (long long k = 0 ;k < hc_lr ;++ k ) 854tr_up [(size_t ) (k * hc_dim + i )]= q_up [(size_t ) (i * hc_lr + k )]; 855check (cudaMemcpy (d_down_bad ,tr_down .data (),tr_down .size ()* 2 ,cudaMemcpyHostToDevice ),"cb d" ); 856check (cudaMemcpy (d_up_bad ,tr_up .data (),tr_up .size ()* 2 ,cudaMemcpyHostToDevice ),"cb u" ); 857 std::vector < float > bad_mixed ((size_t )n_embd ); 858float * d_bad = nullptr ; 859check (cudaMalloc (& d_bad ,bad_mixed .size ()* sizeof (float )),"m bad" ); 860 strata::kernels::gr_read (d_R ,d_norm ,d_down_bad ,d_up_bad ,d_inj ,eps ,sh ,ws ,d_bad ,d_inject , 861nullptr ); 862check (cudaMemcpy (bad_mixed .data (),d_bad ,bad_mixed .size ()* sizeof (float ),cudaMemcpyDeviceToHost ),"cb b" ); 863const double rel = rel_diff (want_mixed ,bad_mixed ); 864const bool visible = rel > 0.05 ; 865 std::printf (" %-40s %s (%.1f%% apart)\n" ,"weight orientation is observable" , 866visible ?"yes" :"*** NO ***" ,rel * 100 ); 867if (!visible )++ bad ; 868cudaFree (d_down_bad );cudaFree (d_up_bad );cudaFree (d_bad ); 869 } 870 871// ---- the FINAL mixer passes w_inject = nullptr, and then nothing may be written 872float sentinel = -12345.0f ; 873check (cudaMemcpy (d_inject ,& sentinel ,sizeof (float ),cudaMemcpyHostToDevice ),"c sentinel" ); 874 strata::kernels::gr_read (d_R ,d_norm ,d_down ,d_up ,nullptr ,eps ,sh ,ws ,d_mixed ,d_inject ,nullptr ); 875float after = 0.0f ; 876check (cudaMemcpy (& after ,d_inject ,sizeof (float ),cudaMemcpyDeviceToHost ),"c after" ); 877const bool untouched = (after == sentinel ); 878 std::printf (" %-40s %s\n" ,"null w_inject writes nothing" ,untouched ?"yes" :"*** NO ***" ); 879if (!untouched )++ bad ; 880 881// ---- gr_write: the `2*sigmoid` centring, asserted as the PROPERTY the source comment claims 882 std::vector < float > block_out ((size_t )n_embd ); 883for (auto & x :block_out )x = gauss (rng ); 884 std::vector < float > zero_inj ((size_t )hc ,0.0f ); 885float * d_Rw = nullptr ,* d_bo = nullptr ,* d_zi = nullptr ,* d_outw = nullptr ; 886check (cudaMalloc (& d_Rw ,R .size ()* sizeof (float )),"m Rw" ); 887check (cudaMalloc (& d_bo ,block_out .size ()* sizeof (float )),"m bo" ); 888check (cudaMalloc (& d_zi ,zero_inj .size ()* sizeof (float )),"m zi" ); 889check (cudaMalloc (& d_outw ,R .size ()* sizeof (float )),"m outw" ); 890check (cudaMemcpy (d_Rw ,R .data (),R .size ()* sizeof (float ),cudaMemcpyHostToDevice ),"c Rw" ); 891check (cudaMemcpy (d_bo ,block_out .data (),block_out .size ()* sizeof (float ),cudaMemcpyHostToDevice ),"c bo" ); 892check (cudaMemcpy (d_zi ,zero_inj .data (),zero_inj .size ()* sizeof (float ),cudaMemcpyHostToDevice ),"c zi" ); 893 894 strata::kernels::gr_write (d_Rw ,d_bo ,d_zi ,sh ,d_outw ,nullptr ); 895 std::vector < float > got_write ((size_t )hc_dim ); 896check (cudaMemcpy (got_write .data (),d_outw ,got_write .size ()* sizeof (float ),cudaMemcpyDeviceToHost ),"c outw" ); 897 898double worst_plain = 0 ; 899for (long long c = 0 ;c < hc ;++ c ) 900for (long long d = 0 ;d < n_embd ;++ d ) { 901const size_t i = (size_t ) (c * n_embd + d ); 902const double want = (double )R [i ]+ (double )block_out [(size_t )d ]; 903worst_plain = std::fmax (worst_plain ,rel_terms (want , (double )got_write [i ], (double )R [i ], 904 (double )block_out [(size_t )d ])); 905 } 906const bool plain = worst_plain < 1e-6 ; 907 std::printf (" %-40s %s (worst rel %.3e)\n" ,"zero injection -> plain residual add" , 908plain ?"yes" :"*** NO ***" ,worst_plain ); 909if (!plain )++ bad ; 910 911// the rival reading: sigmoid without the 2 halves the update, and must be visible. 912// 913// The comparison is on the DELTA `out - R`, not on `out`. The claim is about the gate WEIGHT, and `R` 914// now reaches 64 in the last stream, so measuring `R + block_out*w` against the wrong reading dilutes a 915// 100% error in `w` down to 2.27% - which is how this check first passed as "not observable" while being 916// perfectly observable. Measure the quantity the claim is about. 917 { 918 std::vector < float > got_delta ((size_t )hc_dim ),want_delta ((size_t )hc_dim ),wrong_delta ((size_t )hc_dim ); 919for (long long c = 0 ;c < hc ;++ c ) 920for (long long d = 0 ;d < n_embd ;++ d ) { 921const size_t i = (size_t ) (c * n_embd + d ); 922got_delta [i ]= (float ) ((double )got_write [i ]- (double )R [i ]); 923want_delta [i ]= block_out [(size_t )d ];// 2*sigmoid(0) = 1.0 924wrong_delta [i ]= block_out [(size_t )d ]* 0.5f ;// 1*sigmoid(0) = 0.5 925 } 926const double rel = rel_diff (want_delta ,wrong_delta ); 927const double rel_got = rel_diff (want_delta ,got_delta ); 928const bool visible = rel > 0.05 && rel_got < 0.05 ; 929 std::printf (" %-40s %s (readings %.0f%% apart, kernel within %.2e)\n" , 930"1*sigmoid vs 2*sigmoid is observable" ,visible ?"yes" :"*** NO ***" ,rel * 100 ,rel_got ); 931if (!visible )++ bad ; 932 } 933 934// a non-zero injection: every stream receives the SAME block output, differing only by weight 935 std::vector < float > inj ((size_t )hc ); 936for (auto & x :inj )x = gauss (rng )* 3.0f ; 937check (cudaMemcpy (d_zi ,inj .data (),inj .size ()* sizeof (float ),cudaMemcpyHostToDevice ),"c inj2" ); 938 strata::kernels::gr_write (d_Rw ,d_bo ,d_zi ,sh ,d_outw ,nullptr ); 939check (cudaMemcpy (got_write .data (),d_outw ,got_write .size ()* sizeof (float ),cudaMemcpyDeviceToHost ),"c outw2" ); 940double worst_stream = 0 ; 941for (long long c = 0 ;c < hc ;++ c ) { 942const double w = 2.0 / (1.0 + std::exp (- (double )inj [(size_t )c ] / (double )hc )); 943for (long long d = 0 ;d < n_embd ;++ d ) { 944const size_t i = (size_t ) (c * n_embd + d ); 945const double want = (double )R [i ]+ (double )block_out [(size_t )d ]* w ; 946worst_stream = std::fmax (worst_stream ,rel_terms (want , (double )got_write [i ], (double )R [i ], 947 (double )block_out [(size_t )d ]* w )); 948 } 949 } 950const bool streams_ok = worst_stream < 1e-6 ; 951 std::printf (" %-40s %s (worst rel %.3e)\n" ,"every stream adds the same block output" , 952streams_ok ?"yes" :"*** NO ***" ,worst_stream ); 953if (!streams_ok )++ bad ; 954 955// ---- THE REAL DIMENSIONS. Exercise all activation/projection variants at n_embd=2560, hc=4, hc_lr=320. 956// Workspace sizing and every full weight row must agree with the model geometry. 957 { 958const long long rn = 2560 ,rhc = 4 ,rlr = 320 ,rdim = rhc * rn ; 959 std::mt19937 rrng (99 ); 960 std::normal_distribution < float > rg (0.0f ,1.0f ); 961auto rbf16 = [& ](size_t n ,float sigma ) { 962 std::vector < float > v (n ); 963for (auto & x :v )x = to_bf16 (rg (rrng )* sigma ); 964return v ; 965 }; 966 std::vector < float > rR ((size_t )rdim ); 967for (auto & x :rR )x = rg (rrng ); 968for (long long c = 0 ;c < rhc ;++ c ) 969for (long long d = 0 ;d < rn ;++ d )rR [(size_t ) (c * rn + d )] *= std::pow (4.0f , (float )c ); 970 std::vector < float > rnorm ((size_t )rdim ); 971for (auto & x :rnorm )x = 1.0f + 0.1f * rg (rrng ); 972 std::vector < float > rdown = rbf16 ((size_t ) (rlr * rdim ),0.2f ); 973 std::vector < float > rup = rbf16 ((size_t ) (rdim * rlr ),0.2f ); 974 std::vector < float > rinj = rbf16 ((size_t ) (rhc * rdim ),0.05f ); 975 976 std::vector < uint16_t > qd (rdown .size ()),qu (rup .size ()),qi (rinj .size ()); 977for (size_t i = 0 ;i < rdown .size ();++ i )qd [i ]= bf16_bits (rdown [i ]); 978for (size_t i = 0 ;i < rup .size ();++ i )qu [i ]= bf16_bits (rup [i ]); 979for (size_t i = 0 ;i < rinj .size ();++ i )qi [i ]= bf16_bits (rinj [i ]); 980 981float * dR = nullptr ,* dN = nullptr ,* dM = nullptr ,* dI = nullptr ; 982uint16_t * dD = nullptr ,* dU = nullptr ,* dJ = nullptr ; 983check (cudaMalloc (& dR ,rR .size ()* 4 ),"rR" ); 984check (cudaMalloc (& dN ,rnorm .size ()* 4 ),"rN" ); 985check (cudaMalloc (& dD ,qd .size ()* 2 ),"rD" ); 986check (cudaMalloc (& dU ,qu .size ()* 2 ),"rU" ); 987check (cudaMalloc (& dJ ,qi .size ()* 2 ),"rJ" ); 988check (cudaMalloc (& dM , (size_t )rn * 4 ),"rM" ); 989check (cudaMalloc (& dI , (size_t )rhc * 4 ),"rI" ); 990check (cudaMemcpy (dR ,rR .data (),rR .size ()* 4 ,cudaMemcpyHostToDevice ),"crR" ); 991check (cudaMemcpy (dN ,rnorm .data (),rnorm .size ()* 4 ,cudaMemcpyHostToDevice ),"crN" ); 992check (cudaMemcpy (dD ,qd .data (),qd .size ()* 2 ,cudaMemcpyHostToDevice ),"crD" ); 993check (cudaMemcpy (dU ,qu .data (),qu .size ()* 2 ,cudaMemcpyHostToDevice ),"crU" ); 994check (cudaMemcpy (dJ ,qi .data (),qi .size ()* 2 ,cudaMemcpyHostToDevice ),"crJ" ); 995 996const strata::kernels::GrShapes rsh {rn ,rhc ,rlr }; 997void * rws_raw = nullptr ; 998check (cudaMalloc (& rws_raw , strata::kernels::gr_workspace_bytes (rsh )),"m rws" ); 999 strata::kernels::GrWorkspace rws ; 1000 strata::kernels::gr_workspace_init (rsh ,rws_raw ,rws ); 1001for (int mode = 0 ;mode < 3 ;++ mode ) { 1002select_activation_mode (mode ); 1003 strata::kernels::gr_read (dR ,dN ,dD ,dU ,dJ ,eps ,rsh ,rws ,dM ,dI ,nullptr ); 1004 std::vector < float > gm ((size_t )rn ),gi ((size_t )rhc ); 1005check (cudaMemcpy (gm .data (),dM ,gm .size ()* 4 ,cudaMemcpyDeviceToHost ),"cgm" ); 1006check (cudaMemcpy (gi .data (),dI ,gi .size ()* 4 ,cudaMemcpyDeviceToHost ),"cgi" ); 1007 1008Opts precision ; 1009precision .round_activation = mode == 0 ; 1010 std::vector < float > wm ,wi ; 1011reference (rR ,rnorm ,rdown ,rup ,rinj ,eps ,rn ,rhc ,rlr ,precision ,wm ,wi ); 1012const double rm = rel_diff (wm ,gm ),ri = rel_diff (wi ,gi ); 1013const bool ok = rm <=1e-4 && ri <=1e-4 ; 1014 std::printf ("\n real dims 2560/4/320 %s %s (mixed %.3e, inject %.3e)\n" , 1015activation_mode_name (mode ),ok ?"pass" :"*** FAIL ***" ,rm ,ri ); 1016if (!ok )++ bad ; 1017 } 1018bad += fused_multi_lds_parity (dN ,dD ,dU ,dJ ,eps ); 1019select_activation_mode (0 ); 1020cudaFree (rws_raw ); 1021cudaFree (dR );cudaFree (dN );cudaFree (dD );cudaFree (dU );cudaFree (dJ );cudaFree (dM );cudaFree (dI ); 1022 } 1023 1024bad += scalar_activation_contract (); 1025 std::printf ("\ngr_read/gr_write: %d failures\n" ,bad ); 1026if (bad )return 1 ; 1027if (selftest ) std::printf ("gr_parity OK\n" ); 1028return 0 ; 1029}