okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
7c5e84612
main
1// Recovered from the Claude Daily merge parity harness (lab/wip-0809), extended to the Daily's chunk kernel. 2// src/kernels/qsa_merge_daily_parity.cpp - the Daily's decode attention (STRATA_ATTN_DAILY=1: attn_chunk_kernel_pre75 3// and attn_merge_v2_kernel) against upstream's (attn_chunk_kernel, attn_merge_kernel), bitwise (GPU, synthetic, no 4// model). 5// 6// The reference is upstream's pair: one chunk pass with attn_chunk_kernel (qsa_decode_attn_chunks_only, daily_chunk 0) 7// and attn_merge_kernel (qsa_decode_attn_merge_only, v2 0). Against it, memcmp of: attn_merge_v2_kernel on the same 8// partials; the production entry points (qsa_decode_attn_batch, and _step at n_q 1) as the environment sets them; the 9// Daily's chunk kernel (daily_chunk 1, NaN-filled scratch again) merged by both mergers. Kernels are chosen by the 10// arguments, never by the environment, except for the production entries. Covers: 11// - the four KV formats the chunk kernel reads: fp16, int8, q4_0 (rotated or not, the merge cannot tell), k8v4; 12// - page tables with unresident (-1) pages: scattered single pages, and one run of 64 pages (256 consecutive cells) 13// that the window-shaped selections land on, so whole chunks inside the width are masked (m = -FLT_MAX, l = 0, 14// acc = 0) and some queries are masked entirely (output 0); 15// - widths {-1, 0, 1, 63, 64, 65, 1023, 1024, 1025, 2051, 4096, 16383, 16385, 32768, INT_MAX} per query (1023-1025 and 16383 / 16// 16385 sit on the v2 merge's 16-chunk block edges), all queries alike and mixed within one batch; a width above 17// aligned cap (2112/32768) is bounded by the chunk grid; an unaligned cap2051 keeps width<=cap; 18// - n_q 1..6, caps2051/2112 (main selection and its rounded capacity) and32768 (the drafter's 19// default --mtp-window); 20// - selections shaped like the drafter's window (consecutive cells, window_ids), like the indexer's (ascending, with 21// gaps) and unordered with repeats. 22// The scratch is filled with NaN before every chunk pass, so a merge that read a partial the chunk pass did not write 23// (a chunk past the width, say) would differ from the other one or produce a NaN. ~260 MB of VRAM. 24// 25// qsa_merge_daily_parity [--device N] the sweep; exit 0 when every case is bit-identical (77: no GPU) 26// qsa_merge_daily_parity [--device N] --bench plus each merge's time per call at target/drafter caps2051/32768 27#include "strata/kernels/kv_q4.hpp" 28#include "strata/kernels/kv_q8.hpp" 29#include "strata/kernels/qsa.hpp" 30#include "strata/kernels/qsa_decode_attn.hpp" 31 32#include <cuda_runtime.h> 33 34#include <algorithm> 35#include <cmath> 36#include <cstdint> 37#include <cstdio> 38#include <cstdlib> 39#include <cstring> 40#include <limits> 41#include <random> 42#include <vector> 43 44namespace k= strata::kernels; 45 46namespace { 47void ck (cudaError_t e ,const char * what ) { 48if (e != cudaSuccess ) { 49 std::fprintf (stderr ,"qsa_merge_daily_parity: %s: %s\n" ,what ,cudaGetErrorString (e )); 50 std::exit (2 ); 51 } 52} 53template < typename T > T * dalloc (size_t n ) { 54T * p = nullptr ; 55ck (cudaMalloc ((void ** )& p ,n * sizeof (T )+ 256 ),"cudaMalloc" ); 56ck (cudaMemset (p ,0 ,n * sizeof (T )+ 256 ),"cudaMemset" ); 57return p ; 58} 59template < typename T > T * upload (const std::vector < T >& h ) { 60T * d = dalloc < T > (h .size ()); 61ck (cudaMemcpy (d ,h .data (),h .size ()* sizeof (T ),cudaMemcpyHostToDevice ),"upload" ); 62return d ; 63} 64 65// fp16 bits drawn directly: a random sign, exponent 11..15 (|x| in [2^-4, 2)), a random mantissa; finite by 66// construction. Scales are positive: INT8 [1/64, 1/32) (codes up to 127 give |x| < 4), Q4_0 [1/4, 1/2). 67uint16_t f16_value (uint32_t r ) {return (uint16_t ) ((r & 0x8000u ) | ((11u + (r >>16 ) %5u ) <<10 ) | (r & 0x3FFu )); } 68uint16_t f16_scale_q8 (uint32_t r ) {return (uint16_t ) ((9u <<10 ) | (r & 0x3FFu )); } 69uint16_t f16_scale_q4 (uint32_t r ) {return (uint16_t ) ((13u <<10 ) | (r & 0x3FFu )); } 70 71constexpr int kFmtF16 = 0 ,kFmtInt8 = 1 ,kFmtQ4 = 2 ,kFmtK8V4 = 3 ; 72const char * fmt_name (int f ) {return f == kFmtF16 ?"fp16" :f == kFmtInt8 ?"int8" :f == kFmtQ4 ?"q4_0" :"k8v4" ; } 73 74struct DevicePools {// every format's pools over the same physical rows; `view` picks what a KV mode reads 75uint16_t * k16 = nullptr ,* v16 = nullptr ,* k8s = nullptr ,* v8s = nullptr ; 76int8_t * k8 = nullptr ,* v8 = nullptr ; 77uint8_t * k4 = nullptr ,* v4 = nullptr ; 78 k::QsaAttnPools view (int fmt ,const int32_t * table )const { 79 k::QsaAttnPools p ; 80p .page_table = table ; 81if (fmt == kFmtF16 ) {p .k_pool = k16 ;p .v_pool = v16 ; } 82else if (fmt == kFmtInt8 ) {p .k_q = k8 ;p .v_q = v8 ;p .k_scale = k8s ;p .v_scale = v8s ; } 83else if (fmt == kFmtQ4 ) {p .k_q4 = k4 ;p .v_q4 = v4 ; } 84else {p .k_q = k8 ;p .k_scale = k8s ;p .v_q4 = v4 ; }// K8V4: INT8 K, Q4_0 V (qsa_decode_attn.cu kv_mode 3) 85return p ; 86 } 87}; 88 89DevicePools make_pools (int64_t rows ,int64_t D , std::mt19937 & rng ) { 90DevicePools p ; 91 { 92 std::vector < uint16_t > h ((size_t ) (rows * D )); 93for (auto & x :h )x = f16_value (rng ()); 94p .k16 = upload (h ); 95for (auto & x :h )x = f16_value (rng ()); 96p .v16 = upload (h ); 97 } 98 { 99 std::vector < int8_t > c ((size_t ) (rows * D )); 100for (auto & x :c )x = (int8_t ) ((int ) (rng () %255u )- 127 ); 101p .k8 = upload (c ); 102for (auto & x :c )x = (int8_t ) ((int ) (rng () %255u )- 127 ); 103p .v8 = upload (c ); 104 std::vector < uint16_t > sc ((size_t ) (rows * (D / k::KV_Q8_GROUP ))); 105for (auto & x :sc )x = f16_scale_q8 (rng ()); 106p .k8s = upload (sc ); 107for (auto & x :sc )x = f16_scale_q8 (rng ()); 108p .v8s = upload (sc ); 109 } 110 { 111const size_t blocks = (size_t ) (rows * (D / k::QK4_0 )); 112 std::vector < uint8_t > b (blocks * sizeof (k::block_q4_0 )); 113for (int side = 0 ;side < 2 ;++ side ) { 114for (size_t i = 0 ;i < blocks ;++ i ) { 115 k::block_q4_0 blk ; 116blk .d = f16_scale_q4 (rng ()); 117for (auto & q :blk .qs )q = (uint8_t ) (rng ()& 0xFFu ); 118 std::memcpy (b .data ()+ i * sizeof (k::block_q4_0 ),& blk ,sizeof (blk )); 119 } 120 (side == 0 ?p .k4 :p .v4 )= upload (b ); 121 } 122 } 123return p ; 124} 125 126struct Case { 127int fmt = 0 ; 128int64_t cap = 0 ; 129int n_q = 0 ; 130 std::vector < int32_t > widths ; 131}; 132 133// the cells this harness can address, and where the 64 unresident pages in a row sit (cells [kHole, kHole + 256)) 134constexpr int64_t kCells = 40960 ; 135constexpr int64_t kHole = 8192 ; 136 137// query `i`'s `cap` selection ids. Every entry is a valid cell: a width above the cap makes the chunk kernel read 138// all `cap` of them. 139void selection (int32_t * ids ,int64_t cap ,int width ,int shape , std::mt19937 & rng ) { 140const int64_t w = std::max < int64_t > (0 , std::min < int64_t > (width ,cap )); 141if (shape == 0 ) {// the drafter's window: consecutive cells (window_ids), often across the unresident run 142int64_t start ; 143if (rng () %2 == 0 )start = std::max < int64_t > (0 ,kHole - 64 * (int64_t ) (rng () %8 ));// chunk-aligned on it 144else start = (int64_t ) (rng () % (uint32_t ) (kCells - std::max < int64_t > (w ,1 ))); 145if (start + w > kCells )start = kCells - w ; 146for (int64_t j = 0 ;j < cap ;++ j )ids [j ]= (int32_t ) ((start + j ) %kCells ); 147 }else if (shape == 1 ) {// the indexer's: ascending with gaps 148 std::vector < int32_t > v ((size_t )cap ); 149for (auto & x :v )x = (int32_t ) (rng () % (uint32_t )kCells ); 150 std::sort (v .begin (),v .begin ()+ w ); 151 std::copy (v .begin (),v .end (),ids ); 152 }else {// unordered, with repeats 153for (int64_t j = 0 ;j < cap ;++ j )ids [j ]= (int32_t ) (rng () % (uint32_t )kCells ); 154 } 155} 156 157struct Harness { 158 k::QsaShapes s = k::qsa_real_shapes (); 159DevicePools pools ; 160int32_t * table = nullptr ; 161float * q = nullptr ,* scratch = nullptr ,* attn_old = nullptr ,* attn_new = nullptr ; 162int32_t * ids = nullptr ,* steps = nullptr ; 163int64_t max_q = 0 ,max_cap = 0 ; 164 165void init (std::mt19937 & rng ,int64_t max_q_ ,int64_t max_cap_ ) { 166max_q = max_q_ ; 167max_cap = max_cap_ ; 168const int64_t PS = s .page_size ,pages = kCells /PS ,H = s .n_head_kv ,D = s .head_dim ; 169// logical page -> physical page: a permutation, ~3% scattered unresident pages, and the 64-page hole 170 std::vector < int32_t > tab ((size_t )pages ); 171for (int64_t i = 0 ;i < pages ;++ i )tab [(size_t )i ]= (int32_t )i ; 172 std::shuffle (tab .begin (),tab .end (),rng ); 173for (int64_t i = 0 ;i < pages ;++ i ) 174if (rng () %32u == 0 )tab [(size_t )i ]= -1 ; 175for (int64_t i = kHole /PS ;i < (kHole + 256 ) /PS ;++ i )tab [(size_t )i ]= -1 ; 176table = upload (tab ); 177pools = make_pools (pages * H * PS ,D ,rng ); 178q = dalloc < float > ((size_t ) (max_q * s .n_head * D )); 179ids = dalloc < int32_t > ((size_t ) (max_q * max_cap )); 180steps = dalloc < int32_t > ((size_t ) (max_q * k::kStepCount )); 181scratch = dalloc < float > ((size_t ) (k::qsa_decode_attn_scratch_floats (max_cap ,s )* (uint64_t )max_q )); 182attn_old = dalloc < float > ((size_t ) (max_q * s .n_head * D )); 183attn_new = dalloc < float > ((size_t ) (max_q * s .n_head * D )); 184 } 185 186 ~Harness () { 187for (void * p : {static_cast < void *> (table ),static_cast < void *> (q ),static_cast < void *> (ids ), 188static_cast < void *> (steps ),static_cast < void *> (scratch ),static_cast < void *> (attn_old ), 189static_cast < void *> (attn_new ),static_cast < void *> (pools .k16 ),static_cast < void *> (pools .v16 ), 190static_cast < void *> (pools .k8 ),static_cast < void *> (pools .v8 ),static_cast < void *> (pools .k8s ), 191static_cast < void *> (pools .v8s ),static_cast < void *> (pools .k4 ),static_cast < void *> (pools .v4 )}) 192if (p )ck (cudaFree (p ),"free harness" ); 193 } 194 195// uploads the case's queries, selections and widths, and runs ONE chunk pass into NaN-filled scratch 196void prepare (const Case & c , std::mt19937 & rng ) { 197const int64_t D = s .head_dim ,NH = s .n_head ; 198 std::normal_distribution < float > nd (0.0f ,1.0f ); 199 std::vector < float > hq ((size_t ) (c .n_q * NH * D )); 200for (auto & x :hq )x = nd (rng ); 201ck (cudaMemcpy (q ,hq .data (),hq .size ()* 4 ,cudaMemcpyHostToDevice ),"q" ); 202 std::vector < int32_t > hid ((size_t ) (c .n_q * c .cap )); 203 std::vector < int32_t > hst ((size_t ) (c .n_q * k::kStepCount ),0 ); 204for (int i = 0 ;i < c .n_q ;++ i ) { 205selection (hid .data ()+ (size_t )i * c .cap ,c .cap ,c .widths [(size_t )i ], (int ) (rng () %3u ),rng ); 206hst [(size_t )i * k::kStepCount + k::kStepWidth ]= c .widths [(size_t )i ]; 207 } 208ck (cudaMemcpy (ids ,hid .data (),hid .size ()* 4 ,cudaMemcpyHostToDevice ),"ids" ); 209ck (cudaMemcpy (steps ,hst .data (),hst .size ()* 4 ,cudaMemcpyHostToDevice ),"steps" ); 210const size_t scratch_bytes = (size_t ) k::qsa_decode_attn_scratch_floats (c .cap ,s )* (size_t )c .n_q * 4 ; 211ck (cudaMemset (scratch ,0xFF ,scratch_bytes ),"scratch NaN fill" ); 212ck (cudaMemset (attn_old ,0xFF , (size_t ) (c .n_q * NH * D )* 4 ),"attn_old fill" ); 213ck (cudaMemset (attn_new ,0x7F , (size_t ) (c .n_q * NH * D )* 4 ),"attn_new fill" ); 214 k::qsa_decode_attn_chunks_only (q ,pools .view (c .fmt ,table ),ids ,steps ,c .cap ,s ,scratch ,c .n_q ,0 ,nullptr ); 215 } 216 217// true when the two merges' outputs are bit-identical (and the original's has no NaN) 218bool check (const Case & c ,long long & nonzero ) { 219const int64_t D = s .head_dim ,NH = s .n_head ; 220 k::qsa_decode_attn_merge_only (steps ,c .cap ,s ,scratch ,attn_old ,c .n_q ,0 ,nullptr ); 221 k::qsa_decode_attn_merge_only (steps ,c .cap ,s ,scratch ,attn_new ,c .n_q ,1 ,nullptr ); 222ck (cudaDeviceSynchronize (),"merge sync" ); 223const size_t n = (size_t ) (c .n_q * NH * D ); 224 std::vector < float > a (n ),b (n ); 225ck (cudaMemcpy (a .data (),attn_old ,n * 4 ,cudaMemcpyDeviceToHost ),"attn_old" ); 226ck (cudaMemcpy (b .data (),attn_new ,n * 4 ,cudaMemcpyDeviceToHost ),"attn_new" ); 227nonzero = 0 ; 228long long nan = 0 ; 229for (size_t i = 0 ;i < n ;++ i ) { 230if (a [i ]!= 0.0f )++ nonzero ; 231if (!std::isfinite (a [i ]))++ nan ; 232 } 233const bool same = std::memcmp (a .data (),b .data (),n * 4 )== 0 ; 234if (!same || nan ) { 235size_t first = 0 ; 236while (first < n && std::memcmp (& a [first ],& b [first ],4 )== 0 )++ first ; 237 std::printf ("FAIL %s cap %lld n_q %d widths" ,fmt_name (c .fmt ), (long long )c .cap ,c .n_q ); 238for (int32_t w :c .widths ) std::printf (" %d" ,w ); 239if (first < n ) { 240uint32_t ua ,ub ; 241 std::memcpy (& ua ,& a [first ],4 ); 242 std::memcpy (& ub ,& b [first ],4 ); 243 std::printf (": first difference at query %lld head %lld dim %lld: %.9g (0x%08x) vs %.9g (0x%08x)" , 244 (long long ) (first / (NH * D )), (long long ) (first /D %NH ), (long long ) (first %D ), 245a [first ],ua ,b [first ],ub ); 246 } 247if (nan ) std::printf (" (%lld nonfinite values in the original's output)" ,nan ); 248 std::printf ("\n" ); 249 } 250// Exercise production entry points too: either selected merger must equal the reference partials. 251bool entry_same = true; 252 k::qsa_decode_attn_batch (q ,pools .view (c .fmt ,table ),ids ,steps ,c .cap ,s ,scratch ,attn_new ,c .n_q ,nullptr ); 253ck (cudaDeviceSynchronize (),"batch entry sync" ); 254ck (cudaMemcpy (b .data (),attn_new ,n * 4 ,cudaMemcpyDeviceToHost ),"batch entry output" ); 255if (std::memcmp (a .data (),b .data (),n * 4 )!= 0 ) { 256 std::puts ("FAIL production batch differs from merge reference" ); 257entry_same = false; 258 } 259if (c .n_q == 1 ) { 260 k::qsa_decode_attn_step (q ,pools .view (c .fmt ,table ),ids ,steps ,c .cap ,s ,scratch ,attn_new ,nullptr ); 261ck (cudaDeviceSynchronize (),"step entry sync" ); 262ck (cudaMemcpy (b .data (),attn_new ,n * 4 ,cudaMemcpyDeviceToHost ),"step entry output" ); 263if (std::memcmp (a .data (),b .data (),n * 4 )!= 0 ) { 264 std::puts ("FAIL production step differs from merge reference" ); 265entry_same = false; 266 } 267 } 268// The Daily's chunk kernel on the same inputs, into NaN-filled scratch, merged by both mergers. 269bool daily_same = true; 270const size_t scratch_bytes = (size_t ) k::qsa_decode_attn_scratch_floats (c .cap ,s )* (size_t )c .n_q * 4 ; 271ck (cudaMemset (scratch ,0xFF ,scratch_bytes ),"scratch NaN refill" ); 272 k::qsa_decode_attn_chunks_only (q ,pools .view (c .fmt ,table ),ids ,steps ,c .cap ,s ,scratch ,c .n_q ,1 ,nullptr ); 273for (int v2 = 0 ;v2 < 2 ;++ v2 ) { 274ck (cudaMemset (attn_new ,0x7F ,n * 4 ),"attn_new refill" ); 275 k::qsa_decode_attn_merge_only (steps ,c .cap ,s ,scratch ,attn_new ,c .n_q ,v2 ,nullptr ); 276ck (cudaDeviceSynchronize (),"daily chunk sync" ); 277ck (cudaMemcpy (b .data (),attn_new ,n * 4 ,cudaMemcpyDeviceToHost ),"daily chunk output" ); 278if (std::memcmp (a .data (),b .data (),n * 4 )!= 0 ) { 279 std::printf ("FAIL Daily chunk kernel + %s differs from upstream reference\n" , 280v2 ?"attn_merge_v2_kernel" :"attn_merge_kernel" ); 281daily_same = false; 282 } 283 } 284return same && nan == 0 && entry_same && daily_same ; 285 } 286}; 287 288void bench (Harness & hx , std::mt19937 & rng ) { 289 std::printf ("\n--bench: CUDA events over 200 calls per arm, each arm warmed outside timing, same buffers.\n" 290"chunks BAB: B1(Daily chunk) -> A(upstream chunk) -> B2(Daily chunk); merges on hot partials:\n" 291"BAB: B1(v2) -> A(upstream merger) -> B2(v2).\n" ); 292 std::printf ("%-6s %6s %4s %6s | %9s %9s %9s %9s | %9s %9s %9s %9s\n" ,"fmt" ,"cap" ,"n_q" ,"width" , 293"chB1 us" ,"chA us" ,"chB2 us" ,"meanB/A" ,"B1 us" ,"A us" ,"B2 us" ,"meanB/A" ); 294cudaEvent_t e0 ,e1 ; 295ck (cudaEventCreate (& e0 ),"event" ); 296ck (cudaEventCreate (& e1 ),"event" ); 297const int iters = 200 ; 298for (int64_t cap : {(int64_t )2051 , (int64_t )32768 }) 299for (int fmt : {kFmtQ4 ,kFmtInt8 ,kFmtF16 }) { 300for (int n_q : {1 ,2 ,3 ,4 }) { 301for (int width : {64 ,2051 ,16384 ,31000 ,32768 }) { 302Case c ; 303c .fmt = fmt ; 304if (width > cap )continue ; 305c .cap = cap ; 306c .n_q = n_q ; 307c .widths .assign ((size_t )n_q ,width ); 308hx .prepare (c ,rng ); 309ck (cudaDeviceSynchronize (),"bench prepare" ); 310double us_ch [3 ]= {}; 311for (int arm = 0 ;arm < 3 ;++ arm ) {// B1 -> A -> B2: Daily, upstream, Daily chunk kernel 312const int daily = arm == 1 ?0 :1 ; 313auto chunks = [& ] { 314 k::qsa_decode_attn_chunks_only (hx .q ,hx .pools .view (fmt ,hx .table ),hx .ids ,hx .steps ,c .cap , 315hx .s ,hx .scratch ,n_q ,daily ,nullptr ); 316 }; 317for (int i = 0 ;i < iters ;++ i )chunks (); 318ck (cudaDeviceSynchronize (),"chunk warm" ); 319ck (cudaEventRecord (e0 ,nullptr ),"record" ); 320for (int i = 0 ;i < iters ;++ i )chunks (); 321ck (cudaEventRecord (e1 ,nullptr ),"record" ); 322ck (cudaEventSynchronize (e1 ),"sync" ); 323float ms_chunks = 0.0f ; 324ck (cudaEventElapsedTime (& ms_chunks ,e0 ,e1 ),"elapsed" ); 325us_ch [arm ]= 1000.0 * ms_chunks /iters ; 326 } 327const double ratio_ch = us_ch [1 ]> 0.0 ? (us_ch [0 ]+ us_ch [2 ]) / (2.0 * us_ch [1 ]) : std::nan ("" ); 328double us [3 ]= {}; 329for (int arm = 0 ;arm < 3 ;++ arm ) {// B1 -> A -> B2 at this identical shape 330const int v2 = arm == 1 ?0 :1 ; 331auto merge = [& ] { 332 k::qsa_decode_attn_merge_only (hx .steps ,c .cap ,hx .s ,hx .scratch ,hx .attn_new ,n_q ,v2 ,nullptr ); 333 }; 334for (int i = 0 ;i < iters ;++ i )merge (); 335ck (cudaDeviceSynchronize (),"merge arm warm" ); 336ck (cudaEventRecord (e0 ,nullptr ),"record" ); 337for (int i = 0 ;i < iters ;++ i )merge (); 338ck (cudaEventRecord (e1 ,nullptr ),"record" ); 339ck (cudaEventSynchronize (e1 ),"sync" ); 340float ms = 0.0f ; 341ck (cudaEventElapsedTime (& ms ,e0 ,e1 ),"elapsed" ); 342us [arm ]= 1000.0 * ms /iters ; 343 } 344const double ratio = us [1 ]> 0.0 ? (us [0 ]+ us [2 ]) / (2.0 * us [1 ]) : std::nan ("" ); 345 std::printf ("%-6s %6lld %4d %6d | %9.2f %9.2f %9.2f %9.4f | %9.2f %9.2f %9.2f %9.4f\n" , 346fmt_name (fmt ), (long long )cap ,n_q ,width ,us_ch [0 ],us_ch [1 ],us_ch [2 ],ratio_ch ,us [0 ], 347us [1 ],us [2 ],ratio ); 348 } 349 } 350 } 351ck (cudaEventDestroy (e0 ),"destroy event" ); 352ck (cudaEventDestroy (e1 ),"destroy event" ); 353} 354}// namespace 355 356int main (int argc ,char ** argv ) { 357int device = 0 ; 358bool do_bench = false; 359for (int i = 1 ;i < argc ;++ i ) { 360if (std::strcmp (argv [i ],"--bench" )== 0 )do_bench = true; 361else if (std::strcmp (argv [i ],"--device" )== 0 && i + 1 < argc )device = std::atoi (argv [++ i ]); 362else { 363 std::fprintf (stderr ,"usage: qsa_merge_daily_parity [--device N] [--bench]\n" ); 364return 2 ; 365 } 366 } 367int devices = 0 ; 368if (cudaGetDeviceCount (& devices )!= cudaSuccess || devices == 0 ) { 369 std::puts ("SKIP: no GPU" ); 370return 77 ; 371 } 372ck (cudaSetDevice (device ),"cudaSetDevice" ); 373cudaDeviceProp prop {}; 374ck (cudaGetDeviceProperties (& prop ,device ),"cudaGetDeviceProperties" ); 375 std::printf ("qsa_merge_daily_parity on device %d (%s, %d.%d)\n" ,device ,prop .name ,prop .major ,prop .minor ); 376 377 std::mt19937 rng (20261004u ); 378Harness hx ; 379hx .init (rng ,7 ,32768 ); 380 381// 1023 / 1024 / 1025 and 16383 / 16385: the v2 merge's U = 16 chunks x 64 cells block edges 382const int32_t widths []= {-1 ,0 ,1 ,63 ,64 ,65 ,1023 ,1024 ,1025 ,2051 ,4096 ,16383 ,16384 ,16385 ,32768 , std::numeric_limits < int32_t > ::max ()}; 383const int n_widths = (int ) (sizeof (widths ) /sizeof (widths [0 ])); 384long long cases = 0 ,failed = 0 ,nonzero_total = 0 ; 385for (int fmt = 0 ;fmt < 4 ;++ fmt ) { 386long long fmt_cases = 0 ,fmt_failed = 0 ; 387for (int64_t cap : {(int64_t )2051 , (int64_t )2112 , (int64_t )32768 }) { 388for (int n_q = 1 ;n_q <=6 ;++ n_q ) { 389// every width for all queries alike, then three batches of mixed widths 390for (int wc = 0 ;wc < n_widths + 3 ;++ wc ) { 391Case c ; 392c .fmt = fmt ; 393c .cap = cap ; 394c .n_q = n_q ; 395for (int i = 0 ;i < n_q ;++ i ) 396c .widths .push_back (wc < n_widths ?widths [wc ] :widths [rng () % (uint32_t )n_widths ]); 397// The unchanged chunk contract requires width<=cap for an unaligned last chunk. 398// Overflow widths remain tested at aligned caps, where the grid bounds the reads. 399if (cap %64 != 0 && std::any_of (c .widths .begin (),c .widths .end (), 400 [cap ](int32_t w ) {return (int64_t )w > cap ; }))continue ; 401hx .prepare (c ,rng ); 402long long nz = 0 ; 403const bool ok = hx .check (c ,nz ); 404nonzero_total += nz ; 405++ cases ; 406++ fmt_cases ; 407if (!ok ) {++ failed ;++ fmt_failed ; } 408 } 409 } 410 } 411 std::printf ("[%s] %lld cases, %s (%lld differ)\n" ,fmt_name (fmt ),fmt_cases ,fmt_failed ?"FAIL" :"bit-identical" , 412fmt_failed ); 413 } 414 std::printf ("qsa_merge_daily_parity: %lld cases, %lld differ, %lld nonzero output values in all -> %s\n" ,cases ,failed , 415nonzero_total ,failed == 0 && nonzero_total > 0 ?"PASS" :"FAIL" ); 416if (do_bench )bench (hx ,rng ); 417return failed == 0 && nonzero_total > 0 ?0 :1 ; 418}