okami.mom/strata

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

Niko1221Merge f-ds-out ac306afb2 into f-own-intaff3a132f

main
8.8 KiB160 linesraw
1"""Linux on ARM64 (the NVIDIA DGX Spark, 0.2.7.1): the platform name, the CUDA engine package for it, and the release tools that make
2the Terminal and the engine packages for it. Nothing here needs an ARM machine; tests/terminal/test_release_matrix.py holds the feed
3selection rows."""
4from __future__ import annotations
5
6import re
7import sys
8import unittest
9from pathlib import Path
10from unittest import mock
11
12ROOT = Path(__file__).resolve().parents[2]
13sys.path.insert(0, str(ROOT))
14sys.path.insert(0, str(ROOT / "packaging"))
15
16from strata_control import engine_component as ec  # noqa: E402
17from strata_control import feed as feedmod  # noqa: E402
18from strata_control import targets  # noqa: E402
19import feed_tool  # noqa: E402
20import lock_wheels  # noqa: E402
21import package_terminal  # noqa: E402
22
23
24def host(system: str, machine: str) -> str:
25    # the name and the architecture come from strata_control.sysinfo: on Windows from sys.platform and the environment (no WMI, #4975),
26    # elsewhere from the platform module
27    win = system == "Windows"
28    env = {"PROCESSOR_ARCHITECTURE": machine, "PROCESSOR_ARCHITEW6432": ""} if win else {}
29    with mock.patch.object(targets.platform, "system", return_value=system), mock.patch.object(targets.platform, "machine", return_value=machine), \
30            mock.patch.object(sys, "platform", "win32" if win else ("darwin" if system == "Darwin" else "linux")), \
31            mock.patch.dict("os.environ", env):
32        return targets.host_platform()
33
34
35class PlatformName(unittest.TestCase):
36    def test_linux_arm64_is_linux_aarch64_and_nothing_else_changed(self):
37        self.assertEqual(host("Linux", "aarch64"), "linux-aarch64")
38        self.assertEqual(host("Linux", "arm64"), "linux-aarch64")
39        self.assertEqual(host("Linux", "x86_64"), "linux-x86_64")
40        self.assertEqual(host("Windows", "AMD64"), "windows-x86_64")
41        self.assertEqual(host("Darwin", "arm64"), "macos-arm64")        # a Mac keeps its name
42        self.assertEqual(host("Darwin", "x86_64"), "macos-x86_64")
43
44    def test_cuda_is_committed_to_on_linux_aarch64_and_no_other_backend_is(self):
45        self.assertTrue(targets.backend_supported("cuda", "linux-aarch64")[0])
46        for backend in ("hip", "sycl", "metal", "cpu"):
47            self.assertFalse(targets.backend_supported(backend, "linux-aarch64")[0], backend)
48        # the rows that were there are still there
49        self.assertTrue(targets.backend_supported("cuda", "linux-x86_64")[0])
50        self.assertTrue(targets.backend_supported("hip", "linux-x86_64")[0])
51        self.assertTrue(targets.backend_supported("metal", "macos-arm64")[0])
52
53    def test_the_feed_knows_the_platform(self):
54        self.assertIn("linux-aarch64", feedmod.PLATFORMS)
55        self.assertEqual(feedmod.TAURI_TARGETS["linux-aarch64"], "linux-aarch64")
56        self.assertEqual(feedmod.PLATFORMS[:2], ("windows-x86_64", "linux-x86_64"))
57
58
59class GB10Cards(unittest.TestCase):
60    def test_sm_121_code_covers_a_gb10_and_only_a_12_x_card(self):
61        archs = ("sm_121", "compute_121")
62        self.assertTrue(ec.arch_covers(archs, ["sm_121"]))
63        self.assertFalse(ec.arch_covers(archs, ["sm_120"]))      # machine code is for the card's own minor or a newer one; PTX needs >= 121
64        self.assertFalse(ec.arch_covers(archs, ["sm_86"]))
65        self.assertTrue(ec.arch_native(archs, ["sm_121"]))
66
67
68class ReleaseTools(unittest.TestCase):
69    def test_the_terminal_has_a_pinned_interpreter_for_linux_aarch64(self):
70        pin = package_terminal.PYTHON_PINS["linux-aarch64"]
71        self.assertIn("aarch64-unknown-linux-gnu-install_only.tar.gz", pin["file"])
72        self.assertRegex(pin["sha256"], r"^[0-9a-f]{64}$")
73        self.assertNotEqual(pin["sha256"], package_terminal.PYTHON_PINS["linux-x86_64"]["sha256"])
74        self.assertTrue(pin["url"].startswith(package_terminal.PBS_BASE) and "%2B" in pin["url"])
75        self.assertEqual(pin["python"], "python/bin/python3")
76
77    def test_host_target_names_linux_arm64(self):
78        with mock.patch.object(package_terminal.sys, "platform", "linux"), mock.patch.object(package_terminal.platform, "machine", return_value="aarch64"):
79            self.assertEqual(package_terminal.host_target(), "linux-aarch64")
80        with mock.patch.object(package_terminal.sys, "platform", "linux"), mock.patch.object(package_terminal.platform, "machine", return_value="x86_64"):
81            self.assertEqual(package_terminal.host_target(), "linux-x86_64")
82
83    def test_the_runtime_lock_for_linux_aarch64_is_made_the_same_way_and_is_complete(self):
84        spec = lock_wheels.TARGETS["linux-aarch64"]
85        self.assertTrue(all(p.endswith("aarch64") for p in spec["platforms"]))
86        self.assertEqual(spec["env"], lock_wheels.TARGETS["linux-x86_64"]["env"])
87        lock = ROOT / "packaging" / "locks" / "runtime-linux-aarch64-cp312.txt"
88        self.assertTrue(lock.is_file(), "run packaging/lock_wheels.py --target linux-aarch64 on the Spark")
89        names = package_terminal.lock_names(lock)
90        x86 = package_terminal.lock_names(ROOT / "packaging" / "locks" / "runtime-linux-x86_64-cp312.txt")
91        self.assertEqual(set(names), set(x86), "the same packages as the x86_64 Linux lock")
92        text = lock.read_text(encoding="utf-8")
93        self.assertEqual(len(re.findall(r"^[A-Za-z0-9_.-]+==", text, re.M)), len(names))
94        hashed = re.findall(r"^[A-Za-z0-9_.-]+==\S+ \\\n(?:\s+--hash=sha256:[0-9a-f]{64}(?: \\)?\n?)+", text, re.M)
95        self.assertEqual(len(hashed), len(names), "every package of the lock names the SHA-256 of its wheel")
96
97    def test_pack_control_takes_the_platform(self):
98        parser_choices = feedmod.PLATFORMS
99        self.assertIn("linux-aarch64", parser_choices)
100        # the installers' one-line manifest wants tar.gz for every non-Windows platform
101        self.assertTrue(feed_tool.feedmod.PLATFORMS is feedmod.PLATFORMS)
102
103    def test_install_sh_installs_on_linux_arm64_and_says_what_the_engine_is_for(self):
104        text = (ROOT / "packaging" / "install.sh").read_text(encoding="utf-8")
105        self.assertIn("Linux-aarch64|Linux-arm64) platform=linux-aarch64", text)
106        self.assertNotIn("has no engine for ARM Linux", text)
107        self.assertIn("GB10", text)
108        self.assertIn("Linux-x86_64|Linux-amd64) platform=linux-x86_64", text)        # unchanged
109        self.assertIn("Darwin-x86_64) die", text)
110
111    def test_the_spark_build_script_is_lf_and_names_the_platform(self):
112        raw = (ROOT / "spark" / "build_engine.sh").read_bytes()
113        self.assertNotIn(b"\r", raw)
114        self.assertIn(b'linux-aarch64', raw)
115        self.assertIn(b"-DSTRATA_PORTABLE=ON", raw)
116
117
118class GB10Probes(unittest.TestCase):
119    """nvidia-smi gives a GB10's memory.total as [N/A]: the probes the Terminal reads must neither drop the card nor invent a size."""
120    LINE = "0, NVIDIA GB10, GPU-0a1b2c3d-0000-1111-2222-333344445555, [N/A], 580.173.02\n"
121
122    def cards(self, machine):
123        from strata_control import doctor
124        done = mock.Mock(returncode=0, stdout=self.LINE)
125        with mock.patch.object(doctor.shutil, "which", return_value="/usr/bin/nvidia-smi"), \
126                mock.patch.object(doctor.subprocess, "run", return_value=done), \
127                mock.patch.object(doctor.platform, "machine", return_value=machine),                 mock.patch.object(sys, "platform", "linux"):          # (sysinfo answers from the platform module only off Windows)
128            return doctor._nvidia_cards()
129
130    def test_doctor_lists_a_gb10_with_no_memory_figure(self):
131        cards, why = self.cards("aarch64")
132        self.assertIsNone(why)
133        self.assertEqual(cards[0]["name"], "NVIDIA GB10")
134        self.assertIsNone(cards[0]["memory_mib"])
135
136    def test_doctor_on_x86_still_refuses_a_card_without_a_memory_figure(self):
137        cards, why = self.cards("x86_64")
138        self.assertIsNone(cards)
139        self.assertIn("unrecognized", why)
140
141    def test_the_launch_probe_counts_a_gb10_as_unified_memory(self):
142        sys.path.insert(0, str(ROOT / "tools"))
143        import strata_mcp
144        smi = "0, NVIDIA GB10, [N/A], 12.1, 580.173.02\n"
145        with mock.patch.object(strata_mcp, "run_quiet", return_value=smi), \
146                mock.patch.object(strata_mcp, "own_ram_gb", return_value=121.6), \
147                mock.patch.object(strata_mcp.platform, "machine", return_value="aarch64"):
148            found = strata_mcp.own_nvidia_gpus()
149        self.assertEqual(len(found), 1)
150        self.assertTrue(found[0]["uma"])
151        self.assertEqual(found[0]["arch"], "121")
152        self.assertAlmostEqual(found[0]["vram_gb"], 115.6, places=1)
153        self.assertEqual(found[0]["dedicated_gb"], 0.0)
154        with mock.patch.object(strata_mcp, "run_quiet", return_value=smi), \
155                mock.patch.object(strata_mcp.platform, "machine", return_value="x86_64"):
156            self.assertEqual(strata_mcp.own_nvidia_gpus(), [])
157
158
159if __name__ == "__main__":
160    unittest.main()