okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
fe57a0cb5
main
1// The batched drafts' row plan (src/batch_plan.hpp) on the CPU: it stays within the rows and each lane's offer, it finds 2// the most tokens per ms a brute force finds over every split (random offers of 1..6 lanes), and for one lane it picks 3// the chain length Session::generate's chain_len picks alone. 4// 5// batch_plan_test (make bin/batch_plan_test; no card, no model) 6#include <cmath> 7#include <cstdio> 8#include <functional> 9#include <random> 10#include <stdexcept> 11#include <vector> 12 13#include "../src/batch_plan.hpp" 14 15namespace { 16constexpr int ROWS = 16 ;// MAXT 17 18double rate (const std::vector < fnx::RowOffer >& L ,const std::vector < int >& d ,double base ,const fnx::RowCost & c ) { 19double tok = (double )L .size (),ms = base ; 20int rows = (int )L .size (); 21for (size_t i = 0 ;i < L .size ();++ i ) 22for (int j = 0 ;j < d [i ];++ j ) { 23tok += L [i ].reach [(size_t )j ]; 24ms += c .row (++ rows )+ (L [i ].chain && j >=1 ?c .chain_ms :0.0 ); 25 } 26return tok /ms ; 27} 28 29double best_rate (const std::vector < fnx::RowOffer >& L ,double base ,const fnx::RowCost & c ) { 30 std::vector < int > d (L .size (),0 ); 31double best = 0.0 ; 32 std::function < void (size_t ,int )> go = [& ](size_t i ,int left ) { 33if (i == L .size ()) { 34best = std::max (best ,rate (L ,d ,base ,c )); 35return ; 36 } 37for (int k = 0 ;k <= std::min (L [i ].avail ,left );++ k ) { 38d [i ]= k ; 39go (i + 1 ,left - k ); 40 } 41d [i ]= 0 ; 42 }; 43go (0 ,ROWS - (int )L .size ()); 44return best ; 45} 46}// namespace 47 48int main () { 49 std::mt19937_64 rng (12345 ); 50 std::uniform_real_distribution < double > u (0.0 ,1.0 ); 51const fnx::RowCost c ; 52int fails = 0 ,cases = 0 ; 53for (int it = 0 ;it < 4000 ;++ it ) { 54const int n = 1 + (int ) (rng () %6 ); 55 std::vector < fnx::RowOffer > L ((size_t )n ); 56for (auto & o :L ) { 57o .chain = rng () %2 ; 58o .avail = (int ) (rng () % (o .chain ?7 :16 )); 59const double p = u (rng );// per-position acceptance, non-increasing reach 60double r = 1.0 ; 61for (int j = 0 ;j < o .avail ;++ j )o .reach .push_back (r *=p * (0.8 + 0.2 * u (rng ))); 62 } 63const double base = c .round_ms + c .rows_ms (n )+ c .call_ms * n ; 64const std::vector < int > d = fnx::plan_rows (L ,ROWS ,base ,c ); 65int rows = n ; 66for (int i = 0 ;i < n ;++ i ) { 67rows += d [(size_t )i ]; 68if (d [(size_t )i ]< 0 || d [(size_t )i ]> L [(size_t )i ].avail ) { 69 std::printf ("case %d: lane %d gets %d drafts of %d offered\n" ,it ,i ,d [(size_t )i ],L [(size_t )i ].avail ); 70++ fails ; 71 } 72 } 73if (rows > ROWS ) { 74 std::printf ("case %d: %d rows\n" ,it ,rows ); 75++ fails ; 76 } 77const double got = rate (L ,d ,base ,c ),best = best_rate (L ,base ,c ); 78if (got < best * (1.0 - 1e-9 )) { 79 std::printf ("case %d (%d lanes): %.6f tokens/ms, the best split %.6f\n" ,it ,n ,got ,best ); 80++ fails ; 81 } 82++ cases ; 83 } 84// one lane, a chain: chain_len's choice (k drafts, at least 1) where the first draft pays at all 85for (int it = 0 ;it < 2000 ;++ it ) { 86 fnx::RowOffer o ; 87o .chain = true; 88o .avail = 1 + (int ) (rng () %6 ); 89double r = 1.0 ; 90const double p = 0.2 + 0.8 * u (rng ); 91for (int j = 0 ;j < o .avail ;++ j )o .reach .push_back (r *=p * (0.8 + 0.2 * u (rng ))); 92int best = 1 ; 93double best_rate = -1.0 ,expect = 1.0 ; 94for (int k = 1 ;k <=o .avail ;++ k ) { 95expect += o .reach [(size_t )k - 1 ]; 96const double rt = expect / (c .round_ms + c .rows_ms (k + 1 )+ std::max (0 ,k - 1 )* c .chain_ms ); 97if (rt > best_rate )best_rate = rt ,best = k ; 98 } 99const int d = fnx::plan_rows ({o },ROWS ,c .round_ms ,c )[0 ]; 100if (d != best ) { 101 std::printf ("one lane: plan %d drafts, chain_len %d (avail %d, reach0 %.3f)\n" ,d ,best ,o .avail ,o .reach [0 ]); 102++ fails ; 103 } 104++ cases ; 105 } 106// choose_rows: within the offers and rows; the best of plan_rows' plan, the uniform plans (each charged the capture 107// unless held) and the held layouts that fit; an explorer's draft never adds a capture 108for (int it = 0 ;it < 4000 ;++ it ) { 109const int n = 1 + (int ) (rng () %6 ); 110 std::vector < fnx::RowOffer > L ((size_t )n ); 111for (auto & o :L ) { 112o .chain = rng () %2 ; 113o .avail = (int ) (rng () % (o .chain ?7 :16 )); 114o .explore = rng () %4 == 0 ; 115const double p = u (rng ); 116double r = 1.0 ; 117for (int j = 0 ;j < o .avail ;++ j )o .reach .push_back (r *=p * (0.8 + 0.2 * u (rng ))); 118 } 119const double base = c .round_ms + c .rows_ms (n )+ c .call_ms * n ,cap = 10.0 * u (rng ); 120 std::vector < std::vector < int >> held ; 121for (int h = (int ) (rng () %4 );h > 0 ;-- h ) { 122 std::vector < int > counts ((size_t )n ); 123for (int & k :counts )k = 1 + (int ) (rng () %5 ); 124held .push_back (counts ); 125 } 126auto fits = [& ](const std::vector < int >& d ) { 127int rows = n ; 128for (int i = 0 ;i < n ;++ i ) { 129if (d [(size_t )i ]< 0 || d [(size_t )i ]> L [(size_t )i ].avail )return false; 130rows += d [(size_t )i ]; 131 } 132return rows <=ROWS ; 133 }; 134auto held_d = [& ](const std::vector < int >& d ) { 135for (const auto & h :held ) { 136bool same = true; 137for (int i = 0 ;i < n ;++ i )same = same && h [(size_t )i ]== d [(size_t )i ]+ 1 ; 138if (same )return true; 139 } 140return false; 141 }; 142auto bare = [& ](std::vector < fnx::RowOffer > o ) { 143for (auto & x :o )x .explore = false; 144return o ; 145 }; 146const std::vector < int > d = fnx::choose_rows (L ,ROWS ,base ,c ,held ,cap ); 147const std::vector < int > d0 = fnx::choose_rows (bare (L ),ROWS ,base ,c ,held ,cap ); 148if (!fits (d )|| !fits (d0 )) { 149 std::printf ("choose case %d: a plan past the offers or the rows\n" ,it ); 150++ fails ; 151continue ; 152 } 153// d0's rate (charged as chosen) is the best of every candidate's: plan_rows' plan, the uniform plans, the held 154const auto Lb = bare (L ); 155const std::vector < int > p = fnx::plan_rows (Lb ,ROWS ,base ,c ); 156double best = fnx::plan_rate (Lb ,p ,base ,c ,held_d (p ) ?0.0 :cap ); 157for (int k = 0 ;k <=15 ;++ k ) { 158 std::vector < int > u ; 159for (const auto & o :Lb )u .push_back (std::min (k ,o .avail )); 160if (fits (u ))best = std::max (best , fnx::plan_rate (Lb ,u ,base ,c ,held_d (u ) ?0.0 :cap )); 161 } 162for (const auto & h :held ) { 163 std::vector < int > hd ; 164for (int k :h )hd .push_back (k - 1 ); 165if (fits (hd ))best = std::max (best , fnx::plan_rate (Lb ,hd ,base ,c ,0.0 )); 166 } 167const double got = fnx::plan_rate (Lb ,d0 ,base ,c ,held_d (d0 ) ?0.0 :cap ); 168if (got < best * (1.0 - 1e-9 )) { 169 std::printf ("choose case %d: %.6f tokens/ms, the best candidate %.6f\n" ,it ,got ,best ); 170++ fails ; 171 } 172if (held_d (d0 )&& !held_d (d )) { 173 std::printf ("choose case %d: an explorer's draft made a held layout capture\n" ,it ); 174++ fails ; 175 } 176cases += 1 ; 177 } 178bool threw = false; 179try { 180 fnx::RowOffer o ; 181o .avail = 2 ; 182 fnx::plan_rows ({o },ROWS ,c .round_ms ,c ); 183 }catch (const std::runtime_error & ) { 184threw = true; 185 } 186if (!threw ) { 187 std::printf ("an offer without its reach was taken\n" ); 188++ fails ; 189 } 190 std::printf ("%d cases, %d failures: %s\n" ,cases ,fails ,fails ?"FAIL" :"ok" ); 191return fails ?1 :0 ; 192}