okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
6ca60e9a0
main
1// src/kernels/qsa_pattn_parity.cpp - STRATA_PATTN_SM60's score and value kernels (qsa_pattn.cu, PR #3943) against the 2// decode kernels' output, bitwise (GPU, synthetic data, no model). 3// 4// The reference is what the prompt path reads on sm_6x today: the chunk pass of attn_chunk_kernel_pre75 (the Daily's 5// chunk kernel, qsa_decode_attn_chunks_only with daily_chunk 1, which runs on any card) and attn_merge_kernel 6// (qsa_decode_attn_merge_only, v2 0). The candidate is qsa_pattn_batch_any_arch (no compute-capability test), so the 7// same bytes can be checked on an Ampere card too: the two paths do the same operations in the same order, and a 8// pass there shows the indexing, masking and merge are right. The last word is a run on a Pascal card (P100): there 9// the checked entry qsa_pattn_batch must take the shapes and the production qsa_decode_attn_batch must give the same 10// bytes as the reference. 11// 12// Data: random int8 K/V codes, fp16 scales in a safe range, normal queries, a shuffled page table with ~3% unresident 13// (-1) pages and one run of 64 unresident pages (so whole chunks are masked and some queries read nothing), selections 14// shaped like the drafter's window, the indexer's (ascending with gaps) and unordered with repeats, caps 64 / 130 / 15// 2051 / 2112 (1, 3 and 33 chunks), widths 0 .. cap, n_q up to 40 with a scratch that holds only 2 queries of the 16// decode layout (so qsa_pattn_batch runs several batches). The pattn scratch and the output are filled with 17// junk before each call, so a read of a block that was not written shows up as a difference. 18// 19// qsa_pattn_parity [--device N] exit 0: every case is bit-identical; 77: no GPU or a build without the kernels 20#include "strata/kernels/qsa.hpp" 21#include "strata/kernels/qsa_decode_attn.hpp" 22#include "strata/kernels/qsa_pattn.hpp" 23 24#include <cuda_runtime.h> 25 26#include <algorithm> 27#include <cmath> 28#include <cstdint> 29#include <cstdio> 30#include <cstdlib> 31#include <cstring> 32#include <random> 33#include <vector> 34 35namespace k= strata::kernels; 36 37namespace { 38void ck (cudaError_t e ,const char * what ) { 39if (e != cudaSuccess ) { 40 std::fprintf (stderr ,"qsa_pattn_parity: %s: %s\n" ,what ,cudaGetErrorString (e )); 41 std::exit (2 ); 42 } 43} 44template < typename T > T * dalloc (size_t n ) { 45T * p = nullptr ; 46ck (cudaMalloc ((void ** )& p ,n * sizeof (T )+ 256 ),"cudaMalloc" ); 47ck (cudaMemset (p ,0 ,n * sizeof (T )+ 256 ),"cudaMemset" ); 48return p ; 49} 50template < typename T > T * upload (const std::vector < T >& h ) { 51T * d = dalloc < T > (h .size ()); 52ck (cudaMemcpy (d ,h .data (),h .size ()* sizeof (T ),cudaMemcpyHostToDevice ),"upload" ); 53return d ; 54} 55// positive fp16 scale in [1/64, 1/32): codes up to 127 give |x| < 4 56uint16_t f16_scale_q8 (uint32_t r ) {return (uint16_t ) ((9u <<10 ) | (r & 0x3FFu )); } 57 58constexpr int64_t kCells = 40960 ;// the cells this harness can address 59constexpr int64_t kHole = 8192 ;// 64 unresident pages in a row: cells [kHole, kHole + 256) 60 61void selection (int32_t * ids ,int64_t cap ,int width ,int shape , std::mt19937 & rng ) { 62const int64_t w = std::max < int64_t > (0 , std::min < int64_t > (width ,cap )); 63if (shape == 0 ) {// the drafter's window: consecutive cells, often across the unresident run 64int64_t start ; 65if (rng () %2 == 0 )start = std::max < int64_t > (0 ,kHole - 64 * (int64_t ) (rng () %8 )); 66else start = (int64_t ) (rng () % (uint32_t ) (kCells - std::max < int64_t > (w ,1 ))); 67if (start + w > kCells )start = kCells - w ; 68for (int64_t j = 0 ;j < cap ;++ j )ids [j ]= (int32_t ) ((start + j ) %kCells ); 69 }else if (shape == 1 ) {// the indexer's: ascending with gaps 70 std::vector < int32_t > v ((size_t )cap ); 71for (auto & x :v )x = (int32_t ) (rng () % (uint32_t )kCells ); 72 std::sort (v .begin (),v .begin ()+ w ); 73 std::copy (v .begin (),v .end (),ids ); 74 }else {// unordered, with repeats 75for (int64_t j = 0 ;j < cap ;++ j )ids [j ]= (int32_t ) (rng () % (uint32_t )kCells ); 76 } 77} 78 79struct Harness { 80 k::QsaShapes s = k::qsa_real_shapes (); 81 k::QsaAttnPools pools ; 82int32_t * table = nullptr ; 83float * q = nullptr ,* scratch_ref = nullptr ,* scratch_new = nullptr ,* attn_ref = nullptr ,* attn_new = nullptr ; 84int32_t * ids = nullptr ,* steps = nullptr ; 85int64_t max_q = 0 ,max_cap = 0 ; 86 std::vector < void *> to_free ; 87 88template < typename T > T * keep (T * p ) {to_free .push_back (p );return p ; } 89 90void init (std::mt19937 & rng ,int64_t max_q_ ,int64_t max_cap_ ) { 91max_q = max_q_ ; 92max_cap = max_cap_ ; 93const int64_t PS = s .page_size ,pages = kCells /PS ,H = s .n_head_kv ,D = s .head_dim ; 94 std::vector < int32_t > tab ((size_t )pages );// logical page -> physical page: a permutation, with holes 95for (int64_t i = 0 ;i < pages ;++ i )tab [(size_t )i ]= (int32_t )i ; 96 std::shuffle (tab .begin (),tab .end (),rng ); 97for (int64_t i = 0 ;i < pages ;++ i ) 98if (rng () %32u == 0 )tab [(size_t )i ]= -1 ; 99for (int64_t i = kHole /PS ;i < (kHole + 256 ) /PS ;++ i )tab [(size_t )i ]= -1 ; 100table = keep (upload (tab )); 101const int64_t rows = pages * H * PS ; 102pools .page_table = table ; 103 std::vector < int8_t > c ((size_t ) (rows * D )); 104for (auto & x :c )x = (int8_t ) ((int ) (rng () %255u )- 127 ); 105pools .k_q = keep (upload (c )); 106for (auto & x :c )x = (int8_t ) ((int ) (rng () %255u )- 127 ); 107pools .v_q = keep (upload (c )); 108 std::vector < uint16_t > sc ((size_t ) (rows * (D /64 ))); 109for (auto & x :sc )x = f16_scale_q8 (rng ()); 110pools .k_scale = keep (upload (sc )); 111for (auto & x :sc )x = f16_scale_q8 (rng ()); 112pools .v_scale = keep (upload (sc )); 113q = keep (dalloc < float > ((size_t ) (max_q * s .n_head * D ))); 114ids = keep (dalloc < int32_t > ((size_t ) (max_q * max_cap ))); 115steps = keep (dalloc < int32_t > ((size_t ) (max_q * k::kStepCount ))); 116const size_t sf = (size_t ) (k::qsa_decode_attn_scratch_floats (max_cap ,s )* (uint64_t )max_q ); 117scratch_ref = keep (dalloc < float > (sf )); 118scratch_new = keep (dalloc < float > (sf )); 119attn_ref = keep (dalloc < float > ((size_t ) (max_q * s .n_head * D ))); 120attn_new = keep (dalloc < float > ((size_t ) (max_q * s .n_head * D ))); 121 } 122 ~Harness () { 123for (void * p :to_free )cudaFree (p ); 124 } 125 126// returns 0 identical, 1 differs, -1 the candidate declined (build without the kernels) 127int run (int64_t cap ,int n_q ,int scr_q ,const std::vector < int32_t >& widths , std::mt19937 & rng ,bool on_pascal , 128long long & nonzero ) { 129const int64_t D = s .head_dim ,NH = s .n_head ; 130 std::normal_distribution < float > nd (0.0f ,1.0f ); 131 std::vector < float > hq ((size_t ) (n_q * NH * D )); 132for (auto & x :hq )x = nd (rng ); 133ck (cudaMemcpy (q ,hq .data (),hq .size ()* 4 ,cudaMemcpyHostToDevice ),"q" ); 134 std::vector < int32_t > hid ((size_t ) (n_q * cap )); 135 std::vector < int32_t > hst ((size_t ) (n_q * k::kStepCount ),0 ); 136for (int i = 0 ;i < n_q ;++ i ) { 137selection (hid .data ()+ (size_t )i * cap ,cap ,widths [(size_t )i ], (int ) (rng () %3u ),rng ); 138hst [(size_t )i * k::kStepCount + k::kStepWidth ]= widths [(size_t )i ]; 139 } 140ck (cudaMemcpy (ids ,hid .data (),hid .size ()* 4 ,cudaMemcpyHostToDevice ),"ids" ); 141ck (cudaMemcpy (steps ,hst .data (),hst .size ()* 4 ,cudaMemcpyHostToDevice ),"steps" ); 142const size_t n = (size_t ) (n_q * NH * D ); 143const size_t sbytes = (size_t ) k::qsa_decode_attn_scratch_floats (cap ,s )* (size_t )n_q * 4 ; 144 145// the reference: the pre-sm_75 chunk kernel and attn_merge_kernel 146ck (cudaMemset (scratch_ref ,0xFF ,sbytes ),"scratch_ref fill" ); 147ck (cudaMemset (attn_ref ,0xFF ,n * 4 ),"attn_ref fill" ); 148 k::qsa_decode_attn_chunks_only (q ,pools ,ids ,steps ,cap ,s ,scratch_ref ,n_q ,1 ,nullptr ); 149 k::qsa_decode_attn_merge_only (steps ,cap ,s ,scratch_ref ,attn_ref ,n_q ,0 ,nullptr ); 150ck (cudaDeviceSynchronize (),"reference" ); 151 std::vector < float > a (n ),b (n ); 152ck (cudaMemcpy (a .data (),attn_ref ,n * 4 ,cudaMemcpyDeviceToHost ),"read reference" ); 153nonzero = 0 ; 154for (size_t i = 0 ;i < n ;++ i ) { 155if (!std::isfinite (a [i ])) { std::printf ("FAIL: the reference has a non-finite value\n" );return 1 ; } 156if (a [i ]!= 0.0f )++ nonzero ; 157 } 158 159// the candidate: the scratch holds scr_q queries of the decode layout (the caller's attn_batch), junk in it 160ck (cudaMemset (scratch_new ,0xFF ,sbytes ),"scratch_new fill" ); 161ck (cudaMemset (attn_new ,0x7F ,n * 4 ),"attn_new fill" ); 162const uint64_t scratch_floats = (uint64_t )scr_q * k::qsa_decode_attn_scratch_floats (cap ,s ); 163const bool took = k::qsa_pattn_batch_any_arch (q ,pools ,ids ,steps ,cap ,s ,scratch_new ,scratch_floats ,attn_new , 164n_q ,nullptr ); 165if (!took )return -1 ; 166ck (cudaDeviceSynchronize (),"candidate" ); 167ck (cudaMemcpy (b .data (),attn_new ,n * 4 ,cudaMemcpyDeviceToHost ),"read candidate" ); 168bool same = std::memcmp (a .data (),b .data (),n * 4 )== 0 ; 169if (!same ) { 170size_t first = 0 ; 171while (first < n && std::memcmp (& a [first ],& b [first ],4 )== 0 )++ first ; 172 std::printf ("FAIL cap %lld n_q %d scratch %d queries, widths" , (long long )cap ,n_q ,scr_q ); 173for (int32_t w :widths ) std::printf (" %d" ,w ); 174 std::printf (": first difference at query %lld head %lld dim %lld: %.9g vs %.9g\n" , 175 (long long ) (first / (NH * D )), (long long ) (first /D %NH ), (long long ) (first %D ),a [first ], 176b [first ]); 177 } 178if (on_pascal ) {// the checked entry takes the shapes, and the production path is the reference 179ck (cudaMemset (scratch_new ,0xFF ,sbytes ),"scratch_new refill" ); 180ck (cudaMemset (attn_new ,0x7F ,n * 4 ),"attn_new refill" ); 181if (!k::qsa_pattn_batch (q ,pools ,ids ,steps ,cap ,s ,scratch_new ,scratch_floats ,attn_new ,n_q ,nullptr )) { 182 std::printf ("FAIL: qsa_pattn_batch declined on a compute capability 6 card (cap %lld n_q %d)\n" , 183 (long long )cap ,n_q ); 184same = false; 185 }else { 186ck (cudaDeviceSynchronize (),"checked entry" ); 187ck (cudaMemcpy (b .data (),attn_new ,n * 4 ,cudaMemcpyDeviceToHost ),"read checked entry" ); 188if (std::memcmp (a .data (),b .data (),n * 4 )!= 0 ) { 189 std::printf ("FAIL: qsa_pattn_batch differs from the reference (cap %lld n_q %d)\n" , (long long )cap ,n_q ); 190same = false; 191 } 192 } 193ck (cudaMemset (scratch_new ,0xFF ,sbytes ),"scratch_new refill" ); 194 k::qsa_decode_attn_batch (q ,pools ,ids ,steps ,cap ,s ,scratch_new ,attn_new ,n_q ,nullptr ); 195ck (cudaDeviceSynchronize (),"production" ); 196ck (cudaMemcpy (b .data (),attn_new ,n * 4 ,cudaMemcpyDeviceToHost ),"read production" ); 197if (std::memcmp (a .data (),b .data (),n * 4 )!= 0 ) { 198 std::printf ("FAIL: qsa_decode_attn_batch differs from the reference (cap %lld n_q %d)\n" , (long long )cap , 199n_q ); 200same = false; 201 } 202 } 203return same ?0 :1 ; 204 } 205}; 206}// namespace 207 208int main (int argc ,char ** argv ) { 209int device = 0 ; 210for (int i = 1 ;i < argc ;++ i ) { 211if (std::strcmp (argv [i ],"--device" )== 0 && i + 1 < argc )device = std::atoi (argv [++ i ]); 212else { 213 std::fprintf (stderr ,"usage: qsa_pattn_parity [--device N]\n" ); 214return 2 ; 215 } 216 } 217int devices = 0 ; 218if (cudaGetDeviceCount (& devices )!= cudaSuccess || devices == 0 ) { 219 std::puts ("SKIP: no GPU" ); 220return 77 ; 221 } 222ck (cudaSetDevice (device ),"cudaSetDevice" ); 223cudaDeviceProp prop {}; 224ck (cudaGetDeviceProperties (& prop ,device ),"cudaGetDeviceProperties" ); 225const bool on_pascal = prop .major == 6 ; 226 std::printf ("qsa_pattn_parity on device %d (%s, %d.%d)%s\n" ,device ,prop .name ,prop .major ,prop .minor , 227on_pascal ?": the checked entry and the production path are compared too" :"" ); 228 229 std::mt19937 rng (20261026u ); 230Harness hx ; 231hx .init (rng ,40 ,2112 ); 232 233const int32_t base_widths []= {0 ,1 ,63 ,64 ,65 ,1023 ,1024 ,1025 ,2051 ,2112 }; 234const int n_base = (int ) (sizeof (base_widths ) /sizeof (base_widths [0 ])); 235long long cases = 0 ,failed = 0 ,nonzero_total = 0 ; 236for (int64_t cap : {(int64_t )64 , (int64_t )130 , (int64_t )2051 , (int64_t )2112 }) { 237for (int n_q : {1 ,2 ,3 ,7 ,40 }) { 238for (int scr_q : {n_q ,2 }) { 239if (scr_q > n_q )continue ; 240for (int wc = 0 ;wc < n_base + 3 ;++ wc ) { 241 std::vector < int32_t > widths ; 242for (int i = 0 ;i < n_q ;++ i ) 243widths .push_back (std::min < int32_t > ((int32_t )cap ,wc < n_base ?base_widths [wc ] :base_widths [rng () % (uint32_t )n_base ])); 244long long nz = 0 ; 245const int r = hx .run (cap ,n_q ,scr_q ,widths ,rng ,on_pascal ,nz ); 246if (r < 0 ) { 247 std::puts ("SKIP: this build has no sm_6x kernels (CUDA with STRATA_EXPERIMENTAL_SM60)" ); 248return 77 ; 249 } 250nonzero_total += nz ; 251++ cases ; 252if (r != 0 )++ failed ; 253 } 254 } 255 } 256 } 257 std::printf ("qsa_pattn_parity: %lld cases, %lld differ, %lld nonzero output values in all -> %s\n" ,cases ,failed , 258nonzero_total ,failed == 0 && nonzero_total > 0 ?"PASS" :"FAIL" ); 259return failed == 0 && nonzero_total > 0 ?0 :1 ; 260}