okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
4f5e6c841
main
1// src/kernels/qsa_topk_parity.cpp - the QSA top-k the engine dispatches (qsa_block_topk) against the original kernel 2// (qsa_block_topk_ref): the selected cells must be identical (GPU, synthetic, no model). 3// 4// A case is a context under a capacity (the engine's --max-context; max_blocks = capacity / 4 + 2, and without an 5// active-block count the dispatch follows it), with consecutive queries at the context's end so that the tail block 6// holds 0 to 3 cells. Three score sets per case: continuous, 64 levels (many ties at the threshold) and all equal 7// (the rule's ties go to the lowest index). The dispatch is called as a decode window calls it (no block count) and as 8// the prompt path does (with the call's largest block count). The reference and the dispatched kernel are also timed. 9// 10// Usage: qsa_topk_parity --selftest 11// qsa_topk_parity CONTEXT [QUERIES=256] [REPS=10] [CAPACITY=CONTEXT] [COUNT=1] (COUNT=0: no block count) 12#include "strata/kernels/qsa.hpp" 13#include "strata/kernels/qsa_select.hpp" 14 15#include <cuda_runtime.h> 16 17#include <algorithm> 18#include <cmath> 19#include <cstdio> 20#include <cstdlib> 21#include <cstring> 22#include <random> 23#include <vector> 24 25namespace k= strata::kernels; 26 27namespace { 28void ck (cudaError_t e ,const char * w ) { 29if (e != cudaSuccess ) { std::fprintf (stderr ,"%s: %s\n" ,w ,cudaGetErrorString (e )); std::exit (2 ); } 30} 31 32bool run_case (int64_t ctx ,int64_t nq ,int reps ,int64_t capacity ,bool counted ) { 33const k::QsaShapes s = k::qsa_real_shapes (); 34const int64_t max_blocks = capacity /s .idx_block + 2 ,cap = k::qsa_selection_width (k::kTopkMaxCells ,s ); 35 std::vector < int32_t > steps ((size_t ) (nq * k::kStepCount )); 36for (int64_t i = 0 ;i < nq ;++ i ) { 37int32_t * st = steps .data ()+ i * k::kStepCount ; 38const int64_t pos = ctx - nq + i ; 39st [k::kStepPos ]= (int32_t )pos ; 40st [k::kStepNKv ]= (int32_t ) (pos + 1 ); 41st [k::kStepNBid ]= (int32_t ) ((pos + 1 ) /s .idx_block ); 42st [k::kStepWidth ]= (int32_t ) k::qsa_selection_width (pos + 1 ,s ); 43 } 44 std::mt19937 rng ((uint32_t ) (ctx * 31 + capacity )); 45 std::normal_distribution < float > nd (0.f ,1.f ); 46 std::vector < float > scores ((size_t ) (nq * max_blocks )),tied (scores .size ()),equal (scores .size (),0.25f ); 47for (int64_t i = 0 ;i < nq ;++ i ) { 48const int32_t * st = steps .data ()+ i * k::kStepCount ; 49float * row = scores .data ()+ i * max_blocks ; 50for (int64_t b = 0 ;b < max_blocks ;++ b )row [b ]= nd (rng ); 51// the tail block as the scorers leave it: far above the others when it holds cells 52if (st [k::kStepNKv ] %s .idx_block != 0 )row [st [k::kStepNBid ]]+= 1e9f ; 53for (int64_t b = 0 ;b < max_blocks ;++ b ) 54tied [(size_t ) (i * max_blocks + b )]= row [b ]> 1e8f ?row [b ] : std::floor (row [b ]* 8.0f ) /8.0f ;// ~64 levels 55 } 56int32_t * d_steps = nullptr ,* ids_ref = nullptr ,* ids_new = nullptr ; 57float * d_scores = nullptr ; 58ck (cudaMalloc (& d_steps ,steps .size ()* 4 ),"malloc" ); 59ck (cudaMalloc (& d_scores ,scores .size ()* 4 ),"malloc" ); 60ck (cudaMalloc (& ids_ref , (size_t ) (nq * cap )* 4 ),"malloc" ); 61ck (cudaMalloc (& ids_new , (size_t ) (nq * cap )* 4 ),"malloc" ); 62ck (cudaMemcpy (d_steps ,steps .data (),steps .size ()* 4 ,cudaMemcpyHostToDevice ),"upload" ); 63const int64_t active = counted ?steps [(size_t ) ((nq - 1 )* k::kStepCount + k::kStepNBid )]+ 1 :-1 ; 64 std::vector < int32_t > a ((size_t ) (nq * cap )),b (a .size ()); 65auto same = [& ](const std::vector < float >& sc )-> int64_t { 66ck (cudaMemcpy (d_scores ,sc .data (),sc .size ()* 4 ,cudaMemcpyHostToDevice ),"upload" ); 67ck (cudaMemset (ids_ref ,0xff ,a .size ()* 4 ),"memset" ); 68ck (cudaMemset (ids_new ,0xff ,a .size ()* 4 ),"memset" ); 69 k::qsa_block_topk_ref (d_scores ,d_steps ,nq ,max_blocks ,cap ,s ,ids_ref ,nullptr ); 70 k::qsa_block_topk (d_scores ,d_steps ,nq ,max_blocks ,cap ,s ,ids_new ,nullptr ,active ); 71ck (cudaDeviceSynchronize (),"sync" ); 72ck (cudaMemcpy (a .data (),ids_ref ,a .size ()* 4 ,cudaMemcpyDeviceToHost ),"down" ); 73ck (cudaMemcpy (b .data (),ids_new ,b .size ()* 4 ,cudaMemcpyDeviceToHost ),"down" ); 74int64_t n = 0 ; 75for (int64_t i = 0 ;i < nq ;++ i ) { 76const int64_t w = steps [(size_t ) (i * k::kStepCount + k::kStepWidth )]; 77n += std::equal (a .begin ()+ i * cap ,a .begin ()+ i * cap + w ,b .begin ()+ i * cap ); 78 } 79return n ; 80 }; 81const int64_t same_tied = same (tied ),same_equal = same (equal ),same_cont = same (scores );// d_scores: continuous 82cudaEvent_t e0 ,e1 ; 83ck (cudaEventCreate (& e0 ),"event" ); 84ck (cudaEventCreate (& e1 ),"event" ); 85auto timed = [& ](auto f ) { 86ck (cudaEventRecord (e0 ),"record" ); 87for (int r = 0 ;r < reps ;++ r )f (); 88ck (cudaEventRecord (e1 ),"record" ); 89ck (cudaEventSynchronize (e1 ),"time" ); 90float ms = 0 ; 91ck (cudaEventElapsedTime (& ms ,e0 ,e1 ),"elapsed" ); 92return ms / (float )reps ; 93 }; 94const float t_ref = timed ([& ] { k::qsa_block_topk_ref (d_scores ,d_steps ,nq ,max_blocks ,cap ,s ,ids_ref ,nullptr ); }); 95const float t_new = timed ([& ] { k::qsa_block_topk (d_scores ,d_steps ,nq ,max_blocks ,cap ,s ,ids_new ,nullptr ,active ); }); 96cudaEventDestroy (e0 );cudaEventDestroy (e1 ); 97cudaFree (d_steps );cudaFree (d_scores );cudaFree (ids_ref );cudaFree (ids_new ); 98const bool ok = same_cont == nq && same_tied == nq && same_equal == nq ; 99 std::printf ("%s context %lld under a capacity of %lld (%lld blocks), %lld queries %s: reference %.3f ms, " 100"dispatched %.3f ms (%.1fx); identical ids: continuous %lld/%lld, 64 levels %lld/%lld, equal scores " 101"%lld/%lld\n" ,ok ?"PASS" :"FAIL" , (long long )ctx , (long long )capacity , (long long )max_blocks , 102 (long long )nq ,counted ?"with their block count" :"without a block count" ,t_ref ,t_new ,t_ref /t_new , 103 (long long )same_cont , (long long )nq , (long long )same_tied , 104 (long long )nq , (long long )same_equal , (long long )nq ); 105return ok ; 106} 107}// namespace 108 109int main (int argc ,char ** argv ) { 110if (argc > 1 && std::strcmp (argv [1 ],"--selftest" )== 0 ) { 111// {context, capacity}: both sides of the register kernel's reach (33,792 blocks = 135,168 cells), short 112// contexts under a long capacity (fewer blocks than the kernel's threads; a prompt batch below the dispatch's 113// 4,608 blocks), and the long contexts themselves 114const int64_t cases [][2 ]= {{9000 ,9000 }, {135160 ,135160 }, {135176 ,135176 }, {2100 ,262144 }, 115 {9000 ,262144 }, {32768 ,262144 }, {262144 ,262144 }, {300001 ,524288 }, 116 {524288 ,524288 }}; 117bool ok = true; 118for (const auto & c :cases ) { 119ok = run_case (c [0 ],8 ,3 ,c [1 ], false)&& ok ;// a decode window's call 120ok = run_case (c [0 ],64 ,3 ,c [1 ], true)&& ok ;// the prompt path's 121 } 122 std::printf ("%s\n" ,ok ?"qsa_topk_parity: PASS" :"qsa_topk_parity: FAIL" ); 123return ok ?0 :1 ; 124 } 125if (argc < 2 ) { 126 std::fprintf (stderr ,"usage: qsa_topk_parity --selftest | CONTEXT [QUERIES=256] [REPS=10] [CAPACITY=CONTEXT] " 127"[COUNT=1]\n" ); 128return 2 ; 129 } 130const int64_t ctx = std::atoll (argv [1 ]); 131const int64_t nq = argc > 2 ? std::atoll (argv [2 ]) :256 ; 132const int reps = argc > 3 ? std::atoi (argv [3 ]) :10 ; 133const int64_t capacity = argc > 4 ? std::atoll (argv [4 ]) :ctx ; 134if (ctx < nq || nq <=0 || reps <=0 || capacity < ctx ) { 135 std::fprintf (stderr ,"qsa_topk_parity: CONTEXT >= QUERIES > 0, REPS > 0, CAPACITY >= CONTEXT\n" ); 136return 2 ; 137 } 138return run_case (ctx ,nq ,reps ,capacity ,argc <=5 || std::atoi (argv [5 ])!= 0 ) ?0 :1 ; 139}