Qwen3.8-Flash-Next on any consumer hardware: one-click install for Windows / Linux. Strata inference engine, OpenAI/Anthropic API on localhost, optional image input.
C++
Downloads: the MTP fetch shows progress in the app and survives a flaky link (runs again, fewer connections, one writer per folder); a cancel and a killed setup end the whole tree on Windows (snapshot + the fetch ends with its parent); Gyro's low-RAM pack gets --compat-bf16 (#3257 #3214 #3193)
Niko122196a9107a278e
diff --git a/apps/host/src/lib/download-eta.ts b/apps/host/src/lib/download-eta.tsindex 5d02f9055..35a239093 100644--- a/apps/host/src/lib/download-eta.ts+++ b/apps/host/src/lib/download-eta.ts@@ -55,12 +55,12 @@ export function installProgress(install: InstallParts | undefined): { done_gb: n}/** Which part of a multi-part install is being fetched now: the model's parts (with "part 2 of 4"), the picture reader or the engine. */-export interface InstallStage { kind: 'model' | 'vision' | 'engine' | 'other'; index?: number; count?: number }+export interface InstallStage { kind: 'model' | 'vision' | 'engine' | 'draft' | 'other'; index?: number; count?: number }export function installStage(install: { parts?: { kind?: string; state?: string; files_done?: number; files_total?: number }[]; running?: boolean } | undefined): InstallStage | undefined {const parts = install?.parts ?? [];const now = parts.find(p => p.state === 'downloading') ?? parts.find(p => !['done', 'current', 'skipped', 'failed'].includes(p.state ?? ''));if (!now) return undefined;- const kind = now.kind === 'model' || now.kind === 'vision' || now.kind === 'engine' ? now.kind : 'other';+ const kind = now.kind === 'model' || now.kind === 'vision' || now.kind === 'engine' || now.kind === 'draft' ? now.kind : 'other';const count = Number(now.files_total) > 1 ? Number(now.files_total) : undefined;return { kind, ...(count ? { index: Math.min(count, Number(now.files_done ?? 0) + 1), count } : {}) };}@@ -68,6 +68,7 @@ export function installStage(install: { parts?: { kind?: string; state?: string;export function stageText(stage: InstallStage | undefined): string {if (!stage) return tr('Downloading model files…');if (stage.kind === 'vision') return tr('Downloading the picture reader…');+ if (stage.kind === 'draft') return tr('Preparing your model…'); // the speed-up layer's own download (setup step 6)if (stage.kind === 'engine') return tr('Downloading the engine for this PC…');if (stage.count && stage.index) return tr('Downloading model files · part {0} of {1}', [stage.index, stage.count]);return tr('Downloading model files…');diff --git a/apps/host/tests/qa1-downloads.test.mjs b/apps/host/tests/qa1-downloads.test.mjsindex 4c0b7f9fc..3aee6cb64 100644--- a/apps/host/tests/qa1-downloads.test.mjs+++ b/apps/host/tests/qa1-downloads.test.mjs@@ -163,3 +163,13 @@ test('no recognised engine: the app still downloads, warns in the card, and load// the install is not refused for a missing engine (only the engine-minimum refusal for a version that is too old stays)assert.doesNotMatch(source.slice(source.indexOf('async function install(job,args)'), source.indexOf('function terminate')), /engine\.usable/);});++test('the draft layer download of setup step 6 is a stage of its own and counts in the one figure (#3257)', () => {+ lib.setTranslationLanguage('en');+ const draft = { parts: [{ kind: 'model', name: 'Model files', state: 'done', done_gb: 60, total_gb: 60 }, { kind: 'draft', name: 'Speed-up layer', state: 'downloading', done_gb: 2, total_gb: 5 }] };+ assert.deepEqual(lib.installStage(draft), { kind: 'draft' });+ assert.equal(lib.stageText(lib.installStage(draft)), 'Preparing your model…');+ const p = lib.installProgress(draft);+ assert.equal(p.total_gb, 65);+ assert.equal(p.done_gb, 62);+});diff --git a/setup.py b/setup.pyindex aa39757bf..68d333172 100644--- a/setup.py+++ b/setup.py@@ -751,6 +751,8 @@ def run(cmd, cwd=None, env=None, check=True, quiet=False):raise acquisition.AcquisitionRefused("the desktop app's installer only runs its bundled preparation tools; "+ " ".join(argv[:2]))say(" > " + " ".join(str(c) for c in cmd))+ env = dict(os.environ if env is None else env)+ env.setdefault("STRATA_EXIT_WITH_PARENT", "1") # tools/mtp_fetch.py ends when setup ends (#3257: a cancel left it writing)r = subprocess.run([str(c) for c in cmd], cwd=cwd, env=env,stdout=subprocess.PIPE if quiet else None, stderr=subprocess.STDOUT if quiet else None,text=True)@@ -761,6 +763,25 @@ def run(cmd, cwd=None, env=None, check=True, quiet=False):return r+def fetch_mtp(mtp: Path, env: dict, tries: int = 3):+ """The ~5 GB of MTP tensors (tools/mtp_fetch.py). A timeout or a dropped connection stops one run, not the install (#3214 #3193):+ it is run again, and each run continues where the last one stopped. The progress lines it prints are what the app shows."""+ cmd = [sys.executable, str(ROOT / "tools" / "mtp_fetch.py"), "fetch", "--out", str(mtp)]+ env = {**env, "STRATA_SOURCE": model_source()}+ for n in range(1, tries + 1):+ r = run(cmd, env=env, check=False)+ rc = 0 if r is None else r.returncode # (a test double answers None)+ if rc == 0:+ return+ if n < tries:+ warn(f"the draft layer's download stopped (exit {rc}); trying again, it continues where it stopped "+ f"({n + 1} of {tries})")+ time.sleep(5)+ fail(f"the draft layer's download keeps stopping (exit {rc}): the connection to Hugging Face is slow or drops",+ "run setup again - what is downloaded is kept. If it still stops, set STRATA_SOURCE=modelscope (setup --source modelscope) "+ "to read the same files from ModelScope")++def out(cmd):try:return subprocess.run(cmd, capture_output=True, text=True, timeout=60).stdout@@ -7160,8 +7181,7 @@ def mac_control_setup(a) -> int:ok(f"the experts stream from the SSD; the MTP draft layer stays on, with --spec {MF.STREAM_SPEC}")if use_mtp and not (mtp / "mtp-q2_0.gguf").exists():say(" The MTP draft layer (speculative decoding) comes from the original Qwen checkpoint: only its ~5 GB of MTP tensors are downloaded.")- run([sys.executable, str(ROOT / "tools" / "mtp_fetch.py"), "fetch", "--out", str(mtp)],- env={**env, "STRATA_SOURCE": model_source()})+ fetch_mtp(mtp, env)run([sys.executable, str(ROOT / "tools" / "mtp_pack.py"), "--src", str(mtp), "--experts", "q2_0","--out", str(mtp / "mtp-q2_0.gguf")], env=env)if use_mtp:@@ -8101,7 +8121,7 @@ def main() -> int:if low_ram and not ready and not (pack / "experts.bin").exists():say(f" Writing the experts into one file for the low-RAM mode (one time, {MODELS[model]['arena_gb']:.0f} GB) ...")run([sys.executable, str(ROOT / "tools" / "iq_pack.py"), "--gguf", str(shards[0]), "--out", str(pack),- "--experts-bin"], env=env)+ "--experts-bin", *fam.get("pack_args", [])], env=env) # #3193: Gyro needs --compat-bf16 here toook(f"model prepared: {pack}")mtp = ready_paths(model, models_dir)["mtp"] if prepared else \(find_in(roots, "mtp/rt/experts.bin") or data / "mtp/rt/experts.bin").parent.parent@@ -8113,8 +8133,7 @@ def main() -> int:if corrupt or not (rt / "experts.bin").exists():say(" The MTP draft layer (speculative decoding, ~2x faster output) comes from the original Qwen checkpoint:")say(" only its ~5 GB of MTP tensors are downloaded.")- run([sys.executable, str(ROOT / "tools" / "mtp_fetch.py"), "fetch", "--out", str(mtp)],- env={**env, "STRATA_SOURCE": model_source()})+ fetch_mtp(mtp, env)run([sys.executable, str(ROOT / "tools" / "mtp_pack.py"), "--src", str(mtp), "--experts", "q2_0","--out", str(mtp / "mtp-q2_0.gguf")], env=env)run([sys.executable, str(ROOT / "tools" / "mtp_rt.py"), "--gguf", str(mtp / "mtp-q2_0.gguf"), "--out", str(rt)],diff --git a/tools/mtp_fetch.py b/tools/mtp_fetch.pyindex b4a44dfd5..14898aef0 100644--- a/tools/mtp_fetch.py+++ b/tools/mtp_fetch.py@@ -130,7 +130,7 @@ def _final(url):return hit[0] if hit and hit[1] > time.monotonic() else url-def get(url, start=None, end=None, retries=4):+def get(url, start=None, end=None, retries=6):for attempt in range(retries):try:used = _final(url)@@ -153,10 +153,90 @@ def get(url, start=None, end=None, retries=4):_cdn.pop(url, None)if attempt == retries - 1:raise- time.sleep(2 ** attempt)+ time.sleep(min(2 ** attempt, 16))print("retry %s: %s" % (url, e), file=sys.stderr)+class Progress:+ """One line a couple of seconds, `MTP draft layer: 1.23 / 5.04 GB (24%)`: the form of setup's own download line, so the app's+ progress parser (tools/strata_mcp.py install_progress) shows it. `done` counts what an earlier run already has."""+ LABEL = "MTP draft layer"++ def __init__(self, total, done=0, every=2.0, out=None):+ self.total, self.done, self.every, self.out = total, done, every, out or sys.stderr+ self.last = 0.0+ self.lock = threading.Lock()++ def add(self, n):+ with self.lock:+ self.done += n+ self.show()++ def show(self, force=False):+ now = time.monotonic()+ if not force and now - self.last < self.every:+ return+ self.last = now+ if self.total > 0:+ done = max(0, min(self.done, self.total))+ print(" %s: %6.2f / %.2f GB (%d%%)" % (self.LABEL, done / 1e9, self.total / 1e9, 100 * done // self.total),+ file=self.out, flush=True)+++def lock_folder(out, wait=900.0):+ """Only one fetch writes a folder: a second one (a retry while the first still runs, #3257) waits for the first and then finds+ its tensors done. Returns the open lock file (keep it open), or exits when the first does not end in `wait` seconds."""+ os.makedirs(out, exist_ok=True)+ f = open(os.path.join(out, ".mtp_fetch.lock"), "a+b")+ end = time.monotonic() + wait+ told = False+ while True:+ try:+ if os.name == "nt":+ import msvcrt+ f.seek(0)+ msvcrt.locking(f.fileno(), msvcrt.LK_NBLCK, 1)+ else:+ import fcntl+ fcntl.flock(f.fileno(), fcntl.LOCK_EX | fcntl.LOCK_NB)+ return f+ except OSError:+ if time.monotonic() > end:+ sys.exit("another MTP download is still writing %s: wait for it or end it (Task Manager: python.exe), then try again" % out)+ if not told:+ print("another MTP download is writing %s: waiting for it to end" % out, file=sys.stderr, flush=True)+ told = True+ time.sleep(2)+++def exit_with_parent():+ """STRATA_EXIT_WITH_PARENT=1 (set by setup.py for the tools it runs): end when the process that started this one ends, so a cancelled+ or killed setup does not leave a download running on its own (#3257: taskkill /T does not reach a grandchild whose parent is gone)."""+ if (os.environ.get("STRATA_EXIT_WITH_PARENT") or "").strip() != "1":+ return+ parent = os.getppid()+ if parent <= 1:+ return+ if os.name == "nt":+ import ctypes+ k32 = ctypes.windll.kernel32+ k32.OpenProcess.restype = ctypes.c_void_p+ k32.WaitForSingleObject.argtypes = (ctypes.c_void_p, ctypes.c_ulong)+ h = k32.OpenProcess(0x00100000, False, parent) # SYNCHRONIZE: a handle keeps the answer true if the id is reused+ if not h:+ return # no access (or already gone): nothing to watch+ gone = lambda: k32.WaitForSingleObject(h, 0) == 0+ else:+ gone = lambda: os.getppid() != parent++ def watch():+ while not gone():+ time.sleep(1.5)+ print("the process that started this download has ended: stopping", file=sys.stderr, flush=True)+ os._exit(1)+ threading.Thread(target=watch, daemon=True).start()++def connections():"""Ranges read at once: STRATA_DL_CONNECTIONS (the setting of setup's downloads), else 16; 1 reads them one by one."""try:@@ -297,36 +377,59 @@ def fetch(out, only):manifest = []workers = connections()chunk = (16 if workers > 1 else 64) << 20- for r in rows:- if only and only not in r["name"]:- continue+ todo = [r for r in rows if not only or only in r["name"]]+ have_all = 0+ for r in todo:+ tp = os.path.join(tdir, r["name"] + ".bin")+ if os.path.exists(tp):+ have_all += min(os.path.getsize(tp), r["bytes"])+ progress = Progress(sum(r["bytes"] for r in todo), have_all)+ progress.show(force=True)+ for r in todo:path = os.path.join(tdir, r["name"] + ".bin")want = SHA256.get(r["name"]) if pinned() else Noneif os.path.exists(path) and not same and (want is None or os.path.getsize(path) != r["bytes"] or sha256_of(path) != want):os.remove(path)- for attempt in range(2):+ attempt, net_fails = 0, 0+ while attempt < 2:have = os.path.getsize(path) if os.path.exists(path) else 0if have > r["bytes"]:os.remove(path)have = 0- with open(path, "ab") as f:- pos = r["start"] + have- for data in ranged(REPO + r["shard"], pos, r["end"], chunk, workers):- f.write(data)- pos += len(data)- print("%s %.0f%%" % (r["name"], 100 * (pos - r["start"]) / r["bytes"]), file=sys.stderr)+ try:+ with open(path, "ab") as f:+ pos = r["start"] + have+ for data in ranged(REPO + r["shard"], pos, r["end"], chunk, workers):+ f.write(data)+ pos += len(data)+ progress.add(len(data))+ except (OSError, urllib.error.URLError) as e:+ # #3214: a timeout or a dropped connection mid-tensor ends only this try: what was written stays, the next try+ # continues after it with fewer connections (a link that drops 16 at once often holds 4)+ net_fails += 1+ if net_fails > 3:+ sys.exit("%s: the download keeps failing (%s). Check the connection and try again - what is downloaded is kept; "+ "if Hugging Face is slow to reach, `--source modelscope` reads the same files elsewhere" % (r["name"], e))+ workers = max(4, workers // 2)+ print("%s: %s - continuing with %d connections (try %d)" % (r["name"], e, workers, net_fails + 1), file=sys.stderr, flush=True)+ time.sleep(min(5 * net_fails, 20))+ continueif os.path.getsize(path) != r["bytes"]:sys.exit("%s: size %d != %d" % (path, os.path.getsize(path), r["bytes"]))digest = sha256_of(path)if want is None or digest == want:break+ progress.done -= os.path.getsize(path)os.remove(path) # wrong bytes (#327): the whole tensor again, once- if attempt:+ attempt += 1+ if attempt > 1:sys.exit("%s: sha256 %s is not the checkpoint's %s - the download source sends wrong data (a mirror ""that ignores Range requests?)" % (r["name"], digest, want))print("%s: wrong bytes (sha256 %s...), fetching it again" % (r["name"], digest[:12]), file=sys.stderr)manifest.append(dict(r, file=os.path.relpath(path, out), sha256=digest))+ print("%s done" % r["name"], file=sys.stderr, flush=True)+ progress.show(force=True)with open(os.path.join(out, "mtp-manifest.json"), "w", encoding="utf-8") as f:json.dump(manifest, f, indent=1)@@ -375,8 +478,12 @@ def main():for name in bad:print("MTP tensor missing or corrupt: %s" % name, file=sys.stderr)sys.exit(BAD if bad else 0)+ exit_with_parent()+ lock = lock_folder(a.out) if a.cmd == "fetch" else Noneresolve_repo()inventory(a.out) if a.cmd == "inventory" else fetch(a.out, a.only)+ if lock:+ lock.close()if __name__ == "__main__":diff --git a/tools/strata_mcp.py b/tools/strata_mcp.pyindex f9ddcc04f..09e9318dd 100644--- a/tools/strata_mcp.py+++ b/tools/strata_mcp.py@@ -136,6 +136,16 @@ if WIN:_k32.WaitForSingleObject.argtypes = (wintypes.HANDLE, wintypes.DWORD)_k32.CloseHandle.argtypes = (wintypes.HANDLE,)_QUERY, _TERMINATE, _SYNCHRONIZE, _STILL_ACTIVE = 0x1000, 0x0001, 0x00100000, 259++ class _PROCESSENTRY32W(ctypes.Structure):+ _fields_ = [("dwSize", wintypes.DWORD), ("cntUsage", wintypes.DWORD), ("th32ProcessID", wintypes.DWORD),+ ("th32DefaultHeapID", ctypes.c_size_t), ("th32ModuleID", wintypes.DWORD),+ ("cntThreads", wintypes.DWORD), ("th32ParentProcessID", wintypes.DWORD),+ ("pcPriClassBase", ctypes.c_long), ("dwFlags", wintypes.DWORD), ("szExeFile", ctypes.c_wchar * 260)]+ _k32.CreateToolhelp32Snapshot.restype = wintypes.HANDLE+ _k32.CreateToolhelp32Snapshot.argtypes = (wintypes.DWORD, wintypes.DWORD)+ _k32.Process32FirstW.argtypes = (wintypes.HANDLE, ctypes.POINTER(_PROCESSENTRY32W))+ _k32.Process32NextW.argtypes = (wintypes.HANDLE, ctypes.POINTER(_PROCESSENTRY32W))CREATE_NEW_PROCESS_GROUP, CREATE_NO_WINDOW, CREATE_BREAKAWAY_FROM_JOB = 0x200, 0x08000000, 0x01000000@@ -220,14 +230,60 @@ def _rmtree_ignore_missing(func, path, exc_info):raise exc+def proc_children_map() -> dict:+ """Windows: {parent pid: [child pids]} of every running process (a Toolhelp snapshot); {} elsewhere or when it fails."""+ if not WIN:+ return {}+ snap = _k32.CreateToolhelp32Snapshot(0x2, 0) # TH32CS_SNAPPROCESS+ if not snap or snap == wintypes.HANDLE(-1).value:+ return {}+ kids: dict = {}+ try:+ entry = _PROCESSENTRY32W()+ entry.dwSize = ctypes.sizeof(_PROCESSENTRY32W)+ ok = _k32.Process32FirstW(snap, ctypes.byref(entry))+ while ok:+ kids.setdefault(entry.th32ParentProcessID, []).append(entry.th32ProcessID)+ ok = _k32.Process32NextW(snap, ctypes.byref(entry))+ finally:+ _k32.CloseHandle(snap)+ return kids+++def proc_tree_snapshot(pid) -> list:+ """Every process below `pid` as [(pid, identity)], taken NOW: once a middle process of the tree ends, taskkill /T can no longer reach+ what it started (#3257: a cancelled install left tools/mtp_fetch.py writing). Windows only; [] elsewhere (a process group does that)."""+ kids = proc_children_map()+ out, seen, todo = [], {int(pid)}, [int(pid)]+ while todo:+ for child in kids.get(todo.pop(), []):+ if child in seen:+ continue+ seen.add(child)+ ident = proc_identity(child)+ if ident:+ out.append((child, ident))+ todo.append(child)+ return out+++def kill_snapshot(procs) -> None:+ """End what a proc_tree_snapshot() listed and still runs (the same process: its start time still matches)."""+ for pid, ident in reversed(list(procs)):+ if proc_alive(pid, ident):+ proc_terminate(pid, ident)++def proc_kill_tree(pid, ident) -> bool:"""End a process WE started together with the processes it started (setup's pip / cmake, the engine)."""if not proc_alive(pid, ident):return Falseif WIN:+ tree = proc_tree_snapshot(pid) # before taskkill: it walks parent ids, and a break in the chain hides the restr = subprocess.run(["taskkill", "/PID", str(int(pid)), "/T", "/F"], capture_output=True,creationflags=CREATE_NO_WINDOW)- return r.returncode == 0+ kill_snapshot(tree)+ return r.returncode == 0 or not proc_alive(pid, ident)try:os.killpg(int(pid), signal.SIGKILL) # started as a session leader: its group is its ownreturn True@@ -1350,12 +1406,34 @@ def files_progress(prog: dict, job: dict, files: list) -> dict:return prog+def draft_progress(prog: dict, job: dict, dl: dict) -> dict:+ """Setup step 6 downloads the ~5 GB of MTP tensors (tools/mtp_fetch.py prints `MTP draft layer: 1.2 / 5.0 GB (24%)`). The figure+ the app shows is the whole job's: the model files (done by now) plus this part, so the bar does not fall back to 0 (#3257)."""+ done_gb, total_gb = float(dl.get("done_gb") or 0), float(dl.get("total_gb") or 0)+ parts = []+ if job.get("files"):+ base = files_progress({}, job, job["files"])+ parts = [dict(p, state="done", done_gb=p["total_gb"]) for p in base.get("parts", []) if p.get("state") == "done"]+ parts.append({"kind": "draft", "name": "Speed-up layer", "state": "done" if int(dl.get("percent") or 0) >= 100 else "downloading",+ "done_gb": round(done_gb, 3), "total_gb": round(total_gb, 3)})+ total = sum(p["total_gb"] for p in parts)+ done = min(sum(p["done_gb"] for p in parts), total)+ prog["parts"] = parts+ prog["download"] = {"file": "Speed-up layer", "done_gb": round(done, 3), "total_gb": round(total, 3),+ "remaining_gb": round(max(0.0, total - done), 3),+ "percent": min(99, int(100 * done / total)) if total else 0}+ return prog++def combined_progress(prog: dict, job: dict) -> dict:"""One total for the whole model: the bytes of all its files (finished, partial and the one in flight, read from themodel's folder) over the download size, instead of each file's own 0-100% (a two-file model went 70 GB -> 0 -> upagain). Only while the download step runs; later steps are not a download."""folder, total = job.get("folder"), job.get("total_gb")step = str(prog.get("step") or "")+ dl = prog.get("download") or {}+ if step.startswith("6/") and str(dl.get("file", "")).startswith("MTP draft layer"):+ return draft_progress(prog, job, dl)if job.get("files") and step.startswith("5/"): # the install knows its files: count each of themreturn files_progress(prog, job, job["files"])if not folder or not total or not step.startswith("5/"):@@ -1769,9 +1847,11 @@ class Tools:j = s.state("install")res = s.state("install-result")child = res.get("child_pid") if res.get("job") == j.get("job") else None+ tree = proc_tree_snapshot(j["pid"]) if proc_alive(j["pid"], j["ident"]) else [] # setup.py and what it runs (#3257)if child:proc_kill_tree(child, res.get("child_ident"))proc_kill_tree(j["pid"], j["ident"])+ kill_snapshot(tree)wait_gone(j["pid"], j["ident"], 15)return {"summary": "the install was stopped; calling strata_install again resumes it (downloads ""continue where they stopped)", "install": s.install_job()}diff --git a/tools/test_mtp_fetch.py b/tools/test_mtp_fetch.pyindex bf2476506..9133f8108 100644--- a/tools/test_mtp_fetch.py+++ b/tools/test_mtp_fetch.py@@ -278,5 +278,107 @@ class Setup(unittest.TestCase):self.assertIn("verify", run.call_args[0][0])+++class Progress(FetchCase):+ """#3257: the fetch says how far it is in the form of setup's own download line, so the app can show it."""++ def test_progress_lines_name_the_whole_download_and_end_at_100(self):+ err = self.fetch(Mirror())+ lines = [x for x in err.splitlines() if "MTP draft layer:" in x]+ total = len(A) + len(B)+ self.assertTrue(lines)+ self.assertIn("/ %.2f GB" % (total / 1e9), lines[-1])+ self.assertIn("(100%)", lines[-1])+ import re+ for x in lines: # the shape tools/strata_mcp.py install_progress reads+ self.assertTrue(re.match(r"\s*(\S.*?): +([\d.]+) / ([\d.]+) GB \((\d+)%\)", x), x)++ def test_a_resumed_fetch_starts_from_what_is_already_there(self):+ self.fetch(Mirror())+ err = self.fetch(Mirror())+ self.assertIn("(100%)", [x for x in err.splitlines() if "MTP draft layer:" in x][0])++ def test_the_progress_figure_follows_the_job_in_strata_mcp(self):+ import strata_mcp as MCP+ got = MCP.install_progress(["=== Step 6: preparing the model for Strata ===",+ " MTP draft layer: 1.23 / 5.04 GB (24%)"])+ self.assertEqual(got["download"], {"file": "MTP draft layer", "done_gb": 1.23, "total_gb": 5.04, "percent": 24})+++class Flaky(FetchCase):+ def test_a_connection_that_drops_mid_tensor_does_not_end_the_run(self):+ """#3214: every range of the tensor fails past get()'s own retries once: the tensor is tried again (fewer connections)."""+ honest, fails = Mirror(), [0]++ def mirror(req, timeout=None):+ if req.get_header("Range") and req.full_url.endswith("model-2.safetensors") and fails[0] < 12:+ a, b = map(int, req.get_header("Range").split("=")[1].split("-"))+ if (a, b) == Mirror.span("model-2.safetensors", "mtp.b"):+ fails[0] += 1+ raise IOError("timed out")+ return honest(req, timeout)++ err = self.fetch(mirror)+ self.assertEqual(self.tensor("mtp.b"), B)+ self.assertIn("continuing with", err)++ def test_a_link_that_never_works_ends_with_a_plain_sentence(self):+ def mirror(req, timeout=None):+ if req.get_header("Range") and req.full_url.endswith("model-2.safetensors"):+ a, b = map(int, req.get_header("Range").split("=")[1].split("-"))+ if (a, b) == Mirror.span("model-2.safetensors", "mtp.b"):+ raise IOError("timed out")+ return Mirror()(req, timeout)++ with self.assertRaisesRegex(SystemExit, "keeps failing"):+ self.fetch(mirror)+++class OneAtATime(unittest.TestCase):+ def test_a_second_fetch_in_the_same_folder_waits_for_the_first(self):+ with tempfile.TemporaryDirectory() as d:+ first = M.lock_folder(d)+ told = io.StringIO()+ with mock.patch.object(M.time, "sleep", lambda s: None), contextlib.redirect_stderr(told), \+ self.assertRaisesRegex(SystemExit, "another MTP download"):+ M.lock_folder(d, wait=0.0)+ first.close()+ M.lock_folder(d).close() # free again once the first ends++ def test_a_download_ends_with_the_process_that_started_it(self):+ """#3257: setup killed, its child must not go on: a grandchild whose parent is gone."""+ import subprocess+ import time+ child_code = ("import sys,time; sys.path.insert(0, %r); import mtp_fetch as M; M.exit_with_parent(); "+ "print('up', flush=True); time.sleep(60)" % str(ROOT / "tools"))+ parent_code = ("import subprocess,sys,os; env=dict(os.environ, STRATA_EXIT_WITH_PARENT='1'); "+ "p=subprocess.Popen([sys.executable,'-c',%r], env=env, stdout=subprocess.PIPE, text=True); "+ "print(p.pid, flush=True); p.stdout.readline(); print('ready', flush=True); sys.stdout.flush(); os._exit(0)" % child_code)+ r = subprocess.Popen([sys.executable, "-c", parent_code], stdout=subprocess.PIPE, text=True)+ pid = int(r.stdout.readline())+ r.stdout.readline()+ r.wait(30)+ end = time.time() + 15+ alive = True+ while time.time() < end and alive:+ time.sleep(0.5)+ if os.name == "nt":+ out = subprocess.run(["tasklist", "/FI", "PID eq %d" % pid], capture_output=True, text=True).stdout+ alive = str(pid) in out+ else:+ try:+ os.kill(pid, 0)+ alive = True+ except OSError:+ alive = False+ if alive:+ if os.name == "nt":+ subprocess.run(["taskkill", "/PID", str(pid), "/F"], capture_output=True)+ else:+ os.kill(pid, 9)+ self.assertFalse(alive, "the download kept running after its parent ended")++if __name__ == "__main__":unittest.main()diff --git a/tools/test_setup_dl_fix.py b/tools/test_setup_dl_fix.pynew file mode 100644index 000000000..02ddee85c--- /dev/null+++ b/tools/test_setup_dl_fix.py@@ -0,0 +1,80 @@+"""Setup's MTP download and the Gyro low-RAM pack (#3193 #3214 #3257): the draft layer's download is run again when it stops, every tool+setup runs ends with setup, and Gyro's low-RAM pack gets --compat-bf16 like its first pack. Everything mocked; nothing downloaded.++ python -m unittest tools.test_setup_dl_fix+"""+import os+import pathlib+import sys+import unittest+from unittest import mock++ROOT = pathlib.Path(__file__).resolve().parent.parent+sys.path.insert(0, str(ROOT))+sys.path.insert(0, str(ROOT / "tools"))+import setup # noqa: E402+import test_setup_unsloth as U # noqa: E402+++class FetchMtp(unittest.TestCase):+ def run_with(self, codes):+ calls = []++ def fake_run(cmd, cwd=None, env=None, check=True, quiet=False):+ calls.append((cmd, env, check))+ return mock.Mock(returncode=codes[min(len(calls) - 1, len(codes) - 1)])+ with mock.patch.object(setup, "run", fake_run), mock.patch.object(setup.time, "sleep", lambda s: None), \+ mock.patch.object(setup, "say", lambda *a, **k: None), mock.patch.object(setup, "warn", lambda *a, **k: None):+ try:+ setup.fetch_mtp(pathlib.Path("mtp"), {"A": "1"})+ result = "ok"+ except SystemExit as e:+ result = e.code+ return result, calls++ def test_a_stopped_download_is_run_again_and_continues(self):+ result, calls = self.run_with([1, 1, 0])+ self.assertEqual((result, len(calls)), ("ok", 3))+ self.assertTrue(all(c[0][2] == "fetch" and c[1]["A"] == "1" and c[2] is False for c in calls))++ def test_a_download_that_never_works_ends_setup_with_a_hint(self):+ out = []+ with mock.patch.object(setup, "fail", lambda msg, hint=None: out.append((msg, hint)) or (_ for _ in ()).throw(SystemExit(1))):+ result, calls = self.run_with([1])+ self.assertEqual((result, len(calls)), (1, 3))+ self.assertIn("modelscope", out[0][1])++ def test_the_source_reaches_the_fetch(self):+ with mock.patch.object(setup, "model_source", lambda: "modelscope"):+ _, calls = self.run_with([0])+ self.assertEqual(calls[0][1]["STRATA_SOURCE"], "modelscope")+++class RunEndsWithSetup(unittest.TestCase):+ def test_tools_are_told_to_end_when_setup_ends(self):+ seen = {}++ def fake_subprocess_run(cmd, **kw):+ seen.update(kw)+ return mock.Mock(returncode=0)+ with mock.patch.object(setup.subprocess, "run", fake_subprocess_run), mock.patch.object(setup, "say", lambda *a, **k: None), \+ mock.patch.object(setup.acquisition, "require", lambda *a, **k: None):+ setup.run([sys.executable, "x.py"])+ self.assertEqual(seen["env"]["STRATA_EXIT_WITH_PARENT"], "1")+ self.assertIn("PATH", seen["env"] if os.name != "nt" else {k.upper(): v for k, v in seen["env"].items()})+ setup.run([sys.executable, "x.py"], env={"B": "2", "STRATA_EXIT_WITH_PARENT": "0"})+ self.assertEqual(seen["env"]["STRATA_EXIT_WITH_PARENT"], "0", "a value the caller set is kept")+ self.assertEqual(seen["env"]["B"], "2")+++class GyroLowRam(U.Base):+ def test_the_low_ram_pack_gets_compat_bf16_like_the_first_pack(self):+ code, out, _ = self.main(["--family", "gyro", "--low-ram", "on", "--context", "8192"], m="Gyro-S", family=False, ram=31.9, version="0.1.45.1")+ packs = [r for r in self.runs if any(x.endswith("iq_pack.py") for x in r)]+ self.assertTrue(packs, (code, out[-1500:]))+ self.assertTrue(all("--compat-bf16" in r for r in packs), packs)+ self.assertTrue(any("--experts-bin" in r for r in packs), "the low-RAM pack ran")+++if __name__ == "__main__":+ unittest.main()diff --git a/tools/test_strata_mcp.py b/tools/test_strata_mcp.pyindex 1bcb31d1c..68aafaa17 100644--- a/tools/test_strata_mcp.py+++ b/tools/test_strata_mcp.py@@ -893,3 +893,58 @@ class DiskUsageTest(unittest.TestCase):self.blob(self.own / "Model-00003-of-00003.gguf", 10)desc = s.describe_config(s.configs()[0])self.assertEqual([m for m in desc["missing_files"] if m.endswith(".gguf")], [])+++class DraftLayerAndCancel(unittest.TestCase):+ """#3257: the draft layer's download shows real progress, and a cancel ends the whole tree."""++ def test_step_6_shows_the_draft_layer_download_added_to_the_finished_model_files(self):+ lines = ["=== Step 6: preparing the model for Strata ===", " > python mtp_fetch.py fetch",+ " MTP draft layer: 2.00 / 5.00 GB (40%)"]+ with tempfile.TemporaryDirectory() as d:+ shard = Path(d) / "m-00001-of-00001.gguf"+ shard.write_bytes(b"x" * 20_000_000)+ job = {"files": [{"path": str(shard), "bytes": None, "role": "model"}], "total_gb": 60}+ prog = M.combined_progress(M.install_progress(lines), job)+ self.assertEqual(prog["download"]["file"], "Speed-up layer")+ self.assertEqual([p["kind"] for p in prog["parts"]], ["model", "draft"])+ self.assertEqual(prog["parts"][0]["state"], "done")+ self.assertAlmostEqual(prog["parts"][1]["done_gb"], 2.0)+ self.assertAlmostEqual(prog["parts"][1]["total_gb"], 5.0)+ self.assertGreater(prog["download"]["total_gb"], 5.0)+ self.assertEqual(prog["download"]["percent"], min(99, int(100 * prog["download"]["done_gb"] / prog["download"]["total_gb"])))++ def test_without_a_file_list_the_draft_layer_alone_is_shown(self):+ prog = M.combined_progress(M.install_progress(["=== Step 6: x ===", " MTP draft layer: 5.00 / 5.00 GB (100%)"]), {})+ self.assertEqual([(p["kind"], p["state"]) for p in prog["parts"]], [("draft", "done")])++ def test_other_step_6_output_still_shows_no_download(self):+ prog = M.combined_progress(M.install_progress(["=== Step 6: x ===", " IQ2_XS: 0.00 / 0.00 GB (0%)"]), {})+ self.assertNotIn("download", prog)++ @unittest.skipUnless(M.WIN, "Windows process trees")+ def test_cancel_ends_a_grandchild_even_when_taskkill_does_not_reach_it(self):+ """setup.py -> tools/*.py: taskkill /T walks parent ids; a snapshot taken first still finds the grandchild."""+ code = ("import subprocess,sys,time; c=subprocess.Popen([sys.executable,'-c','import time; time.sleep(120)']);"+ "print(c.pid, flush=True); time.sleep(120)")+ top = subprocess.Popen([sys.executable, "-c", code], stdout=subprocess.PIPE, text=True)+ grand = int(top.stdout.readline())+ try:+ ident, gident = M.proc_identity(top.pid), M.proc_identity(grand)+ self.assertTrue(ident and gident)+ self.assertIn((grand, gident), M.proc_tree_snapshot(top.pid))+ real_run = subprocess.run++ def no_taskkill(cmd, *a, **kw): # the case of #3257: taskkill ends the parent only+ if cmd and cmd[0] == "taskkill":+ return real_run(["taskkill", "/PID", str(top.pid), "/F"], capture_output=True)+ return real_run(cmd, *a, **kw)+ with mock.patch.object(M.subprocess, "run", no_taskkill):+ M.proc_kill_tree(top.pid, ident)+ M.wait_gone(grand, gident, 10)+ self.assertFalse(M.proc_alive(grand, gident), "the grandchild kept running")+ self.assertFalse(M.proc_alive(top.pid, ident))+ finally:+ for pid in (grand, top.pid):+ subprocess.run(["taskkill", "/PID", str(pid), "/T", "/F"], capture_output=True)+ top.wait(10)