okami.mom/strata

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

Niko1221Merge main dbb2d168c (picture-reader fix, GGML_NO_BACKTRACE) into f-own-int2503b206b

main
13.5 KiB221 linesraw
1"""A launch onto a card another program has filled: the picture reader's out-of-memory lines are read as what they are (strata_control/reader_room.py),
2and the launch's error says so in plain words with the next step, not the last line of whatever a debugger printed.  Lane 10 (V100 box), 11-10:
3"Python Exception <class 'gdb.error'>: Couldn't get registers: No such process".
4"""
5import json
6import tempfile
7import unittest
8from pathlib import Path
9from unittest import mock
10
11from strata_cli.launch import runner as R
12from strata_control import reader_room, split_engine
13
14ALLOC = "ggml_backend_cuda_buffer_type_alloc_buffer: allocating 865.48 MiB on device 0: cudaMalloc failed: out of memory"
15BUFFER = "alloc_tensor_range: failed to allocate CUDA0 buffer of size 907524736"
16ASSERT = "/data/split-pkg/llama.cpp/ggml/src/ggml-backend.cpp:188: GGML_ASSERT(buffer) failed"
17GDB = ["[New LWP 477206]", "❌️ Couldn't get registers: No such process.", "Python Exception <class 'gdb.error'>: Couldn't get registers: No such process."]
18MAC = ["WARNING: Using native backtrace. Set GGML_BACKTRACE_LLDB for more info.", "3   strata-vision   0x0000000102a4c9b8 ggml_abort + 132",
19       "4   strata-vision   0x0000000102a2f1c0 ggml_backend_alloc_ctx_tensors_from_buft + 220"]
20
21
22class Find(unittest.TestCase):
23    def test_the_three_lines_of_the_v100_launch(self):
24        room = reader_room.find([ALLOC, BUFFER, ASSERT])
25        self.assertEqual(room.device, 0)
26        self.assertAlmostEqual(room.need_mib, 865.48, places=2)
27
28    def test_the_buffer_line_alone_gives_the_size_in_mib(self):
29        room = reader_room.find([BUFFER])
30        self.assertEqual(room.device, 0)
31        self.assertAlmostEqual(room.need_mib, 865.48, places=1)
32
33    def test_amd_buffer_names(self):
34        self.assertEqual(reader_room.find(["failed to allocate ROCm2 buffer of size 1048576"]).device, 2)
35
36    def test_the_text_may_come_as_one_string(self):
37        self.assertEqual(reader_room.find("\n".join([ALLOC, ASSERT])).device, 0)
38
39    def test_a_plain_out_of_memory_without_numbers_is_still_found(self):
40        room = reader_room.find(["CUDA error: cudaMalloc failed: out of memory"])
41        self.assertEqual((room.device, room.need_mib), (None, None))
42
43    def test_the_last_failed_allocation_is_the_one_reported(self):
44        later = "ggml_backend_cuda_buffer_type_alloc_buffer: allocating 12.00 MiB on device 1: cudaMalloc failed: out of memory"
45        room = reader_room.find([ALLOC, later])
46        self.assertEqual((room.device, room.need_mib), (1, 12.0))
47
48    def test_lines_that_are_not_about_memory_are_not_found(self):
49        for line in ("ggml_cuda_init: failed to initialize CUDA: no CUDA-capable device",       # the harmless one: the reader runs on the processor then
50                     "CUDA error: device kernel image is invalid", "strata generate: CUDA error: out of memory",
51                     "cudaMalloc failed: CUDA-capable device(s) is/are busy or unavailable", ""):
52            with self.subTest(line=line):
53                self.assertIsNone(reader_room.find([line]))
54        self.assertIsNone(reader_room.find([]))
55        self.assertIsNone(reader_room.find(None))
56
57    def test_the_readers_card_number_is_looked_up_in_the_list_it_was_shown(self):
58        self.assertEqual(reader_room.physical_card(0, "2,3"), 2)
59        self.assertEqual(reader_room.physical_card(1, [2, 3]), 3)
60        self.assertEqual(reader_room.physical_card(1, "2,3"), 3)
61        self.assertEqual(reader_room.physical_card(0, None), 0)          # it saw every card: its number is the card's
62        self.assertEqual(reader_room.physical_card(0, ""), 0)
63        self.assertIsNone(reader_room.physical_card(5, "2,3"))
64        self.assertIsNone(reader_room.physical_card(0, "GPU-8c3f"))      # a UUID: not a number we can print
65        self.assertIsNone(reader_room.physical_card(None, "0"))
66
67
68class Need(unittest.TestCase):
69    def test_the_whole_need_at_a_limit(self):
70        self.assertEqual(reader_room.reader_needs_mib(1024), 1536)
71        self.assertEqual(reader_room.reader_needs_mib(None), 1536)
72        self.assertEqual(reader_room.reader_needs_mib(300), 1536)                 # below the default limit it is not smaller: the warm-up picture is the same
73        self.assertAlmostEqual(reader_room.reader_needs_mib(16384), 3674)
74        self.assertAlmostEqual(reader_room.reader_needs_mib(0), 3674)             # no limit: the largest the Terminal offers
75        self.assertAlmostEqual(reader_room.reader_needs_mib(99999), 3674)
76        self.assertTrue(1536 < reader_room.reader_needs_mib(4096) < 3674)
77
78    def test_what_the_person_is_told_is_the_larger_of_the_failed_allocation_and_that(self):
79        self.assertEqual(reader_room.whole_need_mib(865.48, 1024), 1536)
80        self.assertEqual(reader_room.whole_need_mib(None, 1024), 1536)
81        self.assertEqual(reader_room.whole_need_mib(2600, 1024), 2600)
82        self.assertAlmostEqual(reader_room.whole_need_mib(865.48, 16384), 3674)
83
84    def test_in_the_sentence(self):
85        said, _ = reader_room.could_not_start(0, reader_room.whole_need_mib(865.48), 600)
86        self.assertEqual(said, "The picture reader could not start: graphics card 0 has no room for it (it needs about 1.5 GB; 0.6 GB are free).")
87
88
89class Words(unittest.TestCase):
90    """The owner's wording (11-10), exactly."""
91
92    def test_the_summary_line_when_the_processor_reads(self):
93        self.assertEqual(reader_room.on_processor(0, 865.48, 114),
94                         "Pictures are read on the processor, which is slower: graphics card 0 has no room for the picture reader "
95                         "(it needs about 0.8 GB; 0.1 GB are free). To read them on the card, close the other programs that use it and start again.")
96
97    def test_the_error_when_the_launch_ends(self):
98        said, nxt = reader_room.could_not_start(0, 865.48, 114)
99        self.assertEqual(said, "The picture reader could not start: graphics card 0 has no room for it (it needs about 0.8 GB; 0.1 GB are free).")
100        self.assertEqual(nxt, "Close the other programs that use the card and start again, or add --vision off to start without pictures.")
101
102    def test_what_is_not_known_is_left_out(self):
103        self.assertEqual(reader_room.could_not_start(2, 865.48, None)[0], "The picture reader could not start: graphics card 2 has no room for it (it needs about 0.8 GB).")
104        self.assertEqual(reader_room.could_not_start(2, None, 3000)[0], "The picture reader could not start: graphics card 2 has no room for it (2.9 GB are free).")
105        self.assertEqual(reader_room.could_not_start(None, None, None)[0], "The picture reader could not start: the graphics card has no room for it.")
106        self.assertEqual(reader_room.could_not_start(None, 865.48, 114)[0],
107                         "The picture reader could not start: the graphics card has no room for it (it needs about 0.8 GB; 0.1 GB are free).")
108        self.assertIn("the graphics card has no room for the picture reader.", reader_room.on_processor(None))
109
110    def test_the_summary_line_for_any_other_reason(self):
111        self.assertEqual(reader_room.not_started_on_processor(0, "/home/u/engine.log"),
112                         "Pictures are read on the processor, which is slower: the picture reader could not start on graphics card 0. "
113                         "The reason is in the engine's log: /home/u/engine.log.")
114        self.assertEqual(reader_room.not_started_on_processor(None, "/home/u/engine.log"),
115                         "Pictures are read on the processor, which is slower: the picture reader could not start. The reason is in the engine's log: /home/u/engine.log.")
116        self.assertEqual(reader_room.not_started_on_processor(2, None),
117                         "Pictures are read on the processor, which is slower: the picture reader could not start on graphics card 2.")
118        self.assertEqual(reader_room.not_started_on_processor(), "Pictures are read on the processor, which is slower: the picture reader could not start.")
119
120    def test_a_card_with_next_to_nothing_free_is_not_called_a_tenth(self):
121        self.assertIn("(less than 0.1 GB are free)", reader_room.on_processor(0, None, 10))
122        self.assertIn("(less than 0.1 GB are free)", reader_room.on_processor(0, None, 0))
123
124
125class LaunchSaysIt(unittest.TestCase):
126    """explain_engine_exit on the engine log a full card leaves, on every platform's version of it."""
127
128    def setUp(self):
129        self.free = [114]
130        patcher = mock.patch.object(split_engine, "probe_cards", lambda indexes=None: [split_engine.Card(0, "Tesla", 16384, 60, free_mib=self.free[0])] if self.free else [])
131        patcher.start()
132        self.addCleanup(patcher.stop)
133
134    def log(self, lines, cfg=None):
135        folder = tempfile.TemporaryDirectory()
136        self.addCleanup(folder.cleanup)
137        log = Path(folder.name) / "engine.log"
138        log.write_text("\n".join(lines) + "\n", encoding="utf-8")
139        path = Path(folder.name) / "run.json"
140        path.write_text(json.dumps({"log": str(log), **(cfg or {})}), encoding="utf-8")
141        return path, log
142
143    def explain(self, lines, cfg=None, code=1):
144        path, log = self.log(lines, cfg)
145        text, nxt = R.explain_engine_exit({"state": "failed", "error": "server_exited", "engine_exit_code": code}, path)
146        return text, nxt, log
147
148    NEW2 = "The picture reader could not start: graphics card 0 has no room for it (it needs about 1.5 GB; 0.1 GB are free)."      # 865 MiB failed; the reader needs 1,536 in all
149    STEP = "Close the other programs that use the card and start again, or add --vision off to start without pictures."
150
151    def test_linux_with_gdb_as_seen_on_the_v100_box(self):
152        text, nxt, log = self.explain(["loading the model"] + [ALLOC, BUFFER, ASSERT] + GDB)
153        self.assertEqual(text, self.NEW2)
154        self.assertTrue(nxt.startswith(self.STEP), nxt)
155        self.assertIn(str(log), nxt)
156        self.assertNotIn("gdb", text + nxt)
157        self.assertNotIn("Exception", text + nxt)
158
159    def test_linux_without_a_debugger_the_backtrace_frames_follow(self):
160        frames = ["/opt/strata/strata-vision(ggml_abort+0xd0) [0x55d7c4a1f0d0]", "/opt/strata/strata-vision(+0x3f1a2) [0x55d7c4a3f1a2]"]
161        text, nxt, _ = self.explain([ALLOC, BUFFER, ASSERT] + frames)
162        self.assertEqual(text, self.NEW2)
163        self.assertTrue(nxt.startswith(self.STEP))
164
165    def test_macos_style_frames_and_warnings(self):
166        text, nxt, _ = self.explain([ALLOC, BUFFER, ASSERT] + MAC)
167        self.assertEqual(text, self.NEW2)
168
169    def test_windows_the_assert_is_the_last_line_and_the_exit_code_is_3(self):
170        text, nxt, _ = self.explain([ALLOC, BUFFER, ASSERT], code=3)
171        self.assertEqual(text, self.NEW2)
172        self.assertNotIn("GGML_ASSERT", text + nxt)
173
174    def test_the_free_memory_is_left_out_when_it_cannot_be_read(self):
175        self.free.clear()
176        text, _, _ = self.explain([ALLOC, BUFFER, ASSERT] + GDB)
177        self.assertEqual(text, "The picture reader could not start: graphics card 0 has no room for it (it needs about 1.5 GB).")
178
179    def test_the_card_is_the_one_in_the_config_in_the_engines_order(self):
180        text, _, _ = self.explain([ALLOC.replace("device 0", "device 1"), ASSERT], cfg={"gpu": [2, 3], "gpu_order": "as_given"})
181        self.assertIn("graphics card 3 has no room", text)
182
183    def test_a_reader_on_its_own_card_names_that_card(self):
184        text, _, _ = self.explain([ALLOC, ASSERT], cfg={"gpu": [0, 1], "vision": {"cuda_device": 1}})
185        self.assertIn("graphics card 1 has no room", text)
186
187    def test_without_a_number_it_says_the_card(self):
188        for cfg in (None, {"gpu": [2, 3], "gpu_order": "as_given"}):
189            with self.subTest(cfg=cfg):
190                text, _, _ = self.explain(["CUDA error: cudaMalloc failed: out of memory", "GGML_ASSERT failed"], cfg=cfg)
191                self.assertEqual(text, "The picture reader could not start: the graphics card has no room for it (it needs about 1.5 GB).")       # no figure in the line: the reader's known need
192
193    def test_the_need_is_the_readers_whole_need_not_the_one_allocation_that_failed(self):
194        self.free[0] = 600
195        text, _, _ = self.explain([ALLOC, BUFFER, ASSERT] + GDB)             # 865 MiB failed, 600 MiB free
196        self.assertEqual(text, "The picture reader could not start: graphics card 0 has no room for it (it needs about 1.5 GB; 0.6 GB are free).")
197
198    def test_the_need_grows_with_the_readers_image_limit(self):
199        for limit, said in ((None, "1.5 GB"), (1024, "1.5 GB"), (4096, "1.9 GB"), (16384, "3.6 GB"), (0, "3.6 GB")):
200            with self.subTest(limit=limit):
201                cfg = {"vision": {"max_tokens": limit}} if limit is not None else None
202                text, _, _ = self.explain([ALLOC, BUFFER, ASSERT], cfg=cfg)
203                self.assertIn("(it needs about " + said + ";", text)
204
205    def test_a_bigger_failed_allocation_is_the_need(self):
206        text, _, _ = self.explain([ALLOC.replace("865.48 MiB", "2600.00 MiB"), ASSERT])
207        self.assertIn("(it needs about 2.5 GB;", text)
208
209    def test_the_engines_own_out_of_memory_later_in_the_log_wins(self):
210        text, nxt, _ = self.explain([ALLOC, BUFFER, "strata serve: the GPU has too little free VRAM for the prompt path: turn images off (setup: --vision no)"])
211        self.assertIn("too little free VRAM", text)
212        self.assertEqual(nxt.split(" The engine's log")[0], R.VRAM_STOP_NEXT)
213
214    def test_other_reader_trouble_is_not_called_a_full_card(self):
215        text, nxt, _ = self.explain(["ggml_cuda_init: failed to initialize CUDA: no CUDA-capable device"])
216        self.assertNotIn("no room", text)
217        self.assertIn("The engine stopped while it was loading the model", text)
218
219
220if __name__ == "__main__":
221    unittest.main()