okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
9aea047ee
main
1"""After an aborted request the engine and the server are back in step: the next requests get their OWN answers. 2 3A request that ends on an engine ERR (the DeepSeek NaN stop printed `ERR ...` and then also `DONE ...`), a client that 4cancels mid-answer, and one that disconnects during the prompt: each is followed by two ordinary requests, and each of those 5must receive the tokens meant for it - never the previous request's DONE or tokens (found on 2 x A4000 at 64K: the needle prompt 6after a NaN-stopped request came back empty at once, and its answer went to the request after it). No GPU or model is used. 7""" 8import sys 9import tempfile 10import threading 11import unittest 12from pathlib import Path 13from unittest import mock 14 15from serve import server 16 17FAKE = r'''import queue, sys, threading, time 18mode = sys.argv[sys.argv.index('--mode') + 1] 19cmds, stop = queue.Queue(), threading.Event() 20def reader(): 21for line in sys.stdin: 22line = line.strip() 23if line == 'STOP': 24stop.set() 25else: 26cmds.put(line) 27cmds.put('QUIT') 28threading.Thread(target=reader, daemon=True).start() 29print('READY 4096 stop', flush=True) 30n = 0 31while True: 32line = cmds.get() 33if line == 'QUIT': 34break 35if not line.startswith('GEN '): 36continue 37n += 1 38stop.clear() 39ids = line.split()[-1] 40if n == 1 and mode in ('err_done', 'err_only'): 41print('T 7', flush=True) 42print("ERR the model's numbers went non-finite (NaN reached the router), so the answer would be nonsense", flush=True) 43if mode == 'err_done': 44print('DONE 1 5 1 1 cancel 0 0 0', flush=True) 45elif n == 1 and mode == 'cancel': 46print('T 7', flush=True) 47stop.wait(10) 48time.sleep(.05) 49print('DONE 1 5 1 1 cancel 0 0 0', flush=True) 50else: 51# the answer of request n: its own marker token (100 + n) twice, so a late or early line is visible 52print(f'T {100 + n}', flush=True) 53print(f'T {100 + n}', flush=True) 54print('DONE 2 5 1 1 stop 0 0 0', flush=True) 55''' 56 57 58class AbortSync (unittest .TestCase ): 59def engine (self ,mode ): 60tmp = tempfile .TemporaryDirectory () 61self .addCleanup (tmp .cleanup ) 62script = Path (tmp .name )/ 'engine.py' 63script .write_text (FAKE ,encoding = 'utf-8' ) 64real_popen = server .subprocess .Popen 65with mock .patch .object (server .subprocess ,'Popen' ,lambda cmd ,** kw : 66real_popen ([sys .executable ,str (script ),* cmd [1 :]],** kw )): 67eng = server .StrataEngine ('strata' , ['--mode' ,mode ]) 68self .addCleanup (eng .close ) 69return eng 70 71def ask (self ,eng ,cancel_after = None ): 72cancel = threading .Event () 73got = [] 74try : 75for t in eng .generate ([1 ,2 ,3 ],8 , {},cancel ): 76got .append (t ) 77if cancel_after is not None and len (got )>= cancel_after : 78cancel .set () 79break 80except ValueError as e : 81return got ,str (e ) 82return got ,None 83 84def two_following (self ,eng ,first_n ): 85a ,err_a = self .ask (eng ) 86b ,err_b = self .ask (eng ) 87self .assertEqual ((a ,err_a ), ([100 + first_n + 1 ]* 2 ,None ),"the first request after the abort got someone else's answer" ) 88self .assertEqual ((b ,err_b ), ([100 + first_n + 2 ]* 2 ,None ),"the second request after the abort got someone else's answer" ) 89 90def test_err_then_done_does_not_shift_the_next_answers (self ): 91eng = self .engine ('err_done' ) 92got ,err = self .ask (eng ) 93self .assertEqual (got , [7 ]) 94self .assertIn ('non-finite' ,err ) 95self .two_following (eng ,1 ) 96 97def test_err_alone_is_over_with_the_err (self ): 98eng = self .engine ('err_only' ) 99got ,err = self .ask (eng ) 100self .assertIn ('non-finite' ,err ) 101self .two_following (eng ,1 ) 102 103def test_a_cancel_mid_answer_leaves_the_engine_in_step (self ): 104eng = self .engine ('cancel' ) 105got ,err = self .ask (eng ,cancel_after = 1 ) 106self .assertEqual ((got ,err ), ([7 ],None )) 107self .two_following (eng ,1 ) 108 109 110if __name__ == '__main__' : 111unittest .main ()