okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
dd08a7d0e
main
1"""serve/test_slot_alloc.py - which batch slot a request gets (StrataEngine.pick_slot), no engine needed. 2 3python -m unittest serve.test_slot_alloc -v 4""" 5from __future__import annotations 6 7import random 8import sys 9import unittest 10from pathlib import Path 11 12sys .path .insert (0 ,str (Path (__file__ ).resolve ().parents [1 ])) 13from serve .server import StrataEngine # noqa: E402 14 15 16def engine (batch ,groups ,pipelined = True ): 17e = object .__new__ (StrataEngine )# pick_slot reads only the slot tables 18gs = batch // groups 19e .batch = batch 20e .slot_order = [g * gs + t for t in range (gs )for g in range (groups )] 21e .slot_busy = [False ]* batch 22e .slot_held = [[]for _ in range (batch )] 23e .slot_used = [0.0 ]* batch 24e .slot_gs = gs if pipelined and groups > 1 else 0 25return e 26 27 28class Pipelined (unittest .TestCase ): 29def test_spreads_over_the_groups_first (self ): 30e = engine (6 ,3 ) 31got = [] 32for _ in range (6 ): 33b = e .pick_slot ([1 ,2 ,3 ]) 34e .slot_busy [b ]= True 35got .append (b ) 36self .assertEqual (got , [0 ,2 ,4 ,1 ,3 ,5 ]) 37 38def test_fills_a_leading_hole_first (self ): 39# #793's report: a group left as [_ B] costs a whole row; the next request goes into that hole 40e = engine (6 ,3 ) 41e .slot_busy [3 ]= True # group 1 = [_ B] 42e .slot_used [0 ]= 0.0 # least recently used: what the LRU fallback picked 43e .slot_used [2 ]= 99.0 44self .assertEqual (e .pick_slot ([1 ]),2 ) 45 46def test_never_opens_a_hole_below_the_pick (self ): 47for batch ,groups in ((6 ,3 ), (8 ,4 ), (8 ,2 ), (4 ,2 )): 48gs = batch // groups 49e ,rng = engine (batch ,groups ),random .Random (batch * 10 + groups ) 50for _ in range (2000 ): 51busy = [b for b in range (batch )if e .slot_busy [b ]] 52if busy and (rng .random ()< 0.45 or len (busy )== batch ): 53e .slot_busy [rng .choice (busy )]= False # a request ends, anywhere 54continue 55b = e .pick_slot ([7 ]) 56g0 = b - b % gs 57with self .subTest (batch = batch ,groups = groups ): 58self .assertTrue (all (e .slot_busy [x ]for x in range (g0 ,b )), 59f"slot { b } picked with a free slot below it in its group: { e . slot_busy } " ) 60e .slot_busy [b ]= True 61 62def test_held_tokens_are_ignored (self ): 63# the engine keeps no pipelined slot as a cache: a held prefix must not pull a request into a hole-making slot 64e = engine (6 ,3 ) 65e .slot_held [5 ]= [1 ,2 ] 66self .assertEqual (e .pick_slot ([1 ,2 ,3 ]),0 ) 67 68 69class OneGroup (unittest .TestCase ): 70def test_reuse_then_least_recently_used (self ): 71e = engine (4 ,1 ,pipelined = False ) 72e .slot_held [2 ]= [1 ,2 ] 73self .assertEqual (e .pick_slot ([1 ,2 ,3 ]),2 )# its conversation's next turn 74e .slot_held [2 ]= [9 ] 75e .slot_held = [[5 ], [], [9 ], [6 ]] 76self .assertEqual (e .pick_slot ([1 ,2 ,3 ]),1 )# an empty slot before evicting a held one 77e .slot_held [1 ]= [8 ] 78e .slot_used = [5.0 ,3.0 ,9.0 ,1.0 ] 79self .assertEqual (e .pick_slot ([1 ,2 ,3 ]),3 )# else the least recently used 80 81 82class OneSlotGroups (unittest .TestCase ): 83"""--batch 2 on a two-GPU layer split: two pipelined groups of ONE slot. No slot sits below another in its group, 84so no pad row writes an idle slot and each one keeps its chat (#2518): the next turn goes back to it.""" 85 86def test_the_slot_of_the_chat (self ): 87T = StrataEngine .TURN_ID 88e = engine (2 ,2 ) 89self .assertEqual (e .slot_gs ,1 ) 90hist_a ,hist_b = list (range (1000 ,1700 )),list (range (2000 ,2700 )) 91e .slot_held = [hist_a + [T ,7 ,8 ]+ [50 ,51 ],hist_b + [T ,7 ,8 ]+ [60 ]] 92e .slot_used = [10.0 ,20.0 ] 93again = lambda h :h + [T ,5 ,6 ]+ list (range (3000 ,3050 )) 94self .assertEqual (e .pick_slot (again (hist_b )),1 ) 95self .assertEqual (e .pick_slot (again (hist_a )),0 ) 96self .assertEqual (e .pick_slot (list (range (5000 ,5100 ))),0 )# a new chat: the least recently used one 97 98def test_groups_of_several_slots_still_ignore_held_tokens (self ): 99T = StrataEngine .TURN_ID 100e = engine (4 ,2 ) 101self .assertEqual (e .slot_gs ,2 ) 102hist = list (range (1000 ,1700 )) 103e .slot_held [3 ]= hist + [T ,7 ,8 ]+ [50 ] 104self .assertEqual (e .pick_slot (hist + [T ,5 ,6 ]+ list (range (3000 ,3050 ))),0 ) 105 106 107if __name__ == "__main__" : 108unittest .main ()