Merge branch 'f-dsv41-tune' into f-024-rel

This commit is contained in:
Niko1221 committed 2026-10-23 23:33:46 +02:00
commit f9f297bf90
7 files changed
+308 -53

No files matched your search

+1 -4
View File
@@ -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
+43 -1
View File
@@ -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:
+10 -45
View File
@@ -13,7 +13,7 @@ card, and computes the rest of the experts on the CPU, reading them in place fro
Measured (r9700, 4 x Radeon AI PRO R9700 32 GB, 251 GB RAM, 2026-10-20, f-dsv41-port 01389205e): per card 3.4-4.7 GB of dense
weights, 28.5-29.8 GB of experts (1,505-1,575 of its 3,840), ~0.6 GB free; 6,197 experts (116.5 GB) in graphics memory and
9,163 (172.3 GB) on the CPU lane; --tp 2: prompts 143-181 tokens/s (3K prompts), answers 25-31 tokens/s. Pure functions, no I/O:
9,163 (172.3 GB) on the CPU lane; --tp 2: prompts 143-181 tokens a second (3K prompts), answers 25-31 tokens a second. Pure functions, no I/O:
setup.py, tools/strata_mcp.py and strata_cli/launch import it. Recommend, never force: nothing here refuses a computer.
"""
@@ -44,16 +44,16 @@ REST_RAM_GB = 9.0 # RAM the engine and the system keep besid
SPARE_FITS_GB = 4.0 # RAM left beyond the CPU lane's experts below which "fits" becomes "tight"
DISK_HEADROOM_GB = 6.0 # the pack folder (hash tables, tokenizer) and room to work
SMALLEST_CARD_GB = 8.0 # a card under this adds little: the dense weights alone are ~4 GB of it per card of four
MEASURED = ("4 x Radeon AI PRO R9700 (32 GB) with 251 GB of RAM: prompts of 3,000 tokens read at 143-181 tokens/s, answers "
"at 25-31 tokens/s (--tp 2); one answer at a time")
MEASURED = ("4 x Radeon AI PRO R9700 (32 GB) with 251 GB of RAM: prompts of 3,000 tokens read at 143-181 tokens a second, answers "
"at 25-31 tokens a second (--tp 2); one answer at a time")
# The two official IST-DAS sets (helcig/tmp-oct26-2, setup.py DSV41_ISTA_FILES): 4 shards each (the experts as IQ1_M / IQ2 / IQ3 types, the
# dense weights Q8_0 / Q5_K), plus ONE Engram (110.6 GB, IQ4_NL, shared by both sets, stays on the SSD) and the DSpark drafter (8.0 GB).
# Measured 2026-10-23 (f-dsv41-rel): quality against the original model (ppl +16% for 2.00, +7.7-8.3% for 2.25; code / agent +3.5% / +2.5%
# on 2.25); speed on the owner PC (RTX 5070 12 GB, 64 GB RAM, 3.6 GB/s NVMe): answers ~4.7 tokens/s, prompts 121 tokens/s at 4K and
# 231 tokens/s at 8K; RTX 3060 12 GB with 46 GB of RAM (the engine held to 36 GB): answers ~7.2 tokens/s on 2.00; 4 x Radeon AI PRO
# R9700: answers ~31 tokens/s, prompts 330-373 tokens/s at 8K.
# on 2.25); speed on the owner PC (RTX 5070 12 GB, 64 GB RAM, 3.6 GB/s NVMe): answers ~4.7 tokens a second, prompts 121 tokens a second at 4K and
# 231 tokens a second at 8K; RTX 3060 12 GB with 46 GB of RAM (the engine held to 36 GB): answers ~7.2 tokens a second on 2.00; 4 x Radeon AI PRO
# R9700: answers ~31 tokens a second, prompts 330-373 tokens a second at 8K.
ISTA_ENGRAM_BYTES = 110_595_290_720
ISTA_DSPARK_BYTES = 7_976_914_240
ISTA_META_BYTES = 5_780_589 # the first shard of Unsloth's UD-Q8_K_XL: only the metadata --engram-meta reads
@@ -68,9 +68,9 @@ ISTA_SETS = {
}
ISTA_DENSE_GB = 6.16 # the dense weights in the shards (attention, router, shared experts, the head; the embedding stays on the CPU)
ISTA_DENSE_ON_GPU_GB = 4.5 # what a card holds of them with a 12 GB card (measured on the 5070 / 3060: the engine places the rest itself)
ISTA_MEASURED = ("an RTX 5070 (12 GB) with 64 GB of RAM and a 3.6 GB/s NVMe drive: answers about 4.7 tokens/s, a 4,000-token prompt "
"read at 121 tokens/s; an RTX 3060 (12 GB) with 46 GB of RAM: about 7.2 tokens/s; four Radeon AI PRO R9700 cards: "
"about 31 tokens/s")
ISTA_MEASURED = ("an RTX 5070 (12 GB) with 64 GB of RAM and a 3.6 GB/s NVMe drive: answers about 4.7 tokens a second, a 4,000-token prompt "
"read at 121 tokens a second; an RTX 3060 (12 GB) with 46 GB of RAM: about 7.2 tokens a second; four Radeon AI PRO R9700 cards: "
"about 31 tokens a second")
def variant(model: str | None) -> dict | None:
@@ -89,41 +89,6 @@ def ista_download_bytes(model: str, have_engram: bool = False) -> int:
return v["set_bytes"] + ISTA_DSPARK_BYTES + ISTA_META_BYTES + (0 if have_engram else ISTA_ENGRAM_BYTES)
# The two official IST-DAS sets (helcig/tmp-oct26-2, setup.py DSV41_ISTA_FILES): 4 shards each (the experts as IQ1_M / IQ2 / IQ3 types, the
# dense weights Q8_0 / Q5_K), plus ONE Engram (110.6 GB, IQ4_NL, shared by both sets, stays on the SSD) and the DSpark drafter (8.0 GB).
# Measured 2026-10-23 (f-dsv41-rel): quality against the original model (ppl +16% for 2.00, +7.7-8.3% for 2.25; code / agent +3.5% / +2.5%
# on 2.25); speed on the owner PC (RTX 5070 12 GB, 64 GB RAM, 3.6 GB/s NVMe): answers ~4.7 tokens/s, prompts 121 tokens/s at 4K and
# 231 tokens/s at 8K; RTX 3060 12 GB with 46 GB of RAM (the engine held to 36 GB): answers ~7.2 tokens/s on 2.00; 4 x Radeon AI PRO
# R9700: answers ~31 tokens/s, prompts 330-373 tokens/s at 8K.
ISTA_ENGRAM_BYTES = 110_595_290_720
ISTA_DSPARK_BYTES = 7_976_914_240
ISTA_META_BYTES = 5_780_589 # the first shard of Unsloth's UD-Q8_K_XL: only the metadata --engram-meta reads
ISTA_SETS = {
"IST-DAS-2.00bpw": {"title": "IST-DAS 2.00 bpw", "set_bytes": 138_238_892_160, "experts_gb": 132.07, "profile": "profile-2.00.bin",
"ram_ok_gb": 40.0, "disk_total": 257.0,
"quality": "about 16% above the original model's perplexity (the 2-bit price)"},
"IST-DAS-2.25bpw": {"title": "IST-DAS 2.25 bpw", "set_bytes": 155_225_823_328, "experts_gb": 149.06, "profile": "profile-2.25.bin",
"ram_ok_gb": 44.0, "disk_total": 274.0,
"quality": "about 8% above the original model's perplexity (code and agent work about 2.5-3.5% above), "
"the better of the two, 17 GB more disk"},
}
ISTA_DENSE_GB = 6.16 # the dense weights in the shards (attention, router, shared experts, the head; the embedding stays on the CPU)
ISTA_DENSE_ON_GPU_GB = 4.5 # what a card holds of them with a 12 GB card (the engine places the rest itself)
ISTA_MEASURED = ("an RTX 5070 (12 GB) with 64 GB of RAM and a 3.6 GB/s NVMe drive: answers about 4.7 tokens/s, a 4,000-token prompt "
"read at 121 tokens/s; an RTX 3060 (12 GB) with 46 GB of RAM: about 7.2 tokens/s; four Radeon AI PRO R9700 cards: "
"about 31 tokens/s")
def variant(model: str | None) -> dict | None:
"""The official-set entry for a model name (case-insensitive), None for UD-Q8_K_XL and anything else."""
return ISTA_SETS.get(variant_key(model)) if variant_key(model) else None
def ista_download_bytes(model: str, have_engram: bool = False) -> int:
v = variant(model)
return v["set_bytes"] + ISTA_DSPARK_BYTES + ISTA_META_BYTES + (0 if have_engram else ISTA_ENGRAM_BYTES)
def _round1(x: float) -> float:
return round(float(x) * 10) / 10
@@ -167,7 +132,7 @@ def _plan_ista(model: str, cards: list[dict] | None, ram_gb: float | None, disk_
vendor: str | None) -> dict[str, Any]:
"""plan() for an official IST-DAS set (ISTA_SETS): the same keys, the measured speeds in plain words. RAM classes: with RAM for every
expert the CPU lane keeps ("fits"), with 40 GB or more the engine's RAM store keeps the hot experts and reads the rest from the SSD
("tight": measured 4.7 tokens/s on 64 GB, 7.2 on 46 GB), under 40 GB it is untested and slower ("spills"). Never a refusal."""
("tight": measured 4.7 tokens a second on 64 GB, 7.2 on 46 GB), under 40 GB it is untested and slower ("spills"). Never a refusal."""
v = variant(model)
keep, ignored = gpu_fit.usable_cards(gpu_fit.for_vendor(list(cards or []), vendor)[0]) if vendor else gpu_fit.usable_cards(list(cards or []))
keep = [c for c in keep if float(c.get("vram_gb") or 0) >= SMALLEST_CARD_GB]
+80
View File
@@ -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()
+2 -2
View File
@@ -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",
+171
View File
@@ -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())
+1 -1
View File
@@ -373,7 +373,7 @@ class OfficialSets(unittest.TestCase):
self.assertEqual(p["args"], [])
p = dsv41_fit.plan([RTX5070], 64.0, 100.0, model=ISTA)
text = " ".join(p["lines"]) + " ".join(p["warnings"])
for needle in ("4.7", "7.2", "31 tokens/s", "free disk space", "only once" if False else "downloads it once"):
for needle in ("4.7", "7.2", "31 tokens a second", "free disk space", "only once" if False else "downloads it once"):
self.assertIn(needle, text)
self.assertFalse(p["disk_ok"])
p225 = dsv41_fit.plan([RTX5070], 64.0, None, model="IST-DAS-2.25bpw")