okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
f32c3c461
main
1// Penalty-history rows of a BATCH window, per slot (strata::kernels::slot_penalty_rows). Header-only, CPU, no model. 2// 3// Row t of a batch window feeds tokens[t] into slot rows[t]; its history is the slot's reply tail followed by that slot's tokens in this window up to 4// and including row t - what penalty_rows gives a solo window of that slot, whatever the other slots do. 5#include "strata/kernels/sampler.hpp" 6 7#include <cstdio> 8#include <algorithm> 9#include <cstdlib> 10#include <random> 11#include <vector> 12 13using strata::kernels::penalty_rows ; 14using strata::kernels::slot_penalty_rows ; 15 16namespace { 17int checks = 0 ; 18void check (bool ok ,const char * label ) { 19++ checks ; 20if (!ok ) { std::fprintf (stderr ,"FAIL: %s\n" ,label ); std::exit (1 ); } 21} 22}// namespace 23 24int main () { 25 std::mt19937 rng (7 ); 26constexpr int kSlots = 6 ,kH = 16 ; 27for (int trial = 0 ;trial < 400 ;++ trial ) { 28// per slot: a reply tail of 0..40 tokens 29 std::vector < std::vector < int32_t >> tail (kSlots ); 30 std::vector < const int32_t *> tails (kSlots ,nullptr ); 31 std::vector < int64_t > n_tails (kSlots ,0 ); 32for (int b = 0 ;b < kSlots ;++ b ) { 33const int n = (int ) (rng () %41 ); 34for (int i = 0 ;i < n ;++ i )tail [b ].push_back ((int32_t ) (rng () %500 )); 35tails [b ]= tail [b ].empty () ?nullptr :tail [b ].data (); 36n_tails [b ]= (int64_t )tail [b ].size (); 37 } 38// a window: 1..8 rows; each slot at most once, or as a contiguous group of 1..3 rows (MTP drafts) 39 std::vector < int > rows ; std::vector < int32_t > toks ; 40 std::vector < int > order = {0 ,1 ,2 ,3 ,4 ,5 }; 41 std::shuffle (order .begin (),order .end (),rng ); 42for (int b :order ) { 43if (rows .size () >=8 )break ; 44if (rng () %3 == 0 )continue ; 45const int g = 1 + (int ) (rng () %3 ); 46for (int j = 0 ;j < g && rows .size ()< 8 ;++ j ) {rows .push_back (b );toks .push_back ((int32_t ) (rng () %500 )); } 47 } 48if (rows .empty ()) {rows .push_back (2 );toks .push_back (9 ); } 49const int S = (int )rows .size (); 50 std::vector < int32_t > out ((size_t )S * kH ,12345 ); 51slot_penalty_rows (rows .data (),toks .data (),S ,tails .data (),n_tails .data (),kH ,out .data ()); 52// the definition: per slot, penalty_rows over (its tail, its rows of this window) 53for (int b = 0 ;b < kSlots ;++ b ) { 54 std::vector < int > idx ; 55 std::vector < int32_t > win ; 56for (int t = 0 ;t < S ;++ t )if (rows [t ]== b ) {idx .push_back (t );win .push_back (toks [t ]); } 57if (idx .empty ())continue ; 58 std::vector < int32_t > ref (idx .size ()* kH ); 59penalty_rows (tail [b ].data (), (int64_t )tail [b ].size (),win .data (), (int )idx .size (),kH ,ref .data ()); 60for (size_t k = 0 ;k < idx .size ();++ k ) 61for (int j = 0 ;j < kH ;++ j ) 62check (out [(size_t )idx [k ]* kH + j ]== ref [k * kH + j ],"slot row equals penalty_rows of the slot alone" ); 63 } 64 } 65// ---- fixed cases: an empty tail, the fed-back head counted, other slots' tokens never in a row 66 { 67const int rows [3 ]= {0 ,1 ,1 }; 68const int32_t toks [3 ]= {10 ,20 ,21 }; 69const int32_t t0 [2 ]= {1 ,2 }; 70const int32_t t1 [3 ]= {7 ,8 ,9 }; 71const int32_t * tails [2 ]= {t0 ,t1 }; 72const int64_t n_tails [2 ]= {2 ,3 }; 73 std::vector < int32_t > out (3 * 4 ,0 ); 74slot_penalty_rows (rows ,toks ,3 ,tails ,n_tails ,4 ,out .data ()); 75const int32_t e [12 ]= {-1 ,1 ,2 ,10 ,7 ,8 ,9 ,20 ,8 ,9 ,20 ,21 }; 76for (int i = 0 ;i < 12 ;++ i )check (out [i ]== e [i ],"fixed case" ); 77 } 78 {// a slot with no tail (no penalty history): its row holds the fed token only 79const int rows [1 ]= {0 }; 80const int32_t toks [1 ]= {42 }; 81const int32_t * tails [1 ]= {nullptr }; 82const int64_t n_tails [1 ]= {0 }; 83int32_t out [4 ]= {0 ,0 ,0 ,0 }; 84slot_penalty_rows (rows ,toks ,1 ,tails ,n_tails ,4 ,out ); 85check (out [0 ]== -1 && out [1 ]== -1 && out [2 ]== -1 && out [3 ]== 42 ,"empty tail" ); 86 } 87 std::printf ("slot_history_test: %d checks passed\n" ,checks ); 88return 0 ; 89}