okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
94a4a34b0
main
1// adapt_async_default_test: the asynchronous adaptive tier as a measured choice (include/strata/core/adapt_async_default.hpp). 2#include "strata/core/adapt_async_default.hpp" 3 4#include <cstdio> 5#include <cstring> 6 7static int g_fail = 0 ; 8#define CHECK (c ) \ 9 do { \ 10 if (!(c)) { std::fprintf(stderr, "FAIL %s:%d %s\n", __FILE__, __LINE__, #c); ++g_fail; } \ 11 } while (0) 12 13using strata::core::AdaptAsyncAuto ; 14using strata::core::AdaptAsyncSelf ; 15using Action = AdaptAsyncSelf ::Action ; 16using Phase = AdaptAsyncSelf ::Phase ; 17 18static AdaptAsyncAuto good () { 19AdaptAsyncAuto a ; 20a .cuda = a .serve = a .adaptive = a .arena = true; 21return a ; 22} 23 24static AdaptAsyncSelf ::Config cfg0 () {// no warm-up: the cases below count their windows from the first 25AdaptAsyncSelf ::Config c ; 26c .warmup_windows = 0 ; 27return c ; 28} 29 30// feed `n` windows of `ms` / `wait` / `tok`; returns the last non-None action 31static Action feed (AdaptAsyncSelf & s ,int n ,double ms ,double wait ,int tok ) { 32Action last = Action ::None ; 33for (int i = 0 ;i < n ;++ i ) { 34const Action a = s .window (ms ,wait ,tok ); 35if (a != Action ::None )last = a ; 36 } 37return last ; 38} 39 40int main () { 41const char * why = nullptr ; 42// ---- where the engine may choose by measurement 43CHECK (strata::core::adapt_async_self_decides (good (),& why )); 44 {AdaptAsyncAuto a = good ();a .flag = 0 ;CHECK (!strata::core::adapt_async_self_decides (a ,& why ));CHECK (why && std::strstr (why ,"given" )); } 45 {AdaptAsyncAuto a = good ();a .flag = 1 ;CHECK (!strata::core::adapt_async_self_decides (a )); } 46 {AdaptAsyncAuto a = good ();a .env = 0 ;CHECK (!strata::core::adapt_async_self_decides (a ,& why ));CHECK (why && std::strstr (why ,"STRATA_ADAPT_ASYNC" )); } 47 {AdaptAsyncAuto a = good ();a .env = 1 ;CHECK (strata::core::adapt_async_self_decides (a )); } 48 {AdaptAsyncAuto a = good ();a .env = -1 ;CHECK (strata::core::adapt_async_self_decides (a )); } 49 {AdaptAsyncAuto a = good ();a .cuda = false;CHECK (!strata::core::adapt_async_self_decides (a )); } 50 {AdaptAsyncAuto a = good ();a .serve = false;CHECK (!strata::core::adapt_async_self_decides (a )); } 51 {AdaptAsyncAuto a = good ();a .adaptive = false;CHECK (!strata::core::adapt_async_self_decides (a )); }// --adapt-every 100000 gates 52 {AdaptAsyncAuto a = good ();a .arena = false;CHECK (!strata::core::adapt_async_self_decides (a )); } 53 {AdaptAsyncAuto a = good ();a .multi_gpu = true;CHECK (!strata::core::adapt_async_self_decides (a )); } 54 {AdaptAsyncAuto a = good ();a .pipeline_windows = true;CHECK (!strata::core::adapt_async_self_decides (a )); } 55 {AdaptAsyncAuto a = good ();a .batch = true;CHECK (!strata::core::adapt_async_self_decides (a )); } 56 {AdaptAsyncAuto a = good ();a .peer = true;CHECK (!strata::core::adapt_async_self_decides (a )); } 57 {AdaptAsyncAuto a = good ();a .remote_opt = true;CHECK (!strata::core::adapt_async_self_decides (a )); } 58 {AdaptAsyncAuto a = good ();a .adapt_overlap = true;CHECK (!strata::core::adapt_async_self_decides (a )); } 59CHECK (!strata::core::adapt_async_self_decides (AdaptAsyncAuto {})); 60 61// ---- the state machine 62 {// not armed: nothing happens 63AdaptAsyncSelf s ; 64CHECK (s .phase ()== Phase ::Off ); 65CHECK (feed (s ,500 ,20.0 ,5.0 ,3 )== Action ::None ); 66 } 67 {// the wait is small (3% of a window): measure 64 windows, stay blocking, never switch 68AdaptAsyncSelf s (cfg0 ());s .arm (); 69CHECK (feed (s ,63 ,20.0 ,0.6 ,3 )== Action ::None ); 70CHECK (s .phase ()== Phase ::Measure ); 71CHECK (feed (s ,1 ,20.0 ,0.6 ,3 )== Action ::None ); 72CHECK (s .phase ()== Phase ::Done && !s .kept ()); 73CHECK (feed (s ,500 ,20.0 ,0.6 ,3 )== Action ::None ); 74 } 75 {// the wait is 7%: switch to async after the 64th window; the async windows are 10% faster per token: kept 76AdaptAsyncSelf s (cfg0 ());s .arm (); 77CHECK (feed (s ,63 ,20.0 ,1.4 ,3 )== Action ::None ); 78CHECK (s .window (20.0 ,1.4 ,3 )== Action ::ToAsync ); 79CHECK (s .phase ()== Phase ::Trial && s .trialing ()); 80CHECK (s .wait_fraction ()> 0.069 && s .wait_fraction ()< 0.071 ); 81CHECK (feed (s ,4 + 63 ,18.0 ,0.1 ,3 )== Action ::None );// 4 warm-up windows are not counted 82CHECK (s .window (18.0 ,0.1 ,3 )== Action ::None );// kept: no action 83CHECK (s .phase ()== Phase ::Done && s .kept ()); 84CHECK (s .trial_ms_per_token ()< s .measure_ms_per_token ()); 85 } 86 {// the wait is 7% and async is 3% SLOWER per token (> 2%): back to blocking, and it stays there 87AdaptAsyncSelf s (cfg0 ());s .arm (); 88CHECK (feed (s ,64 ,20.0 ,1.4 ,3 )== Action ::ToAsync ); 89CHECK (feed (s ,4 + 63 ,20.6 ,0.1 ,3 )== Action ::None ); 90CHECK (s .window (20.6 ,0.1 ,3 )== Action ::ToBlocking ); 91CHECK (s .phase ()== Phase ::Done && !s .kept ()); 92CHECK (feed (s ,1000 ,19.6 ,0.1 ,3 )== Action ::None ); 93 } 94 {// async slower (the CPU-bound box): back 95AdaptAsyncSelf s (cfg0 ());s .arm (); 96CHECK (feed (s ,64 ,55.0 ,4.0 ,3 )== Action ::ToAsync ); 97CHECK (feed (s ,4 + 63 ,58.0 ,0.2 ,3 )== Action ::None ); 98CHECK (s .window (58.0 ,0.2 ,3 )== Action ::ToBlocking ); 99 } 100 {// async 1% slower or equal: kept (the trial guards against harm, it does not demand a gain) 101AdaptAsyncSelf s (cfg0 ());s .arm (); 102CHECK (feed (s ,64 ,20.0 ,1.4 ,3 )== Action ::ToAsync ); 103CHECK (feed (s ,4 + 64 ,20.2 ,0.1 ,3 )== Action ::None ); 104CHECK (s .kept ()); 105 } 106 {// 5% wait switches; a 3% gain is kept 107AdaptAsyncSelf s (cfg0 ());s .arm (); 108CHECK (feed (s ,64 ,100.0 ,5.0 ,4 )== Action ::ToAsync ); 109const double t = 100.0 /1.0301 ; 110CHECK (feed (s ,4 + 64 ,t ,0.0 ,4 )== Action ::None ); 111CHECK (s .kept ()); 112 } 113 {// windows with no token or no time are ignored (they neither count nor move the phase) 114AdaptAsyncSelf s (cfg0 ());s .arm (); 115CHECK (feed (s ,200 ,20.0 ,5.0 ,0 )== Action ::None ); 116CHECK (feed (s ,200 ,0.0 ,0.0 ,3 )== Action ::None ); 117CHECK (s .phase ()== Phase ::Measure ); 118 } 119 {// tokens per window matter: more tokens per window at the same time is faster per token 120AdaptAsyncSelf s (cfg0 ());s .arm (); 121CHECK (feed (s ,64 ,20.0 ,2.0 ,2 )== Action ::ToAsync );// 10 ms per token 122CHECK (feed (s ,4 + 64 ,20.0 ,0.1 ,3 )== Action ::None );// 6.7 ms per token: kept 123CHECK (s .kept ()); 124 } 125 {// the default has a warm-up: the first 64 steady windows are not measured (a fresh engine's cache is still settling) 126AdaptAsyncSelf s ;s .arm (); 127CHECK (feed (s ,64 ,20.0 ,5.0 ,3 )== Action ::None );// warm-up 128CHECK (s .phase ()== Phase ::Measure ); 129CHECK (feed (s ,63 ,20.0 ,1.4 ,3 )== Action ::None ); 130CHECK (s .window (20.0 ,1.4 ,3 )== Action ::ToAsync );// 128th window: the measurement is the last 64 only 131CHECK (s .wait_fraction ()> 0.069 && s .wait_fraction ()< 0.071 ); 132 } 133if (g_fail == 0 ) std::printf ("adapt_async_default_test: OK\n" ); 134return g_fail == 0 ?0 :1 ; 135}