|
| 1 | +"""A failed admission answers its own request; the concurrent CUDA scheduler goes on serving the others.""" |
| 2 | + |
| 3 | +import importlib |
| 4 | +import threading |
| 5 | +import time |
| 6 | +from types import SimpleNamespace |
| 7 | + |
| 8 | +import pytest |
| 9 | +import torch |
| 10 | + |
| 11 | +from tensorfold.cuda.scheduler import Scheduler |
| 12 | +from tensorfold.cuda.streams import Stream |
| 13 | +from tests.test_cuda_geometry import allocations # noqa: F401 (fixture: fake triton, so the module imports) |
| 14 | + |
| 15 | +WAIT = 10.0 # seconds a reply may take; a stopped worker never replies |
| 16 | + |
| 17 | + |
| 18 | +class Request: |
| 19 | + """``Scheduler.submit`` on its own thread; ``reply()`` is ("done", stats), ("error", exception) or None.""" |
| 20 | + |
| 21 | + def __init__(self, sched, prompt, count): |
| 22 | + self.got = [] |
| 23 | + self.thread = threading.Thread(target=self._go, args=(sched, list(prompt), count), daemon=True) |
| 24 | + self.thread.start() |
| 25 | + |
| 26 | + def _go(self, sched, prompt, count): |
| 27 | + try: |
| 28 | + self.got.append(("done", sched.submit(prompt, count, None, True, lambda new: False))) |
| 29 | + except Exception as exc: # noqa: BLE001 |
| 30 | + self.got.append(("error", exc)) |
| 31 | + |
| 32 | + def reply(self): |
| 33 | + self.thread.join(WAIT) |
| 34 | + return self.got[0] if self.got else None |
| 35 | + |
| 36 | + |
| 37 | +def ask(sched, prompt, count): |
| 38 | + return Request(sched, prompt, count).reply() |
| 39 | + |
| 40 | + |
| 41 | +class St: |
| 42 | + """A committed state: its position only (the stand-in prefill and copies hold no tensors).""" |
| 43 | + |
| 44 | + def __init__(self, pos=0): |
| 45 | + self.pos, self.limit = pos, 0 |
| 46 | + |
| 47 | + |
| 48 | +@pytest.fixture |
| 49 | +def decoders(monkeypatch, allocations): # noqa: F811 |
| 50 | + """``make(world)``: the 27B's ``MultiDecoder`` on CPU stand-ins; ``fail["copy"]`` fails the next prompt-end copy.""" |
| 51 | + |
| 52 | + multi = importlib.import_module("tensorfold.families.qwen3_5.cuda.multi") |
| 53 | + fail = {"copy": False} |
| 54 | + |
| 55 | + def prefill(w, prompt, sampling, *rest, state=None): |
| 56 | + return St(len(prompt)), 7 |
| 57 | + |
| 58 | + def private(st): |
| 59 | + if fail["copy"]: |
| 60 | + fail["copy"] = False |
| 61 | + raise torch.OutOfMemoryError("CUDA out of memory (simulated at the prompt-end copy)") |
| 62 | + return St(st.pos) |
| 63 | + |
| 64 | + monkeypatch.setattr(multi, "prefill", prefill) |
| 65 | + monkeypatch.setattr(multi, "prefill_tp", prefill) |
| 66 | + monkeypatch.setattr(multi, "private", private) |
| 67 | + monkeypatch.setattr(multi, "State", lambda w: St()) |
| 68 | + monkeypatch.setattr(multi, "_share", lambda values, src, device: values) # two ranks: no NCCL here |
| 69 | + |
| 70 | + def make(world=1): |
| 71 | + w = SimpleNamespace(config=SimpleNamespace(eos=(0,), vocab=10), norm=SimpleNamespace(device="cpu"), |
| 72 | + head=SimpleNamespace(n=10)) |
| 73 | + dec = multi.MultiDecoder(w, None, allow_copy=False, world=world) |
| 74 | + dec.gate, dec.entered = threading.Event(), threading.Event() # a round waits at the gate while it is shut |
| 75 | + dec.gate.set() |
| 76 | + |
| 77 | + def verify(plan, copied=None): # each window keeps its pending token, samples 5 |
| 78 | + dec.entered.set() |
| 79 | + dec.gate.wait(WAIT) |
| 80 | + return [([pending], [-1]) for _, _, pending, _ in plan], None, None, None, [[5] for _ in plan] |
| 81 | + |
| 82 | + dec._verify = verify |
| 83 | + dec._commit = lambda *args: None |
| 84 | + return dec |
| 85 | + |
| 86 | + return make, fail |
| 87 | + |
| 88 | + |
| 89 | +A, B, C, D, E = ([k] * 5 + list(range(k * 10, k * 10 + 30)) for k in (1, 2, 3, 4, 5)) |
| 90 | + |
| 91 | + |
| 92 | +def test_a_failed_prompt_end_copy_answers_its_request_and_the_worker_goes_on(decoders): |
| 93 | + make, fail = decoders |
| 94 | + dec = make() |
| 95 | + sched = Scheduler(dec, max_streams=4) |
| 96 | + assert ask(sched, A, 1)[0] == "done" |
| 97 | + fail["copy"] = True |
| 98 | + b = ask(sched, B, 64) |
| 99 | + assert b is not None and b[0] == "error" and isinstance(b[1], torch.OutOfMemoryError), b |
| 100 | + c, d = ask(sched, C, 1), ask(sched, D, 3) # D decodes two rounds |
| 101 | + assert c is not None and c[0] == "done", c |
| 102 | + assert d is not None and d[0] == "done", d |
| 103 | + assert sched.thread.is_alive() |
| 104 | + assert dec.live() == 0 and not dec.streams |
| 105 | + assert B not in [entry[0] for entry in dec.cache.entries] |
| 106 | + assert [entry[0] for entry in dec.cache.entries] == [A, C, D] |
| 107 | + |
| 108 | + |
| 109 | +def test_a_failed_admission_leaves_the_live_streams_decoding(decoders): |
| 110 | + make, fail = decoders |
| 111 | + dec = make() |
| 112 | + dec.gate.clear() |
| 113 | + sched = Scheduler(dec, max_streams=4) |
| 114 | + e = Request(sched, E, 3) |
| 115 | + assert dec.entered.wait(WAIT) # E is admitted; its first round waits |
| 116 | + fail["copy"] = True |
| 117 | + b = Request(sched, B, 64) |
| 118 | + deadline = time.monotonic() + WAIT |
| 119 | + while sched.waiting.qsize() == 0 and time.monotonic() < deadline: |
| 120 | + time.sleep(0.01) |
| 121 | + dec.gate.set() # B is admitted after E's first round |
| 122 | + got_b, got_e = b.reply(), e.reply() |
| 123 | + assert got_b is not None and got_b[0] == "error" and isinstance(got_b[1], torch.OutOfMemoryError), got_b |
| 124 | + assert got_e is not None and got_e[0] == "done", got_e |
| 125 | + assert sched.thread.is_alive() and not dec.streams |
| 126 | + |
| 127 | + |
| 128 | +def test_two_ranks_answer_every_later_request_with_the_restart_error(decoders): |
| 129 | + make, fail = decoders |
| 130 | + dec = make(world=2) |
| 131 | + sched = Scheduler(dec, max_streams=4) |
| 132 | + assert ask(sched, A, 1)[0] == "done" |
| 133 | + fail["copy"] = True |
| 134 | + b = ask(sched, B, 64) |
| 135 | + assert b is not None and b[0] == "error" and isinstance(b[1], torch.OutOfMemoryError), b |
| 136 | + assert isinstance(dec.broken, torch.OutOfMemoryError) |
| 137 | + for prompt in (C, D): |
| 138 | + got = ask(sched, prompt, 1) |
| 139 | + assert got is not None and got[0] == "error", got |
| 140 | + assert "restart both" in str(got[1]) |
| 141 | + assert sched.thread.is_alive() and not dec.streams |
| 142 | + |
| 143 | + |
| 144 | +class FailingRound: |
| 145 | + """A decoder whose first round fails; its ``drop()`` also returns a stream no request waits on.""" |
| 146 | + |
| 147 | + def __init__(self): |
| 148 | + self.streams, self.failed = [], False |
| 149 | + |
| 150 | + def live(self): |
| 151 | + return len(self.streams) |
| 152 | + |
| 153 | + def admit(self, s): |
| 154 | + self.streams.append(s) |
| 155 | + s.take([1]) |
| 156 | + |
| 157 | + def round(self): |
| 158 | + live = [s for s in self.streams if not s.done] |
| 159 | + if live and not self.failed: |
| 160 | + self.failed = True |
| 161 | + raise RuntimeError("a round failed") |
| 162 | + for s in live: |
| 163 | + s.take([2]) |
| 164 | + return [s for s in live if s.done] |
| 165 | + |
| 166 | + def drop(self): |
| 167 | + live = [s for s in self.streams if not s.done] |
| 168 | + self.streams = [s for s in self.streams if s.done] |
| 169 | + return live + [Stream([9], 1)] |
| 170 | + |
| 171 | + def finish(self, done): |
| 172 | + gone = {id(s) for s in done} |
| 173 | + self.streams = [s for s in self.streams if id(s) not in gone] |
| 174 | + |
| 175 | + |
| 176 | +def test_a_dropped_stream_without_a_request_does_not_stop_the_worker(): |
| 177 | + dec = FailingRound() |
| 178 | + sched = Scheduler(dec, max_streams=4) |
| 179 | + x = ask(sched, [1, 2, 3], 2) |
| 180 | + assert x is not None and x[0] == "error" and str(x[1]) == "a round failed", x |
| 181 | + y = ask(sched, [4, 5, 6], 2) |
| 182 | + assert y is not None and y[0] == "done", y |
| 183 | + assert sched.thread.is_alive() and not dec.streams |
| 184 | + |
0 commit comments