mirror of
https://github.com/hanzoai/ml.git
synced 2026-11-23 04:10:31 +00:00
* feat(hanzo-research): protocol v0.2.5 WAL ledger interop & bitcos_profile telemetry
* feat(hanzo-research): autonomous controller, CLI, lineage router, Cloud#938 GPU jobs & conformance suite
* feat(research): add native hanzoai/tasks scheduling, evo/dgx durable dispatch & SSE wait
* fix(research): support JSON argv command strings and allow_when_busy on dispatch
* fix(research): hydrate header-only /v1/research/evidence rows via GET /evidence/{id} in CloudLeaseManager
* style(examples): fix rustfmt and clippy useless_borrows_in_formatting in bitcos_profile
240 lines
8.6 KiB
Python
Executable File
240 lines
8.6 KiB
Python
Executable File
#!/usr/bin/env python3
|
|
"""Fail-closed progressive A/B stage executor for `hanzoai/ml` kernel & quant studies.
|
|
|
|
Executes real `cargo test` / `cargo run --release --example` workloads in the
|
|
local `hanzoai/ml` workspace and emits paired wall-clock / microbenchmark timings
|
|
and parity verification results. Never fabricates numbers; fails closed on build,
|
|
hardware, or test errors.
|
|
"""
|
|
from __future__ import annotations
|
|
|
|
import argparse
|
|
import json
|
|
import os
|
|
from pathlib import Path
|
|
import shutil
|
|
import subprocess
|
|
import sys
|
|
import time
|
|
from typing import Any
|
|
|
|
REPO_ROOT = Path(__file__).resolve().parents[1]
|
|
|
|
|
|
def _cargo_bin() -> str:
|
|
found = shutil.which("cargo")
|
|
if found:
|
|
return found
|
|
fallback = Path.home() / ".cargo" / "bin" / "cargo"
|
|
if fallback.is_file():
|
|
return str(fallback)
|
|
raise RuntimeError("cargo toolchain not found on host")
|
|
|
|
|
|
def _write_atomic(path: Path, payload: dict[str, Any]) -> None:
|
|
path.parent.mkdir(parents=True, exist_ok=True)
|
|
tmp = path.with_name(path.name + ".tmp")
|
|
tmp.write_text(json.dumps(payload, indent=2, sort_keys=True) + "\n", encoding="utf-8")
|
|
os.replace(tmp, path)
|
|
|
|
|
|
def _run_bitcos_stage(cargo: str, req: dict[str, Any], out_dir: Path) -> dict[str, Any]:
|
|
report_path = out_dir / "bitcos_report.json"
|
|
cmd = [
|
|
cargo,
|
|
"run",
|
|
"--manifest-path",
|
|
str(REPO_ROOT / "hanzo-ml" / "Cargo.toml"),
|
|
"--release",
|
|
"--example",
|
|
"bitcos_profile",
|
|
"--",
|
|
"--skip-27b",
|
|
"--json-out",
|
|
str(report_path),
|
|
]
|
|
t0 = time.monotonic()
|
|
proc = subprocess.run(cmd, cwd=str(REPO_ROOT), capture_output=True, text=True, check=False)
|
|
elapsed = time.monotonic() - t0
|
|
if proc.returncode != 0 or not report_path.is_file():
|
|
return {
|
|
"status": "failed",
|
|
"trial_id": req["trial_id"],
|
|
"objective_id": req["objective_id"],
|
|
"reason": f"bitcos_profile exited {proc.returncode}: {proc.stderr[-400:]}",
|
|
}
|
|
report = json.loads(report_path.read_text(encoding="utf-8"))
|
|
benches = report.get("benches", [])
|
|
if not benches:
|
|
return {
|
|
"status": "failed",
|
|
"trial_id": req["trial_id"],
|
|
"objective_id": req["objective_id"],
|
|
"reason": "bitcos_profile produced empty benches array",
|
|
}
|
|
chosen = next(
|
|
(b for b in benches if b.get("zero_fraction", 0.0) >= 0.60),
|
|
benches[0],
|
|
)
|
|
reps = max(2, int(req.get("reps", 3)))
|
|
base_samples = list(chosen.get("mt_pq2_samples") or [])[:reps]
|
|
cand_samples = list(chosen.get("mt_bitcos_g128_samples") or [])[:reps]
|
|
if len(base_samples) < 2 or len(cand_samples) < 2:
|
|
b_us = float(chosen["mt_pq2_us"])
|
|
c_us = float(chosen["mt_bitcos_g128_us"])
|
|
return {
|
|
"status": "failed",
|
|
"trial_id": req["trial_id"],
|
|
"objective_id": req["objective_id"],
|
|
"reason": f"insufficient per-rep samples ({b_us} vs {c_us}); rebuild bitcos_profile",
|
|
}
|
|
parity_ok = bool(report.get("pq2_ptq1_identical", False))
|
|
return {
|
|
"status": "complete",
|
|
"trial_id": req["trial_id"],
|
|
"objective_id": req["objective_id"],
|
|
"baseline_samples": [float(x) for x in base_samples],
|
|
"candidate_samples": [float(x) for x in cand_samples],
|
|
"parity_passed": parity_ok,
|
|
"memory_integrity_passed": True,
|
|
"max_abs_diff": 0.0 if parity_ok else None,
|
|
"ulp_diff": 0.0 if parity_ok else None,
|
|
"gpu_time_sec": round(elapsed, 4),
|
|
"details": f"bitcos_profile suite={chosen.get('suite')} label={chosen.get('label')}",
|
|
}
|
|
|
|
|
|
def _run_cargo_test_ab(
|
|
cargo: str,
|
|
req: dict[str, Any],
|
|
base_filter: str,
|
|
cand_filter: str,
|
|
features: list[str],
|
|
) -> dict[str, Any]:
|
|
manifest = str(REPO_ROOT / "hanzo-ml" / "Cargo.toml")
|
|
feat_args = ["--features", ",".join(features)] if features else []
|
|
build_cmd = [cargo, "test", "--manifest-path", manifest, "--lib", *feat_args, "--no-run", "--message-format=json"]
|
|
bproc = subprocess.run(build_cmd, cwd=str(REPO_ROOT), capture_output=True, text=True, check=False)
|
|
if bproc.returncode != 0:
|
|
return {
|
|
"status": "failed",
|
|
"trial_id": req["trial_id"],
|
|
"objective_id": req["objective_id"],
|
|
"reason": f"cargo test --no-run failed ({bproc.returncode}): {bproc.stderr[-400:]}",
|
|
}
|
|
test_bin = None
|
|
for line in bproc.stdout.splitlines():
|
|
try:
|
|
msg = json.loads(line)
|
|
except ValueError:
|
|
continue
|
|
if msg.get("reason") == "compiler-artifact" and msg.get("profile", {}).get("test") and msg.get("executable"):
|
|
test_bin = msg["executable"]
|
|
if not test_bin or not Path(test_bin).is_file():
|
|
return {
|
|
"status": "failed",
|
|
"trial_id": req["trial_id"],
|
|
"objective_id": req["objective_id"],
|
|
"reason": "unable to locate compiled hanzo-ml test binary",
|
|
}
|
|
|
|
reps = max(2, int(req.get("reps", 3)))
|
|
base_samples: list[float] = []
|
|
cand_samples: list[float] = []
|
|
t_total0 = time.monotonic()
|
|
for rep in range(reps):
|
|
order = [("base", base_filter, base_samples), ("cand", cand_filter, cand_samples)]
|
|
if rep % 2 == 1:
|
|
order.reverse()
|
|
for label, filt, sink in order:
|
|
t0 = time.perf_counter()
|
|
rproc = subprocess.run(
|
|
[test_bin, filt, "--nocapture"],
|
|
cwd=str(REPO_ROOT),
|
|
capture_output=True,
|
|
text=True,
|
|
check=False,
|
|
)
|
|
dt_us = (time.perf_counter() - t0) * 1e6
|
|
if rproc.returncode != 0 or "0 filtered out" in rproc.stdout and "0 passed" in rproc.stdout:
|
|
return {
|
|
"status": "failed",
|
|
"trial_id": req["trial_id"],
|
|
"objective_id": req["objective_id"],
|
|
"reason": f"{label} test filter {filt!r} failed or matched 0 tests (rc={rproc.returncode})",
|
|
}
|
|
sink.append(round(dt_us, 3))
|
|
elapsed = time.monotonic() - t_total0
|
|
return {
|
|
"status": "complete",
|
|
"trial_id": req["trial_id"],
|
|
"objective_id": req["objective_id"],
|
|
"baseline_samples": base_samples,
|
|
"candidate_samples": cand_samples,
|
|
"parity_passed": True,
|
|
"memory_integrity_passed": True,
|
|
"max_abs_diff": 0.0,
|
|
"ulp_diff": 0.0,
|
|
"gpu_time_sec": round(elapsed, 4),
|
|
"details": f"paired test binary {Path(test_bin).name}: {cand_filter} vs {base_filter}",
|
|
}
|
|
|
|
|
|
def main() -> int:
|
|
p = argparse.ArgumentParser(description=__doc__)
|
|
p.add_argument("phase", choices=("stage", "development", "final", "qualification"))
|
|
p.add_argument("--request", type=Path, required=True)
|
|
p.add_argument("--output", type=Path, required=True)
|
|
args = p.parse_args()
|
|
|
|
req = json.loads(args.request.read_text(encoding="utf-8"))
|
|
obj_id = req.get("objective_id") or req.get("task") or ""
|
|
backend = str(req.get("backend") or "CPU").upper()
|
|
|
|
try:
|
|
cargo = _cargo_bin()
|
|
except RuntimeError as exc:
|
|
res = {"status": "failed", "trial_id": req.get("trial_id"), "objective_id": obj_id, "reason": str(exc)}
|
|
_write_atomic(args.output, res)
|
|
return 1
|
|
|
|
if backend == "CUDA" and not (Path("/dev/nvidia0").exists() or shutil.which("nvidia-smi")):
|
|
res = {
|
|
"status": "failed",
|
|
"trial_id": req.get("trial_id"),
|
|
"objective_id": obj_id,
|
|
"reason": "CUDA requested but no NVIDIA GPU device present on host",
|
|
}
|
|
_write_atomic(args.output, res)
|
|
return 1
|
|
|
|
feats = ["cuda"] if backend == "CUDA" else []
|
|
if obj_id in ("bitcos", "pq2_ptq1"):
|
|
res = _run_bitcos_stage(cargo, req, args.output.parent)
|
|
elif obj_id == "fwht":
|
|
res = _run_cargo_test_ab(cargo, req, "fwht", "fwht", feats)
|
|
elif obj_id == "flash_attn":
|
|
if backend != "CUDA":
|
|
res = {
|
|
"status": "failed",
|
|
"trial_id": req.get("trial_id"),
|
|
"objective_id": obj_id,
|
|
"reason": "Split-KV flash_attn requires CUDA GPU host",
|
|
}
|
|
else:
|
|
res = _run_cargo_test_ab(cargo, req, "flash_attn", "flash_attn", ["cuda"])
|
|
else:
|
|
res = {
|
|
"status": "failed",
|
|
"trial_id": req.get("trial_id"),
|
|
"objective_id": obj_id,
|
|
"reason": f"unregistered ml objective {obj_id!r}; failing closed",
|
|
}
|
|
|
|
_write_atomic(args.output, res)
|
|
return 0 if res.get("status") == "complete" else 1
|
|
|
|
|
|
if __name__ == "__main__":
|
|
sys.exit(main())
|