okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
7a2948f9b
main
1// src/core/coupled_draft_test.cpp - coupled draft sampling's host-side arithmetic (include/strata/core/coupled_draft.hpp). 2// 3// No GPU. A simulated decode - windows of random size, random accepted counts, the serve loop's first window of one 4// token and the CLI's `draft_first` - checks that 5// 1. every draft the MTP chain makes is drawn with the Philox counter of the verify row that checks it: the verify 6// window at pos0 draws row t with pos0 + t (verify.cpp run()), and row t checks the draft in row t + 1; 7// 2. the MTP cell of each draft predicts exactly the position that draft occupies in the next window; 8// 3. the penalty window each draft sees in the device ring (the staged base, then the chain's earlier drafts) is 9// token for token the history row `penalty_rows` gives the verify row that checks it; 10// 4. the penalty window length matches the target's (serve caps the history at 4,096). 11#include "strata/core/coupled_draft.hpp" 12 13#include <cstdio> 14#include <random> 15#include <vector> 16 17using namespace strata::core ; 18 19namespace { 20int g_fail = 0 ; 21int64_t g_checks = 0 ; 22void check (bool ok ,const char * what ,long long a = 0 ,long long b = 0 ) { 23++ g_checks ; 24if (!ok ) { 25if (++ g_fail <=20 ) std::printf (" FAIL %s (%lld vs %lld)\n" ,what ,a ,b ); 26 } 27} 28 29// verify.cpp run(): sp.counter = pos0, and the kernel draws row t with sp.counter + t 30uint64_t verify_row_counter (int64_t pos0 ,int t ) {return (uint64_t )pos0 + (uint64_t )t ; } 31 32// The counters and positions of a chain of `n` drafts made after a window at `p` with `a` accepted (draft()), or for 33// draft_first (cell0 = p - 1, a = 0), checked against the next window at `p_next`. 34void check_chain (int64_t p ,int a ,int n ,int64_t p_next ) { 35for (int j = 0 ;j < n ;++ j ) { 36const int64_t cell = coupled_draft_cell (p ,a ,j ); 37// mtp.hpp: cell c predicts the token at c + 2; the next window's row j + 1 holds position p_next + j + 1 38check (cell + 2 == p_next + j + 1 ,"draft position = its row in the next window" ,cell + 2 ,p_next + j + 1 ); 39// row j + 1 is kept iff it equals row j's pick, drawn at p_next + j 40check (coupled_draft_counter (cell )== verify_row_counter (p_next ,j ),"draft counter = verifying row's counter" , 41 (long long )coupled_draft_counter (cell ), (long long )verify_row_counter (p_next ,j )); 42 } 43} 44 45// The device ring for one chain: base staged at [cap - h, cap) by the host, draft i at cap + i; draft j's window is 46// [coupled_hist_start(cap, j, h), + h). Compared with penalty_rows over (consumed, [next, drafts...]). 47void check_history (std::mt19937 & rng ,int cap ,int h ,int n_consumed ,int n_drafts ) { 48 std::uniform_int_distribution < int > tok (0 ,999 ); 49 std::vector < int32_t > consumed ((size_t )n_consumed ),drafts ((size_t )n_drafts ); 50for (auto & t :consumed )t = tok (rng ); 51for (auto & t :drafts )t = tok (rng ); 52const int32_t next = tok (rng ); 53 std::vector < int32_t > mapped ((size_t )cap ,777777 ),ring ((size_t ) (cap + n_drafts ),888888 ); 54coupled_hist_base (consumed .data (), (int64_t )consumed .size (),next ,h ,mapped .data ()+ (cap - h ));// set_draft_history 55for (int i = cap - h ;i < cap ;++ i )ring [(size_t )i ]= mapped [(size_t )i ];// coupled_stage_kernel 56// the next window: [next, drafts...], T = n_drafts + 1 rows; row j checks draft j (row j + 1) 57 std::vector < int32_t > window ; 58window .push_back (next ); 59window .insert (window .end (),drafts .begin (),drafts .end ()); 60const int T = (int )window .size (); 61 std::vector < int32_t > rows ((size_t )T * (size_t )h ); 62 strata::kernels::penalty_rows (consumed .data (), (int64_t )consumed .size (),window .data (),T ,h ,rows .data ()); 63for (int j = 0 ;j < n_drafts ;++ j ) { 64const int s = coupled_hist_start (cap ,j ,h ); 65check (s >=0 && s + h <=cap + n_drafts ,"the window lies inside the ring" ,s ,cap + n_drafts ); 66for (int i = 0 ;i < h ;++ i ) 67check (ring [(size_t ) (s + i )]== rows [(size_t )j * (size_t )h + (size_t )i ],"draft history = verify row history" , 68ring [(size_t ) (s + i )],rows [(size_t )j * (size_t )h + (size_t )i ]); 69ring [(size_t ) (cap + j )]= drafts [(size_t )j ];// coupled_merge_kernel appends the draft after sampling it 70 } 71} 72}// namespace 73 74int main () { 75 std::printf ("coupled_draft_test\n" ); 76 std::mt19937 rng (20260930 ); 77// ---- 1 + 2: a serve-style decode (the first window is the last prompt token alone, then MTP windows) 78for (int run = 0 ;run < 200 ;++ run ) { 79 std::uniform_int_distribution < int > tdist (1 ,8 ); 80int64_t p = 100 + run * 37 ;// n - 1 81int T = 1 ; 82for (int round = 0 ;round < 300 ;++ round ) { 83 std::uniform_int_distribution < int > adist (0 ,T - 1 ); 84const int a = adist (rng ); 85const int64_t p_next = p + a + 1 ; 86const int n = 7 ;// the chain makes up to max_t - 1 drafts, whatever T the next window takes 87check_chain (p ,a ,n ,p_next ); 88p = p_next ; 89T = tdist (rng );// --spec-min-p / the draft policy choose the next window's size 90 } 91 } 92// the CLI's draft_first: the first window at p, drafts from cell p - 1 with a = 0 93for (int64_t p = 1 ;p < 5000 ;p += 13 )check_chain (p - 1 ,0 ,7 ,p ); 94// ---- 3: the penalty ring against penalty_rows 95for (int cap : {8 ,64 ,kCoupledHistCap }) { 96for (int h : {1 ,3 ,8 }) { 97if (h > cap )continue ; 98for (int n_consumed : {0 ,1 ,2 ,5 ,7 ,20 ,100 }) 99for (int n_drafts : {1 ,3 ,7 })check_history (rng ,cap ,h ,n_consumed ,n_drafts ); 100 } 101check_history (rng ,cap ,cap ,3 ,7 );// the window is the whole ring base 102check_history (rng ,cap ,cap ,5000 ,7 ); 103 } 104// ---- 4: the window length the target uses: min(penalty_last_n, history_len) with history_len = 105// min(penalty_last_n, 4096) in serve (0 = off) 106for (int pln : {-1 ,0 ,1 ,64 ,4095 ,4096 ,4097 ,100000 }) { 107const int hist_n = std::min (std::max (pln ,0 ),4096 ); 108const int target = hist_n > 0 ? std::min (pln ,hist_n ) :0 ; 109check (coupled_hist_len (pln ,kCoupledHistCap )== target ,"penalty window length = the target's" , 110coupled_hist_len (pln ,kCoupledHistCap ),target ); 111 } 112 std::printf (" %lld checks, %d failed\n" , (long long )g_checks ,g_fail ); 113 std::printf ("%s\n" ,g_fail == 0 ?"PASS" :"FAIL" ); 114return g_fail == 0 ?0 :1 ; 115}