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.

26.8 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

"""GGML_CUDA_ENABLE_UNIFIED_MEMORY must be set only for AMD unified-memory APUs
(gfx1150/gfx1151/gfx1152), never for discrete AMD, NVIDIA, CPU or macOS."""

from __future__ import annotations

import sys
import types

import pytest

from core.inference.llama_cpp import LlamaCppBackend

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


@pytest.fixture(autouse = True)
def _no_inherited_gpu_mask(monkeypatch):
    """Clear inherited GPU masks per test: a shell mask remaps ordinal 0 off the fake host."""
    for _m in _VISIBLE_DEVICE_MASKS:
        monkeypatch.delenv(_m, raising = False)


def _fake_torch(
    hip,
    archs,
    *,
    cuda_ok = True,
):
    t = types.ModuleType("torch")
    t.version = types.SimpleNamespace(hip = hip)
    t.cuda = types.SimpleNamespace(
        is_available = lambda: cuda_ok,
        device_count = lambda: len(archs),
        get_device_properties = lambda i: types.SimpleNamespace(gcnArchName = archs[i]),
    )
    return t


@pytest.mark.parametrize(
    "hip,archs,expected",
    [
        ("6.2.0", ["gfx1151:xnack-"], True),  # Strix Halo APU (suffix stripped)
        ("6.2.0", ["gfx1150"], True),  # Strix Point APU
        ("6.2.0", ["gfx1152"], True),  # Krackan Point APU (Radeon 860M/840M)
        ("6.2.0", ["gfx1152:sramecc-:xnack-"], True),  # same, feature flags stripped
        ("6.2.0", ["gfx1100"], False),  # discrete RDNA3
        ("6.2.0", ["gfx1201"], False),  # discrete RDNA4
        ("6.2.0", ["gfx942"], False),  # MI300X (data center)
        (None, ["sm_90"], False),  # NVIDIA (no torch.version.hip)
        ("6.2.0", ["gfx1100", "gfx1151"], True),  # mixed dGPU + APU
    ],
)
def test_apu_unified_memory_gating(monkeypatch, hip, archs, expected):
    monkeypatch.setitem(sys.modules, "torch", _fake_torch(hip, archs))
    assert LlamaCppBackend._amd_apu_wants_unified_memory() is expected


def test_apu_guard_scopes_to_selected_gpu(monkeypatch):
    # Mixed host: physical id 0 = discrete gfx1100, 1 = gfx1151 APU.
    for _m in ("HIP_VISIBLE_DEVICES", "ROCR_VISIBLE_DEVICES", "CUDA_VISIBLE_DEVICES"):
        monkeypatch.delenv(_m, raising = False)
    monkeypatch.setitem(sys.modules, "torch", _fake_torch("6.2.0", ["gfx1100", "gfx1151"]))
    assert LlamaCppBackend._amd_apu_wants_unified_memory([0]) is False
    assert LlamaCppBackend._amd_apu_wants_unified_memory([]) is False
    assert LlamaCppBackend._amd_apu_wants_unified_memory([1]) is True
    assert LlamaCppBackend._amd_apu_wants_unified_memory() is True


def test_apu_guard_honors_hip_visible_devices_mask(monkeypatch):
    # HIP's mask shows the APU as ordinal 0 but physical id 1, so [1] must still match.
    monkeypatch.delenv("CUDA_VISIBLE_DEVICES", raising = False)
    monkeypatch.delenv("ROCR_VISIBLE_DEVICES", raising = False)
    monkeypatch.setenv("HIP_VISIBLE_DEVICES", "1")
    monkeypatch.setitem(sys.modules, "torch", _fake_torch("6.2.0", ["gfx1151"]))
    assert LlamaCppBackend._amd_apu_wants_unified_memory([1]) is True
    assert LlamaCppBackend._amd_apu_wants_unified_memory([0]) is False


def test_cpu_no_cuda_returns_false(monkeypatch):
    monkeypatch.setitem(sys.modules, "torch", _fake_torch("6.2.0", [], cuda_ok = False))
    assert LlamaCppBackend._amd_apu_wants_unified_memory() is False


def test_missing_torch_returns_false(monkeypatch):
    monkeypatch.setitem(sys.modules, "torch", None)
    assert LlamaCppBackend._amd_apu_wants_unified_memory() is False


_GB = 1024**3
_MIB_PER_GB = 1024
# Module-level (not a class attr) so it stays a plain function, not a bound method.
_shortfall = LlamaCppBackend._apu_ram_shortfall_message


class TestApuRamShortfall:
    """On a unified-memory APU, weights load into RAM, so an oversized model must be refused early."""

    def test_field_case_wsl_cap_refuses(self):
        msg = _shortfall(int(64.6 * _GB), 46 * _MIB_PER_GB)
        assert msg is not None
        assert "65 GB" in msg and "46 GB" in msg
        assert ".wslconfig" in msg

    def test_bare_metal_fits_allows(self):
        assert _shortfall(int(64.6 * _GB), 92 * _MIB_PER_GB) is None

    def test_unknown_available_never_refuses(self):
        assert _shortfall(int(64.6 * _GB), None) is None

    def test_boundary_at_headroom(self):
        assert _shortfall(20 * _GB, 23 * _MIB_PER_GB) is None
        assert _shortfall(20 * _GB, 21 * _MIB_PER_GB) is not None

    def test_available_system_memory_is_int_or_none(self):
        v = LlamaCppBackend._available_system_memory_mib()
        assert v is None or (isinstance(v, int) and v > 0)


# Real B200 profile; the host reserve is an absolute 1 GiB off free, not a percent of total.
_B200_TOTAL_MIB = 183359
_B200_FREE_MIB = 181928
_MIB = 1024 * 1024


def _fake_torch_with_memory(
    hip,
    archs,
    free_mib,
    total_mib,
    *,
    cuda_ok = True,
):
    """_fake_torch plus mem_get_info, which the memory probe needs."""
    t = _fake_torch(hip, archs, cuda_ok = cuda_ok)
    t.cuda.mem_get_info = lambda i: (free_mib * _MIB, total_mib * _MIB)
    return t


def _probe(
    monkeypatch,
    hip,
    archs,
    free_mib = _B200_FREE_MIB,
    total_mib = _B200_TOTAL_MIB,
):
    for _m in ("HIP_VISIBLE_DEVICES", "ROCR_VISIBLE_DEVICES", "CUDA_VISIBLE_DEVICES"):
        monkeypatch.delenv(_m, raising = False)
    monkeypatch.setitem(
        sys.modules, "torch", _fake_torch_with_memory(hip, archs, free_mib, total_mib)
    )
    monkeypatch.setattr(LlamaCppBackend, "_is_vulkan_backend", staticmethod(lambda b: False))
    monkeypatch.setattr(LlamaCppBackend, "_find_llama_server_binary", staticmethod(lambda: "x"))
    # Pin host RAM: the shared path caps by it.
    monkeypatch.setattr(
        LlamaCppBackend, "_available_system_memory_mib", staticmethod(lambda: 1 << 30)
    )
    monkeypatch.setattr(
        "core.inference.llama_cpp.subprocess.run",
        lambda *a, **k: (_ for _ in ()).throw(FileNotFoundError("no nvidia-smi")),
    )
    return LlamaCppBackend._get_gpu_memory()


class TestTheRocmProbeReservesHostRamOnAnApu:
    """ROCm reports no iGPU flag, so APUs were sized as discrete; the Vulkan probe already handled this."""

    def test_an_apu_loses_the_host_reserve_and_reports_no_total(self, monkeypatch):
        assert _probe(monkeypatch, "6.2.0", ["gfx1151:xnack-"]) == [(0, _B200_FREE_MIB - 1024, 0)]

    def test_a_discrete_amd_card_is_untouched(self, monkeypatch):
        assert _probe(monkeypatch, "6.2.0", ["gfx1100"]) == [(0, _B200_FREE_MIB, _B200_TOTAL_MIB)]

    def test_nvidia_through_the_torch_fallback_is_untouched(self, monkeypatch):
        """The real local GPU: no HIP, so nothing here may apply."""
        assert _probe(monkeypatch, None, ["sm_100"]) == [(0, _B200_FREE_MIB, _B200_TOTAL_MIB)]

    def test_a_mixed_host_only_reserves_on_the_apu(self, monkeypatch):
        assert _probe(monkeypatch, "6.2.0", ["gfx1100", "gfx1151"]) == [
            (0, _B200_FREE_MIB, _B200_TOTAL_MIB),
            (1, _B200_FREE_MIB - 1024, 0),
        ]

    def test_the_reserve_cannot_go_negative(self, monkeypatch):
        assert _probe(monkeypatch, "6.2.0", ["gfx1151"], free_mib = 512, total_mib = 512) == [(0, 0, 0)]

    @pytest.mark.parametrize("arch", ["gfx1150", "gfx1151", "gfx1152"])
    def test_every_unified_arch_is_covered(self, monkeypatch, arch):
        assert _probe(monkeypatch, "6.2.0", [arch])[0][1] == _B200_FREE_MIB - 1024

    def test_the_probe_and_the_mlock_gate_agree(self, monkeypatch):
        """Both read one arch map, so they cannot disagree about a device."""
        for arch, shared in (("gfx1151", True), ("gfx1100", False)):
            rows = _probe(monkeypatch, "6.2.0", [arch])
            assert (rows[0][2] == 0) is shared
            assert LlamaCppBackend._amd_apu_wants_unified_memory([0]) is shared


class TestTheGateStillFailsOpen:
    """Load-path helpers answer False rather than raise, so a bad argument skips the optimisation."""

    @pytest.mark.parametrize("gpu_indices", [5, [[0]], "0", [None], object()])
    def test_a_bad_gpu_indices_answers_false(self, monkeypatch, gpu_indices):
        monkeypatch.setitem(sys.modules, "torch", _fake_torch("6.2.0", ["gfx1151"]))
        assert LlamaCppBackend._amd_apu_wants_unified_memory(gpu_indices) is False

    def test_a_good_one_still_works(self, monkeypatch):
        monkeypatch.setitem(sys.modules, "torch", _fake_torch("6.2.0", ["gfx1151"]))
        assert LlamaCppBackend._amd_apu_wants_unified_memory([0]) is True


class TestAmdSdkWheelsCountAsRocm:
    """AMD SDK wheels leave torch.version.hip unset; the arch map must use the same predicate."""

    @staticmethod
    def _torch(
        hip,
        version,
        archs = ("gfx1151",),
    ):
        t = _fake_torch(hip, list(archs))
        t.__version__ = version
        return t

    @pytest.mark.parametrize(
        ("hip", "version", "expected"),
        [
            ("6.2.0", "2.5.0+rocm6.2", True),
            (None, "2.11.0+rocm7.13", True),
            (None, "2.5.0+ROCm7.0", True),
            (None, "2.5.0+cu124", False),
            (None, "2.5.0", False),
        ],
    )
    def test_the_arch_map_matches_the_id_resolver(self, monkeypatch, hip, version, expected):
        monkeypatch.setitem(sys.modules, "torch", self._torch(hip, version))
        assert bool(LlamaCppBackend._rocm_unified_memory_gpu_ids()) is expected
        assert LlamaCppBackend._amd_apu_wants_unified_memory([0]) is expected


class TestTheApuBudgetIsCappedByHostRam:
    """Windows HIP without the SDK reports free==total (#7072), so the ROCm free
    figure cannot be trusted on a shared pool. System RAM is the real ceiling."""

    @staticmethod
    def _probe(monkeypatch, arch, free_mib, avail_mib):
        t = _fake_torch("6.2.0", [arch])
        t.cuda.mem_get_info = lambda i: (free_mib * 1024 * 1024, free_mib * 1024 * 1024)
        monkeypatch.setitem(sys.modules, "torch", t)
        for _m in ("HIP_VISIBLE_DEVICES", "ROCR_VISIBLE_DEVICES", "CUDA_VISIBLE_DEVICES"):
            monkeypatch.delenv(_m, raising = False)
        monkeypatch.setattr(
            LlamaCppBackend, "_available_system_memory_mib", staticmethod(lambda: avail_mib)
        )
        monkeypatch.setattr(LlamaCppBackend, "_is_vulkan_backend", staticmethod(lambda b: False))
        monkeypatch.setattr(
            LlamaCppBackend, "_find_llama_server_binary", staticmethod(lambda: "llama-server")
        )
        monkeypatch.setattr(
            "core.inference.llama_cpp.subprocess.run",
            lambda *a, **k: (_ for _ in ()).throw(FileNotFoundError("no nvidia-smi")),
        )
        return LlamaCppBackend._get_gpu_memory()

    def test_the_sentinel_is_capped(self, monkeypatch):
        assert self._probe(monkeypatch, "gfx1151", 100_000, 12_000) == [(0, 12_000 - 1024, 0)]

    def test_an_honest_smaller_free_wins(self, monkeypatch):
        """The cap is a ceiling, never a floor."""
        assert self._probe(monkeypatch, "gfx1151", 8_000, 64_000) == [(0, 8_000 - 1024, 0)]

    def test_unreadable_system_ram_keeps_the_old_answer(self, monkeypatch):
        assert self._probe(monkeypatch, "gfx1151", 100_000, None) == [(0, 100_000 - 1024, 0)]

    def test_a_discrete_card_is_never_capped(self, monkeypatch):
        assert self._probe(monkeypatch, "gfx1100", 100_000, 12_000) == [(0, 100_000, 100_000)]


class TestRadeonWheelsWithoutAnArchName:
    """Radeon wheels may expose no arch attribute, so this path shares the training worker's classifier."""

    @staticmethod
    def _torch(**props):
        t = _fake_torch("6.2.0", ["unused"])
        t.cuda.get_device_properties = lambda i: types.SimpleNamespace(**props)
        return t

    @pytest.mark.parametrize(
        ("props", "expected"),
        [
            ({"gcnArchName": "gfx1151"}, True),
            ({"gcn_arch_name": "gfx1150"}, True),
            ({"name": "AMD Radeon 8060S Graphics"}, True),
            ({"name": "AMD Radeon 860M"}, True),
            ({"is_integrated": True, "name": "AMD Radeon Graphics"}, True),
            ({"gcnArchName": "gfx1100"}, False),
            ({"name": "AMD Radeon RX 7900 XTX"}, False),
        ],
    )
    def test_the_probe_uses_every_fallback(self, monkeypatch, props, expected):
        monkeypatch.setitem(sys.modules, "torch", self._torch(**props))
        assert LlamaCppBackend._amd_apu_wants_unified_memory([0]) is expected
        assert bool(LlamaCppBackend._rocm_unified_memory_gpu_ids()) is expected


class TestTheProbeTestsDoNotDependOnHostRam:
    """The shared path caps by available RAM, so the spoofed profile must pin availability."""

    def test_the_helper_pins_availability(self, monkeypatch):
        """_probe must survive a runner smaller than the spoofed free figure."""
        monkeypatch.setattr(
            LlamaCppBackend, "_available_system_memory_mib", staticmethod(lambda: 14_000)
        )
        assert _probe(monkeypatch, "6.2.0", ["gfx1151"]) == [(0, _B200_FREE_MIB - 1024, 0)]

    def test_without_the_pin_the_cap_really_would_bite(self, monkeypatch):
        """Proves the pin is load-bearing rather than decorative."""
        rows = _probe(monkeypatch, "6.2.0", ["gfx1151"])
        monkeypatch.setattr(
            LlamaCppBackend, "_available_system_memory_mib", staticmethod(lambda: 14_000)
        )
        capped = LlamaCppBackend._get_gpu_memory()
        assert rows == [(0, _B200_FREE_MIB - 1024, 0)]
        assert capped == [(0, 14_000 - 1024, 0)]


class TestTheOptOutHelper:
    """Decides when to make the var absent: ggml tests presence of the variable, not its value."""

    @pytest.mark.parametrize(
        "env,expected",
        [
            ({}, False),
            ({"GGML_CUDA_ENABLE_UNIFIED_MEMORY": "1"}, False),
            ({"GGML_CUDA_ENABLE_UNIFIED_MEMORY": "2"}, False),  # any value is ON to ggml
            ({"GGML_CUDA_ENABLE_UNIFIED_MEMORY": "true"}, False),
            ({"GGML_CUDA_ENABLE_UNIFIED_MEMORY": "0"}, True),  # the reported trap
            ({"GGML_CUDA_ENABLE_UNIFIED_MEMORY": ""}, True),
            ({"GGML_CUDA_ENABLE_UNIFIED_MEMORY": " Off "}, True),
            ({"GGML_CUDA_ENABLE_UNIFIED_MEMORY": "no"}, True),
            ({"GGML_CUDA_ENABLE_UNIFIED_MEMORY": "false"}, True),
            ({"UNSLOTH_DISABLE_UNIFIED_MEMORY": "1"}, True),
            ({"UNSLOTH_DISABLE_UNIFIED_MEMORY": "0"}, False),
            ({"UNSLOTH_DISABLE_UNIFIED_MEMORY": "yes"}, False),
            (
                {
                    "UNSLOTH_DISABLE_UNIFIED_MEMORY": "1",
                    "GGML_CUDA_ENABLE_UNIFIED_MEMORY": "1",
                },
                True,
            ),
        ],
    )
    def test_opt_out_decisions(self, env, expected):
        assert LlamaCppBackend._unified_memory_opted_out(env) is expected

    def test_none_reads_the_process_env(self, monkeypatch):
        """The default arg is the process env, so a shell export is honoured."""
        monkeypatch.delenv("GGML_CUDA_ENABLE_UNIFIED_MEMORY", raising = False)
        monkeypatch.setenv("UNSLOTH_DISABLE_UNIFIED_MEMORY", "1")
        assert LlamaCppBackend._unified_memory_opted_out() is True

    def test_a_hostile_env_fails_open(self):
        """Fails open: a bad env must not block a load. False is pre-#8651."""

        class _Exploding(dict):
            def get(self, *_args, **_kwargs):
                raise RuntimeError("no")

        assert LlamaCppBackend._unified_memory_opted_out(_Exploding()) is False


def _fake_torch_sized(specs, *, hip = "6.2.0"):
    """Build fake ROCm devices from ``(arch, total_bytes)`` specs."""
    t = types.ModuleType("torch")
    t.version = types.SimpleNamespace(hip = hip)
    t.cuda = types.SimpleNamespace(
        is_available = lambda: True,
        device_count = lambda: len(specs),
        get_device_properties = lambda i: types.SimpleNamespace(
            gcnArchName = specs[i][0], total_memory = specs[i][1]
        ),
    )
    return t


_GIB = 1024**3


class TestTheUnifiedMemorySwapIsOnlyTakenWhenItPays:
    """Managed allocation only when host RAM is the larger pool; the pools alone decide."""

    def _host(self, monkeypatch, specs, ram_mib):
        monkeypatch.setitem(sys.modules, "torch", _fake_torch_sized(specs))
        monkeypatch.setattr(
            LlamaCppBackend, "_available_system_memory_mib", staticmethod(lambda: ram_mib)
        )

    def test_a_small_carve_out_still_gets_it(self, monkeypatch):
        self._host(monkeypatch, [("gfx1151", 16 * _GIB)], 117_000)
        assert LlamaCppBackend._unified_memory_would_help(need_bytes = 100 * _GIB) is True

    def test_a_carve_out_larger_than_host_ram_does_not(self, monkeypatch):
        self._host(monkeypatch, [("gfx1151", 64 * _GIB)], 58_880)
        assert LlamaCppBackend._unified_memory_would_help(need_bytes = 100 * _GIB) is False

    def test_an_exact_tie_does_not(self, monkeypatch):
        self._host(monkeypatch, [("gfx1151", 32 * _GIB)], 32 * 1024)
        assert LlamaCppBackend._unified_memory_would_help(need_bytes = 100 * _GIB) is False

    def test_a_discrete_card_is_never_offered_it(self, monkeypatch):
        self._host(monkeypatch, [("gfx1100", 16 * _GIB)], 117_000)
        assert LlamaCppBackend._unified_memory_would_help(need_bytes = 100 * _GIB) is False

    def test_unreadable_host_ram_fails_closed(self, monkeypatch):
        self._host(monkeypatch, [("gfx1151", 16 * _GIB)], None)
        assert LlamaCppBackend._unified_memory_would_help(need_bytes = 100 * _GIB) is False

    def test_an_unreported_pool_size_fails_closed(self, monkeypatch):
        monkeypatch.setitem(sys.modules, "torch", _fake_torch("6.2.0", ["gfx1151"]))
        monkeypatch.setattr(
            LlamaCppBackend, "_available_system_memory_mib", staticmethod(lambda: 117_000)
        )
        assert LlamaCppBackend._rocm_selected_pool_mib() is None
        assert LlamaCppBackend._unified_memory_would_help(need_bytes = 100 * _GIB) is False

    def test_a_missing_torch_fails_closed(self, monkeypatch):
        monkeypatch.setitem(sys.modules, "torch", None)
        assert LlamaCppBackend._rocm_selected_pool_mib() is None
        assert LlamaCppBackend._unified_memory_would_help(need_bytes = 100 * _GIB) is False

    def test_two_apus_are_weighed_together(self, monkeypatch):
        self._host(monkeypatch, [("gfx1151", 8 * _GIB), ("gfx1150", 24 * _GIB)], 40_000)
        assert LlamaCppBackend._rocm_selected_pool_mib() == 32 * 1024
        assert LlamaCppBackend._unified_memory_would_help(need_bytes = 100 * _GIB) is True
        self._host(monkeypatch, [("gfx1151", 8 * _GIB), ("gfx1150", 24 * _GIB)], 30_000)
        assert LlamaCppBackend._unified_memory_would_help(need_bytes = 100 * _GIB) is False

    def test_the_answer_is_scoped_to_the_selected_gpu(self, monkeypatch):
        self._host(monkeypatch, [("gfx1151", 8 * _GIB), ("gfx1150", 64 * _GIB)], 40_000)
        assert LlamaCppBackend._unified_memory_would_help([0], need_bytes = 100 * _GIB) is True
        assert LlamaCppBackend._unified_memory_would_help([1], need_bytes = 100 * _GIB) is False

    def test_a_discrete_card_in_the_selection_fails_closed(self, monkeypatch):
        self._host(monkeypatch, [("gfx1151", 8 * _GIB), ("gfx1100", 64 * _GIB)], 40_000)
        assert LlamaCppBackend._rocm_selected_pool_mib() is None
        assert LlamaCppBackend._unified_memory_would_help(need_bytes = 100 * _GIB) is False

    def test_the_same_mixed_host_pinned_to_the_apu_still_gains(self, monkeypatch):
        self._host(monkeypatch, [("gfx1151", 8 * _GIB), ("gfx1100", 64 * _GIB)], 40_000)
        assert LlamaCppBackend._rocm_selected_pool_mib([0]) == 8 * 1024
        assert LlamaCppBackend._unified_memory_would_help([0], need_bytes = 100 * _GIB) is True

    def test_an_id_this_host_does_not_enumerate_fails_closed(self, monkeypatch):
        self._host(monkeypatch, [("gfx1151", 8 * _GIB)], 117_000)
        assert LlamaCppBackend._rocm_selected_pool_mib([0, 3]) is None
        assert LlamaCppBackend._unified_memory_would_help([0, 3], need_bytes = 100 * _GIB) is False

    def test_an_empty_selection_fails_closed(self, monkeypatch):
        self._host(monkeypatch, [("gfx1151", 8 * _GIB)], 117_000)
        assert LlamaCppBackend._rocm_selected_pool_mib([]) is None
        assert LlamaCppBackend._unified_memory_would_help([], need_bytes = 100 * _GIB) is False


class TestManagedMemoryIsTakenOnlyWhenTheWeightsOutgrowTheCarveOut:
    """The second condition: managed pages fault in k_set_rows on Linux ROCm gfx1151
    (HF Qwen3.8-Flash-Next-GGUF discussion 30, #10330), so a fitting load never takes them."""

    def _strix_halo(
        self,
        monkeypatch,
        carve_out = 64 * _GIB,
        ram_mib = 117_000,
    ):
        monkeypatch.setitem(sys.modules, "torch", _fake_torch_sized([("gfx1151", carve_out)]))
        monkeypatch.setattr(
            LlamaCppBackend, "_available_system_memory_mib", staticmethod(lambda: ram_mib)
        )

    def test_weights_that_fit_the_carve_out_do_not_get_it(self, monkeypatch):
        self._strix_halo(monkeypatch)
        assert LlamaCppBackend._unified_memory_would_help(need_bytes = 45 * _GIB) is False

    def test_weights_that_outgrow_it_do(self, monkeypatch):
        self._strix_halo(monkeypatch)
        assert LlamaCppBackend._unified_memory_would_help(need_bytes = 100 * _GIB) is True

    def test_weights_exactly_the_carve_out_do_not(self, monkeypatch):
        self._strix_halo(monkeypatch)
        assert LlamaCppBackend._unified_memory_would_help(need_bytes = 64 * _GIB) is False

    def test_one_byte_over_does(self, monkeypatch):
        """Bytes against bytes: flooring the need to MiB hid a sub-MiB overrun."""
        self._strix_halo(monkeypatch)
        assert LlamaCppBackend._unified_memory_would_help(need_bytes = 64 * _GIB + 1) is True

    def test_unpriced_weights_fail_closed(self, monkeypatch):
        self._strix_halo(monkeypatch)
        assert LlamaCppBackend._unified_memory_would_help() is False
        assert LlamaCppBackend._unified_memory_would_help(need_bytes = None) is False

    def test_the_pool_comparison_still_comes_first(self, monkeypatch):
        self._strix_halo(monkeypatch, carve_out = 96 * _GIB, ram_mib = 30_000)
        assert LlamaCppBackend._unified_memory_would_help(need_bytes = 120 * _GIB) is False


class TestTheEnableSwitch:
    """UNSLOTH_ENABLE_UNIFIED_MEMORY=1 takes managed allocation even when weights fit."""

    @pytest.mark.parametrize(
        "env, expected",
        [
            ({"UNSLOTH_ENABLE_UNIFIED_MEMORY": "1"}, True),
            ({"UNSLOTH_ENABLE_UNIFIED_MEMORY": " 1 "}, True),
            ({"UNSLOTH_ENABLE_UNIFIED_MEMORY": "0"}, False),
            ({"UNSLOTH_ENABLE_UNIFIED_MEMORY": "yes"}, False),
            ({}, False),
        ],
    )
    def test_opt_in_decisions(self, env, expected):
        assert LlamaCppBackend._unified_memory_opted_in(env) is expected

    def test_none_reads_the_process_env(self, monkeypatch):
        monkeypatch.delenv("UNSLOTH_ENABLE_UNIFIED_MEMORY", raising = False)
        assert LlamaCppBackend._unified_memory_opted_in() is False
        monkeypatch.setenv("UNSLOTH_ENABLE_UNIFIED_MEMORY", "1")
        assert LlamaCppBackend._unified_memory_opted_in() is True

    def test_a_hostile_env_fails_closed(self):
        class _Exploding:
            def get(self, *_a, **_k):
                raise RuntimeError("no")

        assert LlamaCppBackend._unified_memory_opted_in(_Exploding()) is False

    def _strix_halo(self, monkeypatch):
        monkeypatch.setitem(sys.modules, "torch", _fake_torch_sized([("gfx1151", 64 * _GIB)]))
        monkeypatch.setattr(
            LlamaCppBackend, "_available_system_memory_mib", staticmethod(lambda: 117_000)
        )

    def test_the_switch_takes_it_for_weights_that_fit(self, monkeypatch):
        self._strix_halo(monkeypatch)
        assert LlamaCppBackend._unified_memory_for_launch([0], 45 * _GIB) is False
        assert LlamaCppBackend._unified_memory_for_launch([0], 45 * _GIB, opted_in = True) is True

    def test_the_switch_never_reaches_a_discrete_card(self, monkeypatch):
        monkeypatch.setitem(sys.modules, "torch", _fake_torch_sized([("gfx1100", 16 * _GIB)]))
        monkeypatch.setattr(
            LlamaCppBackend, "_available_system_memory_mib", staticmethod(lambda: 117_000)
        )
        assert LlamaCppBackend._unified_memory_for_launch([0], 45 * _GIB, opted_in = True) is False

    def test_the_switch_refuses_a_mixed_selection(self, monkeypatch):
        monkeypatch.setitem(
            sys.modules,
            "torch",
            _fake_torch_sized([("gfx1151", 64 * _GIB), ("gfx1100", 16 * _GIB)]),
        )
        monkeypatch.setattr(
            LlamaCppBackend, "_available_system_memory_mib", staticmethod(lambda: 117_000)
        )
        assert LlamaCppBackend._unified_memory_for_launch([0, 1], 45 * _GIB, opted_in = True) is False
        assert LlamaCppBackend._unified_memory_for_launch(None, 45 * _GIB, opted_in = True) is False
        assert LlamaCppBackend._unified_memory_for_launch([0], 45 * _GIB, opted_in = True) is True

    def test_without_the_switch_the_priced_gate_decides(self, monkeypatch):
        self._strix_halo(monkeypatch)
        assert LlamaCppBackend._unified_memory_for_launch([0], 100 * _GIB) is True
        assert LlamaCppBackend._unified_memory_for_launch([0], None) is False


class TestTheDirectIoGateNeedsEveryDeviceRead:
    """Every device must be read before DirectIO is chosen, or an unreadable APU reads as discrete."""

    @staticmethod
    def _torch(archs, *, raises_on = None):
        t = _fake_torch("6.2.0", list(archs))

        def props(i):
            if raises_on is not None and i == raises_on:
                raise RuntimeError("HIP error: invalid device ordinal")
            return types.SimpleNamespace(gcnArchName = archs[i])

        t.cuda.get_device_properties = props
        return t

    def test_every_device_readable_answers(self, monkeypatch):
        monkeypatch.setitem(sys.modules, "torch", self._torch(["gfx1100", "gfx1201"]))
        assert LlamaCppBackend._rocm_classification_answered() is True

    def test_one_unreadable_device_declines(self, monkeypatch):
        """A discrete card beside an unreadable device empties the set and looks like two discrete
        cards, yet the unreadable one may be the APU."""
        monkeypatch.setitem(sys.modules, "torch", self._torch(["gfx1100", "gfx1151"], raises_on = 1))
        assert LlamaCppBackend._rocm_unified_memory_gpu_ids() == set()
        assert LlamaCppBackend._rocm_classification_answered() is False

    def test_the_only_device_being_unreadable_declines(self, monkeypatch):
        monkeypatch.setitem(sys.modules, "torch", self._torch(["gfx1151"], raises_on = 0))
        assert LlamaCppBackend._rocm_classification_answered() is False

    def test_no_devices_declines(self, monkeypatch):
        monkeypatch.setitem(sys.modules, "torch", self._torch([]))
        assert LlamaCppBackend._rocm_classification_answered() is False

    def test_non_rocm_torch_declines(self, monkeypatch):
        """No Windows ROCm wheel exists, so the DirectIO decision declines here."""
        monkeypatch.setitem(sys.modules, "torch", _fake_torch(None, ["sm_90"]))
        assert LlamaCppBackend._rocm_classification_answered() is False