okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
bd0e878f4
main
1"""Real serving flow over a fake native engine with accurate partial-read DONE fields.""" 2 3import threading ,time ,unittest 4from unittest import mock 5from serve import test_parallel as tp 6 7 8def accurate_fake (): 9source = tp .FAKE_BATCH 10source = source .replace ("def reused(ids):" ,"partial = {}\ndef reused(ids):" ,1 ) 11old = " return 0 if not REUSE else len(ids) - 1 if continued(ids)[1] else 3" 12assert source .count (old )== 1 13source = source .replace ( 14old , 15""" if not REUSE: return 0 16kept = max((len(h) for h in partial.values() if ids[:len(h)] == h), default=0) 17return max(kept, len(ids) - 1 if continued(ids)[1] else 3)""" , 18 ) 19old = " given = None\n for q in range(CH, len(ids) - 1, CH):" 20assert source .count (old )== 1 21source = source .replace ( 22old , 23" read_from = reused(ids)\n given = None\n for q in range((read_from // CH + 1) * CH, len(ids) - 1, CH):" , 24 ) 25old = ' print(f"DONE 0 {len(ids)} 5.0 0.0 cancel 0 0 {reused(ids)}", flush=True)' 26assert source .count (old )== 1 27source = source .replace ( 28old , 29""" partial[given[0]] = ids[:given[1]] 30print(f"DONE 0 {len(ids)} 5.0 0.0 cancel 0 0 {read_from}" + 31(f" 0 0 0 0 0.0 {given[1] - read_from}" if READF else ""), flush=True)""" , 32 ) 33source = source .replace ("{len(ids) - reused(ids)}" ,"{len(ids) - read_from}" ) 34source = source .replace ("{reused(ids)}" ,"{read_from}" ) 35compile (source ,"accurate_fake" ,"exec" ) 36return source 37 38 39class YieldAccounting (tp .FakeEngineCase ): 40def start_accurate (self ,slots ): 41with mock .patch .object (tp ,"FAKE_BATCH" ,accurate_fake ()): 42self .start (slots ,reuse = True ,more = ["--readfield" ]) 43 44def race_long (self ): 45replies = [] 46errors = [] 47 48def long (): 49try : 50replies .append (self .chat ("long " * 600 ,max_tokens = 64 )) 51except Exception as error : 52errors .append (repr (error )) 53 54thread = threading .Thread (target = long ) 55thread .start () 56time .sleep (0.15 ) 57self .chat ("short" ,max_tokens = 64 ) 58thread .join (20 ) 59self .assertFalse (thread .is_alive ()) 60self .assertEqual (errors , []) 61self .assertEqual (len (replies ),1 ) 62return replies [0 ] 63 64def check_full_input (self ,response ): 65n = response ["usage" ]["prompt_tokens" ] 66log = self .log .read_text ().splitlines () 67self .assertTrue (any (x .startswith ("YIELD " )for x in log ),log ) 68legs = [x for x in log if x .startswith (("GEN " ,"BGEN " ))and int (x .split ()[2 ])== n ] 69self .assertGreaterEqual (len (legs ),2 ) 70self .assertEqual (response ["usage" ]["prompt_tokens_details" ]["cached_tokens" ],3 ) 71self .assertEqual ((response ["timings" ]["cache_n" ],response ["timings" ]["prompt_n" ]), (3 ,n - 3 )) 72self .assertEqual (response ["timings" ]["prompt_ms" ],5.0 * len (legs )) 73row = max (self .svc .history ,key = lambda x :x ["prompt_tokens" ]) 74self .assertEqual ((row ["reused" ],row ["prompt_read" ]), (3 ,n - 3 )) 75 76def test_solo_yield_keeps_original_cache_and_sums_prompt_work (self ): 77self .start_accurate (2 ) 78self .check_full_input (self .race_long ()) 79 80def test_admission_yield_keeps_original_cache_and_sums_prompt_work (self ): 81self .start_accurate (3 ) 82errors = [] 83 84def background (): 85try : 86self .chat ("LONGREPLY background" ,max_tokens = 60 ) 87except Exception as error : 88errors .append (repr (error )) 89 90bg = threading .Thread (target = background ) 91bg .start () 92time .sleep (0.05 ) 93response = self .race_long () 94bg .join (20 ) 95self .assertFalse (bg .is_alive ()) 96self .assertEqual (errors , []) 97self .check_full_input (response ) 98 99def cancel_after_yields (self ,count ): 100self .start_accurate (2 ) 101original = self .engine ._take_control 102seen = 0 103 104def take (cancel ,plen ,after_epoch = None ,** kwargs ): 105nonlocal seen 106if after_epoch is not None : 107seen += 1 108if seen == count : 109cancel .set () 110return False 111return (yield from original (cancel ,plen ,** kwargs )) 112return (yield from original (cancel ,plen ,** kwargs )) 113 114with mock .patch .object (self .engine ,"_shorter_waiting" ,return_value = True ),mock .patch .object ( 115self .engine ,"_take_control" ,side_effect = take 116 ): 117self .chat ("long " * 600 ,max_tokens = 64 ) 118positions = [int (x .split ()[2 ])for x in self .log .read_text ().splitlines ()if x .startswith ("YIELD " )] 119self .assertEqual (len (positions ),count ) 120self .assertEqual (positions ,sorted (set (positions ))) 121row = self .svc .history [- 1 ] 122self .assertEqual (row ["finish" ],"cancel" ) 123self .assertEqual ( 124 (row ["prompt_tokens" ],row ["reused" ],row ["prompt_read" ]), (positions [- 1 ],3 ,positions [- 1 ]- 3 ) 125 ) 126self .assertEqual (row ["prompt_ms" ],5.0 * count ) 127 128def test_cancel_after_one_yield_counts_that_done_once (self ): 129self .cancel_after_yields (1 ) 130 131def test_cancel_after_two_yields_counts_each_done_once (self ): 132self .cancel_after_yields (2 )