Files
strata/strata_control/drafting_fetch.py
T
Niko1221 437a60676f Drafting: strata launch --drafting auto|mtp|dflash2|off, one suggestion table, and the DFlash2 drafter fetched pinned and packed on the person's PC
- strata_control/drafting.py is the one place: the choice (saved in the model's run config as "drafting"), the table card class -> suggested
  drafter with the measured evidence (Auto picks DFlash2 only where it was faster on every tested prompt; today no class qualifies, so Auto is
  the MTP everywhere), the pinned drafter (incoai/Qwen3.8-Flash-Next-DFlash2 rev a22a1744, sizes and SHA-256 of the repository), what each choice
  puts on the engine arguments, and the plain "Drafting: MTP (DFlash2 didn't fit)" words read from the engine's own log.
- serve/server.py applies the choice at every start and reports "drafting" / "drafting_note" in /metrics.
- strata launch --drafting writes it to the launch config and presets, shows it in --plan, adds the drafter to the download plan when it would
  be used, fetches it before the start (a failure starts the MTP and says so), and prints the engine's fallback line.
- strata drafting plan|fetch (JSON/JSONL for the app). tools/dflash2_pack.py now reads safetensors with numpy alone (no torch on the person's
  PC), byte for byte the same file as before (checked against the earlier version), and ships in the Terminal bundle.
- DFlash2 is for Qwen3.8-Flash-Next (qwen, gyro) only. The engine in this release runs it only in its CLI run, not with --serve, so while
  drafting.ENGINE_SERVES_DFLASH2 is False the choice starts the MTP, says "DFlash2 isn't available in this mode yet" and downloads nothing.
2026-10-22 17:27:39 +02:00

103 lines
5.3 KiB
Python

"""Getting the DFlash2 drafter onto this computer: a pinned download, checked against the repository's own SHA-256, packed for the engine.
strata drafting plan [--data-dir DIR] what it would download (nothing is changed)
strata drafting fetch [--data-dir DIR] download, check, pack; progress in plain words
Only an explicit command reaches the network (strata_control/acquisition.py): a start never downloads. The drafter is
incoai/Qwen3.8-Flash-Next-DFlash2 at one revision (strata_control/drafting.py has the revision, the file sizes and hashes). The 1.2 GB
safetensors file is packed (tools/dflash2_pack.py, numpy only) into the engine's 0.76 GB runtime file and then removed.
"""
from __future__ import annotations
import importlib.util
import os
import sys
import threading
from pathlib import Path
from typing import Callable
from strata_control import acquisition, drafting, transfer
Progress = Callable[[str, int, int], None] # (step in plain words, bytes done, bytes total)
class DraftingError(RuntimeError):
"""The drafter could not be fetched or packed. The message is plain words and says what to do."""
def plan(data_dir: str | Path) -> dict:
"""What fetching would do, in numbers and words. ``gb`` is 0 when the drafter is already here."""
ready = drafting.pack_ready(data_dir)
gb = 0.0 if ready else drafting.DOWNLOAD_GB
if ready:
text = "The DFlash2 drafter is already on this computer."
else:
text = (f"The DFlash2 drafter is {drafting.DOWNLOAD_GB:.1f} GB to download (it is packed to {drafting.PACK_GB:.2f} GB for the engine and the "
f"download is removed), from {drafting.REPO}, {drafting.LICENSE}. It needs about {drafting.VRAM_GB:.1f} GB of graphics memory; "
"if it does not fit, the model starts with the MTP instead.")
return {"ready": ready, "gb": gb, "pack": str(drafting.pack_path(data_dir)), "repo": drafting.REPO, "revision": drafting.REVISION, "text": text}
def _packer():
"""tools/dflash2_pack.py as a module: beside strata_control in a source checkout and in the packaged Control (TOOLS_SUBSET)."""
path = Path(__file__).resolve().parents[1] / "tools" / "dflash2_pack.py"
if not path.is_file():
raise DraftingError("The packing tool is missing from this installation. Update Strata, then try again.")
spec = importlib.util.spec_from_file_location("strata_dflash2_pack", path)
module = importlib.util.module_from_spec(spec)
sys.modules.setdefault("strata_dflash2_pack", module)
spec.loader.exec_module(module)
return module
def ensure(data_dir: str | Path, *, progress: Progress | None = None, cancel: threading.Event | None = None,
base_url: str | None = None, files=None) -> Path:
"""Make the packed drafter exist under ``data_dir`` and return its path. A pack that is there is kept. Resumable: an interrupted
download is continued, a file whose hash differs is deleted and the person is told. ``base_url`` / ``files`` are for tests."""
report = progress or (lambda *_: None)
out = drafting.pack_path(data_dir)
if drafting.pack_ready(data_dir):
report("The DFlash2 drafter is ready.", 1, 1)
return out
acquisition.require("the DFlash2 drafter", "strata drafting fetch")
folder = out.parent
src = folder / "src"
src.mkdir(parents=True, exist_ok=True)
spec = tuple(files if files is not None else drafting.FILES)
total = sum(size for _n, size, _h in spec)
done_before = 0
for name, size, sha in spec:
target = src / name
if not (target.is_file() and target.stat().st_size == size and transfer.sha256_file(target) == sha):
base = done_before
partial = src / (name + ".part")
try:
transfer.download((base_url or drafting.BASE_URL) + name, partial, size=size, sha256=sha, cancel=cancel,
progress=lambda d, _s, base=base: report("Downloading the DFlash2 drafter…", base + d, total))
except transfer.ChecksumMismatch:
raise DraftingError("The downloaded DFlash2 file did not pass the safety check, so it was deleted. Try again; if it keeps "
"happening, use MTP for now.") from None
except transfer.TransferError as exc:
raise DraftingError(f"The DFlash2 download did not finish: {exc}. Try again; it continues where it stopped.") from None
os.replace(partial, target)
done_before += size
report("Packing the DFlash2 drafter for the engine…", total, total)
tmp = out.with_suffix(".tmp")
try:
_packer().pack(str(src / "model.safetensors"), str(src / "config.json"), str(tmp), say=lambda *_: None)
os.replace(tmp, out)
except DraftingError:
raise
except Exception as exc: # noqa: BLE001 - a plain sentence for any packing failure; the cause stays in the chain
tmp.unlink(missing_ok=True)
raise DraftingError("The DFlash2 drafter could not be prepared on this computer. The model keeps using the MTP.") from exc
for name, _size, _sha in spec: # the source files are 1.2 GB of nothing the engine reads
(src / name).unlink(missing_ok=True)
try:
src.rmdir()
except OSError:
pass
report("The DFlash2 drafter is ready.", total, total)
return out