Skip to content

Local UI to run and train LLMs and diffusion models. Supports GGUF, MLX, Qwen3.8, DeepSeek-V4.1, MiniMax-H3, Gemma 4, FLUX and more.

34.2 KB
Raw Blame
# SPDX-License-Identifier: AGPL-3.0-only
# Copyright 2026-present the Unsloth AI Inc. team. All rights reserved. See /studio/LICENSE.AGPL-3.0

"""Drift guards for gfx tables hand-copied across the installers; each copy is parsed and compared."""

import ast
import fnmatch
import importlib.util
import re
import sys
from pathlib import Path

import pytest


PACKAGE_ROOT = Path(__file__).resolve().parents[3]

_INSTALL_SH = PACKAGE_ROOT / "install.sh"
_INSTALL_PS1 = PACKAGE_ROOT / "install.ps1"
_SETUP_SH = PACKAGE_ROOT / "studio" / "setup.sh"
_SETUP_PS1 = PACKAGE_ROOT / "studio" / "setup.ps1"
_STACK_PY = PACKAGE_ROOT / "studio" / "install_python_stack.py"
_PREBUILT_PY = PACKAGE_ROOT / "studio" / "install_llama_prebuilt.py"
_SPOOF_PY = PACKAGE_ROOT / "tests" / "_zoo_rocm_spoof.py"


def _load_stack_module():
    spec = importlib.util.spec_from_file_location("studio_install_python_stack_parity", _STACK_PY)
    assert spec is not None and spec.loader is not None
    mod = importlib.util.module_from_spec(spec)
    sys.modules[spec.name] = mod
    spec.loader.exec_module(mod)
    return mod


stack_mod = _load_stack_module()


def _sh_function_body(source: str, name: str) -> str:
    """Return a POSIX-shell function body by brace matching; a local copy of
    _extract_sh_function_body so this file stands alone."""
    needle = f"{name}() {{"
    start = source.find(needle)
    assert start != -1, f"{name}() not found"
    depth = 0
    i = start + len(needle) - 1
    while i < len(source):
        if source[i] == "{":
            depth += 1
        elif source[i] == "}":
            depth -= 1
            if depth == 0:
                return source[start : i + 1]
        i += 1
    raise AssertionError(f"unterminated {name}()")


def _sh_case_block(source: str, subject: str) -> str:
    """Return the body of `case <subject> in ... esac` (first match)."""
    start = source.find(f"case {subject} in")
    assert start != -1, f"case {subject} in ... not found"
    end = source.find("esac", start)
    assert end != -1, f"unterminated case {subject}"
    return source[start:end]


def _ps_block(source: str, header: str, open_ch: str, close_ch: str) -> str:
    """Return the balanced `header <open> ... <close>` block from a PowerShell file."""
    start = source.find(header)
    assert start != -1, f"{header} not found"
    i = source.find(open_ch, start)
    assert i != -1
    depth = 0
    while i < len(source):
        if source[i] == open_ch:
            depth += 1
        elif source[i] == close_ch:
            depth -= 1
            if depth == 0:
                return source[start : i + 1]
        i += 1
    raise AssertionError(f"unterminated {header}")


def _strip_sh_comment(line: str) -> str:
    """Drop a trailing `# ...` comment. Safe here: no table line contains a '#'
    inside a pattern."""
    return line.split("#", 1)[0]


def _gfx_family_map_sh() -> dict[str, str]:
    body = _sh_function_body(
        _INSTALL_SH.read_text(encoding = "utf-8"), "_amd_arch_index_family_for_gfx"
    )
    out: dict[str, str] = {}
    for line in body.splitlines():
        m = re.match(r"\s*(gfx[^)]*)\)\s*echo\s+(\S+)\s*;;", _strip_sh_comment(line))
        if not m:
            continue
        for arch in m.group(1).split("|"):
            out[arch.strip()] = m.group(2).strip()
    return out


def _gfx_family_map_ps(path: Path) -> dict[str, str]:
    block = _ps_block(path.read_text(encoding = "utf-8"), "$archFamilyMap = @{", "{", "}")
    out: dict[str, str] = {}
    for line in block.splitlines():
        for m in re.finditer(r'"(gfx[0-9a-z]+)"\s*=\s*"([A-Za-z0-9-]+)"', _strip_sh_comment(line)):
            out[m.group(1)] = m.group(2)
    return out


def _gfx_family_maps() -> dict[str, dict[str, str]]:
    return {
        "studio/install_python_stack.py": dict(stack_mod._GFX_TO_AMD_INDEX_ARCH),
        "install.sh": _gfx_family_map_sh(),
        "install.ps1": _gfx_family_map_ps(_INSTALL_PS1),
        "studio/setup.ps1": _gfx_family_map_ps(_SETUP_PS1),
    }


class TestGfxIndexFamilyParity:
    """All four gfx -> AMD index family maps must agree, entry for entry."""

    def test_every_copy_is_non_empty(self):
        for where, table in _gfx_family_maps().items():
            assert (
                table
            ), f"{where}: parsed an empty gfx -> index family map (table moved or renamed?)"

    def test_all_copies_identical(self):
        maps = _gfx_family_maps()
        reference_name = "studio/install_python_stack.py"
        reference = maps[reference_name]
        for where, table in maps.items():
            if where == reference_name:
                continue
            missing = {k: v for k, v in reference.items() if k not in table}
            extra = {k: v for k, v in table.items() if k not in reference}
            wrong = {
                k: (v, reference[k])
                for k, v in table.items()
                if k in reference and v != reference[k]
            }
            assert (
                not missing
            ), f"{where} is missing {sorted(missing)} (present in {reference_name})"
            assert not extra, f"{where} has {sorted(extra)} that {reference_name} does not"
            assert not wrong, f"{where} maps {wrong} (value, expected)"

    def test_rdna2_family_present_everywhere(self):
        """#7277 added gfx1030-1036 to three files; install.sh followed later.
        Pin the whole RDNA2 range so the next family lands everywhere at once."""
        for where, table in _gfx_family_maps().items():
            for arch in (
                "gfx1030",
                "gfx1031",
                "gfx1032",
                "gfx1033",
                "gfx1034",
                "gfx1035",
                "gfx1036",
            ):
                assert table.get(arch) == "gfx103X-all", f"{where}: {arch} -> {table.get(arch)!r}"


class TestSupportedWheelArchList:
    """setup.ps1's $_rocmWheelArches decides whether a detected arch gets ROCm torch
    at all. An arch present in the family map but absent here silently installs
    CPU-only PyTorch (the 'not in supported arch list' report from r/unsloth)."""

    def test_wheel_arch_list_covers_every_mapped_arch(self):
        block = _ps_block(
            _SETUP_PS1.read_text(encoding = "utf-8"), "$_rocmWheelArches = @(", "(", ")"
        )
        listed = set(re.findall(r'"(gfx[0-9a-z]+)"', block))
        assert listed, "could not parse $_rocmWheelArches"
        mapped = set(stack_mod._GFX_TO_AMD_INDEX_ARCH)
        assert mapped - listed == set(), (
            f"studio/setup.ps1 $_rocmWheelArches is missing {sorted(mapped - listed)}: "
            "those arches map to an AMD index but would still fall back to CPU torch"
        )


# Copies are first-match-wins: shell case globs (case-sensitive), PowerShell -match (insensitive).
# Run every copy against the same real names instead of diffing patterns.
def _name_table_sh_function(source: str, name: str) -> list[tuple[list[str], str]]:
    body = _sh_function_body(source, name)
    rows: list[tuple[list[str], str]] = []
    for line in body.splitlines():
        m = re.match(r"\s*(\*.*?)\)\s*echo\s+(gfx[0-9a-z]+)\s*;;", _strip_sh_comment(line))
        if m:
            rows.append(([p.strip() for p in m.group(1).split("|")], m.group(2)))
    return rows


def _name_table_sh_case(source: str, subject: str, var: str) -> list[tuple[list[str], str]]:
    """A bare `case ... in` table that assigns to a variable rather than echoing."""
    block = _sh_case_block(source, subject)
    rows: list[tuple[list[str], str]] = []
    for line in block.splitlines():
        m = re.match(
            rf'\s*(\*.*?)\)\s*{re.escape(var)}="(gfx[0-9a-z]+)"\s*;;', _strip_sh_comment(line)
        )
        if m:
            rows.append(([p.strip() for p in m.group(1).split("|")], m.group(2)))
    return rows


def _name_table_ps(path: Path) -> list[tuple[str, str]]:
    block = _ps_block(path.read_text(encoding = "utf-8"), "$nameArchTable = @(", "(", ")")
    return re.findall(r'@\{\s*P\s*=\s*"([^"]+)"\s*;\s*A\s*=\s*"(gfx[0-9a-z]+)"\s*\}', block)


def _match_sh(rows: list[tuple[list[str], str]], gpu_name: str) -> str | None:
    """Evaluate a shell `case` table: first arm whose glob matches wins."""
    for patterns, arch in rows:
        for pattern in patterns:
            # Shell case globs quote literal segments: *"RX 7900"* -> *RX 7900*
            if fnmatch.fnmatchcase(gpu_name, pattern.replace('"', "")):
                return arch
    return None


def _match_ps(rows: list[tuple[str, str]], gpu_name: str) -> str | None:
    """First arm whose regex matches wins; PowerShell -match is case-insensitive, and .NET and
    Python agree on these patterns (alternation plus one negative lookahead)."""
    for pattern, arch in rows:
        if re.search(pattern, gpu_name, re.IGNORECASE):
            return arch
    return None


# Ordering traps: 'RX 9070 XT' must beat bare '9070', 'RX 7700S' must beat 'RX 7700'.
_GPU_NAME_LEAF_CASES = [
    ("AMD Radeon RX 9070 XT", "gfx120X-all"),
    ("AMD Radeon RX 9070", "gfx120X-all"),
    # Navi 48 workstation, gfx1201; its name has neither 9070 nor 9080.
    ("AMD Radeon AI PRO R9700", "gfx120X-all"),
    ("AMD Radeon RX 9060 XT", "gfx120X-all"),
    ("AMD Radeon 8060S Graphics", "gfx1151"),
    ("AMD Ryzen AI Max+ 395 w/ Radeon 8060S Graphics", "gfx1151"),
    ("AMD Radeon 890M Graphics", "gfx1150"),
    ("AMD Radeon 880M Graphics", "gfx1150"),
    ("AMD Radeon 860M Graphics", "gfx1152"),
    ("AMD Radeon 840M Graphics", "gfx1152"),
    ("AMD Ryzen AI 7 350 w/ Radeon 860M", "gfx1152"),
    ("AMD Radeon RX 7900 XTX", "gfx110X-all"),
    ("AMD Radeon RX 7800 XT", "gfx110X-all"),
    ("AMD Radeon PRO W7900", "gfx110X-all"),
    ("AMD Radeon RX 7700S", "gfx110X-all"),
    ("AMD Radeon RX 7600 XT", "gfx110X-all"),
    ("AMD Radeon 780M Graphics", "gfx110X-all"),
    ("AMD Radeon RX 6900 XT", "gfx103X-all"),
    ("AMD Radeon RX 6700 XT", "gfx103X-all"),
    ("AMD Radeon RX 6600 XT", "gfx103X-all"),
    ("AMD Radeon RX 6500 XT", "gfx103X-all"),
    ("AMD Radeon RX 6950 XT", "gfx103X-all"),
    ("AMD Radeon RX 6850M XT", "gfx103X-all"),
    ("AMD Radeon RX 6550M", "gfx103X-all"),
    ("AMD Radeon RX 6450M", "gfx103X-all"),
    ("AMD Radeon PRO W6300", "gfx103X-all"),
]

# Transcribed from AMD's ROCm compatibility matrix, NOT from the installer tables.
_AMD_DOCUMENTED_ARCH = {
    # RDNA 4 -- Navi 48 is gfx1201, Navi 44 is gfx1200.
    "AMD Radeon RX 9070 XT": "gfx1201",
    "AMD Radeon RX 9070 GRE": "gfx1201",
    "AMD Radeon RX 9070": "gfx1201",
    "AMD Radeon RX 9060 XT": "gfx1200",
    "AMD Radeon RX 9060": "gfx1200",
    # Navi 48 as the R9000 workstation card (rocminfo in #7624/#7307).
    "AMD Radeon AI PRO R9700": "gfx1201",
    "AMD Radeon RX 7900 XTX": "gfx1100",
    "AMD Radeon PRO W7900": "gfx1100",
    "AMD Radeon PRO W7800": "gfx1100",
    "AMD Radeon RX 7800 XT": "gfx1101",
    "AMD Radeon RX 7700 XT": "gfx1101",
    "AMD Radeon PRO W7700": "gfx1101",
    "AMD Radeon PRO V710": "gfx1101",
    "AMD Radeon RX 7600 XT": "gfx1102",
    "AMD Radeon RX 7700S": "gfx1102",
    "AMD Radeon PRO W7600": "gfx1102",
    # Strix Point is gfx1150, Krackan Point (860M/840M) is gfx1152 (AMD lemonade GPU table).
    "AMD Radeon 8060S Graphics": "gfx1151",
    "AMD Radeon 890M Graphics": "gfx1150",
    "AMD Radeon 880M Graphics": "gfx1150",
    "AMD Radeon 860M Graphics": "gfx1152",
    "AMD Radeon 840M Graphics": "gfx1152",
}


def _name_tables() -> dict[str, object]:
    install_sh = _INSTALL_SH.read_text(encoding = "utf-8")
    return {
        "install.sh:_infer_amd_gfx_arch_from_gpu_name": _name_table_sh_function(
            install_sh, "_infer_amd_gfx_arch_from_gpu_name"
        ),
        # install.sh's second copy drives the 'Tip: set UNSLOTH_ROCM_GFX_ARCH' line users paste.
        "install.sh:_gpu_disp_gfx": _name_table_sh_case(
            install_sh, '"$_gpu_disp_mkt"', "_gpu_disp_gfx"
        ),
        "studio/setup.sh": _name_table_sh_case(
            _SETUP_SH.read_text(encoding = "utf-8"), '"$_sup_gfx_in"', "_sup_gfx_out"
        ),
        "install.ps1": _name_table_ps(_INSTALL_PS1),
        "studio/setup.ps1": _name_table_ps(_SETUP_PS1),
        "studio/install_python_stack.py": list(stack_mod._WIN_GPU_NAME_ARCH_TABLE),
        # The backend's copy decides whether a Windows adapter without AdapterFamily can be repaired.
        "studio/backend/utils/hardware/hardware.py": _name_table_py_literal(
            PACKAGE_ROOT / "studio" / "backend" / "utils" / "hardware" / "hardware.py",
            "_GPU_NAME_GFX_TABLE",
        ),
    }


def _name_table_py_literal(path: Path, name: str) -> list:
    """A module-level list-of-pairs literal, read without importing the module."""
    import ast

    tree = ast.parse(path.read_text(encoding = "utf-8"))
    for node in tree.body:
        targets = (
            [node.target]
            if isinstance(node, ast.AnnAssign)
            else node.targets
            if isinstance(node, ast.Assign)
            else []
        )
        for target in targets:
            if isinstance(target, ast.Name) and target.id == name:
                return [tuple(pair) for pair in ast.literal_eval(node.value)]
    raise AssertionError(f"{name} not found in {path}")


def _spoof_profiles() -> dict[str, str]:
    """Parsed with ast: importing the spoof module would spoof torch.cuda for every test in the process."""
    tree = ast.parse(_SPOOF_PY.read_text(encoding = "utf-8"))
    for node in tree.body:
        target = node.target if isinstance(node, ast.AnnAssign) else None
        if target is not None and getattr(target, "id", "") == "_PROFILES":
            return {gfx: value[0] for gfx, value in ast.literal_eval(node.value).items()}
    raise AssertionError("_PROFILES not found in tests/_zoo_rocm_spoof.py")


# The spoof fixture maps gfx -> name, an independent witness. RX 6700 XT is a deliberate
# divergence: AMD documents no gfx1031 and nothing consumes the exact id.
_SPOOF_DIVERGENCES = {
    "gfx1031": "installers group Navi 22 into the gfx1030 arm; see comment above",
}


def _resolve(where: str, rows, gpu_name: str) -> str | None:
    """Shell tables are case globs; PowerShell and Python tables are ordered first-match regexes."""
    return (
        _match_sh(rows, gpu_name)
        if where.split(":")[0].endswith(".sh")
        else _match_ps(rows, gpu_name)
    )


class TestGpuNameArchParity:
    """All four name -> gfx tables must resolve the same GPU the same way."""

    def test_every_copy_is_non_empty(self):
        for where, rows in _name_tables().items():
            assert rows, f"{where}: parsed an empty name -> gfx table (table moved or renamed?)"

    @pytest.mark.parametrize("gpu_name", [name for name, _ in _GPU_NAME_LEAF_CASES])
    def test_all_copies_return_the_same_arch(self, gpu_name):
        """Drift guard with no expected value, only agreement: catches a table edited in one
        installer but not the others, and stays honest even where the shipped gfx id is wrong."""
        answers = {where: _resolve(where, rows, gpu_name) for where, rows in _name_tables().items()}
        distinct = set(answers.values())
        assert len(distinct) == 1, f"{gpu_name!r} resolves inconsistently: {answers}"
        assert distinct != {None}, f"{gpu_name!r} is not matched by any copy of the table"

    @pytest.mark.parametrize("gpu_name,expected_leaf", _GPU_NAME_LEAF_CASES)
    def test_every_copy_routes_to_the_right_wheel_index(self, gpu_name, expected_leaf):
        """What the tables are for. A wrong leaf is the user-visible failure:
        CPU-only torch, or a wheel built for the wrong ISA."""
        families = stack_mod._GFX_TO_AMD_INDEX_ARCH
        for where, rows in _name_tables().items():
            arch = _resolve(where, rows, gpu_name)
            assert arch is not None, f"{where}: {gpu_name!r} matched nothing"
            assert (
                families.get(arch) == expected_leaf
            ), f"{where}: {gpu_name!r} -> {arch} -> {families.get(arch)!r}, expected {expected_leaf!r}"

    @pytest.mark.parametrize("gpu_name,expected_arch", sorted(_AMD_DOCUMENTED_ARCH.items()))
    def test_every_copy_matches_amds_documented_arch(self, gpu_name, expected_arch):
        """The gfx id itself, against AMD's matrix rather than against a sibling
        copy of the same table. Agreement between five copies proves nothing if
        all five were transcribed from the same mistake."""
        for where, rows in _name_tables().items():
            arch = _resolve(where, rows, gpu_name)
            assert (
                arch == expected_arch
            ), f"{where}: {gpu_name!r} -> {arch!r}, AMD documents {expected_arch!r}"

    def test_unknown_name_matches_nothing_anywhere(self):
        """An unrecognised card must fall through to the CPU path in every copy,
        never onto a neighbouring arm."""
        for where, rows in _name_tables().items():
            got = _resolve(where, rows, "NVIDIA GeForce RTX 4090")
            assert got is None, f"{where}: RTX 4090 matched {got!r}"

    @pytest.mark.parametrize(
        "gpu_name",
        [
            "ATI Radeon 9700 PRO",
            "ATI Radeon 9800 PRO",
            "AMD Radeon R9 Fury X",
            "AMD Radeon Pro WX 9100",
        ],
    )
    def test_the_r9700_arm_does_not_swallow_older_cards(self, gpu_name):
        """The arm is spelled "R9700", not a bare "9700": ATI shipped a Radeon 9700 PRO in
        2002 and the loose token would hand that card RDNA 4 wheels. None of these pre-RDNA
        names may resolve to anything."""
        for where, rows in _name_tables().items():
            got = _resolve(where, rows, gpu_name)
            assert got is None, f"{where}: {gpu_name!r} matched {got!r}"

    def test_inferred_arch_always_has_an_index_family(self):
        """Every arch a name table can produce must be routable to an AMD wheel
        index, else detection succeeds and the install still lands on CPU torch."""
        families = stack_mod._GFX_TO_AMD_INDEX_ARCH
        # RDNA 1 is Windows-only (multi-arch index, #11614); Linux tables must not produce it.
        multiarch = stack_mod._WINDOWS_MULTIARCH_GFX
        linux_copies = ("install.sh", "studio/setup.sh")
        for where, rows in _name_tables().items():
            for arch in {arch for _, arch in rows}:
                if arch in multiarch and not where.startswith(linux_copies):
                    assert (
                        stack_mod._windows_rocm_index_url(arch) is not None
                    ), f"{where}: {arch} has no Windows route"
                    continue
                assert arch in families, f"{where}: {arch} has no entry in _GFX_TO_AMD_INDEX_ARCH"

    def test_every_documented_gpu_resolves_somewhere(self):
        """A documented GPU missing from every table silently falls back to CPU, not a wrong id."""
        for gpu_name in sorted(_AMD_DOCUMENTED_ARCH):
            for where, rows in _name_tables().items():
                assert (
                    _resolve(where, rows, gpu_name) is not None
                ), f"{where}: {gpu_name!r} matches no arm, so this card gets CPU-only torch"


class TestSpoofFixtureParity:
    """Spoof fixture is another copy of the mapping; drift makes other ROCm tests use an impossible host."""

    def test_spoof_profiles_parse(self):
        profiles = _spoof_profiles()
        assert profiles, "parsed an empty _PROFILES (renamed or restructured?)"
        assert all(gfx.startswith("gfx") for gfx in profiles), profiles

    def test_spoof_names_resolve_back_to_their_own_arch(self):
        """Round-trip: feed each spoofed marketing name through the installer
        tables and the answer must be the gfx the spoof claims to be emulating."""
        tables = _name_tables()
        for gfx, gpu_name in sorted(_spoof_profiles().items()):
            if gfx in _SPOOF_DIVERGENCES:
                continue
            for where, rows in tables.items():
                got = _resolve(where, rows, gpu_name)
                assert (
                    got == gfx
                ), f"{where}: spoof says {gfx} is {gpu_name!r}, installer says {got!r}"

    def test_divergences_are_real_and_still_diverging(self):
        """Keeps the divergence exception list honest: once installers are corrected this fails so
        the entry is removed, not silently suppressing a passing check."""
        tables = _name_tables()
        profiles = _spoof_profiles()
        for gfx in _SPOOF_DIVERGENCES:
            assert gfx in profiles, f"{gfx} is exempted but no longer in the spoof"
            answers = {_resolve(w, r, profiles[gfx]) for w, r in tables.items()}
            assert answers != {gfx}, f"{gfx} now agrees everywhere; drop it from _SPOOF_DIVERGENCES"


_MKT_NAME = re.compile(r"(RX\s*\d{4}|PRO\s*[WV]\d{3,4}|\b90[5-8]0\b)", re.IGNORECASE)
_GFX_ID = re.compile(r"gfx1[0-2][0-9a-z]{1,2}")

# Skip dirs of third-party or generated code; scanning them is slow and any hit would not be ours to fix.
_SCAN_SKIP_DIRS = {".git", "node_modules", ".venv", "venv", "build", "dist", "__pycache__"}

_REGISTERED_TABLE_FILES = {
    "install.sh",
    "install.ps1",
    "studio/setup.sh",
    "studio/setup.ps1",
    "studio/install_python_stack.py",
    "studio/backend/utils/hardware/hardware.py",
    "tests/_zoo_rocm_spoof.py",
}

# Three or more lines means a table; the known one/two-line hits are prose.
_TABLE_LINE_THRESHOLD = 3


def _under_cargo_output(path: Path, root: Path) -> bool:
    """Matches Cargo build output (target/ beside Cargo.toml), since tauri copies install.sh into it."""
    for parent in path.parents:
        if parent == root.parent:
            break
        if parent.name == "target" and (parent.parent / "Cargo.toml").is_file():
            return True
    return False


def _files_carrying_a_name_arch_table(root: Path = PACKAGE_ROOT) -> dict[str, int]:
    found: dict[str, int] = {}
    for path in root.rglob("*"):
        if path.suffix not in {".sh", ".ps1", ".py"} or not path.is_file():
            continue
        rel = path.relative_to(root).as_posix()
        if any(part in _SCAN_SKIP_DIRS for part in path.relative_to(root).parts):
            continue
        if _under_cargo_output(path, root):
            continue
        # Tests quote names beside ids by nature; non-test_ fixtures like _zoo_rocm_spoof.py stay in scope.
        if path.name.startswith("test_"):
            continue
        try:
            text = path.read_text(encoding = "utf-8", errors = "ignore")
        except OSError:
            continue
        hits = sum(
            1 for line in text.splitlines() if _MKT_NAME.search(line) and _GFX_ID.search(line)
        )
        if hits >= _TABLE_LINE_THRESHOLD:
            found[rel] = hits
    return found


class TestNoUnregisteredArchTable:
    """Guards against an unknown copy of the arch table; enumerating copies by hand is what let them
    drift, so the source tree is scanned instead."""

    def test_scan_still_finds_the_known_copies(self):
        """Guards the guard: if the heuristic stops matching (patterns reformatted
        onto multiple lines, say), it would silently find nothing and pass."""
        found = _files_carrying_a_name_arch_table()
        missing = _REGISTERED_TABLE_FILES - set(found)
        assert not missing, f"scan no longer detects known tables in {sorted(missing)}"

    def test_no_unregistered_copies(self):
        found = _files_carrying_a_name_arch_table()
        extra = {rel: n for rel, n in found.items() if rel not in _REGISTERED_TABLE_FILES}
        assert not extra, (
            f"unregistered GPU-name/arch table(s): {extra}. Wire each into "
            f"_name_tables() (or the spoof check) and add it to "
            f"_REGISTERED_TABLE_FILES, so drift there fails CI too."
        )

    def test_cargo_build_output_is_skipped_but_a_plain_target_dir_is_not(self, tmp_path):
        """The skip is narrow on purpose: `target/` next to a Cargo.toml is build output,
        `target/` anywhere else is source and a copy hiding there still has to fail."""
        table = "\n".join(f"# RX 7{n}00 gfx1100" for n in range(1, 6)) + "\n"
        (tmp_path / "src-tauri" / "target" / "debug").mkdir(parents = True)
        (tmp_path / "src-tauri" / "Cargo.toml").write_text("[package]\n")
        (tmp_path / "src-tauri" / "target" / "debug" / "install.sh").write_text(table)
        (tmp_path / "scripts" / "target").mkdir(parents = True)
        (tmp_path / "scripts" / "target" / "install.sh").write_text(table)

        found = _files_carrying_a_name_arch_table(tmp_path)
        assert "scripts/target/install.sh" in found, (
            "a table under a plain target/ directory was skipped; the guard would miss "
            f"a real copy there: {found}"
        )
        assert (
            "src-tauri/target/debug/install.sh" not in found
        ), f"cargo build output is still scanned: {found}"


class TestTorch211PinAllowlistParity:
    """gfx120X-all / gfx1151 / gfx1150 / gfx1152 / gfx103X-all / gfx110X-all (and rocm7.2) ship the null
    _grouped_mm kernel below torch 2.11, so all three installers must raise the
    same floor. A leaf missing from one copy reintroduces the crash there."""

    _EXPECTED = {"gfx120x-all", "gfx1151", "gfx1150", "gfx1152", "gfx103x-all", "gfx110x-all"}

    def test_install_sh_pins_the_same_leaves(self):
        source = _INSTALL_SH.read_text(encoding = "utf-8")
        idx = source.find('case "$_torch_index_leaf" in')
        assert idx != -1
        arm = re.search(r"\n\s*(rocm7\.2\|[^)]*)\)", source[idx:])
        assert arm, "torch 2.11 pin arm not found in install.sh"
        leaves = {leaf.strip() for leaf in arm.group(1).split("|")}
        assert (
            self._EXPECTED <= leaves
        ), f"install.sh pin arm missing {sorted(self._EXPECTED - leaves)}"
        assert "rocm7.2" in leaves

    def test_install_ps1_pins_the_same_leaves(self):
        source = _INSTALL_PS1.read_text(encoding = "utf-8")
        m = re.search(r"\$_pinGfx211\s*=\s*@\(([^)]*)\)", source)
        assert m, "$_pinGfx211 not found in install.ps1"
        leaves = set(re.findall(r"'([^']+)'", m.group(1)))
        assert leaves == self._EXPECTED, f"install.ps1 pins {sorted(leaves)}"

    def test_setup_ps1_pins_the_same_leaves(self):
        source = _SETUP_PS1.read_text(encoding = "utf-8")
        m = re.search(r"return\s+@\(([^)]*)\)\s*-contains\s*\$Leaf", source)
        assert m, "the 2.11 pin allowlist helper was not found in studio/setup.ps1"
        leaves = set(re.findall(r"'([^']+)'", m.group(1)))
        assert leaves == self._EXPECTED, f"studio/setup.ps1 pins {sorted(leaves)}"


class TestShadowingIntegratedGfxParity:
    """The iGPU skip is in four entry points that must agree, or one keeps installing the iGPU's wheels."""

    _STRIX = {"gfx1150", "gfx1151", "gfx1152"}

    def _setup_ps1_list(self):
        source = _SETUP_PS1.read_text(encoding = "utf-8")
        m = re.search(r"\$script:ShadowingIntegratedGfx\s*=\s*@\(([^)]*)\)", source)
        assert m, "$script:ShadowingIntegratedGfx not found in studio/setup.ps1"
        return set(re.findall(r'"([^"]+)"', m.group(1)))

    def _prebuilt_list(self):
        tree = ast.parse(_PREBUILT_PY.read_text(encoding = "utf-8"))
        for node in tree.body:
            if isinstance(node, ast.Assign) and any(
                getattr(t, "id", None) == "SHADOWING_INTEGRATED_GFX" for t in node.targets
            ):
                return set(ast.literal_eval(node.value.args[0]))
        raise AssertionError("SHADOWING_INTEGRATED_GFX not found in install_llama_prebuilt.py")

    def _install_sh_list(self):
        body = _sh_function_body(
            _INSTALL_SH.read_text(encoding = "utf-8"), "_amd_gfx_is_shadowing_integrated"
        )
        m = re.search(r"\n\s*(gfx[^)]*)\)\s*return 0", body)
        assert m, "the shadowing-APU arm was not found in _amd_gfx_is_shadowing_integrated"
        return {arch.strip() for arch in m.group(1).split("|")}

    def test_install_sh_matches_install_python_stack(self):
        # install.sh's family pick is final: _ensure_rocm_torch returns early for a non-ROCm one.
        assert self._install_sh_list() == set(stack_mod._SHADOWING_INTEGRATED_GFX)

    def test_setup_ps1_matches_install_python_stack(self):
        assert self._setup_ps1_list() == set(stack_mod._SHADOWING_INTEGRATED_GFX)

    def test_install_llama_prebuilt_matches_install_python_stack(self):
        # _apply_host_overrides() honours setup's repick only for these, or torch and llama split GPUs.
        assert self._prebuilt_list() == set(stack_mod._SHADOWING_INTEGRATED_GFX)

    def test_strix_is_excluded_from_every_copy(self):
        # Strix arches are training targets; listing them would silently redirect Strix hosts.
        assert not (self._STRIX & set(stack_mod._SHADOWING_INTEGRATED_GFX))
        assert not (self._STRIX & self._setup_ps1_list())
        assert not (self._STRIX & self._prebuilt_list())
        assert not (self._STRIX & self._install_sh_list())


def _sh_call_site_offsets(source: str, helper: str) -> list[int]:
    """Offsets where `helper` is invoked, excluding its definition and comments."""
    offsets = []
    for match in re.finditer(re.escape(helper), source):
        line_start = source.rfind("\n", 0, match.start()) + 1
        line_end = source.find("\n", match.start())
        line = source[line_start : line_end if line_end != -1 else len(source)]
        before = line[: match.start() - line_start]
        if "#" in before:
            continue
        if re.match(rf"\s*{re.escape(helper)}\s*\(\)", line):
            continue
        offsets.append(match.start())
    return offsets


class TestShadowingPreferenceIsApplied:
    """Parity is not enough: each entry point must reach the preference helper, not merely define it."""

    _VISIBILITY_ENV = ("HIP_VISIBLE_DEVICES", "ROCR_VISIBLE_DEVICES", "CUDA_VISIBLE_DEVICES")

    _SHELL_RESOLVERS = ("install.sh", "studio/setup.sh")

    def _shadowing_arm(self, path: Path) -> set[str]:
        body = _sh_function_body(
            path.read_text(encoding = "utf-8"), "_amd_gfx_is_shadowing_integrated"
        )
        m = re.search(r"\n\s*(gfx[^)]*)\)\s*return 0", body)
        assert m, f"the shadowing-APU arm was not found in {path.name}"
        return {arch.strip() for arch in m.group(1).split("|")}

    def test_setup_sh_defines_the_same_shadowing_table(self):
        """studio/setup.sh was the copy that did not exist at all (#11143)."""
        assert self._shadowing_arm(_SETUP_SH) == set(stack_mod._SHADOWING_INTEGRATED_GFX)

    @pytest.mark.parametrize("rel", _SHELL_RESOLVERS)
    def test_every_shell_gfx_resolver_calls_the_preference(self, rel):
        source = (PACKAGE_ROOT / rel).read_text(encoding = "utf-8")
        assert "_amd_prefer_discrete_gfx() {" in source, (
            f"{rel} resolves an AMD gfx arch but does not define _amd_prefer_discrete_gfx; "
            "a table without the preference leaves a hybrid host on its iGPU (#7776, #11143)"
        )
        assert _sh_call_site_offsets(source, "_amd_prefer_discrete_gfx"), (
            f"{rel} defines _amd_prefer_discrete_gfx but never calls it, so the arch it "
            "forwards is still whatever enumerated first"
        )

    # The argv-append sites: a bare '--rocm-gfx' also matches an earlier comment.
    _ROCM_GFX_FORWARDS = ("_PREBUILT_CMD+=(--rocm-gfx", "_WHISPER_CMD+=(--rocm-gfx")

    def test_setup_sh_applies_the_preference_before_forwarding_rocm_gfx(self):
        """Both consumers read $_setup_gfx, so the repick must precede both."""
        source = _SETUP_SH.read_text(encoding = "utf-8")
        calls = _sh_call_site_offsets(source, "_amd_prefer_discrete_gfx")
        assert calls, "studio/setup.sh never calls _amd_prefer_discrete_gfx"
        forwards = {}
        for needle in self._ROCM_GFX_FORWARDS:
            at = source.find(needle)
            assert at != -1, f"studio/setup.sh no longer forwards via {needle}"
            forwards[needle] = at
        late = {n: at for n, at in forwards.items() if at < min(calls)}
        assert not late, (
            f"studio/setup.sh repicks the arch only after forwarding it via {sorted(late)}, "
            "so that consumer still receives the iGPU's arch"
        )

    def test_pick_rocm_gfx_target_consults_the_shadowing_table(self):
        """install_llama_prebuilt.py holds the table; this is the function that has
        to read it. It ended `return _tokens[0]` with no preference at all."""
        tree = ast.parse(_PREBUILT_PY.read_text(encoding = "utf-8"))
        for node in ast.walk(tree):
            if isinstance(node, ast.FunctionDef) and node.name == "_pick_rocm_gfx_target":
                names = {n.id for n in ast.walk(node) if isinstance(n, ast.Name)}
                assert "SHADOWING_INTEGRATED_GFX" in names, (
                    "_pick_rocm_gfx_target does not consult SHADOWING_INTEGRATED_GFX, so a "
                    "leading iGPU still wins on a hybrid host (#11143)"
                )
                return
        raise AssertionError("_pick_rocm_gfx_target not found in install_llama_prebuilt.py")

    @pytest.mark.parametrize("rel", _SHELL_RESOLVERS)
    def test_an_explicit_visibility_mask_is_honoured_by_each_shell_copy(self, rel):
        """`HIP_VISIBLE_DEVICES=1` is the documented #7624 / #7669 workaround; a repick
        that second-guesses a mask would break it."""
        body = _sh_function_body(
            (PACKAGE_ROOT / rel).read_text(encoding = "utf-8"), "_amd_prefer_discrete_gfx"
        )
        missing = [env for env in self._VISIBILITY_ENV if env not in body]
        assert not missing, f"{rel}'s _amd_prefer_discrete_gfx ignores {missing}"

    def test_pick_rocm_gfx_target_honours_an_explicit_visibility_mask(self):
        source = _PREBUILT_PY.read_text(encoding = "utf-8")
        tree = ast.parse(source)
        for node in ast.walk(tree):
            if isinstance(node, ast.FunctionDef) and node.name == "_pick_rocm_gfx_target":
                body = ast.get_source_segment(source, node) or ""
                missing = [env for env in self._VISIBILITY_ENV if env not in body]
                assert not missing, f"_pick_rocm_gfx_target ignores {missing}"
                return
        raise AssertionError("_pick_rocm_gfx_target not found in install_llama_prebuilt.py")

    def test_gfx906_is_never_a_repick_candidate(self):
        """Naming gfx906 on a mixed host strands BOTH cards."""
        for rel in self._SHELL_RESOLVERS:
            body = _sh_function_body(
                (PACKAGE_ROOT / rel).read_text(encoding = "utf-8"), "_amd_prefer_discrete_gfx"
            )
            assert "gfx906" in body, f"{rel}'s repick does not exclude gfx906"


if __name__ == "__main__":
    pytest.main([__file__, "-v"])