diff --git a/docs/DEEPSEEK41.md b/docs/DEEPSEEK41.md index 418a57e4..197eb40e 100644 --- a/docs/DEEPSEEK41.md +++ b/docs/DEEPSEEK41.md @@ -109,7 +109,12 @@ The server speaks to the engine as it does for the other models, so `/v1/chat/co (`STRATA_DS4_SLOT_GB`) and are removed after a week. The system prompt and tools shared by an agent and its sub-agents are kept as a checkpoint too. `STRATA_DS4_SLOT_KEEP=0` and `STRATA_DS4_AUTO_PIN=0` turn this off. -Not in this version: several requests at once (`--parallel` is ignored with a warning) and pictures (`--vision` is ignored with a warning). +Pictures are on by default: the engine reads them with its own vision tower (319 MiB of graphics memory, loaded with the model; no separate +picture reader). The server decodes each picture, brings it to a size the tower takes (both sides a multiple of 42, at most 1024 image +tokens) and hands it to the engine; it needs Pillow for that (`python -m pip install pillow`; setup installs it). `--vision no` in `strata launch` +(or `--vision off` for the engine) turns them off; a request with a picture then gets a 400 that says so. + +Not in this version: several requests at once (`--parallel` is ignored with a warning). ## Speed: Faster or Full quality diff --git a/docs/TERMINAL_LAUNCH.md b/docs/TERMINAL_LAUNCH.md index 3d5834d9..a33938de 100644 --- a/docs/TERMINAL_LAUNCH.md +++ b/docs/TERMINAL_LAUNCH.md @@ -139,7 +139,7 @@ strata launch --family deepseek41 --model ist-das-2.00bpw - The context is 8192 tokens (8K), the size the engine was checked at. - `--speed faster|full` picks how many of the model's 6 experts it uses per word. Faster uses 5 (a small quality change, usually not noticeable) and is the default on a PC with 16 GB of graphics memory or less and on PCs whose graphics share the computer's memory. Full uses all 6. `--full-quality` is the same as `--speed full`. Both need DeepSeek engine 0.1.50 or newer, which `strata launch` fetches. The engine's own flag is `--experts N`. - It answers one request at a time. `--parallel` above 1 is ignored with a note. -- It cannot read pictures, so pictures are off and `--vision on` is ignored with a note. +- It reads pictures (on by default) with the engine's own vision tower, loaded with the model; `--vision off` turns them off. Setup fetches the tower's weights (mmproj-BF16.gguf, 1 GB) with the other shared files and packs them with the model. - `--tune` runs DeepSeek's own short tuner (drafter on or off, CPU threads), not the Qwen tuner. - `--model-path DIR` starts from the shards you already have. - `--plan` shows where the model goes on your cards and how much is held in RAM. diff --git a/serve/ds_pictures.py b/serve/ds_pictures.py new file mode 100644 index 00000000..236a61dc --- /dev/null +++ b/serve/ds_pictures.py @@ -0,0 +1,199 @@ +"""Pictures for DeepSeek-V4.1-Flash through the engine's OWN vision tower (strata-dsv41 --vision on, the default). + +The Qwen engines get pictures from a separate reader process (strata-vision): it turns a picture into embeddings and the server writes +those into the file a `GENI` line names. strata-dsv41 carries its own 319 MiB tower, so the server only has to + 1. decode each picture to RGB and bring it to a size the tower takes (both sides a multiple of 42 = patch 14 x merge 3, at most + `vision.max_image_tokens` image tokens, at least `vision.min_pixels` pixels), + 2. put one run of image tokens into the prompt where the template wrote its single `<|deepseek_image|>` placeholder: the run is + n_h * (n_w + 1) + 2 positions long (the picture's rows, a newline marker per row, the start and end markers), and + 3. write the pictures into the file of a `GENI` line (`DSI1`: int32 count, then per picture int64 start, int32 width, int32 height + and its height x width x 3 RGB bytes), which the engine reads and encodes on its own GPU. +Decoding needs Pillow (the server asks for it by name when it is missing). Nothing here starts a process or touches a GPU. +""" +from __future__ import annotations + +import io +import os +import struct +import tempfile +from pathlib import Path + +PATCH = 14 +MERGE = 3 +MAX_TOKENS = 1024 +MIN_PIXELS = 0 +OFF_WORDS = ("off", "no", "false", "0") + + +def pack_keys(pack_dir) -> dict: + """The pack's ds4.txt as {key: value-string} ({} when it cannot be read).""" + out = {} + try: + for line in (Path(pack_dir) / "ds4.txt").read_text(encoding="utf-8", errors="replace").splitlines(): + k, _, v = line.partition(" ") + if k: + out[k] = v.strip() + except OSError: + pass + return out + + +def args_after(args, flag): + """The value that follows `flag` in an engine argument list (None when absent).""" + for i, a in enumerate(args or []): + if a == flag and i + 1 < len(args): + return str(args[i + 1]) + return None + + +def vision_wanted(args) -> bool: + """`--vision off` (or no/false/0) turns pictures off; everything else, including no flag at all, leaves them on.""" + v = args_after(args, "--vision") + return v is None or v.strip().lower() not in OFF_WORDS + + +def span_length(width: int, height: int, patch: int = PATCH, merge: int = MERGE) -> int: + """The image tokens one picture takes in the prompt: n_h * (n_w + 1) + 2 (the engine's own count, ds4_model.cu encode).""" + gh, gw = height // patch, width // patch + n_h, n_w = -(-gh // merge), -(-gw // merge) + return n_h * (n_w + 1) + 2 + + +def fit_size(width: int, height: int, max_tokens: int = MAX_TOKENS, min_pixels: int = MIN_PIXELS, + patch: int = PATCH, merge: int = MERGE) -> tuple[int, int]: + """The size to resize a width x height picture to: its own shape, both sides a multiple of patch * merge, scaled down until its + span is within `max_tokens` and up until it has `min_pixels` (when that fits the budget). Never smaller than one cell.""" + cell = patch * merge + if width <= 0 or height <= 0: + raise ValueError("the picture has no size") + + def snap(scale: float) -> tuple[int, int]: + return max(cell, int(round(width * scale / cell)) * cell), max(cell, int(round(height * scale / cell)) * cell) + + scale = 1.0 + if width * height < min_pixels: + scale = (min_pixels / (width * height)) ** 0.5 + w, h = snap(scale) + while span_length(w, h, patch, merge) > max_tokens: # shrink in small steps until the span fits the engine's limit + scale *= 0.95 + w, h = snap(scale) + if w == cell and h == cell: + break + return w, h + + +class DsPictures: + """What the Service holds as `vision` for a DeepSeek-V4.1 engine that reads pictures itself. It has the few members the + Service asks of a picture encoder (alive, elastic, restart, unload, dir) and does the DeepSeek-specific work in build().""" + + elastic = False + elastic_mib = 0 + + def __init__(self, image_token: int, max_tokens: int = MAX_TOKENS, min_pixels: int = MIN_PIXELS, tmp_dir=None, + patch: int = PATCH, merge: int = MERGE): + self.image_token = int(image_token) + self.max_tokens = int(max_tokens) + self.min_pixels = int(min_pixels) + self.patch, self.merge = int(patch), int(merge) + self.dir = Path(tmp_dir) if tmp_dir else Path(tempfile.gettempdir()) + + @classmethod + def from_config(cls, cfg: dict, tmp_dir=None): + """-> DsPictures, or None: pictures are off (`--vision off`), or the pack has no vision tower (no vision.* keys / image token).""" + args = cfg.get("args") or [] + if not vision_wanted(args): + return None + pack = args_after(args, "--pack") + if not pack: + return None + k = pack_keys(pack) + try: + if int(k.get("vision.num_hidden_layers", "0")) <= 0 or "image_token_id" not in k: + return None + return cls(int(k["image_token_id"]), int(k.get("vision.max_image_tokens", MAX_TOKENS)), + int(float(k.get("vision.min_pixels", MIN_PIXELS))), tmp_dir, + int(k.get("vision.patch_size", PATCH)), int(k.get("vision.downsample_ratio", MERGE))) + except ValueError: + return None + + # ---- the members the Service asks of any picture encoder + def alive(self) -> bool: + return True + + def restart(self) -> None: + pass + + def unload(self) -> None: + pass + + def stop(self) -> None: + pass + + # ---- the work + @staticmethod + def decode(data: bytes): + """-> a Pillow RGB image (upright by its EXIF tag; transparency over white).""" + try: + from PIL import Image, ImageOps + except ImportError: + raise ValueError("DeepSeek reads pictures with Pillow, which is not installed here (python -m pip install pillow)") from None + try: + im = Image.open(io.BytesIO(data)) + im.load() + im = ImageOps.exif_transpose(im) + except Exception as e: # noqa: BLE001 - any decoder failure is the picture's + raise ValueError(f"the image could not be read ({e})") from None + if im.mode in ("RGBA", "LA") or (im.mode == "P" and "transparency" in im.info): + im = im.convert("RGBA") + bg = Image.new("RGB", im.size, (255, 255, 255)) + bg.paste(im, mask=im.split()[-1]) + return bg + return im if im.mode == "RGB" else im.convert("RGB") + + def prepare(self, data: bytes) -> tuple[int, int, bytes]: + """-> (width, height, RGB bytes) of the picture at the size the tower takes.""" + im = self.decode(data) + w, h = fit_size(im.width, im.height, self.max_tokens, self.min_pixels, self.patch, self.merge) + if (w, h) != im.size: + from PIL import Image + im = im.resize((w, h), Image.LANCZOS) + return w, h, im.tobytes() + + def encode(self, source) -> None: + """Checks that a picture can be read and sized (the Service's pre-check of tool pictures); nothing is stored.""" + data = source if isinstance(source, bytes) else None + if data is None: + from serve.server import Vision + data = Vision.load(source) + self.prepare(data) + + def build(self, ids: list[int], pictures: list[bytes]) -> tuple[list[int], str]: + """The prompt ids with each placeholder expanded to its picture's span, and the path of the DSI1 file for the GENI line. + `pictures`: the bytes of every image of the request, in prompt order.""" + marks = [i for i, t in enumerate(ids) if t == self.image_token] + if len(marks) != len(pictures): + raise ValueError("the prompt and its images do not match") + out: list[int] = [] + entries = [] + last = 0 + for pos, data in zip(marks, pictures): + w, h, rgb = self.prepare(data) + out += ids[last:pos] + entries.append((len(out), w, h, rgb)) + out += [self.image_token] * span_length(w, h, self.patch, self.merge) + last = pos + 1 + out += ids[last:] + self.dir.mkdir(parents=True, exist_ok=True) + fd, path = tempfile.mkstemp(prefix="strata-ds-pics-", suffix=".dsi", dir=str(self.dir)) + try: + with os.fdopen(fd, "wb") as f: + f.write(b"DSI1" + struct.pack(" in sampling_defaults=sampling_defaults, fit_max_tokens=a.fit_max_tokens or (not a.no_fit_max_tokens and cfg.get("fit_max_tokens") is not False)) svc.launch_attestation = launch_attestation + if (cfg.get("arch") or "") == "deepseek41" and vision is None and a.engine == "strata": + # DeepSeek-V4.1 reads pictures with the engine's own tower (--vision on, the default): no separate reader process. + # `--vision off` in the config's args, or a pack without a tower, leaves svc.vision None: pictures are refused with the 400 as before. + _dsp = DsPictures.from_config(cfg, tmp_dir=Path(cfg["log"]).parent / "ds-pictures" if cfg.get("log") else None) + if _dsp is not None: + svc.vision = _dsp + print("[strata] pictures: on (DeepSeek-V4.1 reads them with the engine's own vision tower; --vision off turns them off)", flush=True) svc.offline = a.offline svc.max_queue = max_queue svc.queue_timeout_s = float(queue_timeout) if queue_timeout is not None else None diff --git a/serve/test_ds_pictures.py b/serve/test_ds_pictures.py new file mode 100644 index 00000000..a997d5c5 --- /dev/null +++ b/serve/test_ds_pictures.py @@ -0,0 +1,243 @@ +"""serve/test_ds_pictures.py: DeepSeek-V4.1 pictures through the engine's own tower (serve/ds_pictures.py): the size rule, the span, the +config switch, the prompt expansion and the DSI1 file the engine reads. CPU only; the decode tests need Pillow and skip without it.""" +import io +import os +import struct +import sys +import tempfile +import unittest +from pathlib import Path + +sys.path.insert(0, str(Path(__file__).resolve().parents[1])) +from serve import ds_pictures as D # noqa: E402 + +try: + from PIL import Image +except ImportError: # pragma: no cover + Image = None + +IMG = 129264 + + +def png(w, h, color=(220, 20, 20)) -> bytes: + im = Image.new("RGB", (w, h), color) + out = io.BytesIO() + im.save(out, format="PNG") + return out.getvalue() + + +class Geometry(unittest.TestCase): + def test_span_matches_the_engine(self): + # mkimg.py's 672 x 672 picture: 16 x 17 + 2 = 274 positions + self.assertEqual(D.span_length(672, 672), 274) + self.assertEqual(D.span_length(42, 42), 1 * 2 + 2) + self.assertEqual(D.span_length(14, 14), 1 * 2 + 2) # a partial merge cell still counts one + + def test_sizes_are_multiples_of_42_within_the_budget(self): + for w, h in ((100, 100), (672, 672), (4000, 3000), (3000, 4000), (1920, 1080), (50, 3000)): + nw, nh = D.fit_size(w, h) + self.assertEqual((nw % 42, nh % 42), (0, 0), (w, h)) + self.assertLessEqual(D.span_length(nw, nh), D.MAX_TOKENS, (w, h)) + self.assertGreaterEqual(min(nw, nh), 42) + + def test_small_pictures_grow_to_min_pixels(self): + w, h = D.fit_size(200, 200, min_pixels=295936) + self.assertGreaterEqual(w * h, 295936 * 0.8) # rounded to the 42 grid + + def test_a_huge_picture_shrinks_to_the_limit(self): + w, h = D.fit_size(8000, 6000, 1024) + self.assertLessEqual(D.span_length(w, h), 1024) + self.assertGreater(D.span_length(w, h), 800) # but not far below it + + +class Config(unittest.TestCase): + def pack(self, tower=True): + d = tempfile.mkdtemp() + lines = ["hidden_size 5120"] + if tower: + lines += ["vision.num_hidden_layers 32", "vision.patch_size 14", "vision.downsample_ratio 3", "vision.max_image_tokens 1024", + "vision.min_pixels 295936", f"image_token_id {IMG}"] + Path(d, "ds4.txt").write_text("\n".join(lines) + "\n", encoding="utf-8") + return d + + def test_on_by_default(self): + p = D.DsPictures.from_config({"args": ["--pack", self.pack(), "--max-context", "8192"]}) + self.assertIsNotNone(p) + self.assertEqual((p.image_token, p.max_tokens, p.min_pixels), (IMG, 1024, 295936)) + + def test_vision_on_flag(self): + self.assertIsNotNone(D.DsPictures.from_config({"args": ["--pack", self.pack(), "--vision", "on"]})) + + def test_vision_off_turns_it_off(self): + for word in ("off", "OFF", "no", "false", "0"): + self.assertIsNone(D.DsPictures.from_config({"args": ["--pack", self.pack(), "--vision", word]}), word) + + def test_a_pack_without_a_tower_has_none(self): + self.assertIsNone(D.DsPictures.from_config({"args": ["--pack", self.pack(tower=False)]})) + self.assertIsNone(D.DsPictures.from_config({"args": []})) + self.assertIsNone(D.DsPictures.from_config({"args": ["--pack", "/nonexistent"]})) + + +@unittest.skipIf(Image is None, "Pillow is not installed") +class Build(unittest.TestCase): + def setUp(self): + self.tmp = tempfile.mkdtemp() + self.p = D.DsPictures(IMG, 1024, 0, self.tmp) + + def read(self, path): + raw = Path(path).read_bytes() + self.assertEqual(raw[:4], b"DSI1") + n = struct.unpack("", parse_special=True)[0] + + def start(self, pictures=True): + engine = SeenEngine(self.tok, "ok") + vision = D.DsPictures(self.img, 1024, 0, tempfile.mkdtemp()) if pictures else None + svc = Service(engine, self.tok, ChatTemplate(TEMPLATE), model_name=default_model_name({"arch": "deepseek41"}), vision=vision) + svc.model_arch = "deepseek41" + svc.set_family("deepseek41") + httpd = serve(svc, port=0) + self.addCleanup(httpd.server_close) + self.addCleanup(httpd.shutdown) + self.addCleanup(frontend.use_family, None) + self.engine, self.svc = engine, svc + self.base = f"http://127.0.0.1:{httpd.server_address[1]}" + + def post(self, body): + req = urllib.request.Request(self.base + "/v1/chat/completions", data=json.dumps(body).encode(), + headers={"Content-Type": "application/json"}) + try: + with urllib.request.urlopen(req, timeout=60) as r: + return r.status, json.loads(r.read().decode()) + except urllib.error.HTTPError as e: + return e.code, json.loads(e.read().decode()) + + def picture_body(self): + url = "data:image/png;base64," + base64.b64encode(png(672, 672)).decode() + return {"messages": [{"role": "user", "content": [{"type": "image_url", "image_url": {"url": url}}, + {"type": "text", "text": "What colour is this?"}]}], "max_tokens": 8} + + def test_a_picture_reaches_the_engine_as_a_geni_file(self): + self.start() + code, out = self.post(self.picture_body()) + self.assertEqual(code, 200, out) + self.assertEqual(self.engine.seen_ids.count(self.img), 274) + raw = self.engine.seen_file + self.assertEqual(raw[:4], b"DSI1") + n, = struct.unpack(" where it comes from); they live in models/deepseek-v4.1-flash-shared, once +DSV41_MMPROJ = "mmproj-BF16.gguf" # the vision tower's weights (972 MB, pinned): packed into the model's pack when pictures are on (0.2.7) DSV41_SHARED = { + DSV41_MMPROJ: hf(DSV41_REPO), DSV41_ISTA_ENGRAM: hf(DSV41_ISTA_REPO), DSV41_SHARD.format(i=1): hf(DSV41_REPO) + "UD-Q8_K_XL/", # only its metadata is read (pack_gguf --engram-meta) DSV41_DSPARK: hf(DSV41_REPO), @@ -743,7 +753,7 @@ MULTI_FILE_FAMILIES = { "models": {"UD-Q8_K_XL": {"about": "Unsloth Dynamic 8-bit: the experts in MXFP4 as DeepSeek made them, the rest " "in BF16 (the checkpoint's own precision)", "ram_gb": 306, "shards": [DSV41_SHARD.format(i=i) for i in range(1, 10)]}}, - "sha256": DSV41_ALL_FILES, "vision": False} + "sha256": DSV41_ALL_FILES, "vision": True} # the engine's own tower (strata-dsv41 --vision on, the default), 0.2.7 } # The official sets first (a normal PC), Unsloth's 508 GB set last. Each model: its shards, the sub-folder they are in at the repository # (and in a --gguf-dir folder), the files shared with the other set ("shared"), the pack recipe and the shipped expert profile. @@ -8098,14 +8108,23 @@ def dsv41_find_shared(a, name: str, folder: Path, shared: Path) -> Path: return shared / name -def dsv41_pack_cmd(folder: Path, shards: list, engram: Path, meta: Path, dspark: Path, pack: Path) -> list: +def dsv41_pack_has_tower(pack: Path) -> bool: + """Does the pack carry the vision tower (pack_gguf.py writes vision.* and image_token_id into ds4.txt when it found the mmproj)?""" + try: + txt = (Path(pack) / "ds4.txt").read_text(encoding="utf-8", errors="replace") + except OSError: + return False + return "\nimage_token_id " in "\n" + txt and "vision.num_hidden_layers" in txt + + +def dsv41_pack_cmd(folder: Path, shards: list, engram: Path, meta: Path, dspark: Path, pack: Path, mmproj: Path | None = None) -> list: """The pack recipe for an official IST-DAS set - this and nothing else: another one (--skip-tensors / --dense-override + engram_fold) packs without an error and gives garbage text (P2003, 2026-10-23). The drafter's noise token (128799) and markov rank (256) are stated: the IST-DAS GGUFs do not carry them, and a pack with noise id 0 drafts deep less often (decode ~10% slower with DSpark). --shards names the four files, so an Engram or another set in the same folder is not read as part of the model.""" return [sys.executable, str(ROOT / "tools" / "dsv41" / "pack_gguf.py"), "--model", str(folder), "--shards", *[Path(n).name for n in shards], "--engram", str(engram), "--engram-meta", str(meta), "--dspark-gguf", str(dspark), - "--dspark-noise-token", "128799", "--dspark-markov-rank", "256", "--out", str(pack)] + "--dspark-noise-token", "128799", "--dspark-markov-rank", "256", *(["--mmproj", str(mmproj)] if mmproj else []), "--out", str(pack)] def dsv41_prefill_chunk_args(cards, ram_gb) -> list: @@ -8229,7 +8248,12 @@ def install_dsv41(a, family, gpu, hip, amd, cuda_tk, multi, port, ram, adopted, folder = dsv41_folder(a, fam, tag, model) shards = [dsv41_pick(folder, n) for n in md["shards"]] shared_dir = dsv41_shared_dir(a, fam) - shared = [dsv41_find_shared(a, n, folder, shared_dir) for n in md.get("shared", [])] + # Pictures are on by default (owner rule): the engine reads them with its own vision tower, which the pack carries when the tower's weights + # (mmproj-BF16.gguf, a shared file) were there when it was made. --vision no leaves the file out and the pack text-only. + ds_vision = "off" if a.vision in ("no", "off") else "on" + shared_names = list(md.get("shared", [])) + ([DSV41_MMPROJ] if ds_vision == "on" else []) + shared = [dsv41_find_shared(a, n, folder, shared_dir) for n in shared_names] + mmproj_file = shared[len(md.get("shared", []))] if ds_vision == "on" else None own_files = {f: bool(a.gguf_dir) or f.parent != shared_dir for f in shared} # a file found outside setup's own folder is the person's def partial(f: Path) -> int: @@ -8273,8 +8297,10 @@ def install_dsv41(a, family, gpu, hip, amd, cuda_tk, multi, port, ram, adopted, warn(f"little free disk space in {folder}: {free:.1f} GB free for about {left:.1f} GB. Setup goes on and should fit") if ctx > 65536: warn(f"{ctx // 1024}K tokens of context: the engine was checked at 4K and 8K; longer is kept as you chose, but untested") - if a.vision in ("yes", "gpu", "cpu"): - warn(f"{fam['title']} cannot read pictures in this Strata version (its picture reader is in development): images off") + # Pictures are on by default (owner rule): the engine reads them with its own vision tower (319 MiB, loaded with the model, no separate + # reader process). --vision no turns them off (--vision off for the engine); gpu / cpu name a Flash-Next reader and mean "on" here. + if a.vision in ("gpu", "cpu"): + warn(f"{fam['title']} reads pictures with the engine's own tower, so --vision {a.vision} is taken as yes: pictures on") if a.rope_scaling or a.rope_scale or a.kv or a.resident_budget_gib is not None or a.low_ram not in (None, "auto"): warn(f"--rope-scaling / --kv / --resident-budget-gib / --low-ram are Flash-Next settings: {fam['title']} does not use them") @@ -8322,7 +8348,12 @@ def install_dsv41(a, family, gpu, hip, amd, cuda_tk, multi, port, ram, adopted, download((md.get("hf") or fam["hf"]) + f.name, f) if f.name in DSV41_LEGACY_FILES: ok(f"{f.name}: a file of the first release of this set (the same weights); it is used as it is") - verify_sha256(f, *fam["sha256"][f.name], own=bool(a.gguf_dir) or f.name in DSV41_LEGACY_FILES) + pin = fam["sha256"][f.name] + new = DSV41_NEW_SHARD1.get(f.name) + if new and a.gguf_dir and f.is_file() and f.stat().st_size == new[0] and f.stat().st_size != pin[0]: # shard 1 with the 2026-10-26 chat template + pin = new + ok(f"{f.name}: the version with ISTA's newer chat template (the same weights); it is used as it is") + verify_sha256(f, *pin, own=bool(a.gguf_dir) or f.name in DSV41_LEGACY_FILES) for f in shared: # the Engram, the drafter, the metadata shard: once for every official set if f.exists() and (done(f) or own_files[f]): ok(f"{f.name} already here ({f.parent})") @@ -8335,6 +8366,10 @@ def install_dsv41(a, family, gpu, hip, amd, cuda_tk, multi, port, ram, adopted, data, _ = data_folder_for(a) pack = data / "packs" / tag.lower() # the engine's map of the shards, the hash tables, the tokenizer: a few MB have_pack = (pack / "tokenizer" / "vocab.json").exists() and (pack / "tensors.txt").exists() + if have_pack and ds_vision == "on" and mmproj_file is not None and not dsv41_pack_has_tower(pack): + warn("the pack from an earlier run was made without the picture reader: building it again with it (a few minutes)") + shutil.rmtree(pack, ignore_errors=True) + have_pack = False if have_pack and ista: # a pack from an earlier run is kept only when the engine finds all of its files good, why = dsv41_pack_info(exe, pack, lib_dirs) if not good: @@ -8343,9 +8378,10 @@ def install_dsv41(a, family, gpu, hip, amd, cuda_tk, multi, port, ram, adopted, have_pack = False if not have_pack: if ista: - run(dsv41_pack_cmd(folder, [f.name for f in shards], shared[0], shared[1], shared[2], pack)) + run(dsv41_pack_cmd(folder, [f.name for f in shards], shared[0], shared[1], shared[2], pack, mmproj_file)) else: - run([sys.executable, str(ROOT / "tools" / "dsv41" / "pack_gguf.py"), "--model", str(folder), "--out", str(pack)]) + run([sys.executable, str(ROOT / "tools" / "dsv41" / "pack_gguf.py"), "--model", str(folder), "--out", str(pack), + *(["--mmproj", str(mmproj_file)] if mmproj_file else [])]) if ista: prof = DSV41_PROFILES / md["profile"] # which experts are used most: the RAM store starts with them (the engine reads /expert-profile.bin) if prof.is_file() and not (pack / "expert-profile.bin").exists(): @@ -8381,7 +8417,7 @@ def install_dsv41(a, family, gpu, hip, amd, cuda_tk, multi, port, ram, adopted, # --dspark-conf 0.65: the drafts' confidence floor (H100 replay, DSPARK-DEFAULTS.md: within 0.01-0.02 of the per-class best, # never below 0.3). The engine has no flag for the draft block (3 in the same table): it reads the block size from the pack, # so only the floor is written. - args = ["--pack", str(pack), "--max-context", str(ctx), "--eos", "1", "--dspark", "auto", + args = ["--pack", str(pack), "--max-context", str(ctx), "--eos", "1", "--vision", ds_vision, "--dspark", "auto", *(["--dspark-conf", "0.65"] if md.get("recipe") == "ista" else []), *dsv41_prefill_chunk_args(cards, ram), *plan["args"]] env = dsv41_config_env(cards) diff --git a/strata_cli/launch/cmd.py b/strata_cli/launch/cmd.py index bd050072..6bb6caf9 100644 --- a/strata_cli/launch/cmd.py +++ b/strata_cli/launch/cmd.py @@ -346,13 +346,12 @@ def apply_defaults(s: S.LaunchSettings, catalog) -> tuple[S.LaunchSettings, list if s.vision == "off" and r.images and catalog.reader_state() == "missing": notices.append(S.Notice("warn", "The engine installed here has no picture reader, so pictures are off.", "Update the engine to read pictures, or add --vision off to silence this.")) - if catalog.strata.multi_family(s.family): # DeepSeek-V4.1-Flash: one request at a time, no tuning, no pictures yet + if catalog.strata.multi_family(s.family): # DeepSeek-V4.1-Flash: one request at a time, no tuning if s.parallel and s.parallel > 1: notices.append(S.Notice("warn", f"{s.model} answers one request at a time in this Strata version, so --parallel {s.parallel} is not used.", "")) 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" # (tuning is DeepSeek's own short tune: tools/dsv41/tune.py, run by the launch) + # pictures: on unless the person chose off - the engine reads them with its own vision tower, no separate reader + s.vision = "off" if s.vision == "off" else "on" # (tuning is DeepSeek's own short tune: tools/dsv41/tune.py, run by the launch) elif s.speed: notices.append(S.Notice("warn", "--speed and --full-quality are for DeepSeek; this model uses all its experts, so they are not used.", "")) s.speed = None diff --git a/strata_cli/launch/modelstate.py b/strata_cli/launch/modelstate.py index a2f81974..05218491 100644 --- a/strata_cli/launch/modelstate.py +++ b/strata_cli/launch/modelstate.py @@ -111,7 +111,10 @@ def assess(catalog, family: str | None, model: str | None, *, vision: str = "off mtp = mtp_choice(mtp, drafting) row = next((r for r in (rows if rows is not None else catalog.rows()) if r.family == family and r.model == model), None) if (family and model) else None st.total_gb = float(row.download_gb) if row else 0.0 - wants_pictures = vision != "off" and (row is None or row.images) + # DeepSeek reads pictures with its own engine's tower: nothing separate to fetch or to start (the tower's file is part of its setup) + multi = getattr(getattr(catalog, "strata", None), "multi_family", None) + own_tower = bool(family) and multi is not None and bool(multi(family)) + wants_pictures = vision != "off" and (row is None or row.images) and not own_tower data = Path(data_dir) if data_dir else None if model_path: st.own_files = True diff --git a/strata_cli/launch/runner.py b/strata_cli/launch/runner.py index 5b2a4b3c..d1461986 100644 --- a/strata_cli/launch/runner.py +++ b/strata_cli/launch/runner.py @@ -314,7 +314,10 @@ def apply_choices(cfg: dict, s: S.LaunchSettings) -> dict: kv = s.kv or ("int8" if (s.context or 32768) > 8192 else "fp16") if "--kv" in args or s.kv: put("--kv", kv) - if s.vision == "off": + if cfg.get("arch") == "deepseek41": # the engine's own tower: --vision on / off is its whole picture setting + if s.vision: + put("--vision", "off" if s.vision == "off" else "on") + elif s.vision == "off": args = [a for a in args if a != "--vision"] cfg.pop("vision", None) # the picture reader's own settings: the server adds --vision for them elif s.vision and "--vision" not in args: diff --git a/strata_cli/launch/screens.py b/strata_cli/launch/screens.py index 772b5518..96054165 100644 --- a/strata_cli/launch/screens.py +++ b/strata_cli/launch/screens.py @@ -1017,10 +1017,15 @@ class PicturesScreen(Base): s = self.ctx.settings return next((x for x in self.ctx.rows if (x.family, x.model) == (s.family, s.model)), None) + def own_tower(self) -> bool: + """DeepSeek reads pictures with a tower inside its own engine: no separate picture reader to look for.""" + s = self.ctx.settings + return bool(s.family) and bool(self.ctx.catalog.strata.multi_family(s.family)) + def reader(self) -> str: """"present" when the installed engine carries the picture reader (strata-vision), "missing" when an engine is installed without it, "unknown" before any engine is installed (the first launch fetches it).""" - return self.ctx.catalog.reader_state() + return "present" if self.own_tower() else self.ctx.catalog.reader_state() def on_mount(self) -> None: s = self.ctx.settings @@ -1036,6 +1041,9 @@ class PicturesScreen(Base): self.paint() def explain(self) -> Text: + if self.own_tower() and self.supported: + return Text("Pictures are on by default. DeepSeek reads them with a picture tower of its own inside the engine: a 1 GB file that comes with " + "the model's files and about 0.3 GB of graphics memory. Say No if you only chat in text.", style=MUTE) amd = any(str(g.get("vendor")) == "amd" for g in self.ctx.computer.usable_gpus) on_card = self.ctx.catalog.picture_default(True) == "gpu" extra = ("a 0.9 GB download (if it is not on this computer yet). On your AMD card the picture reader runs on the graphics card " @@ -1056,7 +1064,7 @@ class PicturesScreen(Base): def commit(self) -> None: key = self.menu().current.key - if key == "on": # yes: the reader on the card (NVIDIA, AMD) or the CPU, as the default says + if key == "on" and not self.own_tower(): # yes: the reader on the card (NVIDIA, AMD) or the CPU, as the default says device = self.ctx.catalog.picture_default(True) key = device if device != "off" else "on" self.ctx.settings.vision = key diff --git a/tests/terminal/test_launch_dsv41.py b/tests/terminal/test_launch_dsv41.py index fee3fb0e..b329ec9b 100644 --- a/tests/terminal/test_launch_dsv41.py +++ b/tests/terminal/test_launch_dsv41.py @@ -40,7 +40,7 @@ class LaunchDeepSeek(unittest.TestCase): self.assertEqual([r.download_gb for r in rows[:3]], [256.8, 273.8, 239.3]) self.assertEqual(catalog(self.hw).resolve("ist-das-2.25bpw"), ("deepseek41", "IST-DAS-2.25bpw")) r = rows[3] - self.assertEqual((r.id, r.download_gb, r.verdict, r.images), ("deepseek-v4.1-flash-ud-q8_k_xl", 508.4, "fits", False)) + self.assertEqual((r.id, r.download_gb, r.verdict, r.images), ("deepseek-v4.1-flash-ud-q8_k_xl", 508.4, "fits", True)) # 0.2.7: pictures through the engine's own tower self.assertEqual(catalog(self.hw).resolve("ud-q8_k_xl"), ("deepseek41", "UD-Q8_K_XL")) self.assertEqual(catalog(self.hw).max_context("deepseek41", "UD-Q8_K_XL"), 8192) @@ -50,7 +50,7 @@ class LaunchDeepSeek(unittest.TestCase): self.assertEqual(p["settings"]["family"], "deepseek41") self.assertEqual(p["settings"]["context"], 8192) self.assertEqual(p["settings"]["tune"], "skip") - self.assertEqual(p["settings"]["vision"], "off") + self.assertEqual(p["settings"]["vision"], "on") # pictures are on by default (the engine's own tower) self.assertEqual(pl["verdict"], "fits") self.assertEqual([c["layers"] for c in pl["cards"]], [[0, 9], [10, 19], [20, 29], [30, 39]]) self.assertEqual(pl["args"], ["--tp", "2"]) @@ -107,10 +107,12 @@ 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", "quick")) # the DeepSeek tune is real now: the choice stays + self.assertEqual((s["vision"], s["tune"]), ("on", "quick")) # the DeepSeek tune is real now: the choice stays; pictures stay on text = " ".join(p["warnings"]) self.assertIn("one request at a time", text) - self.assertIn("cannot read pictures", text) + self.assertNotIn("cannot read pictures", text) + off = self.plan("--model", "ud-q8_k_xl", "--vision", "off")["settings"] + self.assertEqual(off["vision"], "off") # --vision off still turns them off self.assertNotIn("Tuning is not offered", text) def test_setup_is_asked_for_the_family_and_the_files(self): diff --git a/tools/strata_mcp.py b/tools/strata_mcp.py index 48d0d2df..c6e28246 100644 --- a/tools/strata_mcp.py +++ b/tools/strata_mcp.py @@ -572,7 +572,7 @@ class Strata: # nine shards; the memory the weights need is VRAM + RAM together WITHOUT the n-gram table (it stays on the SSD) gb = sum(big["sha256"][n][0] for n in [*big["models"][model]["shards"], *big["models"][model].get("shared", [])]) / 1e9 return {"about": big["models"][model]["about"], "download_gb": round(gb, 1), "ram_gb": big["models"][model]["ram_gb"], - "arena_gb": round(float(big["models"][model]["ram_gb"]) - 17.0, 1), "mem_total": True, "vision": False, + "arena_gb": round(float(big["models"][model]["ram_gb"]) - 17.0, 1), "mem_total": True, "vision": bool(big.get("vision", True)), "engine": None, "families": (family,), "multi_file": True} d = (getattr(self.setup_module(), "OWN_PATH_FAMILIES", None) or {}).get(family) if not d or model not in d["models"]: @@ -670,7 +670,8 @@ class Strata: plan = self.removal_plan(path) return {"model": tag, "config": path.name, "model_name": cfg.get("model_name"), "context": int(val("--max-context")) if str(val("--max-context") or "").isdigit() else None, - "kv": val("--kv"), "images": ("gpu" if vis.get("gpu") else "cpu") if isinstance(vis, dict) else "off", + "kv": val("--kv"), "images": ("gpu" if vis.get("gpu") else "cpu") if isinstance(vis, dict) + else ("on" if cfg.get("arch") == "deepseek41" and str(val("--vision") or "on").lower() not in ("off", "no", "false", "0") else "off"), "port": cfg.get("port", DEFAULT_PORT), "host": cfg.get("host", "127.0.0.1"), "gpu": cfg.get("gpu"), "gpu_roles": cfg.get("gpu_roles"), "api_key_set": bool(cfg.get("api_key")), "low_ram": "--resident-experts" in a or "--mmap-experts" in a, diff --git a/tools/test_setup_dsv41.py b/tools/test_setup_dsv41.py index e2c49380..c6470140 100644 --- a/tools/test_setup_dsv41.py +++ b/tools/test_setup_dsv41.py @@ -194,6 +194,7 @@ class Install(unittest.TestCase): self.exe.write_text("x") cards = cards or FOUR kw.setdefault("model", "UD-Q8_K_XL") + kw.setdefault("vision", "no") # the older tests are about the files of the model; pictures-on has its own tests (PicturesByDefault) 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) @@ -220,7 +221,7 @@ class Install(unittest.TestCase): 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.assertEqual(a[a.index("--vision") + 1], "off") # (this test asks for no pictures: they are on by default, see PicturesByDefault) self.assertNotIn("--mtp", a) self.assertIn("4 cards, one block of layers on each", out) self.assertIn("n-gram table", out) @@ -291,7 +292,9 @@ class Install(unittest.TestCase): 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.assertNotIn("cannot read pictures", out) # 0.2.7: the engine's own tower reads them + a = self.cfg["args"] + self.assertEqual(a[a.index("--vision") + 1], "on") 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) @@ -308,7 +311,7 @@ class Catalog(unittest.TestCase): self.assertEqual((small_sets["IST-DAS-2.00bpw"]["download_gb"], small_sets["IST-DAS-2.25bpw"]["download_gb"]), (256.8, 273.8)) self.assertTrue(small_sets["IST-DAS-2.00bpw"]["on_this_pc"].startswith("fits")) size = small_sets["UD-Q8_K_XL"] - 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.assertEqual((size["model"], size["id"], size["download_gb"], size["images"]), ("UD-Q8_K_XL", "deepseek-v4.1-flash-ud-q8_k_xl", 508.4, True)) self.assertTrue(size["on_this_pc"].startswith("fits")) self.assertTrue(size["mem_total"] and size["multi_file"]) self.assertEqual(size["gpu_options"]["recommended"], "all") @@ -338,7 +341,7 @@ class Catalog(unittest.TestCase): "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((plan["title"], plan["download_gb"], plan["context"], plan["images"]), ("DeepSeek-V4.1-Flash UD-Q8_K_XL", 508.4, 8192, "yes")) # 0.2.7: pictures on by default 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"): @@ -572,6 +575,10 @@ class Smallest(unittest.TestCase): self.assertEqual(setup.DSV41_ISTA_FILES[d["shards"][3]][0], 2188841824) self.assertEqual(sum(setup.DSV41_ISTA_FILES[n][0] for n in d["shards"]), 120_721_120_256) self.assertTrue(all(n not in setup.DSV41_LEGACY_NAMES.values() for n in d["shards"])) # no first-release names for it + for n, (size, sha) in setup.DSV41_NEW_SHARD1.items(): # shard 1 after ISTA's 2026-10-26 template update + self.assertIn(n, setup.DSV41_ISTA_FILES) + self.assertNotEqual(setup.DSV41_ISTA_FILES[n][0], size) + self.assertNotEqual(setup.DSV41_ISTA_FILES[n][1], sha) cmd = setup.dsv41_pack_cmd(Path("/m"), d["shards"], Path("/s/e.gguf"), Path("/s/m.gguf"), Path("/s/d.gguf"), Path("/p")) self.assertIn("--dspark-noise-token", cmd) self.assertNotIn("--skip-tensors", cmd) @@ -674,7 +681,7 @@ class InstallOfficial(Install): self.assertEqual(rc, 0, out) a = self.cfg["args"] self.assertEqual(a[a.index("--dspark") + 1], "auto") # never "off": that still loads the drafter and takes the VRAM - self.assertNotIn("off", a) + self.assertNotEqual(a[a.index("--dspark") + 1], "off") self.assertNotIn("--prefill-chunk", a) # the engine's own default is the layer-major prompt path self.assertEqual(a[a.index("--dspark-conf") + 1], "0.65") # never a floor below 0.3 # the layer-major prompt path self.assertNotIn("--ssd-cache", a) # the store sizes itself @@ -729,3 +736,52 @@ class InstallSmallest(InstallOfficial): if __name__ == "__main__": unittest.main() + + +class PicturesByDefault(Install): + """0.2.7: DeepSeek reads pictures with its own engine's tower, on by default: setup fetches the tower's weights (mmproj-BF16.gguf, a shared file), + packs them with the model and writes --vision on; --vision no leaves all three out.""" + + def test_default_fetches_the_tower_packs_it_and_says_on(self): + rc, out = self.install(vision=None) + self.assertEqual(rc, 0, out) + got = [c[2].name for c in self.calls if c[0] == "get"] + self.assertIn(setup.DSV41_MMPROJ, got) + self.assertEqual(got[-1], setup.DSV41_MMPROJ) + pack = [c for c in self.calls if c[0] == "run"][-1][1] + self.assertIn("--mmproj", pack) + self.assertTrue(str(pack[pack.index("--mmproj") + 1]).endswith(setup.DSV41_MMPROJ)) + a = self.cfg["args"] + self.assertEqual(a[a.index("--vision") + 1], "on") + self.assertNotIn("cannot read pictures", out) + + def test_vision_no_leaves_the_tower_out(self): + rc, out = self.install(vision="no") + self.assertEqual(rc, 0, out) + self.assertNotIn(setup.DSV41_MMPROJ, [c[2].name for c in self.calls if c[0] == "get"]) + pack = [c for c in self.calls if c[0] == "run"][-1][1] + self.assertNotIn("--mmproj", pack) + a = self.cfg["args"] + self.assertEqual(a[a.index("--vision") + 1], "off") + + def test_gpu_and_cpu_are_taken_as_yes_with_a_note(self): + rc, out = self.install(vision="cpu") + self.assertEqual(rc, 0, out) + self.assertIn("own tower", out) + a = self.cfg["args"] + self.assertEqual(a[a.index("--vision") + 1], "on") + + def test_a_pack_without_the_tower_is_made_again_with_it(self): + pack = self.root / "data" / "packs" / "deepseek-v4.1-flash-ud-q8_k_xl" + (pack / "tokenizer").mkdir(parents=True) + (pack / "tokenizer" / "vocab.json").write_text("{}") + (pack / "tensors.txt").write_text("x") + (pack / "ds4.txt").write_text("hidden_size 5120\n") + self.assertFalse(setup.dsv41_pack_has_tower(pack)) + rc, out = self.install(vision=None) + self.assertEqual(rc, 0, out) + self.assertIn("made without the picture reader", out) + self.assertTrue([c for c in self.calls if c[0] == "run" and "--mmproj" in c[1]]) + pack.mkdir(parents=True, exist_ok=True) + (pack / "ds4.txt").write_text("vision.num_hidden_layers 32\nimage_token_id 129264\n") + self.assertTrue(setup.dsv41_pack_has_tower(pack))