okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
62db0bf10
main
1// src/kernels/verify_parity.cpp - batched / split verify-window kernels against the kernels they replace, BITWISE. 2// 3// A verify window's token t must come out bit for bit as it would in a window of any other size (the drafts are 4// accepted exactly when greedy decode would have produced them), so every kernel here is checked against the 5// one it stands in for, on random inputs that include -0.0, denormals, NaN, -inf and large values. The tests 6// follow eddoursul's fork (MIT; verify_parity.cpp), ported to this tree's kernels. 7#include "strata/kernels/elementwise.hpp" 8#include "strata/kernels/f16_bits.hpp" 9#include "strata/kernels/quantize_act.hpp" 10#include "strata/kernels/sampler.hpp" 11#include "strata/kernels/verify_kernels.hpp" 12 13#include <cuda_runtime.h> 14 15#include <algorithm> 16#include <cmath> 17#include <cstdio> 18#include <cstdlib> 19#include <cstring> 20#include <random> 21#include <string> 22#include <vector> 23 24namespace { 25 26void check (cudaError_t e ,const char * what ) { 27if (e != cudaSuccess ) { 28 std::fprintf (stderr ,"%s: %s\n" ,what ,cudaGetErrorString (e )); 29 std::exit (1 ); 30 } 31} 32 33template < typename T > T * dev (size_t n ) { 34T * p = nullptr ; 35check (cudaMalloc (& p ,n * sizeof (T )),"cudaMalloc" ); 36return p ; 37} 38template < typename T > void up (T * d ,const std::vector < T >& h ) { 39check (cudaMemcpy (d ,h .data (),h .size ()* sizeof (T ),cudaMemcpyHostToDevice ),"upload" ); 40} 41template < typename T > std::vector < T > down (const T * d ,size_t n ) { 42 std::vector < T > h (n ); 43check (cudaMemcpy (h .data (),d ,n * sizeof (T ),cudaMemcpyDeviceToHost ),"download" ); 44return h ; 45} 46 47// argmax_rows against sample_tokens' greedy pick and row_top_prob_split against row_top_prob, bitwise: rows of both 48// vocabularies with ties at the maximum (the lowest index wins), NaN, -inf, a row with no value above -inf, and 49// launches repeated on one scratch (each must leave its counters at zero) 50int test_argmax (std::mt19937 & rng ,cudaStream_t s ) { 51using namespace strata::kernels ; 52int bad = 0 ; 53 std::normal_distribution < float > nd (0.0f ,4.0f ); 54uint8_t * sa = dev < uint8_t > (argmax_rows_scratch_bytes (8 ));// one scratch for every row count, as in the engine 55uint8_t * st = dev < uint8_t > (row_top_prob_scratch_bytes (8 )); 56check (cudaMemset (sa ,0 ,argmax_rows_scratch_bytes (8 )),"memset" ); 57check (cudaMemset (st ,0 ,row_top_prob_scratch_bytes (8 )),"memset" ); 58for (const int n : {248320 ,40525 ,4097 ,33 }) { 59for (const int rows : {4 ,8 ,1 ,3 ,8 ,2 }) { 60 std::vector < float > h ((size_t )rows * n ); 61for (float & v :h )v = nd (rng ); 62for (int r = 0 ;r < rows ;++ r ) { 63float * row = h .data ()+ (size_t )r * n ; 64const int a = (int ) (rng () %n ),b = (int ) (rng () %n ); 65row [a ]= row [b ]= 40.0f ;// a tie at the maximum 66row [(int ) (rng () %n )]= std::nanf ("" );// never picked 67row [(int ) (rng () %n )]= - INFINITY ; 68if (r == 2 )// no value above -inf: 0 69for (int i = 0 ;i < n ;++ i )row [i ]= i %7 ?- INFINITY : std::nanf ("" ); 70 } 71float * d_l = dev < float > (h .size ()); 72up (d_l ,h ); 73int * d_ref = dev < int > (rows ); 74int32_t * d_out = dev < int32_t > (rows ); 75float * d_p1 = dev < float > (rows ); 76float * d_p2 = dev < float > (rows ); 77SamplerParams sp ; 78sp .greedy = true; 79sp .temperature = 0.0f ; 80sample_tokens (d_l ,rows ,n ,nullptr ,0 ,sp ,d_ref ,s ); 81row_top_prob (d_l ,rows ,n ,d_ref ,d_p1 ,s ); 82for (int rep = 0 ;rep < 3 ;++ rep ) { 83argmax_rows (d_l ,rows ,n ,sa ,d_out ,s ); 84row_top_prob_split (d_l ,rows ,n ,d_out ,d_p2 ,st ,s ); 85check (cudaStreamSynchronize (s ),"argmax" ); 86const std::vector < int > ref = down (d_ref ,rows ); 87const std::vector < int32_t > out = down (d_out ,rows ); 88const std::vector < float > p1 = down (d_p1 ,rows ),p2 = down (d_p2 ,rows ); 89for (int r = 0 ;r < rows ;++ r ) 90if (ref [r ]!= out [r ]|| std::memcmp (& p1 [r ],& p2 [r ],4 )!= 0 ) { 91if (bad < 5 ) 92 std::printf (" argmax n %d rows %d row %d: %d / %d, p %.9g / %.9g\n" ,n ,rows ,r ,ref [r ], 93out [r ],p1 [r ],p2 [r ]); 94++ bad ; 95 } 96 } 97 (void )cudaFree (d_l ); (void )cudaFree (d_ref ); (void )cudaFree (d_out ); (void )cudaFree (d_p1 ); (void )cudaFree (d_p2 ); 98 } 99 } 100 (void )cudaFree (sa ); 101 (void )cudaFree (st ); 102 std::printf ("argmax_rows, row_top_prob_split: %s\n" , 103bad ?"MISMATCH" :"bitwise equal to the one-block kernels (ties, NaN, -inf, 1-8 rows on one scratch)" ); 104return bad ; 105} 106 107int test_mapped_multi (std::mt19937 & rng ,cudaStream_t s ) { 108using namespace strata::kernels ; 109int bad = 0 ; 110// 7-copy round layout + non-multiple-of-4 large copy (1025 words) + 4B-aligned non-16B-aligned large copy (1028 words) 111const int64_t words [7 ]= {5 ,40 ,1025 ,1028 ,51200 ,4 ,16 }; 112const int64_t off [7 ]= {0 ,0 ,0 ,1 ,0 ,0 ,0 };// entry 3 is +1 word (+4 B) offset -> 4B-aligned, not 16B 113int32_t * h_src [7 ]= {}; 114int32_t * d_src [7 ]= {}; 115int32_t * d_ref [7 ]= {}; 116int32_t * d_out [7 ]= {}; 117MappedCopy mc [7 ]= {}; 118for (int i = 0 ;i < 7 ;++ i ) { 119const size_t alloc_w = (size_t ) (words [i ]+ off [i ]); 120check (cudaHostAlloc ((void ** )& h_src [i ],alloc_w * 4 ,cudaHostAllocMapped ),"cudaHostAlloc" ); 121check (cudaHostGetDevicePointer ((void ** )& d_src [i ],h_src [i ],0 ),"cudaHostGetDevicePointer" ); 122for (size_t j = 0 ;j < alloc_w ;++ j )h_src [i ][j ]= (int32_t )rng (); 123d_ref [i ]= dev < int32_t > (alloc_w ); 124d_out [i ]= dev < int32_t > (alloc_w ); 125check (cudaMemset (d_ref [i ],0 ,alloc_w * 4 ),"memset" ); 126if (words [i ]> 1024 && (words [i ]& 3 )== 0 && off [i ]== 0 ) 127copy_from_mapped ((float * ) (d_ref [i ]+ off [i ]), (const float * ) (d_src [i ]+ off [i ]),words [i ],s ); 128else 129copy_i32_from_mapped (d_ref [i ]+ off [i ],d_src [i ]+ off [i ],words [i ],s ); 130mc [i ]= {d_out [i ]+ off [i ],d_src [i ]+ off [i ],words [i ]}; 131 } 132const char * prev = std::getenv ("STRATA_MTP_MULTI_COPY" ); 133const std::string saved = prev ?prev :"" ; 134for (const char * mode : {"1" ,"0" }) { 135#if defined(_WIN32 ) 136_putenv_s ("STRATA_MTP_MULTI_COPY" ,mode ); 137#else 138setenv ("STRATA_MTP_MULTI_COPY" ,mode ,1 ); 139#endif 140for (int i = 0 ;i < 7 ;++ i )check (cudaMemsetAsync (d_out [i ],0x5a , (size_t ) (words [i ]+ off [i ])* 4 ,s ),"poison" ); 141copy_from_mapped_multi (mc ,7 ,s ); 142check (cudaStreamSynchronize (s ),"mapped_multi" ); 143for (int i = 0 ;i < 7 ;++ i ) { 144const std::vector < int32_t > r = down (d_ref [i ]+ off [i ], (size_t )words [i ]); 145const std::vector < int32_t > o = down (d_out [i ]+ off [i ], (size_t )words [i ]); 146if (r != o )++ bad ; 147 } 148 } 149#if defined(_WIN32 ) 150_putenv_s ("STRATA_MTP_MULTI_COPY" ,prev ?saved .c_str () :"" ); 151#else 152if (prev )setenv ("STRATA_MTP_MULTI_COPY" ,saved .c_str (),1 ); 153else unsetenv ("STRATA_MTP_MULTI_COPY" ); 154#endif 155for (int i = 0 ;i < 7 ;++ i ) { 156 (void )cudaFreeHost (h_src [i ]); 157 (void )cudaFree (d_ref [i ]); 158 (void )cudaFree (d_out [i ]); 159 } 160 std::printf ("copy_from_mapped_multi: %s\n" , 161bad ?"MISMATCH" :"bitwise equal to individual mapped copies (multi=1 and fallback=0, aligned + 1025w + 4B-aligned)" ); 162return bad ; 163} 164 165int test_quantize_q8_0_scaled (std::mt19937 & rng ,cudaStream_t s ) { 166using namespace strata::kernels ; 167int bad = 0 ; 168 std::normal_distribution < float > nd (0.0f ,3.0f ); 169for (const int64_t n : {2560 ,10240 ,25600 }) { 170const int64_t nb = n /32 ; 171 std::vector < float > hx ((size_t )n ); 172for (int64_t i = 0 ;i < n ;++ i )hx [(size_t )i ]= nd (rng ); 173for (int64_t j = 0 ;j < 32 && j < n ;++ j )hx [(size_t )j ]= 0.0f ;// all-zero block 174if (nb > 2 ) { 175hx [64 ]= 127.0f ; 176for (int j = 1 ;j < 32 ;++ j )hx [64 + j ]= (float )j - 0.5f ;// exact .5 half-away ties 177 } 178float * d_x = dev < float > ((size_t )n ); 179uint8_t * d_b = dev < uint8_t > ((size_t )nb * 34 ); 180float * d_s = dev < float > ((size_t )nb ); 181up (d_x ,hx ); 182quantize_q8_0_scaled (d_x ,d_b ,d_s ,n ,s ); 183check (cudaStreamSynchronize (s ),"q8_0_scaled" ); 184const std::vector < uint8_t > gb = down (d_b , (size_t )nb * 34 ); 185const std::vector < float > gs = down (d_s , (size_t )nb ); 186for (int64_t b = 0 ;b < nb ;++ b ) { 187const float * xb = hx .data ()+ b * 32 ; 188float amax = 0.0f ; 189for (int i = 0 ;i < 32 ;++ i )amax = std::fmax (amax , std::fabs (xb [i ])); 190const float sc = amax > 0.f ?amax /127.f :0.f ; 191const float inv = sc > 0.f ?1.f /sc :0.f ; 192if (std::memcmp (& gs [(size_t )b ],& sc ,4 )!= 0 )++ bad ; 193const uint16_t d16 = f16_from_f32 (sc ); 194const uint8_t * ob = gb .data ()+ b * 34 ; 195if (ob [0 ]!= (uint8_t ) (d16 & 0xFF )|| ob [1 ]!= (uint8_t ) (d16 >>8 ))++ bad ; 196for (int i = 0 ;i < 32 ;++ i ) { 197const float t = xb [i ]* inv ; 198const float r = t + (t >=0.f ?0.5f :-0.5f ); 199int v = (int )r ; 200v = v < -127 ?-127 : (v > 127 ?127 :v ); 201if (ob [2 + i ]!= (uint8_t ) (int8_t )v )++ bad ; 202 } 203 } 204 (void )cudaFree (d_x ); 205 (void )cudaFree (d_b ); 206 (void )cudaFree (d_s ); 207 } 208 std::printf ("quantize_q8_0_scaled: %s\n" ,bad ?"MISMATCH" :"bitwise equal to CPU act_quant_q8_1 reference (zero, .5 ties, normal)" ); 209return bad ; 210} 211 212void bench_head_ops (cudaStream_t s ) { 213using namespace strata::kernels ; 214uint8_t * sa = dev < uint8_t > (argmax_rows_scratch_bytes (8 )); 215uint8_t * st = dev < uint8_t > (row_top_prob_scratch_bytes (8 )); 216check (cudaMemset (sa ,0 ,argmax_rows_scratch_bytes (8 )),"memset" ); 217check (cudaMemset (st ,0 ,row_top_prob_scratch_bytes (8 )),"memset" ); 218cudaEvent_t e0 ,e1 ; 219check (cudaEventCreate (& e0 ),"ev" ); 220check (cudaEventCreate (& e1 ),"ev" ); 221constexpr int ITERS = 200 ; 222auto time_fn = [& ](auto && fn ) { 223for (int i = 0 ;i < 20 ;++ i )fn (); 224check (cudaStreamSynchronize (s ),"warm" ); 225check (cudaEventRecord (e0 ,s ),"e0" ); 226for (int i = 0 ;i < ITERS ;++ i )fn (); 227check (cudaEventRecord (e1 ,s ),"e1" ); 228check (cudaEventSynchronize (e1 ),"sync" ); 229float ms = 0.0f ; 230check (cudaEventElapsedTime (& ms ,e0 ,e1 ),"elapsed" ); 231return ms * 1000.0 /ITERS ; 232 }; 233for (const int n : {106299 ,248320 }) { 234for (const int rows : {1 ,4 ,5 }) { 235if (n == 106299 && rows > 1 )continue ; 236 std::vector < float > h ((size_t )rows * n ,0.1f ); 237for (int r = 0 ;r < rows ;++ r )h [(size_t )r * n + (r * 1337 + 42 ) %n ]= 10.0f ; 238float * d_l = dev < float > (h .size ()); 239up (d_l ,h ); 240int32_t * d_out = dev < int32_t > (rows ); 241float * d_p = dev < float > (rows ); 242SamplerParams sp ; 243sp .greedy = true; 244sp .temperature = 0.0f ; 245const double us_g1 = time_fn ([& ] {sample_tokens (d_l ,rows ,n ,nullptr ,0 ,sp ,d_out ,s ); }); 246const double us_gm = time_fn ([& ] {argmax_rows (d_l ,rows ,n ,sa ,d_out ,s ); }); 247const double us_p1 = time_fn ([& ] {row_top_prob (d_l ,rows ,n ,d_out ,d_p ,s ); }); 248const double us_pm = time_fn ([& ] {row_top_prob_split (d_l ,rows ,n ,d_out ,d_p ,st ,s ); }); 249 std::printf (" V=%6d rows=%d: greedy 1-blk %6.2f us -> argmax_rows %6.2f us (x%.2f) | top_prob 1-blk %6.2f us -> split %6.2f us (x%.2f)\n" , 250n ,rows ,us_g1 ,us_gm ,us_g1 /us_gm ,us_p1 ,us_pm ,us_p1 /us_pm ); 251 (void )cudaFree (d_l ); (void )cudaFree (d_out ); (void )cudaFree (d_p ); 252 } 253 } 254 (void )cudaEventDestroy (e0 ); 255 (void )cudaEventDestroy (e1 ); 256 (void )cudaFree (sa ); 257 (void )cudaFree (st ); 258} 259 260}// namespace 261 262int main (int argc ,char ** argv ) { 263bool do_bench = false; 264for (int i = 1 ;i < argc ;++ i ) { 265if (std::string (argv [i ])== "--bench" )do_bench = true; 266else if (std::string (argv [i ])!= "--selftest" ) { 267 std::fprintf (stderr ,"usage: verify_parity [--selftest] [--bench]\n" ); 268return 2 ; 269 } 270 } 271 std::mt19937 rng (20260925 ); 272cudaStream_t s = nullptr ; 273check (cudaStreamCreate (& s ),"stream" ); 274int bad = 0 ; 275bad += test_argmax (rng ,s ); 276bad += test_mapped_multi (rng ,s ); 277bad += test_quantize_q8_0_scaled (rng ,s ); 278if (do_bench && bad == 0 )bench_head_ops (s ); 279 (void )cudaStreamDestroy (s ); 280 std::printf ("verify_parity: %s\n" ,bad ?"FAIL" :"PASS" ); 281return bad ?1 :0 ; 282}