okami.mom/strata

git clone https://git.nkp.pet/okami.mom/strata

Niko1221Merge branch 'f-021-settings' into f-021-int40bf5f5c4

main
2.8 KiB56 linesraw
1"""The draft layer (MTP) switch of a model's run config.
2
3``"mtp": false`` in a model's run config (strata-<model>.json) starts the engine without the draft layer.  The key is the
4person's: setup keeps it when it writes the file again (setup.carry_over), the app's Models settings and `strata launch --mtp off`
5write it, and every start applies it (serve/server.py engine_args), so a setup run that puts ``--mtp <folder>`` back into
6the engine arguments cannot bring the draft layer back.
7
8What the draft layer costs, in plain words: about 1 GB of graphics memory.  Its weights are the files in the model data
9folder's ``mtp/rt`` (0.7 GB of experts and 0.1 GB of dense weights, 786 MiB), the engine sets aside another 218 MiB for its
10draft head on a 12 GB card ("expert cache auto: ... +218 MiB for the draft head"), and its own conversation memory grows with
11the context (with a pinned copy of that in RAM when the context streams).  The expert cache gets what is left, so every GB here
12is a GB less of cached experts.  It buys speed (about twice as many words per second on a
13card that holds the model) and costs that room in the expert cache, so on a small card or a PC with little RAM the saving can be
14worth more than the speed.
15
16Without it the engine still guesses ahead from the text already in the conversation (``--spec`` with prompt lookup), which is
17why ``--spec`` stays and only the draft layer's own options go: ``--mtp``, ``--spec-min-p`` (the draft layer's confidence
18threshold) and ``--batch-mtp`` (the draft layer per request).
19"""
20
21from __future__ import annotations
22
23from typing import Any
24
25#: engine options that exist only because of the draft layer: flag -> takes a value
26MTP_FLAGS = {"--mtp": True, "--spec-min-p": True, "--batch-mtp": False}
27
28
29def is_off(cfg: dict[str, Any] | None) -> bool:
30    """True when the run config says the draft layer is off (only an explicit ``false``; absent or true: on)."""
31    return isinstance(cfg, dict) and cfg.get("mtp") is False
32
33
34def strip(args: list[Any]) -> tuple[list[str], list[str]]:
35    """(the arguments without the draft layer's options, the flags that were removed)."""
36    out: list[str] = []
37    removed: list[str] = []
38    items = [str(a) for a in args]
39    i = 0
40    while i < len(items):
41        a = items[i]
42        if a in MTP_FLAGS:
43            removed.append(a)
44            i += 2 if MTP_FLAGS[a] and i + 1 < len(items) and not items[i + 1].startswith("--") else 1
45            continue
46        out.append(a)
47        i += 1
48    return out, removed
49
50
51def apply(cfg: dict[str, Any]) -> list[str]:
52    """Takes the draft layer's options out of ``cfg["args"]`` when the config has it off; returns the flags removed."""
53    if not is_off(cfg) or not isinstance(cfg.get("args"), list):
54        return []
55    cfg["args"], removed = strip(cfg["args"])
56    return removed