okami.mom/strata

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

Niko1221Fit checks and slot sizing count the draft layer's ~1 GB of graphics memory while MTP is on671f4573d

main
4.4 KiB102 linesraw
1"""Tests for setup.py's low-RAM messages and the paging question (#250): the one-GPU warning says why and shows the
2RAM math, and an explicit --low-ram off installs with --yes instead of stopping at "Install it anyway?" (default
3n).  Mocked input - no GPU, no downloads.
4
5    python -m unittest tools.test_setup_lowram
6"""
7from __future__ import annotations
8
9import contextlib
10import io
11import sys
12import unittest
13from pathlib import Path
14from unittest import mock
15
16ROOT = Path(__file__).resolve().parents[1]
17sys.path.insert(0, str(ROOT))
18import setup  # noqa: E402
19
20
21def run(fn, *args, stdin=None):
22    """-> (exit code or None, printed text); `stdin` answers input() (None: never asked)."""
23    out = io.StringIO()
24    asked = []
25
26    def fake_input(prompt=""):
27        asked.append(prompt)
28        if stdin is None:
29            raise AssertionError(f"asked {prompt!r} with --yes")
30        return stdin
31
32    code = None
33    with contextlib.redirect_stdout(out), mock.patch("builtins.input", fake_input):
34        try:
35            fn(*args)
36        except SystemExit as e:
37            code = e.code
38    return code, out.getvalue(), asked
39
40
41class OneGpuWhy(unittest.TestCase):
42    def test_auto_shows_the_ram_math(self):
43        lines = setup.low_ram_one_gpu_why("IQ3_XXS", 46, "auto")       # the #250 report: 46 GB, IQ3_XXS
44        text = "\n".join(lines)
45        arena = setup.MODELS["IQ3_XXS"]["arena_gb"]
46        need = arena + setup.LOW_RAM_HEADROOM_GB
47        self.assertIn(f"{arena:.0f} + {setup.LOW_RAM_HEADROOM_GB} = {need:.0f} GB", text)
48        self.assertIn("this PC has 46 GB", text)
49        self.assertIn("layer split", text)                            # why one GPU is recommended
50        self.assertIn("--gpus 0,1", text)                             # #364 #384: and the way to use them all
51        self.assertIn("OS file cache", text)
52        self.assertNotIn("--low-ram off", text)                       # (that pages the experts: not the way)
53
54    def test_an_explicit_choice_says_so(self):
55        text = "\n".join(setup.low_ram_one_gpu_why("Q2_0", 64, "resident"))
56        self.assertIn("you chose the low-RAM mode (--low-ram resident)", text)
57
58
59class PagingQuestion(unittest.TestCase):
60    MODEL, RAM = "IQ3_XXS", 46                                       # 60 GB wanted, 46 here
61
62    def test_yes_with_low_ram_off_installs(self):
63        code, out, asked = run(setup.confirm_paging, self.MODEL, self.RAM, "off", True)
64        self.assertIsNone(code, out)
65        self.assertEqual(asked, [])
66        self.assertIn("installing IQ3_XXS with 46 GB of RAM, as you chose (--low-ram off)", out)
67
68    def test_yes_with_auto_still_stops(self):
69        code, out, asked = run(setup.confirm_paging, self.MODEL, self.RAM, "auto", True)
70        self.assertEqual(code, 1)
71        self.assertIn("IQ3_XXS needs about 60 GB of RAM; this PC has 46 GB", out)
72        self.assertIn("--low-ram off --yes", out)                    # the way to insist
73
74    def test_interactive(self):
75        for choice, answer, want in (("off", "", None), ("auto", "", 1), ("auto", "y", None), ("off", "n", 1)):
76            with self.subTest(choice=choice, answer=answer):
77                code, out, asked = run(setup.confirm_paging, self.MODEL, self.RAM, choice, False, stdin=answer)
78                self.assertEqual(code, want, out)
79                self.assertEqual(len(asked), 1)
80                self.assertIn("[y]" if choice == "off" else "[n]", asked[0])
81
82
83class DraftLayerCountsInTheFit(unittest.TestCase):
84    """The draft layer (MTP) takes about 1 GB of graphics memory out of the expert cache: every fit check counts it while it is on."""
85
86    def test_the_card_holds_one_gb_less_with_mtp_on(self):
87        on = setup.low_ram_gpu_gb("IQ3_XXS", 12.0)
88        off = setup.low_ram_gpu_gb("IQ3_XXS", 12.0, mtp=False)
89        self.assertAlmostEqual(off - on, setup.MTP_VRAM_GB)
90        self.assertEqual(setup.MTP_VRAM_GB, 1.0)
91        self.assertAlmostEqual(setup.low_ram_gpu_share("IQ3_XXS", 12.0, mtp=False) - setup.low_ram_gpu_share("IQ3_XXS", 12.0),
92                               1.0 / setup.MODELS["IQ3_XXS"]["arena_gb"])
93
94    def test_the_edge_of_what_fits_moves_by_the_draft_layer(self):
95        arena = setup.MODELS["IQ1_M"]["arena_gb"]
96        ram = arena + 6 - 7.0 + 0.5            # a card of 12 GB holds 7 GB beside the dense part; the draft layer needs one of them
97        self.assertFalse(setup.low_ram_fits("IQ1_M", ram, 12.0))
98        self.assertTrue(setup.low_ram_fits("IQ1_M", ram, 12.0, mtp=False))
99
100
101if __name__ == "__main__":
102    unittest.main()