tools/calibrate.py read any "gpu" list as a layer split and added --layer-split auto, so a one-card config with a
helper card for the expert tier ("gpu": [0] + --expert-cache-device1) was refused by the engine before it read a
weight, a "0,2" string never got its split, and split_skip_if_fits was ignored. The tuner now builds the arguments
with serve.server.engine_args, the same as a normal start.
When the tuning fails, setup prints the engine's own last error line from the log (only lines written during the
tuning), and says the failure again where it is not lost: before --calibrate starts the model, and in the
"All set." summary of an install that tuned.
235 lines
12 KiB
Python
235 lines
12 KiB
Python
"""Tests for tools/calibrate.py and setup's use of it, without a GPU: a stand-in engine whose decode speed is a
|
|
function of the settings it runs with.
|
|
|
|
python -m unittest tools.test_calibrate
|
|
"""
|
|
from __future__ import annotations
|
|
|
|
import json
|
|
import os
|
|
import sys
|
|
import tempfile
|
|
import unittest
|
|
from pathlib import Path
|
|
from unittest import mock
|
|
|
|
ROOT = Path(__file__).resolve().parents[1]
|
|
sys.path.insert(0, str(ROOT / "tools"))
|
|
sys.path.insert(0, str(ROOT))
|
|
import calibrate as CAL # noqa: E402
|
|
|
|
|
|
class FakeEngine:
|
|
"""Speed = f(pcie_frac, spec_min_p, workers): the GEN line's tune keys arrive as `strata_tune`."""
|
|
|
|
def __init__(self, args, speed, info_workers=6, starts=None):
|
|
self.args = list(args)
|
|
w = CAL.arg_value(args, "--pool-workers")
|
|
self.workers = int(w) if w else info_workers
|
|
self.info = {"pool_workers": info_workers, "pcie_frac": 0.55, "spec_min_p": float(CAL.arg_value(args, "--spec-min-p") or 0)}
|
|
self.speed = speed
|
|
self.last = {}
|
|
self.proc = None
|
|
if starts is not None:
|
|
starts.append(list(args))
|
|
|
|
def generate(self, ids, max_new, sampling, cancel):
|
|
tune = sampling.get("strata_tune") or {}
|
|
rate = self.speed(tune.get("pcie_frac", 0.55), tune.get("spec_min_p", self.info["spec_min_p"]), self.workers)
|
|
for _ in range(max_new):
|
|
yield 1
|
|
self.last = {"generated": max_new, "decode_ms": max_new / rate * 1000.0}
|
|
|
|
|
|
BASE = ["--pack", "p", "--spec", "4", "--spec-min-p", "0.5", "--max-context", "8192"]
|
|
|
|
|
|
class Calibrate(unittest.TestCase):
|
|
def run_with(self, speed, workers=6, base=BASE):
|
|
starts = []
|
|
res = CAL.measure(base, [[1, 2, 3]] * 3, lambda a: FakeEngine(a, speed, workers, starts), say=lambda *_: None)
|
|
return res, starts
|
|
|
|
def test_defaults_kept_when_flat(self):
|
|
res, _ = self.run_with(lambda f, p, w: 50.0)
|
|
self.assertEqual(res["settings"], {})
|
|
|
|
def test_small_gain_is_noise(self):
|
|
# 2% better at pcie 0.35: below MIN_GAIN, so the default stays
|
|
res, _ = self.run_with(lambda f, p, w: 51.0 if abs(f - 0.35) < 1e-6 else 50.0)
|
|
self.assertEqual(res["settings"], {})
|
|
|
|
def test_finds_pcie_and_min_p(self):
|
|
def speed(f, p, w):
|
|
return 50.0 + (10.0 if abs(f - 0.2) < 1e-6 else 0.0) + (5.0 if abs(p - 0.7) < 1e-6 else 0.0)
|
|
res, _ = self.run_with(speed)
|
|
self.assertEqual(res["settings"].get("--pcie-frac"), "0.20")
|
|
self.assertEqual(res["settings"].get("--spec-min-p"), "0.70")
|
|
self.assertNotIn("--pool-workers", res["settings"])
|
|
|
|
def test_fewer_workers(self):
|
|
# a hybrid CPU: half the workers is 20% faster
|
|
res, starts = self.run_with(lambda f, p, w: 60.0 if w == 3 else 50.0, workers=6)
|
|
self.assertEqual(res["settings"].get("--pool-workers"), "3")
|
|
self.assertEqual(len(starts), 1 + len(CAL.worker_candidates(6))) # one start per worker count, plus the sweep
|
|
self.assertEqual(CAL.arg_value(starts[0], "--spec-min-p"), "0.5") # measured against the product default
|
|
|
|
def test_old_calibration_is_the_baseline_reset(self):
|
|
# a config tuned earlier: the measurement starts from the product defaults, not from those values
|
|
base = CAL.apply(BASE, {"--pcie-frac": "0.20", "--pool-workers": "3", "--spec-min-p": "0.70"})
|
|
_, starts = self.run_with(lambda f, p, w: 50.0, base=base)
|
|
self.assertIsNone(CAL.arg_value(starts[0], "--pcie-frac"))
|
|
self.assertIsNone(CAL.arg_value(starts[0], "--pool-workers"))
|
|
self.assertEqual(CAL.arg_value(starts[0], "--spec-min-p"), "0.5")
|
|
|
|
def test_apply(self):
|
|
a = CAL.apply(BASE, {"--pcie-frac": "0.35", "--pool-workers": "4"})
|
|
self.assertEqual(CAL.arg_value(a, "--pcie-frac"), "0.35")
|
|
self.assertEqual(CAL.arg_value(a, "--pool-workers"), "4")
|
|
self.assertEqual(CAL.arg_value(a, "--spec-min-p"), "0.5")
|
|
b = CAL.apply(a, {}) # back to the defaults
|
|
self.assertIsNone(CAL.arg_value(b, "--pcie-frac"))
|
|
self.assertIsNone(CAL.arg_value(b, "--pool-workers"))
|
|
self.assertEqual(b.count("--spec-min-p"), 1)
|
|
|
|
def test_engine_args_are_the_servers(self):
|
|
# #447: a "gpu" list is a layer split only with two or more cards; "0,2" is one too; split_skip_if_fits counts
|
|
helper = {"args": BASE + ["--expert-cache-device1", "1800"], "gpu": [0], "env": {"CUDA_VISIBLE_DEVICES": "0,1"}}
|
|
self.assertEqual(CAL.engine_args(helper), helper["args"]) # one stage + a helper card: no split
|
|
self.assertEqual(CAL.engine_args({"args": BASE, "gpu": 1}), BASE)
|
|
self.assertEqual(CAL.engine_args({"args": BASE, "gpu": [0, 2]}), BASE + ["--layer-split", "auto"])
|
|
self.assertEqual(CAL.engine_args({"args": BASE, "gpu": "0,2", "layer_split": "18"}),
|
|
BASE + ["--layer-split", "18"])
|
|
self.assertEqual(CAL.engine_args({"args": BASE, "gpu": [0, 1], "split_skip_if_fits": True}),
|
|
BASE + ["--layer-split", "auto", "--split-skip-if-fits"])
|
|
own = BASE + ["--layer-split", ""] # the config's own value wins
|
|
self.assertEqual(CAL.engine_args({"args": own, "gpu": [0, 1]}), own)
|
|
|
|
def test_run_measures_with_the_servers_args(self):
|
|
with tempfile.TemporaryDirectory() as d:
|
|
tok = Path(d)
|
|
(tok / "vocab.json").write_text(json.dumps({"a": 0, "b": 1}))
|
|
(tok / "merges.txt").write_text("")
|
|
(tok / "token_type.json").write_text(json.dumps([1, 1]))
|
|
seen = []
|
|
saved = CAL.measure
|
|
CAL.measure = lambda args, ids_list, start_engine, say=print: seen.append(args) or {}
|
|
fake = type("ST", (), {"Tokenizer": lambda *a: type("T", (), {"encode": lambda s, t, **k: [0]})()})
|
|
try:
|
|
with mock.patch.dict(sys.modules, {"strata_tokenizer": fake}):
|
|
CAL.run({"tokenizer": d, "args": list(BASE), "gpu": [0]}, start_engine=lambda a: None)
|
|
finally:
|
|
CAL.measure = saved
|
|
self.assertEqual(seen, [BASE])
|
|
|
|
def test_engine_error(self):
|
|
with tempfile.TemporaryDirectory() as d:
|
|
log = Path(d) / "strata-x.log"
|
|
log.write_text("strata generate: an old error\n", encoding="utf-8")
|
|
since = log.stat().st_size
|
|
with open(log, "a", encoding="utf-8") as f:
|
|
f.write("loading ...\nstrata generate: --expert-cache-remote with a layer split needs a GPU that runs "
|
|
"no stage (2 visible, 2 used by the split)\n\n")
|
|
self.assertEqual(CAL.engine_error(str(log), since), "strata generate: --expert-cache-remote with a layer "
|
|
"split needs a GPU that runs no stage (2 visible, 2 used by the split)")
|
|
with open(log, "a", encoding="utf-8") as f:
|
|
f.write("Segmentation fault\n")
|
|
self.assertIn("--expert-cache-remote", CAL.engine_error(str(log), since)) # the engine's own line first
|
|
self.assertEqual(CAL.engine_error(str(log), log.stat().st_size), None) # nothing new since
|
|
self.assertIsNone(CAL.engine_error(None))
|
|
self.assertIsNone(CAL.engine_error(str(Path(d) / "missing.log")))
|
|
|
|
def test_worker_candidates(self):
|
|
self.assertEqual(CAL.worker_candidates(6), [6, 4, 3])
|
|
self.assertEqual(CAL.worker_candidates(23), [23, 15, 12])
|
|
self.assertEqual(CAL.worker_candidates(3), [3, 2])
|
|
self.assertEqual(CAL.worker_candidates(1), [1])
|
|
|
|
def test_pick(self):
|
|
self.assertEqual(CAL.pick({"a": [50, 51, 49], "b": [53, 52, 60]}, "a"), "b")
|
|
self.assertEqual(CAL.pick({"a": [50, 51, 49], "b": [51, 51.5, 51]}, "a"), "a")
|
|
self.assertEqual(CAL.pick({}, "a"), "a")
|
|
|
|
|
|
class SetupIntegration(unittest.TestCase):
|
|
def setUp(self):
|
|
self.tmp = tempfile.TemporaryDirectory()
|
|
self.old = {k: os.environ.get(k) for k in ("APPDATA", "XDG_CONFIG_HOME")}
|
|
os.environ["APPDATA"] = self.tmp.name
|
|
os.environ["XDG_CONFIG_HOME"] = self.tmp.name
|
|
import setup as S
|
|
self.S = S
|
|
self.saved = (S.gpu_info, S.cpu_info, S.ram_gb, CAL.run)
|
|
S.gpu_info = lambda pick=None: {"name": "RTX Test", "vram_gb": 12.0, "arch": "120", "count": 1, "index": 0}
|
|
S.cpu_info = lambda: ("Test CPU", True, True)
|
|
S.ram_gb = lambda: 64.0
|
|
|
|
def tearDown(self):
|
|
self.S.gpu_info, self.S.cpu_info, self.S.ram_gb, CAL.run = self.saved
|
|
for k, v in self.old.items():
|
|
if v is None:
|
|
os.environ.pop(k, None)
|
|
else:
|
|
os.environ[k] = v
|
|
self.tmp.cleanup()
|
|
|
|
def test_calibrate_config_writes_and_remembers(self):
|
|
cfg_path = Path(self.tmp.name) / "strata-q2_0.json"
|
|
cfg = {"exe": "x", "args": list(BASE), "model_name": "qwen3.8-flash-next-q2_0"}
|
|
cfg_path.write_text(json.dumps(cfg))
|
|
CAL.run = lambda c, say=print, start_engine=None: {"settings": {"--pcie-frac": "0.20"}, "report": {"tok_s": 61.2}}
|
|
self.assertTrue(self.S.calibrate_config(cfg_path))
|
|
written = json.loads(cfg_path.read_text())
|
|
self.assertEqual(CAL.arg_value(written["args"], "--pcie-frac"), "0.20")
|
|
saved = self.S.saved_calibration(written)
|
|
self.assertEqual(saved["settings"], {"--pcie-frac": "0.20"})
|
|
# another model on the same PC has no calibration yet
|
|
self.assertIsNone(self.S.saved_calibration({**written, "model_name": "swift-iq2_xs"}))
|
|
# another context size is another key (its KV cache changes the expert cache)
|
|
other = dict(written, args=CAL.with_arg(written["args"], "--max-context", "131072"))
|
|
self.assertIsNone(self.S.saved_calibration(other))
|
|
|
|
def test_failed_calibration_keeps_defaults(self):
|
|
cfg_path = Path(self.tmp.name) / "strata-q2_0.json"
|
|
cfg_path.write_text(json.dumps({"exe": "x", "args": list(BASE), "model_name": "m"}))
|
|
|
|
def boom(*a, **k):
|
|
raise RuntimeError("the engine exited before it was ready")
|
|
CAL.run = boom
|
|
self.assertFalse(self.S.calibrate_config(cfg_path))
|
|
self.assertEqual(json.loads(cfg_path.read_text())["args"], BASE)
|
|
self.assertIsNone(self.S.saved_calibration({"args": BASE, "model_name": "m"}))
|
|
|
|
def test_failed_calibration_says_the_engines_reason(self):
|
|
# #447: the engine's last error line is printed, and the --calibrate run repeats the failure before it starts
|
|
import contextlib
|
|
import io
|
|
log = Path(self.tmp.name) / "strata-q2_0.log"
|
|
log.write_text("strata generate: an error of an earlier start\n", encoding="utf-8")
|
|
cfg_path = Path(self.tmp.name) / "strata-q2_0.json"
|
|
cfg_path.write_text(json.dumps({"exe": "x", "args": list(BASE), "model_name": "m", "log": str(log)}))
|
|
|
|
def boom(*a, **k):
|
|
with open(log, "a", encoding="utf-8") as f:
|
|
f.write("strata generate: --expert-cache-remote with a layer split needs a GPU that runs no stage\n")
|
|
raise RuntimeError(f"the engine exited before it was ready (see {log})")
|
|
CAL.run = boom
|
|
out = io.StringIO()
|
|
with contextlib.redirect_stdout(out):
|
|
self.assertFalse(self.S.calibrate_config(cfg_path))
|
|
self.assertIn("the engine said: strata generate: --expert-cache-remote with a layer split", out.getvalue())
|
|
self.assertNotIn("an earlier start", out.getvalue())
|
|
out = io.StringIO()
|
|
with mock.patch.object(self.S, "ROOT", Path(self.tmp.name)), \
|
|
mock.patch.object(self.S, "data_folder", lambda d: (Path(self.tmp.name) / "data", [])), \
|
|
mock.patch.object(self.S, "update_installed_engine", lambda *a: None), \
|
|
mock.patch.object(sys, "argv", ["setup.py", "--calibrate", "--no-start"]), \
|
|
contextlib.redirect_stdout(out):
|
|
self.assertEqual(self.S.main(), 0)
|
|
self.assertIn("this PC is NOT tuned: the tuning failed (the reason is above); the model keeps the default "
|
|
"settings", out.getvalue())
|
|
|
|
|
|
if __name__ == "__main__":
|
|
unittest.main()
|