Minecraft-Friends-AI-ZeroGPU / tests /test_cpu_only_guarantee.py
Erinaldordso's picture
Prove CPU-only instead of asserting it, and do not probe the binary before it is safe
12e0e4a verified
Raw History Blame Contribute Delete
5.7 kB
"""Guarantee CPU-only operation with evidence, instead of a label that claims it.
The status used to publish `gpu_enabled: False` and `n_gpu_layers: 0` as
hardcoded literals. Nothing checked anything, so if the installed wheel had CUDA
compiled in and a load site ever asked for offload, the status would still have
said "cpu-only".
These tests execute the real function and inspect every load site, rather than
looking for a string in a file. One of them guards against a mistake that was
made and caught here: querying the linked binary before the backend is
initialised raises STATUS_ILLEGAL_INSTRUCTION, which on Linux is SIGILL and
cannot be caught.
"""
from __future__ import annotations
import ast
import sys
from pathlib import Path
ROOT = Path(__file__).resolve().parents[1]
sys.path.insert(0, str(ROOT))
import cpu_backend # noqa: E402
def _app_tree() -> ast.Module:
return ast.parse((ROOT / "app.py").read_text(encoding="utf-8"))
def _llama_calls() -> list[ast.Call]:
calls = []
for node in ast.walk(_app_tree()):
if not isinstance(node, ast.Call):
continue
func = node.func
name = func.id if isinstance(func, ast.Name) else getattr(func, "attr", None)
if name == "Llama":
calls.append(node)
return calls
# --- the report is real evidence -------------------------------------------
def test_cpu_only_report_executes_and_reports_evidence():
report = cpu_backend.cpu_only_report()
assert set(report) >= {
"cpu_only",
"gpu_offload_supported",
"gpu_backend_libraries",
"cuda_libraries_present",
"n_gpu_layers_requested",
}
assert isinstance(report["gpu_backend_libraries"], list)
assert isinstance(report["cuda_libraries_present"], list)
assert report["n_gpu_layers_requested"] == 0
def test_this_environment_is_cpu_only():
# llama-cpp-python is installed from the CPU wheel index, so no GPU backend
# library should be present.
report = cpu_backend.cpu_only_report()
assert report["cuda_libraries_present"] == [], report["cuda_libraries_present"]
assert report["cpu_only"] is True
def test_the_backend_libraries_are_the_cpu_ones():
libraries = cpu_backend.backend_libraries()
assert libraries, "no native backend libraries found; the check is not looking anywhere"
joined = " ".join(libraries).lower()
assert "ggml-cpu" in joined or "llama" in joined
# --- the crash guard: never query the binary before the backend is up ------
def test_the_binary_probe_is_not_called_by_default(monkeypatch):
# Measured: llama_supports_gpu_offload() before llama_backend_init() raises
# OSError [WinError -1073741795] = NTSTATUS 0xC000001D,
# STATUS_ILLEGAL_INSTRUCTION. On Linux that is SIGILL and takes the process
# down, so cpu_only_report() must not reach for it on its own.
def _must_not_be_called(*args, **kwargs):
raise AssertionError("the binary probe was called without being asked for")
monkeypatch.setattr(cpu_backend, "binary_offload_support", _must_not_be_called)
monkeypatch.setattr(cpu_backend, "BINARY_PROBE_ENABLED", False)
report = cpu_backend.cpu_only_report()
assert report["gpu_binary_probe_enabled"] is False
assert report["gpu_offload_supported"] == "not probed"
assert report["cpu_only"] is True
def test_the_binary_probe_never_raises():
# Even when explicitly asked for, a failure must be reported, not thrown.
result = cpu_backend.binary_offload_support()
assert isinstance(result, bool) or isinstance(result, str)
def test_the_binary_probe_is_opt_in():
assert cpu_backend.BINARY_PROBE_ENABLED is False
source = (ROOT / "cpu_backend.py").read_text(encoding="utf-8")
assert 'LLAMA_BINARY_PROBE", "0"' in source
# --- every load site must ask for zero offloaded layers --------------------
def test_every_model_load_asks_for_zero_gpu_layers():
calls = _llama_calls()
assert calls, "no Llama(...) construction found in app.py"
problems = []
for call in calls:
keywords = {kw.arg: kw.value for kw in call.keywords}
for name, expected in cpu_backend.REQUIRED_OFFLOAD_ARGUMENTS.items():
node = keywords.get(name)
if node is None:
problems.append(f"line {call.lineno}: {name} is not passed at all")
continue
if not isinstance(node, ast.Constant) or node.value != expected:
problems.append(f"line {call.lineno}: {name} is not {expected!r}")
assert not problems, "; ".join(problems)
def test_the_required_arguments_are_the_ones_this_deployment_needs():
assert cpu_backend.REQUIRED_OFFLOAD_ARGUMENTS == {
"n_gpu_layers": 0,
"offload_kqv": False,
}
# --- the status must derive the claim, not assert it -----------------------
def test_the_status_no_longer_hardcodes_the_cpu_only_claim():
source = (ROOT / "app.py").read_text(encoding="utf-8")
assert '"gpu_enabled": False' not in source, "the claim is a literal again"
assert '"cpu_only": _CPU_ONLY["cpu_only"]' in source
assert '"cuda_libraries_present": _CPU_ONLY["cuda_libraries_present"]' in source
def test_the_boot_log_publishes_the_evidence():
source = (ROOT / "app.py").read_text(encoding="utf-8")
assert "_CPU_ONLY = cpu_only_report()" in source
assert 'print(f"[cpu] {cpu_only_summary(_CPU_ONLY)}", flush=True)' in source
def test_the_requirements_use_the_cpu_wheel_index():
requirements = (ROOT / "requirements.txt").read_text(encoding="utf-8")
assert "abetlen.github.io/llama-cpp-python/whl/cpu" in requirements
assert "llama-cpp-python" in requirements