calibrate: the sweep visits every value in every round instead of once

Phase 1 measures each PCIe share once and takes the maximum, with the three prompts of that rate all at the one
setting.  Neighbours cannot be ordered that way: on this box 0.75 through 0.95 come out at 72.7, 69.0, 71.0 and
71.9, which is 0.2% apart, while the same settings moved 8-28% between engine starts.  A value that was worth a
real gain can lose its single sample and never be measured again, because phase 1's winner is the share phase 2
then sweeps spec_min_p at, and the confirm step only puts that one resulting pair against the default.

sweep() measures every value SWEEP_ROUNDS times inside the one engine start, one round visiting all of them and
every other round in reverse order, and the winners come from the medians.  Drift inside a sweep then lands on
every value the same number of times, and one bad sample cannot decide.  The same function runs phase 2, and the
confirm step uses the same round count instead of its own.

Cost: the sweep multiplies by SWEEP_ROUNDS, 3 by default.  A rate is three prompts of 128 tokens, so on this box
(six values, 95-100 tok/s) phase 1 goes from about 40 s to about 120 s, on a run that already loads the model more
than once.  A machine whose two leaders stay inside each other's noise can raise the constant.

Not in this change: phase 4 needs a restart per worker count and stays sequential, so a count is still taken from
one measurement each.

Tests: python -m unittest tools.test_calibrate (the stand-in engine is a pure function of the tune keys, so the
medians and the winners are the same as before); tools.test_setup_config and tools.test_setup_golden
This commit is contained in:
Ethan authored and Niko1221 committed 2026-10-08 02:36:32 +02:00
1 parent fe0df23120
commit d1990da1a6
1 file changed
+32 -12
+32 -12
View File
@@ -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