Skip to content

VoiceStudio is the open-source, fully-local ElevenLabs alternative — voice cloning, voice design, video dubbing, dictation, transcription & audiobook creation in 646 languages.

7.8 KB
Raw Blame
"""The ROCm reinstall must pin the same Torch stack the project resolves to.

An AMD source install does not come from ``uv.lock`` after setup swaps the CUDA
wheels for ROCm ones, so the pins used by ``scripts/setup.py`` must match the
project constraints.

A pin in two files stays correct only while someone remembers both.
``bootstrap.rs`` says "Keep in sync with [tool.uv.constraint-dependencies]",
and a comment cannot enforce itself — CLAUDE.md's convention is that a rule a
reviewer would have to remember belongs in a test. This is that test.

Failure here means an AMD user would get a different Torch stack from every
other platform, with no error at install time: the mismatch only shows up later
as ``operator torchvision::nms does not exist`` or a silent CPU fallback.
"""
import os
import re

_ROOT = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
_PYPROJECT = os.path.join(_ROOT, "pyproject.toml")
_SETUP = os.path.join(_ROOT, "scripts", "setup.py")

#: Packages whose ROCm reinstall must match the project's constraint. The
#: Torch trio specifically: they ship as one matched set, and mixing versions
#: across them is the failure this guards.
_TORCH_STACK = ("torch", "torchaudio", "torchvision")


def _constraint_pins() -> dict:
    """``{name: version}`` from ``[tool.uv.constraint-dependencies]``."""
    with open(_PYPROJECT, encoding="utf-8") as fh:
        src = fh.read()
    block = src.split("constraint-dependencies = [", 1)
    assert len(block) == 2, "constraint-dependencies block not found in pyproject.toml"
    body = block[1].split("]", 1)[0]
    pins = {}
    for name, version in re.findall(r'"([A-Za-z0-9_.-]+)==([^"]+)"', body):
        pins[name.lower()] = version
    return pins


def _rocm_reinstall_args() -> list:
    """The literal package pins in ``ROCM_TORCH_PINS``."""
    with open(_SETUP, encoding="utf-8") as fh:
        src = fh.read()
    marker = "ROCM_TORCH_PINS = ("
    assert marker in src, f"ROCM_TORCH_PINS renamed or removed from {_SETUP}"
    body = src.split(marker, 1)[1].split(")", 1)[0]
    return re.findall(r'"([^"]+)"', body)


def _electron_rocm_reinstall_args() -> list:
    """The literal package pins in Electron's ``ROCM_TORCH_PINS``."""
    path = os.path.join(_ROOT, "electron", "src", "main", "runtime-project.ts")
    with open(path, encoding="utf-8") as fh:
        src = fh.read()
    marker = "export const ROCM_TORCH_PINS = ["
    assert marker in src, f"ROCM_TORCH_PINS renamed or removed from {path}"
    body = src.split(marker, 1)[1].split("]", 1)[0]
    return re.findall(r"'([^']+)'", body)


def test_electron_rocm_pins_match_the_project_constraint():
    pins = _constraint_pins()
    named = {
        name.lower(): version
        for name, _, version in (arg.partition("==") for arg in _electron_rocm_reinstall_args())
        if version
    }
    assert {pkg: named.get(pkg) for pkg in _TORCH_STACK} == {
        pkg: pins[pkg] for pkg in _TORCH_STACK
    }


def test_the_torch_stack_is_pinned_in_pyproject():
    """Guards the rest of this file from passing vacuously if the pins move."""
    pins = _constraint_pins()
    missing = [p for p in _TORCH_STACK if p not in pins]
    assert not missing, (
        f"{missing} left [tool.uv.constraint-dependencies]. If that is deliberate, "
        f"drop them from _TORCH_STACK here too — but an unpinned Torch package is "
        f"how #972 shipped an AMD install running on the CPU."
    )


def test_rocm_reinstall_pins_match_the_project_constraint():
    args = _rocm_reinstall_args()
    pins = _constraint_pins()
    named = {}
    for arg in args:
        if "==" in arg:
            name, _, version = arg.partition("==")
            named[name.lower()] = version

    problems = []
    for pkg in _TORCH_STACK:
        expected = pins.get(pkg)
        if expected is None:
            continue  # covered by the test above
        actual = named.get(pkg)
        if actual is None:
            unpinned = any(a == pkg for a in args)
            problems.append(
                f"  {pkg}: pyproject pins =={expected}, bootstrap.rs "
                + ("names it with NO version" if unpinned else "does not install it")
            )
        elif actual != expected:
            problems.append(
                f"  {pkg}: pyproject pins =={expected}, bootstrap.rs pins =={actual}"
            )

    assert not problems, (
        "The ROCm reinstall in scripts/setup.py has drifted "
        "from [tool.uv.constraint-dependencies] in pyproject.toml. An AMD user's "
        "install comes from that pip command, not from uv.lock, so they would run "
        "a different Torch stack from every other platform — and it fails later, "
        "at import, not at install:\n" + "\n".join(problems)
    )


def _backend_rocm_pins_and_index():
    """``(pins, index)`` from ``backend/core/torch_indexes.py`` — the values
    the one-click sidecar installer swaps a ROCm host's torch to (#2371)."""
    path = os.path.join(_ROOT, "backend", "core", "torch_indexes.py")
    with open(path, encoding="utf-8") as fh:
        src = fh.read()
    marker = "ROCM_TORCH_PINS: tuple[str, ...] = ("
    assert marker in src, f"ROCM_TORCH_PINS renamed or removed from {path}"
    body = src.split(marker, 1)[1].split(")", 1)[0]
    pins = re.findall(r'"([^"]+)"', body)
    m = re.search(r'PYTORCH_ROCM_INDEX_URL = "([^"]+)"', src)
    assert m, f"PYTORCH_ROCM_INDEX_URL renamed or removed from {path}"
    return pins, m.group(1)


def test_sidecar_rocm_pins_match_the_project_constraint():
    """The sidecar's ROCm swap must install the same torch versions the main
    venv does, or an AMD host runs its engine venv and its app venv on
    different Torch stacks (#2371)."""
    pins, _ = _backend_rocm_pins_and_index()
    constraints = _constraint_pins()
    named = {}
    for arg in pins:
        name, _, version = arg.partition("==")
        named[name.lower()] = version
    assert named, "backend ROCM_TORCH_PINS parsed empty"
    problems = [
        f"  {pkg}: pyproject pins =={constraints[pkg]}, sidecar pins =={version}"
        for pkg, version in named.items()
        if constraints.get(pkg) != version
    ]
    assert not problems, (
        "backend/core/torch_indexes.py ROCM_TORCH_PINS drifted from "
        "[tool.uv.constraint-dependencies] in pyproject.toml:\n" + "\n".join(problems)
    )


def _setup_rocm_index() -> str:
    with open(_SETUP, encoding="utf-8") as fh:
        src = fh.read()
    m = re.search(r'ROCM_TORCH_INDEX = "([^"]+)"', src)
    assert m, f"ROCM_TORCH_INDEX renamed or removed from {_SETUP}"
    return m.group(1)


def test_sidecar_rocm_index_matches_the_main_venv_swap():
    """Three copies of the index (setup.py, runtime-project.ts, the sidecar
    installer) with only a comment between them is how an AMD host ends up
    running its app venv and its engine venv on different ROCm builds (#2371)."""
    _, backend_index = _backend_rocm_pins_and_index()
    assert backend_index == _setup_rocm_index()
    path = os.path.join(_ROOT, "electron", "src", "main", "runtime-project.ts")
    with open(path, encoding="utf-8") as fh:
        src = fh.read()
    m = re.search(r"export const ROCM_TORCH_INDEX = '([^']+)'", src)
    assert m, f"ROCM_TORCH_INDEX renamed or removed from {path}"
    assert backend_index == m.group(1)


def test_the_whole_stack_is_reinstalled_together():
    """Torch, torchaudio and torchvision ship as one matched set.

    Reinstalling a subset leaves the others as the CUDA wheels that the ROCm
    build cannot pair with — exactly the mismatch #1357 reported, arrived at
    from the other direction.
    """
    args = _rocm_reinstall_args()
    named = {a.partition("==")[0].lower() for a in args if "==" in a or a in _TORCH_STACK}
    missing = [p for p in _TORCH_STACK if p not in named]
    assert not missing, (
        f"the ROCm reinstall does not cover {missing}; those stay on the CUDA "
        f"wheels while the rest switch to ROCm"
    )