okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
09817da39
main
1// src/kernels/decode_cluster_parity.cpp - S19: the decode kernels on thread-block clusters (sm_90+) against the ones 2// they replace, bit for bit (GPU, synthetic, no model). 3// 4// QSA top-k : qsa_block_topk_cluster vs qsa_block_topk's one-CTA dispatch (the register kernel up to 33,792 blocks, 5// block_topk_kernel above) and vs qsa_block_topk_ref - every id of every row of the `cap`-wide buffer, 6// including the entries past the width that neither may write. 7// argmax : sample_greedy_cluster vs sample_tokens' one-block greedy kernel. 8// 9// Inputs: random scores like a real indexer's, and adversarial ones - all equal, a few distinct values (ties at the 10// threshold across the CTAs of a cluster), NaN, +-inf, -0.0 against +0.0, denormals, monotone rows, all NaN; contexts 11// from below the selection width (the identity) to 262,144 cells, every tail weight (n_kv mod 4), 1-16 queries; and 12// one CUDA graph that captures both cluster launches, replayed with new inputs. 13// 14// decode_cluster_parity --selftest parity (exit 0 = identical everywhere); skipped (exit 0) below sm_90 15// decode_cluster_parity --bench per-call times, old vs new, at 32K / 128K / 262K 16// 17// The old paths are reached through the public dispatchers with STRATA_QSA_CLUSTER=0 / STRATA_ARGMAX_MULTI=0, set 18// here before their first call (they read the variables once). 19#include "strata/kernels/qsa.hpp" 20#include "strata/kernels/qsa_select.hpp" 21#include "strata/kernels/sampler.hpp" 22 23#include <cuda_runtime.h> 24 25#include <cmath> 26#include <cstdint> 27#include <cstdio> 28#include <cstdlib> 29#include <cstring> 30#include <limits> 31#include <random> 32#include <string> 33#include <vector> 34 35namespace k= strata::kernels; 36 37namespace { 38 39void ck (cudaError_t e ,const char * what ) { 40if (e != cudaSuccess ) { 41 std::fprintf (stderr ,"%s: %s\n" ,what ,cudaGetErrorString (e )); 42 std::exit (2 ); 43 } 44} 45 46void set_env (const char * name ,const char * value ) { 47#if defined(_WIN32 ) 48_putenv_s (name ,value ); 49#else 50setenv (name ,value ,1 ); 51#endif 52} 53 54template < typename T > struct Dev { 55T * p = nullptr ; 56size_t n = 0 ; 57explicit Dev (size_t count ) :n (count ) {ck (cudaMalloc (& p ,count * sizeof (T )+ 64 ),"cudaMalloc" ); } 58 ~Dev () {cudaFree (p ); } 59void put (const std::vector < T >& h ) { 60ck (cudaMemcpy (p ,h .data (),h .size ()* sizeof (T ),cudaMemcpyHostToDevice ),"up" ); 61// #548 #536: a pageable cudaMemcpy can return before its DMA lands, and the graph replays and the timing 62// cases run on a cudaStreamNonBlocking stream that does not wait for the legacy stream - so wait here 63ck (cudaDeviceSynchronize (),"up landed" ); 64 } 65 std::vector < T > get ()const { 66 std::vector < T > h (n ); 67ck (cudaMemcpy (h .data (),p ,n * sizeof (T ),cudaMemcpyDeviceToHost ),"down" ); 68return h ; 69 } 70}; 71 72const float kNaN = std::numeric_limits < float > ::quiet_NaN (),kInf = std::numeric_limits < float > ::infinity (); 73 74// ---- QSA top-k fixtures 75enum Pattern {kRandom ,kAllEqual ,kFewValues ,kNaNs ,kInfs ,kSignedZero ,kDenormal ,kAscending ,kDescending ,kAllNaN , 76kQuantized ,kPatterns }; 77const char * pattern_name (int p ) { 78static const char * n []= {"random" ,"all-equal" ,"few-values" ,"nan" ,"inf" ,"signed-zero" ,"denormal" ,"ascending" , 79"descending" ,"all-nan" ,"quantized" }; 80return n [p ]; 81} 82 83// one query's row of block scores (blocks 0..n_bid); the tail block gets the +1e9 the score kernel gives it 84void fill_row (float * row ,int64_t n_kv ,int64_t n_bid ,int pattern , std::mt19937 & rng ) { 85 std::normal_distribution < float > nd (0.f ,1.f ); 86 std::uniform_int_distribution < int > pick (0 ,99 ); 87for (int64_t b = 0 ;b <=n_bid ;++ b ) { 88float v = 0.f ; 89const float g = nd (rng ); 90switch (pattern ) { 91case kRandom :v = std::fmax (0.f ,g )* 3.f + std::fmax (0.f ,nd (rng ));break ;// relu sums: exact zeros too 92case kAllEqual :v = 1.f ;break ; 93case kFewValues :v = (float ) (pick (rng ) %4 )* 0.5f ;break ; 94case kNaNs :v = pick (rng )< 15 ?kNaN : std::fabs (g );break ; 95case kInfs : { 96const int r = pick (rng ); 97v = r < 5 ?kInf :r < 15 ?- kInf :r < 30 ?- std::fabs (g ) :g ; 98break ; 99 } 100case kSignedZero :v = pick (rng )< 50 ?-0.0f : (pick (rng )< 50 ?0.0f :- std::fabs (g )* 1e-3f );break ; 101case kDenormal :v = (float ) (pick (rng ) %7 )* 1e-41f ;break ; 102case kAscending :v = (float )b ;break ; 103case kDescending :v = (float ) (n_bid - b );break ; 104case kAllNaN :v = kNaN ;break ; 105case kQuantized :v = std::round (std::fabs (g )* 8.f ) /8.f ;break ; 106 } 107row [b ]= v ; 108 } 109if (n_kv %4 != 0 && pattern != kAllNaN && pattern != kAllEqual )row [n_bid ]+= 1e9f ; 110} 111 112struct TopkCase { 113int64_t ctx ;// the last query's n_kv 114int64_t capacity ;// --max-context: max_blocks = capacity / 4 + 2 (the engine's rule) 115int nq ; 116int pattern ; 117}; 118 119int run_topk_case (const TopkCase & c , std::mt19937 & rng ,bool verbose ) { 120const k::QsaShapes s = k::qsa_real_shapes (); 121const int64_t max_blocks = c .capacity /4 + 2 ,cap = k::qsa_selection_width (k::kTopkMaxCells ,s ); 122 std::vector < float > sc ((size_t ) (c .nq * max_blocks ),0.f ); 123 std::vector < int32_t > steps ((size_t )c .nq * k::kStepCount ); 124for (int i = 0 ;i < c .nq ;++ i ) { 125const int64_t n_kv = c .ctx - (c .nq - 1 - i );// the window's queries, one cell apart, the last at ctx 126int32_t * st = steps .data ()+ (size_t )i * k::kStepCount ; 127st [k::kStepPos ]= (int32_t ) (n_kv - 1 ); 128st [k::kStepNKv ]= (int32_t )n_kv ; 129st [k::kStepNBid ]= (int32_t ) (n_kv /s .idx_block ); 130st [k::kStepWidth ]= (int32_t ) k::qsa_selection_width (n_kv ,s ); 131fill_row (sc .data ()+ (size_t )i * max_blocks ,n_kv ,n_kv /s .idx_block ,c .pattern ,rng ); 132 } 133Dev < float > d_sc (sc .size ()); 134const size_t n_ids = (size_t ) (c .nq * cap ); 135Dev < int32_t > d_st (steps .size ()),d_new (n_ids ),d_old (n_ids ),d_ref (n_ids ); 136d_sc .put (sc ); 137d_st .put (steps ); 138ck (cudaMemset (d_new .p ,0xA5 ,d_new .n * 4 ),"memset" ); 139ck (cudaMemset (d_old .p ,0xA5 ,d_old .n * 4 ),"memset" ); 140ck (cudaMemset (d_ref .p ,0xA5 ,d_ref .n * 4 ),"memset" ); 141if (!k::qsa_block_topk_cluster (d_sc .p ,d_st .p ,c .nq ,max_blocks ,cap ,s ,d_new .p ,nullptr )) { 142 std::printf ("FAIL topk ctx %lld cap %lld nq %d: the cluster kernel did not run\n" , (long long )c .ctx , 143 (long long )c .capacity ,c .nq ); 144return 1 ; 145 } 146 k::qsa_block_topk (d_sc .p ,d_st .p ,c .nq ,max_blocks ,cap ,s ,d_old .p ,nullptr ); 147 k::qsa_block_topk_ref (d_sc .p ,d_st .p ,c .nq ,max_blocks ,cap ,s ,d_ref .p ,nullptr ); 148ck (cudaDeviceSynchronize (),"topk sync" ); 149const std::vector < int32_t > a = d_new .get (),b = d_old .get (),r = d_ref .get (); 150int64_t diff_old = 0 ,diff_ref = 0 ,first = -1 ; 151for (size_t i = 0 ;i < a .size ();++ i ) { 152if (a [i ]!= b [i ]) {++ diff_old ;if (first < 0 )first = (int64_t )i ; } 153if (a [i ]!= r [i ])++ diff_ref ; 154 } 155const bool ok = diff_old == 0 && diff_ref == 0 ; 156if (!ok || verbose ) 157 std::printf ("%s topk ctx %7lld cap %7lld nq %2d %-11s: %lld ids differ from the one-CTA dispatch, %lld from " 158"the reference%s\n" ,ok ?"ok " :"FAIL" , (long long )c .ctx , (long long )c .capacity ,c .nq , 159pattern_name (c .pattern ), (long long )diff_old , (long long )diff_ref , 160first >=0 ? (" (first at " + std::to_string (first )+ ")" ).c_str () :"" ); 161return ok ?0 :1 ; 162} 163 164// ---- argmax fixtures 165enum AmPattern {aRandom ,aTies ,aAllEqual ,aAllNegInf ,aAllNaN ,aNaNMax ,aPosInf ,aSignedZero ,aLast ,aFirst , 166aDenormal ,aTiesAcross ,aPatterns }; 167const char * am_name (int p ) { 168static const char * n []= {"random" ,"ties" ,"all-equal" ,"all--inf" ,"all-nan" ,"nan+max" ,"+inf" ,"signed-zero" , 169"max-last" ,"max-first" ,"denormal" ,"ties-across" }; 170return n [p ]; 171} 172 173void fill_logits (float * l ,int nv ,int pattern , std::mt19937 & rng ) { 174 std::normal_distribution < float > nd (0.f ,3.f ); 175 std::uniform_int_distribution < int > at (0 ,nv - 1 ); 176for (int v = 0 ;v < nv ;++ v )l [v ]= nd (rng ); 177switch (pattern ) { 178case aRandom :break ; 179case aTies : {const int n = 1 + at (rng ) %9 ;for (int i = 0 ;i < n ;++ i )l [at (rng )]= 100.f ;break ; } 180case aAllEqual :for (int v = 0 ;v < nv ;++ v )l [v ]= 0.25f ;break ; 181case aAllNegInf :for (int v = 0 ;v < nv ;++ v )l [v ]= - kInf ;break ; 182case aAllNaN :for (int v = 0 ;v < nv ;++ v )l [v ]= kNaN ;break ; 183case aNaNMax : 184for (int v = 0 ;v < nv ;++ v )if (at (rng ) %3 == 0 )l [v ]= kNaN ; 185l [at (rng )]= 50.f ; 186l [at (rng )]= 50.f ; 187break ; 188case aPosInf :for (int i = 0 ;i < 4 ;++ i )l [at (rng )]= kInf ;break ; 189case aSignedZero : 190for (int v = 0 ;v < nv ;++ v )l [v ]= - std::fabs (l [v ])- 1.f ; 191for (int i = 0 ;i < 6 ;++ i )l [at (rng )]= (i & 1 ) ?0.0f :-0.0f ; 192break ; 193case aLast :l [nv - 1 ]= 1e30f ;break ; 194case aFirst :l [0 ]= 1e30f ;if (nv > 1 )l [nv - 1 ]= 1e30f ;break ; 195case aDenormal :for (int v = 0 ;v < nv ;++ v )l [v ]= (float ) (at (rng ) %5 )* 1e-42f - 1e-30f * (v & 1 );break ; 196case aTiesAcross :// the same maximum in several CTAs' and threads' ranges (8 x 1024 stride), plus -inf, NaN 197for (int v = 0 ;v < nv ;v += 1 + at (rng ) %9000 )l [v ]= 7.f ; 198for (int v = 3 ;v < nv ;v += 11 )l [v ]= kNaN ; 199for (int v = 5 ;v < nv ;v += 13 )l [v ]= - kInf ; 200break ; 201 } 202} 203 204int run_argmax_case (int nv ,int T ,int pattern , std::mt19937 & rng ,bool verbose ) { 205 std::vector < float > l ((size_t )nv * T ); 206for (int t = 0 ;t < T ;++ t )fill_logits (l .data ()+ (size_t )t * nv ,nv ,t %2 == 0 ?pattern :aRandom ,rng ); 207Dev < float > d_l (l .size ()); 208Dev < int > d_new ((size_t )T ),d_old ((size_t )T ); 209d_l .put (l ); 210ck (cudaMemset (d_new .p ,0xA5 , (size_t )T * 4 ),"memset" ); 211ck (cudaMemset (d_old .p ,0x5A , (size_t )T * 4 ),"memset" ); 212 k::SamplerParams sp ; 213sp .greedy = true; 214sp .temperature = 0.f ; 215if (!k::sample_greedy_cluster (d_l .p ,T ,nv ,d_new .p ,nullptr )) { 216 std::printf ("FAIL argmax nv %d T %d: the cluster kernel did not run\n" ,nv ,T ); 217return 1 ; 218 } 219 k::sample_tokens (d_l .p ,T ,nv ,nullptr ,0 ,sp ,d_old .p ,nullptr ); 220ck (cudaDeviceSynchronize (),"argmax sync" ); 221const std::vector < int > a = d_new .get (),b = d_old .get (); 222// and an independent host answer: the lowest index of the largest non-NaN value above -inf, else 0 223int bad = 0 ; 224for (int t = 0 ;t < T ;++ t ) { 225const float * r = l .data ()+ (size_t )t * nv ; 226int hb = -1 ; 227for (int v = 0 ;v < nv ;++ v ) 228if (r [v ]> - kInf && (hb < 0 || r [v ]> r [hb ]))hb = v ; 229if (hb < 0 )hb = 0 ; 230if (a [(size_t )t ]!= b [(size_t )t ]|| a [(size_t )t ]!= hb )++ bad ; 231 } 232if (bad || verbose ) 233 std::printf ("%s argmax nv %6d T %d %-11s: %d rows differ (new %d, old %d)\n" ,bad ?"FAIL" :"ok " ,nv ,T , 234am_name (pattern ),bad ,a [0 ],b [0 ]); 235return bad ?1 :0 ; 236} 237 238// both cluster launches captured in one graph, replayed over new inputs written into the same buffers 239int run_graph_case (std::mt19937 & rng ) { 240const k::QsaShapes s = k::qsa_real_shapes (); 241const int nq = 5 ,nv = 248320 ; 242const int64_t capacity = 131072 + 272 ,max_blocks = capacity /4 + 2 ; 243const int64_t cap = k::qsa_selection_width (k::kTopkMaxCells ,s ); 244Dev < float > d_sc ((size_t ) (nq * max_blocks )),d_l ((size_t )nv * nq ); 245Dev < int32_t > d_st ((size_t )nq * k::kStepCount ),d_new ((size_t ) (nq * cap )),d_old ((size_t ) (nq * cap )); 246Dev < int > a_new ((size_t )nq ),a_old ((size_t )nq ); 247ck (cudaMemset (d_new .p ,0 ,d_new .n * 4 ),"memset" );// the entries past a row's width: written by neither 248ck (cudaMemset (d_old .p ,0 ,d_old .n * 4 ),"memset" ); 249cudaStream_t cs ; 250ck (cudaStreamCreateWithFlags (& cs ,cudaStreamNonBlocking ),"stream" ); 251cudaGraph_t g = nullptr ; 252cudaGraphExec_t ge = nullptr ; 253ck (cudaStreamBeginCapture (cs ,cudaStreamCaptureModeThreadLocal ),"begin capture" ); 254const bool l1 = k::qsa_block_topk_cluster (d_sc .p ,d_st .p ,nq ,max_blocks ,cap ,s ,d_new .p ,cs ); 255const bool l2 = k::sample_greedy_cluster (d_l .p ,nq ,nv ,a_new .p ,cs ); 256ck (cudaStreamEndCapture (cs ,& g ),"end capture" ); 257if (!l1 && !l2 ) {// no cluster code here (a card or a build below sm_90): nothing to compare 258if (g )cudaGraphDestroy (g ); 259cudaStreamDestroy (cs ); 260return -1 ; 261 } 262if (!l1 || !l2 ) { std::printf ("FAIL graph: one cluster kernel did not run\n" );return 1 ; } 263ck (cudaGraphInstantiate (& ge ,g ,0 ),"instantiate" ); 264int fails = 0 ; 265for (int rep = 0 ;rep < 6 ;++ rep ) { 266const int64_t ctx = 100000 + rep * 4001 + rep %4 ; 267 std::vector < float > sc ((size_t ) (nq * max_blocks ),0.f ),l ((size_t )nv * nq ); 268 std::vector < int32_t > steps ((size_t )nq * k::kStepCount ); 269for (int i = 0 ;i < nq ;++ i ) { 270const int64_t n_kv = ctx - (nq - 1 - i ); 271int32_t * st = steps .data ()+ (size_t )i * k::kStepCount ; 272st [k::kStepPos ]= (int32_t ) (n_kv - 1 ); 273st [k::kStepNKv ]= (int32_t )n_kv ; 274st [k::kStepNBid ]= (int32_t ) (n_kv /4 ); 275st [k::kStepWidth ]= (int32_t ) k::qsa_selection_width (n_kv ,s ); 276fill_row (sc .data ()+ (size_t )i * max_blocks ,n_kv ,n_kv /4 ,rep %2 ?kFewValues :kRandom ,rng ); 277fill_logits (l .data ()+ (size_t )i * nv ,nv ,rep %2 ?aTiesAcross :aRandom ,rng ); 278 } 279d_sc .put (sc ); 280d_st .put (steps ); 281d_l .put (l ); 282ck (cudaGraphLaunch (ge ,cs ),"graph launch" ); 283 k::qsa_block_topk (d_sc .p ,d_st .p ,nq ,max_blocks ,cap ,s ,d_old .p ,cs ); 284 k::SamplerParams sp ; 285sp .greedy = true; 286 k::sample_tokens (d_l .p ,nq ,nv ,nullptr ,0 ,sp ,a_old .p ,cs ); 287ck (cudaStreamSynchronize (cs ),"graph sync" ); 288const auto x = d_new .get (),y = d_old .get (); 289const auto u = a_new .get (),v = a_old .get (); 290int64_t d1 = 0 ,d2 = 0 ; 291for (size_t i = 0 ;i < x .size ();++ i )d1 += x [i ]!= y [i ]; 292for (size_t i = 0 ;i < u .size ();++ i )d2 += u [i ]!= v [i ]; 293if (d1 || d2 )++ fails ; 294 std::printf ("%s graph replay %d (ctx %lld): %lld ids, %lld tokens differ\n" ,d1 || d2 ?"FAIL" :"ok " ,rep , 295 (long long )ctx , (long long )d1 , (long long )d2 ); 296 } 297cudaGraphExecDestroy (ge ); 298cudaGraphDestroy (g ); 299cudaStreamDestroy (cs ); 300return fails ; 301} 302 303// back-to-back launches on `cs` (the engine's are too: one stream, inside a graph) 304template < typename F > float time_us (cudaStream_t cs ,F && f ,int reps = 300 ) { 305cudaEvent_t a ,b ; 306cudaEventCreate (& a ); 307cudaEventCreate (& b ); 308for (int i = 0 ;i < 5 ;++ i )f (); 309cudaEventRecord (a ,cs ); 310for (int i = 0 ;i < reps ;++ i )f (); 311cudaEventRecord (b ,cs ); 312ck (cudaEventSynchronize (b ),"time" ); 313float ms = 0.f ; 314cudaEventElapsedTime (& ms ,a ,b ); 315cudaEventDestroy (a ); 316cudaEventDestroy (b ); 317return ms * 1000.f / (float )reps ; 318} 319 320int bench () { 321 std::mt19937 rng (11 ); 322const k::QsaShapes s = k::qsa_real_shapes (); 323const int64_t cap = k::qsa_selection_width (k::kTopkMaxCells ,s ); 324cudaStream_t cs ; 325ck (cudaStreamCreateWithFlags (& cs ,cudaStreamNonBlocking ),"stream" ); 326 std::printf ("QSA top-k, per call (us); capacity = context + 272 as the speed runs set --max-context\n" ); 327for (const int64_t ctx : {32768LL ,131072LL ,262144LL - 272 }) { 328for (const int nq : {1 ,5 }) { 329const int64_t capacity = ctx + 272 ,max_blocks = capacity /4 + 2 ; 330 std::vector < float > sc ((size_t ) (nq * max_blocks ),0.f ); 331 std::vector < int32_t > steps ((size_t )nq * k::kStepCount ); 332for (int i = 0 ;i < nq ;++ i ) { 333const int64_t n_kv = ctx - (nq - 1 - i ); 334int32_t * st = steps .data ()+ (size_t )i * k::kStepCount ; 335st [k::kStepPos ]= (int32_t ) (n_kv - 1 ); 336st [k::kStepNKv ]= (int32_t )n_kv ; 337st [k::kStepNBid ]= (int32_t ) (n_kv /4 ); 338st [k::kStepWidth ]= (int32_t ) k::qsa_selection_width (n_kv ,s ); 339fill_row (sc .data ()+ (size_t )i * max_blocks ,n_kv ,n_kv /4 ,kRandom ,rng ); 340 } 341Dev < float > d_sc (sc .size ()); 342Dev < int32_t > d_st (steps .size ()),d_ids ((size_t ) (nq * cap )); 343d_sc .put (sc ); 344d_st .put (steps ); 345const float * sp = d_sc .p ; 346const int32_t * st = d_st .p ; 347int32_t * ids = d_ids .p ; 348const float t_old = time_us (cs , [& ] { k::qsa_block_topk (sp ,st ,nq ,max_blocks ,cap ,s ,ids ,cs ); }); 349const float t_ref = time_us (cs , [& ] { k::qsa_block_topk_ref (sp ,st ,nq ,max_blocks ,cap ,s ,ids ,cs ); }); 350const float t_new = time_us (cs , [& ] { k::qsa_block_topk_cluster (sp ,st ,nq ,max_blocks ,cap ,s ,ids ,cs ); }); 351 std::printf (" ctx %6lld nq %d: one-CTA dispatch %7.1f (ref %7.1f) cluster %6.1f -> %.1fx\n" , 352 (long long )ctx ,nq ,t_old ,t_ref ,t_new ,t_old /t_new ); 353 } 354 } 355 std::printf ("argmax, per call (us)\n" ); 356for (const int nv : {248320 ,32768 }) { 357for (const int T : {1 ,5 }) { 358 std::vector < float > l ((size_t )nv * T ); 359for (int t = 0 ;t < T ;++ t )fill_logits (l .data ()+ (size_t )t * nv ,nv ,aRandom ,rng ); 360Dev < float > d_l (l .size ()); 361Dev < int > d_o ((size_t )T ); 362d_l .put (l ); 363 k::SamplerParams sp ; 364sp .greedy = true; 365const float t_old = time_us (cs , [& ] { k::sample_tokens (d_l .p ,T ,nv ,nullptr ,0 ,sp ,d_o .p ,cs ); }); 366const float t_new = time_us (cs , [& ] { k::sample_greedy_cluster (d_l .p ,T ,nv ,d_o .p ,cs ); }); 367 std::printf (" n_vocab %6d T %d: one block %6.1f cluster %5.1f -> %.1fx\n" ,nv ,T ,t_old ,t_new , 368t_old /t_new ); 369 } 370 } 371cudaStreamDestroy (cs ); 372return 0 ; 373} 374 375}// namespace 376 377int main (int argc ,char ** argv ) { 378set_env ("STRATA_QSA_CLUSTER" ,"0" );// the dispatchers: the one-CTA kernels (the cluster ones called directly) 379set_env ("STRATA_ARGMAX_MULTI" ,"0" ); 380const bool do_bench = argc > 1 && std::strcmp (argv [1 ],"--bench" )== 0 ; 381const bool verbose = argc > 2 && std::strcmp (argv [2 ],"-v" )== 0 ; 382int dev = 0 ,major = 0 ; 383ck (cudaGetDevice (& dev ),"device" ); 384ck (cudaDeviceGetAttribute (& major ,cudaDevAttrComputeCapabilityMajor ,dev ),"cc" ); 385 std::mt19937 rng (20261002 ); 386// first, so that both kernels' first calls (their per-device checks and opt-ins) happen inside a stream capture, 387// as in the engine's decode graphs 388const int gfails = run_graph_case (rng ); 389if (gfails < 0 ) { 390 std::printf ("SKIP: no thread-block clusters on this card / build (compute capability %d)\n" ,major ); 391return 0 ; 392 } 393if (do_bench )return bench (); 394int fails = 0 ,cases = 0 ; 395// contexts: below the width (identity), at it, just past it, odd tails, the register kernel's range, past it 396// (block_topk_kernel), up to 262,144; capacities: tight (the speed runs) and 262,144 (a long --max-context) 397const int64_t ctxs []= {7 ,2050 ,2051 ,2052 ,2053 ,2054 ,2055 ,3001 ,9000 ,32768 ,32769 ,65538 ,100003 , 398131072 ,135170 ,135171 ,200001 ,262142 ,262143 ,262144 }; 399for (const int64_t ctx :ctxs ) { 400for (const int64_t capacity : {ctx + 272 , (int64_t )262144 + 272 }) { 401if (capacity < ctx )continue ; 402for (int p = 0 ;p < kPatterns ;++ p ) { 403const int nq = (p %3 == 0 ) ?1 : (p %3 == 1 ?5 :16 ); 404if (nq > ctx )continue ; 405fails += run_topk_case ({ctx ,capacity ,nq ,p },rng ,verbose ); 406++ cases ; 407 } 408 } 409 } 410 std::printf ("QSA top-k: %d cases, %d failed\n" ,cases ,fails ); 411int afails = 0 ,acases = 0 ; 412for (const int nv : {248320 ,248321 ,248319 ,32771 ,24576 ,8193 ,1000 ,33 ,1 }) { 413for (int p = 0 ;p < aPatterns ;++ p ) { 414for (const int T : {1 ,5 }) { 415afails += run_argmax_case (nv ,T ,p ,rng ,verbose ); 416++ acases ; 417 } 418 } 419 } 420 std::printf ("argmax: %d cases, %d failed\n" ,acases ,afails ); 421const int total = fails + afails + gfails ; 422 std::printf ("%s\n" ,total == 0 ?"PASS: the cluster kernels are bitwise identical" :"FAIL" ); 423return total == 0 ?0 :1 ; 424}