okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
07f611c75
main
1// (asserts stay on in a Release build: the test is its own check) 2#undef NDEBUG 3#include "strata/core/serve_stop_bound.hpp" 4#include <algorithm> 5#include <cassert> 6#include <iostream> 7#include <string> 8#include <vector> 9 10int main () { 11int cases = 0 ; 12// Independent reference: the existing emission loop, including its budget and EOS rules. 13for (int accepted = 1 ;accepted <=16 ;++ accepted ) 14for (int remaining = 1 ;remaining <=18 ;++ remaining ) 15for (int eos = -1 ;eos < accepted ;++ eos ) { 16 std::vector < int > outputs (accepted ); 17for (int i = 0 ;i < accepted ;++ i )outputs [i ]= i == eos ?-1 :100 + i ; 18 std::vector < int > reference ; 19for (int i = 0 ;i < accepted && static_cast < int > (reference .size ())< remaining ;++ i ) { 20reference .push_back (outputs [i ]); 21if (outputs [i ]== -1 )break ; 22 } 23const int kept = strata::core::serve_stop_bound (accepted ,remaining ,outputs .data (), 24 [](int t ) {return t == -1 ; }); 25assert (kept > 0 && kept <=accepted && kept <=remaining ); 26assert (std::vector < int > (outputs .begin (),outputs .begin ()+ kept )== reference ); 27// Unlike the old full-window commit, no unexposed input suffix enters the cached history. 28assert (kept == static_cast < int > (reference .size ())); 29++ cases ; 30 } 31assert (strata::core::serve_stop_bound (4 ,0 ,static_cast < const int *> (nullptr ), [](int ) {return false; })== 0 ); 32// GEN hold=N:ID: against the emission loop with the hold (stop after N emitted tokens unless ID came first; EOS and 33// max_new as before), for every window shape, start, hold, max_new and place of ID / EOS in the window 34int hold_cases = 0 ; 35const int ID = 7777 ;// a real token id (a negative one means "no hold") 36for (int accepted = 1 ;accepted <=8 ;++ accepted ) 37for (int produced = 0 ;produced <=6 ;++ produced ) 38for (int hold = 1 ;hold <=10 ;++ hold ) 39for (int max_new = produced + 1 ;max_new <=12 ;++ max_new ) 40for (int id_at = -1 ;id_at < accepted ;++ id_at ) 41for (int eos = -1 ;eos < accepted ;++ eos ) { 42if (eos >=0 && eos == id_at )continue ; 43if (produced >=hold )continue ;// a hold the request already reached has ended it 44 std::vector < int > outputs (accepted ); 45for (int i = 0 ;i < accepted ;++ i )outputs [i ]= i == eos ?-1 :i == id_at ?ID :100 + i ; 46 std::vector < int > reference ; 47bool on = true; 48int64_t done = produced ; 49for (int i = 0 ;i < accepted ;++ i ) { 50const int64_t cap = on ? std::min (max_new ,hold ) :max_new ; 51if (done >=cap )break ; 52reference .push_back (outputs [i ]); 53++ done ; 54if (outputs [i ]== ID )on = false; 55if (outputs [i ]== -1 )break ; 56 } 57const int64_t rem = strata::core::serve_hold_remaining (accepted ,produced ,max_new ,hold ,ID , 58outputs .data ()); 59const int kept = strata::core::serve_stop_bound (accepted ,rem ,outputs .data (), 60 [](int t ) {return t == -1 ; }); 61assert (kept == static_cast < int > (reference .size ())); 62assert (std::vector < int > (outputs .begin (),outputs .begin ()+ kept )== reference ); 63// no hold: exactly the max_new bound 64assert (strata::core::serve_hold_remaining (accepted ,produced ,max_new ,-1 ,ID ,outputs .data ())== 65max_new - produced ); 66++ hold_cases ; 67 } 68 std::cout <<"SUMMARY hold cases=" <<hold_cases <<" passed\n" ; 69// --batch slots (P2b): the slot's windows (SlotHold helpers) against an independent per-token reference, over a whole reply 70// of several windows of changing width: same tokens, same finish, the commit never past what was emitted, the hold ending at 71// exactly N unless ID came first. (A window may stop at the hold while ID is still in its rows: the next window goes on.) 72int slot_cases = 0 ; 73const int widths [4 ]= {1 ,3 ,2 ,4 }; 74for (int first = 0 ;first < 4 ;++ first ) 75for (int hold = 1 ;hold <=10 ;++ hold ) 76for (int max_new = 1 ;max_new <=12 ;++ max_new ) 77for (int id_at = -1 ;id_at < 12 ;++ id_at ) 78for (int eos = -1 ;eos < 12 ;++ eos ) { 79if (eos >=0 && eos == id_at )continue ; 80if (hold > 12 )continue ; 81 std::vector < int > stream (14 ); 82for (int i = 0 ;i < 14 ;++ i )stream [i ]= i == eos ?-1 :i == id_at ?ID :100 + i ; 83// reference: one token at a time 84 std::vector < int > ref ; 85 std::string ref_fin ; 86 { 87bool on = true; 88for (int i = 0 ;i < 14 ;++ i ) { 89ref .push_back (stream [i ]); 90if (stream [i ]== ID )on = false; 91const int produced = static_cast < int > (ref .size ()); 92if (stream [i ]== -1 ) {ref_fin = "stop" ;break ; } 93if (produced >=max_new ) {ref_fin = "length" ;break ; } 94if (on && produced >=hold ) {ref_fin = "hold" ;break ; } 95 } 96 } 97// the slot: windows of widths[...] rows, the cap per window as generate.cpp has it 98 strata::core::SlotHold h = strata::core::slot_hold_from (hold ,ID , true); 99 std::vector < int > got ; 100 std::string fin ; 101int64_t produced = 0 ,committed_rows = 0 ; 102for (int w = first ;fin .empty ()&& produced < 14 ;++ w ) { 103const int nrow = widths [w %4 ]; 104int kp = 1 ;// the proposals the target picked, in order (here: all of them), and nothing ended 105while (kp < nrow && produced + kp < 14 && stream [produced + kp - 1 ]!= -1 && 106produced + kp + 1 <= strata::core::slot_cap (max_new ,h )) 107++ kp ; 108committed_rows += kp ; 109const int64_t base = produced ;// (the window's rows start here; `produced` counts on while they are emitted) 110for (int j = 0 ;j < kp ;++ j ) { 111const int y = stream [base + j ]; 112got .push_back (y ); 113++ produced ; 114 strata::core::slot_hold_emitted (h ,y ); 115if (y == -1 )fin = "stop" ; 116else if (produced >=max_new )fin = "length" ; 117else if (strata::core::slot_hold_reached (h ,produced ,max_new ))fin = "hold" ; 118if (!fin .empty ())break ; 119 } 120 } 121if (got != ref || fin != ref_fin ) { 122 std::cerr <<"MISMATCH first=" <<first <<" hold=" <<hold <<" max_new=" <<max_new <<" id_at=" <<id_at <<" eos=" <<eos 123 <<" got:" ; 124for (int y :got ) std::cerr <<' ' <<y ; 125 std::cerr <<" (" <<fin <<") ref:" ; 126for (int y :ref ) std::cerr <<' ' <<y ; 127 std::cerr <<" (" <<ref_fin <<")\n" ; 128 } 129assert (got == ref ); 130assert (fin == ref_fin ); 131assert (committed_rows == static_cast < int64_t > (got .size ()));// not one row committed past what was emitted 132if (fin == "hold" ) { 133assert (static_cast < int > (got .size ())== hold ); 134for (int y :got )assert (y != ID ); 135 } 136// no hold: the hold helpers change nothing 137assert (strata::core::slot_cap (max_new , strata::core::SlotHold {})== max_new ); 138++ slot_cases ; 139 } 140assert (!strata::core::slot_hold_from (5 ,-1 , true).on && !strata::core::slot_hold_from (0 ,ID , true).on && 141 !strata::core::slot_hold_from (5 ,ID , false).on && strata::core::slot_hold_from (5 ,ID , true).on ); 142 std::cout <<"SUMMARY slot hold cases=" <<slot_cases <<" passed\n" ; 143// on unless the variable is exactly "0" (every backend, since the bound left HIP-only) 144assert (strata::core::serve_stop_bound_on (nullptr )); 145assert (strata::core::serve_stop_bound_on ("1" )); 146assert (strata::core::serve_stop_bound_on ("" )); 147assert (!strata::core::serve_stop_bound_on ("0" )); 148 std::cout <<"SUMMARY stop_bound cases=" <<cases <<" passed\n" ; 149 150// ---- STRATA_SERVE_STOP_SKIP: a window that finishes after the server's STOP is dropped whole ----------------------- 151// the decision: only when enabled, STOP seen, not merged, not the request's first window, and the commit still undone 152int skip_cases = 0 ; 153for (int m = 0 ;m < 32 ;++ m ) { 154const bool enabled = m & 1 ,stop = m & 2 ,merged = m & 4 ,first = m & 8 ,deferred = m & 16 ; 155assert (strata::core::serve_skip_stopped_window (enabled ,stop ,merged ,first ,deferred )== 156 (enabled && stop && !merged && !first && deferred )); 157++ skip_cases ; 158 } 159assert (strata::core::serve_stop_skip_on (nullptr )); 160assert (strata::core::serve_stop_skip_on ("1" )); 161assert (strata::core::serve_stop_skip_on ("" )); 162assert (!strata::core::serve_stop_skip_on ("0" )); 163// A ledger of the serial loop: windows of 1..6 rows; each window commits and prints its rows (bounded by the cap, as above) unless it is dropped; 164// STOP is seen from the end of window `stop_at` on (-1: never) and the loop breaks after the first window that saw it. 165// Properties: (1) no STOP, or the switch off: the ledger is the plain loop's; (2) the switch on and the stopped window eligible (not the 166// first, commit undone): that window adds nothing - committed rows = the rows of the windows before it; (3) not eligible (the first window, 167// or a one-row window the state advanced itself): as without the switch; (4) the cap ends a request before STOP is looked at: same rows. 168int ledger_cases = 0 ; 169for (int code = 0 ;code < 6 * 6 * 6 * 6 ;++ code ) { 170int rows [4 ]; 171for (int w = 0 ;w < 4 ;++ w )rows [w ]= 1 + (code / (w == 0 ?1 :w == 1 ?6 :w == 2 ?36 :216 )) %6 ; 172for (int stop_at = -1 ;stop_at < 4 ;++ stop_at ) 173for (int self_mask = 0 ;self_mask < 16 ;++ self_mask )// window w is a self-committed one-row window (only when rows[w] == 1) 174for (int cap = 1 ;cap <=12 ;cap += 3 ) 175for (int on = 0 ;on < 2 ;++ on ) { 176auto run = [& ](bool enabled ,int & committed ,int & emitted ) { 177committed = emitted = 0 ; 178for (int w = 0 ;w < 4 && emitted < cap ;++ w ) { 179const bool deferred = !(rows [w ]== 1 && ((self_mask >>w )& 1 )); 180const bool stop_seen = stop_at >=0 && w >=stop_at ; 181if (strata::core::serve_skip_stopped_window (enabled ,stop_seen , false,w == 0 ,deferred ))break ; 182const int b = std::min (rows [w ],cap - emitted ); 183committed += b ; 184emitted += b ; 185if (stop_seen )break ; 186 } 187 }; 188int c_on = 0 ,e_on = 0 ,c_off = 0 ,e_off = 0 ; 189run (on != 0 ,c_on ,e_on ); 190run (false,c_off ,e_off ); 191assert (c_on == e_on && c_off == e_off );// rows committed = tokens printed in this model: never a row the client was not sent 192if (!on || stop_at < 0 ) { 193assert (c_on == c_off );// (1) 194 }else { 195// the rows of the windows before `stop_at`, up to the cap 196int before = 0 ; 197for (int w = 0 ;w < stop_at && before < cap ;++ w )before += std::min (rows [w ],cap - before ); 198const bool deferred = !(rows [stop_at ]== 1 && ((self_mask >>stop_at )& 1 )); 199if (before >=cap )assert (c_on == before && c_off == before );// (4) 200else if (stop_at > 0 && deferred ) { 201assert (c_on == before );// (2) 202assert (c_off == before + std::min (rows [stop_at ],cap - before )); 203 }else { 204assert (c_on == c_off );// (3) 205 } 206 } 207++ ledger_cases ; 208 } 209 } 210 std::cout <<"SUMMARY stop_skip decision cases=" <<skip_cases <<" ledger cases=" <<ledger_cases <<" passed\n" ; 211}