diff --git a/tools/calibrate.py b/tools/calibrate.py index f5f4e2ad..3c279ac2 100644 --- a/tools/calibrate.py +++ b/tools/calibrate.py @@ -37,6 +37,7 @@ sys.path.insert(0, str(ROOT)) MIN_GAIN = 0.03 # a setting must beat the default by this much to be kept PCIE_FRACS = (0.0, 0.2, 0.35, 0.55, 0.75, 0.9, 1.0) # 1.0: every miss over PCIe, the CPU pool gets no expert SPEC_MIN_PS = (0.3, 0.5, 0.7) +SWEEP_ROUNDS = 3 # how many times the sweep visits each value (see `sweep`) # the adaptive tier's candidates (every, swaps, decay) against the engine's own (None): swapping more and remembering # longer, the rest of the set as the engine has it ADAPT_CANDIDATES = (None, ("1", "80", "0.97"), ("1", "160", "0.97")) @@ -175,6 +176,29 @@ def engine_error(log: str | None, since: int = 0) -> str | None: return next((x for x in reversed(lines) if x.startswith(("strata", "ERR"))), lines[-1] if lines else None) +def sweep(s, values: list, base_tune: dict, key: str, label: str, say) -> dict: + """Every value, `SWEEP_ROUNDS` times, one round visiting all of them and every other round in reverse order. + + A single rate per value cannot order neighbours. #1332 has 0.75 to 0.95 inside each other's noise at 72.7, + 69.0, 71.0 and 71.9, and that box moved 8-28% between engine starts at an unchanged setting; a 5080/Xeon box + reports +9.57 tok/s between two blocks at an unchanged setting (p = 0.00004), larger than the effect being + hunted. Rotating the values inside one engine start puts that drift on every value the same number of times, + and the median over the rounds keeps a single bad sample from deciding. The caller takes the winner from the + medians, so every value keeps the same chance the others had. + """ + out = {v: [] for v in values} + for r in range(SWEEP_ROUNDS): + for v in (values if r % 2 == 0 else values[::-1]): + out[v].append(s.rate({**base_tune, key: v})) + say(f" round {r + 1}/{SWEEP_ROUNDS} {label}: " + + " ".join(f"{v:g} {statistics.median(out[v]):.1f}" for v in values)) + med = {v: statistics.median(out[v]) for v in values} + say(f" {label}, median of {SWEEP_ROUNDS}: " + + ", ".join(f"{v:g}: {med[v]:.1f} ({min(out[v]):.1f}-{max(out[v]):.1f})" for v in values) + + f" -> best {max(med, key=lambda k: med[k]):g}") + return out + + def measure(base_args: list[str], ids_list, start_engine, say=print, extra_workers=()) -> dict: t0 = time.time() report: dict = {} @@ -189,22 +213,18 @@ def measure(base_args: list[str], ids_list, start_engine, say=print, extra_worke s = Session(eng, ids_list) s.warm_up() # 1. the PCIe share, at the default draft floor - by_pcie = {} - for f in sorted(set(PCIE_FRACS) | {round(d_pcie, 2)}): - by_pcie[f] = [s.rate({"pcie_frac": f, "spec_min_p": d_minp})] - say(f" PCIe share {f:.2f}: {by_pcie[f][0]:.1f} tok/s") - best_pcie = max(by_pcie, key=lambda k: by_pcie[k][0]) + by_pcie = sweep(s, sorted(set(PCIE_FRACS) | {round(d_pcie, 2)}), {"spec_min_p": d_minp}, "pcie_frac", + "PCIe share", say) + best_pcie = max(by_pcie, key=lambda k: statistics.median(by_pcie[k])) # 2. the draft floor, at that share - by_minp = {} - for p in sorted(set(SPEC_MIN_PS) | {round(d_minp, 2)}): - by_minp[p] = [s.rate({"pcie_frac": best_pcie, "spec_min_p": p})] - say(f" draft floor {p:.2f}: {by_minp[p][0]:.1f} tok/s") - best_minp = max(by_minp, key=lambda k: by_minp[k][0]) - # 3. the winner against the default, interleaved, three times each + by_minp = sweep(s, sorted(set(SPEC_MIN_PS) | {round(d_minp, 2)}), {"pcie_frac": best_pcie}, "spec_min_p", + "draft floor", say) + best_minp = max(by_minp, key=lambda k: statistics.median(by_minp[k])) + # 3. the winner against the default, interleaved, the same number of rounds dflt, cand = (round(d_pcie, 2), round(d_minp, 2)), (best_pcie, best_minp) confirm = {dflt: [], cand: []} if cand != dflt: - for _ in range(3): + for _ in range(SWEEP_ROUNDS): for k in (dflt, cand): confirm[k].append(s.rate({"pcie_frac": k[0], "spec_min_p": k[1]})) chosen = pick(confirm, dflt) if cand != dflt else dflt