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.

8.1 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

"""amd-smi's gpu ids differ from HIP's, so the VRAM branch maps to HIP ids or declines entirely."""

from __future__ import annotations

import subprocess
import sys
import types

import pytest

from core.inference.llama_cpp import LlamaCppBackend
from utils import hardware
from utils.hardware import amd


def _hip_sees(
    monkeypatch,
    count,
    totals = None,
):
    """Declare HIP's own inventory: how many devices it opens, and the total memory
    it reports per physical id. Empty totals mean torch could not describe the
    devices, which the probe fails open on."""
    monkeypatch.setattr(LlamaCppBackend, "_rocm_hip_device_count", staticmethod(lambda: count))
    monkeypatch.setattr(
        LlamaCppBackend,
        "_rocm_total_memory_mib_by_physical_id",
        staticmethod(lambda: dict(totals or {})),
    )


@pytest.fixture
def rocm(monkeypatch):
    """HIP reachability and device count are declared rather than inherited, since CI has no GPU."""
    monkeypatch.setattr(LlamaCppBackend, "_torch_is_rocm", staticmethod(lambda torch: True))
    monkeypatch.setattr(LlamaCppBackend, "_rocm_hip_is_reachable", staticmethod(lambda: True))
    monkeypatch.setattr(
        LlamaCppBackend, "_rocm_unified_memory_gpu_ids", staticmethod(lambda: set())
    )
    for _var in ("HIP_VISIBLE_DEVICES", "ROCR_VISIBLE_DEVICES", "CUDA_VISIBLE_DEVICES"):
        monkeypatch.delenv(_var, raising = False)
    monkeypatch.delenv("GPU_DEVICE_ORDINAL", raising = False)
    _hip_sees(monkeypatch, 1)


def _payload(*gpus: tuple[int, int, int]):
    """amd-smi ``metric`` output for (amd-smi gpu id, used MiB, total MiB) triples."""
    return [
        {
            "gpu": idx,
            "mem_usage": {
                "used_vram": {"value": used, "unit": "MB"},
                "total_vram": {"value": total, "unit": "MB"},
            },
        }
        for idx, used, total in gpus
    ]


def _enumeration(hip_by_gpu: dict):
    """``amd-smi list -e`` output: the amd-smi gpu id -> HIP id mapping."""
    return [{"gpu": gpu, "hip_id": hip} for gpu, hip in hip_by_gpu.items()]


def _fake_amd_smi(metric, enumeration = None):
    """Stub amd-smi: ``metric`` returns the VRAM rows, ``list`` the id mapping."""

    def _run(*args, **kwargs):
        if args and args[0] == "list":
            return enumeration
        return metric

    return _run


class TestTheAmdSmiIdIsTranslatedToHip:
    """Defect A: the branch keyed rows by amd-smi's gpu id and compared that number
    against HIP visibility tokens."""

    def test_a_reordered_host_reports_each_cards_memory_under_its_hip_id(self, rocm, monkeypatch):
        """amd-smi gpu 1 is HIP 0 here. Keyed by amd-smi's number, the 96 GiB card's
        memory lands on HIP id 1 and the planner pins the 16 GiB card for a model
        only the big one fits."""
        monkeypatch.setattr(
            amd,
            "_run_amd_smi",
            _fake_amd_smi(
                _payload((0, 1024, 16384), (1, 2048, 98304)),
                _enumeration({0: 1, 1: 0}),
            ),
        )
        _hip_sees(monkeypatch, 2)
        assert LlamaCppBackend._get_gpu_memory_amd_smi() == [
            (0, 96256, 98304),
            (1, 15360, 16384),
        ]

    def test_the_mask_is_matched_in_hip_space(self, rocm, monkeypatch):
        """HIP_VISIBLE_DEVICES=0 names the 96 GiB card, which amd-smi calls gpu 1."""
        monkeypatch.setenv("HIP_VISIBLE_DEVICES", "0")
        monkeypatch.setattr(
            amd,
            "_run_amd_smi",
            _fake_amd_smi(
                _payload((0, 1024, 16384), (1, 2048, 98304)),
                _enumeration({0: 1, 1: 0}),
            ),
        )
        assert LlamaCppBackend._get_gpu_memory_amd_smi() == [(0, 96256, 98304)]

    def test_a_multi_gpu_host_declines_when_the_mapping_is_unavailable(self, rocm, monkeypatch):
        """Before ROCm 6.4.0 amd-smi lacks list -e, so identity is unproven; defer to torch."""
        monkeypatch.setattr(
            amd,
            "_run_amd_smi",
            _fake_amd_smi(_payload((0, 1024, 16384), (1, 2048, 98304)), None),
        )
        assert LlamaCppBackend._get_gpu_memory_amd_smi() == []

    def test_a_partial_mapping_declines(self, rocm, monkeypatch):
        """``hip_id`` is "N/A" when the library cannot read the device's KFD node."""
        monkeypatch.setattr(
            amd,
            "_run_amd_smi",
            _fake_amd_smi(
                _payload((0, 1024, 16384), (1, 2048, 98304)),
                [{"gpu": 0, "hip_id": 0}, {"gpu": 1, "hip_id": "N/A"}],
            ),
        )
        assert LlamaCppBackend._get_gpu_memory_amd_smi() == []

    def test_a_single_gpu_host_needs_no_mapping(self, rocm, monkeypatch):
        """One card on both sides forces the mapping, so any amd-smi version keeps the saving."""
        monkeypatch.setattr(amd, "_run_amd_smi", _fake_amd_smi(_payload((0, 4096, 24576)), None))
        assert LlamaCppBackend._get_gpu_memory_amd_smi() == [(0, 20480, 24576)]


class TestAnUnreadableMaskDeclines:
    """Defect A, second half: ``_resolve_visible_physical_ids`` returns None both for
    "no mask" and for a mask it cannot parse. ROCr accepts UUID tokens, so reading
    the second as the first offers a deliberately hidden card for ranking."""

    def test_a_uuid_rocr_mask_is_not_read_as_no_mask(self, rocm, monkeypatch):
        monkeypatch.setenv("ROCR_VISIBLE_DEVICES", "GPU-a1b2c3d4e5f60718")
        monkeypatch.setattr(
            amd,
            "_run_amd_smi",
            _fake_amd_smi(
                _payload((0, 1024, 16384), (1, 2048, 98304)),
                _enumeration({0: 0, 1: 1}),
            ),
        )
        assert LlamaCppBackend._get_gpu_memory_amd_smi() == []

    def test_an_empty_mask_still_means_no_gpus(self, rocm, monkeypatch):
        monkeypatch.setenv("HIP_VISIBLE_DEVICES", "")
        monkeypatch.setattr(amd, "_run_amd_smi", _fake_amd_smi(_payload((0, 1024, 16384)), None))
        assert LlamaCppBackend._get_gpu_memory_amd_smi() == []


class TestAPartialAnswerDefersToTorch:
    """A row amd-smi cannot size is dropped, so a partial list must defer to torch."""

    @pytest.fixture
    def apu_plus_dgpu(self, rocm, monkeypatch):
        monkeypatch.setattr(
            amd,
            "_run_amd_smi",
            _fake_amd_smi(
                _payload((0, 512, 0), (1, 1024, 8192)),
                _enumeration({0: 0, 1: 1}),
            ),
        )
        _hip_sees(monkeypatch, 2)

    def test_the_branch_declines_rather_than_answering_for_one_card(self, apu_plus_dgpu):
        assert LlamaCppBackend._get_gpu_memory_amd_smi() == []

    def test_the_probe_still_sees_both_devices(self, apu_plus_dgpu, monkeypatch):
        """End to end: torch answers and both GPUs survive, so tensor-parallel sees both."""
        monkeypatch.setattr(
            LlamaCppBackend, "_is_vulkan_backend", staticmethod(lambda binary: False)
        )

        def _no_nvidia_smi(*args, **kwargs):
            raise FileNotFoundError("nvidia-smi")

        monkeypatch.setattr(subprocess, "run", _no_nvidia_smi)
        torch_mod = types.ModuleType("torch")
        torch_mod.cuda = types.SimpleNamespace(
            is_available = lambda: True,
            device_count = lambda: 2,
            mem_get_info = lambda *a: (0, 0),
        )
        monkeypatch.setitem(sys.modules, "torch", torch_mod)
        monkeypatch.setattr(
            hardware,
            "trusted_mem_get_info",
            lambda ordinal, *a, **k: (
                (16 * 1024**3, 32 * 1024**3) if ordinal == 0 else (7 * 1024**3, 8 * 1024**3)
            ),
        )
        assert LlamaCppBackend._get_gpu_memory() == [(0, 16384, 32768), (1, 7168, 8192)]

    def test_a_visible_device_amd_smi_never_enumerated_declines(self, rocm, monkeypatch):
        """Unenumerated visible devices are unmeasured, not absent, so the probe declines."""
        monkeypatch.setenv("HIP_VISIBLE_DEVICES", "0,1")
        _hip_sees(monkeypatch, 2)
        monkeypatch.setattr(amd, "_run_amd_smi", _fake_amd_smi(_payload((0, 1024, 16384)), None))
        assert LlamaCppBackend._get_gpu_memory_amd_smi() == []