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.

19.3 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

from __future__ import annotations

import sys
import threading
from pathlib import Path
from types import SimpleNamespace

_REPO_ROOT = Path(__file__).resolve().parents[2]
if str(_REPO_ROOT) not in sys.path:
    sys.path.insert(0, str(_REPO_ROOT))

import pytest
import typer
from typer.testing import CliRunner

import unsloth_cli
import unsloth_cli.commands.doctor as doctor_mod


def _gpu(
    index,
    name = "Tesla T4",
    cap = "7.5",
    pci = None,
):
    return {"index": index, "name": name, "capability": cap, "memory_gib": 15.0, "pci": pci}


def _info(*gpus):
    return {"torch": "2.x", "cuda": "12.4", "hip": None, "nccl": "2.21.5", "gpus": list(gpus)}


def _smoke(ok, hung = False):
    return {"ok": ok, "hung": hung, "busbw_gbps": 5.0 if ok else None, "log": ""}


def test_doctor_is_registered_on_the_cli():
    result = CliRunner().invoke(unsloth_cli.app, ["doctor", "--help"])
    assert result.exit_code == 0, result.output
    assert "--gpus" in result.output


def test_verdict_no_gpu_and_single_gpu():
    healthy, hints = doctor_mod.verdict(_info(), None, None, None, None)
    assert not healthy and "No GPU" in hints[0]
    healthy, hints = doctor_mod.verdict(_info(_gpu(0)), None, None, None, None)
    assert healthy and "skipped" in hints[0]


def test_verdict_p2p_corruption_recommends_nccl_p2p_disable():
    p2p = {(0, 1): "corrupt", (1, 0): "ok"}
    healthy, hints = doctor_mod.verdict(_info(_gpu(0), _gpu(1)), None, p2p, _smoke(True), None)
    assert not healthy
    assert any("NCCL_P2P_DISABLE=1" in h and "(0, 1)" in h for h in hints)


def test_verdict_smoke_fixed_by_disabling_p2p():
    healthy, hints = doctor_mod.verdict(
        _info(_gpu(0), _gpu(1)), None, None, _smoke(False, hung = True), _smoke(True)
    )
    assert not healthy
    assert any("hung" in h and "passed with NCCL_P2P_DISABLE=1" in h for h in hints)


def test_verdict_smoke_failing_regardless_points_at_debug_env():
    healthy, hints = doctor_mod.verdict(
        _info(_gpu(0), _gpu(1)), None, None, _smoke(False), _smoke(False)
    )
    assert not healthy
    assert any("UNSLOTH_DEBUG_DISTRIBUTED=1" in h for h in hints)


@pytest.mark.parametrize("level", ["PHB", "NODE", "SYS"])
def test_verdict_pcie_host_topology_prefers_ddp_for_lora(level):
    topo = {(0, 1): {"level": level, "nvlink": False, "p2p_read": True}}
    healthy, hints = doctor_mod.verdict(_info(_gpu(0), _gpu(1)), topo, {}, _smoke(True), None)
    assert healthy
    assert any("prefer DDP" in h for h in hints)


def test_verdict_nvlink_and_mixed_gpus():
    topo = {(0, 1): {"level": "SYS", "nvlink": True, "p2p_read": True}}
    healthy, hints = doctor_mod.verdict(
        _info(_gpu(0), _gpu(1, name = "NVIDIA L4", cap = "8.9")), topo, {}, _smoke(True), None
    )
    assert healthy
    assert not any("prefer DDP" in h for h in hints)
    assert any("NVLinked" in h for h in hints)
    assert any("compute capability" in h for h in hints)


class _FakeNvml:
    NVML_P2P_CAPS_INDEX_READ = 0
    NVML_P2P_CAPS_INDEX_NVLINK = 2
    NVML_P2P_STATUS_OK = 0

    def __init__(self):
        self.shutdown = False

    def nvmlInit(self):
        pass

    def nvmlShutdown(self):
        self.shutdown = True

    def nvmlDeviceGetHandleByPciBusId(self, bus):
        return bus.decode()

    def nvmlDeviceGetTopologyCommonAncestor(self, a, b):
        return 50 if {a, b} == {"bus-a", "bus-c"} else 30

    def nvmlDeviceGetP2PStatus(self, a, b, idx):
        if idx == self.NVML_P2P_CAPS_INDEX_NVLINK:
            return 0 if {a, b} == {"bus-a", "bus-b"} else 1
        return 0


def test_nvml_topology_keys_by_torch_index_via_pci_bus():
    nvml = _FakeNvml()
    gpus = [_gpu(0, pci = "bus-c"), _gpu(1, pci = "bus-a"), _gpu(2, pci = "bus-b")]
    topo, err = doctor_mod.nvml_topology(nvml, gpus)
    assert err is None
    assert topo[(0, 1)] == {"level": "SYS", "nvlink": False, "p2p_read": True}
    assert topo[(1, 2)] == {"level": "PHB", "nvlink": True, "p2p_read": True}
    assert nvml.shutdown


def test_nvml_topology_accepts_tuple_p2p_read_constant():
    # nvidia-ml-py ships `NVML_P2P_CAPS_INDEX_READ = 0,` (a tuple); its ctypes call rejects it.
    class TupleConstant(_FakeNvml):
        NVML_P2P_CAPS_INDEX_READ = (0,)

        def nvmlDeviceGetP2PStatus(self, a, b, idx):
            if not isinstance(idx, int):
                raise TypeError("Don't know how to convert parameter 3")
            return super().nvmlDeviceGetP2PStatus(a, b, idx)

    gpus = [_gpu(0, pci = "bus-a"), _gpu(1, pci = "bus-b")]
    topo, err = doctor_mod.nvml_topology(TupleConstant(), gpus)
    assert err is None
    assert topo[(0, 1)] == {"level": "PHB", "nvlink": True, "p2p_read": True}


def test_nvml_topology_absent_without_pynvml_or_pci():
    topo, err = doctor_mod.nvml_topology(None, [_gpu(0, pci = "a"), _gpu(1, pci = "b")])
    assert topo is None and "nvidia-ml-py" in err
    topo, err = doctor_mod.nvml_topology(_FakeNvml(), [_gpu(0), _gpu(1)])
    assert topo is None and "PCI" in err


def test_nvml_topology_survives_a_refused_query():
    class Refusing(_FakeNvml):
        def nvmlDeviceGetP2PStatus(self, a, b, idx):
            raise RuntimeError("Not Supported")

    topo, err = doctor_mod.nvml_topology(Refusing(), [_gpu(0, pci = "bus-a"), _gpu(1, pci = "bus-c")])
    assert err is None
    assert topo[(0, 1)] == {"level": "SYS", "nvlink": False, "p2p_read": False}


def test_allreduce_smoke_gloo_two_ranks():
    pytest.importorskip("torch.distributed")
    result = doctor_mod.allreduce_smoke(2, "gloo", timeout = 60, numel = 1024)
    assert result["ok"], result["log"]
    assert result["busbw_gbps"] is not None


def test_allreduce_smoke_reports_a_hang(monkeypatch):
    monkeypatch.setattr(doctor_mod, "_SMOKE_WORKER", "import time; time.sleep(60)")
    result = doctor_mod.allreduce_smoke(2, "gloo", timeout = -59, numel = 1024)
    assert result["hung"] and not result["ok"]


def _fake_torch(count):
    props = SimpleNamespace(
        name = "Tesla T4", major = 7, minor = 5, total_memory = 15 * 1024**3, pci_bus_id = None
    )
    cuda = SimpleNamespace(
        is_available = lambda: count > 0,
        device_count = lambda: count,
        get_device_properties = lambda i: props,
        can_device_access_peer = lambda a, b: False,
        nccl = SimpleNamespace(version = lambda: (2, 21, 5)),
    )
    return SimpleNamespace(
        __version__ = "2.x",
        version = SimpleNamespace(cuda = "12.4", hip = None),
        cuda = cuda,
        distributed = SimpleNamespace(is_nccl_available = lambda: True),
    )


def _run_doctor(
    monkeypatch,
    torch,
    smoke_results,
    p2p_disable = None,
):
    monkeypatch.setitem(sys.modules, "torch", torch)
    # These tests cover the CUDA path, so keep the doctor off the MLX path.
    monkeypatch.setattr(doctor_mod, "mlx_backend_active", lambda: False)
    monkeypatch.setattr(doctor_mod, "_load_pynvml", lambda: None)
    monkeypatch.setattr(doctor_mod, "driver_version", lambda pynvml = None: "550.1")
    monkeypatch.setattr(doctor_mod, "p2p_copy_probe", lambda t, n: {(0, 1): "ok", (1, 0): "ok"})
    calls = []

    def fake_smoke(
        world,
        backend,
        timeout,
        extra_env = None,
    ):
        calls.append((world, backend, extra_env))
        return smoke_results[len(calls) - 1]

    monkeypatch.setattr(doctor_mod, "allreduce_smoke", fake_smoke)
    monkeypatch.delenv("NCCL_P2P_DISABLE", raising = False)
    if p2p_disable is not None:
        monkeypatch.setenv("NCCL_P2P_DISABLE", p2p_disable)
    cli = typer.Typer()
    cli.command()(doctor_mod.doctor)
    return CliRunner().invoke(cli, ["--gpus"]), calls


def test_doctor_two_gpus_healthy(monkeypatch):
    result, calls = _run_doctor(monkeypatch, _fake_torch(2), [_smoke(True)])
    assert result.exit_code == 0, result.output
    assert calls == [(2, "nccl", None)]
    assert "driver 550.1" in result.output
    assert "Verdict: OK" in result.output


def test_doctor_retries_smoke_without_p2p(monkeypatch):
    result, calls = _run_doctor(monkeypatch, _fake_torch(2), [_smoke(False), _smoke(True)])
    assert result.exit_code == 1, result.output
    assert calls[1] == (2, "nccl", {"NCCL_P2P_DISABLE": "1"})
    assert "PROBLEMS FOUND" in result.output
    assert "passed with NCCL_P2P_DISABLE=1" in result.output


def test_doctor_without_gpus_exits_nonzero(monkeypatch):
    result, calls = _run_doctor(monkeypatch, _fake_torch(0), [])
    assert result.exit_code == 1 and calls == []
    assert "No GPU" in result.output


def test_doctor_without_nccl_is_unverified_not_a_cpu_gloo_pass(monkeypatch):
    torch = _fake_torch(2)
    torch.distributed = SimpleNamespace(is_nccl_available = lambda: False)
    result, calls = _run_doctor(monkeypatch, torch, [_smoke(True)])
    assert calls == []
    assert result.exit_code == 1, result.output
    assert "no NCCL backend" in result.output and "Verdict: OK" not in result.output


def test_p2p_allocation_failure_is_inconclusive_not_corruption():
    class OomTorch:
        float32 = "f32"

        @staticmethod
        def arange(*a, device, **k):
            if device == "cuda:1":
                raise RuntimeError("CUDA out of memory")
            return SimpleNamespace()

        full = arange
        cuda = SimpleNamespace(empty_cache = lambda: None)

    p2p = doctor_mod.p2p_copy_probe(OomTorch, 2, mib = 1)
    assert all(v.startswith("inconclusive") for v in p2p.values()), p2p
    healthy, hints = doctor_mod.verdict(_info(_gpu(0), _gpu(1)), None, p2p, _smoke(True), None)
    assert healthy
    assert not any("NCCL_P2P_DISABLE" in h for h in hints)
    assert any("not tested" in h for h in hints)


def test_rocm_wheel_without_version_hip_is_rocm():
    torch = _fake_torch(0)
    torch.__version__ = "2.5.1+rocm6.2"
    torch.version = SimpleNamespace(cuda = None, hip = None)
    assert doctor_mod.collect_gpus(torch)["hip"] == "6.2"


def test_allreduce_smoke_survives_a_chatty_nonzero_rank(monkeypatch, tmp_path):
    # Rank 1 writes far more than a pipe buffer (64 KiB) before rank 0 can finish.
    worker = (
        "import os, sys, time\n"
        "if os.environ['RANK'] == '1':\n"
        "    sys.stdout.write('x' * (4 << 20)); sys.stdout.flush(); sys.exit(0)\n"
        "while not os.path.exists(os.environ['DONE']): time.sleep(0.05)\n"
        "print('UNSLOTH_DOCTOR_RESULT ok=1 busbw_gbps=1.00', flush = True)\n"
    )
    done = tmp_path / "rank1_done"
    monkeypatch.setattr(
        doctor_mod, "_SMOKE_WORKER", worker.replace("os.environ['DONE']", repr(str(done)))
    )
    real_popen = doctor_mod.subprocess.Popen
    procs = []

    def popen(*a, **k):
        p = real_popen(*a, **k)
        procs.append(p)
        if len(procs) == 2:
            # Rank 0 only finishes once rank 1 has exited, like a collective would.
            threading.Thread(target = lambda: (p.wait(), done.touch()), daemon = True).start()
        return p

    monkeypatch.setattr(doctor_mod.subprocess, "Popen", popen)
    result = doctor_mod.allreduce_smoke(2, "gloo", timeout = -45, numel = 1024)
    assert result["ok"] and not result["hung"], result["log"][-300:]


def test_driver_version_initialises_nvml():
    class Nvml:
        ready = False

        def nvmlInit(self):
            self.ready = True

        def nvmlShutdown(self):
            self.ready = False

        def nvmlSystemGetDriverVersion(self):
            if not self.ready:
                raise RuntimeError("Uninitialized")
            return b"580.65"

    nvml = Nvml()
    assert doctor_mod.driver_version(nvml) == "580.65"
    assert not nvml.ready


def test_nvml_topology_falls_back_to_uuid_without_pci_bus_id():
    class ByUuid(_FakeNvml):
        def nvmlDeviceGetHandleByPciBusId(self, bus):
            raise AssertionError("no PCI id to look up")

        def nvmlDeviceGetHandleByUUID(self, uuid):
            return {b"GPU-u0": "bus-a", b"GPU-u1": "bus-c"}[uuid]

    torch = _fake_torch(2)
    props = SimpleNamespace(name = "Tesla T4", major = 7, minor = 5, total_memory = 15 * 1024**3)
    torch.cuda.get_device_properties = lambda i: SimpleNamespace(**vars(props), uuid = f"u{i}")
    gpus = doctor_mod.collect_gpus(torch)["gpus"]
    assert [g["pci"] for g in gpus] == [None, None]
    topo, err = doctor_mod.nvml_topology(ByUuid(), gpus)
    assert err is None
    assert topo[(0, 1)]["level"] == "SYS"


def test_allreduce_smoke_kills_started_ranks_when_a_spawn_fails(monkeypatch):
    real_popen = doctor_mod.subprocess.Popen
    started = []

    def flaky_popen(*args, **kwargs):
        if started:
            raise OSError("Resource temporarily unavailable")
        started.append(real_popen(*args, **kwargs))
        return started[-1]

    monkeypatch.setattr(doctor_mod, "_SMOKE_WORKER", "import time; time.sleep(60)")
    monkeypatch.setattr(doctor_mod.subprocess, "Popen", flaky_popen)
    result = doctor_mod.allreduce_smoke(2, "gloo", timeout = 60, numel = 1024)
    assert not result["ok"] and not result["hung"]
    assert "could not start rank 1" in result["log"]
    assert started[0].returncode is not None


def test_doctor_reports_a_torch_that_fails_to_load(monkeypatch):
    import builtins

    real_import = builtins.__import__

    def broken(name, *args, **kwargs):
        if name == "torch":
            raise OSError("libcudnn.so.9: cannot open shared object file")
        return real_import(name, *args, **kwargs)

    monkeypatch.delitem(sys.modules, "torch", raising = False)
    monkeypatch.setattr(builtins, "__import__", broken)
    # This test is about the CUDA path's torch import, so keep the doctor off the MLX path.
    monkeypatch.setattr(doctor_mod, "mlx_backend_active", lambda: False)
    cli = typer.Typer()
    cli.command()(doctor_mod.doctor)
    result = CliRunner().invoke(cli, ["--gpus"])
    assert result.exit_code == 1
    assert result.exception is None or isinstance(result.exception, SystemExit)
    assert "libcudnn" in result.output and "PROBLEMS FOUND" in result.output


@pytest.mark.parametrize("value, retried", [("0", True), ("", True), ("1", False)])
def test_doctor_retry_honours_the_inherited_nccl_p2p_disable_value(monkeypatch, value, retried):
    result, calls = _run_doctor(
        monkeypatch, _fake_torch(2), [_smoke(False), _smoke(True)], p2p_disable = value
    )
    assert result.exit_code == 1, result.output
    assert len(calls) == (2 if retried else 1)


def test_nvml_topology_requires_p2p_read_in_both_directions():
    class OneWay(_FakeNvml):
        def nvmlDeviceGetP2PStatus(self, a, b, idx):
            if idx == self.NVML_P2P_CAPS_INDEX_READ:
                return 0 if (a, b) == ("bus-a", "bus-c") else 1
            return 1

    gpus = [_gpu(0, pci = "bus-a"), _gpu(1, pci = "bus-c")]
    topo, err = doctor_mod.nvml_topology(OneWay(), gpus)
    assert err is None
    assert topo[(0, 1)]["p2p_read"] is False


def test_allreduce_smoke_reaps_ranks_when_the_wait_is_interrupted(monkeypatch):
    real_popen = doctor_mod.subprocess.Popen
    started = []

    def popen(*args, **kwargs):
        started.append(real_popen(*args, **kwargs))
        return started[-1]

    interrupted = []

    def interrupted_wait(self, timeout = None):
        if timeout is not None and not interrupted:
            interrupted.append(True)
            raise KeyboardInterrupt
        return real_wait(self, timeout = timeout)

    real_wait = doctor_mod.subprocess.Popen.wait
    monkeypatch.setattr(doctor_mod, "_SMOKE_WORKER", "import time; time.sleep(60)")
    monkeypatch.setattr(doctor_mod.subprocess, "Popen", popen)
    monkeypatch.setattr(real_popen, "wait", interrupted_wait)
    with pytest.raises(KeyboardInterrupt):
        doctor_mod.allreduce_smoke(2, "gloo", timeout = 60, numel = 1024)
    assert len(started) == 2
    assert all(p.poll() is not None for p in started)


def test_peer_copy_probe_has_a_deadline(monkeypatch):
    release = threading.Event()

    def wedged(torch, count, out, active):
        out[(0, 1)] = "ok"
        active.append((0, 2))
        release.wait(30)
        return out

    monkeypatch.setattr(doctor_mod, "p2p_copy_probe", wedged)
    out, hung = doctor_mod.p2p_copy_probe_with_deadline(None, 3, 0.2)
    release.set()
    assert hung and len(out) == 6
    # Finished pairs keep their result; only the wedged pair is an error, the rest untested.
    assert out[(0, 1)] == "ok"
    assert out[(0, 2)].startswith("error:") and "hung" in out[(0, 2)]
    assert all(v.startswith("inconclusive") for k, v in out.items() if k not in ((0, 1), (0, 2)))
    healthy, hints = doctor_mod.verdict(
        _info(_gpu(0), _gpu(1), _gpu(2)), None, out, _smoke(True), None
    )
    assert not healthy and any("[(0, 2)]" in h and "NCCL_P2P_DISABLE" in h for h in hints)


def test_peer_copy_probe_crash_is_inconclusive_not_ok(monkeypatch):
    def crashing(torch, count, out, active):
        out[(0, 1)] = "ok"
        raise RuntimeError("CUDA error: an illegal memory access was encountered")

    monkeypatch.setattr(doctor_mod, "p2p_copy_probe", crashing)
    out, hung = doctor_mod.p2p_copy_probe_with_deadline(None, 2, 5)
    assert not hung and out[(0, 1)] == "ok"
    assert out[(1, 0)].startswith("inconclusive") and "illegal memory access" in out[(1, 0)]
    _, hints = doctor_mod.verdict(_info(_gpu(0), _gpu(1)), None, out, _smoke(True), None)
    assert any("not tested" in h and "illegal memory access" in h for h in hints)


def test_doctor_reports_a_gpu_enumeration_failure(monkeypatch):
    torch = _fake_torch(2)

    def fault(i):
        raise RuntimeError("CUDA error: unspecified launch failure")

    torch.cuda.get_device_properties = fault
    result, _ = _run_doctor(monkeypatch, torch, [_smoke(True)])
    assert result.exit_code == 1
    assert result.exception is None or isinstance(result.exception, SystemExit)
    assert "GPU enumeration failed" in result.output and "PROBLEMS FOUND" in result.output


def _non_cuda_torch(
    mps = False,
    xpu = False,
    cuda = None,
    hip = None,
):
    torch = _fake_torch(0)
    torch.__version__ = "2.x"
    torch.version = SimpleNamespace(cuda = cuda, hip = hip)
    torch.backends = SimpleNamespace(mps = SimpleNamespace(is_available = lambda: mps))
    torch.xpu = SimpleNamespace(is_available = lambda: xpu)
    return torch


@pytest.mark.parametrize(
    "mps, xpu, name",
    [(True, False, "Metal (MPS)"), (False, True, "Intel XPU")],
)
def test_doctor_on_mps_or_xpu_is_healthy(monkeypatch, mps, xpu, name):
    result, calls = _run_doctor(monkeypatch, _non_cuda_torch(mps = mps, xpu = xpu), [])
    assert result.exit_code == 0, result.output
    assert calls == []
    assert "Verdict: OK" in result.output
    assert f"{name} detected: multi-GPU CUDA checks skipped." in result.output


@pytest.mark.parametrize(
    "cuda, hip",
    [("12.4", None), (None, "6.2")],
)
def test_cuda_or_rocm_build_without_a_device_still_fails_beside_xpu_or_mps(monkeypatch, cuda, hip):
    torch = _non_cuda_torch(mps = True, xpu = True, cuda = cuda, hip = hip)
    result, calls = _run_doctor(monkeypatch, torch, [])
    assert result.exit_code == 1 and calls == []
    assert "No GPU" in result.output


def test_cpu_build_without_mps_or_xpu_still_fails(monkeypatch):
    result, _ = _run_doctor(monkeypatch, _non_cuda_torch(), [])
    assert result.exit_code == 1 and "No GPU" in result.output


def test_a_raising_accelerator_probe_is_no_accelerator():
    torch = _non_cuda_torch()

    def boom():
        raise RuntimeError("level zero")

    torch.xpu = SimpleNamespace(is_available = boom)
    assert doctor_mod.collect_gpus(torch)["accelerator"] is None