313 lines
15 KiB
Python
313 lines
15 KiB
Python
"""Tests for DeepSeek-V4.1-Flash in setup.py (MULTI_FILE_FAMILIES: nine pinned shards, its own engine strata-dsv41, several cards) and
|
|
for strata_control/dsv41_fit.py (where it fits, from the real numbers). Mocked engine, downloads and processes - nothing is built,
|
|
fetched or started.
|
|
|
|
python -m unittest tools.test_setup_dsv41
|
|
"""
|
|
from __future__ import annotations
|
|
|
|
import argparse
|
|
import contextlib
|
|
import io
|
|
import json
|
|
import re
|
|
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))
|
|
sys.path.insert(0, str(ROOT / "tools"))
|
|
import setup # noqa: E402
|
|
from strata_control import dsv41_fit # noqa: E402
|
|
|
|
SHA = re.compile(r"/resolve/[0-9a-f]{40}/")
|
|
REPO = "unsloth/DeepSeek-V4.1-Flash-1017-GGUF"
|
|
R9700 = {"name": "AMD Radeon AI PRO R9700", "vram_gb": 32.0, "vendor": "amd"}
|
|
FOUR = [{**R9700, "index": i} for i in range(4)]
|
|
|
|
|
|
def args(**kw):
|
|
base = dict(family="deepseek41", model=None, check=False, yes=True, vision=None, models_dir=None, gguf_dir=None, context=None,
|
|
rope_scaling=None, rope_scale=None, kv=None, resident_budget_gib=None, low_ram="auto", vram_reserve_mib=None,
|
|
host=None, api_key=None, browser=None, no_start=True, data_dir=None, gpu=None, gpus=None)
|
|
base.update(kw)
|
|
return argparse.Namespace(**base)
|
|
|
|
|
|
def run(fn, *a, **kw):
|
|
out = io.StringIO()
|
|
with contextlib.redirect_stdout(out):
|
|
try:
|
|
return fn(*a, **kw), out.getvalue()
|
|
except SystemExit as e:
|
|
return e.code, out.getvalue()
|
|
|
|
|
|
class Entries(unittest.TestCase):
|
|
def test_pinned_and_hashed(self):
|
|
fam = setup.MULTI_FILE_FAMILIES["deepseek41"]
|
|
self.assertEqual(setup.HF_REVISIONS[REPO], "f924b94f19c1fea02c06a65c8475b0a45e2b88b1")
|
|
self.assertRegex(fam["hf"], SHA)
|
|
self.assertTrue(fam["hf"].endswith(f"{REPO}/resolve/f924b94f19c1fea02c06a65c8475b0a45e2b88b1/UD-Q8_K_XL/"))
|
|
self.assertIs(setup.REPO_PINS[REPO], setup.DSV41_FILES)
|
|
shards = fam["models"]["UD-Q8_K_XL"]["shards"]
|
|
self.assertEqual(len(shards), 9)
|
|
self.assertEqual(sum(setup.DSV41_FILES[n][0] for n in shards), 508_376_763_197) # the nine shards: the pinned total
|
|
self.assertEqual(setup.DSV41_FILES[shards[7]][0], dsv41_fit.ENGRAM_BYTES) # shard 8 is the n-gram table
|
|
for n, (size, sha) in setup.DSV41_FILES.items():
|
|
self.assertRegex(sha, r"^[0-9a-f]{64}$", n)
|
|
self.assertGreater(size, 5e6)
|
|
self.assertEqual(setup.DSV41_FILES["mmproj-BF16.gguf"][0], 972_208_288)
|
|
|
|
def test_not_a_pack_or_single_file_family(self):
|
|
self.assertNotIn("deepseek41", setup.FAMILIES)
|
|
self.assertNotIn("deepseek41", setup.OWN_PATH_FAMILIES)
|
|
self.assertNotIn("UD-Q8_K_XL", setup.MODELS)
|
|
|
|
def test_config_read_back(self):
|
|
with tempfile.TemporaryDirectory() as d:
|
|
p = Path(d) / "strata-deepseek-v4.1-flash-ud-q8_k_xl.json"
|
|
p.write_text(json.dumps({"args": ["--pack", "x", "--max-context", "8192", "--tp", "2"], "port": 8080, "gpu": [0, 1, 2, 3]}))
|
|
ch = setup.choices_from_config(p)
|
|
self.assertEqual((ch["family"], ch["model"], ch["context"], ch["gpu"]), ("deepseek41", "UD-Q8_K_XL", 8192, [0, 1, 2, 3]))
|
|
|
|
|
|
class Fit(unittest.TestCase):
|
|
def test_the_numbers_of_the_model(self):
|
|
self.assertAlmostEqual(dsv41_fit.WEIGHTS_GB, 305.6, delta=0.1)
|
|
self.assertAlmostEqual(dsv41_fit.EXPERTS_GB, 288.8, delta=0.1)
|
|
self.assertAlmostEqual(dsv41_fit.ENGRAM_GB, 202.8, delta=0.1)
|
|
self.assertAlmostEqual(dsv41_fit.DOWNLOAD_GB, 508.4, delta=0.1)
|
|
self.assertEqual(dsv41_fit.EXPERTS, 15360)
|
|
|
|
def test_four_r9700_match_the_measured_run(self):
|
|
# r9700, 2026-10-20: 1,558 + 1,559 + 1,575 + 1,505 = 6,197 experts in graphics memory, 9,163 (172.3 GB) on the CPU lane
|
|
p = dsv41_fit.plan(FOUR, 251.0, 900.0)
|
|
self.assertEqual(p["verdict"], "fits")
|
|
self.assertAlmostEqual(p["experts_in_vram"], 6197, delta=100)
|
|
self.assertAlmostEqual(p["cpu_experts_gb"], 172.3, delta=2.5)
|
|
self.assertEqual([c["layers"] for c in p["cards"]], [[0, 9], [10, 19], [20, 29], [30, 39]])
|
|
self.assertEqual(p["args"], ["--tp", "2"])
|
|
self.assertTrue(p["disk_ok"])
|
|
|
|
def test_one_card_and_little_ram_are_warnings(self):
|
|
p = dsv41_fit.plan([{**R9700, "index": 0}], 64.0, 100.0)
|
|
self.assertEqual(p["verdict"], "spills")
|
|
self.assertEqual(p["args"], [])
|
|
self.assertGreater(p["spill_gb"], 200)
|
|
text = " ".join(p["warnings"])
|
|
self.assertIn("read from the SSD", text)
|
|
self.assertIn("free disk space", text)
|
|
self.assertIn("still allowed", text)
|
|
self.assertFalse(p["disk_ok"])
|
|
|
|
def test_a_card_too_small_for_the_dense_weights_is_said(self):
|
|
p = dsv41_fit.plan([{"index": 0, "name": "RTX 3060", "vram_gb": 12.0}], 128.0)
|
|
self.assertTrue(any("dense weights" in w for w in p["warnings"]))
|
|
|
|
def test_no_card(self):
|
|
p = dsv41_fit.plan([], 128.0)
|
|
self.assertEqual(p["verdict"], "no_card")
|
|
self.assertTrue(any("No graphics card" in w for w in p["warnings"]))
|
|
|
|
def test_cards_of_two_sizes_share_the_layers_in_proportion(self):
|
|
cards = [{**R9700, "index": 0}, {"name": "RTX A4000", "vram_gb": 16.0, "index": 1}]
|
|
p = dsv41_fit.plan(cards, 256.0)
|
|
a, b = p["cards"]
|
|
self.assertGreater(a["layers"][1] - a["layers"][0], b["layers"][1] - b["layers"][0])
|
|
self.assertEqual(b["layers"][1], 39)
|
|
self.assertGreater(a["experts"], b["experts"])
|
|
|
|
def test_split_layers_covers_every_layer(self):
|
|
for w in ([1, 1, 1, 1], [32, 16], [8, 8, 8], [32, 32, 16, 12, 12]):
|
|
r = dsv41_fit.split_layers(w)
|
|
self.assertEqual(r[0][0], 0)
|
|
self.assertEqual(r[-1][1], 39)
|
|
self.assertTrue(all(a[1] + 1 == b[0] for a, b in zip(r, r[1:])))
|
|
|
|
def test_options_like_gpu_fit(self):
|
|
o = dsv41_fit.options(FOUR, 251.0, "amd")
|
|
self.assertEqual(o["recommended"], "all")
|
|
self.assertEqual([x["key"] for x in o["options"]], ["all", "single"])
|
|
self.assertEqual(o["options"][0]["cards"], [0, 1, 2, 3])
|
|
self.assertTrue(o["options"][0]["recommended"])
|
|
|
|
|
|
class Install(unittest.TestCase):
|
|
def setUp(self):
|
|
self.tmp = tempfile.TemporaryDirectory()
|
|
self.addCleanup(self.tmp.cleanup)
|
|
self.root = Path(self.tmp.name)
|
|
self.calls = []
|
|
self.cfg = {}
|
|
self.exe = self.root / "engine" / ("strata-dsv41.exe" if setup.WIN else "strata-dsv41")
|
|
eng = self.root / "engine"
|
|
|
|
def engine_step(a, gpu, hip, amd, cuda_tk, vision):
|
|
self.calls.append(("engine", vision))
|
|
eng.mkdir(parents=True, exist_ok=True)
|
|
return self.root / "llama", eng, {"version": "0.2.2"}, ["/libs"], gpu, vision
|
|
|
|
def write_cfg(path, cfg, source=None, explicit=None):
|
|
self.cfg = cfg
|
|
self.calls.append(("config", path.name))
|
|
|
|
patches = [mock.patch.object(setup, "download", lambda url, dst, what=None: self.calls.append(("get", url, dst))),
|
|
mock.patch.object(setup, "verify_sha256", lambda s, size, sha, own=False: self.calls.append(("sha", s.name, own))),
|
|
mock.patch.object(setup, "free_gb", lambda p: 900.0),
|
|
mock.patch.object(setup, "pip_install", lambda *a, **k: None),
|
|
mock.patch.object(setup, "engine_step", engine_step),
|
|
mock.patch.object(setup, "run", lambda cmd, **k: self.calls.append(("run", cmd))),
|
|
mock.patch.object(setup, "data_folder", lambda d, *a, **k: (self.root / "data", [])),
|
|
mock.patch.object(setup, "write_setup_config", write_cfg),
|
|
mock.patch.object(setup, "write_run_script", lambda tag, p, port, o=True: self.root / f"run-{tag}.sh"),
|
|
mock.patch.object(setup, "ROOT", self.root),
|
|
mock.patch.object(setup, "home", lambda: self.root)]
|
|
for p in patches:
|
|
p.start()
|
|
self.addCleanup(p.stop)
|
|
mock.patch.dict("os.environ", {"STRATA_DSV41_EXE": ""}).start()
|
|
self.addCleanup(mock.patch.stopall)
|
|
|
|
def install(self, ram=251.0, cards=None, engine=True, **kw):
|
|
if engine:
|
|
self.exe.parent.mkdir(parents=True, exist_ok=True)
|
|
self.exe.write_text("x")
|
|
cards = cards or FOUR
|
|
gpu = {"index": 0, "count": len(cards), "vram_gb": 32.0, "name": "R9700", "cards": cards}
|
|
return run(setup.install_dsv41, args(models_dir=str(self.root / "models"), **kw), "deepseek41", gpu, True, [], 13, [], 8080, ram,
|
|
None, cards)
|
|
|
|
def test_installs_the_nine_pinned_shards_and_writes_the_config(self):
|
|
rc, out = self.install()
|
|
self.assertEqual(rc, 0, out)
|
|
get = [c for c in self.calls if c[0] == "get"]
|
|
folder = self.root / "models" / "deepseek-v4.1-flash-UD-Q8_K_XL"
|
|
self.assertEqual([c[2] for c in get], [folder / setup.DSV41_SHARD.format(i=i) for i in range(1, 10)])
|
|
self.assertEqual(get[1][1], setup.hf(REPO) + "UD-Q8_K_XL/" + setup.DSV41_SHARD.format(i=2))
|
|
self.assertEqual(sum(1 for c in self.calls if c[0] == "sha"), 9)
|
|
pack = [c for c in self.calls if c[0] == "run"][-1][1]
|
|
self.assertIn("pack_gguf.py", " ".join(map(str, pack)))
|
|
self.assertEqual(self.cfg["arch"], "deepseek41")
|
|
self.assertEqual(self.cfg["exe"], str(self.exe))
|
|
self.assertEqual(self.cfg["model_name"], "deepseek-v4.1-flash")
|
|
a = self.cfg["args"]
|
|
self.assertEqual(a[a.index("--max-context") + 1], "8192")
|
|
self.assertEqual(a[a.index("--eos") + 1], "1")
|
|
self.assertEqual(a[a.index("--tp") + 1], "2")
|
|
self.assertTrue(Path(a[a.index("--pack") + 1]).match("*packs/deepseek-v4.1-flash-ud-q8_k_xl"))
|
|
self.assertEqual(self.cfg["gpu"], [0, 1, 2, 3])
|
|
self.assertEqual(self.cfg["engine_ready_s"], 3600)
|
|
self.assertEqual(self.cfg["backend"], "hip")
|
|
self.assertTrue(self.cfg["tokenizer"].endswith("tokenizer"))
|
|
self.assertNotIn("--vision", a)
|
|
self.assertNotIn("--mtp", a)
|
|
self.assertIn("4 cards, one block of layers on each", out)
|
|
self.assertIn("n-gram table", out)
|
|
|
|
def test_no_engine_stops_before_the_download(self):
|
|
rc, out = self.install(engine=False)
|
|
self.assertEqual(rc, 1, out)
|
|
self.assertIn("no DeepSeek-V4.1 engine (strata-dsv41)", out)
|
|
self.assertIn("nothing has been downloaded", out)
|
|
self.assertFalse([c for c in self.calls if c[0] == "get"])
|
|
|
|
def test_own_files_are_checked_not_downloaded(self):
|
|
own = self.root / "own"
|
|
own.mkdir()
|
|
for i in range(1, 10):
|
|
(own / setup.DSV41_SHARD.format(i=i)).write_bytes(b"GGUF")
|
|
rc, out = self.install(gguf_dir=str(own))
|
|
self.assertEqual(rc, 0, out)
|
|
self.assertFalse([c for c in self.calls if c[0] == "get"])
|
|
self.assertTrue(all(c[2] for c in self.calls if c[0] == "sha")) # own=True: setup never deletes the person's file
|
|
pack = [c for c in self.calls if c[0] == "run"][-1][1]
|
|
self.assertIn(str(own), pack)
|
|
|
|
def test_a_missing_own_shard_is_said(self):
|
|
own = self.root / "own"
|
|
own.mkdir()
|
|
for i in (1, 2, 3):
|
|
(own / setup.DSV41_SHARD.format(i=i)).write_bytes(b"GGUF")
|
|
rc, out = self.install(gguf_dir=str(own))
|
|
self.assertEqual(rc, 1, out)
|
|
self.assertIn("00004-of-00009.gguf is missing", out)
|
|
|
|
def test_the_repository_layout_is_found(self):
|
|
own = self.root / "repo"
|
|
(own / "UD-Q8_K_XL").mkdir(parents=True)
|
|
for i in range(1, 10):
|
|
(own / "UD-Q8_K_XL" / setup.DSV41_SHARD.format(i=i)).write_bytes(b"GGUF")
|
|
rc, out = self.install(gguf_dir=str(own))
|
|
self.assertEqual(rc, 0, out)
|
|
pack = [c for c in self.calls if c[0] == "run"][-1][1]
|
|
self.assertIn(str(own / "UD-Q8_K_XL"), pack)
|
|
|
|
def test_little_ram_is_asked_and_kept_with_an_explicit_model(self):
|
|
rc, out = self.install(ram=64.0, cards=FOUR[:1], model="UD-Q8_K_XL")
|
|
self.assertEqual(rc, 0, out)
|
|
self.assertIn("read from the SSD", out)
|
|
self.assertIn("installing UD-Q8_K_XL with 64 GB of RAM, as you chose", out)
|
|
self.assertEqual(self.cfg["args"][self.cfg["args"].index("--max-context") + 1], "8192")
|
|
self.assertNotIn("--tp", self.cfg["args"])
|
|
|
|
def test_little_ram_with_yes_alone_stops_where_the_rule_says(self):
|
|
rc, out = self.install(ram=64.0, cards=FOUR[:1])
|
|
self.assertEqual(rc, 1, out)
|
|
self.assertIn("more RAM or cards fix it", out)
|
|
self.assertFalse([c for c in self.calls if c[0] == "get"])
|
|
|
|
def test_a_disk_that_cannot_hold_the_files_is_the_one_hard_stop(self):
|
|
with mock.patch.object(setup, "free_gb", lambda p: 100.0):
|
|
rc, out = self.install()
|
|
self.assertEqual(rc, 1, out)
|
|
self.assertIn("not enough free disk space", out)
|
|
self.assertIn("an NVMe drive is best", out)
|
|
self.assertFalse([c for c in self.calls if c[0] == "get"])
|
|
|
|
def test_pictures_and_flash_next_settings_are_said(self):
|
|
rc, out = self.install(vision="yes", kv="int8", context=131072)
|
|
self.assertEqual(rc, 0, out)
|
|
self.assertIn("cannot read pictures in this Strata version", out)
|
|
self.assertIn("Flash-Next settings", out)
|
|
self.assertEqual(self.cfg["args"][self.cfg["args"].index("--max-context") + 1], "131072")
|
|
self.assertIn("checked at 4K and 8K", out)
|
|
|
|
|
|
class Catalog(unittest.TestCase):
|
|
def test_the_mcp_catalog_lists_the_size(self):
|
|
import strata_mcp as M
|
|
s = M.Strata(ROOT)
|
|
fam = next(f for f in s.model_table({"ram_gb": 251.0, "gpus": [{**c, "usable": True} for c in FOUR], "engine_vendor": "amd"})
|
|
if f["family"] == "deepseek41")
|
|
(size,) = fam["sizes"]
|
|
self.assertEqual((size["model"], size["id"], size["download_gb"], size["images"]), ("UD-Q8_K_XL", "deepseek-v4.1-flash-ud-q8_k_xl", 508.4, False))
|
|
self.assertTrue(size["on_this_pc"].startswith("fits"))
|
|
self.assertTrue(size["mem_total"] and size["multi_file"])
|
|
self.assertEqual(size["gpu_options"]["recommended"], "all")
|
|
small = next(f for f in s.model_table({"ram_gb": 32.0, "gpus": []}) if f["family"] == "deepseek41")
|
|
self.assertTrue(small["sizes"][0]["on_this_pc"].startswith("does not fit"))
|
|
|
|
def test_the_install_plan(self):
|
|
import strata_mcp as M
|
|
s = M.Strata(ROOT)
|
|
s.hardware = lambda fresh=False: {"ram_gb": 251.0, "gpus": [{**c, "usable": True} for c in FOUR], "engine_vendor": "amd",
|
|
"cpu": {"avx2": True}}
|
|
s.disk_free = lambda p: 2000.0
|
|
plan, a = M.Tools(s).install_plan("deepseek41", "UD-Q8_K_XL", None, None, None, None, None, None, None, None, None)
|
|
self.assertEqual((plan["title"], plan["download_gb"], plan["context"], plan["images"]), ("DeepSeek-V4.1-Flash UD-Q8_K_XL", 508.4, 8192, "no"))
|
|
self.assertEqual(a[:6], ["--yes", "--no-start", "--family", "deepseek41", "--model", "UD-Q8_K_XL"])
|
|
self.assertTrue(plan["ram"].startswith("fits"))
|
|
for flag in ("--kv", "--low-ram", "--experimental-speed-projection"):
|
|
self.assertNotIn(flag, a)
|
|
files = M.expected_files(s.setup_module(), "deepseek41", "UD-Q8_K_XL", Path("/m"), False)
|
|
self.assertEqual(len(files), 9)
|
|
self.assertEqual(sum(f["bytes"] for f in files), 508_376_763_197)
|
|
|
|
|
|
if __name__ == "__main__":
|
|
unittest.main()
|