diff --git a/tools/calibrate.py b/tools/calibrate.py index 186604be..23894775 100644 --- a/tools/calibrate.py +++ b/tools/calibrate.py @@ -225,7 +225,10 @@ def measure(base_args: list[str], ids_list, start_engine, say=print, extra_worke by_workers = {} for w in worker_candidates(d_workers, extra_workers): say(f" Measuring with {w} CPU workers (restarts the engine) ...") - e = start_engine(with_arg(tuned, "--pool-workers", None if w == d_workers else str(w))) + e = restart(start_engine, with_arg(tuned, "--pool-workers", None if w == d_workers else str(w)), f"{w} workers", + say, report) + if e is None: + continue try: sw = Session(e, ids_list) sw.warm_up(1) @@ -233,8 +236,8 @@ def measure(base_args: list[str], ids_list, start_engine, say=print, extra_worke say(f" {w} workers: {statistics.median(by_workers[w]):.1f} tok/s") finally: close(e) - w_best = pick(by_workers, d_workers) report["workers"] = {str(k): v for k, v in by_workers.items()} + w_best = pick(by_workers, d_workers) if by_workers else d_workers if w_best != d_workers: settings["--pool-workers"] = str(w_best) base_rate = statistics.median(by_workers[w_best]) @@ -249,7 +252,9 @@ def measure(base_args: list[str], ids_list, start_engine, say=print, extra_worke for flag, v in zip(ADAPT_FLAGS, cand or (None,) * len(ADAPT_FLAGS)): args = with_arg(args, flag, v) say(f" Measuring the expert tier {'(the engine default)' if cand is None else 'every ' + cand[0] + ', ' + cand[1] + ' swaps, decay ' + cand[2]} (restarts the engine) ...") - e = start_engine(args) + e = restart(start_engine, args, "the expert tier " + key, say, report) + if e is None: + continue try: sa = Session(e, ids_list) sa.warm_up(1) @@ -257,7 +262,7 @@ def measure(base_args: list[str], ids_list, start_engine, say=print, extra_worke say(f" {statistics.median(by_adapt[key]):.1f} tok/s") finally: close(e) - a_best = pick(by_adapt, "default") + a_best = pick(by_adapt, "default") if by_adapt else "default" report["adapt"] = by_adapt if a_best != "default": for flag, v in zip(ADAPT_FLAGS, a_best.split("/")): @@ -270,6 +275,20 @@ def measure(base_args: list[str], ids_list, start_engine, say=print, extra_worke return {"settings": settings, "report": report} +def restart(start_engine, args, what, say, report): + """#1337: a later step's engine start that fails is a candidate that loses, not a failed run: what the earlier + steps measured (and the settings they chose) must survive it. Returns the engine, or None after saying why and + noting it in report["failed_starts"]. (The first start, for steps 1-3, is not covered: with no engine there is + nothing measured.)""" + try: + return start_engine(args) + except Exception as e: # noqa: BLE001 - whatever the start raised (RuntimeError, EngineDied, OSError ...) + why = str(e).strip().splitlines()[0][:300] if str(e).strip() else type(e).__name__ + say(f" the engine did not start for {what}: {why} - this candidate is dropped, the earlier results are kept") + report.setdefault("failed_starts", {})[what] = why + return None + + def close(eng): proc = getattr(eng, "proc", None) if proc is None: diff --git a/tools/test_calibrate.py b/tools/test_calibrate.py index 6922d78b..7ce1bdd0 100644 --- a/tools/test_calibrate.py +++ b/tools/test_calibrate.py @@ -78,6 +78,32 @@ class Calibrate(unittest.TestCase): self.assertEqual(len(starts), 1 + len(CAL.worker_candidates(6)) + len(CAL.ADAPT_CANDIDATES)) self.assertEqual(CAL.arg_value(starts[0], "--spec-min-p"), "0.5") # measured against the product default + def test_a_failed_restart_keeps_the_measurements_1337(self): + # the top PCIe share wins by far; every later restart (the worker counts, the expert tier) fails to start + n = {"starts": 0} + + def start(args): + n["starts"] += 1 + if n["starts"] > 1: + raise RuntimeError("the engine exited before it was ready: cudaMalloc failed (213 MiB free)") + return FakeEngine(args, lambda f, p, w: 80.0 if f == 0.75 else 50.0, 6) + said = [] + res = CAL.measure(BASE, [[1, 2, 3]] * 3, start, say=said.append) + self.assertEqual(res["settings"].get("--pcie-frac"), "0.75") # steps 1-3 survived + self.assertGreater(len(res["report"]["failed_starts"]), 1) + self.assertTrue(any("did not start" in x and "cudaMalloc" in x for x in said)) + self.assertEqual(res["report"]["workers"], {}) + self.assertEqual(res["report"]["adapt"], {}) + + def test_one_failing_worker_count_only_drops_that_candidate_1337(self): + def start(args): + if CAL.arg_value(args, "--pool-workers") == "3": + raise RuntimeError("out of memory") + return FakeEngine(args, lambda f, p, w: 60.0 if w == 2 else 50.0, 6) + res = CAL.measure(BASE, [[1, 2, 3]] * 3, start, say=lambda *_: None) + self.assertEqual(res["settings"].get("--pool-workers"), "2") + self.assertEqual(list(res["report"]["failed_starts"]), ["3 workers"]) + def test_adaptive_tier(self): # a slow-RAM PC: swapping 160 experts per round is 10% faster, 80 is 2% (noise) res, starts = self.run_with(lambda f, p, w: 50.0, adapt={"160": 1.10, "80": 1.02})