okami.mom/strata

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

Niko1221Trial2 merge f-0291-pr-nv (97b3d614f)1306424be

main
3.6 KiB82 linesraw
1"""serve/test_fatal_gpu_restart.py: #4350.  After a fatal GPU error the DeepSeek engine ends itself (ERR fatal GPU error: ...) and the
2server starts it again on the next request; three such deaths within ten minutes stop the restarts with a plain 503 text.
3CPU only, no engine: the server's bookkeeping is called on a stand-in."""
4import sys
5import time
6import types
7import unittest
8from pathlib import Path
9
10sys.path.insert(0, str(Path(__file__).resolve().parents[1]))
11from serve import server as S  # noqa: E402
12
13
14def stand_in(pid, last_err):
15    return types.SimpleNamespace(engine=types.SimpleNamespace(last_err=last_err, proc=types.SimpleNamespace(pid=pid)))
16
17
18class FatalGpuRestart(unittest.TestCase):
19    def note(self, svc, code=3):
20        S.Service._note_fatal_gpu_exit.__get__(svc)(code)
21
22    def make(self, pid, err="fatal GPU error: cudaStreamSynchronize(s): an illegal memory access was encountered"):
23        svc = stand_in(pid, err)
24        svc.FATAL_GPU_MAX = S.Service.FATAL_GPU_MAX
25        svc.FATAL_GPU_WINDOW_S = S.Service.FATAL_GPU_WINDOW_S
26        return svc
27
28    def test_one_fatal_death_is_restarted_with_one_plain_line(self):
29        svc = self.make(100)
30        self.note(svc)                      # no exception: the caller goes on to restart the engine
31
32    def test_an_ordinary_death_is_not_counted(self):
33        svc = self.make(100, err="the engine ran out of memory")
34        for pid in range(10):
35            svc.engine.proc.pid = pid
36            self.note(svc)
37        self.assertFalse(svc.__dict__.get("_fatal_gpu_times"))
38
39    def test_the_same_death_seen_by_many_requests_counts_once(self):
40        svc = self.make(100)
41        for _ in range(8):
42            self.note(svc)
43        self.assertEqual(len(svc._fatal_gpu_times), 1)
44
45    def test_three_fatal_deaths_in_ten_minutes_stop_the_restarts_and_say_so(self):
46        svc = self.make(1)
47        self.note(svc)
48        svc.engine.proc.pid = 2
49        self.note(svc)
50        svc.engine.proc.pid = 3
51        with self.assertRaises(S.EngineDied) as cm:
52            self.note(svc)
53        self.assertIn("3 times in 10 minutes", str(cm.exception))
54        self.assertIn("not started again", str(cm.exception))
55        with self.assertRaises(S.EngineDied):          # the same dead engine asked again: still refused
56            self.note(svc)
57
58    def test_after_the_window_it_tries_again(self):
59        svc = self.make(1)
60        self.note(svc)
61        svc.engine.proc.pid = 2
62        self.note(svc)
63        svc._fatal_gpu_times[:] = [t - S.Service.FATAL_GPU_WINDOW_S - 1 for t in svc._fatal_gpu_times]
64        svc.engine.proc.pid = 3
65        self.note(svc)                      # the two old ones aged out: only this one counts
66
67    def test_the_request_in_flight_fails_as_an_engine_death_not_a_bad_request(self):
68        eng = types.SimpleNamespace(last_err=None, ended=False)
69        with self.assertRaises(S.EngineDied) as cm:
70            S.StrataEngine._raise_if_fatal_gpu(eng, "ERR fatal GPU error: cudaStreamSynchronize(s): an illegal memory access was encountered")
71        self.assertIn("illegal memory access", str(cm.exception))
72        self.assertTrue(eng.ended)                                  # the next request starts the engine at once
73        self.assertTrue(eng.last_err.startswith("fatal GPU error"))
74
75    def test_an_ordinary_engine_error_stays_a_bad_request(self):
76        eng = types.SimpleNamespace(last_err=None, ended=False)
77        S.StrataEngine._raise_if_fatal_gpu(eng, "ERR the prompt (9 tokens) and max_new 5 are more than the context 8")   # no exception
78        self.assertFalse(eng.ended)
79
80
81if __name__ == "__main__":
82    unittest.main()