okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
6ca60e9a0
main
1// src/kernels/gdn_parity.cpp - P2.S2's test for the GDN recurrence, the conv and the two norms. 2// 3// `ref/gdn.py` carries eleven PROPERTY checks, several of which exist because a rival reading of the SOURCE 4// produces well-formed output. The four that the kernels here can be wrong about are re-pinned below, each 5// asserted OBSERVABLE against a deliberately-wrong computation before the kernel is judged: 6// 7// 1. MODULO head pairing, h % h_k, not INTERLEAVE. With H_k=2 and H_v=4 the two give [0,1,0,1] and 8// [0,0,1,1]: they differ at h=1 and h=2. Both produce a full-rank state of the right shape. 9// 2. Decay applied BEFORE the rank-1 update. Applying it after is a one-line move that keeps every shape 10// and every magnitude. 11// 3. The conv's SLIDE DIRECTION. `kernel[0]` must read the OLDEST state row and the new input must land in 12// the LAST state row; reversing either is invisible on a symmetric state, so the state rows here are 13// labelled so each position is identifiable. 14// 4. `l2_norm`'s `+ eps` is an absolute floor on the SQUARED NORM, not on the mean. The mean reading 15// differs by sqrt(S) = 11.3x at S = 128. `ref/gdn.py` PROPERTY 1 instruments the derivation, so it is 16// not taken on trust there either. 17// 18// It also checks the STATE LAYOUT, which is the one place these kernels intentionally differ from the 19// reference: (S, h_v, S) with j fastest instead of (S, S, h_v). A layout mix-up is silent, so the state is 20// filled with a value that encodes its own coordinates. 21#include "strata/kernels/gdn.hpp" 22#include "strata/kernels/fused_gdn.hpp" 23#include "strata/kernels/verify_kernels.hpp" 24#include "strata/kernels/native_mmvq.hpp" 25 26#include <cuda_runtime.h> 27 28#include <cmath> 29#include <cstdio> 30#include <cstdlib> 31#include <cstring> 32#include <random> 33#include <string> 34#include <vector> 35 36namespace { 37 38void check (cudaError_t e ,const char * what ) { 39if (e != cudaSuccess ) { 40 std::fprintf (stderr ,"%s: %s\n" ,what ,cudaGetErrorString (e )); 41 std::exit (1 ); 42 } 43} 44 45float sigmoid_f (float x ) {return 1.0f / (1.0f + std::exp (- x )); } 46 47/// The reference's `gdn_step`, with switches for the rival readings. 48/// 49/// `state` is passed in the REFERENCE layout (S, S, h_v) - i.e. (i, j, h) - so that this function is a 50/// transcription of `ref/gdn.py` and not of the kernel. 51void ref_step (std::vector < double >& state ,const std::vector < float >& q ,const std::vector < float >& k , 52const std::vector < float >& v ,const std::vector < float >& gate ,const std::vector < float >& beta , 53int S ,int h_k ,int h_v , std::vector < float >& o ,bool modulo_heads ,bool decay_first ) { 54auto at = [& ](int i ,int j ,int h )-> double & {return state [(size_t ) (i * S + j )* h_v + h ]; }; 55 std::vector < float > oo ((size_t )h_v * S ,0.0f ); 56for (int h = 0 ;h < h_v ;++ h ) { 57const int src = modulo_heads ? (h %h_k ) : (h / (h_v /h_k )); 58const double dec = std::exp ((double )gate [(size_t )h ]); 59const double b = (double )beta [(size_t )h ]; 60for (int j = 0 ;j < S ;++ j ) { 61if (decay_first )for (int i = 0 ;i < S ;++ i )at (i ,j ,h ) *=dec ; 62double sk = 0 ; 63for (int i = 0 ;i < S ;++ i )sk += at (i ,j ,h )* (double )k [(size_t ) (src * S + i )]; 64const double d = ((double )v [(size_t ) (h * S + j )]- sk )* b ; 65for (int i = 0 ;i < S ;++ i )at (i ,j ,h )+= (double )k [(size_t ) (src * S + i )]* d ; 66if (!decay_first )for (int i = 0 ;i < S ;++ i )at (i ,j ,h ) *=dec ; 67double dot = 0 ; 68for (int i = 0 ;i < S ;++ i )dot += at (i ,j ,h )* (double )q [(size_t ) (src * S + i )]; 69oo [(size_t ) (h * S + j )]= (float )dot ; 70 } 71 } 72o = oo ; 73} 74 75/// `ref/gdn.py::conv_step` in its own (d_conv, C) row-major orientation. 76void ref_conv (std::vector < float >& cs ,const std::vector < float >& x ,const std::vector < float >& kern ,int C , 77int dc , std::vector < float >& out ) { 78out .assign ((size_t )C ,0.0f ); 79for (int c = 0 ;c < C ;++ c ) { 80double acc = 0 ; 81for (int i = 0 ;i < dc - 1 ;++ i )acc += (double )cs [(size_t ) (i * C + c )]* (double )kern [(size_t ) (i * C + c )]; 82acc += (double )x [(size_t )c ]* (double )kern [(size_t ) ((dc - 1 )* C + c )]; 83out [(size_t )c ]= (float )acc ; 84for (int i = 0 ;i < dc - 2 ;++ i )cs [(size_t ) (i * C + c )]= cs [(size_t ) ((i + 1 )* C + c )]; 85cs [(size_t ) ((dc - 2 )* C + c )]= x [(size_t )c ]; 86 } 87} 88 89double rel_l1 (const std::vector < float >& a ,const std::vector < float >& b ) { 90double d = 0 ,m = 0 ; 91for (size_t i = 0 ;i < a .size ();++ i ) { 92d += std::fabs ((double )a [i ]- (double )b [i ]); 93m += std::fabs ((double )a [i ]); 94 } 95return d / (m > 1e-30 ?m :1e-30 ); 96} 97 98}// namespace 99 100int main (int argc ,char ** argv ) { 101 std::setvbuf (stdout ,nullptr ,_IONBF ,0 );// (progress lines survive a crash) 102bool selftest = false; 103for (int i = 1 ;i < argc ;++ i ) { 104if (std::string (argv [i ])== "--selftest" )selftest = true; 105else { std::fprintf (stderr ,"usage: gdn_parity [--selftest]\n" );return 2 ; } 106 } 107 108const int S = 128 ,h_k = 16 ,h_v = 48 ;// the artifact's real geometry 109 std::mt19937 rng (2024 ); 110 std::normal_distribution < float > gauss (0.0f ,1.0f ); 111int bad = 0 ; 112 113// ================= 1. the recurrence ================= 114 { 115// The state encodes its own coordinates, so a layout mix-up cannot pass: reading (i,j,h) where the 116// buffer holds (i,h,j) gives a DIFFERENT number, not a wrong-but-plausible one. 117 std::vector < double > st_ref ((size_t )S * S * h_v ); 118 std::vector < float > st_dev ((size_t )S * h_v * S ); 119for (int i = 0 ;i < S ;++ i ) 120for (int j = 0 ;j < S ;++ j ) 121for (int h = 0 ;h < h_v ;++ h ) { 122const float val = (float ) (0.001 * (i + 2 * j + 3 * h )- 1.0 ); 123st_ref [(size_t ) (i * S + j )* h_v + h ]= val ; 124st_dev [(size_t ) (i * h_v + h )* S + j ]= val ;// (S, h_v, S), j fastest 125 } 126 127 std::vector < float > q ((size_t )h_k * S ),k ((size_t )h_k * S ),v ((size_t )h_v * S ),gate ((size_t )h_v ), 128beta ((size_t )h_v ); 129for (auto & x :q )x = gauss (rng )* 0.1f ; 130for (auto & x :k )x = gauss (rng )* 0.1f ; 131for (auto & x :v )x = gauss (rng ); 132for (auto & x :gate )x = - (2.0f + 4.0f * (float ) (rng () %100 ) /100.0f );// dec in (0.0025, 0.135] 133for (auto & x :beta )x = (float ) (rng () %100 ) /100.0f ; 134 135 std::vector < float > want_o ; 136 std::vector < double > st_mod = st_ref ; 137ref_step (st_mod ,q ,k ,v ,gate ,beta ,S ,h_k ,h_v ,want_o , true, true); 138 139// the traps 140 { 141 std::vector < double > a = st_ref ; 142 std::vector < float > oo ; 143ref_step (a ,q ,k ,v ,gate ,beta ,S ,h_k ,h_v ,oo , false, true); 144const double rel = rel_l1 (want_o ,oo ); 145 std::printf (" %-42s %-4s (%.2f%% apart)\n" ,"modulo vs interleave head pairing" , 146rel > 0.05 ?"yes" :"*** NO ***" ,rel * 100 ); 147if (!(rel > 0.05 ))++ bad ; 148 } 149 { 150 std::vector < double > a = st_ref ; 151 std::vector < float > oo ; 152ref_step (a ,q ,k ,v ,gate ,beta ,S ,h_k ,h_v ,oo , true, false); 153const double rel = rel_l1 (want_o ,oo ); 154 std::printf (" %-42s %-4s (%.2f%% apart)\n" ,"decay before vs after the update" , 155rel > 0.05 ?"yes" :"*** NO ***" ,rel * 100 ); 156// The two readings differ by exactly `k (x) (beta * v * (1 - dec))`, so the fixture is only able 157// to see the trap when `dec` is far from 1 and `beta` is far from 0. The first version used 158// gate in [-0.99, 0] - dec in [0.37, 1] - and managed 3.25%, which is a threshold the wrong 159// reading nearly clears. Deep decay is also the realistic regime: `ssm_a = -exp(A_log) < 0` and 160// softplus is positive, so gate is comfortably negative in the real model. 161if (!(rel > 0.05 ))++ bad ; 162 } 163 164// the kernel 165float * d_st = nullptr ,* d_q = nullptr ,* d_k = nullptr ,* d_v = nullptr ,* d_g = nullptr ,* d_b = nullptr , 166* d_o = nullptr ; 167check (cudaMalloc (& d_st ,st_dev .size ()* 4 ),"st" ); 168check (cudaMalloc (& d_q ,q .size ()* 4 ),"q" ); 169check (cudaMalloc (& d_k ,k .size ()* 4 ),"k" ); 170check (cudaMalloc (& d_v ,v .size ()* 4 ),"v" ); 171check (cudaMalloc (& d_g ,gate .size ()* 4 ),"g" ); 172check (cudaMalloc (& d_b ,beta .size ()* 4 ),"b" ); 173check (cudaMalloc (& d_o , (size_t )h_v * S * 4 ),"o" ); 174check (cudaMemcpy (d_st ,st_dev .data (),st_dev .size ()* 4 ,cudaMemcpyHostToDevice ),"cst" ); 175check (cudaMemcpy (d_q ,q .data (),q .size ()* 4 ,cudaMemcpyHostToDevice ),"cq" ); 176check (cudaMemcpy (d_k ,k .data (),k .size ()* 4 ,cudaMemcpyHostToDevice ),"ck" ); 177check (cudaMemcpy (d_v ,v .data (),v .size ()* 4 ,cudaMemcpyHostToDevice ),"cv" ); 178check (cudaMemcpy (d_g ,gate .data (),gate .size ()* 4 ,cudaMemcpyHostToDevice ),"cg" ); 179check (cudaMemcpy (d_b ,beta .data (),beta .size ()* 4 ,cudaMemcpyHostToDevice ),"cb" ); 180 181 strata::kernels::GdnShapes sh {S ,h_k ,h_v }; 182 strata::kernels::gdn_step (d_st ,d_q ,d_k ,d_v ,d_g ,d_b ,d_o ,sh ,nullptr ); 183 184 std::vector < float > got_o ((size_t )h_v * S ),got_st (st_dev .size ()); 185check (cudaMemcpy (got_o .data (),d_o ,got_o .size ()* 4 ,cudaMemcpyDeviceToHost ),"cgo" ); 186check (cudaMemcpy (got_st .data (),d_st ,got_st .size ()* 4 ,cudaMemcpyDeviceToHost ),"cgst" ); 187 188// o, and the STATE read back through the declared layout 189const double rel_o = rel_l1 (want_o ,got_o ); 190double rel_st = 0 ,mag_st = 0 ; 191for (int i = 0 ;i < S ;++ i ) 192for (int j = 0 ;j < S ;++ j ) 193for (int h = 0 ;h < h_v ;++ h ) { 194const size_t dev = (size_t ) (i * h_v + h )* S + j ; 195const double w = st_mod [(size_t ) (i * S + j )* h_v + h ]; 196rel_st += std::fabs (w - (double )got_st [dev ]); 197mag_st += std::fabs (w ); 198 } 199rel_st /= (mag_st > 1e-30 ?mag_st :1e-30 ); 200 std::printf ("\n %-42s rel %.3e\n" ,"o vs reference" ,rel_o ); 201 std::printf (" %-42s rel %.3e\n" ,"state (read via (S,h_v,S)) vs reference" ,rel_st ); 202// The kernel accumulates in f32 and the reference in f64, and the state is a RECURRENCE over 128 203// terms, so 1e-5 is the arithmetic's floor here and not a structural allowance. 204if (!(rel_o <=1e-5 )) { std::printf (" *** o over 1e-5 ***\n" );++ bad ; } 205if (!(rel_st <=1e-5 )) { std::printf (" *** state over 1e-5 ***\n" );++ bad ; } 206 207// a zero state stays zero, and a zero gate/beta leaves the state untouched (the reference's 208// PROPERTY 4 setup) - a cheap independent invariant that no layout can fake 209 { 210 std::vector < float > zero (st_dev .size (),0.0f ),zg ((size_t )h_v ,0.0f ),zb ((size_t )h_v ,0.0f ); 211check (cudaMemcpy (d_st ,zero .data (),zero .size ()* 4 ,cudaMemcpyHostToDevice ),"z0" ); 212check (cudaMemcpy (d_g ,zg .data (),zg .size ()* 4 ,cudaMemcpyHostToDevice ),"z1" ); 213check (cudaMemcpy (d_b ,zb .data (),zb .size ()* 4 ,cudaMemcpyHostToDevice ),"z2" ); 214 strata::kernels::gdn_step (d_st ,d_q ,d_k ,d_v ,d_g ,d_b ,d_o ,sh ,nullptr ); 215 std::vector < float > zz (zero .size ()); 216check (cudaMemcpy (zz .data (),d_st ,zz .size ()* 4 ,cudaMemcpyDeviceToHost ),"z3" ); 217double nz = 0 ; 218for (float x :zz )nz += std::fabs (x ); 219const bool ok = nz == 0.0 ; 220 std::printf (" %-42s %s\n" ,"gate=beta=0 from a zero state is a no-op" ,ok ?"yes" :"*** NO ***" ); 221if (!ok )++ bad ; 222 } 223 224// ephemeral state: unchanged when the state is untouched but beta=1 — the delta rule writing k*d 225cudaFree (d_st );cudaFree (d_q );cudaFree (d_k );cudaFree (d_v );cudaFree (d_g );cudaFree (d_b );cudaFree (d_o ); 226 } 227 228// ================= 2. the conv ================= 229 { 230const int C = 24 ,dc = 4 ;// small, so the host reference is a plain loop 231// TWO DIFFERENT LAYOUTS MEET HERE, and this is where the first version of this test failed at 0.136. 232// `ref/gdn.py` carries the conv state as (d_conv-1, C) ROW-MAJOR - (i, c) at i*C + c - because it is 233// numpy. The kernel carries it as (C, d_conv-1) with c fastest - c*(dc-1) + i - because that is 234// ggml's `ne = {d_conv-1, C}` with ne0 fast, and it is a runtime buffer so either is legal. The 235// mapping between them is written out below rather than assumed, in both directions. 236 std::vector < float > cs_ref ((size_t ) (dc - 1 )* C ),x ((size_t )C ),kern ((size_t )dc * C ); 237for (int i = 0 ;i < dc - 1 ;++ i ) 238for (int c = 0 ;c < C ;++ c )cs_ref [(size_t ) (i * C + c )]= (float ) (10 * (i + 1 )+ c );// labelled 239for (int c = 0 ;c < C ;++ c )x [(size_t )c ]= (float ) (100 + c ); 240for (size_t i = 0 ;i < kern .size ();++ i )kern [i ]= gauss (rng ); 241 242auto to_dev_layout = [& ](const std::vector < float >& r ) { 243 std::vector < float > d (r .size ()); 244for (int i = 0 ;i < dc - 1 ;++ i ) 245for (int c = 0 ;c < C ;++ c )d [(size_t ) (c * (dc - 1 )+ i )]= r [(size_t ) (i * C + c )]; 246return d ; 247 }; 248auto to_ref_layout = [& ](const std::vector < float >& d ) { 249 std::vector < float > r (d .size ()); 250for (int i = 0 ;i < dc - 1 ;++ i ) 251for (int c = 0 ;c < C ;++ c )r [(size_t ) (i * C + c )]= d [(size_t ) (c * (dc - 1 )+ i )]; 252return r ; 253 }; 254 255 std::vector < float > cs = to_dev_layout (cs_ref ); 256 std::vector < float > want ; 257 std::vector < float > cs_want_ref = cs_ref ; 258ref_conv (cs_want_ref ,x ,kern ,C ,dc ,want ); 259const std::vector < float > cs_want = to_dev_layout (cs_want_ref ); 260 261// THE LAYOUT: the manifest's `ssm_conv1d.weight` has ne = [d_conv, C], so ne0 = d_conv is fast. 262// The kernel takes `kW[c*dc + i]`; the reference's `kern` is (dc, C) row-major. The transcription 263// below is the ONLY place the two meet. 264 std::vector < float > kW ((size_t )C * dc ); 265for (int c = 0 ;c < C ;++ c ) 266for (int i = 0 ;i < dc ;++ i )kW [(size_t ) (c * dc + i )]= kern [(size_t ) (i * C + c )]; 267// and the rival reading, which must differ or the fixture cannot see a transposed weight 268 std::vector < float > kW_wrong ((size_t )C * dc ); 269for (size_t i = 0 ;i < kern .size ();++ i )kW_wrong [i ]= kern [i ]; 270 { 271// apply the WRONG layout by hand: tap i of channel c is kW[c*dc+i] read as kW[i*C+c] 272 std::vector < float > cs2 = cs ; 273 std::vector < float > out_bad ((size_t )C ,0.0f ); 274for (int c = 0 ;c < C ;++ c ) { 275double a = 0 ; 276for (int i = 0 ;i < dc - 1 ;++ i )a += (double )cs2 [(size_t ) (i * C + c )]* (double )kW_wrong [(size_t ) (c * dc + i )]; 277a += (double )x [(size_t )c ]* (double )kW_wrong [(size_t ) (c * dc + dc - 1 )]; 278out_bad [(size_t )c ]= (float )a ; 279 } 280const double rel = rel_l1 (want ,out_bad ); 281 std::printf ("\n %-42s %-4s (%.1f%% apart)\n" ,"conv weight layout is observable" , 282rel > 0.05 ?"yes" :"*** NO ***" ,rel * 100 ); 283if (!(rel > 0.05 ))++ bad ; 284 } 285 286float * d_cs = nullptr ,* d_x = nullptr ,* d_w = nullptr ,* d_o = nullptr ; 287check (cudaMalloc (& d_cs ,cs .size ()* 4 ),"cs" ); 288check (cudaMalloc (& d_x ,x .size ()* 4 ),"cx" ); 289check (cudaMalloc (& d_w ,kW .size ()* 4 ),"cw" ); 290check (cudaMalloc (& d_o , (size_t )C * 4 ),"co" ); 291check (cudaMemcpy (d_cs ,cs .data (),cs .size ()* 4 ,cudaMemcpyHostToDevice ),"ccs" ); 292check (cudaMemcpy (d_x ,x .data (),x .size ()* 4 ,cudaMemcpyHostToDevice ),"ccx" ); 293check (cudaMemcpy (d_w ,kW .data (),kW .size ()* 4 ,cudaMemcpyHostToDevice ),"ccw" ); 294 strata::kernels::gdn_conv_step (d_cs ,d_x ,d_w ,d_o ,C ,dc ,nullptr ); 295 std::vector < float > got ((size_t )C ),got_cs (cs .size ()); 296check (cudaMemcpy (got .data (),d_o ,got .size ()* 4 ,cudaMemcpyDeviceToHost ),"cgo" ); 297check (cudaMemcpy (got_cs .data (),d_cs ,got_cs .size ()* 4 ,cudaMemcpyDeviceToHost ),"cgs" ); 298const double rel_out = rel_l1 (want ,got ),rel_cs = rel_l1 (cs_want ,got_cs ); 299 std::printf (" %-42s rel %.3e\n" ,"conv out vs reference" ,rel_out ); 300 std::printf (" %-42s rel %.3e\n" ,"conv state slide (mapped) vs reference" ,rel_cs ); 301if (!(rel_out <=1e-6 )) { std::printf (" *** conv out ***\n" );++ bad ; } 302if (!(rel_cs <=1e-6 )) { std::printf (" *** conv state ***\n" );++ bad ; } 303cudaFree (d_cs );cudaFree (d_x );cudaFree (d_w );cudaFree (d_o ); 304 } 305 306// ================= 3. l2_norm ================= 307 { 308const int rows = 16 ,cols = 128 ; 309 std::vector < float > x ((size_t )rows * cols ),x_ref ; 310for (auto & val :x )val = gauss (rng )* 3.0f ; 311x_ref = x ; 312const float eps = 1e-6f ; 313for (int r = 0 ;r < rows ;++ r ) { 314double acc = 0 ; 315for (int i = 0 ;i < cols ;++ i )acc += (double )x_ref [(size_t ) (r * cols + i )]* (double )x_ref [(size_t ) (r * cols + i )]; 316const float inv = (float ) (1.0 / std::sqrt (acc + (double )eps )); 317for (int i = 0 ;i < cols ;++ i )x_ref [(size_t ) (r * cols + i )] *=inv ; 318 } 319// The rival reading puts THE SAME eps on the MEAN instead of on the squared norm, which at 320// ||x||^2 ~ 1152 and cols = 128 changes the divisor from sqrt(1152) to sqrt(9) - a factor of 11.3, so 321// the trap is large here. It would be a much smaller correction on a near-zero row, where the floor 322// is the whole answer; that regime is not what this fixture exercises. 323 std::vector < float > x_mean = x ; 324for (int r = 0 ;r < rows ;++ r ) { 325double acc = 0 ; 326for (int i = 0 ;i < cols ;++ i )acc += (double )x_mean [(size_t ) (r * cols + i )]* (double )x_mean [(size_t ) (r * cols + i )]; 327const float inv = (float ) (1.0 / std::sqrt (acc /cols + (double )eps )); 328for (int i = 0 ;i < cols ;++ i )x_mean [(size_t ) (r * cols + i )] *=inv ; 329 } 330const double rel_trap = rel_l1 (x_ref ,x_mean ); 331 std::printf ("\n %-42s %-4s (%.1f%% apart)\n" ,"eps on sum vs on mean is observable" , 332rel_trap > 0.05 ?"yes" :"*** NO ***" ,rel_trap * 100 ); 333if (!(rel_trap > 0.05 ))++ bad ; 334 335float * d_x = nullptr ; 336check (cudaMalloc (& d_x ,x .size ()* 4 ),"lx" ); 337check (cudaMemcpy (d_x ,x .data (),x .size ()* 4 ,cudaMemcpyHostToDevice ),"lcx" ); 338 strata::kernels::gdn_l2_norm (d_x ,rows ,cols ,eps ,nullptr ); 339 std::vector < float > got (x .size ()); 340check (cudaMemcpy (got .data (),d_x ,got .size ()* 4 ,cudaMemcpyDeviceToHost ),"lcg" ); 341const double rel = rel_l1 (x_ref ,got ); 342 std::printf (" %-42s rel %.3e\n" ,"l2_norm vs reference" ,rel ); 343if (!(rel <=1e-6 ))++ bad ; 344cudaFree (d_x ); 345 } 346 347// ================= 4. the closing norm ================= 348 { 349const int S2 = 128 ,hv2 = 8 ; 350 std::vector < float > o ((size_t )hv2 * S2 ),z ((size_t )hv2 * S2 ),sn ((size_t )S2 ),y_ref ((size_t )hv2 * S2 ); 351for (auto & val :o )val = gauss (rng ); 352for (auto & val :z )val = gauss (rng ); 353for (auto & val :sn )val = 1.0f + 0.1f * gauss (rng ); 354const float eps = 1e-6f ; 355for (int h = 0 ;h < hv2 ;++ h ) { 356double acc = 0 ; 357for (int i = 0 ;i < S2 ;++ i )acc += (double )o [(size_t ) (h * S2 + i )]* (double )o [(size_t ) (h * S2 + i )]; 358const float inv = (float ) (1.0 / std::sqrt (acc /S2 + (double )eps )); 359for (int i = 0 ;i < S2 ;++ i ) 360y_ref [(size_t ) (h * S2 + i )]= o [(size_t ) (h * S2 + i )]* inv * sn [(size_t )i ]* 361sigmoid_f (z [(size_t ) (h * S2 + i )]); 362 } 363float * d_o = nullptr ,* d_z = nullptr ,* d_sn = nullptr ,* d_y = nullptr ; 364check (cudaMalloc (& d_o ,o .size ()* 4 ),"no" ); 365check (cudaMalloc (& d_z ,z .size ()* 4 ),"nz" ); 366check (cudaMalloc (& d_sn ,sn .size ()* 4 ),"ns" ); 367check (cudaMalloc (& d_y ,y_ref .size ()* 4 ),"ny" ); 368check (cudaMemcpy (d_o ,o .data (),o .size ()* 4 ,cudaMemcpyHostToDevice ),"nco" ); 369check (cudaMemcpy (d_z ,z .data (),z .size ()* 4 ,cudaMemcpyHostToDevice ),"ncz" ); 370check (cudaMemcpy (d_sn ,sn .data (),sn .size ()* 4 ,cudaMemcpyHostToDevice ),"ncs" ); 371 strata::kernels::gdn_out_norm (d_o ,d_z ,d_sn ,d_y ,hv2 ,S2 ,eps ,nullptr ); 372 std::vector < float > got (y_ref .size ()); 373check (cudaMemcpy (got .data (),d_y ,got .size ()* 4 ,cudaMemcpyDeviceToHost ),"ncy" ); 374const double rel = rel_l1 (y_ref ,got ); 375 std::printf ("\n %-42s rel %.3e\n" ,"out norm (rms * ssm_norm * sigmoid z)" ,rel ); 376if (!(rel <=1e-6 ))++ bad ; 377 378// SiLU instead of sigmoid is the qwen3.5 reading; `ref/gdn.py` says this artifact does NOT use it 379 std::vector < float > y_silu (y_ref .size ()); 380for (int h = 0 ;h < hv2 ;++ h ) { 381double acc = 0 ; 382for (int i = 0 ;i < S2 ;++ i )acc += (double )o [(size_t ) (h * S2 + i )]* (double )o [(size_t ) (h * S2 + i )]; 383const float inv = (float ) (1.0 / std::sqrt (acc /S2 + (double )eps )); 384for (int i = 0 ;i < S2 ;++ i ) { 385const float zz = z [(size_t ) (h * S2 + i )]; 386y_silu [(size_t ) (h * S2 + i )]= 387o [(size_t ) (h * S2 + i )]* inv * sn [(size_t )i ]* (zz / (1.0f + std::exp (- zz ))); 388 } 389 } 390const double rel_silu = rel_l1 (y_ref ,y_silu ); 391 std::printf (" %-42s %-4s (%.2f%% apart)\n" ,"sigmoid vs SiLU on the gate is observable" , 392rel_silu > 0.05 ?"yes" :"*** NO ***" ,rel_silu * 100 ); 393if (!(rel_silu > 0.05 ))++ bad ; 394cudaFree (d_o );cudaFree (d_z );cudaFree (d_sn );cudaFree (d_y ); 395 } 396 397// ================= 5. the verify-window kernels, T = 1..8, bitwise against the single-token kernels ================= 398// gdn_ab_multi and gdn_step_norm_multi are documented as bitwise `fused_gdn_ab` / `fused_gdn_step_norm` per 399// token (verify_kernels.hpp). Every T from 1 to kVerifyMaxT is its own exact-T instantiation, so each is run, 400// and every output and the state are compared with memcmp (not a tolerance). 401 { 402const int n_embd = 2560 ,qk = S * h_k ,C = 2 * qk + S * h_v ,vd = S * h_v ; 403const float eps = 1e-6f ; 404auto bf16_of = [](float f ) { 405uint32_t u ; 406 std::memcpy (& u ,& f ,4 ); 407return (uint16_t ) (u >>16 ); 408 }; 409 std::vector < uint16_t > wa ((size_t )h_v * n_embd ),wb ((size_t )h_v * n_embd ); 410for (auto & w :wa )w = bf16_of (gauss (rng )* 0.05f ); 411for (auto & w :wb )w = bf16_of (gauss (rng )* 0.05f ); 412 std::vector < float > dt ((size_t )h_v ),ssm_a ((size_t )h_v ),gamma ((size_t )S ); 413for (auto & x :dt )x = gauss (rng ); 414for (auto & x :ssm_a )x = - (0.1f + (float ) (rng () %100 ) /50.0f ); 415for (auto & x :gamma )x = 1.0f + 0.1f * gauss (rng ); 416 std::vector < float > st0 ((size_t )S * h_v * S ); 417for (auto & x :st0 )x = gauss (rng )* 0.1f ; 418 419auto dev = [& ](size_t bytes ) { 420void * p = nullptr ; 421check (cudaMalloc (& p ,bytes ),"m5" ); 422return p ; 423 }; 424auto up = [& ](const void * h ,size_t bytes ) { 425void * p = dev (bytes ); 426check (cudaMemcpy (p ,h ,bytes ,cudaMemcpyHostToDevice ),"u5" ); 427return p ; 428 }; 429auto down = [& ](std::vector < float >& h ,const void * d ) { 430check (cudaMemcpy (h .data (),d ,h .size ()* 4 ,cudaMemcpyDeviceToHost ),"d5" ); 431 }; 432auto d_wa = (uint16_t * )up (wa .data (),wa .size ()* 2 ); 433auto d_wb = (uint16_t * )up (wb .data (),wb .size ()* 2 ); 434auto d_dt = (float * )up (dt .data (),dt .size ()* 4 ); 435auto d_sa = (float * )up (ssm_a .data (),ssm_a .size ()* 4 ); 436auto d_gm = (float * )up (gamma .data (),gamma .size ()* 4 ); 437int ab_bad = 0 ,sn_bad = 0 ,ab_n = 0 ,sn_n = 0 ; 438for (int T = 1 ;T <= strata::kernels::kVerifyMaxT ;++ T ) { 439// --- alpha/beta 440 std::vector < float > x ((size_t )T * n_embd ); 441for (auto & v :x )v = gauss (rng ); 442auto d_x = (float * )up (x .data (),x .size ()* 4 ); 443auto d_g = (float * )dev ((size_t )T * h_v * 4 ),d_b = (float * )dev ((size_t )T * h_v * 4 ); 444auto d_gr = (float * )dev ((size_t )T * h_v * 4 ),d_br = (float * )dev ((size_t )T * h_v * 4 ); 445 strata::kernels::gdn_ab_multi (d_x ,d_wa ,d_wb ,d_dt ,d_sa ,d_g ,d_b ,n_embd ,h_v ,T ,nullptr ); 446for (int t = 0 ;t < T ;++ t ) 447 strata::kernels::fused_gdn_ab (d_x + (size_t )t * n_embd ,d_wa ,d_wb ,d_dt ,d_sa ,d_gr + (size_t )t * h_v , 448d_br + (size_t )t * h_v ,n_embd ,h_v ,nullptr ); 449 std::vector < float > g ((size_t )T * h_v ),b (g .size ()),gr (g .size ()),br (g .size ()); 450down (g ,d_g );down (b ,d_b );down (gr ,d_gr );down (br ,d_br ); 451++ ab_n ; 452if (std::memcmp (g .data (),gr .data (),g .size ()* 4 )!= 0 || std::memcmp (b .data (),br .data (),b .size ()* 4 )!= 0 ) { 453 std::printf (" *** gdn_ab_multi T=%d differs from fused_gdn_ab (memcmp) ***\n" ,T ); 454++ ab_bad ; 455 } 456cudaFree (d_x );cudaFree (d_g );cudaFree (d_b );cudaFree (d_gr );cudaFree (d_br ); 457 458// --- the recurrence + output norm 459 std::vector < float > h ((size_t )T * C ),gate ((size_t )T * h_v ),beta ((size_t )T * h_v ),z ((size_t )T * vd ); 460for (auto & v :h )v = gauss (rng )* 0.1f ; 461for (auto & v :gate )v = - (0.5f + 3.0f * (float ) (rng () %100 ) /100.0f ); 462for (auto & v :beta )v = (float ) (rng () %100 ) /100.0f ; 463for (auto & v :z )v = gauss (rng ); 464auto d_h = (float * )up (h .data (),h .size ()* 4 ); 465auto d_gate = (float * )up (gate .data (),gate .size ()* 4 ); 466auto d_beta = (float * )up (beta .data (),beta .size ()* 4 ); 467auto d_z = (float * )up (z .data (),z .size ()* 4 ); 468auto d_y = (float * )dev ((size_t )T * vd * 4 ); 469auto d_yr = (float * )dev ((size_t )T * vd * 4 ); 470auto d_st = (float * )dev (st0 .size ()* 4 ); 471auto d_sr = (float * )dev (st0 .size ()* 4 ); 472int32_t * d_nk = (int32_t * )dev (4 ); 473// the reference chain (the single-token kernel), n tokens, into d_sr / d_yr 474auto run_ref = [& ](int n ) { 475check (cudaMemcpy (d_sr ,st0 .data (),st0 .size ()* 4 ,cudaMemcpyHostToDevice ),"r0" ); 476for (int t = 0 ;t < n ;++ t ) 477 strata::kernels::fused_gdn_step_norm (d_sr ,d_h + (size_t )t * C ,d_h + (size_t )t * C + qk , 478d_h + (size_t )t * C + 2 * qk ,d_gate + (size_t )t * h_v , 479d_beta + (size_t )t * h_v ,d_z + (size_t )t * vd ,d_gm ,eps , 480d_yr + (size_t )t * vd ,h_k ,h_v ,nullptr ); 481 }; 482 std::vector < float > y ((size_t )T * vd ),yr (y .size ()),st (st0 .size ()),sr (st0 .size ()); 483// (name, n_keep tokens or -1 = verify half, t_out_begin) 484struct Case {const char * name ;int n_keep ;int tb ; }; 485 std::vector < Case > cases = {{"verify" ,-1 ,0 }, {"verify tb" ,-1 ,T > 1 ?T /2 :0 }, {"commit" ,T ,0 }, 486 {"commit tb" ,T ,T /2 }, {"commit state only" ,T ,T }}; 487if (T > 1 )cases .push_back ({"commit n-1" ,T - 1 ,T - 1 }); 488for (const Case & c :cases ) { 489check (cudaMemcpy (d_st ,st0 .data (),st0 .size ()* 4 ,cudaMemcpyHostToDevice ),"s0" ); 490check (cudaMemset (d_y ,0 , (size_t )T * vd * 4 ),"y0" ); 491check (cudaMemset (d_yr ,0 , (size_t )T * vd * 4 ),"yr0" ); 492const int n = c .n_keep < 0 ?T :c .n_keep ; 493if (c .n_keep >=0 )check (cudaMemcpy (d_nk ,& c .n_keep ,4 ,cudaMemcpyHostToDevice ),"nk" ); 494 strata::kernels::gdn_step_norm_multi (d_st ,d_h ,C ,d_gate ,d_beta ,d_z ,d_gm ,eps ,d_y ,h_k ,h_v ,T , 495c .n_keep >=0 ?d_nk :nullptr ,nullptr ,c .tb ); 496run_ref (n ); 497down (y ,d_y );down (yr ,d_yr );down (st ,d_st );down (sr ,d_sr ); 498bool ok = true; 499size_t y_bad = 0 ,y_all = 0 ; 500for (int t = c .tb ;t < n ;++ t ) { 501for (int i = 0 ;i < vd ;++ i ,++ y_all ) 502if (std::memcmp (& y [(size_t )t * vd + i ],& yr [(size_t )t * vd + i ],4 )!= 0 )++ y_bad ; 503 } 504if (y_bad )ok = false; 505// the verify half leaves the state untouched; the commit half writes the state after n tokens 506const std::vector < float >& want_st = c .n_keep < 0 ?st0 :sr ; 507if (std::memcmp (st .data (),want_st .data (),st .size ()* 4 )!= 0 )ok = false; 508++ sn_n ; 509// #3948 (STRATA_QFUSE): the same call with a q8_1 destination also writes the outputs' q8_1 image, row t - tb 510// at (t - tb) * value_dim: the bytes native_quantize_q8_1 makes of y rows [tb, n), the outputs and the state 511// unchanged by asking for it, and nothing written past those rows (the engine's second group has tb > 0) 512if (c .tb < n ) { 513if (std::getenv ("STRATA_GDN_PARITY_TRACE" )) std::printf (" trace: T=%d '%s' with a q8_1 destination\n" ,T ,c .name ); 514const size_t qb = (size_t ) (vd /32 )* 36 ,rows = (size_t ) (n - c .tb ); 515void * d_q = dev ((size_t )T * qb ),* d_qr = dev ((size_t )T * qb ); 516check (cudaMemset (d_q ,0x5a , (size_t )T * qb ),"q0" ); 517check (cudaMemcpy (d_st ,st0 .data (),st0 .size ()* 4 ,cudaMemcpyHostToDevice ),"s1" ); 518 std::vector < float > y2 (y .size ()),st2 (st0 .size ()); 519check (cudaMemset (d_y ,0 , (size_t )T * vd * 4 ),"y1" ); 520 strata::kernels::gdn_step_norm_multi (d_st ,d_h ,C ,d_gate ,d_beta ,d_z ,d_gm ,eps ,d_y ,h_k ,h_v ,T , 521c .n_keep >=0 ?d_nk :nullptr ,nullptr ,c .tb ,d_q ); 522static cudaStream_t qstream = nullptr ;// (native_quantize_q8_1 refuses the null stream; a blocking stream orders with the legacy one) 523if (qstream == nullptr )check (cudaStreamCreate (& qstream ),"qstream" ); 524 strata::kernels::native_quantize_q8_1 (d_y + (size_t )c .tb * vd ,d_qr ,vd , (int )rows ,qstream ); 525check (cudaDeviceSynchronize (),"q sync" ); 526 std::vector < uint8_t > hq ((size_t )T * qb ),hr ((size_t )T * qb ); 527check (cudaMemcpy (hq .data (),d_q ,hq .size (),cudaMemcpyDeviceToHost ),"q read" ); 528check (cudaMemcpy (hr .data (),d_qr ,rows * qb ,cudaMemcpyDeviceToHost ),"qr read" ); 529down (y2 ,d_y );down (st2 ,d_st ); 530bool q_ok = std::memcmp (hq .data (),hr .data (),rows * qb )== 0 ; 531for (size_t i = rows * qb ;q_ok && i < hq .size ();++ i )q_ok = hq [i ]== 0x5a ; 532for (int t = c .tb ;q_ok && t < n ;++ t ) 533q_ok = std::memcmp (& y2 [(size_t )t * vd ],& y [(size_t )t * vd ], (size_t )vd * 4 )== 0 ; 534q_ok = q_ok && std::memcmp (st2 .data (),st .data (),st .size ()* 4 )== 0 ; 535if (!q_ok ) { 536 std::printf (" *** gdn_step_norm_multi T=%d '%s' with a q8_1 destination: bytes differ from native_quantize_q8_1 or outputs/state changed ***\n" ,T ,c .name ); 537++ sn_bad ; 538 } 539++ sn_n ; 540cudaFree (d_q );cudaFree (d_qr ); 541 } 542if (!ok ) { 543 std::printf (" *** gdn_step_norm_multi T=%d '%s' differs from fused_gdn_step_norm (memcmp): %zu of %zu y words%s ***\n" , 544T ,c .name ,y_bad ,y_all , 545 std::memcmp (st .data (), (c .n_keep < 0 ?st0 :sr ).data (),st .size ()* 4 )!= 0 ?", state too" :"" ); 546++ sn_bad ; 547 } 548 } 549cudaFree (d_h );cudaFree (d_gate );cudaFree (d_beta );cudaFree (d_z );cudaFree (d_y );cudaFree (d_yr );cudaFree (d_st ); 550cudaFree (d_sr );cudaFree (d_nk ); 551 } 552cudaFree (d_wa );cudaFree (d_wb );cudaFree (d_dt );cudaFree (d_sa );cudaFree (d_gm ); 553 std::printf ("\n %-42s %s (%d cases)\n" ,"gdn_ab_multi T=1..8 == fused_gdn_ab" ,ab_bad ?"*** NO ***" :"bitwise" ,ab_n ); 554 std::printf (" %-42s %s (%d cases)\n" ,"gdn_step_norm_multi T=1..8 == single-token" ,sn_bad ?"*** NO ***" :"bitwise" ,sn_n ); 555bad += ab_bad + sn_bad ; 556 } 557 558// ================= 6. the layer-batched commit (engine-latency), bitwise against the per-layer calls ================= 559// gdn_commit_layers runs every layer's conv-history commit and state commit in one launch each; the per-layer loop 560// (gdn_conv_commit + gdn_step_norm_multi with n_keep, no outputs) is the reference. Several layers with their own 561// state, rows and gate/beta; n_keep 1..T; memcmp on the whole state arena (states and conv histories). 562 { 563const int qk = S * h_k ,C = 2 * qk + S * h_v ,L = 5 ,MT = strata::kernels::kVerifyMaxT ; 564const int64_t conv_off = (int64_t )S * h_v * S ,stride = conv_off + (int64_t )C * 3 ; 565 std::vector < float > st0 ((size_t ) (stride * L )),hb ((size_t )L * MT * C ),qkv ((size_t )L * MT * C ), 566gt ((size_t )L * MT * h_v ),bt ((size_t )L * MT * h_v ),zdummy ((size_t )MT * S * h_v ),gm (S ,1.0f ); 567for (auto & x :st0 )x = gauss (rng )* 0.1f ; 568for (auto & x :hb )x = gauss (rng )* 0.1f ; 569for (auto & x :qkv )x = gauss (rng ); 570for (auto & x :gt )x = - (0.5f + (float ) (rng () %100 ) /50.0f ); 571for (auto & x :bt )x = (float ) (rng () %100 ) /100.0f ; 572float * d_a = nullptr ,* d_b = nullptr ,* d_h = nullptr ,* d_qkv = nullptr ,* d_g = nullptr ,* d_bt = nullptr , 573* d_z = nullptr ,* d_y = nullptr ,* d_gm = nullptr ; 574int32_t * d_nk = nullptr ; 575check (cudaMalloc (& d_a ,st0 .size ()* 4 ),"la" );check (cudaMalloc (& d_b ,st0 .size ()* 4 ),"lb" ); 576check (cudaMalloc (& d_h ,hb .size ()* 4 ),"lh" );check (cudaMalloc (& d_qkv ,qkv .size ()* 4 ),"lq" ); 577check (cudaMalloc (& d_g ,gt .size ()* 4 ),"lg" );check (cudaMalloc (& d_bt ,bt .size ()* 4 ),"lbt" ); 578check (cudaMalloc (& d_z ,zdummy .size ()* 4 ),"lz" );check (cudaMalloc (& d_y ,zdummy .size ()* 4 ),"ly" ); 579check (cudaMalloc (& d_gm ,gm .size ()* 4 ),"lgm" );check (cudaMalloc (& d_nk ,4 ),"lnk" ); 580check (cudaMemcpy (d_h ,hb .data (),hb .size ()* 4 ,cudaMemcpyHostToDevice ),"lch" ); 581check (cudaMemcpy (d_qkv ,qkv .data (),qkv .size ()* 4 ,cudaMemcpyHostToDevice ),"lcq" ); 582check (cudaMemcpy (d_g ,gt .data (),gt .size ()* 4 ,cudaMemcpyHostToDevice ),"lcg" ); 583check (cudaMemcpy (d_bt ,bt .data (),bt .size ()* 4 ,cudaMemcpyHostToDevice ),"lcb" ); 584check (cudaMemcpy (d_gm ,gm .data (),gm .size ()* 4 ,cudaMemcpyHostToDevice ),"lcm" ); 585check (cudaMemset (d_z ,0 ,zdummy .size ()* 4 ),"lz0" ); 586int lb_bad = 0 ,lb_n = 0 ; 587bool launched = true; 588for (int nk = 1 ;nk <=MT && launched ;++ nk ) { 589check (cudaMemcpy (d_nk ,& nk ,4 ,cudaMemcpyHostToDevice ),"lnk1" ); 590check (cudaMemcpy (d_a ,st0 .data (),st0 .size ()* 4 ,cudaMemcpyHostToDevice ),"lca" ); 591check (cudaMemcpy (d_b ,st0 .data (),st0 .size ()* 4 ,cudaMemcpyHostToDevice ),"lcb2" ); 592for (int l = 0 ;l < L ;++ l ) {// the reference: the per-layer calls 593float * stl = d_a + (size_t )l * stride ; 594 strata::kernels::gdn_conv_commit (stl + conv_off ,d_qkv + (size_t )l * MT * C ,C ,d_nk ,nullptr ); 595 strata::kernels::gdn_step_norm_multi (stl ,d_h + (size_t )l * MT * C ,C ,d_g + (size_t )l * MT * h_v , 596d_bt + (size_t )l * MT * h_v ,d_z ,d_gm ,1e-6f ,d_y ,h_k ,h_v ,MT ,d_nk , 597nullptr ,MT ); 598 } 599launched = strata::kernels::gdn_commit_layers (d_b ,stride ,conv_off ,d_qkv , (int64_t )MT * C ,d_h , (int64_t )MT * C , 600d_g ,d_bt , (int64_t )MT * h_v ,C ,h_k ,h_v ,MT ,L ,d_nk ,nullptr ); 601if (!launched )break ; 602check (cudaDeviceSynchronize (),"lsync" ); 603 std::vector < float > a (st0 .size ()),b (st0 .size ()); 604check (cudaMemcpy (a .data (),d_a ,a .size ()* 4 ,cudaMemcpyDeviceToHost ),"lda" ); 605check (cudaMemcpy (b .data (),d_b ,b .size ()* 4 ,cudaMemcpyDeviceToHost ),"ldb" ); 606++ lb_n ; 607if (std::memcmp (a .data (),b .data (),a .size ()* 4 )!= 0 || std::memcmp (a .data (),st0 .data (),a .size ()* 4 )== 0 ) { 608 std::printf (" *** gdn_commit_layers n_keep=%d differs from the per-layer commit (or wrote nothing) ***\n" ,nk ); 609++ lb_bad ; 610 } 611 } 612cudaFree (d_a );cudaFree (d_b );cudaFree (d_h );cudaFree (d_qkv );cudaFree (d_g );cudaFree (d_bt );cudaFree (d_z ); 613cudaFree (d_y );cudaFree (d_gm );cudaFree (d_nk ); 614if (launched ) 615 std::printf (" %-42s %s (%d cases, %d layers)\n" ,"gdn_commit_layers == per-layer commit" ,lb_bad ?"*** NO ***" :"bitwise" , 616lb_n ,L ); 617else 618 std::printf (" %-42s skipped (the per-layer commit is not the state-only kernel here)\n" ,"gdn_commit_layers" ); 619bad += lb_bad ; 620 } 621 622 std::printf ("\ngdn: %d failures\n" ,bad ); 623if (bad )return 1 ; 624if (selftest ) std::printf ("gdn_parity OK\n" ); 625return 0 ; 626}