okami.mom/strata

git clone https://git.nkp.pet/okami.mom/strata

Niko1221serve: pick_slot sends a chat's next turn to the slot that holds it, found by its turn checkpoint (#2518)dd08a7d0e

main
4.5 KiB108 linesraw
1"""serve/test_slot_alloc.py - which batch slot a request gets (StrataEngine.pick_slot), no engine needed.
2
3    python -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):
17    e = object.__new__(StrataEngine)          # pick_slot reads only the slot tables
18    gs = batch // groups
19    e.batch = batch
20    e.slot_order = [g * gs + t for t in range(gs) for g in range(groups)]
21    e.slot_busy = [False] * batch
22    e.slot_held = [[] for _ in range(batch)]
23    e.slot_used = [0.0] * batch
24    e.slot_gs = gs if pipelined and groups > 1 else 0
25    return e
26
27
28class Pipelined(unittest.TestCase):
29    def test_spreads_over_the_groups_first(self):
30        e = engine(6, 3)
31        got = []
32        for _ in range(6):
33            b = e.pick_slot([1, 2, 3])
34            e.slot_busy[b] = True
35            got.append(b)
36        self.assertEqual(got, [0, 2, 4, 1, 3, 5])
37
38    def 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
40        e = engine(6, 3)
41        e.slot_busy[3] = True                       # group 1 = [_ B]
42        e.slot_used[0] = 0.0                        # least recently used: what the LRU fallback picked
43        e.slot_used[2] = 99.0
44        self.assertEqual(e.pick_slot([1]), 2)
45
46    def test_never_opens_a_hole_below_the_pick(self):
47        for batch, groups in ((6, 3), (8, 4), (8, 2), (4, 2)):
48            gs = batch // groups
49            e, rng = engine(batch, groups), random.Random(batch * 10 + groups)
50            for _ in range(2000):
51                busy = [b for b in range(batch) if e.slot_busy[b]]
52                if busy and (rng.random() < 0.45 or len(busy) == batch):
53                    e.slot_busy[rng.choice(busy)] = False          # a request ends, anywhere
54                    continue
55                b = e.pick_slot([7])
56                g0 = b - b % gs
57                with self.subTest(batch=batch, groups=groups):
58                    self.assertTrue(all(e.slot_busy[x] for x in range(g0, b)),
59                                    f"slot {b} picked with a free slot below it in its group: {e.slot_busy}")
60                e.slot_busy[b] = True
61
62    def 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
64        e = engine(6, 3)
65        e.slot_held[5] = [1, 2]
66        self.assertEqual(e.pick_slot([1, 2, 3]), 0)
67
68
69class OneGroup(unittest.TestCase):
70    def test_reuse_then_least_recently_used(self):
71        e = engine(4, 1, pipelined=False)
72        e.slot_held[2] = [1, 2]
73        self.assertEqual(e.pick_slot([1, 2, 3]), 2)        # its conversation's next turn
74        e.slot_held[2] = [9]
75        e.slot_held = [[5], [], [9], [6]]
76        self.assertEqual(e.pick_slot([1, 2, 3]), 1)        # an empty slot before evicting a held one
77        e.slot_held[1] = [8]
78        e.slot_used = [5.0, 3.0, 9.0, 1.0]
79        self.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,
84    so no pad row writes an idle slot and each one keeps its chat (#2518): the next turn goes back to it."""
85
86    def test_the_slot_of_the_chat(self):
87        T = StrataEngine.TURN_ID
88        e = engine(2, 2)
89        self.assertEqual(e.slot_gs, 1)
90        hist_a, hist_b = list(range(1000, 1700)), list(range(2000, 2700))
91        e.slot_held = [hist_a + [T, 7, 8] + [50, 51], hist_b + [T, 7, 8] + [60]]
92        e.slot_used = [10.0, 20.0]
93        again = lambda h: h + [T, 5, 6] + list(range(3000, 3050))
94        self.assertEqual(e.pick_slot(again(hist_b)), 1)
95        self.assertEqual(e.pick_slot(again(hist_a)), 0)
96        self.assertEqual(e.pick_slot(list(range(5000, 5100))), 0)      # a new chat: the least recently used one
97
98    def test_groups_of_several_slots_still_ignore_held_tokens(self):
99        T = StrataEngine.TURN_ID
100        e = engine(4, 2)
101        self.assertEqual(e.slot_gs, 2)
102        hist = list(range(1000, 1700))
103        e.slot_held[3] = hist + [T, 7, 8] + [50]
104        self.assertEqual(e.pick_slot(hist + [T, 5, 6] + list(range(3000, 3050))), 0)
105
106
107if __name__ == "__main__":
108    unittest.main()