A pipelined group's window spans its slots up to its last busy one, and the engine keeps no pipelined slot as a conversation cache, so the least-recently-used pick left leading holes ([_ B]) that cost a whole row each. pick_slot now fills such a hole first, then follows the spreading order. The engine reports INFO batch_groups so the server knows the groups really run pipelined. serve/test_slot_alloc.py covers the allocator. Co-authored-by: blange48 <blange48@users.noreply.github.com>
84 lines
3.3 KiB
Python
84 lines
3.3 KiB
Python
"""serve/test_slot_alloc.py - which batch slot a request gets (StrataEngine.pick_slot), no engine needed.
|
|
|
|
python -m unittest serve.test_slot_alloc -v
|
|
"""
|
|
from __future__ import annotations
|
|
|
|
import random
|
|
import sys
|
|
import unittest
|
|
from pathlib import Path
|
|
|
|
sys.path.insert(0, str(Path(__file__).resolve().parents[1]))
|
|
from serve.server import StrataEngine # noqa: E402
|
|
|
|
|
|
def engine(batch, groups, pipelined=True):
|
|
e = object.__new__(StrataEngine) # pick_slot reads only the slot tables
|
|
gs = batch // groups
|
|
e.batch = batch
|
|
e.slot_order = [g * gs + t for t in range(gs) for g in range(groups)]
|
|
e.slot_busy = [False] * batch
|
|
e.slot_held = [[] for _ in range(batch)]
|
|
e.slot_used = [0.0] * batch
|
|
e.slot_gs = gs if pipelined and groups > 1 else 0
|
|
return e
|
|
|
|
|
|
class Pipelined(unittest.TestCase):
|
|
def test_spreads_over_the_groups_first(self):
|
|
e = engine(6, 3)
|
|
got = []
|
|
for _ in range(6):
|
|
b = e.pick_slot([1, 2, 3])
|
|
e.slot_busy[b] = True
|
|
got.append(b)
|
|
self.assertEqual(got, [0, 2, 4, 1, 3, 5])
|
|
|
|
def test_fills_a_leading_hole_first(self):
|
|
# #793's report: a group left as [_ B] costs a whole row; the next request goes into that hole
|
|
e = engine(6, 3)
|
|
e.slot_busy[3] = True # group 1 = [_ B]
|
|
e.slot_used[0] = 0.0 # least recently used: what the LRU fallback picked
|
|
e.slot_used[2] = 99.0
|
|
self.assertEqual(e.pick_slot([1]), 2)
|
|
|
|
def test_never_opens_a_hole_below_the_pick(self):
|
|
for batch, groups in ((6, 3), (8, 4), (8, 2), (4, 2)):
|
|
gs = batch // groups
|
|
e, rng = engine(batch, groups), random.Random(batch * 10 + groups)
|
|
for _ in range(2000):
|
|
busy = [b for b in range(batch) if e.slot_busy[b]]
|
|
if busy and (rng.random() < 0.45 or len(busy) == batch):
|
|
e.slot_busy[rng.choice(busy)] = False # a request ends, anywhere
|
|
continue
|
|
b = e.pick_slot([7])
|
|
g0 = b - b % gs
|
|
with self.subTest(batch=batch, groups=groups):
|
|
self.assertTrue(all(e.slot_busy[x] for x in range(g0, b)),
|
|
f"slot {b} picked with a free slot below it in its group: {e.slot_busy}")
|
|
e.slot_busy[b] = True
|
|
|
|
def test_held_tokens_are_ignored(self):
|
|
# the engine keeps no pipelined slot as a cache: a held prefix must not pull a request into a hole-making slot
|
|
e = engine(6, 3)
|
|
e.slot_held[5] = [1, 2]
|
|
self.assertEqual(e.pick_slot([1, 2, 3]), 0)
|
|
|
|
|
|
class OneGroup(unittest.TestCase):
|
|
def test_reuse_then_least_recently_used(self):
|
|
e = engine(4, 1, pipelined=False)
|
|
e.slot_held[2] = [1, 2]
|
|
self.assertEqual(e.pick_slot([1, 2, 3]), 2) # its conversation's next turn
|
|
e.slot_held[2] = [9]
|
|
e.slot_held = [[5], [], [9], [6]]
|
|
self.assertEqual(e.pick_slot([1, 2, 3]), 1) # an empty slot before evicting a held one
|
|
e.slot_held[1] = [8]
|
|
e.slot_used = [5.0, 3.0, 9.0, 1.0]
|
|
self.assertEqual(e.pick_slot([1, 2, 3]), 3) # else the least recently used
|
|
|
|
|
|
if __name__ == "__main__":
|
|
unittest.main()
|