okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
b1efb70c0
main
1// src/kernels/rope_parity.cpp - P2.S2's parity test for NEOX partial RoPE. 2// 3// TWO CHECKS, deliberately separated so each can be tight: 4// 5// 1. THE TABLE against the float64 reference - `theta ** (-2i/n_rot)`, `pos * inv`, `cos`/`sin`, all in 6// double exactly as `ref/qsa.py::rope_freqs` does. Compared as float32 after the cast, so a mismatch 7// here is a real arithmetic difference and not a formatting one. 8// 2. THE ROTATION against a host rotation written from `ref/qsa.py::rope_neox`, using the SAME table the 9// kernel is given. Both sides then perform identical float32 operations, so this is BIT-EXACT - and a 10// bit-exact rotation plus a table that matches the reference means the composition matches too. 11// 12// A single end-to-end comparison would have had to carry one loose tolerance for both, and the interesting 13// failure - the NEOX pairing being wrong - is a PERMUTATION that a loose tolerance over all 256 dims would 14// happily accept. 15// 16// Check 4 extends the same discipline to the SCALED tables (rope scaling: none/linear/YaRN). The rotation 17// kernel cannot see scaling - it lives in the table contents - so each variant's TABLE is held to a float64 18// transcription of ggml's `rope_yarn` spec (the ramp helper is shared, the `rope_neox_pair` convention), and 19// the two properties a tolerance could never fake get their own structural checks: at position 0 every pair's 20// angle is 0, so YaRN's mscale stands naked in cos_tab[0], and at a far position the first pair must match 21// EXTRAPOLATION while the last matches INTERPOLATION. An observability assertion closes it: if the scaled 22// and unscaled tables were indistinguishable, every green number above would be vacuous. 23// 24// Check 5 holds the TWO PATHS together: the table path's float64 host trig and the native path's float32 25// fast-math device trig must answer to the same `RopeScaling`, yarn and none alike, so one cache never 26// mixes two rotations. 27#include "strata/kernels/rope.hpp" 28#include "strata/kernels/native_qsa.hpp" 29#include "strata/kernels/native_rope.hpp" 30 31#include <cuda_runtime.h> 32 33#include <cmath> 34#include <cstdio> 35#include <cstdlib> 36#include <cstring> 37#include <random> 38#include <string> 39#include <vector> 40 41namespace { 42 43void check (cudaError_t e ,const char * what ) { 44if (e != cudaSuccess ) { 45 std::fprintf (stderr ,"%s: %s\n" ,what ,cudaGetErrorString (e )); 46 std::exit (1 ); 47 } 48} 49 50}// namespace 51 52int main (int argc ,char ** argv ) { 53bool selftest = false; 54for (int i = 1 ;i < argc ;++ i ) { 55if (std::string (argv [i ])== "--selftest" )selftest = true; 56else { std::fprintf (stderr ,"usage: rope_parity [--selftest]\n" );return 2 ; } 57 } 58 59const int n_rot = 64 ;// the artifact's rope.dimension_count 60const double theta = 1.0e7 ;// rope.freq_base 61const int head_dim = 256 ; 62const int rows = 24 * 8 ;// 24 heads over 8 positions, so several positions exercise the table 63const int max_pos = 32 ; 64const int half = n_rot /2 ; 65int bad = 0 ; 66 67// ---- 1. the table against the float64 reference 68 std::vector < float > hcos ((size_t )max_pos * half ),hsin ((size_t )max_pos * half ); 69 strata::kernels::build_rope_table (n_rot ,theta ,max_pos ,hcos .data (),hsin .data ()); 70long long table_bad = 0 ; 71double table_worst = 0.0 ; 72for (int p = 0 ;p < max_pos ;++ p ) { 73for (int i = 0 ;i < half ;++ i ) { 74const double inv = std::pow (theta ,-2.0 * (double )i / (double )n_rot ); 75const double ang = (double )p * inv ; 76const float rc = (float ) std::cos (ang ),rs = (float ) std::sin (ang ); 77if (std::memcmp (& rc ,& hcos [(size_t )p * half + i ],4 )!= 0 )++ table_bad ; 78if (std::memcmp (& rs ,& hsin [(size_t )p * half + i ],4 )!= 0 )++ table_bad ; 79table_worst = std::max (table_worst , 80 std::fabs ((double )rc - (double )hcos [(size_t )p * half + i ])); 81 } 82 } 83 std::printf (" rope table vs float64 reference %s (%lld of %d entries differ, worst %.1e)\n" , 84table_bad ?"*** WRONG ***" :"bit-exact" ,table_bad ,max_pos * half * 2 ,table_worst ); 85bad += (int )table_bad ; 86 87// ---- 2. the rotation, against the same spec in float32 with the SAME table 88 std::mt19937 rng (11 ); 89 std::normal_distribution < float > gauss (0.0f ,1.0f ); 90 std::vector < float > x ((size_t )rows * head_dim ); 91for (auto & v :x )v = gauss (rng ); 92 std::vector < int > pos ((size_t )rows ); 93for (int r = 0 ;r < rows ;++ r )pos [(size_t )r ]= r %max_pos ; 94 95 std::vector < float > ref (x .size ()); 96for (int r = 0 ;r < rows ;++ r ) { 97const float * xr = & x [(size_t )r * head_dim ]; 98float * orow = & ref [(size_t )r * head_dim ]; 99for (int d = 0 ;d < head_dim ;++ d )orow [d ]= xr [d ];// the tail passes through 100for (int i = 0 ;i < half ;++ i ) { 101const float c = hcos [(size_t )pos [(size_t )r ]* half + i ]; 102const float s = hsin [(size_t )pos [(size_t )r ]* half + i ]; 103const float a = xr [i ],b = xr [half + i ]; 104orow [i ]= a * c - b * s ; 105orow [half + i ]= a * s + b * c ; 106 } 107 } 108 109float * d_x = nullptr ,* d_out = nullptr ,* d_cos = nullptr ,* d_sin = nullptr ; 110int * d_pos = nullptr ; 111check (cudaMalloc (& d_x ,x .size ()* sizeof (float )),"malloc x" ); 112check (cudaMalloc (& d_out ,ref .size ()* sizeof (float )),"malloc out" ); 113check (cudaMalloc (& d_cos ,hcos .size ()* sizeof (float )),"malloc cos" ); 114check (cudaMalloc (& d_sin ,hsin .size ()* sizeof (float )),"malloc sin" ); 115check (cudaMalloc (& d_pos ,pos .size ()* sizeof (int )),"malloc pos" ); 116check (cudaMemcpy (d_x ,x .data (),x .size ()* sizeof (float ),cudaMemcpyHostToDevice ),"copy x" ); 117check (cudaMemcpy (d_cos ,hcos .data (),hcos .size ()* sizeof (float ),cudaMemcpyHostToDevice ),"copy cos" ); 118check (cudaMemcpy (d_sin ,hsin .data (),hsin .size ()* sizeof (float ),cudaMemcpyHostToDevice ),"copy sin" ); 119check (cudaMemcpy (d_pos ,pos .data (),pos .size ()* sizeof (int ),cudaMemcpyHostToDevice ),"copy pos" ); 120 strata::kernels::rope_neox_apply (d_x ,d_out ,rows ,head_dim ,n_rot ,d_cos ,d_sin ,d_pos ,nullptr ); 121 std::vector < float > got (ref .size ()); 122check (cudaMemcpy (got .data (),d_out ,got .size ()* sizeof (float ),cudaMemcpyDeviceToHost ),"back" ); 123 124// A RELATIVE TOLERANCE, NOT BIT EQUALITY. *c - b*s is one of the expressions the compiler is free to 125// contract into an FMA, and the host and device compilers choose differently - so a handful of values 126// differ in the last bit. Round 167 established this the hard way: a bit-equality expectation that a 127// kernel cannot meet is a test that reports a correct kernel as broken. The residual is reported, and if 128// it were structural (a wrong pairing, a wrong table entry) it would be O(1), not O(1 ULP). 129long long rot_bad = 0 ; 130double worst = 0.0 ; 131long long first_bad = -1 ; 132// The denominator is the ROW'S INPUT MAGNITUDE, not the element's own value. *c - b*s cancels, so an 133// element whose result is near zero reports a large relative error for a 1-ULP absolute one - which is the 134// same metric mistake round 169 found on Q4_K's dot products, where the fix was to measure against 135// sum|term| instead of the result. Here the natural scale of the computation is the largest input in the 136// row, because every output element is a combination of two inputs with |cos|,|sin| <= 1. 137 std::vector < double > row_scale ((size_t )rows ,0.0 ); 138for (int r = 0 ;r < rows ;++ r ) { 139double m = 0 ; 140for (int d = 0 ;d < head_dim ;++ d )m = std::max (m , (double ) std::fabs (x [(size_t )r * head_dim + d ])); 141row_scale [(size_t )r ]= m > 1e-30 ?m :1e-30 ; 142 } 143for (size_t i = 0 ;i < ref .size ();++ i ) { 144const double a = ref [i ],b = got [i ]; 145const double rel = std::fabs (a - b ) /row_scale [i / (size_t )head_dim ]; 146if (!(rel <=worst ))worst = rel ; 147if (!(rel <=1e-6 )) {if (first_bad < 0 )first_bad = (long long )i ;++ rot_bad ; } 148 } 149 std::printf (" rope rotation vs host reference %s (%lld of %zu over 1e-6, worst rel %.3e)\n" , 150rot_bad ?"*** WRONG ***" :"agrees" ,rot_bad ,ref .size (),worst ); 151if (first_bad >=0 ) std::printf (" first over-tolerance at element %lld\n" ,first_bad ); 152bad += (int )rot_bad ; 153 154// ---- 3. THE PAIRING, asserted structurally rather than as a tolerance. NEOX pairs (i, i+half); the 155// adjacent-pair convention would pair (2i, 2i+1). Feed an input that is 1 at dim 0 and 0 elsewhere, and 156// check WHICH dims move: under NEOX only dims 0 and half may change, under the adjacent convention only 157// dims 0 and 1 may. A tolerance over all 256 dims cannot tell the two apart. 158 { 159 std::vector < float > e ((size_t )head_dim ,0.0f ); 160e [0 ]= 1.0f ; 161 std::vector < float > o ((size_t )head_dim ,0.0f ); 162check (cudaMemcpy (d_x ,e .data (),e .size ()* sizeof (float ),cudaMemcpyHostToDevice ),"copy e" ); 163// position 7, NOT position 0: at pos 0 sin is exactly 0, so dim half legitimately does not move and 164// the check would report the correct kernel as wrong. The first version made exactly that mistake. 165int p7 = 7 ; 166check (cudaMemcpy (d_pos ,& p7 ,sizeof (int ),cudaMemcpyHostToDevice ),"copy p7" ); 167 strata::kernels::rope_neox_apply (d_x ,d_out ,1 ,head_dim ,n_rot ,d_cos ,d_sin ,d_pos ,nullptr ); 168check (cudaMemcpy (o .data (),d_out ,o .size ()* sizeof (float ),cudaMemcpyDeviceToHost ),"back e" ); 169int moved [4 ]= {0 ,0 ,0 ,0 };// dims 0, 1, half, half+1 170const float * chk [4 ]= {& e [0 ],& e [1 ],& e [half ],& e [half + 1 ]}; 171 (void )chk ; 172moved [0 ]= (o [0 ]!= 0.0f || o [half ]!= 0.0f ) ?1 :0 ; 173const bool adjacent_moved = (o [1 ]!= 0.0f ); 174const bool neox_moved = (o [half ]!= 0.0f ); 175 std::printf (" pairing: dim 0 set -> NEOX moves dim %d (o[half]=%.6f), adjacent would move dim 1 " 176"(o[1]=%.6f) %s\n" ,half , (double )o [half ], (double )o [1 ], 177 (neox_moved && !adjacent_moved ) ?"NEOX confirmed" :"*** WRONG CONVENTION ***" ); 178if (!(neox_moved && !adjacent_moved ))++ bad ; 179 } 180 181// ---- 4. THE SCALED TABLES (rope_scaling.hpp). Scaling lives in the table contents, so each variant is 182// a table check: a float64 transcription of the ggml spec, bit-exact after the float32 cast, plus the 183// structural and observability checks the tolerances cannot cover. 184 { 185// 4a. type None IS the five-argument builder above - bit for bit. 186 std::vector < float > nc ((size_t )max_pos * half ),ns ((size_t )max_pos * half ); 187 strata::kernels::RopeScaling none ; 188none .freq_base = theta ; 189 strata::kernels::build_rope_table (n_rot ,none ,max_pos ,nc .data (),ns .data ()); 190const long long none_bad = 191 (long long ) (std::memcmp (nc .data (),hcos .data (),nc .size ()* 4 )!= 0 )+ 192 (long long ) (std::memcmp (ns .data (),hsin .data (),ns .size ()* 4 )!= 0 ); 193 std::printf (" scaled table, None vs unscaled builder %s\n" ,none_bad ?"*** WRONG ***" :"bit-identical" ); 194bad += (int )none_bad ; 195 196// 4b. LINEAR, factor 4: `ang = p * inv / 4` in float64. The factor is a power of two on purpose - 197// dividing by it is exact, so no multiplication-order rounding can sneak between spec and builder. 198 strata::kernels::RopeScaling lin ; 199lin .type = strata::kernels::RopeScalingType ::Linear ; 200lin .freq_base = theta ; 201lin .factor = 4.0 ; 202 std::vector < float > lc ((size_t )max_pos * half ),ls ((size_t )max_pos * half ); 203 strata::kernels::build_rope_table (n_rot ,lin ,max_pos ,lc .data (),ls .data ()); 204long long lin_bad = 0 ; 205for (int p = 0 ;p < max_pos ;++ p ) { 206for (int i = 0 ;i < half ;++ i ) { 207const double inv = std::pow (theta ,-2.0 * (double )i / (double )n_rot ); 208const double ang = (double )p * inv /4.0 ; 209const float rc = (float ) std::cos (ang ),rs = (float ) std::sin (ang ); 210if (std::memcmp (& rc ,& lc [(size_t )p * half + i ],4 )!= 0 )++ lin_bad ; 211if (std::memcmp (& rs ,& ls [(size_t )p * half + i ],4 )!= 0 )++ lin_bad ; 212 } 213 } 214 std::printf (" scaled table, linear factor 4 %s (%lld of %d entries differ)\n" , 215lin_bad ?"*** WRONG ***" :"bit-exact" ,lin_bad ,max_pos * half * 2 ); 216bad += (int )lin_bad ; 217 218// 4c. THE INTERPOLATION CLAIM ITSELF: linear(4) at position p is `none` at p/4 - the same table row, 219// bit for bit, at every position divisible by the factor. 220 std::vector < float > wc ((size_t )4 * max_pos * half ),ws ((size_t )4 * max_pos * half ); 221 strata::kernels::build_rope_table (n_rot ,lin ,4 * max_pos ,wc .data (),ws .data ()); 222long long equiv_bad = 0 ; 223for (int p = 0 ;p < 4 * max_pos ;p += 4 ) { 224for (int i = 0 ;i < half ;++ i ) { 225if (std::memcmp (& wc [(size_t )p * half + i ],& hcos [(size_t ) (p /4 )* half + i ],4 )!= 0 )++ equiv_bad ; 226if (std::memcmp (& ws [(size_t )p * half + i ],& hsin [(size_t ) (p /4 )* half + i ],4 )!= 0 )++ equiv_bad ; 227 } 228 } 229 std::printf (" linear(4) @ p == none @ p/4 %s (%lld of %d rows differ)\n" , 230equiv_bad ?"*** WRONG ***" :"bit-identical" ,equiv_bad ,max_pos ); 231bad += (int )equiv_bad ; 232 233// 4d. YARN vs a float64 transcription of ggml's rope_yarn (factor 4, the default correction knobs). 234// The ramp helper is shared with the builder (the rope_neox_pair convention: one definition, not two 235// transcriptions of it); the interpolation mix and the mscale formula are written here from the ggml 236// source lines, which is the spec being tested. 237 strata::kernels::RopeScaling yarn ; 238yarn .type = strata::kernels::RopeScalingType ::YaRN ; 239yarn .freq_base = theta ; 240yarn .factor = 4.0 ; 241yarn .ext_factor = 1.0 ; 242 std::vector < float > yc ((size_t )max_pos * half ),ys ((size_t )max_pos * half ); 243 strata::kernels::build_rope_table (n_rot ,yarn ,max_pos ,yc .data (),ys .data ()); 244const double fs = yarn .freq_scale ();// 0.25 245const double ms = yarn .mscale ();// attn_factor * (1 + 0.1*ln(4)) 246double cd [2 ]; 247yarn .corr_dims (n_rot ,cd ); 248long long yarn_bad = 0 ; 249for (int p = 0 ;p < max_pos ;++ p ) { 250for (int i = 0 ;i < half ;++ i ) { 251const double inv = std::pow (theta ,-2.0 * (double )i / (double )n_rot ); 252const double extrap = (double )p * inv ; 253const double interp = fs * extrap ; 254const double ramp = (double ) strata::kernels::rope_yarn_ramp ((float )cd [0 ], (float )cd [1 ],i )* 255yarn .ext_factor ; 256const double ang = interp * (1.0 - ramp )+ extrap * ramp ; 257const float rc = (float ) (std::cos (ang )* ms ),rs = (float ) (std::sin (ang )* ms ); 258if (std::memcmp (& rc ,& yc [(size_t )p * half + i ],4 )!= 0 )++ yarn_bad ; 259if (std::memcmp (& rs ,& ys [(size_t )p * half + i ],4 )!= 0 )++ yarn_bad ; 260 } 261 } 262 std::printf (" scaled table, yarn factor 4 %s (%lld of %d entries differ)\n" , 263yarn_bad ?"*** WRONG ***" :"bit-exact" ,yarn_bad ,max_pos * half * 2 ); 264bad += (int )yarn_bad ; 265 266// 4e. THE STRUCTURE a tolerance cannot fake. At position 0 every pair's angle is 0, so YaRN's whole 267// magnitude correction stands naked: cos_tab[0] == mscale. At the far end of the table the first 268// pair (the highest trained frequency) must match EXTRAPOLATION and the last INTERPOLATION. The two 269// halves need different observables: at the first pair cos separates the hypotheses outright, but at 270// the last they differ by ~4 microradians here, invisible to cos near 1 - sin sees it, because near 271// zero sin IS the angle. That asymmetry is the YaRN design: interpolation happens where angles are 272// small. Both hypotheses go through the same mix formula the builder uses, so the bit comparison is 273// spec against spec, not shortcut against implementation. 274 { 275const float msv = (float )ms ; 276const bool zero_ok = std::memcmp (& msv ,& yc [0 ],4 )== 0 && ys [0 ]== 0.0f ; 277const int far = max_pos - 1 ; 278const double inv_f = 1.0 ;// pair 0: theta ** 0 279const double extrap_f = (double )far * inv_f ,interp_f = fs * extrap_f ; 280const double inv_l = std::pow (theta ,-2.0 * (double ) (half - 1 ) / (double )n_rot ); 281const double extrap_l = (double )far * inv_l ,interp_l = fs * extrap_l ; 282const float ef = (float ) (std::cos (interp_f * (1.0 - 1.0 )+ extrap_f * 1.0 )* ms ); 283const float itf = (float ) (std::cos (interp_f * (1.0 - 0.0 )+ extrap_f * 0.0 )* ms ); 284const float sl_e = (float ) (std::sin (interp_l * (1.0 - 1.0 )+ extrap_l * 1.0 )* ms ); 285const float sl_i = (float ) (std::sin (interp_l * (1.0 - 0.0 )+ extrap_l * 0.0 )* ms ); 286const float got_f = yc [(size_t )far * half ],got_l = ys [(size_t )far * half + (half - 1 )]; 287const bool first_extrapolates = std::memcmp (& got_f ,& ef ,4 )== 0 ; 288const bool last_interpolates = std::memcmp (& got_l ,& sl_i ,4 )== 0 ; 289const bool distinguishable = std::fabs ((double )ef - (double )itf )> 1e-3 && 290 std::fabs ((double )sl_e - (double )sl_i )> 1e-8 ; 291 std::printf (" yarn structure: cos_tab[0]==mscale %.4f, first pair extrapolates, last interpolates " 292"%s\n" , (double )msv , 293zero_ok && first_extrapolates && last_interpolates && distinguishable 294 ?"confirmed" 295 :"*** WRONG ***" ); 296if (!(zero_ok && first_extrapolates && last_interpolates && distinguishable ))++ bad ; 297 } 298 299// 4f. THE OBSERVABILITY assertion: the fixtures must SEE scaling. If the builder ignored its config, 300// linear(2) would equal `none` everywhere and 4b-4e would be green on a broken builder. 301 { 302 strata::kernels::RopeScaling lin2 ; 303lin2 .type = strata::kernels::RopeScalingType ::Linear ; 304lin2 .freq_base = theta ; 305lin2 .factor = 2.0 ; 306 std::vector < float > l2c ((size_t )101 * half ),l2s ((size_t )101 * half ); 307 strata::kernels::build_rope_table (n_rot ,lin2 ,101 ,l2c .data (),l2s .data ()); 308const float c_none = (float ) std::cos (100.0 );// pair 0, position 100, unscaled: ang = 100 309const float c_lin = l2c [(size_t )100 * half ];// pair 0, position 100, linear(2): ang = 50 310const bool sees = std::fabs ((double )c_none - (double )c_lin )> 1e-3 ; 311 std::printf (" observability: none vs linear(2) at position 100 %s (|%.4f - %.4f|)\n" , 312sees ?"visible" :"*** VACUUM ***" , (double )c_none , (double )c_lin ); 313if (!sees )++ bad ; 314 } 315 316// 4g. THE ROTATION under a scaled table. The kernel is table-agnostic and must not know or care: 317// the host reference is the same rope_neox_pair walk as check 2, over the YaRN table. 318 std::vector < float > ref2 (x .size ()); 319for (int r = 0 ;r < rows ;++ r ) { 320const float * xr = & x [(size_t )r * head_dim ]; 321float * orow = & ref2 [(size_t )r * head_dim ]; 322for (int d = 0 ;d < head_dim ;++ d )orow [d ]= xr [d ]; 323for (int i = 0 ;i < half ;++ i ) { 324float c = yc [(size_t )pos [(size_t )r ]* half + i ],s = ys [(size_t )pos [(size_t )r ]* half + i ]; 325 strata::kernels::rope_neox_pair (xr [i ],xr [half + i ],c ,s ,orow [i ],orow [half + i ]); 326 } 327 } 328check (cudaMemcpy (d_x ,x .data (),x .size ()* sizeof (float ),cudaMemcpyHostToDevice ),"copy x" ); 329check (cudaMemcpy (d_cos ,yc .data (),yc .size ()* sizeof (float ),cudaMemcpyHostToDevice ),"copy ycos" ); 330check (cudaMemcpy (d_sin ,ys .data (),ys .size ()* sizeof (float ),cudaMemcpyHostToDevice ),"copy ysin" ); 331check (cudaMemcpy (d_pos ,pos .data (),pos .size ()* sizeof (int ),cudaMemcpyHostToDevice ),"copy pos" ); 332 strata::kernels::rope_neox_apply (d_x ,d_out ,rows ,head_dim ,n_rot ,d_cos ,d_sin ,d_pos ,nullptr ); 333 std::vector < float > got2 (ref2 .size ()); 334check (cudaMemcpy (got2 .data (),d_out ,got2 .size ()* sizeof (float ),cudaMemcpyDeviceToHost ),"back" ); 335long long rot2_bad = 0 ; 336double worst2 = 0.0 ; 337for (size_t i = 0 ;i < ref2 .size ();++ i ) { 338const double a = ref2 [i ],b = got2 [i ]; 339const double rel = std::fabs (a - b ) /row_scale [i / (size_t )head_dim ]; 340worst2 = std::max (worst2 ,rel ); 341if (!(rel <=1e-6 ))++ rot2_bad ; 342 } 343 std::printf (" rope rotation over the yarn table %s (%lld of %zu over 1e-6, worst rel %.3e)\n" , 344rot2_bad ?"*** WRONG ***" :"agrees" ,rot2_bad ,ref2 .size (),worst2 ); 345bad += (int )rot2_bad ; 346 } 347 348// ---- 5. THE TWO PATHS MUST AGREE. The table path (the default) computes cos/sin on the host in float64; 349// the native path (`--native-rope`) computes the same angles on device in float32 under `--use_fast_math`. 350// They answer to ONE config - `RopeScaling` - and a disagreement between them would put differently-rotated 351// K into the cache depending on which path ran. The tolerance is NOT the 1e-6 of checks 2/4g: the device 352// side takes fast-math trig at angles up to ~2048 rad, where the fp32 range reduction alone is worth ~1e-4, 353// so the bar is the row-magnitude-relative 3e-3 and the test keeps the positions where that holds. (The 354// engine's default is the table path precisely because float64 host trig has no such floor.) 355 { 356const int npos = 2048 ; 357 strata::kernels::RopeScaling yarn ; 358yarn .type = strata::kernels::RopeScalingType ::YaRN ; 359yarn .factor = 2.0 ; 360yarn .ext_factor = 1.0 ; 361 std::vector < float > sc ((size_t )npos * half ),ss ((size_t )npos * half ); 362 strata::kernels::build_rope_table (n_rot ,yarn ,npos ,sc .data (),ss .data ()); 363 std::mt19937 rng2 (23 ); 364 std::normal_distribution < float > gauss2 (0.0f ,1.0f ); 365const int nrows = 24 * 16 ;// 16 positions spread over the table's range 366 std::vector < float > x2 ((size_t )nrows * head_dim ); 367for (auto & v :x2 )v = gauss2 (rng2 ); 368 std::vector < int > pos2 ((size_t )nrows ); 369for (int r = 0 ;r < nrows ;++ r )pos2 [(size_t )r ]= (r * 127 ) %npos ; 370 371float * d_x2 = nullptr ,* d_t2 = nullptr ,* d_n2 = nullptr ,* d_c2 = nullptr ,* d_s2 = nullptr ; 372int * d_p2 = nullptr ; 373check (cudaMalloc (& d_x2 ,x2 .size ()* sizeof (float )),"malloc x2" ); 374check (cudaMalloc (& d_t2 ,x2 .size ()* sizeof (float )),"malloc t2" ); 375check (cudaMalloc (& d_n2 ,x2 .size ()* sizeof (float )),"malloc n2" ); 376check (cudaMalloc (& d_c2 ,sc .size ()* sizeof (float )),"malloc c2" ); 377check (cudaMalloc (& d_s2 ,ss .size ()* sizeof (float )),"malloc s2" ); 378check (cudaMalloc (& d_p2 ,pos2 .size ()* sizeof (int )),"malloc p2" ); 379check (cudaMemcpy (d_x2 ,x2 .data (),x2 .size ()* sizeof (float ),cudaMemcpyHostToDevice ),"copy x2" ); 380check (cudaMemcpy (d_c2 ,sc .data (),sc .size ()* sizeof (float ),cudaMemcpyHostToDevice ),"copy c2" ); 381check (cudaMemcpy (d_s2 ,ss .data (),ss .size ()* sizeof (float ),cudaMemcpyHostToDevice ),"copy s2" ); 382check (cudaMemcpy (d_p2 ,pos2 .data (),pos2 .size ()* sizeof (int ),cudaMemcpyHostToDevice ),"copy p2" ); 383// The native path demands an explicit stream - a null one is refused by validation, not defaulted. 384cudaStream_t cs5 = nullptr ; 385check (cudaStreamCreate (& cs5 ),"stream5" ); 386 strata::kernels::rope_neox_apply (d_x2 ,d_t2 ,nrows ,head_dim ,n_rot ,d_c2 ,d_s2 ,d_p2 ,nullptr ); 387 strata::kernels::native_rope_apply (d_x2 ,d_n2 ,nrows ,head_dim ,n_rot ,yarn ,d_p2 ,cs5 ); 388check (cudaStreamSynchronize (cs5 ),"sync5" ); 389 std::vector < float > got_t (x2 .size ()),got_n (x2 .size ()); 390check (cudaMemcpy (got_t .data (),d_t2 ,got_t .size ()* sizeof (float ),cudaMemcpyDeviceToHost ),"back t2" ); 391check (cudaMemcpy (got_n .data (),d_n2 ,got_n .size ()* sizeof (float ),cudaMemcpyDeviceToHost ),"back n2" ); 392long long agree_bad = 0 ; 393double worst = 0.0 ; 394for (int r = 0 ;r < nrows ;++ r ) { 395double m = 0 ; 396for (int d = 0 ;d < head_dim ;++ d )m = std::max (m , (double ) std::fabs (x2 [(size_t )r * head_dim + d ])); 397const double scale = m > 1e-30 ?m :1e-30 ; 398for (int d = 0 ;d < head_dim ;++ d ) { 399const double rel = std::fabs ((double )got_t [(size_t )r * head_dim + d ]- 400 (double )got_n [(size_t )r * head_dim + d ]) /scale ; 401worst = std::max (worst ,rel ); 402if (!(rel <=3e-3 ))++ agree_bad ; 403 } 404 } 405 std::printf (" native path vs table path, yarn factor 2 %s (%lld of %d over 3e-3, worst rel %.3e)\n" , 406agree_bad ?"*** WRONG ***" :"agrees" ,agree_bad ,nrows * head_dim ,worst ); 407bad += (int )agree_bad ; 408 409// and the None config: the native path against the UNSCALED table - the identity this feature must 410// not disturb. 411 strata::kernels::RopeScaling none5 ;// all defaults: type None, freq_scale 1, mscale 1 412 strata::kernels::native_rope_apply (d_x2 ,d_n2 ,nrows ,head_dim ,n_rot ,none5 ,d_p2 ,cs5 ); 413check (cudaStreamSynchronize (cs5 ),"sync5b" ); 414check (cudaMemcpy (got_n .data (),d_n2 ,got_n .size ()* sizeof (float ),cudaMemcpyDeviceToHost ),"back n2b" ); 415 std::vector < float > nc5 ((size_t )npos * half ),ns5 ((size_t )npos * half ); 416 strata::kernels::build_rope_table (n_rot ,none5 ,npos ,nc5 .data (),ns5 .data ()); 417check (cudaMemcpy (d_c2 ,nc5 .data (),nc5 .size ()* sizeof (float ),cudaMemcpyHostToDevice ),"copy c2b" ); 418check (cudaMemcpy (d_s2 ,ns5 .data (),ns5 .size ()* sizeof (float ),cudaMemcpyHostToDevice ),"copy s2b" ); 419 strata::kernels::rope_neox_apply (d_x2 ,d_t2 ,nrows ,head_dim ,n_rot ,d_c2 ,d_s2 ,d_p2 ,nullptr ); 420check (cudaMemcpy (got_t .data (),d_t2 ,got_t .size ()* sizeof (float ),cudaMemcpyDeviceToHost ),"back t2b" ); 421long long none_bad5 = 0 ; 422double worst5 = 0.0 ; 423for (int r = 0 ;r < nrows ;++ r ) { 424double m = 0 ; 425for (int d = 0 ;d < head_dim ;++ d )m = std::max (m , (double ) std::fabs (x2 [(size_t )r * head_dim + d ])); 426const double scale = m > 1e-30 ?m :1e-30 ; 427for (int d = 0 ;d < head_dim ;++ d ) { 428const double rel = std::fabs ((double )got_t [(size_t )r * head_dim + d ]- 429 (double )got_n [(size_t )r * head_dim + d ]) /scale ; 430worst5 = std::max (worst5 ,rel ); 431if (!(rel <=3e-3 ))++ none_bad5 ; 432 } 433 } 434 std::printf (" native path vs table path, none %s (%lld of %d over 3e-3, worst rel %.3e)\n" , 435none_bad5 ?"*** WRONG ***" :"agrees" ,none_bad5 ,nrows * head_dim ,worst5 ); 436bad += (int )none_bad5 ; 437 438// ---- 6. FUSED RMSNorm + RoPE (`native_qsa_rms_norm_rope`) MUST BE BIT-IDENTICAL to 439// `native_qsa_rms_norm_weighted` followed by `native_rope_apply` for both contiguous 440// (`in_stride == head_dim`) and strided Q/gate split (`in_stride == 2 * head_dim`) inputs, 441// across supported head_dims (128, 256) and both unscaled and YaRN scaling. 442// The kernel is off by default on the HIP build until this check passes there (native_norm_rope_usable): the 443// check then reports SKIPPED, and STRATA_NORM_ROPE=1 runs it (gfx1151, Aurora, 0.1.40: it does not pass). 444const bool run6 = strata::kernels::native_norm_rope_usable (256 ,64 ); 445if (!run6 ) 446 std::printf (" native_qsa_rms_norm_rope SKIPPED (the fused kernel is not enabled on this build)\n" ); 447for (int hd6 : {128 ,256 }) { 448if (!run6 )break ; 449const int nr6 = 64 ; 450const int rows6 = 96 ; 451 std::vector < float > x6_strided ((size_t )rows6 * 2 * hd6 ); 452 std::vector < float > gamma6 ((size_t )hd6 ); 453 std::vector < int > pos6 ((size_t )rows6 ); 454for (auto & v :x6_strided )v = gauss2 (rng2 ); 455for (auto & g :gamma6 )g = 0.5f + std::fabs (gauss2 (rng2 )); 456for (int r = 0 ;r < rows6 ;++ r )pos6 [(size_t )r ]= (r * 53 + 1 ) %npos ; 457 458float * d_xs6 = nullptr ,* d_g6 = nullptr ,* d_ref6 = nullptr ,* d_fus6 = nullptr ; 459int * d_pos6 = nullptr ; 460check (cudaMalloc (& d_xs6 ,x6_strided .size ()* sizeof (float )),"malloc xs6" ); 461check (cudaMalloc (& d_g6 ,gamma6 .size ()* sizeof (float )),"malloc g6" ); 462check (cudaMalloc (& d_ref6 , (size_t )rows6 * hd6 * sizeof (float )),"malloc ref6" ); 463check (cudaMalloc (& d_fus6 , (size_t )rows6 * hd6 * sizeof (float )),"malloc fus6" ); 464check (cudaMalloc (& d_pos6 ,pos6 .size ()* sizeof (int )),"malloc pos6" ); 465check (cudaMemcpy (d_xs6 ,x6_strided .data (),x6_strided .size ()* sizeof (float ),cudaMemcpyHostToDevice ),"copy xs6" ); 466check (cudaMemcpy (d_g6 ,gamma6 .data (),gamma6 .size ()* sizeof (float ),cudaMemcpyHostToDevice ),"copy g6" ); 467check (cudaMemcpy (d_pos6 ,pos6 .data (),pos6 .size ()* sizeof (int ),cudaMemcpyHostToDevice ),"copy pos6" ); 468 469long long fbad = 0 ; 470for (const auto & sc6 : {none5 ,yarn }) { 471// (a) strided Q/gate split (in_stride = 2 * hd6) vs cudaMemcpy2DAsync + norm + rope 472check (cudaMemcpy2DAsync (d_ref6 , (size_t )hd6 * sizeof (float ),d_xs6 , (size_t )2 * hd6 * sizeof (float ), 473 (size_t )hd6 * sizeof (float ), (size_t )rows6 ,cudaMemcpyDeviceToDevice ,cs5 ), 474"split6" ); 475 strata::kernels::native_qsa_rms_norm_weighted (d_ref6 ,d_g6 ,d_ref6 ,hd6 ,rows6 ,1e-6f ,cs5 ); 476 strata::kernels::native_rope_apply (d_ref6 ,d_ref6 ,rows6 ,hd6 ,nr6 ,sc6 ,d_pos6 ,cs5 ); 477 478 strata::kernels::native_qsa_rms_norm_rope (d_xs6 ,2 * hd6 ,d_g6 ,d_fus6 ,rows6 ,hd6 ,nr6 ,1e-6f ,sc6 , 479d_pos6 ,cs5 ); 480check (cudaStreamSynchronize (cs5 ),"sync6a" ); 481 std::vector < float > href ((size_t )rows6 * hd6 ),hfus ((size_t )rows6 * hd6 ); 482check (cudaMemcpy (href .data (),d_ref6 ,href .size ()* sizeof (float ),cudaMemcpyDeviceToHost ),"back ref6a" ); 483check (cudaMemcpy (hfus .data (),d_fus6 ,hfus .size ()* sizeof (float ),cudaMemcpyDeviceToHost ),"back fus6a" ); 484for (size_t i = 0 ;i < href .size ();++ i ) { 485if (std::memcmp (& href [i ],& hfus [i ],4 )!= 0 )++ fbad ; 486 } 487 488// (b) contiguous in-place (in_stride = hd6, output == input) 489check (cudaMemcpy2DAsync (d_ref6 , (size_t )hd6 * sizeof (float ),d_xs6 , (size_t )2 * hd6 * sizeof (float ), 490 (size_t )hd6 * sizeof (float ), (size_t )rows6 ,cudaMemcpyDeviceToDevice ,cs5 ), 491"contig6_ref" ); 492check (cudaMemcpy (d_fus6 ,d_ref6 , (size_t )rows6 * hd6 * sizeof (float ),cudaMemcpyDeviceToDevice ), 493"contig6_fus" ); 494 strata::kernels::native_qsa_rms_norm_weighted (d_ref6 ,d_g6 ,d_ref6 ,hd6 ,rows6 ,1e-6f ,cs5 ); 495 strata::kernels::native_rope_apply (d_ref6 ,d_ref6 ,rows6 ,hd6 ,nr6 ,sc6 ,d_pos6 ,cs5 ); 496 strata::kernels::native_qsa_rms_norm_rope (d_fus6 ,hd6 ,d_g6 ,d_fus6 ,rows6 ,hd6 ,nr6 ,1e-6f ,sc6 , 497d_pos6 ,cs5 ); 498check (cudaStreamSynchronize (cs5 ),"sync6b" ); 499check (cudaMemcpy (href .data (),d_ref6 ,href .size ()* sizeof (float ),cudaMemcpyDeviceToHost ),"back ref6b" ); 500check (cudaMemcpy (hfus .data (),d_fus6 ,hfus .size ()* sizeof (float ),cudaMemcpyDeviceToHost ),"back fus6b" ); 501for (size_t i = 0 ;i < href .size ();++ i ) { 502if (std::memcmp (& href [i ],& hfus [i ],4 )!= 0 )++ fbad ; 503 } 504 } 505 std::printf (" native_qsa_rms_norm_rope hd=%3d nr=%2d %s (%lld of %d elements differ)\n" , 506hd6 ,nr6 ,fbad ?"*** WRONG ***" :"bit-identical" ,fbad ,rows6 * hd6 * 4 ); 507bad += (int )fbad ; 508cudaFree (d_xs6 ); 509cudaFree (d_g6 ); 510cudaFree (d_ref6 ); 511cudaFree (d_fus6 ); 512cudaFree (d_pos6 ); 513 } 514 } 515 516 std::printf ("\nrope: %d failures\n" ,bad ); 517if (bad )return 1 ; 518if (selftest ) std::printf ("rope_parity OK\n" ); 519return 0 ; 520}