okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
6ca60e9a0
main
1// src/kernels/qsa_select_long_parity.cpp - STRATA_SELECT_LONG's decode select (PR #3820): qsa_block_scores_long 2// (block_scores_dx_kernel) and qsa_block_topk_long (the multi-CTA top-k) against qsa_block_scores and qsa_block_topk, 3// bit for bit (GPU, synthetic data, no model). 4// 5// Needs the CUDA build with STRATA_EXPERIMENTAL_SM60 (elsewhere the _long calls forward to the plain ones and there is 6// nothing to compare; the test skips with 77) and STRATA_SCORES_DX=1 STRATA_TOPK_MULTI=1 in the environment, which make 7// the new kernels run on any card (on compute capability 6.x they are the default). STRATA_TOPK_MULTI_N=<n> fixes the 8// CTAs per query (the ctest "n3" case), otherwise one per 2,048 blocks of capacity. 9// 10// scores: random pooled keys, queries and a dead key; nq 1..8 queries with their own n_bid (a decode window's 11// consecutive positions, so tail blocks of 0..3 cells), n_bid 0 / 1 / 31 / 32 / 33 / 1000 / 70001 ...; the 12// scores of every block 0..n_bid of every query are compared by memcmp (the tail block n_bid included). 13// top-k: scores that are random, all equal, 7 distinct values (many ties across CTAs, so the "first eq_budget cells 14// at the threshold" rule is exercised) and random with a boosted tail block; contexts 1,000 (width equals 15// n_kv: the identity) up to 524,288 cells; nq 1, 2, 5, 8, 16; capacity max_blocks = the largest context / 4 + 2. 16// The selected ids of every query are compared on the first `width` entries. The multi-CTA scratch is filled 17// with junk before every call (the contract says no initial value) and must have been written (the new 18// kernels ran; a forwarded call would leave it untouched). 19// 20// qsa_select_long_parity [--device N] exit 0: all cases identical; 77: no GPU or a build without the kernels 21#include "strata/kernels/qsa.hpp" 22#include "strata/kernels/qsa_select.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_select_long_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 )+ 64 ),"cudaMalloc" ); 47ck (cudaMemset (p ,0 ,n * sizeof (T )+ 64 ),"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 56// steps of a decode window: query i at position pos0 + i 57std::vector < int32_t > make_steps (int nq ,int64_t pos0 ,const k::QsaShapes & s ) { 58 std::vector < int32_t > st ((size_t )nq * k::kStepCount ,0 ); 59for (int i = 0 ;i < nq ;++ i ) { 60int32_t * p = st .data ()+ (size_t )i * k::kStepCount ; 61const int64_t pos = pos0 + i ; 62p [k::kStepPos ]= (int32_t )pos ; 63p [k::kStepNKv ]= (int32_t ) (pos + 1 ); 64p [k::kStepNBid ]= (int32_t ) ((pos + 1 ) /s .idx_block ); 65p [k::kStepWidth ]= (int32_t ) k::qsa_selection_width (pos + 1 ,s ); 66 } 67return st ; 68} 69 70long long g_cases = 0 ,g_failed = 0 ; 71 72void check_scores (const k::QsaShapes & s ,int nq ,int64_t n_bid0 , std::mt19937 & rng ) { 73const int64_t D = s .idx_dim ,H = s .idx_n_head ; 74const int64_t max_blocks = n_bid0 + nq + 8 ; 75 std::normal_distribution < float > nd (0.0f ,1.0f ); 76 std::vector < float > pooled ((size_t ) (max_blocks * D )),dead ((size_t )D ),q ((size_t ) (nq * H * D )); 77for (auto & x :pooled )x = nd (rng ); 78for (auto & x :dead )x = nd (rng ); 79for (auto & x :q )x = nd (rng ); 80// the window's last query has n_bid0 blocks; the others one position earlier each (their tails differ) 81const std::vector < int32_t > steps = make_steps (nq ,n_bid0 * s .idx_block - 1 + (int64_t ) (rng () %4u )- (nq - 1 ),s ); 82float * dp = upload (pooled ),* dd = upload (dead ),* dq = upload (q ); 83int32_t * dst = upload (steps ); 84float * a = dalloc < float > ((size_t ) (nq * max_blocks )); 85float * b = dalloc < float > ((size_t ) (nq * max_blocks )); 86ck (cudaMemset (a ,0xFF , (size_t ) (nq * max_blocks )* 4 ),"fill a" ); 87ck (cudaMemset (b ,0xFF , (size_t ) (nq * max_blocks )* 4 ),"fill b" ); 88 k::qsa_block_scores (dp ,dd ,dq ,dst ,nq ,max_blocks ,s ,a ,nullptr ); 89 k::qsa_block_scores_long (dp ,dd ,dq ,dst ,nq ,max_blocks ,s ,b ,nullptr ,-1 ); 90ck (cudaDeviceSynchronize (),"scores" ); 91 std::vector < float > ha ((size_t ) (nq * max_blocks )),hb (ha .size ()); 92ck (cudaMemcpy (ha .data (),a ,ha .size ()* 4 ,cudaMemcpyDeviceToHost ),"read a" ); 93ck (cudaMemcpy (hb .data (),b ,hb .size ()* 4 ,cudaMemcpyDeviceToHost ),"read b" ); 94bool ok = true; 95for (int i = 0 ;i < nq && ok ;++ i ) { 96const int64_t nb = steps [(size_t )i * k::kStepCount + k::kStepNBid ]; 97for (int64_t j = 0 ;j <=nb ;++ j ) {// block n_bid is the tail 98if (std::memcmp (& ha [(size_t ) (i * max_blocks + j )],& hb [(size_t ) (i * max_blocks + j )],4 )!= 0 ) { 99 std::printf ("FAIL scores nq %d n_bid0 %lld: query %d block %lld: %.9g vs %.9g\n" ,nq , (long long )n_bid0 ,i , 100 (long long )j ,ha [(size_t ) (i * max_blocks + j )],hb [(size_t ) (i * max_blocks + j )]); 101ok = false; 102break ; 103 } 104 } 105 } 106++ g_cases ; 107if (!ok )++ g_failed ; 108for (void * p : {(void * )dp , (void * )dd , (void * )dq , (void * )dst , (void * )a , (void * )b })cudaFree (p ); 109} 110 111void check_topk (const k::QsaShapes & s ,int nq ,int64_t ctx ,int mode ,int64_t max_blocks , std::mt19937 & rng ,bool & multi_ran ) { 112const int64_t cap = k::qsa_selection_width (k::kTopkMaxCells ,s ); 113const std::vector < int32_t > steps = make_steps (nq ,ctx - nq ,s ); 114 std::vector < float > sc ((size_t ) (nq * max_blocks ),-999.0f ); 115 std::uniform_real_distribution < float > ur (0.0f ,200.0f ); 116for (int i = 0 ;i < nq ;++ i ) { 117const int32_t nb = steps [(size_t )i * k::kStepCount + k::kStepNBid ]; 118const bool tail = steps [(size_t )i * k::kStepCount + k::kStepNKv ] %s .idx_block != 0 ; 119for (int64_t j = 0 ;j <=nb ;++ j ) { 120float v = mode == 1 ?3.0f :mode == 2 ? (float ) (rng () %7u ) :ur (rng ); 121sc [(size_t ) (i * max_blocks + j )]= v ; 122 } 123if (tail && mode != 1 )sc [(size_t ) (i * max_blocks + nb )]+= 1e9f ; 124 } 125float * ds = upload (sc ); 126int32_t * dst = upload (steps ); 127int32_t * ia = dalloc < int32_t > ((size_t ) (nq * cap )); 128int32_t * ib = dalloc < int32_t > ((size_t ) (nq * cap )); 129const int64_t scratch_ints = k::qsa_topk_multi_scratch_ints (nq ); 130int32_t * scratch = dalloc < int32_t > ((size_t )scratch_ints ); 131ck (cudaMemset (scratch ,0xFF , (size_t )scratch_ints * 4 ),"scratch junk" ); 132ck (cudaMemset (ia ,0xFF , (size_t ) (nq * cap )* 4 ),"fill ia" ); 133ck (cudaMemset (ib ,0xFF , (size_t ) (nq * cap )* 4 ),"fill ib" ); 134 k::qsa_block_topk (ds ,dst ,nq ,max_blocks ,cap ,s ,ia ,nullptr ); 135 k::qsa_block_topk_long (ds ,dst ,nq ,max_blocks ,cap ,s ,ib ,nullptr ,-1 ,scratch ); 136ck (cudaDeviceSynchronize (),"topk" ); 137 std::vector < int32_t > ha ((size_t ) (nq * cap )),hb (ha .size ()),hs ((size_t )scratch_ints ); 138ck (cudaMemcpy (ha .data (),ia ,ha .size ()* 4 ,cudaMemcpyDeviceToHost ),"read ia" ); 139ck (cudaMemcpy (hb .data (),ib ,hb .size ()* 4 ,cudaMemcpyDeviceToHost ),"read ib" ); 140ck (cudaMemcpy (hs .data (),scratch ,hs .size ()* 4 ,cudaMemcpyDeviceToHost ),"read scratch" ); 141// the multi kernels write the scratch (pass 0's histogram rows) when the contexts are above the identity width 142const bool touched = std::any_of (hs .begin (),hs .end (), [](int32_t v ) {return v != -1 ; }); 143if (touched )multi_ran = true; 144bool ok = true; 145for (int i = 0 ;i < nq && ok ;++ i ) { 146const int64_t w = steps [(size_t )i * k::kStepCount + k::kStepWidth ]; 147if (!std::equal (ha .begin ()+ i * cap ,ha .begin ()+ i * cap + w ,hb .begin ()+ i * cap )) { 148size_t first = 0 ; 149while (ha [(size_t ) (i * cap )+ first ]== hb [(size_t ) (i * cap )+ first ])++ first ; 150 std::printf ("FAIL topk nq %d ctx %lld mode %d max_blocks %lld: query %d entry %zu: %d vs %d\n" ,nq , 151 (long long )ctx ,mode , (long long )max_blocks ,i ,first ,ha [(size_t ) (i * cap )+ first ], 152hb [(size_t ) (i * cap )+ first ]); 153ok = false; 154 } 155 } 156++ g_cases ; 157if (!ok )++ g_failed ; 158for (void * p : {(void * )ds , (void * )dst , (void * )ia , (void * )ib , (void * )scratch })cudaFree (p ); 159} 160}// namespace 161 162int main (int argc ,char ** argv ) { 163int device = 0 ; 164for (int i = 1 ;i < argc ;++ i ) { 165if (std::strcmp (argv [i ],"--device" )== 0 && i + 1 < argc )device = std::atoi (argv [++ i ]); 166else { 167 std::fprintf (stderr ,"usage: qsa_select_long_parity [--device N]\n" ); 168return 2 ; 169 } 170 } 171int devices = 0 ; 172if (cudaGetDeviceCount (& devices )!= cudaSuccess || devices == 0 ) { 173 std::puts ("SKIP: no GPU" ); 174return 77 ; 175 } 176ck (cudaSetDevice (device ),"cudaSetDevice" ); 177if (k::qsa_topk_multi_scratch_ints (1 ) <=0 ) { 178 std::puts ("SKIP: this build has no multi-CTA select (CUDA with STRATA_EXPERIMENTAL_SM60)" ); 179return 77 ; 180 } 181const char * dx = std::getenv ("STRATA_SCORES_DX" ); 182const char * mt = std::getenv ("STRATA_TOPK_MULTI" ); 183if (dx == nullptr || dx [0 ]!= '1' || mt == nullptr || mt [0 ]!= '1' ) { 184 std::fprintf (stderr ,"qsa_select_long_parity: set STRATA_SCORES_DX=1 and STRATA_TOPK_MULTI=1 (the kernels are " 185"otherwise default only on compute capability 6.x)\n" ); 186return 2 ; 187 } 188cudaDeviceProp prop {}; 189ck (cudaGetDeviceProperties (& prop ,device ),"cudaGetDeviceProperties" ); 190 std::printf ("qsa_select_long_parity on device %d (%s, %d.%d), STRATA_TOPK_MULTI_N=%s\n" ,device ,prop .name ,prop .major , 191prop .minor , std::getenv ("STRATA_TOPK_MULTI_N" ) ? std::getenv ("STRATA_TOPK_MULTI_N" ) :"(per capacity)" ); 192 193const k::QsaShapes s = k::qsa_real_shapes (); 194 std::mt19937 rng (20261026u ); 195for (int nq : {1 ,2 ,3 ,5 ,8 }) 196for (int64_t nb : {(int64_t )8 , (int64_t )31 , (int64_t )32 , (int64_t )33 , (int64_t )1000 , (int64_t )70001 , (int64_t )131072 }) 197check_scores (s ,nq ,nb ,rng ); 198 199bool multi_ran = false; 200const int64_t max_ctx = 524288 ,max_blocks = max_ctx /s .idx_block + 2 ; 201for (int nq : {1 ,2 ,5 ,8 ,16 }) 202for (int64_t ctx : {(int64_t )1000 , (int64_t )2051 , (int64_t )2055 , (int64_t )8192 , (int64_t )70000 , (int64_t )135164 , 203 (int64_t )135168 , (int64_t )300003 ,max_ctx }) 204for (int mode = 0 ;mode < 3 ;++ mode ) 205check_topk (s ,nq ,ctx ,mode ,max_blocks ,rng ,multi_ran ); 206// a smaller capacity than the context asks for (max_blocks only bounds the row stride) 207for (int nq : {1 ,4 })check_topk (s ,nq ,40000 ,0 ,40000 /s .idx_block + 2 ,rng ,multi_ran ); 208if (!multi_ran ) { 209 std::printf ("FAIL: the multi-CTA top-k never wrote its scratch: the new kernels did not run\n" ); 210++ g_failed ; 211 } 212 std::printf ("qsa_select_long_parity: %lld cases, %lld differ -> %s\n" ,g_cases ,g_failed ,g_failed == 0 ?"PASS" :"FAIL" ); 213return g_failed == 0 ?0 :1 ; 214}