okami.mom/strata

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

Niko1221Merge f-0213-life (final 576d06553) into f-0213-int66331f9bc

main
2.8 KiB59 linesraw
1"""Which OS and backend combinations the project commits to, and what this machine is.
2
3Pure and tiny on purpose: the engine resolver on the start path (engine_component.py) needs these answers, and the
4start path must not import the component fetcher (components.py) to get them.
5"""
6
7from __future__ import annotations
8
9import platform  # noqa: F401 - kept: tests replace targets.platform's functions
10
11from strata_control import sysinfo
12
13
14class ComponentError(RuntimeError):
15    """A component could not be used. The message names the reason and nothing was changed."""
16
17    def __init__(self, message: str, code: int = 6) -> None:
18        super().__init__(message)
19        self.code = code
20
21
22#: The OS × backend families the handoff commits to. A component may only be activated where its
23#: family is listed here, so an unqualified combination is refused rather than attempted: the packet
24#: is explicit that Windows SYCL and Intel Macs are not supported rather than merely untested.
25BACKEND_MATRIX: dict[str, tuple[str, ...]] = {
26    "metal": ("macos-arm64",),
27    "cuda": ("windows-x86_64", "linux-x86_64", "linux-aarch64"),   # linux-aarch64: the NVIDIA DGX Spark (GB10, sm_121) only
28    "hip": ("windows-x86_64", "linux-x86_64"),
29    "sycl": ("linux-x86_64",),
30    "cpu": ("macos-arm64", "macos-x86_64", "windows-x86_64", "linux-x86_64"),
31}
32
33def host_platform() -> str:
34    """This machine as an `os-arch` key, matching `BACKEND_MATRIX`."""
35    # sysinfo, not platform: on Windows `platform.system()` and `.machine()` query WMI with no timeout, and a stuck winmgmt
36    # froze every command that resolves the engine (#4975)
37    system = {"Darwin": "macos", "Linux": "linux", "Windows": "windows"}.get(sysinfo.system())
38    if system is None:
39        raise ComponentError(f"unsupported operating system: {sysinfo.system()}")
40    machine = sysinfo.machine().lower()
41    arch = {"arm64": "arm64", "aarch64": "arm64",
42            "x86_64": "x86_64", "amd64": "x86_64"}.get(machine, machine)
43    if system == "linux" and arch == "arm64":
44        return "linux-aarch64"         # Linux names its ARM64 machines as uname does (the Tauri target and the Python build too); a Mac stays macos-arm64
45    return f"{system}-{arch}"
46
47
48def backend_supported(backend: str, target: str | None = None) -> tuple[bool, str]:
49    """Whether a backend family is committed to on a platform, with the reason if not."""
50    target = target or host_platform()
51    if backend not in BACKEND_MATRIX:
52        return False, f"unknown backend family {backend!r}"
53    platforms = BACKEND_MATRIX[backend]
54    if target in platforms:
55        return True, ""
56    return False, (f"{backend} is not a supported combination on {target}; it is supported on "
57                   f"{', '.join(platforms)}. Unsupported combinations are not attempted.")
58
59