diff --git a/README.md b/README.md index 05226068..77f5e0d4 100644 --- a/README.md +++ b/README.md @@ -84,9 +84,17 @@ App). Everything else - Python, the engine, the model - is set up for you. Then it downloads everything (the model is ~70 GB, so the first time takes a while - you can stop and it picks up where it left off) and **starts the model**. Your browser opens the Strata app at `http://127.0.0.1:8080`. +> **While the model starts, your PC can be slow or stop responding for 1-3 minutes** (longest the first time): Strata +> loads 35-55 GB into your RAM and locks part of it for the graphics card. That's normal - wait, and don't close the +> window. The window tells you what it is doing. + **Next time**, just double-click `START-HERE.bat` again: it starts right away, nothing is downloaded twice. Close its window to stop the model. +**Updating:** download the new version and unzip it anywhere (or `git pull`), then run `START-HERE.bat` in it. The +model files are kept in a `Strata-data` folder next to your Strata folder, so a new copy finds them and sets itself up +the same way - nothing big is downloaded again. + **Linux:** run `./setup.sh` - same questions, same result. ## Using it @@ -113,8 +121,8 @@ window to stop the model. ## Something went wrong? **My PC froze, or got very slow, the first time Strata started.** -That's normal the first time. Strata loads 35-55 GB into your RAM, locks part of it for the graphics card, and works -out how much of the model fits on your GPU. The mouse can freeze for a few minutes. **Wait, and don't close the +That's normal while it starts, most of all the first time. Strata loads 35-55 GB into your RAM, locks part of it for +the graphics card, and works out how much of the model fits on your GPU. The mouse can freeze for a few minutes. **Wait, and don't close the window.** The next starts are much faster. Still frozen after 10 minutes? Restart the PC, close other programs (browsers use a lot of RAM) and try again. If it keeps happening, pick a smaller size (Q2_0 or IQ2_XS). diff --git a/docs/DETAILS.md b/docs/DETAILS.md index 1c65629c..ba50fcd6 100644 --- a/docs/DETAILS.md +++ b/docs/DETAILS.md @@ -134,7 +134,11 @@ You need **only an NVIDIA driver** (version 580 or newer; update it with the NVI | Disk | ~70-80 GB free for the model, ~6 GB for the MTP layer (+1 GB with images). **Q2_0 on an AVX-512 CPU** also writes a one-time ~40 GB copy of its experts for the fast CPU kernel. An NVMe SSD is strongly recommended. | | OS | Windows 10/11, or Linux (Ubuntu 22.04/24.04 get everything installed automatically). | -What the first start installs, all inside this folder (`.venv/`, `engine/`, `third_party/`, `models/`, `packs/`, `mtp/`): +What the first start installs: in this folder `.venv/`, `engine/` and `third_party/`; the model files (`models/`, +`packs/`, `mtp/`, 70-120 GB) in **`Strata-data` next to this folder**, so a new copy of Strata (an update unzipped +elsewhere) finds them and sets itself up the same way. The place is remembered per user (`%APPDATA%\Strata\settings.json`, +`~/.config/strata/settings.json`); `--data-dir` chooses another. Installs from before 0.1.16 are moved there by the next +start (a rename on the same drive; files on another drive are used where they are). Python 3.12 if you have none (for your user account, no admin), a private Python environment, NVIDIA's CUDA libraries (from pip, ~0.4 GB), the ready-made Strata engine for RTX 30/40/50, the model and the MTP draft layer. If no ready-made engine fits your PC, it offers to install the build tools (Visual Studio Build Tools + CUDA Toolkit on @@ -172,6 +176,7 @@ downloaded again. Closing the window stops the model. START-HERE.bat --setup install another model, or change context / images START-HERE.bat --model IQ2_XS --context 32768 --vision yes --yes no questions START-HERE.bat --gguf-dir D:\models\IQ2_XS use GGUF files you already have +START-HERE.bat --data-dir E:\Strata-data keep the model files somewhere else START-HERE.bat --port 8081 another port ``` @@ -405,7 +410,7 @@ the document, +0.4% on the chat. Details: `bench/results/2026-09-27-esp/`. | `port 8080 is already in use` | Strata is already running (look for its window), or another program uses the port: `START-HERE.bat --port 8081`. | | `cudaHostRegister ... out of memory` in the log | Normal on Windows: the engine pins the experts in per-layer slices instead. Only a problem if the load then fails. | | The first start takes minutes | It is reading 34-55 GB into RAM; the second start is faster while the files are in the OS cache. | -| The PC freezes for a few minutes at the first start | Normal the first time: the engine loads the experts into RAM, pins part of it for the GPU and sizes the expert cache. Wait; don't close the window. Still frozen after 10 minutes: restart the PC, close other programs, try again, or pick a smaller size. | +| The PC freezes for a few minutes at the start | Normal, most of all the first time (the server window says when it happens): the engine loads the experts into RAM, pins part of it for the GPU and sizes the expert cache. Wait; don't close the window. Still frozen after 10 minutes: restart the PC, close other programs, try again, or pick a smaller size. | | `the engine stopped unexpectedly (exit code ...)` | The engine process ended mid-answer - usually out of RAM (Linux ends the biggest program: `sudo dmesg \| grep -i -E 'killed process\|out of memory'`). The next request starts it again by itself. If it repeats: close other programs or pick a smaller size. The server also warns at start when the model's experts leave less than ~6 GB of RAM for everything else. | | Slow output, disk light busy | Not enough free RAM: close other programs, or choose Q2_0 / IQ2_XS. | | `prompt ... exceeds the context` | The request is longer than the context you chose: run setup again with a bigger `--context`. | diff --git a/serve/server.py b/serve/server.py index 3e95fb4c..8d795aeb 100644 --- a/serve/server.py +++ b/serve/server.py @@ -76,6 +76,57 @@ class EngineDied(RuntimeError): """The engine process ended in the middle of a request (issue #27: on Linux, the out-of-memory killer).""" +def narrate_start(log_path: str, offset: int, args: list, done: threading.Event, heartbeat=20.0) -> None: + """While the engine starts, say in the server window what it is doing, from its log: the start reads tens of GB + into RAM and locks part of it for the GPU, and on many PCs everything is slow or frozen for a minute or more - + people closed the window thinking it had hung. The warning comes at that step, not after it.""" + gb = 0.0 + if "--native" in args: # about the size of the experts it will read + try: + gb = os.path.getsize(args[args.index("--native") + 1]) / 1e9 + except (OSError, IndexError): + pass + size = f"about {gb:.0f} GB" if gb >= 1 else "tens of GB" + t0 = last = time.time() + said = set() + + def say(key, text): + nonlocal last + if key not in said: + said.add(key) + last = time.time() + print(text, flush=True) + + say("weights", "[strata] starting the engine: reading the model's weights ...") + pos = offset + while not done.wait(0.5): + try: + with open(log_path, "rb") as f: + f.seek(pos) + chunk = f.read() + except OSError: + chunk = b"" + if chunk.count(b"\n"): + cut = chunk.rfind(b"\n") + 1 + pos += cut + for line in chunk[:cut].decode("utf-8", "replace").splitlines(): + if "PLE on" in line or "expert arena:" in line: + say("arena", f"[strata] loading the experts into RAM ({size}) and locking part of them for the GPU.\n" + " YOUR PC CAN BE SLOW OR STOP RESPONDING FOR 1-3 MINUTES NOW - this is normal.\n" + " Please wait and don't close this window; the browser opens when it is ready.") + elif " loaded " in line and "GiB at" in line: + say("loaded", "[strata] experts loaded: " + line.split(" loaded ", 1)[1].strip() + + f" ({time.time() - t0:.0f} s so far)") + elif "expert cache " in line and " slots, " in line and "auto" not in line: + n = line.split("expert cache ", 1)[1].split(";")[0].replace(" slots,", " experts,").strip() + say("cache", f"[strata] filling the GPU's expert cache ({n}) ...") + elif "session is up" in line: + say("up", "[strata] almost ready ...") + if time.time() - last > heartbeat: + last = time.time() + print(f"[strata] still starting ({time.time() - t0:.0f} s) - please wait ...", flush=True) + + class StrataEngine: """The resident engine: `strata --serve` reads `GEN ` lines and streams `T ` lines, then `DONE ...`. Requests are serialized by the service's FIFO, so one pipe is enough. @@ -90,6 +141,10 @@ class StrataEngine: self.spawn = (exe, list(args), cwd, log, env) # to start it again after it died (issue #27) self.log_path = log self.log = open(log, "a", encoding="utf-8") if log else subprocess.DEVNULL + loading = threading.Event() # set once READY: the narrator below stops + if log: + threading.Thread(target=narrate_start, args=(log, os.path.getsize(log), args, loading), + daemon=True).start() self.proc = subprocess.Popen([exe, "--serve", *args], cwd=cwd, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=self.log, text=True, encoding="utf-8", bufsize=1, env=env) self.max_context = 0 @@ -111,6 +166,7 @@ class StrataEngine: self.max_context = int(f[1]) self.can_stop = "stop" in f[2:] break + loading.set() if self.max_context <= 0: raise RuntimeError("the engine exited before it was ready" + (f" (see {log})" if log else "")) # the engine's stdout on a thread, so a request can wait with a timeout (heartbeats, cancel checks) diff --git a/setup.py b/setup.py index e0d38752..291804ee 100644 --- a/setup.py +++ b/setup.py @@ -654,6 +654,203 @@ def build_engine(gpu, vision, yes, llama) -> Path: return eng +# ------------------------------------------------------------------------------------------------ the data folder +# The model files - the GGUFs, the prepared packs and the MTP layer, 70-120 GB - live in a data folder NEXT TO the +# Strata folder (`Strata-data`), not inside it: updating Strata by unzipping a new copy used to give a new, empty +# folder and a full download again. Where it is, and which Strata folders this user ran, is kept in a small +# per-user file, so every Strata folder on the PC finds the same files. +DATA_ITEMS = ("models", "packs", "mtp") + + +def settings_path() -> Path: + if WIN: + return Path(os.environ.get("APPDATA") or Path.home() / "AppData" / "Roaming") / "Strata" / "settings.json" + return Path(os.environ.get("XDG_CONFIG_HOME") or Path.home() / ".config") / "strata" / "settings.json" + + +def load_settings() -> dict: + try: + return json.loads(settings_path().read_text(encoding="utf-8")) + except (OSError, ValueError): + return {} + + +def save_settings(s: dict) -> None: + try: + settings_path().parent.mkdir(parents=True, exist_ok=True) + settings_path().write_text(json.dumps(s, indent=1), encoding="utf-8") + except OSError as e: + warn(f"could not save {settings_path()} ({e})") + + +def has_data(folder: Path) -> bool: + for d in DATA_ITEMS: + try: + if (folder / d).is_dir() and any((folder / d).iterdir()): + return True + except OSError: + pass + return False + + +def other_installs(settings: dict) -> list: + """Strata folders besides this one that may hold model files: the ones this user ran before, and Strata* folders + next to this one (a zip unpacked again lands in e.g. `Strata-main (1)\\Strata-main`).""" + cands = [Path(p) for p in settings.get("installs", [])] + for base in dict.fromkeys((ROOT.parent, ROOT.parent.parent)): + try: + for d in base.iterdir(): + if d.is_dir() and d.name.lower().startswith("strata"): + cands.append(d) + cands += [c for c in d.iterdir() if c.is_dir() and c.name.lower().startswith("strata")] + except OSError: + pass + found = [] + for d in cands: + try: + d = d.resolve() + if d != ROOT and d not in found and (d / "setup.py").is_file(): + found.append(d) + except OSError: + pass + return found + + +def same_drive(a: Path, b: Path) -> bool: + try: + return os.stat(a).st_dev == os.stat(b).st_dev + except OSError: + return False + + +def move_into(src: Path, dst: Path) -> None: + """A rename into the data folder (same drive: instant); a folder merges into one already there, keeping what the + destination has. Whatever cannot be moved (a file in use) stays where it is.""" + if not dst.exists(): + try: + dst.parent.mkdir(parents=True, exist_ok=True) + os.replace(src, dst) + return + except OSError: + if not src.is_dir(): + return + dst.mkdir(parents=True, exist_ok=True) + if src.is_dir() and dst.is_dir(): + for c in list(src.iterdir()): + move_into(c, dst / c.name) + try: + src.rmdir() + except OSError: + pass + + +def repoint_config(cfg_file: Path, old: Path, new: Path) -> None: + """A config whose model files moved from `old` to `new` points at them there (each path only if its file is + now there and no longer at the old place).""" + try: + cfg = json.loads(cfg_file.read_text(encoding="utf-8-sig")) + except (OSError, ValueError): + return + + def fix(v): + if isinstance(v, list): + return [fix(x) for x in v] + if isinstance(v, dict): + return {k: fix(x) for k, x in v.items()} + if isinstance(v, str): + for d in DATA_ITEMS: + o = str(old / d) + nv, no = os.path.normcase(v), os.path.normcase(o) # Windows: C:\ and c:\ are the same place + if nv == no or nv.startswith(no + os.sep): + n = str(new / d) + v[len(o):] + if Path(n).exists() and not Path(v).exists(): + return n + return v + + new_cfg = fix(cfg) + if new_cfg != cfg: + cfg_file.write_text(json.dumps(new_cfg, indent=1), encoding="utf-8") + + +def data_folder(requested: str | None) -> tuple: + """(the data folder, folders on other drives that still hold model files). Moves the model files of this folder + and of earlier Strata folders on the same drive into the data folder, and points their configs there.""" + settings = load_settings() + dest = Path(requested).expanduser().resolve() if requested else \ + Path(settings["data_dir"]) if settings.get("data_dir") else ROOT.parent / "Strata-data" + try: + dest.mkdir(parents=True, exist_ok=True) + except OSError as e: # e.g. no write access next to the Strata folder + warn(f"cannot use {dest} for the model files ({e}): keeping them in {ROOT}") + dest = ROOT + elsewhere = [] + for folder in [ROOT, *other_installs(settings)]: + if folder == dest or not has_data(folder): + continue + if not same_drive(folder, dest): + elsewhere.append(folder) # another drive: used where it is (no 70 GB copy) + continue + # the downloads merge file by file (the same file wherever it came from); a prepared pack or MTP layer moves + # whole or not at all, so two copies are never mixed + if (folder / "models").is_dir(): + move_into(folder / "models", dest / "models") + for item in [*((folder / "packs").glob("*") if (folder / "packs").is_dir() else []), folder / "mtp"]: + rel = item.relative_to(folder) + if item.exists() and not (dest / rel).exists(): + move_into(item, dest / rel) + for d in ("packs",): + try: + (folder / d).rmdir() # empty now + except OSError: + pass + for c in folder.glob("strata-*.json"): + repoint_config(c, folder, dest) + if has_data(folder): + elsewhere.append(folder) # in use, or a copy the data folder already has + warn(f"some model files are still in {folder} (in use, or already in {dest})") + else: + ok(f"model files from {folder} moved to {dest} (a new copy of Strata finds them there)") + installs = [str(ROOT)] + [p for p in settings.get("installs", []) if p != str(ROOT) and Path(p).is_dir()] + save_settings({**settings, "data_dir": str(dest), "installs": installs[:20]}) + return dest, elsewhere + + +def previous_config(elsewhere_first: list, settings: dict): + """The most recently used model config of another Strata folder on this PC, for a folder that has none yet.""" + cands = [] + for folder in [*elsewhere_first, *other_installs(settings)]: + cands += list(folder.glob("strata-*.json")) + cands = [c for c in dict.fromkeys(cands) if c.is_file()] + return max(cands, key=lambda p: p.stat().st_mtime) if cands else None + + +def choices_from_config(cfg_path: Path) -> dict: + """The setup answers a config was written with (family, size, context, KV, images, projection, network).""" + cfg = json.loads(cfg_path.read_text(encoding="utf-8-sig")) + tag = cfg_path.stem[len("strata-"):] + family = "swift" if tag.startswith("swift-") else "qwen" + model = tag.split("-")[-1].upper() + a = cfg.get("args", []) + val = lambda k: a[a.index(k) + 1] if k in a and a.index(k) + 1 < len(a) else None # noqa: E731 + vis = cfg.get("vision") + esp = val("--control-vector-scaled") + esp_path = esp.rsplit(":", 1)[0] if esp else None + return {"family": family, "model": model if model in MODELS else None, + "context": int(val("--max-context")) if val("--max-context") else None, + "kv": val("--kv") if val("--kv") in ("int8", "q4_0") else None, + "vision": ("gpu" if vis.get("gpu") else "cpu") if isinstance(vis, dict) else "none", + "esp": ("on" if Path(esp_path).name == ESP_VECTOR.name else esp_path) if esp_path else "off", + "host": cfg.get("host"), "api_key": cfg.get("api_key"), "port": cfg.get("port")} + + +def find_in(roots: list, rel: str): + """The first of roots/rel that exists.""" + for r in roots: + if (r / rel).exists(): + return r / rel + return None + + # ------------------------------------------------------------------------------------------------ start def installed_configs(): return sorted(ROOT.glob("strata-*.json"), key=lambda p: p.stat().st_mtime, reverse=True) @@ -722,8 +919,20 @@ def start(cfg_path: Path, port: int | None, open_browser=True) -> int: "--port", str(port or cfg.get("port", 8080))] if open_browser: cmd.append("--open") + gb = 0.0 + if "--native" in cfg["args"]: + try: + gb = Path(cfg["args"][cfg["args"].index("--native") + 1]).stat().st_size / 1e9 + except (OSError, IndexError): + pass say() - say(f"Starting {cfg.get('model_name', 'the model')} (loads 34-43 GB into RAM: 30-90 s). Close this window to stop it.") + say(" " + "-" * 100) + say(f" Starting {cfg.get('model_name', 'the model')}: it loads {f'about {gb:.0f} GB' if gb >= 1 else '34-55 GB'} " + "into RAM and locks part of it for the GPU.") + say(" While it does, YOUR PC CAN BE SLOW OR STOP RESPONDING FOR 1-3 MINUTES (longer the first time after a") + say(" restart). That is normal: please wait and don't close this window - the browser opens when it is ready.") + say(" Later, closing this window stops the model.") + say(" " + "-" * 100) return subprocess.call(cmd) @@ -760,7 +969,9 @@ def main() -> int: ap.add_argument("--host", help="where the server listens: 127.0.0.1 = this PC only (default), 0.0.0.0 = also other " "devices on your network (issue #26; set --api-key too)") ap.add_argument("--api-key", help="require this key from clients (recommended with --host 0.0.0.0)") - ap.add_argument("--models-dir", default=str(ROOT / "models"), help="where the GGUF files go (~70 GB)") + ap.add_argument("--data-dir", help="where the model files go (~70-120 GB): default Strata-data next to this folder, " + "remembered for every Strata folder on this PC") + ap.add_argument("--models-dir", help="where the GGUF files go (default: /models)") ap.add_argument("--gguf-dir", help="use GGUF files you already have (a folder with the two shards)") ap.add_argument("--yes", action="store_true", help="accept the recommended answers") ap.add_argument("--setup", action="store_true", help="install another model or change settings") @@ -771,12 +982,30 @@ def main() -> int: ap.add_argument("--check", action="store_true", help="only check this PC and exit") ap.add_argument("--skip-build", action="store_true", help=argparse.SUPPRESS) a = ap.parse_args() - port = a.port or 8080 # a new install's port (issue #32: --port for an existing one) - say("Strata - Qwen3.8-Flash-Next on a normal PC (NVIDIA GPU + system RAM + CPU)") + data, elsewhere = data_folder(a.data_dir) # the model files: in the data folder, found from any copy + roots = [data, *elsewhere] + if a.models_dir is None: + a.models_dir = str(data / "models") # ---- 0. already installed: just start it have = installed_configs() + explicit = a.setup or a.model or a.family or a.check or a.no_start + if not have and not explicit: # a new copy of Strata (an update unzipped elsewhere): set it + prev = previous_config(elsewhere, load_settings()) # up like the last one, from the files already here + if prev is not None: + ch = choices_from_config(prev) + if ch["model"]: + say(f" Found your earlier install in {prev.parent} ({prev.stem[len('strata-'):]}): setting up this " + "copy the same way - the model files are reused, nothing big is downloaded.") + a.family, a.model, a.context = ch["family"], ch["model"], a.context or ch["context"] + a.kv = a.kv or ch["kv"] + a.vision = a.vision or ch["vision"] + a.experimental_speed_projection = a.experimental_speed_projection or ch["esp"] + a.host, a.api_key = a.host or ch["host"], a.api_key or ch["api_key"] + a.port = a.port or ch["port"] + a.yes = True + port = a.port or 8080 # a new install's port (issue #32: --port for an existing one) if have and not (a.setup or a.model or a.family or a.check or a.no_start): if not a.build: update_installed_engine(a.prebuilt) @@ -910,6 +1139,13 @@ def main() -> int: warn("the experimental speed projection is made for the original Qwen3.8-Flash-Next, not Swift 1.5: left off") models_dir = Path(a.gguf_dir) if a.gguf_dir else Path(a.models_dir) / tag shards = [models_dir / fam["file"].format(q=model, i=i) for i in (1, 2)] + if not a.gguf_dir and not all(sh.exists() and done(sh) for sh in shards): + for r in elsewhere: # already downloaded in a Strata folder on another drive + cand = [r / "models" / tag / sh.name for sh in shards] + if all(c.exists() and done(c) for c in cand): + models_dir, shards = cand[0].parent, cand + ok(f"model files found in {models_dir}") + break have_model = all(s.exists() and (done(s) or a.gguf_dir) for s in shards) need = (0 if a.gguf_dir or have_model else MODELS[model]["download_gb"]) + 8 + \ (40 if model == "Q2_0" and avx512 and family == "qwen" else 0) + (1 if vision != "none" else 0) @@ -959,6 +1195,8 @@ def main() -> int: fail(f"missing {s}") ok("model files present") mmproj = Path(a.models_dir) / fam["mmproj"] + if not mmproj.exists(): + mmproj = find_in(roots, f"models/{fam['mmproj']}") or mmproj if vision != "none": if not mmproj.exists() and a.gguf_dir and (Path(a.gguf_dir) / fam["mmproj"]).exists(): mmproj = Path(a.gguf_dir) / fam["mmproj"] @@ -968,7 +1206,7 @@ def main() -> int: # ---- 6. the pack and the MTP draft layer step(6, "preparing the model for Strata") - pack = ROOT / "packs" / tag.lower() + pack = find_in(roots, f"packs/{tag.lower()}") or data / "packs" / tag.lower() env = dict(os.environ, STRATA_GGUF_PY=str(llama / "gguf-py")) if model == "Q2_0" and avx512 and family == "qwen": # the Q2_0 experts repacked for the AVX-512 kernel (the measured speed): a one-time ~40 GB conversion @@ -984,7 +1222,7 @@ def main() -> int: # every tensor as the GGUF stores it; the experts are read from the GGUF at start (seconds to build) run([sys.executable, str(ROOT / "tools" / "iq_pack.py"), "--gguf", str(shards[0]), "--out", str(pack)], env=env) ok(f"model prepared: {pack}") - mtp = ROOT / "mtp" + mtp = (find_in(roots, "mtp/rt/experts.bin") or data / "mtp/rt/experts.bin").parent.parent rt = mtp / "rt" if not (rt / "experts.bin").exists(): say(" The MTP draft layer (speculative decoding, ~2x faster output) comes from the original Qwen checkpoint:")