launch: a real short tune for DeepSeek-V4.1-Flash (tools/dsv41/tune.py: the drafter on or off, CPU threads; gains over 5% only, written into the launch config; any failure leaves the standard settings with a plain line); the launch no longer forces --tune skip for it; tests
This commit is contained in:
1 parent
3feaa2aa3f
commit
a2aeb4ecb1
5 files changed
+297
-7
No files matched your search
@@ -332,10 +332,7 @@ def apply_defaults(s: S.LaunchSettings, catalog) -> tuple[S.LaunchSettings, list
|
||||
s.parallel = None
|
||||
if s.vision not in (None, "off"):
|
||||
notices.append(S.Notice("warn", "DeepSeek-V4.1-Flash cannot read pictures in this Strata version, so pictures are off.", ""))
|
||||
s.vision = "off"
|
||||
if s.tune not in (None, "skip"):
|
||||
notices.append(S.Notice("warn", "Tuning is not offered for DeepSeek-V4.1-Flash yet; it starts with the engine's own settings.", ""))
|
||||
s.tune = "skip"
|
||||
s.vision = "off" # (tuning is DeepSeek's own short tune: tools/dsv41/tune.py, run by the launch)
|
||||
notices += S.check_context(s, max_fit_tokens=None)
|
||||
if s.access != "local" and not s.api_key and not s.no_key:
|
||||
from strata_cli.launch import access
|
||||
|
||||
@@ -1252,12 +1252,54 @@ class Runner:
|
||||
return None
|
||||
|
||||
# ---- 4. tuning
|
||||
def tune_deepseek(self) -> None:
|
||||
"""DeepSeek-V4.1-Flash has its own short tuner (tools/dsv41/tune.py: the drafter on or off, the CPU threads; the RAM store sizes itself):
|
||||
a few test runs of the engine, a result written into this launch's config, and the standard settings on any failure."""
|
||||
s = self.s
|
||||
cfg = getattr(self, "config_path", None)
|
||||
if cfg is None:
|
||||
self.emit("tune", "There are no DeepSeek settings to tune yet, so it starts with the standard ones.", 100, level="warn")
|
||||
return
|
||||
mode = "thorough" if s.tune == "thorough" else "quick"
|
||||
argv = [sys.executable, str(Path(__file__).resolve().parents[2] / "tools" / "dsv41" / "tune.py"), "--config", str(cfg), "--mode", mode,
|
||||
"--output", "json"]
|
||||
self.emit("tune", "Tuning DeepSeek for the best speed: a few short test runs (1-3 minutes each). Close other heavy programs.", 0)
|
||||
try:
|
||||
p = self._spawn(argv)
|
||||
except Exception as e: # noqa: BLE001 - a tune never stops a launch
|
||||
self.emit("tune", f"The DeepSeek tuner could not start ({type(e).__name__}), so the standard settings run.", 100, level="warn")
|
||||
return
|
||||
lines: list[str] = []
|
||||
reader = threading.Thread(target=lambda: lines.extend(p.stdout.readlines()), daemon=True) # type: ignore[union-attr]
|
||||
reader.start()
|
||||
while p.poll() is None:
|
||||
if self.cancel.is_set():
|
||||
self._stop_children()
|
||||
raise Cancelled()
|
||||
time.sleep(1.0)
|
||||
reader.join(timeout=3)
|
||||
result = None
|
||||
for line in reversed(lines):
|
||||
try:
|
||||
result = json.loads(line).get("result")
|
||||
break
|
||||
except (ValueError, AttributeError):
|
||||
continue
|
||||
if result is None:
|
||||
self.tune_word = "failed"
|
||||
self.emit("tune", "The DeepSeek tuner did not finish, so the standard settings run. Nothing was changed.", 100, level="warn")
|
||||
return
|
||||
self.tune_result = result
|
||||
self.tune_word = "faster" if result.get("changed") else "already fastest"
|
||||
s.tuned_at = time.strftime("%Y-%m-%d")
|
||||
self.emit("tune", " ".join(result.get("lines") or ["Tuning finished."]), 100, level="info", word=self.tune_word)
|
||||
|
||||
def tune(self) -> None:
|
||||
s = self.s
|
||||
if s.tune == "skip":
|
||||
return
|
||||
if self.multi_file():
|
||||
self.emit("tune", "Tuning is not offered for DeepSeek-V4.1-Flash yet: it starts with the engine's own settings.", 100)
|
||||
self.tune_deepseek()
|
||||
return
|
||||
from strata_control import presets
|
||||
if not self.retune:
|
||||
|
||||
@@ -0,0 +1,80 @@
|
||||
"""tools/dsv41/tune.py: the short DeepSeek tune picks only clear gains, writes them into the config, and never stops a launch."""
|
||||
from __future__ import annotations
|
||||
|
||||
import json
|
||||
import sys
|
||||
import tempfile
|
||||
import unittest
|
||||
from pathlib import Path
|
||||
from unittest import mock
|
||||
|
||||
ROOT = Path(__file__).resolve().parents[2]
|
||||
sys.path.insert(0, str(ROOT / "tools" / "dsv41"))
|
||||
sys.path.insert(0, str(ROOT / "tools"))
|
||||
import tune # noqa: E402
|
||||
|
||||
|
||||
class Tune(unittest.TestCase):
|
||||
def setUp(self):
|
||||
self.tmp = tempfile.TemporaryDirectory()
|
||||
self.addCleanup(self.tmp.cleanup)
|
||||
self.cfg = Path(self.tmp.name) / "c.json"
|
||||
self.cfg.write_text(json.dumps({"exe": "x", "args": ["--pack", "P", "--max-context", "8192", "--eos", "1", "--dspark", "auto",
|
||||
"--dspark-conf", "0.65"]}))
|
||||
p = mock.patch.object(tune, "prompt_ids", lambda pack: [1, 2, 3])
|
||||
p.start()
|
||||
self.addCleanup(p.stop)
|
||||
p = mock.patch.object(tune, "physical_cores", lambda: 6)
|
||||
p.start()
|
||||
self.addCleanup(p.stop)
|
||||
p = mock.patch.object(tune.os, "cpu_count", lambda: 12)
|
||||
p.start()
|
||||
self.addCleanup(p.stop)
|
||||
|
||||
def run_tune(self, speed):
|
||||
calls = []
|
||||
|
||||
def fake(exe, args, f, n, timeout, env):
|
||||
calls.append(list(args))
|
||||
return speed(args)
|
||||
return tune.tune(self.cfg, "quick", measure_fn=fake), calls
|
||||
|
||||
def test_parse(self):
|
||||
self.assertEqual(tune.parse_speed("x\ndecode: 48 tokens in 9000 ms (5.33 tok/s); CPU lane: 1"), 5.33)
|
||||
self.assertIsNone(tune.parse_speed("nothing"))
|
||||
|
||||
def test_a_clear_gain_is_written(self):
|
||||
res, calls = self.run_tune(lambda a: 6.0 if "on" in a[a.index("--dspark") + 1:a.index("--dspark") + 2] else 4.5)
|
||||
args = json.loads(self.cfg.read_text())["args"]
|
||||
self.assertEqual(args[args.index("--dspark") + 1], "on")
|
||||
self.assertIn("faster", " ".join(res["lines"]))
|
||||
self.assertEqual(len(calls), 3) # baseline, drafter on, threads
|
||||
self.assertIn("tuned", json.loads(self.cfg.read_text()))
|
||||
|
||||
def test_noise_changes_nothing(self):
|
||||
before = self.cfg.read_text()
|
||||
res, _ = self.run_tune(lambda a: 4.5 + 0.1 * (len(a) % 2))
|
||||
self.assertEqual(self.cfg.read_text(), before)
|
||||
self.assertEqual(res["changed"], [])
|
||||
self.assertIn("nothing changed", " ".join(res["lines"]))
|
||||
|
||||
def test_a_failing_baseline_leaves_the_config_and_says_so(self):
|
||||
before = self.cfg.read_text()
|
||||
res, calls = self.run_tune(lambda a: None)
|
||||
self.assertEqual(self.cfg.read_text(), before)
|
||||
self.assertEqual(len(calls), 1)
|
||||
self.assertIn("stay as they are", " ".join(res["lines"]))
|
||||
|
||||
def test_a_failing_trial_is_skipped(self):
|
||||
res, _ = self.run_tune(lambda a: 4.5 if "--cpu-threads" not in a and "on" != a[a.index("--dspark") + 1] else None)
|
||||
self.assertEqual(res["changed"], [])
|
||||
self.assertIn("did not finish", " ".join(res["lines"]))
|
||||
|
||||
def test_main_never_raises(self):
|
||||
with mock.patch.object(tune, "tune", side_effect=RuntimeError("boom")):
|
||||
with mock.patch.object(sys, "argv", ["tune.py", "--config", str(self.cfg)]):
|
||||
self.assertEqual(tune.main(), 0)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
||||
@@ -95,11 +95,11 @@ class LaunchDeepSeek(unittest.TestCase):
|
||||
p = self.plan("--model", "ud-q8_k_xl", "--parallel", "4", "--vision", "on", "--tune", "quick")
|
||||
s = p["settings"]
|
||||
self.assertFalse(s.get("parallel"))
|
||||
self.assertEqual((s["vision"], s["tune"]), ("off", "skip"))
|
||||
self.assertEqual((s["vision"], s["tune"]), ("off", "quick")) # the DeepSeek tune is real now: the choice stays
|
||||
text = " ".join(p["warnings"])
|
||||
self.assertIn("one request at a time", text)
|
||||
self.assertIn("cannot read pictures", text)
|
||||
self.assertIn("Tuning is not offered", text)
|
||||
self.assertNotIn("Tuning is not offered", text)
|
||||
|
||||
def test_setup_is_asked_for_the_family_and_the_files(self):
|
||||
s = S.LaunchSettings(family="deepseek41", model="UD-Q8_K_XL", context=8192, vision="off", tune="skip",
|
||||
|
||||
@@ -0,0 +1,171 @@
|
||||
"""A short tune for DeepSeek-V4.1-Flash (strata-dsv41): the few settings the engine has, measured on this computer.
|
||||
|
||||
python tools/dsv41/tune.py --config <launch config .json> [--mode quick|thorough] [--runs N]
|
||||
|
||||
What it does (a few short decode runs through the engine's own command line; each loads the model, so about 1-3 minutes each):
|
||||
1. the launch's own settings (the baseline);
|
||||
2. the same with the DSpark drafter forced on (`--dspark on --dspark-conf 0.65`) against `--dspark auto`, which leaves it unloaded on a
|
||||
small card: kept only when it is clearly faster;
|
||||
3. the CPU threads: the number of physical cores against every logical thread (SMT), when they differ: kept only when clearly faster.
|
||||
The RAM store (`--ssd-cache`) sizes itself from the free memory at start, so it is left as the engine's own; the line says so.
|
||||
A setting is changed only for a gain of more than 5% (run-to-run noise on this engine is about 5%). Whatever is kept is written into
|
||||
the config's args; a run that fails or a computer that is too busy leaves the config as it was, with one plain line. Always exits 0.
|
||||
The context stays what the person chose.
|
||||
"""
|
||||
from __future__ import annotations
|
||||
|
||||
import argparse
|
||||
import json
|
||||
import os
|
||||
import re
|
||||
import subprocess
|
||||
import sys
|
||||
import tempfile
|
||||
from pathlib import Path
|
||||
|
||||
ROOT = Path(__file__).resolve().parents[2]
|
||||
sys.path.insert(0, str(ROOT / "tools"))
|
||||
sys.path.insert(0, str(Path(__file__).resolve().parent))
|
||||
|
||||
GAIN = 1.05
|
||||
DECODE = re.compile(r"decode:\s*\d+ tokens in [\d.]+ ms \(([\d.]+) tok/s\)")
|
||||
|
||||
|
||||
def say(msg: str) -> None:
|
||||
print(msg, flush=True)
|
||||
|
||||
|
||||
def parse_speed(stderr: str) -> float | None:
|
||||
m = DECODE.findall(stderr or "")
|
||||
return float(m[-1]) if m else None
|
||||
|
||||
|
||||
def drop(args: list[str], flag: str) -> list[str]:
|
||||
"""`args` without `flag` and its value."""
|
||||
out, i = [], 0
|
||||
while i < len(args):
|
||||
if args[i] == flag:
|
||||
i += 2
|
||||
continue
|
||||
out.append(args[i])
|
||||
i += 1
|
||||
return out
|
||||
|
||||
|
||||
def with_flag(args: list[str], flag: str, *values: str) -> list[str]:
|
||||
return [*drop(args, flag), flag, *values]
|
||||
|
||||
|
||||
def physical_cores() -> int | None:
|
||||
try:
|
||||
import psutil
|
||||
return psutil.cpu_count(logical=False)
|
||||
except Exception: # noqa: BLE001
|
||||
return None
|
||||
|
||||
|
||||
def measure(exe: str, args: list[str], ids_file: Path, n: int, timeout: int, env: dict) -> float | None:
|
||||
"""Decode tokens a second of one CLI run, or None when it did not run to the end."""
|
||||
cmd = [exe, *[a for a in args if a not in ("--serve",)], "--ids", "@" + str(ids_file), "--n", str(n)]
|
||||
try:
|
||||
r = subprocess.run(cmd, capture_output=True, text=True, encoding="utf-8", errors="replace", timeout=timeout, env=env)
|
||||
except (OSError, subprocess.TimeoutExpired):
|
||||
return None
|
||||
if r.returncode != 0:
|
||||
return None
|
||||
return parse_speed(r.stderr)
|
||||
|
||||
|
||||
def prompt_ids(pack: Path) -> list[int]:
|
||||
import smoke
|
||||
tok = smoke.tokenizer(pack)
|
||||
return tok.encode(smoke.PROMPT, parse_special=True)
|
||||
|
||||
|
||||
def lib_env(cfg: dict) -> dict:
|
||||
env = dict(os.environ)
|
||||
dirs = [str(d) for d in cfg.get("lib_dirs") or [] if d]
|
||||
key = "PATH" if os.name == "nt" else "LD_LIBRARY_PATH"
|
||||
if dirs:
|
||||
env[key] = os.pathsep.join(dirs + [env.get(key, "")])
|
||||
return env
|
||||
|
||||
|
||||
def tune(config: Path, mode: str = "quick", n: int = 48, timeout: int = 1500, measure_fn=measure) -> dict:
|
||||
"""Returns {"changed": [...], "speeds": {...}, "lines": [plain sentences]}; writes the config when something was kept."""
|
||||
cfg = json.loads(config.read_text(encoding="utf-8-sig"))
|
||||
exe, args = cfg.get("exe"), [str(a) for a in cfg.get("args") or []]
|
||||
res = {"changed": [], "speeds": {}, "lines": []}
|
||||
if not exe or "--pack" not in args:
|
||||
res["lines"].append("This launch has no DeepSeek engine settings to tune, so it starts as it is.")
|
||||
return res
|
||||
pack = Path(args[args.index("--pack") + 1])
|
||||
env = lib_env(cfg)
|
||||
try:
|
||||
ids = prompt_ids(pack)
|
||||
except Exception as e: # noqa: BLE001
|
||||
res["lines"].append(f"The tuner could not prepare its test question ({e}), so the standard settings run.")
|
||||
return res
|
||||
with tempfile.TemporaryDirectory() as d:
|
||||
f = Path(d) / "ids.txt"
|
||||
f.write_text(",".join(str(i) for i in ids), encoding="ascii")
|
||||
base_args = [a for a in args if a != "--serve"]
|
||||
say("Tuning DeepSeek: measuring the standard settings first ...")
|
||||
base = measure_fn(exe, base_args, f, n, timeout, env)
|
||||
if base is None:
|
||||
res["lines"].append("The standard settings did not finish a test run here, so nothing was tuned and they stay as they are.")
|
||||
return res
|
||||
res["speeds"]["standard"] = base
|
||||
best, best_args = base, base_args
|
||||
say(f" standard settings: {base:.2f} tokens a second")
|
||||
trials = [("the drafter on (DSpark)", lambda a: with_flag(with_flag(a, "--dspark", "on"), "--dspark-conf", "0.65"))]
|
||||
cores, logical = physical_cores(), os.cpu_count() or 0
|
||||
if cores and logical and logical > cores and "--cpu-threads" not in base_args:
|
||||
trials.append((f"{cores} CPU threads (one for each physical core)", lambda a, c=cores: with_flag(a, "--cpu-threads", str(c))))
|
||||
if mode == "thorough" and cores:
|
||||
trials.append(("every logical CPU thread", lambda a, c=logical: with_flag(a, "--cpu-threads", str(c))))
|
||||
for label, make in trials:
|
||||
cand = make(best_args)
|
||||
say(f" trying {label} ...")
|
||||
v = measure_fn(exe, cand, f, n, timeout, env)
|
||||
if v is None:
|
||||
res["lines"].append(f"The test with {label} did not finish; that setting stays as it was.")
|
||||
continue
|
||||
res["speeds"][label] = v
|
||||
say(f" {v:.2f} tokens a second")
|
||||
if v > best * GAIN:
|
||||
best, best_args = v, cand
|
||||
res["changed"].append(label)
|
||||
if res["changed"]:
|
||||
new = [a for a in best_args]
|
||||
cfg["args"] = new
|
||||
cfg.setdefault("tuned", {})["dsv41"] = {"speeds": res["speeds"], "changed": res["changed"]}
|
||||
config.write_text(json.dumps(cfg, indent=1), encoding="utf-8")
|
||||
res["lines"].append(f"Done: {', '.join(res['changed'])} - answers about {100 * (best / base - 1):.0f}% faster "
|
||||
f"({base:.1f} -> {best:.1f} tokens a second). The RAM store sizes itself when the model starts.")
|
||||
else:
|
||||
res["lines"].append(f"Your computer already runs DeepSeek best with the standard settings ({base:.1f} tokens a second); nothing changed.")
|
||||
return res
|
||||
|
||||
|
||||
def main() -> int:
|
||||
ap = argparse.ArgumentParser()
|
||||
ap.add_argument("--config", required=True)
|
||||
ap.add_argument("--mode", default="quick", choices=["quick", "thorough"])
|
||||
ap.add_argument("--tokens", type=int, default=48)
|
||||
ap.add_argument("--timeout", type=int, default=1500)
|
||||
ap.add_argument("--output", default="human")
|
||||
a = ap.parse_args()
|
||||
try:
|
||||
res = tune(Path(a.config), a.mode, a.tokens, a.timeout)
|
||||
except Exception as e: # noqa: BLE001 - a tune never stops a launch
|
||||
res = {"changed": [], "speeds": {}, "lines": [f"The tuner stopped ({type(e).__name__}), so the standard settings run."]}
|
||||
for line in res["lines"]:
|
||||
say(line)
|
||||
if a.output == "json":
|
||||
say(json.dumps({"result": res}))
|
||||
return 0
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
sys.exit(main())
|
||||
Reference in new issue
Block a user