okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
c954f5bd1
main
1// src/kernels/spec_verify_parity.cpp - the GPU verifier of probabilistic draft acceptance (sample_tokens_spec) against 2// the host reference (include/strata/core/spec_prob.hpp), token for token. 3// 4// Random logits at three vocabulary sizes (one block, a few blocks, the engine's 248,320), windows of 2..8 rows, 5// chains with top_k / top_p / min_p / temperature and repetition penalties over a per-row history, drafts taken from 6// the row's own top tokens, outside them, and from q; q lists cut by a DIFFERENT chain than p's; n_q from 0 (every 7// row a point mass) to all rows. The host computes p the sampler's way (penalties on the raw logits, top_k with 8// ties to the lowest index, top_p's cut in double, min_p in logit space, temperature), the Philox draws of the kernel 9// (u_pick = pos, u_accept = pos | 1<<62, u_resid = pos | 1<<63), and judges every row with spec_verify_row / 10// spec_sample. Every out[t] must match, accepted or not. Needs a GPU. 11#include "strata/core/spec_prob.hpp" 12#include "strata/kernels/sampler.hpp" 13 14#include <cuda_runtime.h> 15 16#include <algorithm> 17#include <cmath> 18#include <cstdio> 19#include <cstdlib> 20#include <cstring> 21#include <random> 22#include <vector> 23 24using strata::core::SpecP ; 25using strata::core::SpecQ ; 26using strata::kernels::SamplerParams ; 27 28namespace { 29 30void check (cudaError_t e ,const char * what ) { 31if (e != cudaSuccess ) { 32 std::fprintf (stderr ,"%s: %s\n" ,what ,cudaGetErrorString (e )); 33 std::exit (1 ); 34 } 35} 36 37float host_philox_uniform (uint64_t seed ,uint64_t counter ) { 38uint32_t c0 = (uint32_t )counter ,c1 = (uint32_t ) (counter >>32 ); 39uint32_t c2 = (uint32_t )seed ,c3 = (uint32_t ) (seed >>32 ); 40for (int i = 0 ;i < 10 ;++ i ) { 41const uint32_t hi0 = (uint32_t ) (((uint64_t )0x9E3779B9u * c0 ) >>32 ); 42const uint32_t hi1 = (uint32_t ) (((uint64_t )0xBB67AE85u * c2 ) >>32 ); 43const uint32_t lo0 = 0x9E3779B9u * c0 ; 44const uint32_t lo1 = 0xBB67AE85u * c2 ; 45const uint32_t n0 = hi1 ^c1 ^ (uint32_t )i ; 46const uint32_t n2 = hi0 ^c3 ^0u ; 47c0 = n0 ;c1 = lo1 ;c2 = n2 ;c3 = lo0 ; 48 } 49return (float ) (c0 >>8 )* (1.0f /16777216.0f ); 50} 51 52struct PList { 53 std::vector < int32_t > id ; 54 std::vector < double > p ; 55 std::vector < float > pf ; 56}; 57 58// the sampler's chain over one row (penalties over `hist`), as sampled_reference in sampler_parity.cpp 59PList chain_list (const std::vector < float >& l ,const std::vector < int >& hist ,const SamplerParams & p ) { 60auto penal = [& ](float logit ,int count ) { 61if (count <=0 )return logit ; 62if (logit <=0.0f )logit *=p .penalty_repeat ;else logit /=p .penalty_repeat ; 63logit -= (float )count * p .penalty_freq + (count > 0 ?1.0f :0.0f )* p .penalty_present ; 64return logit ; 65 }; 66const int nv = (int )l .size (); 67 std::vector < float > s ((size_t )nv ); 68for (int v = 0 ;v < nv ;++ v )s [(size_t )v ]= l [(size_t )v ]; 69if (p .penalty_last_n > 0 ) { 70 std::vector < int > cnt ((size_t )nv ,0 ); 71const int h = (int )hist .size (); 72for (int i = std::max (0 ,h - p .penalty_last_n );i < h ;++ i ) 73if (hist [(size_t )i ] >=0 && hist [(size_t )i ]< nv )++ cnt [(size_t )hist [(size_t )i ]]; 74for (int v = 0 ;v < nv ;++ v )s [(size_t )v ]= penal (s [(size_t )v ],cnt [(size_t )v ]); 75 } 76const int k = std::min (nv , (p .top_k > 0 && p .top_k < 64 ) ?p .top_k :64 ); 77 std::vector < int > idx ((size_t )nv ); 78for (int i = 0 ;i < nv ;++ i )idx [(size_t )i ]= i ; 79 std::partial_sort (idx .begin (),idx .begin ()+ k ,idx .end (), [& ](int a ,int b ) { 80return s [(size_t )a ]> s [(size_t )b ]|| (s [(size_t )a ]== s [(size_t )b ]&& a < b ); 81 }); 82 std::vector < float > sl ((size_t )k ); 83for (int i = 0 ;i < k ;++ i )sl [(size_t )i ]= s [(size_t )idx [(size_t )i ]]; 84int n_keep = k ; 85if (p .top_p < 1.0f ) { 86double sum = 0.0 ; 87for (int i = 0 ;i < k ;++ i )sum += std::exp ((double )sl [(size_t )i ]- (double )sl [0 ]); 88double cum = 0.0 ; 89int cut = k ; 90for (int i = 0 ;i < k ;++ i ) { 91cum += std::exp ((double )sl [(size_t )i ]- (double )sl [0 ]) /sum ; 92if (cum >= (double )p .top_p ) {cut = i + 1 ;break ; } 93 } 94if (cut < p .min_keep )cut = p .min_keep < k ?p .min_keep :k ; 95n_keep = cut ; 96 } 97if (p .min_p > 0.0f ) { 98const float thresh = sl [0 ]+ std::log (p .min_p ); 99for (int i = 0 ;i < n_keep ;++ i ) 100if (sl [(size_t )i ]< thresh ) {n_keep = i ;break ; } 101 } 102const float inv_t = 1.0f /p .temperature ; 103float smx = sl [0 ]* inv_t ; 104for (int i = 1 ;i < n_keep ;++ i )smx = std::fmax (smx ,sl [(size_t )i ]* inv_t ); 105PList L ; 106double sum = 0.0 ; 107 std::vector < double > e ((size_t )n_keep ); 108for (int i = 0 ;i < n_keep ;++ i ) { 109e [(size_t )i ]= std::exp ((double ) (sl [(size_t )i ]* inv_t )- (double )smx ); 110sum += e [(size_t )i ]; 111 } 112for (int i = 0 ;i < n_keep ;++ i ) { 113L .id .push_back (idx [(size_t )i ]); 114L .p .push_back (e [(size_t )i ] /sum ); 115L .pf .push_back ((float ) (e [(size_t )i ] /sum )); 116 } 117return L ; 118} 119 120}// namespace 121 122int main (int argc ,char ** argv ) { 123 (void )argc ; (void )argv ; 124 std::mt19937_64 g (777 ); 125long long rows = 0 ,accepted = 0 ,rejected = 0 ,plain = 0 ,bad = 0 ,qrows_total = 0 ; 126const int vocabs []= {1000 ,50000 ,248320 }; 127const int Ts []= {2 ,3 ,5 ,8 }; 128const int kMaxT = 8 ; 129float * d_logits = nullptr ; 130int * d_out = nullptr ; 131int32_t * d_hist = nullptr ; 132int32_t * d_dtok = nullptr ; 133int32_t * d_q = nullptr ; 134check (cudaMalloc ((void ** )& d_logits , (size_t )kMaxT * 248320 * sizeof (float )),"malloc logits" ); 135check (cudaMalloc ((void ** )& d_out ,kMaxT * sizeof (int )),"malloc out" ); 136check (cudaMalloc ((void ** )& d_hist , (size_t )kMaxT * 64 * sizeof (int32_t )),"malloc hist" ); 137check (cudaMalloc ((void ** )& d_dtok ,kMaxT * sizeof (int32_t )),"malloc dtok" ); 138check (cudaMalloc ((void ** )& d_q , (size_t )kMaxT * strata::core::kSpecQStride * sizeof (int32_t )),"malloc q" ); 139for (int nv :vocabs ) { 140for (int T :Ts ) { 141for (int rep = 0 ;rep < 24 ;++ rep ) { 142SamplerParams p ; 143const int tk []= {1 ,5 ,20 ,64 ,0 }; 144const float tp []= {1.0f ,0.95f ,0.5f }; 145const float mp []= {0.0f ,0.05f ,0.1f }; 146const float tt []= {0.3f ,0.7f ,1.0f ,1.5f }; 147p .top_k = tk [g () %5 ]; 148p .top_p = tp [g () %3 ]; 149p .min_p = mp [g () %3 ]; 150p .temperature = tt [g () %4 ]; 151p .seed = g (); 152p .counter = g () %100000 ; 153const bool pen = rep %3 == 2 ; 154if (pen ) {p .penalty_last_n = 32 ;p .penalty_repeat = 1.2f ;p .penalty_freq = 0.1f ;p .penalty_present = 0.2f ; } 155 std::normal_distribution < float > nd (0.0f ,2.5f ); 156 std::vector < std::vector < float >> logits ((size_t )T , std::vector < float > ((size_t )nv )); 157 std::vector < float > flat ((size_t )T * nv ); 158for (int t = 0 ;t < T ;++ t ) { 159for (int v = 0 ;v < nv ;++ v )logits [(size_t )t ][(size_t )v ]= nd (g ); 160// a few clustered peaks, so top_p / min_p cut at interesting places 161for (int j = 0 ;j < 6 ;++ j )logits [(size_t )t ][g () %nv ]+= 4.0f + (float ) (g () %100 ) /25.0f ; 162 std::copy (logits [(size_t )t ].begin (),logits [(size_t )t ].end (),flat .begin ()+ (size_t )t * nv ); 163 } 164 std::vector < int32_t > hist ((size_t )T * 64 ,-1 ); 165 std::vector < std::vector < int >> rowhist ((size_t )T ); 166if (pen ) 167for (int t = 0 ;t < T ;++ t ) { 168for (int i = 0 ;i < 40 ;++ i )hist [(size_t )t * 64 + 24 + (size_t )i ]= -1 ; 169rowhist [(size_t )t ].clear (); 170for (int i = 0 ;i < 32 ;++ i ) { 171const int v = (int ) (g () %200 ); 172hist [(size_t )t * 64 + 32 + (size_t )i ]= v ;// the last 32 slots 173rowhist [(size_t )t ].push_back (v ); 174 } 175 } 176// host: p lists, q lists, drafts 177 std::vector < PList > P ((size_t )T ),Q ((size_t )T ); 178const int n_q = (int ) (g () % (T ));// 0 .. T-1 rows with a list 179 std::vector < int32_t > dtok ((size_t ) (T - 1 ),0 ); 180 std::vector < int32_t > qbuf ((size_t )T * strata::core::kSpecQStride ,-1 ); 181for (int t = 0 ;t < T ;++ t )P [(size_t )t ]= chain_list (logits [(size_t )t ],rowhist [(size_t )t ],p ); 182for (int t = 0 ;t < T - 1 ;++ t ) { 183if (t < n_q ) { 184// the drafter's own chain: a perturbed copy, its own top_k / top_p / temperature, no penalties 185 std::vector < float > lq = logits [(size_t )t ]; 186for (float & x :lq )x += 0.7f * nd (g ) /2.5f ; 187SamplerParams q ; 188q .top_k = tk [g () %5 ];q .top_p = tp [g () %3 ];q .min_p = 0.0f ;q .temperature = tt [g () %4 ]; 189Q [(size_t )t ]= chain_list (lq , {},q ); 190int32_t * row = qbuf .data ()+ (size_t )t * strata::core::kSpecQStride ; 191float * prow = (float * ) (row + strata::core::kSpecQEntries ); 192for (size_t i = 0 ;i < Q [(size_t )t ].id .size ();++ i ) {row [i ]= Q [(size_t )t ].id [i ];prow [i ]= Q [(size_t )t ].pf [i ]; } 193// the draft: drawn from q 194const float u = (float ) (g () >>40 )* (1.0f /16777216.0f ); 195double cum = 0.0 ; 196int32_t d = Q [(size_t )t ].id .back (); 197for (size_t i = 0 ;i < Q [(size_t )t ].id .size ();++ i ) {cum += Q [(size_t )t ].p [i ];if ((double )u < cum ) {d = Q [(size_t )t ].id [i ];break ; } } 198dtok [(size_t )t ]= d ; 199 }else { 200const int r = (int ) (g () %10 ); 201const PList & L = P [(size_t )t ]; 202dtok [(size_t )t ]= r < 6 ?L .id [g () % std::min < size_t > (L .id .size (),5 )] : (int32_t ) (g () %nv ); 203 } 204 } 205check (cudaMemcpy (d_logits ,flat .data (),flat .size ()* sizeof (float ),cudaMemcpyHostToDevice ),"up logits" ); 206check (cudaMemcpy (d_hist ,hist .data (),hist .size ()* sizeof (int32_t ),cudaMemcpyHostToDevice ),"up hist" ); 207check (cudaMemcpy (d_dtok ,dtok .data (),dtok .size ()* sizeof (int32_t ),cudaMemcpyHostToDevice ),"up dtok" ); 208check (cudaMemcpy (d_q ,qbuf .data (),qbuf .size ()* sizeof (int32_t ),cudaMemcpyHostToDevice ),"up q" ); 209const bool ran = strata::kernels::sample_tokens_spec (d_logits ,T ,nv ,pen ?d_hist :nullptr ,pen ?64 :0 ,p , 210d_dtok ,d_q ,n_q ,d_out ,nullptr ); 211if (!ran ) { std::printf ("sample_tokens_spec did not run (nv %d T %d)\n" ,nv ,T );return 1 ; } 212check (cudaDeviceSynchronize (),"sync" ); 213int out [kMaxT ]; 214check (cudaMemcpy (out ,d_out , (size_t )T * sizeof (int ),cudaMemcpyDeviceToHost ),"down" ); 215for (int t = 0 ;t < T ;++ t ) { 216const uint64_t pos = p .counter + (uint64_t )t ; 217const SpecP sp {P [(size_t )t ].id .data (),P [(size_t )t ].p .data (), (int )P [(size_t )t ].id .size ()}; 218int32_t want ; 219if (t == T - 1 ) { 220want = strata::core::spec_sample (sp ,host_philox_uniform (p .seed ,pos )); 221++ plain ; 222 }else { 223SpecQ sq {nullptr ,nullptr ,-1 }; 224if (t < n_q )sq = SpecQ {Q [(size_t )t ].id .data (),Q [(size_t )t ].pf .data (), (int )Q [(size_t )t ].id .size ()}; 225const auto r = strata::core::spec_verify_row (sp ,dtok [(size_t )t ],sq , 226host_philox_uniform (p .seed ,pos | strata::core::kSpecCtrAccept ), 227host_philox_uniform (p .seed ,pos | strata::core::kSpecCtrResid )); 228want = r .token ; 229 (r .accepted ?accepted :rejected )+= 1 ; 230qrows_total += t < n_q ; 231 } 232++ rows ; 233if (out [t ]!= want ) { 234if (++ bad <=10 ) 235 std::printf ("MISMATCH nv %d T %d row %d: kernel %d host %d (draft %d, n_q %d, top_k %d top_p %.2f min_p %.2f temp %.1f%s)\n" , 236nv ,T ,t ,out [t ],want ,t < T - 1 ?dtok [(size_t )t ] :-1 ,n_q ,p .top_k ,p .top_p ,p .min_p , 237p .temperature ,pen ?" penalties" :"" ); 238 } 239 } 240 } 241 } 242 } 243 std::printf ("spec_verify_parity: %lld rows (%lld drafts kept, %lld rejected, %lld plain last rows, %lld with a q list): %lld mismatches\n" , 244rows ,accepted ,rejected ,plain ,qrows_total ,bad ); 245cudaFree (d_logits );cudaFree (d_out );cudaFree (d_hist );cudaFree (d_dtok );cudaFree (d_q ); 246return bad == 0 ?0 :1 ; 247}