Files
strata/tools/test_setup_mac.py
T
Niko1221 ae02e7db17 metal: vision - image rows in the prompt, M-RoPE (qsa_prep constant 24), setup's --vision
The prefill hook hands the prompt's image rows (sp.embd_rows) to the Metal prompt paths (prompt_chain.inc, the
streaming prompt path, the 8-row windows), which take an image row in place of the token embedding.
mrope_table_set keeps the host view of the server's (t, h, w) table; every window, prompt chunk and draft round copies
the rows it reads into the window's buffer, bound at index 20 of qsa_prep with function constant 24 (mac-kern
29724e5e) - the window, both prompt paths and the draft layer. The cached window's QsaArgs are patched by the last
inline argument (the table is bound after it). setup_mac.py --vision yes: the pinned projector (SHA-256) and
strata-vision built with ggml-metal; the config gets --vision and the encoder.
missing.cpp: a crash prints its call stack and fault address.

M5 Max, Q2_0, ISTA's projector, a test picture (shapes and 'STRATA 42'): 'STRATA 42' and 'Red circle / Blue square /
Green triangle', the same answers as the HIP engine on the R9700 (p620) with the same picture and files, also
through the config setup writes. Text greedy unchanged with and without --vision (prompt1, story, the 1,662-token
chain prompt). prefill.cpp: the CUDA (P1004) and HIP (p620) objects are byte-IDENTICAL (preprocessor-only).
2026-10-12 23:36:13 +02:00

138 lines
6.4 KiB
Python

"""Tests for setup on a Mac (metal/setup_mac.py and setup.py's hand-over): the Mac check, the pack recommended by
unified memory, what a bigger choice risks, the engine arguments with the Metal defaults and the config. Runs on any
PC: sysctl / sw_vers / xcode-select are mocked. No GPU, no network.
python -m unittest tools.test_setup_mac
"""
from __future__ import annotations
import subprocess
import sys
import unittest
from pathlib import Path
from unittest import mock
ROOT = Path(__file__).resolve().parents[1]
sys.path.insert(0, str(ROOT))
sys.path.insert(0, str(ROOT / "metal"))
import setup # noqa: E402
import setup_mac as M # noqa: E402
def fake_run(values: dict, clt_rc=0):
def run(cmd, **kw):
if cmd[0] == "sysctl":
return subprocess.CompletedProcess(cmd, 0, values.get(cmd[2], ""), "")
if cmd[0] == "sw_vers":
return subprocess.CompletedProcess(cmd, 0, values.get("macos", ""), "")
if cmd[0] == "xcode-select":
return subprocess.CompletedProcess(cmd, clt_rc, "/Library/Developer/CommandLineTools" if clt_rc == 0 else "",
"")
raise AssertionError(cmd)
return run
M5 = {"machdep.cpu.brand_string": "Apple M5 Max", "hw.memsize": str(128 * 2 ** 30), "macos": "27.2",
"hw.perflevel0.physicalcpu": "12"}
class MacCheck(unittest.TestCase):
def test_reads_chip_memory_macos_and_tools(self):
with mock.patch.object(M.platform, "machine", return_value="arm64"):
info = M.mac_info(fake_run(M5))
self.assertEqual(info["chip"], "Apple M5 Max")
self.assertAlmostEqual(info["ram_gb"], 128.0)
self.assertEqual(info["macos"], "27.2")
self.assertTrue(info["arm64"])
self.assertTrue(info["clt"])
self.assertEqual(M.mac_problems(info), [])
def test_intel_old_macos_and_missing_tools_are_named(self):
with mock.patch.object(M.platform, "machine", return_value="x86_64"):
info = M.mac_info(fake_run({**M5, "macos": "14.6", "machdep.cpu.brand_string": "Intel Core i9"}, clt_rc=2))
p = " | ".join(M.mac_problems(info))
self.assertIn("Apple Silicon", p)
self.assertIn("macOS 15", p)
self.assertIn("xcode-select --install", p)
class Recommendation(unittest.TestCase):
def test_by_memory(self):
self.assertEqual(M.recommend_model(128)[0], "IQ3_S")
self.assertEqual(M.recommend_model(96)[0], "IQ3_XXS")
self.assertEqual(M.recommend_model(64)[0], "Q2_0")
rec, notes = M.recommend_model(32)
self.assertEqual(rec, "Q2_0")
self.assertTrue(notes and "stream" in notes[0] and "~14 tok/s" in notes[0])
self.assertTrue(M.streams("Q2_0", 32) and M.streams("Q2_0", 48) and not M.streams("Q2_0", 64))
def test_a_bigger_choice_is_kept_with_a_note(self):
notes = M.model_note("IQ3_XXS", 64)
self.assertTrue(any("stream from the SSD" in n for n in notes))
self.assertEqual(M.model_note("Q2_0", 128), [])
self.assertEqual(M.model_note("IQ3_S", 128), [])
def test_context(self):
self.assertEqual(M.recommend_context(64), 32768)
self.assertEqual(M.recommend_context(128), 65536)
class Config(unittest.TestCase):
def test_engine_args_carry_the_metal_defaults(self):
args = M.engine_args(Path("/p/packs/q2_0"), [Path("/m/a-00001.gguf"), Path("/m/a-00002.gguf")],
Path("/d/mtp/rt"), 32768)
self.assertEqual(args[args.index("--spec") + 1], "2")
self.assertEqual(args[args.index("--native") + 1], str(Path("/m/a-00001.gguf")))
self.assertEqual(args[args.index("--ple-gguf") + 1], str(Path("/m/a-00002.gguf")))
self.assertEqual(args[args.index("--mtp") + 1], str(Path("/d/mtp/rt")))
self.assertIn("--expert-profile", args)
self.assertNotIn("--hc-q8", args) # Q8_0 HC is the engine's own default on Metal
st = M.engine_args(Path("/p"), [Path("/a1.gguf"), Path("/a2.gguf")], Path("/d/mtp/rt"), 32768, stream=True)
self.assertNotIn("--mtp", st) # streaming: one token per step, no draft layer
self.assertEqual(st[st.index("--suffix-draft") + 1], "0")
def test_config_is_a_model_config_with_the_metal_backend(self):
cfg = M.mac_config(Path("/r/build-metal/strata"), ["--pack", "x"], Path("/p"), "qwen3.8-flash-next-q2_0",
"Q2_0", 8080)
self.assertEqual(cfg["backend"], "metal")
self.assertEqual(cfg["tokenizer"], str(Path("/p") / "tokenizer"))
self.assertTrue(cfg["exe"] and isinstance(cfg["args"], list))
class Vision(unittest.TestCase):
def test_the_projector_is_pinned_and_the_encoder_builds_with_metal(self):
self.assertIn("mmproj-Qwen3.8-Flash-Next-BF16.gguf", M.VISION_SHA256)
src = (ROOT / "metal" / "setup_mac.py").read_text(encoding="utf-8")
self.assertIn('"-DSTRATA_VISION_CUDA=OFF"', src)
self.assertIn('cfg["args"] += ["--vision"]', src)
class HandOver(unittest.TestCase):
def test_darwin_hands_the_run_to_setup_mac(self):
with mock.patch.object(setup.sys, "platform", "darwin"), \
mock.patch.object(setup.subprocess, "call", return_value=0) as call, \
mock.patch.object(setup.sys, "argv", ["setup.py", "--model", "Q2_0", "--yes"]):
self.assertEqual(setup.main(), 0)
cmd = call.call_args[0][0]
self.assertTrue(cmd[1].endswith(str(Path("metal") / "setup_mac.py")))
self.assertEqual(cmd[2:], ["--model", "Q2_0", "--yes"])
def test_other_platforms_never_reach_it(self):
src = (ROOT / "setup.py").read_text(encoding="utf-8")
self.assertIn('if sys.platform == "darwin":', src)
with mock.patch.object(setup, "mac_setup", side_effect=AssertionError("called")), \
mock.patch.object(setup.sys, "platform", "linux"), \
mock.patch.object(setup.sys, "argv", ["setup.py", "--inspect", "x"]), \
mock.patch.object(setup.subprocess, "run", return_value=subprocess.CompletedProcess([], 0)):
with self.assertRaises(SystemExit):
setup.main()
def test_setup_sh_fetches_a_pinned_python_on_macos_only(self):
sh = (ROOT / "setup.sh").read_text(encoding="utf-8")
self.assertIn('[ "$(uname -s)" = Darwin ]', sh)
self.assertIn("83dcb04777c798bd95f3687acf9ae84b09860272ee6bdfb05be8bcf9447f75b8", sh)
if __name__ == "__main__":
unittest.main()