okami.mom/strata

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

Niko1221Merge branch 'f-021-iA' into f-021-int34e7c5cb1

main
10.9 KiB192 linesraw
1"""The engine options a person set by hand survive setup, the app and every start (#3216 control vectors, #3215 and #3270
2--vram-reserve-mib).  strata_control/user_args.py is the rule; setup.carry_over, serve/server.py engine_args, serve/runconfig.py
3and strata_control/tune_apply.py use it.  Every outside effect is mocked (tools/test_setup_golden.py's install()).
4
5    python -m unittest tools.test_user_args
6"""
7from __future__ import annotations
8
9import contextlib
10import io
11import sys
12import unittest
13from pathlib import Path
14
15ROOT = Path(__file__).resolve().parents[1]
16sys.path.insert(0, str(ROOT))
17sys.path.insert(0, str(ROOT / "tools"))
18import setup  # noqa: E402
19import strata_mcp  # noqa: E402
20from serve import runconfig  # noqa: E402
21from serve.server import engine_args  # noqa: E402
22from strata_control import tune_apply, user_args  # noqa: E402
23from test_setup_golden import PROFILES, install  # noqa: E402
24
25CVEC = ["--control-vector-scaled", "C:/vectors/mine.gguf:1.0", "--control-vector-layer-range", "4", "44",
26        "--cvec-mode", "project", "--cvec-dir", "per-layer"]
27
28
29def value(args, flag):
30    return args[args.index(flag) + 1] if flag in args[:-1] else None
31
32
33class Rule(unittest.TestCase):
34    def test_groups_and_merge(self):
35        self.assertEqual(user_args.groups(["--a", "1", "--b", "--c", "x", "y"]), [["--a", "1"], ["--b"], ["--c", "x", "y"]])
36        self.assertEqual(user_args.merge(["--a", "1", "--b", "2", "--a", "3"], ["--a", "9", "--z"]), ["--b", "2", "--a", "9", "--z"])
37        self.assertEqual(user_args.merge(["--a", "1"], []), ["--a", "1"])
38
39    def test_capture_tells_hand_edits_from_what_was_written(self):
40        written = ["--max-context", "32768", "--vram-reserve-mib", "700", "--spec", "4"]
41        cfg = {"args": written + CVEC, "args_written": written}
42        self.assertEqual(user_args.capture(cfg), CVEC)
43        cfg = {"args": ["--max-context", "32768", "--vram-reserve-mib", "0", "--spec", "4"], "args_written": written}
44        self.assertEqual(user_args.capture(cfg), ["--vram-reserve-mib", "0"])
45        self.assertEqual(user_args.capture({**cfg, "args": written}), [])                  # nothing changed
46        # the context is setup's own choice: a hand edit of it is not taken as an override
47        self.assertEqual(user_args.capture({"args": ["--max-context", "1"], "args_written": ["--max-context", "2"]},
48                                           setup.SETUP_CHOICE_FLAGS), [])
49        # an option taken out of "args" by hand is the person's no more
50        self.assertEqual(user_args.capture({"args": written[:4], "args_written": written, "user_args": ["--spec", "2"]}), [])
51        self.assertEqual(user_args.capture({"args": written, "args_written": written, "user_args": ["--spec", "2"]}), ["--spec", "2"])   # user_args stands
52        # no copy of what was written: only user_args is known
53        self.assertEqual(user_args.capture({"args": CVEC, "user_args": ["--a", "1"]}), ["--a", "1"])
54
55
56class SetupRerun(unittest.TestCase):
57    """The app's Load runs setup again whenever the context, KV type or pictures differ from the installed ones."""
58
59    def setUp(self):
60        self.ram, self.found = PROFILES["64GB-1x32GB"]
61
62    def run_setup(self, *extra, configs=()):
63        return install(self.ram, self.found, ["--family", "qwen", "--model", "Q2_0", "--no-start", *extra], configs=configs)
64
65    def first(self):
66        code, out, first, _ = self.run_setup("--context", "65536")
67        self.assertEqual(code, 0, out)
68        return first
69
70    def test_3216_control_vector_arguments_survive_a_load(self):
71        first = self.first()
72        old = {**first, "args": first["args"] + CVEC, "args_written": first["args"]}      # typed into the JSON editor
73        code, out, cfg, _ = self.run_setup("--context", "131072", configs=[("strata-q2_0.json", old)])
74        self.assertEqual(code, 0, out)
75        a = cfg["args"]
76        self.assertEqual(a[a.index("--control-vector-scaled"):][:9], CVEC)
77        self.assertEqual(value(a, "--max-context"), "131072")                              # setup's own choice still applies
78        self.assertEqual(cfg["user_args"], CVEC)
79        # the second Load: the options are in user_args, so they stay
80        code, out, again, _ = self.run_setup("--context", "65536", configs=[("strata-q2_0.json", cfg)])
81        self.assertEqual(again["user_args"], CVEC)
82        self.assertEqual(value(again["args"], "--cvec-mode"), "project")
83
84    def test_3216_a_config_without_args_written_keeps_them_too(self):
85        first = self.first()
86        old = {**first, "args": first["args"] + CVEC}                                      # 0.2.0-beta: no copy of what was written
87        code, out, cfg, _ = self.run_setup("--context", "131072", configs=[("strata-q2_0.json", old)])
88        self.assertEqual(code, 0, out)
89        self.assertEqual(cfg["user_args"], CVEC)
90        self.assertEqual(cfg["args"][-len(CVEC):], CVEC)
91
92    def test_3215_a_reserve_of_zero_is_kept(self):
93        first = self.first()
94        written = list(first["args"])
95        edited = [*written]
96        edited[edited.index("--vram-reserve-mib") + 1] = "0"
97        for old in ({**first, "args": edited, "args_written": written},                    # with the copy of what was written
98                    {**first, "args": edited}):                                           # a 0.2.0-beta file
99            code, out, cfg, _ = self.run_setup("--context", "131072", configs=[("strata-q2_0.json", old)])
100            self.assertEqual(code, 0, out)
101            self.assertEqual(value(cfg["args"], "--vram-reserve-mib"), "0")
102            self.assertEqual(cfg["user_args"], ["--vram-reserve-mib", "0"])
103
104    def test_3270_a_lowered_reserve_is_not_reset(self):
105        first = self.first()
106        written = list(first["args"])
107        for mib in ("1024", "256"):
108            edited = [*written]
109            edited[edited.index("--vram-reserve-mib") + 1] = mib
110            code, out, cfg, _ = self.run_setup("--context", "131072", configs=[("strata-q2_0.json", {**first, "args": edited, "args_written": written})])
111            self.assertEqual(value(cfg["args"], "--vram-reserve-mib"), mib)
112
113    def test_the_vision_default_is_not_taken_for_a_choice(self):
114        first = self.first()                                                               # pictures on the GPU write 700 themselves
115        self.assertEqual(value(first["args"], "--vram-reserve-mib"), "700")
116        code, out, cfg, _ = self.run_setup("--context", "131072", "--vision", "cpu", configs=[("strata-q2_0.json", first)])
117        self.assertNotIn("user_args", cfg)
118        self.assertNotIn("--vram-reserve-mib", cfg["args"])                                # the CPU reader needs no reserve
119
120    def test_an_option_given_on_the_command_line_wins_and_is_kept(self):
121        first = self.first()
122        old = {**first, "args": first["args"] + ["--vram-reserve-mib", "0"], "user_args": ["--vram-reserve-mib", "0"]}
123        code, out, cfg, _ = self.run_setup("--context", "131072", "--vram-reserve-mib", "256", configs=[("strata-q2_0.json", old)])
124        self.assertEqual(code, 0, out)
125        self.assertEqual(cfg["user_args"], ["--vram-reserve-mib", "256"])
126        self.assertEqual(value(cfg["args"], "--vram-reserve-mib"), "256")
127
128    def test_a_first_install_with_a_reserve_records_it(self):
129        code, out, cfg, _ = self.run_setup("--vram-reserve-mib", "300")
130        self.assertEqual(cfg["user_args"], ["--vram-reserve-mib", "300"])
131
132    def test_setup_writes_args_written_and_a_rerun_with_no_edit_makes_no_user_args(self):
133        first = self.first()
134        code, out, cfg, _ = self.run_setup("--context", "131072", configs=[("strata-q2_0.json", {**first, "args_written": first["args"]})])
135        self.assertNotIn("user_args", cfg)
136
137
138class Start(unittest.TestCase):
139    def test_every_start_puts_the_persons_options_over_the_generated_ones(self):
140        cfg = {"args": ["--pack", "/p", "--vram-reserve-mib", "1536", "--spec", "4"],
141               "user_args": ["--vram-reserve-mib", "0"] + CVEC}
142        with contextlib.redirect_stdout(io.StringIO()):
143            args = engine_args(cfg)
144        self.assertEqual(value(args, "--vram-reserve-mib"), "0")
145        self.assertEqual(args.count("--vram-reserve-mib"), 1)
146        self.assertEqual(args[args.index("--control-vector-scaled"):][:9], CVEC)
147        self.assertEqual(cfg["args"][3], "1536")                                           # the config in memory is untouched
148
149    def test_the_settings_page_records_the_reserve_as_the_persons(self):
150        cfg = {"args": ["--pack", "/p", "--vram-reserve-mib", "1536"], "args_written": ["--pack", "/p", "--vram-reserve-mib", "1536"]}
151        new, changed = runconfig.apply(cfg, {"vram_reserve_mib": 0})
152        self.assertEqual(changed, ["vram_reserve_mib"])
153        self.assertEqual(new["user_args"], ["--vram-reserve-mib", "0"])
154        self.assertEqual(new["args_written"], new["args"])
155        back, _ = runconfig.apply(new, {"vram_reserve_mib": None})
156        self.assertNotIn("user_args", back)
157        self.assertNotIn("--vram-reserve-mib", back["args"])
158
159    def test_a_tuning_replaces_only_the_flags_it_sets(self):
160        cfg = {"args": ["--pack", "/p", "--vram-reserve-mib", "0", *CVEC], "args_written": ["--pack", "/p"],
161               "user_args": ["--vram-reserve-mib", "0", *CVEC]}
162        out = tune_apply.apply_args_to_config(cfg, ["--vram-reserve-mib", "512", "--pcie-frac", "0.5"])
163        self.assertEqual(value(out["args"], "--vram-reserve-mib"), "512")
164        self.assertEqual(out["user_args"], CVEC)                                           # the tuned flag is no longer the old pin
165        self.assertEqual(out["args_written"], out["args"])
166
167
168class DisplayCards(unittest.TestCase):
169    """The app keeps the safety reserve for a card that drives a display and less on one that does not (#3215)."""
170
171    def test_nvidia_by_nvidia_smi(self):
172        gpus = [{"vendor": "nvidia", "index": 0}, {"vendor": "nvidia", "index": 1}, {"vendor": "nvidia", "index": 2}]
173        strata_mcp.mark_display(gpus, {0: False, 1: True}, [])
174        self.assertIs(gpus[0]["drives_display"], False)
175        self.assertIs(gpus[1]["drives_display"], True)
176        self.assertNotIn("drives_display", gpus[2])                                        # not told: unknown
177
178    def test_amd_by_the_windows_adapter_with_a_desktop(self):
179        amd = {"vendor": "amd", "index": 1, "pci_id": 0x744C}
180        gpus = [{"vendor": "amd", "index": 0, "pci_id": 0x164E}, amd]
181        strata_mcp.mark_display(gpus, {}, ["PCI\\VEN_1002&DEV_164E&SUBSYS_1"])
182        self.assertIs(gpus[0]["drives_display"], True)
183        self.assertIs(amd["drives_display"], False)
184        twins = [{"vendor": "amd", "pci_id": 0x744C}, {"vendor": "amd", "pci_id": 0x744C}]
185        strata_mcp.mark_display(twins, {}, ["PCI\\VEN_1002&DEV_744C"])
186        self.assertNotIn("drives_display", twins[0])                                       # two of one model: not told apart
187        strata_mcp.mark_display(gpus[:1] + [{"vendor": "amd", "pci_id": 0x1}], {}, [])
188        self.assertTrue(True)
189
190
191if __name__ == "__main__":
192    unittest.main()