okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
2b36e8a02
main
1#!/usr/bin/env python3 2"""SAVE / RESTORE round trip with parked conversations holding the host RAM (#1572 / #1573). 3 4python3 research/tools/live-probes-2026-10/session_pressure_test.py CFG OUTFILE 5STRATA_EXTRA_ARGS="--prompt-cache 4 --conversation-cache-mib 3072 --conversation-cache-slots 4 6--conversation-cache-min-free-mib <floor> [--session-save-reclaim]" 7 8Parks three conversations, runs a fourth (D), SAVEs it, runs an unrelated one, RESTOREs, and checks that D's next turn 9gives the same token ids after the restore as it does when continued live. Prints SAVE: ok|refused (...) and PASS/FAIL. 10""" 11import json ,os ,sys ,threading ,time 12from pathlib import Path 13 14REPO = Path (os .environ .get ("STRATA_REPO" ,Path (__file__ ).resolve ().parents [3 ])) 15sys .path .insert (0 ,str (REPO / "tools" ));sys .path .insert (0 ,str (REPO )) 16import strata_tokenizer as ST 17from serve import server as S 18from serve .server import StrataEngine ,child_env 19 20cfg = json .load (open (sys .argv [1 ])) 21out_file = sys .argv [2 ] 22tp = Path (cfg ["tokenizer" ]) 23vocab = json .loads ((tp / "vocab.json" ).read_text ()) 24toks = [None ]* len (vocab ) 25for t ,i in vocab .items (): 26toks [i ]= t 27tok = ST .Tokenizer (toks , (tp / "merges.txt" ).read_text ().split ("\n" ),json .loads ((tp / "token_type.json" ).read_text ())) 28args = cfg ["args" ]+ os .environ .get ("STRATA_EXTRA_ARGS" ,"" ).split () 29eng = StrataEngine (os .environ .get ("STRATA_EXE" ,cfg ["exe" ]),args ,cwd = cfg .get ("cwd" ), 30log = os .environ .get ("SMOKE_LOG" ,cfg ["log" ]),env = child_env (cfg )) 31GEN = "<|im_end|>\n<|im_start|>assistant\n<think>\n\n</think>\n\n" 32 33 34def enc (text ): 35return tok .encode (text ,parse_special = True ) 36 37 38def run (ids ,max_new = 60 ): 39out = [t for t in eng .generate (ids ,max_new , {"temperature" :0 },threading .Event ())if t is not None ] 40return out ,int (eng .last .get ("reused" )or 0 ) 41 42 43def chat (tag ,n ): 44return enc (f"<|im_start|>user\n[ { tag } ] " + " " .join (f"Entry { i } of { tag } : value { i * 13 % 97 } ." for i in range (n )) 45+ "\nSummarize in one line." + GEN ) 46 47 48for tag in ("alpha" ,"bravo" ,"charlie" ): 49p = chat (tag ,70 ) 50run (p ,20 ) 51D = chat ("delta" ,80 ) 52outD ,_ = run (D ,60 ) 53f"D: prompt { len ( D ) } tokens, { len ( outD ) } generated" ,flush = True ) 54nxt = enc ("<|im_end|>\n<|im_start|>user\nNow say only the word done." + GEN ) 55saved = False 56hog = None 57if os .environ .get ("HOG_TARGET_MIB" ):# take the RAM so that only HOG_TARGET_MIB stay available: a SAVE needs more 58import psutil 59extra = psutil .virtual_memory ().available - int (os .environ ["HOG_TARGET_MIB" ])* 2 ** 20 60if extra > 0 : 61hog = bytearray (extra ) 62for i in range (0 ,len (hog ),4096 ): 63hog [i ]= 1 64"available now: %d MiB" % (psutil .virtual_memory ().available >> 20 ),flush = True ) 65try : 66r = eng .session_file ("save" ,out_file ) 67saved = True 68"SAVE: ok" ,r ,flush = True ) 69except S .SessionRefused as e : 70"SAVE: refused" ,e .kind ,str (e )[:300 ],flush = True ) 71if not saved and "--session-save-reclaim" in args : 72# the server's bounded retry on the same engine 73class Svc : 74engine = eng 75from serve .session_save_retry import session_file as retry 76try : 77r = retry (Svc ,"save" ,out_file ,S .SessionRefused ) 78saved = True 79"SAVE after the bounded wait: ok" ,r ,flush = True ) 80except S .SessionRefused as e : 81"SAVE after the bounded wait: refused" ,e .kind ,str (e )[:300 ],flush = True ) 82hog = None # the RAM goes back 83if not saved : 84"NOSAVE" ) 85eng .proc .stdin .close () 86sys .exit (2 ) 87live_next ,reused_live = run (D + outD + nxt ,20 ) 88f"D live next turn: reused { reused_live } " ,flush = True ) 89run (chat ("echo" ,70 ),20 )# something else takes the session 90r = eng .session_file ("restore" ,out_file ) 91"RESTORE:" ,r ,flush = True ) 92again ,reused_r = run (D + outD + nxt ,20 ) 93f"D after RESTORE: reused { reused_r } " ,flush = True ) 94eng .proc .stdin .close () 95try : 96eng .proc .wait (60 ) 97except Exception : 98eng .proc .kill () 99ok = again == live_next and reused_r > 0 100"PASS" if ok else "FAIL (ids %s)" % ("equal" if again == live_next else "differ" )) 101sys .exit (0 if ok else 1 )