okami.mom/strata

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

Niko1221Merge branch 'f-0212-int' into f-0212-splitdf2c65b9f8be

main
7.1 KiB135 linesraw
1"""GUI-FB3 item 2(b): an Auto start that picked DFlash2 and ended before the engine was ready is started once more with the MTP, in one quiet line. A start the person
2named DFlash2 for fails with the engine's own reason. A real process and a real pipe (a fake engine program), like serve/test_fatal_recovery.py."""
3import os
4import sys
5import tempfile
6import unittest
7from pathlib import Path
8from unittest import mock
9
10from serve.server import EngineDied, StrataEngine
11from strata_control import drafting
12
13FAKE = r'''
14import sys
15from pathlib import Path
16args = sys.argv[1:]
17Path(args[args.index('--starts') + 1]).open('a').write(' '.join(args) + '\n')
18if '--draft' in args and '--die-on-draft' in args:
19    sys.stderr.write('strata generate: --draft dflash2 drafts without the MTP layer and needs --draft-model PACK, --spec 2 or more and --mtp DIR (for DIR/draft_vocab.bin)\n')
20    sys.stderr.flush()
21    raise SystemExit(2)
22if '--die-short' in args and ('--draft' in args or '--mtp' in args):
23    sys.stderr.write('ggml_cuda_init: failed to initialize CUDA: no CUDA-capable device is detected'+chr(10))
24    sys.stderr.write('strata generate: layer split, CUDA1 expert cache: no room'+chr(10))
25    sys.stderr.flush()
26    raise SystemExit(1)
27if '--die-always' in args:
28    sys.stderr.write('strata generate: something else is wrong\n')
29    sys.stderr.flush()
30    raise SystemExit(2)
31print('READY 4096 stop', flush=True)
32for line in sys.stdin:
33    if line.startswith('QUIT'):
34        break
35'''
36
37
38class AutoRetriesWithTheMtp(unittest.TestCase):
39    def setUp(self):
40        self.tmp = tempfile.TemporaryDirectory()
41        self.addCleanup(self.tmp.cleanup)
42        self.root = Path(self.tmp.name)
43        py = self.root / "native.py"
44        if os.name == "nt":
45            py.write_text(FAKE)
46            self.script = self.root / "native.cmd"
47            self.script.write_text(f'@"{sys.executable}" "{py}" %*\r\n')
48        else:
49            self.script = py
50            py.write_text("#!" + sys.executable + "\n" + FAKE)
51            py.chmod(0o700)
52        self.starts = self.root / "starts.txt"
53        self.log = str(self.root / "engine.log")
54        self.first = ["--pack", "/d/packs/g", "--starts", str(self.starts), "--die-on-draft", "--draft", "dflash2", "--draft-model", "/p", "--draft-fallback", "none",
55                      "--mtp-draft-vocab", "/d/mtp/rt/draft_vocab.bin"]
56        self.mtp = ["--pack", "/d/packs/g", "--starts", str(self.starts), "--die-on-draft", "--mtp", "/d/mtp/rt", "--spec", "4"]
57        drafting.take_note()
58        self.addCleanup(drafting.take_note)
59
60    def started(self):
61        return self.starts.read_text().splitlines() if self.starts.exists() else []
62
63    def test_auto_that_picked_dflash2_starts_again_with_the_mtp_and_says_so_quietly(self):
64        engine = StrataEngine(str(self.script), self.first, log=self.log, retry_args=self.mtp)
65        self.addCleanup(engine.close)
66        self.assertTrue(engine.alive())
67        starts = self.started()
68        self.assertEqual(len(starts), 2, "one start with DFlash2, one more with the MTP, never a third")
69        self.assertIn("--draft dflash2", starts[0])
70        self.assertNotIn("--draft", starts[1])
71        self.assertEqual(engine.spawn[1], self.mtp, "a later restart of this engine uses the arguments that worked")
72        self.assertEqual(engine.info["drafting"], "mtp")
73        self.assertEqual(engine.info["drafting_note"], "Drafting: MTP (DFlash2 didn't start)", "the one quiet line the Models screen shows")
74        text = Path(self.log).read_text(encoding="utf-8")
75        self.assertIn("starting it again with the MTP", text)
76        self.assertIn("needs --draft-model PACK", text, "the engine's own reason stays in the log")
77
78    def test_a_start_with_no_retry_arguments_fails_with_the_engines_reason(self):
79        """Named by the person (or nothing to retry with): a real error that says why."""
80        with self.assertRaises(EngineDied) as caught:
81            StrataEngine(str(self.script), self.first, log=self.log)
82        self.assertIn("exited before it was ready", str(caught.exception))
83        self.assertIn("needs --draft-model PACK", str(caught.exception))
84        self.assertEqual(len(self.started()), 1)
85
86    def test_the_retry_is_one_start_only(self):
87        both = [*self.first, "--die-always"]
88        retry = [*self.mtp, "--die-always"]
89        with self.assertRaises(EngineDied) as caught:
90            StrataEngine(str(self.script), both, log=self.log, retry_args=retry)
91        self.assertIn("something else is wrong", str(caught.exception), "the second start's reason, not the first one's")
92        self.assertEqual(len(self.started()), 2)
93
94    def test_a_start_that_works_is_not_started_twice(self):
95        ok = ["--pack", "/d/packs/g", "--starts", str(self.starts)]
96        engine = StrataEngine(str(self.script), ok, log=self.log, retry_args=self.mtp)
97        self.addCleanup(engine.close)
98        self.assertEqual(len(self.started()), 1)
99        self.assertEqual(engine.info.get("drafting_note", ""), "")
100
101    def test_a_start_that_ran_out_of_memory_is_repeated_with_no_drafter_never_with_the_mtp(self):
102        """2 small cards on a layer split: the engine says a card has no room.  The old retry swapped DFlash2 for the MTP, which takes MORE memory from a card that is short."""
103        first = [a for a in self.first if a != "--die-on-draft"] + ["--die-short"]
104        mtp = [a for a in self.mtp if a != "--die-on-draft"] + ["--die-short"]
105        nodraft = ["--pack", "/d/packs/g", "--starts", str(self.starts), "--die-short"]
106        engine = StrataEngine(str(self.script), first, log=self.log, retry_args=mtp, retry_vram_args=nodraft)
107        self.addCleanup(engine.close)
108        self.assertTrue(engine.alive())
109        starts = self.started()
110        self.assertEqual(len(starts), 2, "one start with DFlash2, one without a drafter, never the MTP in between")
111        self.assertIn("--draft dflash2", starts[0])
112        self.assertNotIn("--mtp", starts[1])
113        self.assertNotIn("--draft", starts[1])
114        self.assertEqual(engine.spawn[1], nodraft)
115        self.assertEqual(engine.info["drafting"], "off")
116        self.assertEqual(engine.info["drafting_note"], "Drafting: off (the cards were short of memory)")
117        text = Path(self.log).read_text(encoding="utf-8")
118        self.assertIn("short of memory", text)
119        self.assertIn("expert cache: no room", text, "the engine's own reason stays in the log")
120
121    def test_a_memory_stop_without_a_no_drafter_retry_fails_with_the_engines_reason_and_does_not_try_the_mtp(self):
122        first = [a for a in self.first if a != "--die-on-draft"] + ["--die-short"]
123        with self.assertRaises(EngineDied) as caught:
124            StrataEngine(str(self.script), first, log=self.log, retry_args=[*self.mtp, "--die-short"])
125        self.assertIn("expert cache: no room", str(caught.exception))
126        self.assertEqual(len(self.started()), 1)
127
128    def test_the_same_arguments_are_not_a_retry(self):
129        with self.assertRaises(EngineDied):
130            StrataEngine(str(self.script), self.first, log=self.log, retry_args=list(self.first))
131        self.assertEqual(len(self.started()), 1)
132
133
134if __name__ == "__main__":
135    unittest.main()