okami.mom/strata

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

Niko1221Merge f-ds-out ac306afb2 into f-own-intaff3a132f

main
5.2 KiB92 linesraw
1"""The serving profile of a four-card AMD box: 4 x Radeon AI PRO R9700 (32 GB, gfx1201) running Qwen3.8-Flash-Next IQ3_S.
2
3Measured on that box (docs/BATCHING.md, "Four R9700 cards: the serving profile"): the cards hold the whole model, so what limits
4them is how many requests decode together and how the four stages are fed, not memory.  The profile sets
5
6* 32 slots in 4 pipelined groups (one group per card), instead of the launcher's 4 requests at a time;
7* the layer split 13,26,39: the output head costs about four layers on the last card, so it gets 9 layers and the others 13;
8* a 2048-token prompt chunk: a smaller chunk reads long prompts faster beside the decodes of the other slots;
9* the three batch switches STRATA_QSA_SLOTS, STRATA_BATCH_SELF_COMMIT and STRATA_SLOT_COPY_DIRECT, which change how the work is
10  scheduled and copied, not what is computed (tools/batch_test.py: every slot decodes the tokens it decodes alone).
11
12Recommend, never force: a setting the person made (the config, `--parallel`, `--layer-split`, an advanced flag, an environment
13variable) always wins, the profile only fills what is not set, and `STRATA_FOUR_CARD_PROFILE=0` (or "four_card_profile": false in
14the run config) turns it off.  Only this hardware class is touched; every other computer gets exactly what it got before.
15
16Pure functions, no I/O (the callers pass the cards and the run config).
17"""
18
19from __future__ import annotations
20
21import os
22
23PARALLEL = 32                     # slots (requests decoding together)
24GROUPS = 4                        # pipelined groups: one per card
25LAYER_SPLIT = "13,26,39"          # Qwen3.8-Flash-Next has 48 layers: 13 / 13 / 13 / 9 (the output head sits on the last card)
26PREFILL_CHUNK = "2048"
27BATCH_ENV = {"STRATA_QSA_SLOTS": "1", "STRATA_BATCH_SELF_COMMIT": "1", "STRATA_SLOT_COPY_DIRECT": "1"}
28PROMPT_CACHE_OFF = False          # --prompt-cache 0 is not part of the default: see docs/BATCHING.md
29MIN_CARD_GB = 28.0                # a 32 GB card reports a little less than its name
30CARDS = 4
31OFF_VALUES = ("0", "off", "no", "false")
32
33
34def disabled(cfg: dict | None = None) -> bool:
35    """The person turned the profile off: STRATA_FOUR_CARD_PROFILE=0 in the environment, or "four_card_profile": false in the config."""
36    if str(os.environ.get("STRATA_FOUR_CARD_PROFILE", "")).strip().lower() in OFF_VALUES:
37        return True
38    return bool(cfg) and cfg.get("four_card_profile") is False
39
40
41def card_class(cards: list[dict]) -> bool:
42    """Exactly four AMD cards of one kind, each with at least MIN_CARD_GB: the Radeon AI PRO R9700 (the 32 GB gfx12 card this was
43    measured on)."""
44    if len(cards) != CARDS:
45        return False
46    names = {str(c.get("name") or "") for c in cards}
47    if len(names) != 1 or "R9700" not in next(iter(names)).upper().replace(" ", ""):
48        return False
49    return all(str(c.get("vendor") or "amd").lower() == "amd" and float(c.get("vram_gb") or 0.0) >= MIN_CARD_GB for c in cards)
50
51
52def model_ok(cfg: dict) -> bool:
53    """The measured model: Qwen3.8-Flash-Next at IQ3_S (48 layers; the pack that fits whole on four 32 GB cards)."""
54    text = " ".join([str(cfg.get("model_name") or "")] + [str(a) for a in cfg.get("args") or []]).lower().replace("-", "_").replace(".", "_")
55    return "qwen3_8" in text and "iq3_s" in text
56
57
58def applies(cards: list[dict], cfg: dict) -> bool:
59    args = [str(a) for a in cfg.get("args") or []]
60    return (not disabled(cfg)) and card_class(cards) and cfg.get("backend", "hip") == "hip" and model_ok(cfg) \
61        and "--peer-device" not in args and not cfg.get("replicas")
62
63
64def apply(cards: list[dict], cfg: dict, *, parallel_given: bool = False) -> list[str]:
65    """Fill the profile into the run config `cfg` (in place) where the person has not chosen; returns the plain lines to show.
66    `parallel_given`: the person named a number of requests (--parallel): then only the split, the chunk and the switches are
67    filled (the engine groups the slots itself)."""
68    if not applies(cards, cfg):
69        return []
70    args = [str(a) for a in cfg.get("args") or []]
71    env = dict(cfg.get("env") or {})
72    notes: list[str] = []
73    if str(cfg.get("layer_split") or "auto") == "auto" and "--layer-split" not in args:
74        cfg["layer_split"] = LAYER_SPLIT
75        notes.append(f"layer split {LAYER_SPLIT}: the last card also holds the output head, so it takes fewer layers")
76    if "--prefill" not in args:
77        args.extend(["--prefill", PREFILL_CHUNK])
78    elif args[args.index("--prefill") + 1:args.index("--prefill") + 2] == ["auto"]:   # "auto" is what setup writes, not a choice
79        args[args.index("--prefill") + 1] = PREFILL_CHUNK
80    if PROMPT_CACHE_OFF and "--prompt-cache" not in args:
81        args.extend(["--prompt-cache", "0"])
82    for k, v in BATCH_ENV.items():
83        if k not in os.environ:                                  # an environment variable of the person's own wins
84            env.setdefault(k, v)
85    if not parallel_given and "parallel" not in cfg and not {"--batch", "--slots"} & set(args):
86        cfg["parallel"] = PARALLEL
87        if "--batch-groups" not in args:
88            args.extend(["--batch-groups", str(GROUPS)])
89        notes.append(f"{PARALLEL} requests at once in {GROUPS} groups, one group per card (--parallel N changes it)")
90    cfg["args"] = args
91    cfg["env"] = env
92    return notes