Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion src/inferrsi/infra/platforms/mlx.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,13 @@ def _parse_sysctl_info(stdout: str) -> tuple[bool, str]:
return flag == "1", chip


def _model_names(chip: str) -> set[str]:
# A chip is selected by its whole name, with or without the "Apple" prefix,
# so "M1" or "Pro" never selects an "Apple M1 Pro".
name = " ".join(chip.casefold().split())
return {name, name.removeprefix("apple ")}


def inspect_devices(*, device_ids, device_model, command):
result = {"status": "unavailable", "tool": "sysctl", "selection_verified": False}
if device_ids:
Expand All @@ -36,7 +43,7 @@ def inspect_devices(*, device_ids, device_model, command):
result["chip"] = chip
if not arm64:
result["reason"] = "Host is not Apple Silicon"
elif device_model and device_model.casefold() not in chip.casefold():
elif device_model and " ".join(device_model.casefold().split()) not in _model_names(chip):
result["status"] = "device_model_mismatch"
result["reason"] = "Discovered chip does not match the requested device model"
else:
Expand Down
66 changes: 66 additions & 0 deletions tests/test_mlx_discovery.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
from inferrsi.cli import main
from inferrsi.infra.commands import probe_command
from inferrsi.infra.platforms import inspect_device
from inferrsi.infra.platforms import mlx as mlx_platform
from inferrsi.infra.platforms.mlx import _parse_sysctl_info
from inferrsi.knowledge_operations import KnowledgeReader

Expand Down Expand Up @@ -66,6 +67,71 @@ def test_actual_non_macos_doctor_reports_unavailable(capsys):
assert environment["compatibility"] == "unverified"


def _mocked_darwin(monkeypatch, *, returncode=0, stdout=""):
# Mocked software branches only; these cases claim no Apple hardware validation.
monkeypatch.setattr(mlx_platform.platform, "system", lambda: "Darwin")
return lambda argv: {"argv": argv, "returncode": returncode, "stdout": stdout, "stderr": ""}


M1_PRO = "hw.optional.arm64: 1\nmachdep.cpu.brand_string: Apple M1 Pro\n"


@pytest.mark.parametrize("device_model", [None, "Apple M1 Pro", "m1 pro", " M1 PRO "])
def test_mocked_darwin_apple_silicon_is_available(monkeypatch, device_model):
result = mlx_platform.inspect_devices(
device_ids=(), device_model=device_model, command=_mocked_darwin(monkeypatch, stdout=M1_PRO)
)
assert result["status"] == "available"
assert result["selection_verified"] is True
assert result["chip"] == "Apple M1 Pro"


@pytest.mark.parametrize("device_model", ["M1", "pro", "Apple M", "M1 Max", "M4"])
def test_mocked_darwin_partial_or_other_model_is_a_mismatch(monkeypatch, device_model):
result = mlx_platform.inspect_devices(
device_ids=(), device_model=device_model, command=_mocked_darwin(monkeypatch, stdout=M1_PRO)
)
assert result["status"] == "device_model_mismatch"
assert result["selection_verified"] is False
assert result["chip"] == "Apple M1 Pro"


def test_mocked_darwin_intel_chip_is_unavailable(monkeypatch):
stdout = "hw.optional.arm64: 0\nmachdep.cpu.brand_string: Intel Core i7\n"
result = mlx_platform.inspect_devices(
device_ids=(), device_model=None, command=_mocked_darwin(monkeypatch, stdout=stdout)
)
assert result["status"] == "unavailable"
assert result["reason"] == "Host is not Apple Silicon"
assert result["selection_verified"] is False


def test_mocked_darwin_failed_sysctl_is_unavailable(monkeypatch):
result = mlx_platform.inspect_devices(
device_ids=(), device_model=None, command=_mocked_darwin(monkeypatch, returncode=1)
)
assert result["status"] == "unavailable"
assert "sysctl" in result["reason"]
assert "chip" not in result


@pytest.mark.skipif(
platform.system() != "Darwin" or platform.machine() != "arm64",
reason="Requires a real Apple Silicon host",
)
def test_actual_apple_silicon_doctor_reports_available(capsys):
result = main([
"doctor", "--repo", str(PROJECT), "--repo-type", "vllm_omni",
"--backend", "mlx", "--python", sys.executable,
])
environment = json.loads(capsys.readouterr().out)
assert result == 0
assert environment["device"]["status"] == "available"
assert environment["device"]["selection_verified"] is True
assert environment["device"]["chip"].startswith("Apple M")
assert environment["compatibility"] == "unverified"


def test_discrete_ids_are_rejected_by_real_adapter():
result = inspect_device(
"mlx", device_ids=("0",), device_model=None, command=probe_command
Expand Down
Loading